public void TestWorkshopSerialization()
    {
        Workshop workshop = new Workshop();

        workshop.PossibleProductions = new System.Collections.Generic.List <Workshop.ProductionChain>();
        Workshop.ProductionChain chain1 = new Workshop.ProductionChain()
        {
            Name = "Iron smelting", ProcessingTime = 4.0f
        };
        chain1.Input = new System.Collections.Generic.List <Workshop.Item>();
        chain1.Input.Add(new Workshop.Item()
        {
            ObjectType = "Raw Iron", Amount = 3, SlotPosX = 0, SlotPosY = 0
        });
        chain1.Output = new System.Collections.Generic.List <Workshop.Item>();
        chain1.Output.Add(new Workshop.Item()
        {
            ObjectType = "Steel Plate", Amount = 3, SlotPosX = 2, SlotPosY = 0
        });

        workshop.PossibleProductions.Add(chain1);

        Workshop.ProductionChain chain2 = new Workshop.ProductionChain()
        {
            Name = "Copper smelting", ProcessingTime = 3.0f
        };
        chain2.Input = new System.Collections.Generic.List <Workshop.Item>();
        chain2.Input.Add(new Workshop.Item()
        {
            ObjectType = "Raw Copper", Amount = 3, SlotPosX = 0, SlotPosY = 0
        });
        chain2.Output = new System.Collections.Generic.List <Workshop.Item>();
        chain2.Output.Add(new Workshop.Item()
        {
            ObjectType = "Copper Wire", Amount = 6, SlotPosX = 2, SlotPosY = 0
        });

        workshop.PossibleProductions.Add(chain2);

        workshop.ParamsDefinitions = new Workshop.WorkShopParameterDefinitions();

        // serialize
        StringWriter  writer     = new StringWriter();
        XmlSerializer serializer = new XmlSerializer(typeof(BuildableComponent), new Type[] { typeof(Workshop) });

        serializer.Serialize(writer, workshop);

        StringReader sr = new StringReader(writer.ToString());

        // if you want to dump file to disk for visual check, uncomment this
        ////File.WriteAllText("Workshop.xml", writer.ToString());

        // deserialize
        Workshop deserializedWorkshop = (Workshop)serializer.Deserialize(sr);

        Assert.NotNull(deserializedWorkshop);
        Assert.AreEqual("Raw Iron", deserializedWorkshop.PossibleProductions[0].Input[0].ObjectType);
    }
示例#2
0
    private static Workshop CreateWorkshop()
    {
        Workshop workshop = new Workshop();

        workshop.PossibleProductions = new System.Collections.Generic.List <Workshop.ProductionChain>();
        Workshop.ProductionChain chain1 = new Workshop.ProductionChain()
        {
            Name           = "Iron smelting",
            ProcessingTime = 4.0f
        };
        chain1.Input = new System.Collections.Generic.List <Workshop.Item>();
        chain1.Input.Add(new Workshop.Item()
        {
            ObjectType = "Raw Iron",
            Amount     = 3,
            SlotPosX   = 0,
            SlotPosY   = 0
        });
        chain1.Output = new System.Collections.Generic.List <Workshop.Item>();
        chain1.Output.Add(new Workshop.Item()
        {
            ObjectType = "Steel Plate",
            Amount     = 3,
            SlotPosX   = 2,
            SlotPosY   = 0
        });

        workshop.PossibleProductions.Add(chain1);

        Workshop.ProductionChain chain2 = new Workshop.ProductionChain()
        {
            Name           = "Copper smelting",
            ProcessingTime = 3.0f
        };
        chain2.Input = new System.Collections.Generic.List <Workshop.Item>();
        chain2.Input.Add(new Workshop.Item()
        {
            ObjectType = "Raw Copper",
            Amount     = 3,
            SlotPosX   = 0,
            SlotPosY   = 0
        });
        chain2.Output = new System.Collections.Generic.List <Workshop.Item>();
        chain2.Output.Add(new Workshop.Item()
        {
            ObjectType = "Copper Wire",
            Amount     = 6,
            SlotPosX   = 2,
            SlotPosY   = 0
        });

        workshop.PossibleProductions.Add(chain2);

        workshop.ParamsDefinitions = new Workshop.WorkShopParameterDefinitions();
        return(workshop);
    }