comparison RollcallFactory.cs @ 6:0509ed2e686a

Re #16 - File loading * Refactor race detail parsing and unit type parsing in to their own classes Re #17 - Unit detail loading * Add a couple of extra attributes to those loaded
author IBBoard <dev@ibboard.co.uk>
date Mon, 26 Jan 2009 20:31:05 +0000
parents 8c34e01a11da
children 35bc86f8c283
comparison
equal deleted inserted replaced
5:8c34e01a11da 6:0509ed2e686a
13 13
14 namespace IBBoard.WarFoundry.Plugin.Rollcall 14 namespace IBBoard.WarFoundry.Plugin.Rollcall
15 { 15 {
16 public class RollcallFactory : AbstractNonNativeFileExtensionWarFoundryFactory 16 public class RollcallFactory : AbstractNonNativeFileExtensionWarFoundryFactory
17 { 17 {
18 private static RollcallFactory factory;
18 private GameSystem rollcallSystem; 19 private GameSystem rollcallSystem;
19 private bool addedSystem = false; 20 private bool addedSystem = false;
20 21
21 public static RollcallFactory CreateFactory() 22 public static RollcallFactory GetFactory()
22 { 23 {
23 return new RollcallFactory(); 24 if (factory == null)
25 {
26 factory = new RollcallFactory();
27 }
28
29 return factory;
24 } 30 }
25 31
26 private RollcallFactory() 32 private RollcallFactory()
27 { 33 {
28 rollcallSystem = new GameSystem("Rollcall", "Rollcall armies", this); 34 rollcallSystem = new GameSystem("Rollcall", "Rollcall armies", this);
43 } 49 }
44 50
45 protected override string RaceFileExtension { 51 protected override string RaceFileExtension {
46 get { return ".adf"; } 52 get { return ".adf"; }
47 } 53 }
54
55 public GameSystem RollcallSystem
56 {
57 get
58 {
59 return rollcallSystem;
60 }
61 }
48 62
49 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile(FileInfo file) 63 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile(FileInfo file)
50 { 64 {
51 ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>(); 65 ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();
52 66
59 objects.Add(CreateArmyFromFile(file)); 73 objects.Add(CreateArmyFromFile(file));
60 } 74 }
61 75
62 if (!addedSystem && objects.Count > 0) 76 if (!addedSystem && objects.Count > 0)
63 { 77 {
64 objects.Add(rollcallSystem); 78 objects.Add(RollcallSystem);
65 addedSystem = true; 79 addedSystem = true;
66 } 80 }
67 81
68 return objects; 82 return objects;
69 } 83 }
74 } 88 }
75 89
76 protected override Race CreateRaceFromFile(FileInfo file) 90 protected override Race CreateRaceFromFile(FileInfo file)
77 { 91 {
78 IniFile rollcallFile = IniFileReader.ReadFile(file); 92 IniFile rollcallFile = IniFileReader.ReadFile(file);
79 Race race = ReadRaceDetails(rollcallFile); 93 Race race = RollcallRaceParser.ReadRaceDetails(rollcallFile);
80 ReadCategories(rollcallFile, race); 94 RollcallRaceParser.ReadCategories(rollcallFile, race);
81 ReadUnitTypeAndEquipmentSections(rollcallFile, race); 95 RollcallRaceParser.ReadUnitTypeAndEquipmentSections(rollcallFile, race);
82 return race; 96 return race;
83 }
84
85 private Race ReadRaceDetails(IniFile file)
86 {
87 string id = null, name = null;
88 id = "Rollcall" + file["Army"]["BitCodeID"].Value;
89 name = file["Army"]["Name"].Value;
90 LogNotifier.Debug(GetType(), "Loading Rollcall race ID "+id);
91 Race race = new Race(id, name, "Rollcall", this);
92 race.GameSystem = rollcallSystem;
93 return race;
94 }
95
96 private void ReadCategories(IniFile file, Race race)
97 {
98 IniSection section = file["Category"];
99
100 foreach (string key in section.Keys)
101 {
102 string valueString = section[key].Value;
103 string[] values = valueString.Split(',');
104
105 if (values.Length == 3)
106 {
107 LogNotifier.Debug(GetType(), "Loading category " + values[0]);
108 int minPercent = 0;
109 int.TryParse(values[1], out minPercent);
110 int maxPercent = 100;
111 int.TryParse(values[2], out maxPercent);
112 maxPercent = Math.Max(0, Math.Min(100, Math.Max(minPercent, maxPercent)));
113 minPercent = Math.Max(0, Math.Min(100, minPercent));
114 Category category = new Category(key, values[0]);
115 category.MaximumPercentage = maxPercent;
116 category.MinimumPercentage = minPercent;
117 race.AddCategory(category);
118
119 }
120 //Special cases (allies and aliases) need to be handled later
121 }
122 }
123
124 private void ReadUnitTypeAndEquipmentSections(IniFile file, Race race)
125 {
126 foreach (IniSection section in file)
127 {
128 string sectionName = section.Name;
129
130 if (sectionName == "Army" || sectionName == "Category")
131 {
132 continue;
133 }
134 else if (sectionName.StartsWith("Unit"))
135 {
136 ReadUnitTypeSection(file, section, race);
137 }
138 else
139 {
140 ReadEquipmentSection(file, section, race);
141 }
142 }
143 }
144
145 private UnitType ReadUnitTypeSectionFromID(IniFile file, String sectionID, Race race)
146 {
147 return ReadUnitTypeSection(file, "Unit"+sectionID, race);
148 }
149
150 private UnitType ReadUnitTypeSection(IniFile file, String sectionName, Race race)
151 {
152 return ReadUnitTypeSection(file, file[sectionName], race);
153 }
154
155 private UnitType ReadUnitTypeSection(IniFile file, IniSection section, Race race)
156 {
157 UnitType unitType = race.GetUnitType(section.Name);
158
159 if (unitType == null)
160 {
161 string unitID = GetUnitID(section);
162 string name = section["Name"].Value;
163 unitType = new UnitType(unitID, name, race);
164 Category mainCat = race.GetCategory(section["Category"].Value);
165 unitType.AddCategory(mainCat);
166 unitType.MainCategory = mainCat;
167 //Load other values
168 race.AddUnitType(unitType);
169 }
170
171 return unitType;
172 }
173
174 private string GetUnitID(IniSection section)
175 {
176 string sectionName = section.Name;
177
178 if (sectionName != "Unit" + section["UnitID"].Value)
179 {
180 throw new InvalidFileException("Attribute 'UnitID' for "+sectionName+" did not match section name");
181 }
182
183 return sectionName;
184 }
185
186 private void ReadEquipmentSection(IniFile file, IniSection section, Race race)
187 {
188 } 97 }
189 98
190 protected override GameSystem CreateGameSystemFromFile (FileInfo file) 99 protected override GameSystem CreateGameSystemFromFile (FileInfo file)
191 { 100 {
192 throw new InvalidDataException("No such file format (Rollcall GameSystem)"); 101 throw new InvalidDataException("No such file format (Rollcall GameSystem)");