} // 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
/// <param name="popNode"></param> private void ReadPopulationNode(XmlNode popNode) { try { DataStore.strictCapacity = Convert.ToBoolean(popNode.Attributes["strictCapacity"].InnerText); } catch (Exception) { // Do nothing } foreach (XmlNode node in popNode.ChildNodes) { { 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.bufferWarning("readPopulationNode, part a: " + e.Message); } try { if (Convert.ToBoolean(node.Attributes["calc"].InnerText.Equals("plot"))) { array[DataStore.CALC_METHOD] = 1; } else { array[DataStore.CALC_METHOD] = 0; } } catch { } if (!name.StartsWith("Res")) { 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.bufferWarning("readPopulationNode, part b: " + e.Message); } } } // end if } // end foreach }
/// <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.bufferWarning("readPollutionNode: " + name + " " + e.Message); } } // end foreach }
/// <param name="doc"></param> public override void readXML(XmlDocument doc) { XmlElement root = doc.DocumentElement; try { //DataStore.enableExperimental = Convert.ToBoolean(root.Attributes["experimental"].InnerText); //DataStore.timeBasedRealism = Convert.ToBoolean(root.Attributes["enableTimeVariation"].InnerText); } catch (Exception) { DataStore.enableExperimental = false; } foreach (XmlNode node in root.ChildNodes) { try { if (node.Name.Equals(popNodeName)) { ReadPopulationNode(node); } else if (node.Name.Equals(consumeNodeName)) { ReadConsumptionNode(node); } else if (node.Name.Equals(visitNodeName)) { ReadVisitNode(node); } else if (node.Name.Equals(pollutionNodeName)) { ReadPollutionNode(node); } else if (node.Name.Equals(productionNodeName)) { ReadProductionNode(node); } else if (node.Name.Equals(overrideHouseName)) { ReadOverrideHouseNode(node); } else if (node.Name.Equals(overrideWorkName)) { ReadOverrideWorkers(node); } else if (node.Name.Equals(bonusHouseName)) { ReadBonusHouseNode(node); } else if (node.Name.Equals(bonusWorkName)) { ReadBonusWorkers(node); } else if (node.Name.Equals(printHouseName)) { ReadPrintHouseNode(node); } else if (node.Name.Equals(printWorkName)) { ReadPrintWorkers(node); } } catch (Exception e) { Debugging.bufferWarning(e.Message); UnityEngine.Debug.LogException(e); } } } // end readXML
/// <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 "ResEcoLow": array = DataStore.resEcoLow[level]; break; case "ResEcoHigh": array = DataStore.resEcoHigh[level]; break; case "CommercialLow": array = DataStore.commercialLow[level]; break; case "CommercialHigh": array = DataStore.commercialHigh[level]; break; case "CommercialEco": array = DataStore.commercialEco[level]; break; case "CommercialTourist": array = DataStore.commercialTourist[level]; break; case "CommercialLeisure": array = DataStore.commercialLeisure[level]; break; case "Office": array = DataStore.office[level]; break; case "OfficeHighTech": array = DataStore.officeHighTech[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="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 }