Example #1
0
        UnityEngine.Object DeserializeUnityObjectInner()
        {
            if (EatField() != "Name")
            {
                throw new Exception("Expected 'Name' field");
            }
            string name = EatField();

            if (name == null)
            {
                return(null);
            }

            if (EatField() != "Type")
            {
                throw new Exception("Expected 'Type' field");
            }
            string typename = EatField();

            // Remove assembly information
            if (typename.IndexOf(',') != -1)
            {
                typename = typename.Substring(0, typename.IndexOf(','));
            }

            // Note calling through assembly is more stable on e.g WebGL
            var type = WindowsStoreCompatibility.GetTypeInfo(typeof(AstarPath)).Assembly.GetType(typename);

            type = type ?? WindowsStoreCompatibility.GetTypeInfo(typeof(Transform)).Assembly.GetType(typename);

            if (Type.Equals(type, null))
            {
                Debug.LogError("Could not find type '" + typename + "'. Cannot deserialize Unity reference");
                return(null);
            }

            // Check if there is another field there
            EatWhitespace();
            if ((char)reader.Peek() == '"')
            {
                if (EatField() != "GUID")
                {
                    throw new Exception("Expected 'GUID' field");
                }
                string guid = EatField();

                foreach (var helper in UnityEngine.Object.FindObjectsOfType <UnityReferenceHelper>())
                {
                    if (helper.GetGUID() == guid)
                    {
                        if (Type.Equals(type, typeof(GameObject)))
                        {
                            return(helper.gameObject);
                        }
                        else
                        {
                            return(helper.GetComponent(type));
                        }
                    }
                }
            }

            // Try to load from resources
            UnityEngine.Object[] objs = Resources.LoadAll(name, type);

            for (int i = 0; i < objs.Length; i++)
            {
                if (objs[i].name == name || objs.Length == 1)
                {
                    return(objs[i]);
                }
            }

            return(null);
        }
Example #2
0
 /// <summary>Returns the first graph which inherits from the type type. Returns null if no graph was found.</summary>
 public NavGraph FindGraphWhichInheritsFrom(System.Type type)
 {
     return(FindGraph(graph => WindowsStoreCompatibility.GetTypeInfo(type).IsAssignableFrom(WindowsStoreCompatibility.GetTypeInfo(graph.GetType()))));
 }
Example #3
0
        void Serialize(System.Object obj)
        {
            if (obj == null)
            {
                output.Append("null");
                return;
            }

            var type     = obj.GetType();
            var typeInfo = WindowsStoreCompatibility.GetTypeInfo(type);

            if (serializers.ContainsKey(type))
            {
                serializers[type] (obj);
            }
            else if (typeInfo.IsEnum)
            {
                output.Append('"' + obj.ToString() + '"');
            }
            else if (obj is System.Collections.IList)
            {
                output.Append("[");
                var arr = obj as System.Collections.IList;
                for (int i = 0; i < arr.Count; i++)
                {
                    if (i != 0)
                    {
                        output.Append(", ");
                    }
                    Serialize(arr[i]);
                }
                output.Append("]");
            }
            else if (obj is UnityEngine.Object)
            {
                SerializeUnityObject(obj as UnityEngine.Object);
            }
            else
            {
#if NETFX_CORE
                var optIn = tpInfo.CustomAttributes.Any(attr => attr.GetType() == typeof(JsonOptInAttribute));
#else
                var optIn = typeInfo.GetCustomAttributes(typeof(JsonOptInAttribute), true).Length > 0;
#endif
                output.Append("{");

#if NETFX_CORE
                var fields = tpInfo.DeclaredFields.Where(f => !f.IsStatic).ToArray();
#else
                var fields = type.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
#endif
                bool earlier = false;
                foreach (var field in fields)
                {
                    if ((!optIn && field.IsPublic) ||
#if NETFX_CORE
                        field.CustomAttributes.Any(attr => attr.GetType() == typeof(JsonMemberAttribute))
#else
                        field.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length > 0
#endif
                        )
                    {
                        if (earlier)
                        {
                            output.Append(", ");
                        }

                        earlier = true;
                        output.AppendFormat("\"{0}\": ", field.Name);
                        Serialize(field.GetValue(obj));
                    }
                }
                output.Append("}");
            }
        }
Example #4
0
        /** Deserializes an object of type tp.
         * Will load all fields into the \a populate object if it is set (only works for classes).
         */
        System.Object Deserialize(Type tp, System.Object populate = null)
        {
            var tpInfo = WindowsStoreCompatibility.GetTypeInfo(tp);

            if (tpInfo.IsEnum)
            {
                return(Enum.Parse(tp, EatField()));
            }
            else if (TryEat('n'))
            {
                Eat("ull");
                return(null);
            }
            else if (Type.Equals(tp, typeof(float)))
            {
                return(float.Parse(EatField(), numberFormat));
            }
            else if (Type.Equals(tp, typeof(int)))
            {
                return(int.Parse(EatField(), numberFormat));
            }
            else if (Type.Equals(tp, typeof(uint)))
            {
                return(uint.Parse(EatField(), numberFormat));
            }
            else if (Type.Equals(tp, typeof(bool)))
            {
                return(bool.Parse(EatField()));
            }
            else if (Type.Equals(tp, typeof(string)))
            {
                return(EatField());
            }
            else if (Type.Equals(tp, typeof(Version)))
            {
                return(new Version(EatField()));
            }
            else if (Type.Equals(tp, typeof(Vector2)))
            {
                Eat("{");
                var res = new Vector2();
                EatField();
                res.x = float.Parse(EatField(), numberFormat);
                EatField();
                res.y = float.Parse(EatField(), numberFormat);
                Eat("}");
                return(res);
            }
            else if (Type.Equals(tp, typeof(Vector3)))
            {
                Eat("{");
                var res = new Vector3();
                EatField();
                res.x = float.Parse(EatField(), numberFormat);
                EatField();
                res.y = float.Parse(EatField(), numberFormat);
                EatField();
                res.z = float.Parse(EatField(), numberFormat);
                Eat("}");
                return(res);
            }
            else if (Type.Equals(tp, typeof(Pathfinding.Util.Guid)))
            {
                Eat("{");
                EatField();
                var res = Pathfinding.Util.Guid.Parse(EatField());
                Eat("}");
                return(res);
            }
            else if (Type.Equals(tp, typeof(LayerMask)))
            {
                Eat("{");
                EatField();
                var res = (LayerMask)int.Parse(EatField());
                Eat("}");
                return(res);
            }
            else if (Type.Equals(tp, typeof(List <string>)))
            {
                System.Collections.IList ls;
                ls = new List <string>();

                Eat("[");
                while (!TryEat(']'))
                {
                    ls.Add(Deserialize(typeof(string)));
                }
                return(ls);
            }
            else if (tpInfo.IsArray)
            {
                List <System.Object> ls = new List <System.Object>();
                Eat("[");
                while (!TryEat(']'))
                {
                    ls.Add(Deserialize(tp.GetElementType()));
                }
                var arr = Array.CreateInstance(tp.GetElementType(), ls.Count);
                ls.ToArray().CopyTo(arr, 0);
                return(arr);
            }
            else if (Type.Equals(tp, typeof(Mesh)) || Type.Equals(tp, typeof(Texture2D)) || Type.Equals(tp, typeof(Transform)) || Type.Equals(tp, typeof(GameObject)))
            {
                return(DeserializeUnityObject());
            }
            else
            {
                var obj = populate ?? Activator.CreateInstance(tp);
                Eat("{");
                while (!TryEat('}'))
                {
                    var name  = EatField();
                    var field = tpInfo.GetField(name);
                    if (field == null)
                    {
                        SkipFieldData();
                    }
                    else
                    {
                        field.SetValue(obj, Deserialize(field.FieldType));
                    }
                    TryEat(',');
                }
                return(obj);
            }
        }
        // Token: 0x06002822 RID: 10274 RVA: 0x001B6D04 File Offset: 0x001B4F04
        private object Deserialize(Type tp, object populate = null)
        {
            Type typeInfo = WindowsStoreCompatibility.GetTypeInfo(tp);

            if (typeInfo.IsEnum)
            {
                return(Enum.Parse(tp, this.EatField()));
            }
            if (this.TryEat('n'))
            {
                this.Eat("ull");
                this.TryEat(',');
                return(null);
            }
            if (object.Equals(tp, typeof(float)))
            {
                return(float.Parse(this.EatField(), TinyJsonDeserializer.numberFormat));
            }
            if (object.Equals(tp, typeof(int)))
            {
                return(int.Parse(this.EatField(), TinyJsonDeserializer.numberFormat));
            }
            if (object.Equals(tp, typeof(uint)))
            {
                return(uint.Parse(this.EatField(), TinyJsonDeserializer.numberFormat));
            }
            if (object.Equals(tp, typeof(bool)))
            {
                return(bool.Parse(this.EatField()));
            }
            if (object.Equals(tp, typeof(string)))
            {
                return(this.EatField());
            }
            if (object.Equals(tp, typeof(Version)))
            {
                return(new Version(this.EatField()));
            }
            if (object.Equals(tp, typeof(Vector2)))
            {
                this.Eat("{");
                Vector2 vector = default(Vector2);
                this.EatField();
                vector.x = float.Parse(this.EatField(), TinyJsonDeserializer.numberFormat);
                this.EatField();
                vector.y = float.Parse(this.EatField(), TinyJsonDeserializer.numberFormat);
                this.Eat("}");
                return(vector);
            }
            if (object.Equals(tp, typeof(Vector3)))
            {
                this.Eat("{");
                Vector3 vector2 = default(Vector3);
                this.EatField();
                vector2.x = float.Parse(this.EatField(), TinyJsonDeserializer.numberFormat);
                this.EatField();
                vector2.y = float.Parse(this.EatField(), TinyJsonDeserializer.numberFormat);
                this.EatField();
                vector2.z = float.Parse(this.EatField(), TinyJsonDeserializer.numberFormat);
                this.Eat("}");
                return(vector2);
            }
            if (object.Equals(tp, typeof(Pathfinding.Util.Guid)))
            {
                this.Eat("{");
                this.EatField();
                Pathfinding.Util.Guid guid = Pathfinding.Util.Guid.Parse(this.EatField());
                this.Eat("}");
                return(guid);
            }
            if (object.Equals(tp, typeof(LayerMask)))
            {
                this.Eat("{");
                this.EatField();
                LayerMask layerMask = int.Parse(this.EatField());
                this.Eat("}");
                return(layerMask);
            }
            if (object.Equals(tp, typeof(List <string>)))
            {
                IList list = new List <string>();
                this.Eat("[");
                while (!this.TryEat(']'))
                {
                    list.Add(this.Deserialize(typeof(string), null));
                    this.TryEat(',');
                }
                return(list);
            }
            if (typeInfo.IsArray)
            {
                List <object> list2 = new List <object>();
                this.Eat("[");
                while (!this.TryEat(']'))
                {
                    list2.Add(this.Deserialize(tp.GetElementType(), null));
                    this.TryEat(',');
                }
                Array array = Array.CreateInstance(tp.GetElementType(), list2.Count);
                list2.ToArray().CopyTo(array, 0);
                return(array);
            }
            if (object.Equals(tp, typeof(Mesh)) || object.Equals(tp, typeof(Texture2D)) || object.Equals(tp, typeof(Transform)) || object.Equals(tp, typeof(GameObject)))
            {
                return(this.DeserializeUnityObject());
            }
            object obj = populate ?? Activator.CreateInstance(tp);

            this.Eat("{");
            while (!this.TryEat('}'))
            {
                string    name      = this.EatField();
                Type      type      = tp;
                FieldInfo fieldInfo = null;
                while (fieldInfo == null && type != null)
                {
                    fieldInfo = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    type      = type.BaseType;
                }
                if (fieldInfo == null)
                {
                    this.SkipFieldData();
                }
                else
                {
                    fieldInfo.SetValue(obj, this.Deserialize(fieldInfo.FieldType, null));
                }
                this.TryEat(',');
            }
            return(obj);
        }
        // Token: 0x06002824 RID: 10276 RVA: 0x001B7164 File Offset: 0x001B5364
        private UnityEngine.Object DeserializeUnityObjectInner()
        {
            string a = this.EatField();

            if (a == "InstanceID")
            {
                this.EatField();
                a = this.EatField();
            }
            if (a != "Name")
            {
                throw new Exception("Expected 'Name' field");
            }
            string text = this.EatField();

            if (text == null)
            {
                return(null);
            }
            if (this.EatField() != "Type")
            {
                throw new Exception("Expected 'Type' field");
            }
            string text2 = this.EatField();

            if (text2.IndexOf(',') != -1)
            {
                text2 = text2.Substring(0, text2.IndexOf(','));
            }
            Type type = WindowsStoreCompatibility.GetTypeInfo(typeof(AstarPath)).Assembly.GetType(text2);

            type = (type ?? WindowsStoreCompatibility.GetTypeInfo(typeof(Transform)).Assembly.GetType(text2));
            if (object.Equals(type, null))
            {
                Debug.LogError("Could not find type '" + text2 + "'. Cannot deserialize Unity reference");
                return(null);
            }
            this.EatWhitespace();
            if ((ushort)this.reader.Peek() == 34)
            {
                if (this.EatField() != "GUID")
                {
                    throw new Exception("Expected 'GUID' field");
                }
                string b = this.EatField();
                UnityReferenceHelper[] array = UnityEngine.Object.FindObjectsOfType <UnityReferenceHelper>();
                int i = 0;
                while (i < array.Length)
                {
                    UnityReferenceHelper unityReferenceHelper = array[i];
                    if (unityReferenceHelper.GetGUID() == b)
                    {
                        if (object.Equals(type, typeof(GameObject)))
                        {
                            return(unityReferenceHelper.gameObject);
                        }
                        return(unityReferenceHelper.GetComponent(type));
                    }
                    else
                    {
                        i++;
                    }
                }
            }
            UnityEngine.Object[] array2 = Resources.LoadAll(text, type);
            for (int j = 0; j < array2.Length; j++)
            {
                if (array2[j].name == text || array2.Length == 1)
                {
                    return(array2[j]);
                }
            }
            return(null);
        }
Example #7
0
        private UnityEngine.Object DeserializeUnityObjectInner()
        {
            // Ignore InstanceID field (compatibility only)
            var fieldName = EatField();

            if (fieldName == "InstanceID")
            {
                EatField();
                fieldName = EatField();
            }

            if (fieldName != "Name")
            {
                throw new Exception("Expected 'Name' field");
            }
            var name = EatField();

            if (name == null)
            {
                return(null);
            }

            if (EatField() != "Type")
            {
                throw new Exception("Expected 'Type' field");
            }
            var typename = EatField();

            // Remove assembly information
            if (typename.IndexOf(',') != -1)
            {
                typename = typename.Substring(0, typename.IndexOf(','));
            }

            // Note calling through assembly is more stable on e.g WebGL
            var type = WindowsStoreCompatibility.GetTypeInfo(typeof(AstarPath)).Assembly.GetType(typename);

            type = type ?? WindowsStoreCompatibility.GetTypeInfo(typeof(Transform)).Assembly.GetType(typename);

            if (Equals(type, null))
            {
                Debug.LogError("Could not find type '" + typename + "'. Cannot deserialize Unity reference");
                return(null);
            }

            // Check if there is another field there
            EatWhitespace();
            if ((char)reader.Peek() == '"')
            {
                if (EatField() != "GUID")
                {
                    throw new Exception("Expected 'GUID' field");
                }
                var guid = EatField();

                foreach (var helper in UnityEngine.Object.FindObjectsOfType <UnityReferenceHelper>())
                {
                    if (helper.GetGUID() == guid)
                    {
                        if (Equals(type, typeof(GameObject)))
                        {
                            return(helper.gameObject);
                        }
                        else
                        {
                            return(helper.GetComponent(type));
                        }
                    }
                }
            }

            // Note: calling LoadAll with an empty string will make it load the whole resources folder, which is probably a bad idea.
            if (!string.IsNullOrEmpty(name))
            {
                // Try to load from resources
                var objs = Resources.LoadAll(name, type);

                for (var i = 0; i < objs.Length; i++)
                {
                    if (objs[i].name == name || objs.Length == 1)
                    {
                        return(objs[i]);
                    }
                }
            }

            return(null);
        }
        // Token: 0x0600286B RID: 10347 RVA: 0x001BC41C File Offset: 0x001BA61C
        private void Serialize(object obj)
        {
            if (obj == null)
            {
                this.output.Append("null");
                return;
            }
            Type type     = obj.GetType();
            Type typeInfo = WindowsStoreCompatibility.GetTypeInfo(type);

            if (this.serializers.ContainsKey(type))
            {
                this.serializers[type](obj);
                return;
            }
            if (typeInfo.IsEnum)
            {
                this.output.Append("\"" + obj.ToString() + "\"");
                return;
            }
            if (obj is IList)
            {
                this.output.Append("[");
                IList list = obj as IList;
                for (int i = 0; i < list.Count; i++)
                {
                    if (i != 0)
                    {
                        this.output.Append(", ");
                    }
                    this.Serialize(list[i]);
                }
                this.output.Append("]");
                return;
            }
            if (obj is UnityEngine.Object)
            {
                this.SerializeUnityObject(obj as UnityEngine.Object);
                return;
            }
            bool flag = typeInfo.GetCustomAttributes(typeof(JsonOptInAttribute), true).Length != 0;

            this.output.Append("{");
            bool flag2 = false;

            do
            {
                foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    if (!(fieldInfo.DeclaringType != type) && ((!flag && fieldInfo.IsPublic) || fieldInfo.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length != 0))
                    {
                        if (flag2)
                        {
                            this.output.Append(", ");
                        }
                        flag2 = true;
                        this.output.AppendFormat("\"{0}\": ", fieldInfo.Name);
                        this.Serialize(fieldInfo.GetValue(obj));
                    }
                }
                type = type.BaseType;
            }while (!(type == null));
            this.output.Append("}");
        }
Example #9
0
 // Token: 0x060021F7 RID: 8695 RVA: 0x00191008 File Offset: 0x0018F208
 public NavGraph FindGraphWhichInheritsFrom(Type type)
 {
     return(this.FindGraph((NavGraph graph) => WindowsStoreCompatibility.GetTypeInfo(type).IsAssignableFrom(WindowsStoreCompatibility.GetTypeInfo(graph.GetType()))));
 }