Exemple #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));
            }
Exemple #2
0
            public FieldInfo        GetField(Type t, string fieldName)
            {
                Dictionary <string, FieldInfo> fields;
                FieldInfo field = null;

                if (this.fields.TryGetValue(t, out fields) == false)
                {
                    fields = new Dictionary <string, FieldInfo>();
                    this.fields.Add(t, fields);
                }

                if (fields.TryGetValue(fieldName, out field) == false)
                {
                    foreach (FieldInfo f in UONUtility.EachFieldHierarchyOrdered(t, typeof(object), BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
                    {
                        if (f.Name == fieldName)
                        {
                            field = f;
                            break;
                        }
                    }

                    if (field == null && UON.VerboseLevel > 1)
                    {
                        Debug.LogWarning("Field \"" + fieldName + "\" was fot found in \"" + t.Name + "\".");
                    }

                    fields.Add(fieldName, field);
                }

                return(field);
            }
Exemple #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();
            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));
        }
Exemple #4
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));
        }
Exemple #5
0
        public static T[]                               CreateInstancesOf <T>(params object[] args) where T : class
        {
            UONUtility.tempList.Clear();

            foreach (Type type in UONUtility.EachSubClassesOf(typeof(T)))
            {
                UONUtility.tempList.Add(Activator.CreateInstance(type, args));
            }

            T[] result = new T[UONUtility.tempList.Count];

            for (int i = 0; i < UONUtility.tempList.Count; i++)
            {
                result[i] = UONUtility.tempList[i] as T;
            }

            return(result);
        }
Exemple #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);
        }
Exemple #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();
            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);
        }
Exemple #8
0
 public static sbyte             HexToSByte(this string str)
 {
     return((sbyte)UONUtility.HexToByte(str));
 }
Exemple #9
0
 public static string    ToHex(this sbyte input)
 {
     return(UONUtility.ToHex((byte)input));
 }
Exemple #10
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);
        }
Exemple #11
0
        static UON()
        {
            UON.types = UONUtility.CreateInstancesOf <UONType>();

            for (int i = 0; i < UON.types.Length; i++)
            {
                if (UON.types[i] is ClassUON)
                {
                    if (i != UON.types.Length - 1)
                    {
                        UONType tmp = UON.types[UON.types.Length - 1];
                        UON.types[UON.types.Length - 1] = UON.types[i];
                        UON.types[i] = tmp;
                    }
                }
            }

            //UnityEditor.EditorApplication.delayCall += () =>
            //{
            //	try
            //	{
            //		//UON.Verbose = true;

            //		UON.Test(BindingFlags.DeclaredOnly | BindingFlags.CreateInstance);
            //		UON.Test(null);
            //		UON.Test(1);
            //		UON.Test(0);
            //		UON.Test(-1);
            //		UON.Test(1.234F);
            //		UON.Test(0F);
            //		UON.Test(-1.234F);
            //		UON.Test(true);
            //		UON.Test(false);
            //		UON.Test(string.Empty);
            //		UON.Test("Abcdefg");
            //		UON.Test(new int[] { 1, 0, -1 });
            //		UON.Test(new byte[] { 1, 0, 255 });
            //		UON.Test(new sbyte[] { -123, 1, 0, 124 });
            //		UON.Test(new float[] { 1.234567F, 0.1234567F, -1.234567F });
            //		UON.Test(new double[] { 1.23456789D, 0.123456789D, -1.23456789D });
            //		UON.Test(new decimal[] { 1.234568790M, 0.1234567890M, -1.234567890M });
            //		UON.Test(new string[] { "aa", "bb", "cc", string.Empty, null });
            //		UON.Test(new string[] { });
            //		UON.Test(new List<int> { 1, 0, -1 });
            //		UON.Test(new List<byte> { 1, 0, 255 });
            //		UON.Test(new List<sbyte> { -123, 1, 0, 124 });
            //		UON.Test(new List<float> { 1F, 0F, -1F });
            //		UON.Test(new List<double> { 1D, 0D, -1D });
            //		UON.Test(new List<decimal> { 1M, 0M, -1M });
            //		UON.Test(new List<string> { "aa", "bb", "cc", string.Empty, null });
            //		UON.Test(new List<string> { });
            //		UON.Test(new Action(() => { }));
            //		UON.Test(new Action<int>((a) => { }));
            //		UON.Test(new Action<int, string>((a, b) => { }));
            //		UON.Test(new Func<string>(() => null));
            //		UON.Test(new Dictionary<int, byte>());
            //		UON.Test(new Dictionary<Action, Func<string>>());
            //		UON.Test(new List<Action<int, Action<string, Dictionary<int, LocalVariableInfo>>>>());
            //		var t1 = typeof(int);
            //		var t2 = typeof(bool);
            //		UON.Test(new List<object>() { t1, t1, t2, null });
            //		UON.Test(new object[] { t1, t1, t2, null });
            //		UON.Test(t1);
            //		UON.Test(t2);
            //	}
            //	catch (Exception ex)
            //	{
            //		Debug.LogException(ex);
            //	}
            //};
        }
Exemple #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);
        }