/// <param name="name"></param> /// <param name="level"></param> /// <param name="callingFunction">For debug purposes</param> /// <returns></returns> private static int[] getArray(string name, int level, string callingFunction) { int[] array = new int[14]; switch (name) { case "ResidentialLow": array = DataStore.residentialLow[level]; break; case "ResidentialHigh": array = DataStore.residentialHigh[level]; break; case "CommercialLow": array = DataStore.commercialLow[level]; break; case "CommercialHigh": array = DataStore.commercialHigh[level]; break; case "CommercialTourist": array = DataStore.commercialTourist[level]; break; case "CommercialLeisure": array = DataStore.commercialLeisure[level]; break; case "Office": array = DataStore.office[level]; break; case "Industry": array = DataStore.industry[level]; break; case "IndustryOre": array = DataStore.industry_ore[level]; break; case "IndustryOil": array = DataStore.industry_oil[level]; break; case "IndustryForest": array = DataStore.industry_forest[level]; break; case "IndustryFarm": array = DataStore.industry_farm[level]; break; default: Debugging.panelMessage(callingFunction + ". unknown element name: " + name); break; } return(array); } // end getArray
/// <summary> /// /// </summary> /// <param name="consumeNode"></param> private void readConsumptionNode(XmlNode consumeNode) { foreach (XmlNode node in consumeNode.ChildNodes) { try { // Extract power, water, sewage, garbage and wealth string[] attr = node.Name.Split(new char[] { '_' }); string name = attr[0]; int level = Convert.ToInt32(attr[1]) - 1; int power = Convert.ToInt32(node.Attributes["power"].InnerText); int water = Convert.ToInt32(node.Attributes["water"].InnerText); int sewage = Convert.ToInt32(node.Attributes["sewage"].InnerText); int garbage = Convert.ToInt32(node.Attributes["garbage"].InnerText); int wealth = Convert.ToInt32(node.Attributes["wealth"].InnerText); int[] array = getArray(name, level, "readConsumptionNode"); setConsumptionRates(array, power, water, sewage, garbage, wealth); } catch (Exception e) { Debugging.panelMessage("readConsumptionNode: " + e.Message); } } }
public override void OnLevelLoaded(LoadMode mode) { if (mode == LoadMode.LoadGame || mode == LoadMode.NewGame) { if (!isLevelLoaded) { isLevelLoaded = true; // Now we can remove people DataStore.allowRemovalOfCitizens = true; Debugging.releaseBuffer(); Debugging.panelMessage("Successfully loaded in " + sw.ElapsedMilliseconds + " ms."); } } }
/// <summary> /// /// </summary> /// <param name="node"></param> private void readBonusHouseNode(XmlNode node) { try { string[] values = node.InnerText.Split(new char[] { STRING_DELIMITER }); foreach (string value in values) { // Marge into the list or dictionary } } catch (Exception e) { Debugging.panelMessage("readBonusHouseNode: " + e.Message); } }
public override void OnReleased() { if (isModEnabled) { isModEnabled = false; try { WG_XMLBaseVersion xml = new XML_VersionFive(); xml.writeXML(currentFileLocation); } catch (Exception e) { Debugging.panelMessage(e.Message); } RevertRedirect(true); } }
/// <summary> /// /// </summary> /// <param name="produceNode"></param> private void readProductionNode(XmlNode produceNode) { foreach (XmlNode node in produceNode.ChildNodes) { try { // Extract power, water, sewage, garbage and wealth string[] attr = node.Name.Split(new char[] { '_' }); string name = attr[0]; int level = Convert.ToInt32(attr[1]) - 1; int[] array = getArray(name, level, "readProductionNode"); array[DataStore.PRODUCTION] = Convert.ToInt32(node.Attributes["production"].InnerText); if (array[DataStore.PRODUCTION] <= 0) { array[DataStore.PRODUCTION] = 1; } } catch (Exception e) { Debugging.panelMessage("readProductionNode: " + e.Message); } } }
} // end readXML /// <param name="fullPathFileName"></param> /// <returns></returns> public override bool writeXML(string fullPathFileName) { XmlDocument xmlDoc = new XmlDocument(); XmlNode rootNode = xmlDoc.CreateElement("WG_CityMod"); XmlAttribute attribute = xmlDoc.CreateAttribute("version"); attribute.Value = "5"; rootNode.Attributes.Append(attribute); /* * attribute = xmlDoc.CreateAttribute("experimental"); * attribute.Value = DataStore.enableExperimental ? "true" : "false"; * rootNode.Attributes.Append(attribute); */ xmlDoc.AppendChild(rootNode); XmlNode popNode = xmlDoc.CreateElement(popNodeName); attribute = xmlDoc.CreateAttribute("strictCapacity"); attribute.Value = DataStore.strictCapacity ? "true" : "false"; popNode.Attributes.Append(attribute); XmlNode consumeNode = xmlDoc.CreateElement(consumeNodeName); XmlNode visitNode = xmlDoc.CreateElement(visitNodeName); XmlNode pollutionNode = xmlDoc.CreateElement(pollutionNodeName); XmlNode productionNode = xmlDoc.CreateElement(productionNodeName); try { makeNodes(xmlDoc, "ResidentialLow", DataStore.residentialLow, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "ResidentialHigh", DataStore.residentialHigh, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "CommercialLow", DataStore.commercialLow, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "CommercialHigh", DataStore.commercialHigh, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "CommercialLeisure", DataStore.commercialLeisure, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "CommercialTourist", DataStore.commercialTourist, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "Office", DataStore.office, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "Industry", DataStore.industry, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "IndustryFarm", DataStore.industry_farm, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "IndustryForest", DataStore.industry_forest, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "IndustryOre", DataStore.industry_ore, popNode, consumeNode, visitNode, pollutionNode, productionNode); makeNodes(xmlDoc, "IndustryOil", DataStore.industry_oil, popNode, consumeNode, visitNode, pollutionNode, productionNode); } catch (Exception e) { Debugging.panelMessage(e.Message); } // First segment createPopulationNodeComment(xmlDoc, rootNode); rootNode.AppendChild(popNode); createConsumptionNodeComment(xmlDoc, rootNode); rootNode.AppendChild(consumeNode); createVisitNodeComment(xmlDoc, rootNode); rootNode.AppendChild(visitNode); createProductionNodeComment(xmlDoc, rootNode); rootNode.AppendChild(productionNode); createPollutionNodeComment(xmlDoc, rootNode); rootNode.AppendChild(pollutionNode); // Add mesh names to XML XmlNode bonusHouseholdNode = xmlDoc.CreateElement(bonusHouseName); attribute = xmlDoc.CreateAttribute("printResNames"); attribute.Value = DataStore.printResidentialNames ? "true" : "false"; bonusHouseholdNode.Attributes.Append(attribute); attribute = xmlDoc.CreateAttribute("mergeResNames"); attribute.Value = DataStore.mergeResidentialNames ? "true" : "false"; bonusHouseholdNode.Attributes.Append(attribute); XmlComment comment = xmlDoc.CreateComment("Defaults: L1 2x3 Detached05,L1 3x3 Detached02,L1 4x4 Detached02,L1 4x4 Detached06a,"); bonusHouseholdNode.AppendChild(comment); comment = xmlDoc.CreateComment("L1 4x4 Detached11,L2 2x2 Detached05,L2 2x3 Semi-detachedhouse01,L2 3x4 Semi-detachedhouse02a,"); bonusHouseholdNode.AppendChild(comment); comment = xmlDoc.CreateComment("L3 3x3 Semi-detachedhouse02,L3 4x4 Semi-detachedhouse03a"); bonusHouseholdNode.AppendChild(comment); SortedList <string, int> list = new SortedList <string, int>(DataStore.bonusHouseholdCache); foreach (string data in list.Keys) { XmlNode meshNameNode = xmlDoc.CreateElement(meshName); meshNameNode.InnerXml = data; attribute = xmlDoc.CreateAttribute("bonus"); int value = 1; DataStore.bonusHouseholdCache.TryGetValue(data, out value); attribute.Value = Convert.ToString(value); meshNameNode.Attributes.Append(attribute); bonusHouseholdNode.AppendChild(meshNameNode); } rootNode.AppendChild(bonusHouseholdNode); // Append the bonusHousehold to root // Add mesh names to dictionary XmlNode bonusWorkNode = xmlDoc.CreateElement(bonusWorkName); attribute = xmlDoc.CreateAttribute("printWorkNames"); attribute.Value = DataStore.printEmploymentNames ? "true" : "false"; bonusWorkNode.Attributes.Append(attribute); attribute = xmlDoc.CreateAttribute("mergeWorkNames"); attribute.Value = DataStore.mergeEmploymentNames ? "true" : "false"; bonusWorkNode.Attributes.Append(attribute); list = new SortedList <string, int>(DataStore.bonusWorkerCache); foreach (string data in list.Keys) { XmlNode meshNameNode = xmlDoc.CreateElement(meshName); meshNameNode.InnerXml = data; attribute = xmlDoc.CreateAttribute("bonus"); int value = 1; DataStore.bonusWorkerCache.TryGetValue(data, out value); attribute.Value = Convert.ToString(value); meshNameNode.Attributes.Append(attribute); bonusWorkNode.AppendChild(meshNameNode); } rootNode.AppendChild(bonusWorkNode); // Append the bonusWorkers to root try { if (File.Exists(fullPathFileName)) { if (File.Exists(fullPathFileName + ".bak")) { File.Delete(fullPathFileName + ".bak"); } File.Move(fullPathFileName, fullPathFileName + ".bak"); } } catch (Exception e) { Debugging.panelMessage(e.Message); } try { xmlDoc.Save(fullPathFileName); } catch (Exception e) { Debugging.panelMessage(e.Message); return(false); // Only time when we say there's an error } return(true); } // end writeXML
} // end readXML /// <param name="fullPathFileName"></param> /// <returns></returns> public override bool writeXML(string fullPathFileName) { XmlDocument xmlDoc = new XmlDocument(); XmlNode rootNode = xmlDoc.CreateElement("WG_CityMod"); XmlAttribute attribute = xmlDoc.CreateAttribute("version"); attribute.Value = "6"; rootNode.Attributes.Append(attribute); /* * attribute = xmlDoc.CreateAttribute("experimental"); * attribute.Value = DataStore.enableExperimental ? "true" : "false"; * rootNode.Attributes.Append(attribute); */ xmlDoc.AppendChild(rootNode); XmlNode popNode = xmlDoc.CreateElement(popNodeName); attribute = xmlDoc.CreateAttribute("strictCapacity"); attribute.Value = DataStore.strictCapacity ? "true" : "false"; popNode.Attributes.Append(attribute); XmlNode consumeNode = xmlDoc.CreateElement(consumeNodeName); XmlNode visitNode = xmlDoc.CreateElement(visitNodeName); XmlNode pollutionNode = xmlDoc.CreateElement(pollutionNodeName); XmlNode productionNode = xmlDoc.CreateElement(productionNodeName); try { MakeNodes(xmlDoc, "ResidentialLow", DataStore.residentialLow, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "ResidentialHigh", DataStore.residentialHigh, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "ResEcoLow", DataStore.resEcoLow, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "ResEcoHigh", DataStore.resEcoHigh, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "CommercialLow", DataStore.commercialLow, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "CommercialHigh", DataStore.commercialHigh, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "CommercialEco", DataStore.commercialEco, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "CommercialTourist", DataStore.commercialTourist, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "CommercialLeisure", DataStore.commercialLeisure, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "Office", DataStore.office, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "OfficeHighTech", DataStore.officeHighTech, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "Industry", DataStore.industry, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "IndustryFarm", DataStore.industry_farm, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "IndustryForest", DataStore.industry_forest, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "IndustryOre", DataStore.industry_ore, popNode, consumeNode, visitNode, pollutionNode, productionNode); MakeNodes(xmlDoc, "IndustryOil", DataStore.industry_oil, popNode, consumeNode, visitNode, pollutionNode, productionNode); } catch (Exception e) { Debugging.panelMessage(e.Message); } // First segment CreatePopulationNodeComment(xmlDoc, rootNode); rootNode.AppendChild(popNode); CreateConsumptionNodeComment(xmlDoc, rootNode); rootNode.AppendChild(consumeNode); CreateVisitNodeComment(xmlDoc, rootNode); rootNode.AppendChild(visitNode); CreateProductionNodeComment(xmlDoc, rootNode); rootNode.AppendChild(productionNode); CreatePollutionNodeComment(xmlDoc, rootNode); rootNode.AppendChild(pollutionNode); // Add mesh names to XML for house holds XmlComment comment = xmlDoc.CreateComment(" ******* House hold data ******* "); rootNode.AppendChild(comment); XmlNode overrideHouseholdNode = xmlDoc.CreateElement(overrideHouseName); attribute = xmlDoc.CreateAttribute("printResNames"); attribute.Value = DataStore.printResidentialNames ? "true" : "false"; overrideHouseholdNode.Attributes.Append(attribute); attribute = xmlDoc.CreateAttribute("mergeResNames"); attribute.Value = DataStore.mergeResidentialNames ? "true" : "false"; overrideHouseholdNode.Attributes.Append(attribute); SortedList <string, int> list = new SortedList <string, int>(DataStore.householdCache); foreach (string name in list.Keys) { XmlNode meshNameNode = xmlDoc.CreateElement(meshName); meshNameNode.InnerXml = name; attribute = xmlDoc.CreateAttribute("value"); int value = 1; DataStore.householdCache.TryGetValue(name, out value); attribute.Value = Convert.ToString(value); meshNameNode.Attributes.Append(attribute); overrideHouseholdNode.AppendChild(meshNameNode); } rootNode.AppendChild(overrideHouseholdNode); // Append the overrideHousehold to root // Add mesh names to XML comment = xmlDoc.CreateComment(" ******* Printed out house hold data. To activate the value, move the line into the override segment ******* "); rootNode.AppendChild(comment); XmlNode printHouseholdNode = xmlDoc.CreateElement(printHouseName); list = new SortedList <string, int>(DataStore.housePrintOutCache); foreach (string data in list.Keys) { XmlNode meshNameNode = xmlDoc.CreateElement(meshName); meshNameNode.InnerXml = data; attribute = xmlDoc.CreateAttribute("value"); int value = 1; DataStore.housePrintOutCache.TryGetValue(data, out value); attribute.Value = Convert.ToString(value); meshNameNode.Attributes.Append(attribute); printHouseholdNode.AppendChild(meshNameNode); } rootNode.AppendChild(printHouseholdNode); // Append the printHousehold to root // Add mesh names to XML list = new SortedList <string, int>(DataStore.bonusHouseholdCache); if (list.Keys.Count != 0) { XmlNode bonusHouseholdNode = xmlDoc.CreateElement(bonusHouseName); foreach (string data in list.Keys) { XmlNode meshNameNode = xmlDoc.CreateElement(meshName); meshNameNode.InnerXml = data; attribute = xmlDoc.CreateAttribute("value"); DataStore.bonusHouseholdCache.TryGetValue(data, out int value); attribute.Value = Convert.ToString(value); meshNameNode.Attributes.Append(attribute); bonusHouseholdNode.AppendChild(meshNameNode); } rootNode.AppendChild(bonusHouseholdNode); // Append the bonusHousehold to root } // Add mesh names to XML for workers comment = xmlDoc.CreateComment(" ******* Worker data ******* "); rootNode.AppendChild(comment); XmlNode overrideWorkNode = xmlDoc.CreateElement(overrideWorkName); attribute = xmlDoc.CreateAttribute("printWorkNames"); attribute.Value = DataStore.printEmploymentNames ? "true" : "false"; overrideWorkNode.Attributes.Append(attribute); attribute = xmlDoc.CreateAttribute("mergeWorkNames"); attribute.Value = DataStore.mergeEmploymentNames ? "true" : "false"; overrideWorkNode.Attributes.Append(attribute); SortedList <string, int> wList = new SortedList <string, int>(DataStore.workerCache); foreach (string name in wList.Keys) { XmlNode meshNameNode = xmlDoc.CreateElement(meshName); meshNameNode.InnerXml = name; int value = 1; DataStore.workerCache.TryGetValue(name, out value); attribute = xmlDoc.CreateAttribute("value"); attribute.Value = Convert.ToString(value); meshNameNode.Attributes.Append(attribute); overrideWorkNode.AppendChild(meshNameNode); } rootNode.AppendChild(overrideWorkNode); // Append the overrideWorkers to root // Add mesh names to dictionary comment = xmlDoc.CreateComment(" ******* Printed out worker data. To activate the value, move the line into the override segment ******* "); rootNode.AppendChild(comment); XmlNode printWorkNode = xmlDoc.CreateElement(printWorkName); wList = new SortedList <string, int>(DataStore.workerPrintOutCache); foreach (string data in wList.Keys) { if (!DataStore.workerCache.ContainsKey(data)) { XmlNode meshNameNode = xmlDoc.CreateElement(meshName); meshNameNode.InnerXml = data; DataStore.workerPrintOutCache.TryGetValue(data, out int value); attribute = xmlDoc.CreateAttribute("value"); attribute.Value = Convert.ToString(value); meshNameNode.Attributes.Append(attribute); printWorkNode.AppendChild(meshNameNode); } } rootNode.AppendChild(printWorkNode); // Append the printWorkers to root // Add mesh names to dictionary wList = new SortedList <string, int>(DataStore.bonusWorkerCache); if (wList.Keys.Count != 0) { XmlNode bonusWorkNode = xmlDoc.CreateElement(bonusWorkName); foreach (string data in wList.Keys) { XmlNode meshNameNode = xmlDoc.CreateElement(meshName); meshNameNode.InnerXml = data; DataStore.bonusWorkerCache.TryGetValue(data, out int value); attribute = xmlDoc.CreateAttribute("value"); attribute.Value = Convert.ToString(value); meshNameNode.Attributes.Append(attribute); bonusWorkNode.AppendChild(meshNameNode); } rootNode.AppendChild(bonusWorkNode); // Append the bonusWorkers to root } try { if (File.Exists(fullPathFileName)) { if (File.Exists(fullPathFileName + ".bak")) { File.Delete(fullPathFileName + ".bak"); } File.Move(fullPathFileName, fullPathFileName + ".bak"); } } catch (Exception e) { Debugging.panelMessage(e.Message); } try { xmlDoc.Save(fullPathFileName); } catch (Exception e) { Debugging.panelMessage(e.Message); return(false); // Only time when we say there's an error } return(true); } // end writeXML
/// <summary> /// /// </summary> /// <param name="pollutionNode"></param> private void readPollutionNode(XmlNode pollutionNode) { string name = ""; foreach (XmlNode node in pollutionNode.ChildNodes) { try { // Extract power, water, sewage, garbage and wealth string[] attr = node.Name.Split(new char[] { '_' }); name = attr[0]; int level = Convert.ToInt32(attr[1]) - 1; int ground = Convert.ToInt32(node.Attributes["ground"].InnerText); int noise = Convert.ToInt32(node.Attributes["noise"].InnerText); switch (name) { case "ResidentialLow": setPollutionRates(DataStore.residentialLow[level], ground, noise); break; case "ResidentialHigh": setPollutionRates(DataStore.residentialHigh[level], ground, noise); break; case "CommercialLow": setPollutionRates(DataStore.commercialLow[level], ground, noise); break; case "CommercialHigh": setPollutionRates(DataStore.commercialHigh[level], ground, noise); break; case "CommercialTourist": setPollutionRates(DataStore.commercialTourist[level], ground, noise); break; case "CommercialLeisure": setPollutionRates(DataStore.commercialLeisure[level], ground, noise); break; case "Office": setPollutionRates(DataStore.office[level], ground, noise); break; case "Industry": setPollutionRates(DataStore.industry[level], ground, noise); break; case "IndustryOre": setPollutionRates(DataStore.industry_ore[level], ground, noise); break; case "IndustryOil": setPollutionRates(DataStore.industry_oil[level], ground, noise); break; case "IndustryForest": setPollutionRates(DataStore.industry_forest[level], ground, noise); break; case "IndustryFarm": setPollutionRates(DataStore.industry_farm[level], ground, noise); break; } } catch (Exception e) { Debugging.panelMessage("readPollutionNode: " + name + " " + e.Message); } } }
/// <summary> /// /// </summary> /// <param name="popNode"></param> private void readPopulationNode(XmlNode popNode) { foreach (XmlNode node in popNode.ChildNodes) { if (node.Name.Equals(bonusHouseName)) { string[] values = node.InnerText.Split(new char[] { STRING_DELIMITER }); DataStore.bonusHouseholdCache.Clear(); // Okay to clear now that we have seen the meshname node foreach (string name in values) { DataStore.bonusHouseholdCache.Add(name, 1); } } else { string[] attr = node.Name.Split(new char[] { '_' }); string name = attr[0]; int level = Convert.ToInt32(attr[1]) - 1; int[] array = new int[12]; try { array = getArray(name, level, "readPopulationNode"); int temp = Convert.ToInt32(node.Attributes["level_height"].InnerText); array[DataStore.LEVEL_HEIGHT] = temp > 0 ? temp : 10; temp = Convert.ToInt32(node.Attributes["space_pp"].InnerText); if (temp <= 0) { temp = 100; // Bad person trying to give negative or div0 error. } array[DataStore.PEOPLE] = transformPopulationModifier(name, level, temp, false); } catch (Exception e) { Debugging.panelMessage("readPopulationNode, part a: " + e.Message); } if (!name.Contains("Residential")) { try { int dense = Convert.ToInt32(node.Attributes["ground_mult"].InnerText); array[DataStore.DENSIFICATION] = dense >= 0 ? dense : 0; // Force to be greater than 0 int level0 = Convert.ToInt32(node.Attributes["lvl_0"].InnerText); int level1 = Convert.ToInt32(node.Attributes["lvl_1"].InnerText); int level2 = Convert.ToInt32(node.Attributes["lvl_2"].InnerText); int level3 = Convert.ToInt32(node.Attributes["lvl_3"].InnerText); // Ensure all is there first array[DataStore.WORK_LVL0] = level0; array[DataStore.WORK_LVL1] = level1; array[DataStore.WORK_LVL2] = level2; array[DataStore.WORK_LVL3] = level3; } catch (Exception e) { Debugging.panelMessage("readPopulationNode, part b: " + e.Message); } } } } // end foreach }