Mercurial > repos > IBBoard.WarFoundry.Plugin.Rollcall
annotate RollcallUnitTypeParser.cs @ 11:9d0d40324494
* Update Rollcall plugin to use GPLv3 headers
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Thu, 25 Jun 2009 19:51:39 +0000 |
parents | 5f0259e69a7f |
children | 9aecf6628eb0 |
rev | line source |
---|---|
11
9d0d40324494
* Update Rollcall plugin to use GPLv3 headers
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
1 // This file (RollcallUnitTypeParser.cs) is a part of the IBBoard.WarFoundry.Plugin.Rollcall project and is copyright 2009 IBBoard. |
6 | 2 // |
11
9d0d40324494
* Update Rollcall plugin to use GPLv3 headers
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL 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 } |