Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
 public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
 {
     if (this.GetAssetPath != null)
     {
         return(this.LoadAssetAtPath.Invoke(null, new object[] { raw.ToString(1, raw.Length - 2), typeof(UnityEngine.Object) }));
     }
     return(null);
 }
Ejemplo n.º 3
0
 public override string  Serialize(UON.SerializationData data, object o)
 {
     if (this.GetAssetPath != null)
     {
         return("\"" + this.GetAssetPath.Invoke(null, new object[] { o }) + "\"");
     }
     return(null);
 }
Ejemplo n.º 4
0
        public string   AppendTypeIfNecessary(UON.SerializationData data, Type targetType)
        {
            if (data.nestedLevel > 0 && (data.latestType == null || data.latestType != targetType))
            {
                data.latestType = targetType;
                return("(" + data.GetTypeIndex(targetType) + ')');
            }

            return(string.Empty);
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
 public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
 {
     return(null);
 }
Ejemplo n.º 8
0
 public override string  Serialize(UON.SerializationData data, object o)
 {
     return(null);
 }
Ejemplo n.º 9
0
 public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
 {
     return(Enum.ToObject(data.latestType, int.Parse(raw.ToString())));
 }
Ejemplo n.º 10
0
 public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
 {
     return(raw[0] == 'T' ? true : false);
 }
Ejemplo n.º 11
0
 public override string  Serialize(UON.SerializationData data, object o)
 {
     return((Boolean)o == true ? "T" : "F");
 }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
 public override string  Serialize(UON.SerializationData data, object o)
 {
     return(((byte)o).ToHex());
 }
Ejemplo n.º 14
0
 public override string  Serialize(UON.SerializationData data, object o)
 {
     return(o.ToString());
 }
Ejemplo n.º 15
0
 public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
 {
     return(raw.Replace("\\\"", "\"").ToString(1, raw.Length - 2));
 }
Ejemplo n.º 16
0
 public override string  Serialize(UON.SerializationData data, object o)
 {
     return("\"" + (o as string).Replace("\"", "\\\"") + '"');
 }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
 public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
 {
     return(UInt32.Parse(raw.ToString()));
 }
Ejemplo n.º 19
0
 public abstract string  Serialize(UON.SerializationData data, object instance);
Ejemplo n.º 20
0
 public override object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance)
 {
     return(raw.ToString().HexToByte());
 }
Ejemplo n.º 21
0
 public abstract object  Deserialize(UON.SerializationData data, StringBuilder raw, object instance);