Beispiel #1
0
        /// <summary>
        /// Convenience function to set the data using a single string notation such as "key = value".
        /// </summary>

        public void Set(string text)
        {
            if (!string.IsNullOrEmpty(text))
            {
                var parts = text.Split(new char[] { '=' }, 2);

                if (parts.Length == 2)
                {
                    var key  = parts[0].Trim();
                    var val  = parts[1].Trim();
                    var node = new DataNode(key, val);
                    if (node.ResolveValue())
                    {
                        Set(node.name, node.value);
                    }
                }
                else
                {
                    Debug.LogWarning("Invalid syntax [" + text + "]. Expected [key = value].");
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Process the string values, converting them to proper objects.
        /// Returns whether child nodes should be processed in turn.
        /// </summary>

        public bool ResolveValue(Type type = null)
        {
            if (mValue is string)
            {
                mResolved = true;
                string line = mValue as string;

                // Trim strings wrapped in quotes
                if (type == typeof(string))
                {
                    if (line == "\"\"")
                    {
                        mValue = "";
                    }
                    else
                    {
                        int len = line.Length;
                        if (len > 2 && line[0] == '"' && line[len - 1] == '"')
                        {
                            mValue = line.Substring(1, len - 2);
                        }
                    }
                    return(true);
                }

                // Try to resolve this type as a simple type
                if (Serialization.ReadObject(line, out mValue, type))
                {
                    return(true);
                }

                // This type is either a class or an array
                if (type == null)
                {
                    type = Serialization.NameToType(line);
                }

                if (type == null || type == typeof(void))
                {
                    mValue = null;
                    return(true);
                }
                else if (type.Implements(typeof(IDataNodeSerializable)))
                {
                    IDataNodeSerializable ds = (IDataNodeSerializable)type.Create();
                    ds.Deserialize(this);
                    mValue = ds;
                    return(false);
                }
#if !STANDALONE
                else if (type == typeof(AnimationCurve))
                {
                    if (children.size != 0)
                    {
                        AnimationCurve cv  = new AnimationCurve();
                        Keyframe[]     kfs = new Keyframe[children.size];

                        for (int i = 0; i < children.size; ++i)
                        {
                            DataNode child = children[i];

                            if (child.value == null)
                            {
                                child.mValue    = child.name;
                                child.mResolved = false;
                                child.ResolveValue(typeof(Vector4));

                                Vector4 v = (Vector4)child.mValue;
                                kfs[i] = new Keyframe(v.x, v.y, v.z, v.w);
                            }
                            else
                            {
                                Vector4 v = (Vector4)child.mValue;
                                kfs[i] = new Keyframe(v.x, v.y, v.z, v.w);
                            }
                        }

                        cv.keys = kfs;
                        mValue  = cv;
                        children.Clear();
                    }
                    return(false);
                }
                else if (type == typeof(LayerMask))
                {
                    mValue = (LayerMask)Get <int>();
                }
#endif
                else
#if !STANDALONE
                if (!type.IsSubclassOf(typeof(Component)))
#endif
                {
                    bool isIList = type.Implements(typeof(System.Collections.IList));
                    bool isTList = (!isIList && type.Implements(typeof(TList)));
                    mValue = (isTList || isIList) ? type.Create(children.size) : type.Create();

                    if (mValue == null)
                    {
                        Tools.LogError("Unable to create a " + type);
                        return(true);
                    }

                    if (isTList)
                    {
                        TList list     = mValue as TList;
                        Type  elemType = type.GetGenericArgument();

                        if (elemType != null)
                        {
                            for (int i = 0; i < children.size; ++i)
                            {
                                DataNode child = children[i];

                                if (child.value == null)
                                {
                                    child.mValue    = child.name;
                                    child.mResolved = false;
                                    child.ResolveValue(elemType);
                                    list.Add(child.mValue);
                                }
                                else if (child.name == "Add")
                                {
                                    child.ResolveValue(elemType);
                                    list.Add(child.mValue);
                                }
                                else
                                {
                                    Tools.LogError("Unexpected node in an array: " + child.name);
                                }
                            }
                            return(false);
                        }
                        else
                        {
                            Tools.LogError("Unable to determine the element type of " + type);
                        }
                    }
                    else if (isIList)
                    {
                        // This is for both List<Type> and Type[] arrays.
                        System.Collections.IList list = mValue as System.Collections.IList;
                        Type elemType = type.GetGenericArgument();
                        if (elemType == null)
                        {
                            elemType = type.GetElementType();
                        }
                        bool fixedSize = (list.Count == children.size);

                        if (elemType != null)
                        {
                            for (int i = 0; i < children.size; ++i)
                            {
                                DataNode child = children[i];

                                if (child.value == null)
                                {
                                    child.mValue    = child.name;
                                    child.mResolved = false;
                                    child.ResolveValue(elemType);

                                    if (fixedSize)
                                    {
                                        list[i] = child.mValue;
                                    }
                                    else
                                    {
                                        list.Add(child.mValue);
                                    }
                                }
                                else if (child.name == "Add")
                                {
                                    child.ResolveValue(elemType);
                                    if (fixedSize)
                                    {
                                        list[i] = child.mValue;
                                    }
                                    else
                                    {
                                        list.Add(child.mValue);
                                    }
                                }
                                else
                                {
                                    Tools.LogError("Unexpected node in an array: " + child.name);
                                }
                            }
                            return(false);
                        }
                        else
                        {
                            Tools.LogError("Unable to determine the element type of " + type);
                        }
                    }
                    else if (type.IsClass)
                    {
                        for (int i = 0; i < children.size; ++i)
                        {
                            DataNode child = children[i];
                            mValue.SetFieldOrPropertyValue(child.name, child.value);
                        }
                        return(false);
                    }
#if UNITY_EDITOR
                    else
                    {
                        Debug.LogError("Unhandled type: " + type);
                    }
#else
                    else
                    {
                        Tools.LogError("Unhandled type: " + type);
                    }
#endif
                }
                return(true);
            }
Beispiel #3
0
        /// <summary>
        /// Set the node's value using its text representation.
        /// Returns whether the child nodes should be processed or not.
        /// </summary>

        bool SetValue(string text, Type type, string[] parts)
        {
            if (type == null || type == typeof(void))
            {
                mValue = null;
            }
            else if (type == typeof(string))
            {
                mValue = text;
            }
            else if (type == typeof(bool))
            {
                bool b = false;
                if (bool.TryParse(text, out b))
                {
                    mValue = b;
                }
            }
            else if (type == typeof(byte))
            {
                byte b;
                if (byte.TryParse(text, out b))
                {
                    mValue = b;
                }
            }
            else if (type == typeof(Int16))
            {
                Int16 b;
                if (Int16.TryParse(text, out b))
                {
                    mValue = b;
                }
            }
            else if (type == typeof(UInt16))
            {
                UInt16 b;
                if (UInt16.TryParse(text, out b))
                {
                    mValue = b;
                }
            }
            else if (type == typeof(Int32))
            {
                Int32 b;
                if (Int32.TryParse(text, out b))
                {
                    mValue = b;
                }
            }
            else if (type == typeof(UInt32))
            {
                UInt32 b;
                if (UInt32.TryParse(text, out b))
                {
                    mValue = b;
                }
            }
            else if (type == typeof(float))
            {
                float b;
                if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out b))
                {
                    mValue = b;
                }
            }
            else if (type == typeof(double))
            {
                double b;
                if (double.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out b))
                {
                    mValue = b;
                }
            }
            else if (type == typeof(Vector2))
            {
                if (parts == null)
                {
                    parts = text.Split(',');
                }

                if (parts.Length == 2)
                {
                    Vector2 v;
                    if (float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out v.x) &&
                        float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out v.y))
                    {
                        mValue = v;
                    }
                }
            }
            else if (type == typeof(Vector3))
            {
                if (parts == null)
                {
                    parts = text.Split(',');
                }

                if (parts.Length == 3)
                {
                    Vector3 v;
                    if (float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out v.x) &&
                        float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out v.y) &&
                        float.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out v.z))
                    {
                        mValue = v;
                    }
                }
            }
            else if (type == typeof(Vector4))
            {
                if (parts == null)
                {
                    parts = text.Split(',');
                }

                if (parts.Length == 4)
                {
                    Vector4 v;
                    if (float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out v.x) &&
                        float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out v.y) &&
                        float.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out v.z) &&
                        float.TryParse(parts[3], NumberStyles.Float, CultureInfo.InvariantCulture, out v.w))
                    {
                        mValue = v;
                    }
                }
            }
            else if (type == typeof(Quaternion))
            {
                if (parts == null)
                {
                    parts = text.Split(',');
                }

                if (parts.Length == 3)
                {
                    Vector3 v;
                    if (float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out v.x) &&
                        float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out v.y) &&
                        float.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out v.z))
                    {
                        mValue = Quaternion.Euler(v);
                    }
                }
                else if (parts.Length == 4)
                {
                    Quaternion v;
                    if (float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out v.x) &&
                        float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out v.y) &&
                        float.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out v.z) &&
                        float.TryParse(parts[3], NumberStyles.Float, CultureInfo.InvariantCulture, out v.w))
                    {
                        mValue = v;
                    }
                }
            }
            else if (type == typeof(Color))
            {
                if (parts == null)
                {
                    parts = text.Split(',');
                }

                if (parts.Length == 4)
                {
                    Color v;
                    if (float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out v.r) &&
                        float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out v.g) &&
                        float.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out v.b) &&
                        float.TryParse(parts[3], NumberStyles.Float, CultureInfo.InvariantCulture, out v.a))
                    {
                        mValue = v;
                    }
                }
            }
            else if (type == typeof(Rect))
            {
                if (parts == null)
                {
                    parts = text.Split(',');
                }

                if (parts.Length == 4)
                {
                    Vector4 v;
                    if (float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out v.x) &&
                        float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out v.y) &&
                        float.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out v.z) &&
                        float.TryParse(parts[3], NumberStyles.Float, CultureInfo.InvariantCulture, out v.w))
                    {
                        mValue = new Rect(v.x, v.y, v.z, v.w);
                    }
                }
            }
            else if (type.Implements(typeof(IDataNodeSerializable)))
            {
                IDataNodeSerializable ds = (IDataNodeSerializable)type.Create();
                ds.Deserialize(this);
                mValue = ds;
                return(false);
            }
            else if (!type.IsSubclassOf(typeof(Component)))
            {
                bool isIList = type.Implements(typeof(System.Collections.IList));
                bool isTList = (!isIList && type.Implements(typeof(TList)));
                mValue = (isTList || isIList) ? type.Create(children.size) : type.Create();

                if (mValue == null)
                {
                    Debug.LogError("Unable to create a " + type);
                    return(true);
                }

                if (isTList)
                {
                    TList list     = mValue as TList;
                    Type  elemType = type.GetGenericArgument();

                    if (elemType != null)
                    {
                        for (int i = 0; i < children.size; ++i)
                        {
                            DataNode child = children[i];

                            if (child.value == null)
                            {
                                child.mValue    = child.name;
                                child.mResolved = false;
                                child.ResolveValue(elemType);
                                list.Add(child.mValue);
                            }
                            else if (child.name == "Add")
                            {
                                child.ResolveValue(elemType);
                                list.Add(child.mValue);
                            }
                            else
                            {
                                Debug.LogWarning("Unexpected node in an array: " + child.name);
                            }
                        }
                        return(false);
                    }
                    else
                    {
                        Debug.LogError("Unable to determine the element type of " + type);
                    }
                }
                else if (isIList)
                {
                    // This is for both List<Type> and Type[] arrays.
                    System.Collections.IList list = mValue as System.Collections.IList;
                    Type elemType = type.GetGenericArgument();
                    if (elemType == null)
                    {
                        elemType = type.GetElementType();
                    }
                    bool fixedSize = (list.Count == children.size);

                    if (elemType != null)
                    {
                        for (int i = 0; i < children.size; ++i)
                        {
                            DataNode child = children[i];

                            if (child.value == null)
                            {
                                child.mValue    = child.name;
                                child.mResolved = false;
                                child.ResolveValue(elemType);
                                if (fixedSize)
                                {
                                    list[i] = child.mValue;
                                }
                                else
                                {
                                    list.Add(child.mValue);
                                }
                            }
                            else if (child.name == "Add")
                            {
                                child.ResolveValue(elemType);
                                if (fixedSize)
                                {
                                    list[i] = child.mValue;
                                }
                                else
                                {
                                    list.Add(child.mValue);
                                }
                            }
                            else
                            {
                                Debug.LogWarning("Unexpected node in an array: " + child.name);
                            }
                        }
                        return(false);
                    }
                    else
                    {
                        Debug.LogError("Unable to determine the element type of " + type);
                    }
                }
                else if (type.IsClass)
                {
                    for (int i = 0; i < children.size; ++i)
                    {
                        DataNode child = children[i];
                        mValue.SetSerializableField(child.name, child.value);
                    }
                    return(false);
                }
                else
                {
                    Debug.LogError("Unhandled type: " + type);
                }
            }
            return(true);
        }