6
|
1 // This file (RollcallUnitTypeParser.cs) is a part of the IBBoard.WarFoundry.Plugin.Rollcall library and is copyright 2009 IBBoard.
|
|
2 //
|
|
3 // The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license.
|
|
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);
|
|
35 string name = section["Name"].Value;
|
|
36 unitType = new UnitType(unitID, name, race);
|
|
37 Category mainCat = race.GetCategory(section["Category"].Value);
|
|
38 unitType.AddCategory(mainCat);
|
|
39 unitType.MainCategory = mainCat;
|
|
40 IniKeyValuePairLine line = section["MaximumSize"];
|
|
41
|
|
42 if (line!=null)
|
|
43 {
|
|
44 unitType.MaxSize = NumberParser.ParseAsInt(line.Value, -1);
|
|
45 }
|
|
46
|
|
47 line = section["MinimumSize"];
|
|
48
|
|
49 if (line!=null)
|
|
50 {
|
|
51 unitType.MinSize = NumberParser.ParseAsInt(line.Value, 1);
|
|
52 }
|
|
53
|
|
54 line = section["TroopCost"];
|
|
55
|
|
56 if (line!=null)
|
|
57 {
|
|
58 unitType.CostPerTrooper = NumberParser.ParseAsInt(line.Value, 0);
|
|
59 }
|
|
60
|
|
61 race.AddUnitType(unitType);
|
|
62 }
|
|
63
|
|
64 return unitType;
|
|
65 }
|
|
66
|
|
67 private static string GetUnitID(IniSection section)
|
|
68 {
|
|
69 string sectionName = section.Name;
|
|
70
|
|
71 if (sectionName != "Unit" + section["UnitID"].Value)
|
|
72 {
|
|
73 throw new InvalidFileException("Attribute 'UnitID' for "+sectionName+" did not match section name");
|
|
74 }
|
|
75
|
|
76 return sectionName;
|
|
77 }
|
|
78
|
|
79 public static void ReadEquipmentSection(IniFile file, IniSection section, Race race)
|
|
80 {
|
|
81 }
|
|
82 }
|
|
83 }
|