Пример #1
0
        internal static void SerializeReferences(Serialization.GraphSerializationContext ctx)
        {
            var links = GetModifiersOfType <NodeLink2>();

            ctx.writer.Write(links.Count);
            foreach (var link in links)
            {
                ctx.writer.Write(link.uniqueID);
                ctx.SerializeNodeReference(link.startNode);
                ctx.SerializeNodeReference(link.endNode);
                ctx.SerializeNodeReference(link.connectedNode1);
                ctx.SerializeNodeReference(link.connectedNode2);
                ctx.SerializeVector3(link.clamped1);
                ctx.SerializeVector3(link.clamped2);
                ctx.writer.Write(link.postScanCalled);
            }
        }
Пример #2
0
 public override void DeserializeNode(GraphSerializationContext ctx)
 {
     base.DeserializeNode(ctx);
     this.position = new Int3(ctx.reader.ReadInt32(), ctx.reader.ReadInt32(), ctx.reader.ReadInt32());
     this.gridFlags = ctx.reader.ReadUInt16();
 }
Пример #3
0
 /** Used to serialize references to other nodes e.g connections.
  * Use the GraphSerializationContext.GetNodeIdentifier and
  * GraphSerializationContext.GetNodeFromIdentifier methods
  * for serialization and deserialization respectively.
  *
  * Nodes must override this method and serialize their connections.
  * Graph generators do not need to call this method, it will be called automatically on all
  * nodes at the correct time by the serializer.
  */
 public virtual void SerializeReferences(GraphSerializationContext ctx)
 {
 }
Пример #4
0
        public virtual void DeserializeNode(GraphSerializationContext ctx)
        {
            Penalty = ctx.reader.ReadUInt32();
            Flags = ctx.reader.ReadUInt32();

            // Set the correct graph index (which might have changed, e.g if loading additively)
            GraphIndex = (uint)ctx.graphIndex;
        }
Пример #5
0
		public void SerializeSettings ( GraphSerializationContext ctx ) {
			ctx.writer.Write ((int)type);
			ctx.writer.Write (diameter);
			ctx.writer.Write (height);
			ctx.writer.Write (collisionOffset);
			ctx.writer.Write ((int)rayDirection);
			ctx.writer.Write ((int)mask);
			ctx.writer.Write ((int)heightMask);
			ctx.writer.Write (fromHeight);
			ctx.writer.Write (thickRaycast);
			ctx.writer.Write (thickRaycastDiameter);

			ctx.writer.Write (unwalkableWhenNoGround);
			ctx.writer.Write (use2D);
			ctx.writer.Write (collisionCheck);
			ctx.writer.Write (heightCheck);
		}
Пример #6
0
		public virtual void SerializeSettings ( GraphSerializationContext ctx ) {

			ctx.writer.Write (guid.ToByteArray());
			ctx.writer.Write (initialPenalty);
			ctx.writer.Write (open);
			ctx.writer.Write (name);
			ctx.writer.Write (drawGizmos);
			ctx.writer.Write (infoScreenOpen);

			for ( int i = 0; i < 4; i++ ) {
				for ( int j = 0; j < 4; j++ ) {
					ctx.writer.Write (matrix.GetRow(i)[j]);
				}
			}
		}
Пример #7
0
        internal static void DeserializeReferences(Serialization.GraphSerializationContext ctx)
        {
            var count = ctx.reader.ReadInt32();

            for (var i = 0; i < count; i++)
            {
                var linkID         = ctx.reader.ReadUInt64();
                var startNode      = ctx.DeserializeNodeReference();
                var endNode        = ctx.DeserializeNodeReference();
                var connectedNode1 = ctx.DeserializeNodeReference();
                var connectedNode2 = ctx.DeserializeNodeReference();
                var clamped1       = ctx.DeserializeVector3();
                var clamped2       = ctx.DeserializeVector3();
                var postScanCalled = ctx.reader.ReadBoolean();

                GraphModifier link;
                if (usedIDs.TryGetValue(linkID, out link))
                {
                    var link2 = link as NodeLink2;
                    if (link2 != null)
                    {
                        if (startNode != null)
                        {
                            reference[startNode] = link2;
                        }
                        if (endNode != null)
                        {
                            reference[endNode] = link2;
                        }

                        // If any nodes happened to be registered right now
                        if (link2.startNode != null)
                        {
                            reference.Remove(link2.startNode);
                        }
                        if (link2.endNode != null)
                        {
                            reference.Remove(link2.endNode);
                        }

                        link2.startNode      = startNode as PointNode;
                        link2.endNode        = endNode as PointNode;
                        link2.connectedNode1 = connectedNode1;
                        link2.connectedNode2 = connectedNode2;
                        link2.postScanCalled = postScanCalled;
                        link2.clamped1       = clamped1;
                        link2.clamped2       = clamped2;
                    }
                    else
                    {
                        throw new System.Exception(
                                  "Tried to deserialize a NodeLink2 reference, but the link was not of the correct type or it has been destroyed.\nIf a NodeLink2 is included in serialized graph data, the same NodeLink2 component must be present in the scene when loading the graph data.");
                    }
                }
                else
                {
                    throw new System.Exception(
                              "Tried to deserialize a NodeLink2 reference, but the link could not be found in the scene.\nIf a NodeLink2 is included in serialized graph data, the same NodeLink2 component must be present in the scene when loading the graph data.");
                }
            }
        }
Пример #8
0
		public override void DeserializeReferences (GraphSerializationContext ctx)
		{
			int count = ctx.reader.ReadInt32();
			if (count == -1) {
				connections = null;
				connectionCosts = null;
			} else {
				connections = new GraphNode[count];
				connectionCosts = new uint[count];
				
				for (int i=0;i<count;i++) {
					connections[i] = ctx.GetNodeFromIdentifier (ctx.reader.ReadInt32());
					connectionCosts[i] = ctx.reader.ReadUInt32();
				}
			}
		}
Пример #9
0
		public override void DeserializeExtraInfo (GraphSerializationContext ctx) {
			//NavMeshGraph.DeserializeMeshNodes (this,nodes,bytes);
			
			System.IO.BinaryReader reader = ctx.reader;
			
			tileXCount = reader.ReadInt32();
			
			if (tileXCount < 0) return;
				
			tileZCount = reader.ReadInt32();
			
			tiles = new NavmeshTile[tileXCount * tileZCount];
			
			//Make sure mesh nodes can reference this graph
			TriangleMeshNode.SetNavmeshHolder (ctx.graphIndex, this);
			
			for (int z=0;z<tileZCount;z++) {
				for (int x=0;x<tileXCount;x++) {
					
					int tileIndex = x + z*tileXCount;
					int tx = reader.ReadInt32();
					if (tx < 0) throw new System.Exception ("Invalid tile coordinates (x < 0)");
					
					int tz = reader.ReadInt32();
					if (tz < 0) throw new System.Exception ("Invalid tile coordinates (z < 0)");
					
					// This is not the origin of a large tile. Refer back to that tile.
					if (tx != x || tz != z) {
						tiles[tileIndex] = tiles[tz*tileXCount + tx];
						continue;
					}
					
					NavmeshTile tile = new NavmeshTile ();
					
					tile.x = tx;
					tile.z = tz;
					tile.w = reader.ReadInt32();
					tile.d = reader.ReadInt32();
					tile.bbTree = new BBTree (tile);
					
					tiles[tileIndex] = tile;
					
					int trisCount = reader.ReadInt32 ();
					
					if (trisCount % 3 != 0) throw new System.Exception ("Corrupt data. Triangle indices count must be divisable by 3. Got " + trisCount);
					
					tile.tris = new int[trisCount];
					for (int i=0;i<tile.tris.Length;i++) tile.tris[i] = reader.ReadInt32();
					
					tile.verts = new Int3[reader.ReadInt32()];
					for (int i=0;i<tile.verts.Length;i++) {
						tile.verts[i] = new Int3 (reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
					}
					
					int nodeCount = reader.ReadInt32();
					tile.nodes = new TriangleMeshNode[nodeCount];
					
					//Prepare for storing in vertex indices
					tileIndex <<= TileIndexOffset;
					
					for (int i=0;i<tile.nodes.Length;i++) {
						TriangleMeshNode node = new TriangleMeshNode (active);
						tile.nodes[i] = node;
						node.GraphIndex = (uint)ctx.graphIndex;
						
						node.DeserializeNode (ctx);
						node.v0 = tile.tris[i*3+0] | tileIndex;
						node.v1 = tile.tris[i*3+1] | tileIndex;
						node.v2 = tile.tris[i*3+2] | tileIndex;
						node.UpdatePositionFromVertices();
						
						tile.bbTree.Insert (node);
					}
				}
			}
		}
Пример #10
0
		/** Serializes Node Info.
		 * Should serialize:
		 * - Base
		 *    - Node Flags
		 *    - Node Penalties
		 *    - Node 
		 * - Node Positions (if applicable)
		 * - Any other information necessary to load the graph in-game
		 * All settings marked with json attributes (e.g JsonMember) have already been
		 * saved as graph settings and do not need to be handled here.
		 * 
		 * It is not necessary for this implementation to be forward or backwards compatible.
		 * 
		 * \see 
		 */
		public override void SerializeExtraInfo (GraphSerializationContext ctx) {
			
			System.IO.BinaryWriter writer = ctx.writer;
			
			if (tiles == null) {
				writer.Write (-1);
				return;
			}
			writer.Write (tileXCount);
			writer.Write (tileZCount);
			
			for (int z=0;z<tileZCount;z++) {
				for (int x=0;x<tileXCount;x++) {
					NavmeshTile tile = tiles[x + z*tileXCount];
					
					if (tile == null) {
						throw new System.Exception ("NULL Tile");
						//writer.Write (-1);
						//continue;
					}
					
					writer.Write(tile.x);
					writer.Write(tile.z);
					
					Debug.Log (tile.x + " " + tile.z + " " + x + " " + z);
					if (tile.x != x || tile.z != z) continue;
					
					writer.Write(tile.w);
					writer.Write(tile.d);
					
					writer.Write (tile.tris.Length);
					Debug.Log ("Tris saved " + tile.tris.Length);
					for (int i=0;i<tile.tris.Length;i++) writer.Write (tile.tris[i]);
					
					writer.Write (tile.verts.Length);
					for (int i=0;i<tile.verts.Length;i++) {
						writer.Write (tile.verts[i].x);
						writer.Write (tile.verts[i].y);
						writer.Write (tile.verts[i].z);
					}
					
					writer.Write (tile.nodes.Length);
					for (int i=0;i<tile.nodes.Length;i++) {
						tile.nodes[i].SerializeNode (ctx);
					}
				}
			}
			
			
			
			//return NavMeshGraph.SerializeMeshNodes (this,nodes);
		}
Пример #11
0
        /** Deserializes extra graph info.
         * Extra graph info is specified by the graph types.
         * \see Pathfinding.NavGraph.DeserializeExtraInfo
         * \note Stored in files named "graph#_extra.binary" where # is the graph number.
         */
        public void DeserializeExtraInfo()
        {
            bool anySerialized = false;

            // Loop through all graphs and deserialize the extra info
            // if there is any such info in the zip file
            for (int i = 0; i < graphs.Length; i++)
            {
                var entry = zip["graph" + i + "_extra" + binaryExt];
                if (entry == null)
                {
                    continue;
                }

                anySerialized = true;

                var str = new MemoryStream();

                entry.Extract(str);
                str.Seek(0, SeekOrigin.Begin);

                var reader = new BinaryReader(str);

                var ctx = new GraphSerializationContext(reader, null, i + graphIndexOffset);

                // Call the graph to process the data
                graphs[i].DeserializeExtraInfo(ctx);
            }

            if (!anySerialized)
            {
                return;
            }

            // Sanity check
            // Make sure the graphs don't contain destroyed nodes
            int totCount = 0;

            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] == null)
                {
                    continue;
                }
                graphs[i].GetNodes(delegate(GraphNode node) {
                    totCount = Math.Max(node.NodeIndex, totCount);
                    if (node.NodeIndex == -1)
                    {
                        Debug.LogError("Graph contains destroyed nodes. This is a bug.");
                    }
                    return(true);
                });
            }

            {
                // Get the file containing the list of all node indices
                // This is correlated with the new indices of the nodes and a mapping from old to new
                // is done so that references can be resolved
                var entry = zip["graph_references" + binaryExt];
                if (entry == null)
                {
                    throw new Exception("Node references not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
                }

                var str = new MemoryStream();
                entry.Extract(str);
                str.Seek(0, SeekOrigin.Begin);

                var reader = new BinaryReader(str);

                int count    = reader.ReadInt32();
                var int2Node = new GraphNode[count + 1];

                try {
                    for (int i = 0; i < graphs.Length; i++)
                    {
                        if (graphs[i] == null)
                        {
                            continue;
                        }
                        graphs[i].GetNodes(node => {
                            int2Node[reader.ReadInt32()] = node;
                            return(true);
                        });
                    }
                } catch (Exception e) {
                    throw new Exception("Some graph(s) has thrown an exception during GetNodes, or some graph(s) have deserialized more or fewer nodes than were serialized", e);
                }

#if NETFX_CORE
                reader.Dispose();
#else
                reader.Close();
#endif

                // Deserialize node references
                for (int i = 0; i < graphs.Length; i++)
                {
                    if (graphs[i] == null)
                    {
                        continue;
                    }

                    entry = zip["graph" + i + "_references" + binaryExt];
                    if (entry == null)
                    {
                        throw new Exception("Node references for graph " + i + " not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
                    }

                    str = new MemoryStream();
                    entry.Extract(str);
                    str.Seek(0, SeekOrigin.Begin);

                    reader = new BinaryReader(str);


                    var ctx = new GraphSerializationContext(reader, int2Node, i + graphIndexOffset);

                    graphs[i].GetNodes(delegate(GraphNode node) {
                        node.DeserializeReferences(ctx);
                        return(true);
                    });
                }
            }
        }
Пример #12
0
        /** Deserializes graph settings.
         * \note Stored in files named "graph#.json" where # is the graph number.
         */
        public NavGraph[] DeserializeGraphs()
        {
            // Allocate a list of graphs to be deserialized
            graphs = new NavGraph[meta.graphs];

            int nonNull = 0;

            for (int i = 0; i < meta.graphs; i++)
            {
                // Get the graph type from the metadata we deserialized earlier
                var tp = meta.GetGraphType(i);

                // Graph was null when saving, ignore
                if (System.Type.Equals(tp, null))
                {
                    continue;
                }

                nonNull++;

                var entry = zip["graph" + i + jsonExt];

                if (entry == null)
                {
                    throw new FileNotFoundException("Could not find data for graph " + i + " in zip. Entry 'graph+" + i + jsonExt + "' does not exist");
                }

                // Create a new graph of the right type
                NavGraph graph = data.CreateGraph(tp);
                graph.graphIndex = (uint)(i + graphIndexOffset);

#if !ASTAR_NO_JSON
                var entryText = GetString(entry);

                var reader = new JsonReader(entryText, readerSettings);

                reader.PopulateObject(ref graph);
#else
                var mem = new MemoryStream();
                entry.Extract(mem);
                mem.Position = 0;
                var reader = new BinaryReader(mem);
                var ctx    = new GraphSerializationContext(reader, null, i + graphIndexOffset);
                graph.DeserializeSettings(ctx);
#endif

                graphs[i] = graph;
                if (graphs[i].guid.ToString() != meta.guids[i])
                {
                    throw new Exception("Guid in graph file not equal to guid defined in meta file. Have you edited the data manually?\n" + graphs[i].guid + " != " + meta.guids[i]);
                }
            }

            // Remove any null entries from the list
            var compressed = new NavGraph[nonNull];
            nonNull = 0;
            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] != null)
                {
                    compressed[nonNull] = graphs[i];
                    nonNull++;
                }
            }

            graphs = compressed;

            return(graphs);
        }
Пример #13
0
        public void DeserializeExtraInfo()
        {
            bool flag = false;

            for (int i = 0; i < this.graphs.Length; i++)
            {
                ZipEntry zipEntry = this.zip["graph" + i + "_extra.binary"];
                if (zipEntry != null)
                {
                    flag = true;
                    MemoryStream memoryStream = new MemoryStream();
                    zipEntry.Extract(memoryStream);
                    memoryStream.Seek(0L, 0);
                    BinaryReader reader2           = new BinaryReader(memoryStream);
                    GraphSerializationContext ctx2 = new GraphSerializationContext(reader2, null, i);
                    this.graphs[i].DeserializeExtraInfo(ctx2);
                }
            }
            if (!flag)
            {
                return;
            }
            int totCount = 0;

            for (int j = 0; j < this.graphs.Length; j++)
            {
                if (this.graphs[j] != null)
                {
                    this.graphs[j].GetNodes(delegate(GraphNode node)
                    {
                        totCount = Math.Max(node.NodeIndex, totCount);
                        if (node.NodeIndex == -1)
                        {
                            Debug.LogError("Graph contains destroyed nodes. This is a bug.");
                        }
                        return(true);
                    });
                }
            }
            ZipEntry zipEntry2 = this.zip["graph_references.binary"];

            if (zipEntry2 == null)
            {
                throw new Exception("Node references not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
            }
            MemoryStream memoryStream2 = new MemoryStream();

            zipEntry2.Extract(memoryStream2);
            memoryStream2.Seek(0L, 0);
            BinaryReader reader = new BinaryReader(memoryStream2);
            int          num    = reader.ReadInt32();

            GraphNode[] int2Node = new GraphNode[num + 1];
            try
            {
                for (int k = 0; k < this.graphs.Length; k++)
                {
                    if (this.graphs[k] != null)
                    {
                        this.graphs[k].GetNodes(delegate(GraphNode node)
                        {
                            int2Node[reader.ReadInt32()] = node;
                            return(true);
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Some graph(s) has thrown an exception during GetNodes, or some graph(s) have deserialized more or fewer nodes than were serialized", ex);
            }
            reader.Close();
            for (int l = 0; l < this.graphs.Length; l++)
            {
                if (this.graphs[l] != null)
                {
                    zipEntry2 = this.zip["graph" + l + "_references.binary"];
                    if (zipEntry2 == null)
                    {
                        throw new Exception("Node references for graph " + l + " not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
                    }
                    memoryStream2 = new MemoryStream();
                    zipEntry2.Extract(memoryStream2);
                    memoryStream2.Seek(0L, 0);
                    reader = new BinaryReader(memoryStream2);
                    GraphSerializationContext ctx = new GraphSerializationContext(reader, int2Node, l);
                    this.graphs[l].GetNodes(delegate(GraphNode node)
                    {
                        node.DeserializeReferences(ctx);
                        return(true);
                    });
                }
            }
        }
Пример #14
0
        public void SerializeExtraInfo()
        {
            if (!this.settings.nodes)
            {
                return;
            }
            int totCount = 0;

            for (int i = 0; i < this.graphs.Length; i++)
            {
                if (this.graphs[i] != null)
                {
                    this.graphs[i].GetNodes(delegate(GraphNode node)
                    {
                        totCount = Math.Max(node.NodeIndex, totCount);
                        if (node.NodeIndex == -1)
                        {
                            Debug.LogError("Graph contains destroyed nodes. This is a bug.");
                        }
                        return(true);
                    });
                }
            }
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter wr           = new BinaryWriter(memoryStream);

            wr.Write(totCount);
            int c = 0;

            for (int j = 0; j < this.graphs.Length; j++)
            {
                if (this.graphs[j] != null)
                {
                    this.graphs[j].GetNodes(delegate(GraphNode node)
                    {
                        c = Math.Max(node.NodeIndex, c);
                        wr.Write(node.NodeIndex);
                        return(true);
                    });
                }
            }
            if (c != totCount)
            {
                throw new Exception("Some graphs are not consistent in their GetNodes calls, sequential calls give different results.");
            }
            byte[] bytes = memoryStream.ToArray();
            wr.Close();
            this.AddChecksum(bytes);
            this.zip.AddEntry("graph_references.binary", bytes);
            for (int k = 0; k < this.graphs.Length; k++)
            {
                if (this.graphs[k] != null)
                {
                    MemoryStream memoryStream2    = new MemoryStream();
                    BinaryWriter binaryWriter     = new BinaryWriter(memoryStream2);
                    GraphSerializationContext ctx = new GraphSerializationContext(binaryWriter);
                    this.graphs[k].SerializeExtraInfo(ctx);
                    byte[] bytes2 = memoryStream2.ToArray();
                    binaryWriter.Close();
                    this.AddChecksum(bytes2);
                    this.zip.AddEntry("graph" + k + "_extra.binary", bytes2);
                    memoryStream2 = new MemoryStream();
                    binaryWriter  = new BinaryWriter(memoryStream2);
                    ctx           = new GraphSerializationContext(binaryWriter);
                    this.graphs[k].GetNodes(delegate(GraphNode node)
                    {
                        node.SerializeReferences(ctx);
                        return(true);
                    });
                    binaryWriter.Close();
                    bytes2 = memoryStream2.ToArray();
                    this.AddChecksum(bytes2);
                    this.zip.AddEntry("graph" + k + "_references.binary", bytes2);
                }
            }
        }
Пример #15
0
        public byte[] SerializeExtraInfoBytes()
        {
            AstarSerializer._SerializeExtraInfoBytes_c__AnonStorey20 _SerializeExtraInfoBytes_c__AnonStorey = new AstarSerializer._SerializeExtraInfoBytes_c__AnonStorey20();
            if (!this.settings.nodes)
            {
                return(null);
            }
            _SerializeExtraInfoBytes_c__AnonStorey.totCount = 0;
            for (int i = 0; i < this.graphs.Length; i++)
            {
                if (this.graphs[i] != null)
                {
                    this.graphs[i].GetNodes(delegate(GraphNode node)
                    {
                        _SerializeExtraInfoBytes_c__AnonStorey.totCount = Math.Max(node.NodeIndex, _SerializeExtraInfoBytes_c__AnonStorey.totCount);
                        if (node.NodeIndex == -1)
                        {
                            Debug.LogError("Graph contains destroyed nodes. This is a bug.");
                        }
                        return(true);
                    });
                }
            }
            MemoryStream memoryStream = new MemoryStream();

            _SerializeExtraInfoBytes_c__AnonStorey.wr = new BinaryWriter(memoryStream);
            _SerializeExtraInfoBytes_c__AnonStorey.wr.Write(_SerializeExtraInfoBytes_c__AnonStorey.totCount);
            int c = 0;

            for (int j = 0; j < this.graphs.Length; j++)
            {
                if (this.graphs[j] != null)
                {
                    this.graphs[j].GetNodes(delegate(GraphNode node)
                    {
                        c = Math.Max(node.NodeIndex, c);
                        _SerializeExtraInfoBytes_c__AnonStorey.wr.Write(node.NodeIndex);
                        return(true);
                    });
                }
            }
            if (c != _SerializeExtraInfoBytes_c__AnonStorey.totCount)
            {
                throw new Exception("Some graphs are not consistent in their GetNodes calls, sequential calls give different results.");
            }
            for (int k = 0; k < this.graphs.Length; k++)
            {
                if (this.graphs[k] != null)
                {
                    GraphSerializationContext ctx = new GraphSerializationContext(_SerializeExtraInfoBytes_c__AnonStorey.wr);
                    this.graphs[k].SerializeExtraInfo(ctx);
                    ctx = new GraphSerializationContext(_SerializeExtraInfoBytes_c__AnonStorey.wr);
                    this.graphs[k].GetNodes(delegate(GraphNode node)
                    {
                        node.SerializeReferences(ctx);
                        return(true);
                    });
                }
            }
            _SerializeExtraInfoBytes_c__AnonStorey.wr.Close();
            return(memoryStream.ToArray());
        }
Пример #16
0
		public override void DeserializeExtraInfo (GraphSerializationContext ctx)
		{
			
			int count = ctx.reader.ReadInt32();
			if (count == -1) {
				nodes = null;
				return;
			}
			
			nodes = new GridNode[count];
			
			for (int i=0;i<nodes.Length;i++) {
				nodes[i] = new GridNode (active);
				nodes[i].DeserializeNode(ctx);
			}
		}
Пример #17
0
		public override void DeserializeNode (GraphSerializationContext ctx)
		{
			base.DeserializeNode (ctx);
			position = new Int3 (ctx.reader.ReadInt32(), ctx.reader.ReadInt32(), ctx.reader.ReadInt32());
		}
Пример #18
0
		/** Deserializes extra graph info.
		 * Extra graph info is specified by the graph types.
		 * \see Pathfinding.NavGraph.DeserializeExtraInfo
		 * \note Stored in files named "graph#_extra.binary" where # is the graph number.
		 */
		public void DeserializeExtraInfo () {
			
			bool anySerialized = false;
			
			for (int i=0;i<graphs.Length;i++) {
				ZipEntry entry = zip["graph"+i+"_extra"+binaryExt];
				if (entry == null) continue;
				
				anySerialized = true;
				
				MemoryStream str = new MemoryStream();
				
				entry.Extract (str);
				str.Seek (0, SeekOrigin.Begin);
				
				BinaryReader reader = new BinaryReader (str);
				//byte[] bytes = str.ToArray();
				
				GraphSerializationContext ctx = new GraphSerializationContext(reader, null, i);
				
				graphs[i].DeserializeExtraInfo (ctx);
			}
			
			if (!anySerialized) {
				return;
			}
			
			int totCount = 0;
			for (int i=0;i<graphs.Length;i++) {
				if (graphs[i] == null) continue;
				graphs[i].GetNodes (delegate (GraphNode node) {
					
					totCount = System.Math.Max (node.NodeIndex, totCount);
					if (node.NodeIndex == -1) {
						Debug.LogError ("Graph contains destroyed nodes. This is a bug.");
					}
					return true;
				});
			}
			
			{
			
				// Get the file containing the list of all node indices
				// This is correlated with the new indices of the nodes and a mapping from old to new
				// is done so that references can be resolved
				ZipEntry entry = zip["graph_references"+binaryExt];
				if (entry == null) throw new System.Exception ("Node references not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
				
				MemoryStream str = new MemoryStream();
				entry.Extract (str);
				str.Seek (0, SeekOrigin.Begin);
				
				BinaryReader reader = new BinaryReader (str);
				
				int count = reader.ReadInt32();
				GraphNode[] int2Node = new GraphNode[count+1];
				
				try {
					for (int i=0;i<graphs.Length;i++) {
						if (graphs[i] == null) continue;
						graphs[i].GetNodes (delegate (GraphNode node) {
							int2Node[reader.ReadInt32()] = node;
							return true;
						});
					}
				} catch (System.Exception e) {
					throw new System.Exception ("Some graph(s) has thrown an exception during GetNodes, or some graph(s) have deserialized more or fewer nodes than were serialized", e);
				}
				
				reader.Close();
				
				// Deserialize node references
				for (int i=0;i<graphs.Length;i++) {
					if (graphs[i] == null) continue;
					
					entry = zip["graph"+i+"_references"+binaryExt];
					if (entry == null) throw new System.Exception ("Node references for graph " +i + " not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
					
					str = new MemoryStream();
					entry.Extract (str);
					str.Seek (0, SeekOrigin.Begin);
					
					reader = new BinaryReader (str);
					
					
					GraphSerializationContext ctx = new GraphSerializationContext(reader, int2Node, i);
					
					graphs[i].GetNodes (delegate (GraphNode node) {
						node.DeserializeReferences (ctx);
						return true;
					});
				}
			}
		}
Пример #19
0
        /** Deserializes graph settings.
         * \note Stored in files named "graph#.json" where # is the graph number.
         */
        public NavGraph[] DeserializeGraphs()
        {
            //for (int j=0;j<1;j++) {
            //System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            //watch.Start();

            graphs = new NavGraph[meta.graphs];

            int nonNull = 0;

            for (int i = 0; i < meta.graphs; i++)
            {
                Type tp = meta.GetGraphType(i);

                //Graph was null when saving, ignore
                if (System.Type.Equals(tp, null))
                {
                    continue;
                }

                nonNull++;

                ZipEntry entry = zip["graph" + i + jsonExt];

                if (entry == null)
                {
                    throw new FileNotFoundException("Could not find data for graph " + i + " in zip. Entry 'graph+" + i + jsonExt + "' does not exist");
                }


                NavGraph tmp = data.CreateGraph(tp);                //(NavGraph)System.Activator.CreateInstance(tp);

#if !ASTAR_NO_JSON
                String entryText = GetString(entry);

                JsonReader reader = new JsonReader(entryText, readerSettings);

                //NavGraph graph = tmp.Deserialize(reader);//reader.Deserialize<NavGraph>();
                reader.PopulateObject(ref tmp);
#else
                var mem = new MemoryStream();
                entry.Extract(mem);
                mem.Position = 0;
                var reader = new BinaryReader(mem);
                var ctx    = new GraphSerializationContext(reader, null, i);
                tmp.DeserializeSettings(ctx);
#endif

                graphs[i] = tmp;
                if (graphs[i].guid.ToString() != meta.guids[i])
                {
                    throw new System.Exception("Guid in graph file not equal to guid defined in meta file. Have you edited the data manually?\n" + graphs[i].guid.ToString() + " != " + meta.guids[i]);
                }

                //NavGraph graph = (NavGraph)JsonConvert.DeserializeObject (entryText,tp,settings);
            }

            NavGraph[] compressed = new NavGraph[nonNull];
            nonNull = 0;
            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] != null)
                {
                    compressed[nonNull] = graphs[i];
                    nonNull++;
                }
            }

            graphs = compressed;

            return(graphs);

            //watch.Stop();
            //Debug.Log ((watch.ElapsedTicks*0.0001).ToString ("0.00"));
            //}
        }
Пример #20
0
		public override void SerializeSettings (GraphSerializationContext ctx) {
			base.SerializeSettings(ctx);
			ctx.writer.Write(aspectRatio);
			ctx.SerializeVector3(rotation);
			ctx.SerializeVector3(center);
			ctx.SerializeVector3((Vector3)unclampedSize);
			ctx.writer.Write(nodeSize);
			// collision
			collision.SerializeSettings(ctx);

			ctx.writer.Write(maxClimb);
			ctx.writer.Write(maxClimbAxis);
			ctx.writer.Write(maxSlope);
			ctx.writer.Write(erodeIterations);
			ctx.writer.Write(erosionUseTags);
			ctx.writer.Write(erosionFirstTag);
			ctx.writer.Write(autoLinkGrids);
			ctx.writer.Write((int)neighbours);
			ctx.writer.Write(cutCorners);
			ctx.writer.Write(penaltyPosition);
			ctx.writer.Write(penaltyPositionFactor);
			ctx.writer.Write(penaltyAngle);
			ctx.writer.Write(penaltyAngleFactor);
			ctx.writer.Write(penaltyAnglePower);
			ctx.writer.Write(isometricAngle);
			ctx.writer.Write(uniformEdgeCosts);
		}
Пример #21
0
		/** Deserializes graph type specific node data.
		 * \see SerializeExtraInfo
		 */
		public virtual void DeserializeExtraInfo (GraphSerializationContext ctx) {
		}
Пример #22
0
		public override void DeserializeSettings (GraphSerializationContext ctx) {
			base.DeserializeSettings(ctx);

			aspectRatio = ctx.reader.ReadSingle();
			rotation = ctx.DeserializeVector3();
			center = ctx.DeserializeVector3();
			unclampedSize = (Vector2)ctx.DeserializeVector3();
			nodeSize = ctx.reader.ReadSingle();
			collision.DeserializeSettings(ctx);
			maxClimb = ctx.reader.ReadSingle();
			maxClimbAxis = ctx.reader.ReadInt32();
			maxSlope = ctx.reader.ReadSingle();
			erodeIterations = ctx.reader.ReadInt32();
			erosionUseTags = ctx.reader.ReadBoolean();
			erosionFirstTag = ctx.reader.ReadInt32();
			autoLinkGrids = ctx.reader.ReadBoolean();
			neighbours = (NumNeighbours)ctx.reader.ReadInt32();
			cutCorners = ctx.reader.ReadBoolean();
			penaltyPosition = ctx.reader.ReadBoolean();
			penaltyPositionFactor = ctx.reader.ReadSingle();
			penaltyAngle = ctx.reader.ReadBoolean();
			penaltyAngleFactor = ctx.reader.ReadSingle();
			penaltyAnglePower = ctx.reader.ReadSingle();
			isometricAngle = ctx.reader.ReadSingle();
			uniformEdgeCosts = ctx.reader.ReadBoolean();
		}
Пример #23
0
		public virtual void DeserializeSettings ( GraphSerializationContext ctx ) {

			guid = new Guid(ctx.reader.ReadBytes (16));
			initialPenalty = ctx.reader.ReadUInt32 ();
			open = ctx.reader.ReadBoolean();
			name = ctx.reader.ReadString();
			drawGizmos = ctx.reader.ReadBoolean();
			infoScreenOpen = ctx.reader.ReadBoolean();

			for ( int i = 0; i < 4; i++ ) {
				Vector4 row = Vector4.zero;
				for ( int j = 0; j < 4; j++ ) {
					row[j] = ctx.reader.ReadSingle ();
				}
				matrix.SetRow (i, row);
			}
		}
Пример #24
0
		/** Serializes the graph settings to JSON and returns the data */
		public byte[] Serialize (NavGraph graph) {
#if !ASTAR_NO_JSON
			// Grab a cached string builder to avoid allocations
			var output = GetStringBuilder ();
			var writer = new JsonWriter (output,writerSettings);
			writer.Write (graph);
			
			return encoding.GetBytes (output.ToString());
#else
			var mem = new System.IO.MemoryStream();
			var writer = new System.IO.BinaryWriter(mem);
			var ctx = new GraphSerializationContext (writer);
			graph.SerializeSettings (ctx);
			return mem.ToArray();
#endif
		}
Пример #25
0
		public void DeserializeSettings ( GraphSerializationContext ctx ) {
			type = (ColliderType)ctx.reader.ReadInt32();
			diameter = ctx.reader.ReadSingle ();
			height = ctx.reader.ReadSingle ();
			collisionOffset = ctx.reader.ReadSingle ();
			rayDirection = (RayDirection)ctx.reader.ReadInt32 ();
			mask = (LayerMask)ctx.reader.ReadInt32 ();
			heightMask = (LayerMask)ctx.reader.ReadInt32 ();
			fromHeight = ctx.reader.ReadSingle ();
			thickRaycast = ctx.reader.ReadBoolean ();
			thickRaycastDiameter = ctx.reader.ReadSingle ();

			unwalkableWhenNoGround = ctx.reader.ReadBoolean();
			use2D = ctx.reader.ReadBoolean();
			collisionCheck = ctx.reader.ReadBoolean();
			heightCheck = ctx.reader.ReadBoolean();
		}
Пример #26
0
		public void SerializeExtraInfo () {
			if (!settings.nodes) return;
			
			int totCount = 0;
			for (int i=0;i<graphs.Length;i++) {
				if (graphs[i] == null) continue;
				graphs[i].GetNodes (node => {
					totCount = Math.Max (node.NodeIndex, totCount);
					if (node.NodeIndex == -1) {
						Debug.LogError ("Graph contains destroyed nodes. This is a bug.");
					}
					return true;
				});
			}
			
			{
				var stream = new MemoryStream ();
				var wr = new BinaryWriter (stream);
				
				wr.Write (totCount);
				
				int c = 0;
				for (int i=0;i<graphs.Length;i++) {
					if (graphs[i] == null) continue;
					graphs[i].GetNodes (node => {
						c = Math.Max (node.NodeIndex, c);
						wr.Write (node.NodeIndex);
						return true;
					});
				}
				
				if (c != totCount) throw new Exception ("Some graphs are not consistent in their GetNodes calls, sequential calls give different results.");

				byte[] bytes = stream.ToArray ();
#if NETFX_CORE
				wr.Dispose();
#else
				wr.Close ();
#endif

				
				AddChecksum (bytes);
				zip.AddEntry ("graph_references"+binaryExt,bytes);
			}
					
			for (int i=0;i<graphs.Length;i++) {
				if (graphs[i] == null) continue;
				
				var stream = new MemoryStream ();
				var wr = new BinaryWriter (stream);
				var ctx = new GraphSerializationContext(wr);
				
				graphs[i].SerializeExtraInfo (ctx);
				byte[] bytes = stream.ToArray ();

#if NETFX_CORE
				wr.Dispose();
#else
				wr.Close ();
#endif
				
				AddChecksum (bytes);
				zip.AddEntry ("graph"+i+"_extra"+binaryExt,bytes);
				
				
				stream = new MemoryStream ();
				wr = new BinaryWriter (stream);
				ctx = new GraphSerializationContext(wr);
				graphs[i].GetNodes (delegate (GraphNode node) {
					node.SerializeReferences (ctx);
					return true;
				});

#if NETFX_CORE
				wr.Dispose();
#else
				wr.Close ();
#endif
				
				bytes = stream.ToArray ();
				
				AddChecksum (bytes);
				zip.AddEntry ("graph"+i+"_references"+binaryExt,bytes);
			}
		}
Пример #27
0
 public virtual void SerializeNode(GraphSerializationContext ctx)
 {
     //Write basic node data.
     ctx.writer.Write (Penalty);
     ctx.writer.Write (Flags);
 }
Пример #28
0
		/** Deserializes graph settings.
		 * \note Stored in files named "graph#.json" where # is the graph number.
		 */
		public NavGraph[] DeserializeGraphs () {
			// Allocate a list of graphs to be deserialized
			graphs = new NavGraph[meta.graphs];

			int nonNull = 0;

			for (int i=0;i<meta.graphs;i++) {
				// Get the graph type from the metadata we deserialized earlier
				var tp = meta.GetGraphType(i);
				
				// Graph was null when saving, ignore
				if (System.Type.Equals (tp, null)) continue;

				nonNull++;

				var entry = zip["graph"+i+jsonExt];
				
				if (entry == null)
					throw new FileNotFoundException ("Could not find data for graph "+i+" in zip. Entry 'graph+"+i+jsonExt+"' does not exist");

				// Create a new graph of the right type
				NavGraph graph = data.CreateGraph(tp);
				graph.graphIndex = (uint)(i + graphIndexOffset);

#if !ASTAR_NO_JSON
				var entryText = GetString(entry);
					
				var reader = new JsonReader(entryText,readerSettings);

				reader.PopulateObject (ref graph);
				
#else
				var mem = new MemoryStream ();
				entry.Extract(mem);
				mem.Position = 0;
				var reader = new BinaryReader (mem);
				var ctx = new GraphSerializationContext(reader, null, i + graphIndexOffset);
				graph.DeserializeSettings (ctx);
#endif

				graphs[i] = graph;
				if (graphs[i].guid.ToString () != meta.guids[i])
					throw new Exception ("Guid in graph file not equal to guid defined in meta file. Have you edited the data manually?\n"+graphs[i].guid+" != "+meta.guids[i]);
			}

			// Remove any null entries from the list
			var compressed = new NavGraph[nonNull];
			nonNull = 0;
			for ( int i=0;i<graphs.Length;i++) {
				if ( graphs[i] != null ) {
					compressed[nonNull] = graphs[i];
					nonNull++;
				}
			}

			graphs = compressed;

			return graphs;
		}
		public override void DeserializeNode (GraphSerializationContext ctx)
		{
			base.DeserializeNode (ctx);
			position = new Int3(ctx.reader.ReadInt32(), ctx.reader.ReadInt32(), ctx.reader.ReadInt32());
			gridFlags = ctx.reader.ReadUInt16();
#if ASTAR_LEVELGRIDNODE_FEW_LAYERS
			gridConnections = ctx.reader.ReadUInt16();
#else
			gridConnections = ctx.reader.ReadUInt32();
#endif
		}
Пример #30
0
		/** Deserializes extra graph info.
		 * Extra graph info is specified by the graph types.
		 * \see Pathfinding.NavGraph.DeserializeExtraInfo
		 * \note Stored in files named "graph#_extra.binary" where # is the graph number.
		 */
		public void DeserializeExtraInfo () {
			
			bool anySerialized = false;

			// Loop through all graphs and deserialize the extra info
			// if there is any such info in the zip file
			for (int i=0;i<graphs.Length;i++) {
				var entry = zip["graph"+i+"_extra"+binaryExt];
				if (entry == null) continue;
				
				anySerialized = true;
				
				var str = new MemoryStream();
				
				entry.Extract (str);
				str.Seek (0, SeekOrigin.Begin);
				
				var reader = new BinaryReader (str);
				
				var ctx = new GraphSerializationContext(reader, null, i + graphIndexOffset);

				// Call the graph to process the data
				graphs[i].DeserializeExtraInfo (ctx);
			}
			
			if (!anySerialized) {
				return;
			}

			// Sanity check
			// Make sure the graphs don't contain destroyed nodes
			int totCount = 0;
			for (int i=0;i<graphs.Length;i++) {
				if (graphs[i] == null) continue;
				graphs[i].GetNodes (delegate (GraphNode node) {
					
					totCount = Math.Max (node.NodeIndex, totCount);
					if (node.NodeIndex == -1) {
						Debug.LogError ("Graph contains destroyed nodes. This is a bug.");
					}
					return true;
				});
			}
			
			{
			
				// Get the file containing the list of all node indices
				// This is correlated with the new indices of the nodes and a mapping from old to new
				// is done so that references can be resolved
				var entry = zip["graph_references"+binaryExt];
				if (entry == null) throw new Exception ("Node references not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
				
				var str = new MemoryStream();
				entry.Extract (str);
				str.Seek (0, SeekOrigin.Begin);
				
				var reader = new BinaryReader (str);
				
				int count = reader.ReadInt32();
				var int2Node = new GraphNode[count+1];
				
				try {
					for (int i=0;i<graphs.Length;i++) {
						if (graphs[i] == null) continue;
						graphs[i].GetNodes (node => {
							int2Node[reader.ReadInt32()] = node;
							return true;
						});
					}
				} catch (Exception e) {
					throw new Exception ("Some graph(s) has thrown an exception during GetNodes, or some graph(s) have deserialized more or fewer nodes than were serialized", e);
				}

#if NETFX_CORE
				reader.Dispose();
#else
				reader.Close ();
#endif
				
				// Deserialize node references
				for (int i=0;i<graphs.Length;i++) {
					if (graphs[i] == null) continue;
					
					entry = zip["graph"+i+"_references"+binaryExt];
					if (entry == null) throw new Exception ("Node references for graph " +i + " not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
					
					str = new MemoryStream();
					entry.Extract (str);
					str.Seek (0, SeekOrigin.Begin);
					
					reader = new BinaryReader (str);
					
					
					var ctx = new GraphSerializationContext(reader, int2Node, i + graphIndexOffset);
					
					graphs[i].GetNodes (delegate (GraphNode node) {
						node.DeserializeReferences (ctx);
						return true;
					});
				}
			}
		}
Пример #31
0
		public override void SerializeExtraInfo (GraphSerializationContext ctx)
		{
			if (nodes == null) {
				ctx.writer.Write(-1);
				return;
			}
			
			ctx.writer.Write (nodes.Length);
			
			for (int i=0;i<nodes.Length;i++) {
				nodes[i].SerializeNode(ctx);
			}
		}
Пример #32
0
 public override void DeserializeReferences(GraphSerializationContext ctx)
 {
     int num = ctx.reader.ReadInt32();
     if (num == -1)
     {
         this.connections = null;
         this.connectionCosts = null;
     }
     else
     {
         this.connections = new GraphNode[num];
         this.connectionCosts = new uint[num];
         for (int i = 0; i < num; i++)
         {
             this.connections[i] = ctx.GetNodeFromIdentifier(ctx.reader.ReadInt32());
             this.connectionCosts[i] = ctx.reader.ReadUInt32();
         }
     }
 }
Пример #33
0
		public override void SerializeNode (GraphSerializationContext ctx)
		{
			base.SerializeNode (ctx);
			ctx.writer.Write (position.x);
			ctx.writer.Write (position.y);
			ctx.writer.Write (position.z);
		}
Пример #34
0
		public override void SerializeNode (GraphSerializationContext ctx)
		{
			base.SerializeNode (ctx);
			ctx.writer.Write(v0);
			ctx.writer.Write(v1);
			ctx.writer.Write(v2);
		}
Пример #35
0
		public override void SerializeReferences (GraphSerializationContext ctx)
		{
			if (connections == null) {
				ctx.writer.Write(-1);
			} else {
				ctx.writer.Write (connections.Length);
				for (int i=0;i<connections.Length;i++) {
					ctx.writer.Write (ctx.GetNodeIdentifier (connections[i]));
					ctx.writer.Write (connectionCosts[i]);
				}
			}
		}
Пример #36
0
		public override void DeserializeNode (GraphSerializationContext ctx)
		{
			base.DeserializeNode (ctx);
			v0 = ctx.reader.ReadInt32();
			v1 = ctx.reader.ReadInt32();
			v2 = ctx.reader.ReadInt32();
		}
Пример #37
0
        public void SerializeExtraInfo()
        {
            if (!settings.nodes)
            {
                return;
            }

            int totCount = 0;

            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] == null)
                {
                    continue;
                }
                graphs[i].GetNodes(delegate(GraphNode node) {
                    totCount = System.Math.Max(node.NodeIndex, totCount);
                    if (node.NodeIndex == -1)
                    {
                        Debug.LogError("Graph contains destroyed nodes. This is a bug.");
                    }
                    return(true);
                });
            }

            {
                MemoryStream stream = new MemoryStream();
                BinaryWriter wr     = new BinaryWriter(stream);

                wr.Write(totCount);

                int c = 0;
                for (int i = 0; i < graphs.Length; i++)
                {
                    if (graphs[i] == null)
                    {
                        continue;
                    }
                    graphs[i].GetNodes(delegate(GraphNode node) {
                        c = System.Math.Max(node.NodeIndex, c);
                        wr.Write(node.NodeIndex);
                        return(true);
                    });
                }

                if (c != totCount)
                {
                    throw new System.Exception("Some graphs are not consistent in their GetNodes calls, sequential calls give different results.");
                }

                byte[] bytes = stream.ToArray();
#if NETFX_CORE
                wr.Dispose();
#else
                wr.Close();
#endif


                AddChecksum(bytes);
                zip.AddEntry("graph_references" + binaryExt, bytes);
            }

            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] == null)
                {
                    continue;
                }

                MemoryStream stream           = new MemoryStream();
                BinaryWriter wr               = new BinaryWriter(stream);
                GraphSerializationContext ctx = new GraphSerializationContext(wr);

                graphs[i].SerializeExtraInfo(ctx);
                byte[] bytes = stream.ToArray();

#if NETFX_CORE
                wr.Dispose();
#else
                wr.Close();
#endif

                AddChecksum(bytes);
                zip.AddEntry("graph" + i + "_extra" + binaryExt, bytes);


                stream = new MemoryStream();
                wr     = new BinaryWriter(stream);
                ctx    = new GraphSerializationContext(wr);
                graphs[i].GetNodes(delegate(GraphNode node) {
                    node.SerializeReferences(ctx);
                    return(true);
                });

#if NETFX_CORE
                wr.Dispose();
#else
                wr.Close();
#endif

                bytes = stream.ToArray();

                AddChecksum(bytes);
                zip.AddEntry("graph" + i + "_references" + binaryExt, bytes);
            }
        }
Пример #38
0
		public override void DeserializeExtraInfo (GraphSerializationContext ctx)
		{
			
			uint graphIndex = (uint)active.astarData.GetGraphIndex(this);
			TriangleMeshNode.SetNavmeshHolder ((int)graphIndex,this);
			
			int c1 = ctx.reader.ReadInt32();
			int c2 = ctx.reader.ReadInt32();
			
			if (c1 == -1) {
				nodes = new TriangleMeshNode[0];
				_vertices = new Int3[0];
				originalVertices = new Vector3[0];
			}
			
			nodes = new TriangleMeshNode[c1];
			_vertices = new Int3[c2];
			originalVertices = new Vector3[c2];
			
			for (int i=0;i<c2;i++) {
				_vertices[i] = new Int3(ctx.reader.ReadInt32(), ctx.reader.ReadInt32(), ctx.reader.ReadInt32());
				originalVertices[i] = new Vector3(ctx.reader.ReadSingle(), ctx.reader.ReadSingle(), ctx.reader.ReadSingle());
			}
			
			bbTree = new BBTree(this);
			
			for (int i=0;i<c1;i++) {
				nodes[i] = new TriangleMeshNode(active);
				TriangleMeshNode node = nodes[i];
				node.DeserializeNode(ctx);
				node.GraphIndex = graphIndex;
				node.UpdatePositionFromVertices();
				bbTree.Insert (node);
			}
		}
Пример #39
0
        /** Deserializes extra graph info.
         * Extra graph info is specified by the graph types.
         * \see Pathfinding.NavGraph.DeserializeExtraInfo
         * \note Stored in files named "graph#_extra.binary" where # is the graph number.
         */
        public void DeserializeExtraInfo()
        {
            bool anySerialized = false;

            for (int i = 0; i < graphs.Length; i++)
            {
                ZipEntry entry = zip["graph" + i + "_extra" + binaryExt];
                if (entry == null)
                {
                    continue;
                }

                anySerialized = true;

                MemoryStream str = new MemoryStream();

                entry.Extract(str);
                str.Seek(0, SeekOrigin.Begin);

                BinaryReader reader = new BinaryReader(str);
                //byte[] bytes = str.ToArray();

                GraphSerializationContext ctx = new GraphSerializationContext(reader, null, i);

                graphs[i].DeserializeExtraInfo(ctx);
            }

            if (!anySerialized)
            {
                return;
            }

            int totCount = 0;

            for (int i = 0; i < graphs.Length; i++)
            {
                if (graphs[i] == null)
                {
                    continue;
                }
                graphs[i].GetNodes(delegate(GraphNode node) {
                    totCount = System.Math.Max(node.NodeIndex, totCount);
                    if (node.NodeIndex == -1)
                    {
                        Debug.LogError("Graph contains destroyed nodes. This is a bug.");
                    }
                    return(true);
                });
            }

            {
                // Get the file containing the list of all node indices
                // This is correlated with the new indices of the nodes and a mapping from old to new
                // is done so that references can be resolved
                ZipEntry entry = zip["graph_references" + binaryExt];
                if (entry == null)
                {
                    throw new System.Exception("Node references not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
                }

                MemoryStream str = new MemoryStream();
                entry.Extract(str);
                str.Seek(0, SeekOrigin.Begin);

                BinaryReader reader = new BinaryReader(str);

                int         count    = reader.ReadInt32();
                GraphNode[] int2Node = new GraphNode[count + 1];

                try {
                    for (int i = 0; i < graphs.Length; i++)
                    {
                        if (graphs[i] == null)
                        {
                            continue;
                        }
                        graphs[i].GetNodes(delegate(GraphNode node) {
                            int2Node[reader.ReadInt32()] = node;
                            return(true);
                        });
                    }
                } catch (System.Exception e) {
                    throw new System.Exception("Some graph(s) has thrown an exception during GetNodes, or some graph(s) have deserialized more or fewer nodes than were serialized", e);
                }

#if NETFX_CORE
                reader.Dispose();
#else
                reader.Close();
#endif

                // Deserialize node references
                for (int i = 0; i < graphs.Length; i++)
                {
                    if (graphs[i] == null)
                    {
                        continue;
                    }

                    entry = zip["graph" + i + "_references" + binaryExt];
                    if (entry == null)
                    {
                        throw new System.Exception("Node references for graph " + i + " not found in the data. Was this loaded from an older version of the A* Pathfinding Project?");
                    }

                    str = new MemoryStream();
                    entry.Extract(str);
                    str.Seek(0, SeekOrigin.Begin);

                    reader = new BinaryReader(str);


                    GraphSerializationContext ctx = new GraphSerializationContext(reader, int2Node, i);

                    graphs[i].GetNodes(delegate(GraphNode node) {
                        node.DeserializeReferences(ctx);
                        return(true);
                    });
                }
            }
        }
Пример #40
0
		public override void SerializeExtraInfo (GraphSerializationContext ctx)
		{
			if (nodes == null || originalVertices == null || _vertices == null || originalVertices.Length != _vertices.Length) {
				ctx.writer.Write (-1);
				ctx.writer.Write (-1);
				return;
			}
			ctx.writer.Write(nodes.Length);
			ctx.writer.Write(_vertices.Length);
			
			for (int i=0;i<_vertices.Length;i++) {
				ctx.writer.Write (_vertices[i].x);
				ctx.writer.Write (_vertices[i].y);
				ctx.writer.Write (_vertices[i].z);
				
				ctx.writer.Write (originalVertices[i].x);
				ctx.writer.Write (originalVertices[i].y);
				ctx.writer.Write (originalVertices[i].z);
			}
			
			for (int i=0;i<nodes.Length;i++) {
				nodes[i].SerializeNode (ctx);
			}
		}