示例#1
0
        /** Saves all graphs and also user connections, but does not close, nor opens the stream */
        public void SerializeGraphsPart(AstarSerializer serializer)
        {
            //AstarSerializer serializer = new AstarSerializer ();

            //serializer.OpenSerializeSettings ();
            SizeProfiler.Initialize();
            SizeProfiler.Begin("File", serializer.writerStream, false);
            SizeProfiler.Begin("Graphs init", serializer.writerStream);
            serializer.writerStream.Write(graphs.Length);
            serializer.writerStream.Write(graphs.Length);

            SizeProfiler.End("Graphs init", serializer.writerStream);

            int[] masks = new int[graphs.Length];

            for (int i = 0; i < graphs.Length; i++)
            {
                NavGraph graph = graphs[i];

                int tmpMask = serializer.mask;

                SizeProfiler.Begin("Graphs type " + i, serializer.writerStream);

                serializer.AddAnchor("Graph" + i);
                serializer.writerStream.Write(graph.GetType().Name);
                serializer.writerStream.Write(graph.guid.ToString());

                SizeProfiler.Begin("Graphs settings " + i, serializer.writerStream);

                //Set an unique prefix for all variables in this graph
                serializer.sPrefix = i.ToString();
                serializer.SerializeSettings(graph, active);
                serializer.sPrefix = "";

                masks[i]        = serializer.mask;
                serializer.mask = tmpMask;

                SizeProfiler.End("Graphs settings " + i, serializer.writerStream);
            }


            SizeProfiler.Begin("User Connections", serializer.writerStream);

            serializer.SerializeUserConnections(userConnections);

            SizeProfiler.End("User Connections", serializer.writerStream);
            //data = (serializer.writerStream.BaseStream as System.IO.MemoryStream).ToArray ();
            //serializer.Close ();
            SizeProfiler.End("File", serializer.writerStream);
            SizeProfiler.Log();
        }
示例#2
0
        //This is intended for quick saving of settings for e.g Undo operations
        public void DeSerializeSettings(NavGraph graph, AstarPath active)
        {
            ISerializableGraph serializeGraph = graph as ISerializableGraph;

            if (serializeGraph == null) {
                Debug.LogError ("The graph specified is not (de)serializable (how it could be serialized in the first place is a mystery) the graph was of type "+graph.GetType());
                return;
            }

            graph.open = readerStream.ReadBoolean ();

            //readerStream.ReadString ();
            serializeGraph.DeSerializeSettings (this);
        }
示例#3
0
        /** Deserializes nodes in the graph. The deserialized nodes will be created using graph.CreateNodes (numberOfNodes).
         * \astarpro */
        public void DeserializeNodes(NavGraph graph, NavGraph[] graphs, int graphIndex, AstarPath active)
        {
            if (mask == SMask.SaveNodes) {

                ISerializableGraph serializeGraph = graph as ISerializableGraph;

                if (serializeGraph == null) {
                    Debug.LogError ("The graph specified is not serializable, the graph is of type "+graph.GetType());
                    return;
                }

                int numNodes = readerStream.ReadInt32 ();

                graph.nodes = serializeGraph.CreateNodes (numNodes);

                if (numNodes == 0) {
                    return;
                }

                for (int i=0;i<graph.nodes.Length;i++) {
                    graph.nodes[i].graphIndex = graphIndex;
                }

                Debug.Log ("Loading "+numNodes+ " nodes");
                if (!MoveToVariableAnchor ("DeserializeGraphNodes")) {
                    Debug.LogError ("Error loading nodes - Couldn't find anchor");
                }

                serializeGraph.DeSerializeNodes (graph.nodes,this);

                if (!MoveToVariableAnchor ("DeserializeNodes")) {
                    Debug.LogError ("Error loading nodes - Couldn't find anchor");
                    return;
                }

                if (mask == SMask.RunLengthEncoding) {
                    int totalCount = 0;

                    //Penalties
                    while (totalCount < graph.nodes.Length) {
                        int runLength = (int)readerStream.ReadByte ();
                        int value = readerStream.ReadInt32 ();
                        int endIndex = totalCount+runLength;

                        if (endIndex > graph.nodes.Length) {
                            Debug.LogError ("Run Length Encoding is too long "+runLength+" "+endIndex+ " "+graph.nodes.Length+" "+totalCount);
                            endIndex = graph.nodes.Length;
                        }

                        for (int i=totalCount;i<endIndex;i++) {
                            graph.nodes[i].penalty = (uint)value;
                        }
                        totalCount = endIndex;
                    }

                    totalCount = 0;

                    //Flags
                    while (totalCount < graph.nodes.Length) {
                        int runLength = (int)readerStream.ReadByte ();
                        int value = readerStream.ReadInt32 ();
                        int endIndex = totalCount+runLength;

                        if (endIndex > graph.nodes.Length) {
                            Debug.LogError ("Run Length Encoding is too long "+runLength+" "+endIndex+ " "+graph.nodes.Length+" "+totalCount);
                            endIndex = graph.nodes.Length;
                        }

                        for (int i=totalCount;i<endIndex;i++) {
                            graph.nodes[i].flags = value;
                        }
                        totalCount += runLength;
                    }
                }

                for (int i=0;i<graph.nodes.Length;i++) {
                    DeSerializeNode (graph.nodes[i], graphs, graphIndex, readerStream);
                }
            }
        }
示例#4
0
        /** Called to serialize a graphs settings. \note Before calling this, setting #sPrefix to something unique for the graph is a good idea to avoid collisions in variable names */
        public void SerializeSettings(NavGraph graph, AstarPath active)
        {
            ISerializableGraph serializeGraph = graph as ISerializableGraph;

            if (serializeGraph == null) {
                Debug.LogError ("The graph specified is not serializable, the graph is of type "+graph.GetType());
                return;
            }

            serializeGraph.SerializeSettings (this);
        }
示例#5
0
        /** Serializes the nodes in the graph.
         * \astarpro */
        public void SerializeNodes(NavGraph graph, AstarPath active)
        {
            if (mask == SMask.SaveNodes) {

                ISerializableGraph serializeGraph = graph as ISerializableGraph;

                if (serializeGraph == null) {
                    Debug.LogError ("The graph specified is not serializable, the graph is of type "+graph.GetType());
                    return;
                }

                if (graph.nodes == null || graph.nodes.Length == 0) {
                    writerStream.Write (0);
                    //Debug.LogWarning ("No nodes to serialize");
                    return;
                }

                writerStream.Write (graph.nodes.Length);

                //writerStream.Write (savingToFile ? 753 : 1337);
                Debug.Log ("Stored nodes "+" "+writerStream.BaseStream.Position);

                SizeProfiler.Begin ("Graph specific nodes",writerStream);

                AddVariableAnchor ("DeserializeGraphNodes");
                serializeGraph.SerializeNodes (graph.nodes,this);

                SizeProfiler.End ("Graph specific nodes",writerStream);

                AddVariableAnchor ("DeserializeNodes");

                if (mask == SMask.RunLengthEncoding) {

                    SizeProfiler.Begin ("RLE Penalty",writerStream);
                    //Penalties
                    int lastValue = (int)graph.nodes[0].penalty;
                    int lastEntry = 0;

                    for (int i=1;i<graph.nodes.Length;i++) {
                        if (graph.nodes[i].penalty != lastValue || (i-lastEntry) >= byte.MaxValue-1) {
                            writerStream.Write ((byte)(i-lastEntry));
                            writerStream.Write (lastValue);
                            lastValue = (int)graph.nodes[i].penalty;
                            lastEntry = i;
                        }
                    }

                    writerStream.Write ((byte)(graph.nodes.Length-lastEntry));
                    writerStream.Write (lastValue);

                    SizeProfiler.Begin ("RLE Flags",writerStream);

                    //Flags
                    lastValue = graph.nodes[0].flags;
                    lastEntry = 0;

                    for (int i=1;i<graph.nodes.Length;i++) {
                        if (graph.nodes[i].flags != lastValue || (i-lastEntry) >= byte.MaxValue) {
                            writerStream.Write ((byte)(i-lastEntry));
                            writerStream.Write (lastValue);
                            lastValue = graph.nodes[i].flags;
                            lastEntry = i;
                        }
                    }
                    writerStream.Write ((byte)(graph.nodes.Length-lastEntry));
                    writerStream.Write (lastValue);

                    SizeProfiler.End ("RLE Flags",writerStream);
                }

                SizeProfiler.Begin ("Nodes, other",writerStream);

                for (int i=0;i<graph.nodes.Length;i++) {
                    SerializeNode (graph.nodes[i], writerStream);
                }

                SizeProfiler.End ("Nodes, other",writerStream);
            }
        }
示例#6
0
        //This is intended for quick saving of settings for e.g Undo operations
        public void DeSerializeSettings(NavGraph graph, AstarPath active)
        {
            ISerializableGraph serializeGraph = graph as ISerializableGraph;

            if (serializeGraph == null)
            {
                Debug.LogError("The graph specified is not (de)serializable (how it could be serialized in the first place is a mystery) the graph was of type " + graph.GetType());
                return;
            }

            graph.open = readerStream.ReadBoolean();

            //readerStream.ReadString ();
            serializeGraph.DeSerializeSettings(this);
        }
示例#7
0
        /** Called to serialize a graphs settings. \note Before calling this, setting #sPrefix to something unique for the graph is a good idea to avoid collisions in variable names */
        public void SerializeSettings(NavGraph graph, AstarPath active)
        {
            ISerializableGraph serializeGraph = graph as ISerializableGraph;

            if (serializeGraph == null)
            {
                Debug.LogError("The graph specified is not serializable, the graph is of type " + graph.GetType());
                return;
            }

            serializeGraph.SerializeSettings(this);
        }