Mercurial > repos > IBBoard.WarFoundry.Plugin.Rollcall
diff RollcallUnitTypeParser.cs @ 8:35bc86f8c283
Re #17 - Load unit data
* Parse more attributes including troop cost
* Improve handling of required values
* Refactor code in to generic parent
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 21 Feb 2009 21:00:52 +0000 |
parents | 0509ed2e686a |
children | 5f0259e69a7f |
line wrap: on
line diff
--- a/RollcallUnitTypeParser.cs Tue Feb 17 16:36:15 2009 +0000 +++ b/RollcallUnitTypeParser.cs Sat Feb 21 21:00:52 2009 +0000 @@ -32,32 +32,29 @@ if (unitType == null) { string unitID = RollcallUnitTypeParser.GetUnitID(section); - string name = section["Name"].Value; + string name = section.GetLineValue("Name", ""); + + if (name == null || name == "") + { + throw new InvalidFileException("Attribute 'Name' for "+unitID+" was missing"); + } + unitType = new UnitType(unitID, name, race); - Category mainCat = race.GetCategory(section["Category"].Value); + + Category mainCat = race.GetCategory(section.GetLineValue("Category")); + + if (mainCat == null) + { + throw new InvalidFileException("Attribute 'Category' for "+unitID+" did not match a category"); + } + unitType.AddCategory(mainCat); unitType.MainCategory = mainCat; - IniKeyValuePairLine line = section["MaximumSize"]; - - if (line!=null) - { - unitType.MaxSize = NumberParser.ParseAsInt(line.Value, -1); - } - - line = section["MinimumSize"]; - - if (line!=null) - { - unitType.MinSize = NumberParser.ParseAsInt(line.Value, 1); - } - - line = section["TroopCost"]; - - if (line!=null) - { - unitType.CostPerTrooper = NumberParser.ParseAsInt(line.Value, 0); - } - + unitType.MaxSize = GetRequiredNumericLine(section, "MaximumSize"); + unitType.MinSize = GetNumericLine(section, "MinimumSize", 1); + unitType.MaxNumber = GetNumericLine(section, "MinNumber", 0); + unitType.MinNumber = GetNumericLine(section, "MaxNumber", -1); + unitType.CostPerTrooper = GetRequiredNumericLine(section, "TroopCost"); race.AddUnitType(unitType); } @@ -68,7 +65,11 @@ { string sectionName = section.Name; - if (sectionName != "Unit" + section["UnitID"].Value) + if (!sectionName.StartsWith("Unit") || sectionName.Length <= 4) + { + throw new InvalidFileException("Unit section named "+sectionName+" did not have a valid section name"); + } + if (sectionName != "Unit" + section.GetLineValue("UnitID", "")) { throw new InvalidFileException("Attribute 'UnitID' for "+sectionName+" did not match section name"); } @@ -76,6 +77,29 @@ return sectionName; } + private static int GetRequiredNumericLine(IniSection section, string key) + { + int lineValue = GetNumericLine(section, key); + + if (lineValue == int.MinValue) + { + throw new InvalidFileException("Attribute '"+key+"' was required but did not exist in section "+section.Name); + } + + return lineValue; + } + + private static int GetNumericLine(IniSection section, string key) + { + return GetNumericLine(section, key, int.MinValue); + } + + private static int GetNumericLine(IniSection section, string key, int defaultValue) + { + string line = section.GetLineValue(key, defaultValue.ToString()); + return NumberParser.ParseAsInt(line, defaultValue); + } + public static void ReadEquipmentSection(IniFile file, IniSection section, Race race) { }