Created
July 2, 2015 12:48
-
-
Save boformer/a41ce6f199387d538d6a to your computer and use it in GitHub Desktop.
Better Growables config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.IO; | |
using System.Xml.Serialization; | |
using UnityEngine; | |
namespace BetterGrowables | |
{ | |
public class Configuration | |
{ | |
[XmlArray(ElementName = "Buildings")] | |
[XmlArrayItem(ElementName = "Building")] | |
public List<Building> buildings = new List<Building>(); | |
public Building GetBuilding(string name) | |
{ | |
foreach (Building building in buildings) | |
{ | |
if (building.name == name) return building; | |
} | |
return null; | |
} | |
public class Building | |
{ | |
[XmlIgnoreAttribute] | |
public bool isBuiltIn = false; | |
[XmlAttribute("name")] | |
public string name; | |
[XmlArray(ElementName = "Variations")] | |
[XmlArrayItem(ElementName = "Variation")] | |
public List<Variation> variations; | |
public Building(string name) | |
{ | |
this.name = name; | |
} | |
public Building() { } | |
public Variation GetVariation(string name) | |
{ | |
foreach (Variation variation in variations) | |
{ | |
if (variation.name == name) return variation; | |
} | |
return null; | |
} | |
} | |
public class Variation | |
{ | |
[XmlIgnoreAttribute] | |
public bool isBuiltIn = false; | |
[XmlAttribute("name"), DefaultValue(null)] | |
public string name; | |
[XmlAttribute("zone"), DefaultValue(null)] | |
public string zone; | |
[XmlAttribute("level"), DefaultValue(-1)] | |
public int level; | |
public ItemClass ToItemClass(ItemClass baseClass) | |
{ | |
var itemClass = new ItemClass(); | |
switch (zone.ToLowerInvariant()) | |
{ | |
case "residentiallow": | |
itemClass.m_service = ItemClass.Service.Residential; | |
itemClass.m_subService = ItemClass.SubService.ResidentialLow; | |
break; | |
case "residentialhigh": | |
itemClass.m_service = ItemClass.Service.Residential; | |
itemClass.m_subService = ItemClass.SubService.ResidentialHigh; | |
break; | |
case "commerciallow": | |
itemClass.m_service = ItemClass.Service.Commercial; | |
itemClass.m_subService = ItemClass.SubService.CommercialLow; | |
break; | |
case "commercialhigh": | |
itemClass.m_service = ItemClass.Service.Commercial; | |
itemClass.m_subService = ItemClass.SubService.CommercialHigh; | |
break; | |
case "industrial": | |
goto case "industrialgeneric"; | |
case "industrialgeneric": | |
itemClass.m_service = ItemClass.Service.Industrial; | |
itemClass.m_subService = ItemClass.SubService.IndustrialGeneric; | |
break; | |
case "industrialforestry": | |
itemClass.m_service = ItemClass.Service.Industrial; | |
itemClass.m_subService = ItemClass.SubService.IndustrialForestry; | |
break; | |
case "industrialfarming": | |
itemClass.m_service = ItemClass.Service.Industrial; | |
itemClass.m_subService = ItemClass.SubService.IndustrialFarming; | |
break; | |
case "industrialoil": | |
itemClass.m_service = ItemClass.Service.Industrial; | |
itemClass.m_subService = ItemClass.SubService.IndustrialOil; | |
break; | |
case "industrialore": | |
itemClass.m_service = ItemClass.Service.Industrial; | |
itemClass.m_subService = ItemClass.SubService.IndustrialOre; | |
break; | |
case "office": | |
itemClass.m_service = ItemClass.Service.Office; | |
itemClass.m_subService = ItemClass.SubService.None; | |
break; | |
default: | |
itemClass.m_service = baseClass.m_service; | |
itemClass.m_subService = baseClass.m_subService; | |
break; | |
} | |
switch (level) | |
{ | |
case 1: | |
itemClass.m_level = ItemClass.Level.Level1; | |
break; | |
case 2: | |
itemClass.m_level = ItemClass.Level.Level2; | |
break; | |
case 3: | |
itemClass.m_level = ItemClass.Level.Level3; | |
break; | |
case 4: | |
itemClass.m_level = ItemClass.Level.Level4; | |
break; | |
case 5: | |
itemClass.m_level = ItemClass.Level.Level5; | |
break; | |
default: | |
itemClass.m_level = baseClass.m_level; | |
break; | |
} | |
if (itemClass.m_service != ItemClass.Service.Residential && itemClass.m_level > ItemClass.Level.Level3) | |
{ | |
itemClass.m_level = ItemClass.Level.Level3; | |
} | |
if (itemClass.m_service == ItemClass.Service.Industrial && itemClass.m_subService != ItemClass.SubService.IndustrialGeneric) | |
{ | |
itemClass.m_level = ItemClass.Level.Level1; | |
} | |
return itemClass; | |
} | |
} | |
public static Configuration Deserialize(string filename) | |
{ | |
if (!File.Exists(filename)) return null; | |
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration)); | |
try | |
{ | |
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(filename)) | |
{ | |
return (Configuration)xmlSerializer.Deserialize(streamReader); | |
} | |
} | |
catch (Exception e) | |
{ | |
Debug.Log("Couldn't load configuration (XML malformed?)"); | |
throw e; | |
} | |
} | |
public static void Serialize(string filename, Configuration config) | |
{ | |
var xmlSerializer = new XmlSerializer(typeof(Configuration)); | |
try | |
{ | |
using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(filename)) | |
{ | |
var configCopy = new Configuration(); | |
foreach (var building in config.buildings) | |
{ | |
var newBuilding = new Building(building.name); | |
foreach (var variation in building.variations.Where(variation => !building.isBuiltIn || !variation.isBuiltIn)) | |
{ | |
newBuilding.variations.Add(variation); | |
} | |
if (!building.isBuiltIn || newBuilding.variations.Count > 0) | |
{ | |
configCopy.buildings.Add(newBuilding); | |
} | |
} | |
xmlSerializer.Serialize(streamWriter, configCopy); | |
} | |
} | |
catch (Exception e) | |
{ | |
Debug.Log("Couldn't create configuration file at \"" + Directory.GetCurrentDirectory() + "\""); | |
throw e; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment