예제 #1
0
 public GraphSerializationContext(BinaryReader reader, GraphNode[] id2NodeMapping, uint graphIndex, GraphMeta meta)
 {
     this.reader         = reader;
     this.id2NodeMapping = id2NodeMapping;
     this.graphIndex     = graphIndex;
     this.meta           = meta;
 }
예제 #2
0
        private GraphMeta DeserializeBinaryMeta(ZipEntry entry)
        {
            var meta = new GraphMeta();

            var reader = GetBinaryReader(entry);

            if (reader.ReadString() != "A*")
            {
                throw new System.Exception("Invalid magic number in saved data");
            }
            int major    = reader.ReadInt32();
            int minor    = reader.ReadInt32();
            int build    = reader.ReadInt32();
            int revision = reader.ReadInt32();

            // Required because when saving a version with a field not set, it will save it as -1
            // and then the Version constructor will throw an exception (which we do not want)
            if (major < 0)
            {
                meta.version = new Version(0, 0);
            }
            else if (minor < 0)
            {
                meta.version = new Version(major, 0);
            }
            else if (build < 0)
            {
                meta.version = new Version(major, minor);
            }
            else if (revision < 0)
            {
                meta.version = new Version(major, minor, build);
            }
            else
            {
                meta.version = new Version(major, minor, build, revision);
            }

            meta.graphs = reader.ReadInt32();

            meta.guids = new List <string>();
            int count = reader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                meta.guids.Add(reader.ReadString());
            }

            meta.typeNames = new List <string>();
            count          = reader.ReadInt32();
            for (int i = 0; i < count; i++)
            {
                meta.typeNames.Add(reader.ReadString());
            }

            return(meta);
        }
예제 #3
0
        public void OpenSerialize()
        {
            // Create a new zip file, here we will store all the data
            zipStream = new MemoryStream();
#if NETFX_CORE
            zip = new ZipFile(zipStream, System.IO.Compression.ZipArchiveMode.Create);
#else
            zip = new ZipFile();
            zip.AlternateEncoding      = System.Text.Encoding.UTF8;
            zip.AlternateEncodingUsage = ZipOption.Always;
#endif
            meta = new GraphMeta();
        }
예제 #4
0
        public bool OpenDeserialize(byte[] bytes)
        {
            // Copy the bytes to a stream
            zipStream = new MemoryStream();
            zipStream.Write(bytes, 0, bytes.Length);
            zipStream.Position = 0;
            try {
#if NETFX_CORE
                zip = new ZipFile(zipStream);
#else
                zip = ZipFile.Read(zipStream);
#endif
            } catch (Exception e) {
#if !SERVER
                // Catches exceptions when an invalid zip file is found
                UnityEngine.Debug.LogError("Caught exception when loading from zip\n" + e);
#endif
                zipStream.Dispose();
                return(false);
            }

            if (ContainsEntry("meta" + jsonExt))
            {
                meta = DeserializeMeta(GetEntry("meta" + jsonExt));
            }
            else if (ContainsEntry("meta" + binaryExt))
            {
                meta = DeserializeBinaryMeta(GetEntry("meta" + binaryExt));
            }
            else
            {
                throw new Exception("No metadata found in serialized data.");
            }
#if !SERVER
            if (FullyDefinedVersion(meta.version) > FullyDefinedVersion(AstarPath.Version))
            {
                UnityEngine.Debug.LogWarning("Trying to load data from a newer version of the A* Pathfinding Project\nCurrent version: " + AstarPath.Version + " Data version: " + meta.version +
                                             "\nThis is usually fine as the stored data is usually backwards and forwards compatible." +
                                             "\nHowever node data (not settings) can get corrupted between versions (even though I try my best to keep compatibility), so it is recommended " +
                                             "to recalculate any caches (those for faster startup) and resave any files. Even if it seems to load fine, it might cause subtle bugs.\n");
            }
            else if (FullyDefinedVersion(meta.version) < FullyDefinedVersion(AstarPath.Version))
            {
                UnityEngine.Debug.LogWarning("Upgrading serialized pathfinding data from version " + meta.version + " to " + AstarPath.Version +
                                             "\nThis is usually fine, it just means you have upgraded to a new version." +
                                             "\nHowever node data (not settings) can get corrupted between versions (even though I try my best to keep compatibility), so it is recommended " +
                                             "to recalculate any caches (those for faster startup) and resave any files. Even if it seems to load fine, it might cause subtle bugs.\n");
            }
#endif
            return(true);
        }