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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using IBBoard.IO;
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using IBBoard.Ini;
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using IBBoard.CustomMath;
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 using IBBoard.WarFoundry.API.Objects;
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 namespace IBBoard.WarFoundry.Plugin.Rollcall
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 /// <summary>
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 /// A helper class to parse INI sections as Units
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 /// </summary>
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 public class RollcallUnitTypeParser
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 public static UnitType ReadUnitTypeSectionFromID(IniFile file, String sectionID, Race race)
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 return ReadUnitTypeSection(file, "Unit"+sectionID, race);
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 }
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 public static UnitType ReadUnitTypeSection(IniFile file, String sectionName, Race race)
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 return ReadUnitTypeSection(file, file[sectionName], race);
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 }
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 public static UnitType ReadUnitTypeSection(IniFile file, IniSection section, Race race)
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 UnitType unitType = race.GetUnitType(section.Name);
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 if (unitType == null)
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 string unitID = RollcallUnitTypeParser.GetUnitID(section);
8
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
35 string name = section.GetLineValue("Name", "");
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
36
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
37 if (name == null || name == "")
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
38 {
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
39 throw new InvalidFileException("Attribute 'Name' for "+unitID+" was missing");
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
40 }
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
41
6
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 unitType = new UnitType(unitID, name, race);
8
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
43
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
44 Category mainCat = race.GetCategory(section.GetLineValue("Category"));
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
45
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
46 if (mainCat == null)
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
47 {
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
48 throw new InvalidFileException("Attribute 'Category' for "+unitID+" did not match a category");
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
49 }
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
50
6
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 unitType.MainCategory = mainCat;
8
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
52 unitType.MaxSize = GetRequiredNumericLine(section, "MaximumSize");
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
53 unitType.MinSize = GetNumericLine(section, "MinimumSize", 1);
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
54 unitType.MaxNumber = GetNumericLine(section, "MinNumber", 0);
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
55 unitType.MinNumber = GetNumericLine(section, "MaxNumber", -1);
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
56 unitType.CostPerTrooper = GetRequiredNumericLine(section, "TroopCost");
6
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 race.AddUnitType(unitType);
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 }
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 return unitType;
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 }
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 private static string GetUnitID(IniSection section)
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 string sectionName = section.Name;
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66
8
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
67 if (!sectionName.StartsWith("Unit") || sectionName.Length <= 4)
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
68 {
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
69 throw new InvalidFileException("Unit section named "+sectionName+" did not have a valid section name");
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
70 }
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
71 if (sectionName != "Unit" + section.GetLineValue("UnitID", ""))
6
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 throw new InvalidFileException("Attribute 'UnitID' for "+sectionName+" did not match section name");
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 }
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 return sectionName;
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 }
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78
8
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
79 private static int GetRequiredNumericLine(IniSection section, string key)
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
80 {
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
81 int lineValue = GetNumericLine(section, key);
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
82
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
83 if (lineValue == int.MinValue)
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
84 {
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
85 throw new InvalidFileException("Attribute '"+key+"' was required but did not exist in section "+section.Name);
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
86 }
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
87
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
88 return lineValue;
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
89 }
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
90
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
91 private static int GetNumericLine(IniSection section, string key)
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
92 {
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
93 return GetNumericLine(section, key, int.MinValue);
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
94 }
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
95
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
96 private static int GetNumericLine(IniSection section, string key, int defaultValue)
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
97 {
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
98 string line = section.GetLineValue(key, defaultValue.ToString());
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
99 return NumberParser.ParseAsInt(line, defaultValue);
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
100 }
35bc86f8c283 Re #17 - Load unit data
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
101
6
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102 public static void ReadEquipmentSection(IniFile file, IniSection section, Race race)
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
103 {
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
104 }
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
105 }
0509ed2e686a Re #16 - File loading
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
106 }