Mercurial > repos > IBBoard.WarFoundry.Plugin.Rollcall
annotate RollcallUnitTypeParser.cs @ 14:f7582a3c8bdf default tip
* Fix tooling version - we're 2.0 compatible, not 3.5
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 26 Jan 2011 20:04:35 +0000 |
parents | 9aecf6628eb0 |
children |
rev | line source |
---|---|
13
9aecf6628eb0
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
11
diff
changeset
|
1 // This file (RollcallUnitTypeParser.cs) is a part of the IBBoard.WarFoundry.Plugin.Rollcall project and is copyright 2008, 2009 IBBoard. |
6 | 2 // |
13
9aecf6628eb0
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
11
diff
changeset
|
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. |
6 | 4 |
5 using System; | |
6 using IBBoard.IO; | |
7 using IBBoard.Ini; | |
8 using IBBoard.CustomMath; | |
9 using IBBoard.WarFoundry.API.Objects; | |
10 | |
11 namespace IBBoard.WarFoundry.Plugin.Rollcall | |
12 { | |
13 /// <summary> | |
14 /// A helper class to parse INI sections as Units | |
15 /// </summary> | |
16 public class RollcallUnitTypeParser | |
17 { | |
18 public static UnitType ReadUnitTypeSectionFromID(IniFile file, String sectionID, Race race) | |
19 { | |
20 return ReadUnitTypeSection(file, "Unit"+sectionID, race); | |
21 } | |
22 | |
23 public static UnitType ReadUnitTypeSection(IniFile file, String sectionName, Race race) | |
24 { | |
25 return ReadUnitTypeSection(file, file[sectionName], race); | |
26 } | |
27 | |
28 public static UnitType ReadUnitTypeSection(IniFile file, IniSection section, Race race) | |
29 { | |
30 UnitType unitType = race.GetUnitType(section.Name); | |
31 | |
32 if (unitType == null) | |
33 { | |
34 string unitID = RollcallUnitTypeParser.GetUnitID(section); | |
8 | 35 string name = section.GetLineValue("Name", ""); |
36 | |
37 if (name == null || name == "") | |
38 { | |
39 throw new InvalidFileException("Attribute 'Name' for "+unitID+" was missing"); | |
40 } | |
41 | |
6 | 42 unitType = new UnitType(unitID, name, race); |
8 | 43 |
44 Category mainCat = race.GetCategory(section.GetLineValue("Category")); | |
45 | |
46 if (mainCat == null) | |
47 { | |
48 throw new InvalidFileException("Attribute 'Category' for "+unitID+" did not match a category"); | |
49 } | |
50 | |
6 | 51 unitType.MainCategory = mainCat; |
8 | 52 unitType.MaxSize = GetRequiredNumericLine(section, "MaximumSize"); |
53 unitType.MinSize = GetNumericLine(section, "MinimumSize", 1); | |
54 unitType.MaxNumber = GetNumericLine(section, "MinNumber", 0); | |
55 unitType.MinNumber = GetNumericLine(section, "MaxNumber", -1); | |
56 unitType.CostPerTrooper = GetRequiredNumericLine(section, "TroopCost"); | |
6 | 57 race.AddUnitType(unitType); |
58 } | |
59 | |
60 return unitType; | |
61 } | |
62 | |
63 private static string GetUnitID(IniSection section) | |
64 { | |
65 string sectionName = section.Name; | |
66 | |
8 | 67 if (!sectionName.StartsWith("Unit") || sectionName.Length <= 4) |
68 { | |
69 throw new InvalidFileException("Unit section named "+sectionName+" did not have a valid section name"); | |
70 } | |
71 if (sectionName != "Unit" + section.GetLineValue("UnitID", "")) | |
6 | 72 { |
73 throw new InvalidFileException("Attribute 'UnitID' for "+sectionName+" did not match section name"); | |
74 } | |
75 | |
76 return sectionName; | |
77 } | |
78 | |
8 | 79 private static int GetRequiredNumericLine(IniSection section, string key) |
80 { | |
81 int lineValue = GetNumericLine(section, key); | |
82 | |
83 if (lineValue == int.MinValue) | |
84 { | |
85 throw new InvalidFileException("Attribute '"+key+"' was required but did not exist in section "+section.Name); | |
86 } | |
87 | |
88 return lineValue; | |
89 } | |
90 | |
91 private static int GetNumericLine(IniSection section, string key) | |
92 { | |
93 return GetNumericLine(section, key, int.MinValue); | |
94 } | |
95 | |
96 private static int GetNumericLine(IniSection section, string key, int defaultValue) | |
97 { | |
98 string line = section.GetLineValue(key, defaultValue.ToString()); | |
99 return NumberParser.ParseAsInt(line, defaultValue); | |
100 } | |
101 | |
6 | 102 public static void ReadEquipmentSection(IniFile file, IniSection section, Race race) |
103 { | |
104 } | |
105 } | |
106 } |