public void SerialisationTest() { ServerData serverState = new ServerData(); // Setup the initial state serverState.TurnYear = 2101; serverState.GameInProgress = true; serverState.GameFolder = "dummy_value"; serverState.StatePathName = "unit_test.sstate"; serverState.AllTechLevels[1] = 10; serverState.AllTechLevels[2] = 5; Common.Fleet fleet1 = new Nova.Common.Fleet("foofleet", Player1Id, 1, new Nova.Common.DataStructures.NovaPoint(0, 0)); Common.Fleet fleet2 = new Nova.Common.Fleet("barfleet", Player1Id, 2, new Nova.Common.DataStructures.NovaPoint(0, 0)); EmpireData empire1 = new EmpireData(); EmpireData empire2 = new EmpireData(); empire1.Id = Player1Id; empire2.Id = Player2Id; serverState.AllEmpires.Add(Player1Id, empire1); serverState.AllEmpires.Add(Player2Id, empire2); serverState.AllEmpires[Player1Id].OwnedFleets[fleet1.Key] = fleet1; serverState.AllEmpires[Player2Id].OwnedFleets[fleet2.Key] = fleet2; // Serialize serverState.Save(); // change the value to ensure it is restored. serverState.TurnYear = 2102; serverState.GameInProgress = false; serverState.GameFolder = "foo_bar"; serverState.AllTechLevels[1] = 2; serverState.AllTechLevels[2] = 7; fleet1 = new Nova.Common.Fleet("fleetfoo", Player1Id, 1, new Nova.Common.DataStructures.NovaPoint(0, 0)); fleet2 = new Nova.Common.Fleet("fleetbar", Player1Id, 2, new Nova.Common.DataStructures.NovaPoint(0, 0)); empire1.Id = Player1Id; empire2.Id = Player2Id; serverState.AllEmpires[Player1Id].OwnedFleets[fleet1.Key] = fleet1; serverState.AllEmpires[Player2Id].OwnedFleets[fleet2.Key] = fleet2; // deSerialize serverState.Restore(); // test Assert.AreEqual(2101, serverState.TurnYear); Assert.AreEqual("dummy_value", serverState.GameFolder); Assert.AreEqual(10, serverState.AllTechLevels[1]); Assert.AreEqual(5, serverState.AllTechLevels[2]); Assert.AreEqual("foofleet", serverState.AllEmpires[Player1Id].OwnedFleets[fleet1.Key].Name); Assert.AreEqual("barfleet", serverState.AllEmpires[Player2Id].OwnedFleets[fleet2.Key].Name); Assert.AreEqual(true, serverState.GameInProgress); }
/// <summary> /// Load: initializing constructor to read in a Star from an XmlNode (from a saved file). /// </summary> /// <param name="node">An XmlNode representing a Star.</param> public Star(XmlNode node) : base(node) { Starbase = null; XmlNode mainNode = node.FirstChild; // Read the node while (mainNode != null) { try { switch (mainNode.Name.ToLower()) { case "hasfleetsinorbit": HasFleetsInOrbit = bool.Parse(mainNode.FirstChild.Value); break; case "productionqueue": ManufacturingQueue = new ProductionQueue(mainNode); break; case "mineralconcentration": MineralConcentration = new Resources(mainNode); break; case "resourcesonhand": ResourcesOnHand = new Resources(mainNode); break; case "colonists": Colonists = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "defenses": Defenses = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "factories": Factories = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "mines": Mines = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "researchallocation": ResearchAllocation = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "onlyleftover": OnlyLeftover = bool.Parse(mainNode.FirstChild.Value); break; case "scanrange": ScanRange = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "defensetype": DefenseType = mainNode.FirstChild.Value; break; case "scannertype": ScannerType = mainNode.FirstChild.Value; break; case "gravity": Gravity = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "radiation": Radiation = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "temperature": Temperature = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "originalgravity": OriginalGravity = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "originalradiation": OriginalRadiation = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "originaltemperature": OriginalTemperature = int.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; // These are placeholder objects that will be linked to the real objects once // loading from the file is complete (as they may not exist yet, and cannot be // referenced from here). The node will only hold enough information to identify // the referenced object. // ThisRace will point to the Race that owns the Star, // for now create a placeholder Race and load its Name case "thisrace": ThisRace = new Race(); ThisRace.Name = mainNode.FirstChild.Value; break; // Starbase will point to the Fleet that is this planet's starbase (if any), // for now create a placeholder Fleet and load its FleetID case "starbase": Starbase = new Fleet(long.Parse(mainNode.FirstChild.Value, NumberStyles.HexNumber)); break; default: break; } } catch (Exception e) { Report.FatalError(e.Message + "\n Details: \n" + e.ToString()); } mainNode = mainNode.NextSibling; } }
/// ---------------------------------------------------------------------------- /// <summary> /// Read in a set of orders. /// </summary> /// <param name="xmlnode">An <see cref="XmlNode"/> containing orders.</param> /// ---------------------------------------------------------------------------- public Orders(XmlNode xmlnode) { // temporary variables for reading in designs, fleets, stars Design design = null; ShipDesign shipDesign = null; Fleet fleet = null; Star star = null; // Read the node while (xmlnode != null) { try { switch (xmlnode.Name.ToLower()) { case "root": xmlnode = xmlnode.FirstChild; continue; case "orders": xmlnode = xmlnode.FirstChild; continue; case "techlevel": TechLevel = int.Parse(xmlnode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; // When loading designs we need to know what type of design it is. // To do this we first look ahead at the Type field of the design, // then load a design of the appropriate type (currently Design or ShipDesign). case "design": { string type = xmlnode.FirstChild.SelectSingleNode("Type").FirstChild.Value; if (type.ToLower() == "ship" || type == "starbase") { shipDesign = new ShipDesign(xmlnode); RaceDesigns.Add(shipDesign.Name, shipDesign); } else { design = new Design(xmlnode); RaceDesigns.Add(design.Name, design); } } break; case "shipdesign": { shipDesign = new ShipDesign(xmlnode); RaceDesigns.Add(shipDesign.Name, shipDesign); } break; // Deleted designs are in their own section to seperate them from // current designs. We load this section in one loop. The comments // above for designs apply equally to deleted designs. case "deleteddesigns": XmlNode deletedDesignsNode = xmlnode.FirstChild; while (deletedDesignsNode != null) { string type = deletedDesignsNode.FirstChild.SelectSingleNode("Type").FirstChild.Value; if (type.ToLower() == "ship" || type == "starbase") { shipDesign = new ShipDesign(deletedDesignsNode); DeletedDesigns.Add(shipDesign); } else { design = new Design(deletedDesignsNode); DeletedDesigns.Add(design); } deletedDesignsNode = deletedDesignsNode.NextSibling; } break; case "star": star = new Star(xmlnode); RaceStars.Add(star); break; case "fleet": fleet = new Fleet(xmlnode); RaceFleets.Add(fleet.FleetID, fleet); break; // Deleted fleets are contained in their own section to seperate them from // current fleets. We load this section in this loop. case "deletedfleets": XmlNode deletedFleetsNode = xmlnode.FirstChild; while (deletedFleetsNode != null) { fleet = new Fleet(deletedFleetsNode); DeletedFleets.Add(fleet); deletedFleetsNode = deletedFleetsNode.NextSibling; } break; case "racedata": PlayerData = new RaceData(xmlnode); break; default: break; } } catch (Exception e) { Report.FatalError(e.Message + "\n Details: \n" + e.ToString()); } xmlnode = xmlnode.NextSibling; } }