示例#1
0
        /// <summary>
        /// Loads the traffic volume setup from the given XML file
        /// </summary>
        /// <param name="xd">XmlDocument to parse</param>
        /// <param name="nodesList">List of all existing LineNodes</param>
        /// <param name="lf">LoadingForm for status updates</param>
        public void LoadFromFile(XmlDocument xd, List<LineNode> nodesList, LoadingForm.LoadingForm lf)
        {
            lf.SetupLowerProgess("Parsing XML...", 3);

            // clear everything first
            _trafficVolumes.Clear();
            _startPoints.Clear();
            _destinationPoints.Clear();

            // parse save file version (currently not needed, but probably in future)
            int saveVersion = 0;
            XmlNode mainNode = xd.SelectSingleNode("//CityTrafficSimulator");
            XmlNode saveVersionNode = mainNode.Attributes.GetNamedItem("saveVersion");
            if (saveVersionNode != null)
                saveVersion = Int32.Parse(saveVersionNode.Value);
            else
                saveVersion = 0;

            // Load start points:
            lf.StepLowerProgress();
            // get corresponding XML nodes
            XmlNodeList xnlStartNodes = xd.SelectNodes("//CityTrafficSimulator/TrafficVolumes/StartPoints/BunchOfNodes");
            foreach (XmlNode aXmlNode in xnlStartNodes)
                {
                // Deserialize each node
                TextReader tr = new StringReader(aXmlNode.OuterXml);
                XmlSerializer xs = new XmlSerializer(typeof(BunchOfNodes));
                BunchOfNodes bof = (BunchOfNodes)xs.Deserialize(tr);
                bof.RecoverFromLoad(saveVersion, nodesList);
                _startPoints.Add(bof);
                }

            // Load destination points:
            lf.StepLowerProgress();
            // get corresponding XML nodes
            XmlNodeList xnlDestinationNodes = xd.SelectNodes("//CityTrafficSimulator/TrafficVolumes/DestinationPoints/BunchOfNodes");
            foreach (XmlNode aXmlNode in xnlDestinationNodes)
                {
                // Deserialize each node
                TextReader tr = new StringReader(aXmlNode.OuterXml);
                XmlSerializer xs = new XmlSerializer(typeof(BunchOfNodes));
                BunchOfNodes bof = (BunchOfNodes)xs.Deserialize(tr);
                bof.RecoverFromLoad(saveVersion, nodesList);
                // ensure that no hashcode is assigned twice
                BunchOfNodes.hashcodeIndex = Math.Max(bof.hashcode + 1, BunchOfNodes.hashcodeIndex);
                _destinationPoints.Add(bof);
                }

            // Load traffic volumes:
            lf.StepLowerProgress();
            // get corresponding XML nodes
            XmlNodeList xnlTrafficVolumes = xd.SelectNodes("//CityTrafficSimulator/TrafficVolumes/TrafficVolume");
            foreach (XmlNode aXmlNode in xnlTrafficVolumes)
                {
                // Deserialize each node
                TextReader tr = new StringReader(aXmlNode.OuterXml);
                XmlSerializer xs = new XmlSerializer(typeof(TrafficVolume));
                TrafficVolume tv = (TrafficVolume)xs.Deserialize(tr);

                tv.RecoverFromLoad(saveVersion, startPoints, destinationPoints);
                if (tv.startNodes != null && tv.destinationNodes != null)
                    {
                    _trafficVolumes.Add(tv);
                    tv.VehicleSpawned += new TrafficVolume.VehicleSpawnedEventHandler(newTV_VehicleSpawned);
                    }
                else
                    {
                    lf.Log("Error during traffic volume deserialization: Could not dereference start-/end nodes. Traffic volume was dismissed.");
                    }
                }

            OnStartPointsChanged(new StartPointsChangedEventArgs());
            OnDestinationPointsChanged(new DestinationPointsChangedEventArgs());
        }