private void ToXml(FileStream stream) { // Setup the XML document XmlDocument xmldoc = new XmlDocument(); XmlElement xmlRoot = Global.InitializeXmlDocument(xmldoc); XmlElement xmlelClientState = xmldoc.CreateElement("ClientState"); xmlRoot.AppendChild(xmlelClientState); // Empire Data xmlelClientState.AppendChild(EmpireState.ToXml(xmldoc)); // Commands. This places the first command at the bottom of the list, and the newset // at the top. XmlElement xmlelCommands = xmldoc.CreateElement("Commands"); foreach (ICommand command in Commands) { xmlelCommands.AppendChild(command.ToXml(xmldoc)); } xmlelClientState.AppendChild(xmlelCommands); // Messages foreach (Nova.Common.Message message in Messages) { xmlelClientState.AppendChild(message.ToXml(xmldoc)); } // FIXME (priority 6) - THIS HAS TO GO! xmlelClientState.AppendChild(InputTurn.ToXml(xmldoc)); Global.SaveData(xmldoc, xmlelClientState, "FirstTurn", FirstTurn.ToString(System.Globalization.CultureInfo.InvariantCulture)); Global.SaveData(xmldoc, xmlelClientState, "GameFolder", GameFolder); Global.SaveData(xmldoc, xmlelClientState, "StatePathName", StatePathName); xmldoc.Save(stream); }
public void SerialisationTestIntel() { StringWriter stringStream = new StringWriter(); try { // setup a test object Intel testData = new Intel(); // setup the test data testData.EmpireState.TurnYear = 3500; Star testStar = new Star(); testStar.Name = "Pluto"; testStar.Colonists = 25000; testData.EmpireState.StarReports.Add(testStar.Name, testStar.GenerateReport(ScanLevel.Owned, testData.EmpireState.TurnYear)); // setup the file name string saveFileName = "IntelTest.xml"; // setup the streams MemoryStream memoryStream = new MemoryStream(); // Setup the XML document XmlDocument xmldoc = new XmlDocument(); Global.InitializeXmlDocument(xmldoc); // add the Intel to the document xmldoc.ChildNodes.Item(1).AppendChild(testData.ToXml(xmldoc)); xmldoc.Save(memoryStream); xmldoc.Save(stringStream); // Serialize to file if (TestControls.CreateFiles) { using (Stream fileStream = new FileStream(saveFileName, FileMode.Create)) { xmldoc.Save(fileStream); } } // deserialize Intel loadedData; xmldoc.RemoveAll(); if (TestControls.CreateFiles) { using (FileStream saveFileStream = new FileStream(saveFileName, FileMode.Open, FileAccess.Read)) { xmldoc.Load(saveFileStream); loadedData = new Intel(xmldoc); } } else { // move to start of memory stream memoryStream.Seek(0, SeekOrigin.Begin); xmldoc.Load(memoryStream); loadedData = new Intel(xmldoc); } // test if it worked Assert.IsTrue(loadedData.EmpireState.TurnYear == 3500); Assert.IsTrue(loadedData.EmpireState.StarReports.ContainsKey("Pluto") && loadedData.EmpireState.StarReports["Pluto"].Name == "Pluto"); Assert.IsTrue(loadedData.EmpireState.StarReports.ContainsKey("Pluto") && loadedData.EmpireState.StarReports["Pluto"].Colonists == 25000); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Intel.cs SerialisationTestIntel() failed:" + Environment.NewLine + e.Message + Environment.NewLine + stringStream.ToString()); throw e; // fail the test } }
public void SerialisationTestEmptyIntel() { StringWriter stringStream = new StringWriter(); try { // setup a test object Intel testData = new Intel(); // setup the file name string saveFileName = "IntelTest.xml"; // setup the streams MemoryStream memoryStream = new MemoryStream(); // Setup the XML document XmlDocument xmldoc = new XmlDocument(); Global.InitializeXmlDocument(xmldoc); // add the Intel to the document xmldoc.ChildNodes.Item(1).AppendChild(testData.ToXml(xmldoc)); xmldoc.Save(memoryStream); xmldoc.Save(stringStream); // Serialize to file if (TestControls.CreateFiles) { using (Stream fileStream = new FileStream(saveFileName, FileMode.Create)) { xmldoc.Save(fileStream); } } // deserialize Intel loadedData; xmldoc.RemoveAll(); if (TestControls.CreateFiles) { using (FileStream saveFileStream = new FileStream(saveFileName, FileMode.Open, FileAccess.Read)) { xmldoc.Load(saveFileStream); loadedData = new Intel(xmldoc); } } else { // move to start of memory stream memoryStream.Seek(0, SeekOrigin.Begin); xmldoc.Load(memoryStream); loadedData = new Intel(xmldoc); } // test if it worked // CheckTestIntel(loadedData); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Intel.cs SerialisationTestEmptyIntel() failed:" + Environment.NewLine + e.Message + Environment.NewLine + stringStream.ToString()); throw e; // fail the test } }
/// <summary> /// Save the turn data. /// </summary> /// <remarks> /// We have to be very careful that we have a consistent and self-contained data set in the turn file. /// For example, we write out "AllStars" but turnData.AllStars is not the same as stateData.AllStars. /// So make sure any pointers to AllStars refer to the copy in turnData otherwise we'll get /// duplicated (but separate) star objects. /// </remarks> public void WriteIntel() { foreach (EmpireData empire in serverState.AllEmpires.Values) { turnData = new Intel(); turnData.AllMinefields = serverState.AllMinefields; turnData.EmpireState = serverState.AllEmpires[empire.Id]; // Copy any messages foreach (Message message in serverState.AllMessages) { if (message.Audience == Global.Everyone || message.Audience == empire.Id) { turnData.Messages.Add(message); } } // Don't try and generate a scores report on the very start of a new // game. if (serverState.TurnYear > Global.StartingYear) { turnData.AllScores = scores.GetScores(); } else { turnData.AllScores = new List <ScoreRecord>(); } serverState.GameFolder = FileSearcher.GetFolder(Global.ServerFolderKey, Global.ServerFolderName); if (serverState.GameFolder == null) { Report.Error("Intel Writer: WriteIntel() - Unable to create file \"Nova.intel\"."); return; } string turnFileName = Path.Combine(serverState.GameFolder, empire.Race.Name + Global.IntelExtension); // Write out the intel file, as xml bool waitForFile = false; double waitTime = 0.0; // seconds do { try { using (Stream turnFile = new FileStream(turnFileName /*+ ".xml"*/, FileMode.Create)) { // Setup the XML document XmlDocument xmldoc = new XmlDocument(); Global.InitializeXmlDocument(xmldoc); // add the Intel to the document xmldoc.ChildNodes.Item(1).AppendChild(turnData.ToXml(xmldoc)); xmldoc.Save(turnFile); } 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); } }