示例#1
0
            public string   Generate(string content)
            {
                StringBuilder buffer = UONUtility.GetBuffer();

                buffer.Append("[[\"");

                for (int i = 0; i < this.registeredTypes.Count; i++)
                {
                    if (i > 0)
                    {
                        buffer.Append("\",\"");
                    }

                    string typeStringified = this.registeredTypes[i].GetShortAssemblyType();
                    if (Type.GetType(typeStringified) == null)
                    {
                        Debug.Log("Type \"" + typeStringified + "\" becomes null.");
                    }

                    buffer.Append(typeStringified);
                }

                buffer.Append("\"],");
                buffer.Append(content);
                buffer.Append(']');

                return(UONUtility.ReturnBuffer(buffer));
            }
示例#2
0
        public override string  Serialize(UON.SerializationData data, object o)
        {
            int refIndex;

            if (data.GetReferenceIndex(o, out refIndex) == true)
            {
                return("#" + refIndex);
            }

            StringBuilder buffer = UONUtility.GetBuffer();
            IList         array  = o as IList;

            data.workingType = UONUtility.GetArraySubType(data.workingType);

            buffer.Append('[');

            foreach (object element in array)
            {
                string raw = data.ToUON(element);

                if (string.IsNullOrEmpty(raw) == false)
                {
                    if (buffer.Length > 1)
                    {
                        buffer.Append(',');
                    }

                    buffer.Append(raw);
                }
            }

            buffer.Append(']');

            return(UONUtility.ReturnBuffer(buffer));
        }
示例#3
0
        public override string  Serialize(UON.SerializationData data, object o)
        {
            int refIndex;

            if (data.GetReferenceIndex(o, out refIndex) == true)
            {
                return("#" + refIndex);
            }

            StringBuilder buffer = UONUtility.GetBuffer();

            FieldInfo[]       fields;
            IUONSerialization serializationInterface = o as IUONSerialization;

            if (serializationInterface != null)
            {
                serializationInterface.OnSerializing();
            }

            if (this.typesFields.TryGetValue(data.workingType, out fields) == false)
            {
                fields = UONUtility.GetFieldsHierarchyOrdered(data.workingType, typeof(object), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToArray();
            }

            buffer.Append('{');

            foreach (FieldInfo field in fields)
            {
#if NETFX_CORE
                if (field.IsDefined(typeof(NonSerializedAttribute)) == false)
#else
                if (field.IsNotSerialized == false)
#endif
                {
                    string raw = data.ToUON(field.GetValue(o));

                    if (string.IsNullOrEmpty(raw) == false)
                    {
                        if (buffer.Length > 1)
                        {
                            buffer.Append(',');
                        }

                        buffer.Append('"');
                        buffer.Append(field.Name);
                        buffer.Append("\":");
                        buffer.Append(raw);
                    }
                }
            }

            buffer.Append('}');

            return(UONUtility.ReturnBuffer(buffer));
        }
示例#4
0
        public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
        {
            if (raw[0] == '#')
            {
                return(data.deserializedReferences[int.Parse(raw.ToString(1, raw.Length - 1))]);
            }

            if (raw[0] != '[')
            {
                throw new FormatException("No opening char '[' found in \"" + raw + "\".");
            }

            if (instance != null && (instance is Array) == false)
            {
                throw new InvalidCastException("The given object of type \"" + instance.GetType() + "\" is not an Array.");
            }

            StringBuilder currentType = UONUtility.GetBuffer();
            int           deep        = 0;
            Type          arrayType   = data.latestType;
            Array         array       = null;

            for (int i = 1, j = 0; i < raw.Length; i++)
            {
                if (raw[i] == '{' || raw[i] == '[')
                {
                    ++deep;
                }
                else if (raw[i] == '}' || raw[i] == ']' || raw[i] == ',')
                {
                    if (instance == null)
                    {
                        instance           = Array.CreateInstance(arrayType.GetElementType(), int.Parse(currentType.ToString()));
                        currentType.Length = 0;
                        continue;
                    }

                    if (array == null)
                    {
                        array = instance as Array;
                        data.deserializedReferences.Add(array);
                    }

                    if (deep == 0 && currentType.Length > 0)
                    {
                        if (UON.VerboseLevel > 0)
                        {
                            Debug.Log(currentType.ToString());
                        }

                        if (currentType[0] == '(')
                        {
                            int n = currentType.IndexOf(")");
                            data.latestType = data.registeredTypes[int.Parse(currentType.ToString(1, n - 1))];
                            currentType     = currentType.Remove(0, n + 1);
                        }

                        if (j < array.Length)
                        {
                            array.SetValue(data.FromUON(data.latestType, currentType), j++);
                        }

                        currentType.Length = 0;
                        continue;
                    }

                    if (raw[i] == '}' || raw[i] == ']')
                    {
                        --deep;
                    }
                }

                currentType.Append(raw[i]);
            }

            UONUtility.RestoreBuffer(currentType);

            return(instance);
        }
示例#5
0
        public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
        {
            if (raw[0] == '#')
            {
                return(data.deserializedReferences[int.Parse(raw.ToString(1, raw.Length - 1))]);
            }

            if (raw[0] != '{')
            {
                throw new FormatException("No opening char '{' found in \"" + raw + "\".");
            }

            StringBuilder currentType = UONUtility.GetBuffer();
            string        key         = null;
            int           deep        = 0;
            Step          step        = Step.OpenQuoteKey;

            if (instance == null)
            {
                if (typeof(ScriptableObject).IsAssignableFrom(data.latestType) == true)
                {
                    instance = ScriptableObject.CreateInstance(data.latestType);
                }
                else
                {
#if !NETFX_CORE
                    if (data.latestType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null) == null)
                    {
                        instance = FormatterServices.GetUninitializedObject(data.latestType);
                    }
                    else
#else
                    if (data.latestType.GetConstructor(Type.EmptyTypes) == null)
#endif
                    { instance = Activator.CreateInstance(data.latestType); }
                }
            }

            data.deserializedReferences.Add(instance);

            IUONSerialization deserializationInterface = instance as IUONSerialization;

            if (deserializationInterface != null)
            {
                this.fieldsData.entries.Clear();
            }

            if (raw[1] != '}')
            {
                for (int i = 1; i < raw.Length; i++)
                {
                    if (step == Step.OpenQuoteKey)
                    {
                        if (raw[i] != '"')
                        {
                            throw new FormatException("Expected '\"' instead of \"" + raw[i] + "\" at position " + i + ".");
                        }

                        ++step;
                    }
                    else if (step == Step.Key)
                    {
                        for (; i < raw.Length; i++)
                        {
                            if (raw[i] == '"')
                            {
                                key = currentType.ToString();
                                currentType.Length = 0;
                                ++step;
                                break;
                            }

                            currentType.Append(raw[i]);
                        }
                    }
                    else if (step == Step.Colon)
                    {
                        if (raw[i] != ':')
                        {
                            throw new FormatException("Expected ':' instead of \"" + raw[i] + "\" at position " + i + ".");
                        }

                        ++step;
                    }
                    else if (step == Step.Value)
                    {
                        bool inText = false;

                        for (; i < raw.Length; i++)
                        {
                            if (raw[i] == '"' && (inText == false || this.IsSpecialCharCancelled(raw, i) == false))
                            {
                                inText = !inText;
                            }
                            else if ((raw[i] == '{' || raw[i] == '[') && inText == false)
                            {
                                ++deep;
                            }
                            else if ((raw[i] == '}' || raw[i] == ']' || raw[i] == ',') && inText == false)
                            {
                                if (deep == 0 && currentType.Length > 0)
                                {
                                    if (UON.VerboseLevel > 0)
                                    {
                                        Debug.Log(key);
                                        Debug.Log(currentType.ToString());
                                    }

                                    if (deserializationInterface != null)
                                    {
                                        this.fieldsData.entries.Add(key, currentType.ToString());
                                    }

                                    data.AssignField(key, currentType, instance);

                                    currentType.Length = 0;
                                    step = Step.Comma;
                                    --i;
                                    break;
                                }

                                if (raw[i] == '}' || raw[i] == ']')
                                {
                                    --deep;
                                }
                            }

                            currentType.Append(raw[i]);
                        }
                    }
                    else if (step == Step.Comma)
                    {
                        if (raw[i] == '}')
                        {
                            break;
                        }

                        if (raw[i] != ',')
                        {
                            throw new FormatException("Expected ',' instead of \"" + raw[i] + "\" at position " + i + ".");
                        }

                        step = Step.OpenQuoteKey;
                    }
                }
            }

            UONUtility.RestoreBuffer(currentType);

            if (deserializationInterface != null)
            {
                deserializationInterface.OnDeserialized(this.fieldsData);
            }

            return(instance);
        }
示例#6
0
        /// <summary>Reverses UON into object.</summary>
        /// <param name="rawUON">Any UON.</param>
        /// <param name="instance">The <paramref name="instance"/> to overwrite instead of a complete new object.</param>
        /// <returns>Returns a new object or the given <paramref name="instance"/>.</returns>
        public static object    FromUON(string rawUON, object instance = null)
        {
            if (UON.VerboseLevel > 0)
            {
                Debug.Log(rawUON);
            }

            if (string.IsNullOrEmpty(rawUON) == true || rawUON.Length <= 4)
            {
                return(null);
            }

            if (rawUON[0] != '[')
            {
                throw new FormatException("Expected '[' instead of \"" + rawUON[0] + "\" at position 0.");
            }

            SerializationData data = new SerializationData();
            int i = 0;

            if (rawUON[1] != '[')
            {
                throw new FormatException("Expected '[' instead of \"" + rawUON[1] + "\" at position 1.");
            }

            StringBuilder currentType = UONUtility.GetBuffer();
            bool          isOpen      = false;
            int           deep        = 0;

            i = 1;

            for (; i < rawUON.Length; i++)
            {
                if (rawUON[i] == '[')
                {
                    ++deep;
                }
                else if (rawUON[i] == ']')
                {
                    --deep;
                    if (deep == 0)
                    {
                        break;
                    }
                }

                if (rawUON[i] == '"')
                {
                    isOpen = !isOpen;

                    if (isOpen == false)
                    {
                        Type t = Type.GetType(currentType.ToString());

                        if (t == null)
                        {
                            throw new FormatException("A registered type is null using \"" + currentType.ToString() + "\".");
                        }

                        data.registeredTypes.Add(t);
                        currentType.Length = 0;
                    }
                }
                else if (isOpen == true)
                {
                    currentType.Append(rawUON[i]);
                }
            }

            if (data.registeredTypes.Count == 0)
            {
                return(null);
            }

            UONUtility.RestoreBuffer(currentType);

            data.workingType = data.registeredTypes[0];
            data.latestType  = data.registeredTypes[0];

            for (int j = 0; j < UON.types.Length; j++)
            {
                if (UON.types[j] is UnityObjectUON)
                {
                    continue;
                }

                if (UON.types[j].Can(data.workingType) == true)
                {
                    StringBuilder buffer = UONUtility.GetBuffer(rawUON);

                    try
                    {
                        buffer.Length -= 1;
                        buffer.Remove(0, i + 2);
                        return(UON.types[j].Deserialize(data, buffer, instance));
                    }
                    catch (Exception ex)
                    {
                        if (UON.VerboseLevel > 1)
                        {
                            Debug.LogException(ex);
                            Debug.LogError(buffer);
                        }
                    }
                    finally
                    {
                        UONUtility.RestoreBuffer(buffer);
                    }

                    break;
                }
            }

            return(null);
        }
示例#7
0
        public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
        {
            if (raw[0] == '#')
            {
                return(data.deserializedReferences[int.Parse(raw.ToString(1, raw.Length - 1))]);
            }

            if (raw[0] != '[')
            {
                throw new FormatException("No opening char '[' found in \"" + raw + "\".");
            }

            StringBuilder currentType = UONUtility.GetBuffer();
            int           deep        = 0;
            IList         list;

            if (instance == null)
            {
                instance = Activator.CreateInstance(data.latestType);
            }

            data.deserializedReferences.Add(instance);

            list = instance as IList;

            if (list == null)
            {
                throw new InvalidCastException("The given object of type \"" + instance.GetType() + "\" does not implement interface IList.");
            }
            else
            {
                list.Clear();
            }

            for (int i = 1; i < raw.Length; i++)
            {
                if (raw[i] == '{' || raw[i] == '[')
                {
                    ++deep;
                }
                else if (raw[i] == '}' || raw[i] == ']' || raw[i] == ',')
                {
                    if (deep == 0 && currentType.Length > 0)
                    {
                        if (UON.VerboseLevel > 0)
                        {
                            Debug.Log(currentType.ToString());
                        }

                        if (currentType[0] == '(')
                        {
                            int n = currentType.IndexOf(")");
                            data.latestType = data.registeredTypes[int.Parse(currentType.ToString(1, n - 1))];
                            currentType     = currentType.Remove(0, n + 1);
                        }

                        list.Add(data.FromUON(data.latestType, currentType));

                        currentType.Length = 0;
                        continue;
                    }

                    if (raw[i] == '}' || raw[i] == ']')
                    {
                        --deep;
                    }
                }

                currentType.Append(raw[i]);
            }

            UONUtility.RestoreBuffer(currentType);

            return(instance);
        }