Пример #1
0
        public static NavGraph[] Load(string filePath)
        {
            byte[] bytes = AstarSerializer.LoadFromFile(filePath);

            AstarSerializer sr = new AstarSerializer();

            if (!sr.OpenDeserialize(bytes))
            {
                throw new Exception("Invalid data file (cannot read zip).\nThe data is either corrupt or it was saved using a 3.0.x or earlier version of the system");
            }

            var gr = new List <NavGraph>();

            // Set an offset so that the deserializer will load
            // the graphs with the correct graph indexes
            sr.SetGraphIndexOffset(gr.Count);

            gr.AddRange(sr.DeserializeGraphs());

            NavGraph[] graphs = gr.ToArray();

            sr.DeserializeEditorSettingsCompatibility();
            sr.DeserializeExtraInfo();

            //Assign correct graph indices.
            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] == null)
                {
                    continue;
                }
                int i1 = i;
                graphs[i].GetNodes(node => node.GraphIndex = (uint)i1);
            }

            for (int i = 0; i < graphs.Length; i++)
            {
                for (int j = i + 1; j < graphs.Length; j++)
                {
                    if (graphs[i] != null && graphs[j] != null && graphs[i].guid == graphs[j].guid)
                    {
                        graphs[i].guid = Guid.NewGuid();
                        break;
                    }
                }
            }

            sr.PostDeserialization();

            sr.CloseDeserialize();
            return(graphs);
        }
Пример #2
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");
                TryEat(',');
                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 result = new Vector2();
                EatField();
                result.x = float.Parse(EatField(), numberFormat);
                EatField();
                result.y = float.Parse(EatField(), numberFormat);
                Eat("}");
                return(result);
            }
            else if (Type.Equals(tp, typeof(Vector3)))
            {
                Eat("{");
                var result = new Vector3();
                EatField();
                result.x = float.Parse(EatField(), numberFormat);
                EatField();
                result.y = float.Parse(EatField(), numberFormat);
                EatField();
                result.z = float.Parse(EatField(), numberFormat);
                Eat("}");
                return(result);
            }
            else if (Type.Equals(tp, typeof(Guid)))
            {
                Eat("{");
                EatField();
                var result = Guid.Parse(EatField());
                Eat("}");
                return(result);
            }
            else if (Type.Equals(tp, typeof(List <string>)))
            {
                System.Collections.IList result = new List <string>();

                Eat("[");
                while (!TryEat(']'))
                {
                    result.Add(Deserialize(typeof(string)));
                    TryEat(',');
                }
                return(result);
            }
            else if (tpInfo.IsArray)
            {
                List <System.Object> ls = new List <System.Object>();
                Eat("[");
                while (!TryEat(']'))
                {
                    ls.Add(Deserialize(tp.GetElementType()));
                    TryEat(',');
                }
                var arr = Array.CreateInstance(tp.GetElementType(), ls.Count);
                ls.ToArray().CopyTo(arr, 0);
                return(arr);
            }
            else
            {
                var obj = populate ?? Activator.CreateInstance(tp);
                Eat("{");
                while (!TryEat('}'))
                {
                    var name    = EatField();
                    var tmpType = tp;
                    System.Reflection.FieldInfo field = null;
                    while (field == null && tmpType != null)
                    {
                        field   = tmpType.GetField(name, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
                        tmpType = tmpType.BaseType;
                    }

                    if (field == null)
                    {
                        SkipFieldData();
                    }
                    else
                    {
                        field.SetValue(obj, Deserialize(field.FieldType));
                    }
                    TryEat(',');
                }
                return(obj);
            }
        }