예제 #1
0
        /// <summary>
        /// Save: Serialize this object to an <see cref="XmlElement"/>.
        /// </summary>
        /// <param name="xmldoc">The parent <see cref="XmlDocument"/>.</param>
        /// <returns>An <see cref="XmlElement"/> representation of the Intel.</returns>
        public XmlElement ToXml(XmlDocument xmldoc)
        {
            // create the outer element
            XmlElement xmlelIntel = xmldoc.CreateElement("Intel");

            xmlelIntel.AppendChild(EmpireState.ToXml(xmldoc));

            // Messages
            if (Messages.Count > 0)
            {
                foreach (Message message in Messages)
                {
                    xmlelIntel.AppendChild(message.ToXml(xmldoc));
                }
            }

            // AllScores
            foreach (ScoreRecord score in AllScores)
            {
                xmlelIntel.AppendChild(score.ToXml(xmldoc));
            }

            // AllMinefields
            foreach (Minefield mine in AllMinefields.Values)
            {
                xmlelIntel.AppendChild(mine.ToXml(xmldoc));
            }

            // return the outer element
            return(xmlelIntel);
        }
예제 #2
0
파일: Orders.cs 프로젝트: ekolis/stars-nova
        /// <summary>
        /// Write out the orders using xml format.
        /// </summary>
        /// <param name="ordersFileName">The path and filename to save the orders too.</param>
        public void ToXml(string ordersFileName)
        {
            // Setup the XML document
            XmlDocument xmldoc  = new XmlDocument();
            XmlElement  xmlRoot = Global.InitializeXmlDocument(xmldoc);

            // add the orders to the document
            XmlElement xmlelOrders = xmldoc.CreateElement("Orders");

            xmlRoot.AppendChild(xmlelOrders);

            // Place the turn year first, so it can be determined quickly
            Global.SaveData(xmldoc, xmlelOrders, "Turn", EmpireStatus.TurnYear.ToString(System.Globalization.CultureInfo.InvariantCulture));

            // store the designs, for any new designs
            foreach (ShipDesign design in RaceDesigns.Values)
            {
                xmlelOrders.AppendChild(design.ToXml(xmldoc));
            }

            // Deleted fleets and designs are wrapped in a section node
            // so they can be told appart from current designs and fleets when
            // loaded.
            XmlElement xmlelDeletedFleets = xmldoc.CreateElement("DeletedFleets");

            foreach (int fleetKey in DeletedFleets)
            {
                // only need to store enough data to find the deleted fleet.
                Global.SaveData(xmldoc, xmlelDeletedFleets, "FleetKey", fleetKey.ToString("X"));
            }
            xmlelOrders.AppendChild(xmlelDeletedFleets);

            XmlElement xmlelDeletedDesigns = xmldoc.CreateElement("DeletedDesigns");

            foreach (int designKey in DeletedDesigns)
            {
                Global.SaveData(xmldoc, xmlelDeletedDesigns, "DesignKey", designKey);
            }
            xmlelOrders.AppendChild(xmlelDeletedDesigns);

            xmlelOrders.AppendChild(EmpireStatus.ToXml(xmldoc));

            bool   waitForFile = false;
            double waitTime    = 0.0; // seconds

            do
            {
                try
                {
                    using (Stream output = new FileStream(ordersFileName, FileMode.Create))
                    {
                        // output = new GZipStream(output, CompressionMode.Compress);

                        xmldoc.Save(output);
                    }
                    waitForFile = false;
                }
                catch (System.IO.IOException)
                {
                    // IOException. Is the file locked? Try waiting.
                    if (waitTime < Global.TotalFileWaitTime)
                    {
                        waitForFile = true;
                        System.Threading.Thread.Sleep(Global.FileWaitRetryTime);
                        waitTime += 0.1;
                    }
                    else
                    {
                        // Give up, maybe something else is wrong?
                        throw;
                    }
                }
            }while (waitForFile);
        }