comparison api/Factories/Xml/WarFoundryXmlFactory.cs @ 8:613bc5eaac59

Re #9 - Make WarFoundry loading granular * Remove specific staged loading classes * Rework category loading for GameSystem and Race to make it use AddCategory(Category) method * Promote staged loading from Native Factory to all Factories level * Refactor XML Factory to use smaller methods Also removed some commented code that isn't used any more
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Jan 2009 19:24:13 +0000
parents 150a5669cd7b
children 6ad505b6c36e
comparison
equal deleted inserted replaced
7:895c8a2378a1 8:613bc5eaac59
130 130
131 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem) 131 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem)
132 { 132 {
133 string id = elem.GetAttribute("id"); 133 string id = elem.GetAttribute("id");
134 string name = elem.GetAttribute("name"); 134 string name = elem.GetAttribute("name");
135 GameSystem system = new StagedLoadingGameSystem(id, name, this); 135 GameSystem system = new GameSystem(id, name, this);
136 //system.SourceZipFile = file.; 136 //system.SourceZipFile = file.;
137 extraData[system] = elem.OwnerDocument; 137 extraData[system] = elem.OwnerDocument;
138 return system; 138 return system;
139 } 139 }
140 140
154 { 154 {
155 string id = elem.GetAttribute("id"); 155 string id = elem.GetAttribute("id");
156 string subid = elem.GetAttribute("subid"); 156 string subid = elem.GetAttribute("subid");
157 string systemID = elem.GetAttribute("system"); 157 string systemID = elem.GetAttribute("system");
158 string name = elem.GetAttribute("name"); 158 string name = elem.GetAttribute("name");
159 Race race = new StagedLoadingRace(id, subid, name, systemID, this); 159 Race race = new Race(id, subid, name, systemID, this);
160 //race.SourceZipFile = file; //TODO reference source file 160 //race.SourceZipFile = file; //TODO reference source file
161 extraData[race] = elem.OwnerDocument; 161 extraData[race] = elem.OwnerDocument;
162 return race; 162 return race;
163 } 163 }
164 164
165 /*public WarFoundryObject CreateObjectFromStream(ZipFile file, Stream stream) 165 public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
166 { 166 {
167 try 167 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
168 { 168
169 WarFoundryObject obj = LoadFileObjectFromElement(file, elem); 169 if (obj is GameSystem)
170 {
171 CompleteLoading((GameSystem)obj);
172 }
173 else if (obj is Race)
174 {
175 CompleteLoading((Race)obj);
176 }
177 }
178
179 public XmlDocument GetExtraData(IWarFoundryObject obj)
180 {
181 XmlDocument extra = null;
182 extraData.TryGetValue(obj, out extra);
183 return extra;
184 }
185
186 public void CompleteLoading(GameSystem system)
187 {
188 if (system.IsFullyLoaded)
189 {
190 LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID);
191 return;
192 }
193
194 XmlDocument extra = GetExtraData(system);
195 XmlNode elem = extra.LastChild;
196
197 XmlNode catsElem = elem.FirstChild;
198 WarFoundryObject tempObj;
199
200 foreach (XmlElement cat in catsElem.ChildNodes)
201 {
202 tempObj = CreateObjectFromElement(cat);
170 203
171 if (obj != null) 204 if (tempObj is Category)
172 { 205 {
173 extraData[obj] = doc; 206 system.AddCategory((Category)tempObj);
174 return obj;
175 } 207 }
176 else 208 else
177 { 209 {
178 throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidXmlFile", "XML file '{0}' was not a loadable XML file. Please ensure it is a valid and supported WarFoundry XML file."), file.Name)); 210 LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml);
179 } 211 }
180 } 212 }
181 catch (XmlSchemaException ex) 213
182 { 214 XmlElement statsElem = (XmlElement)catsElem.NextSibling;
183 throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidXmlFile", "Failed to parse invalid XML data file in {0}. Error at line {1} position {2}: {3}"), file.Name, ex.LineNumber, ex.LinePosition, ex.Message.Substring(0, ex.Message.IndexOf(".")+1)), ex); 215 LoadSystemStatsFromElement(statsElem, system);
184 } 216 string defaultStatsID = statsElem.GetAttribute("defaultStats");
185 catch (InvalidFileException ex) 217
186 { 218 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
187 throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidNamedXmlFile", "XML data file in '{0}' was not a valid XML file. It should contain three child nodes - XML definition, DocType and root node."), file.Name), ex); 219 system.StandardSystemStatsID = defaultStatsID;
188 } 220 system.SetAsFullyLoaded();
189 } 221 }
190 222
191 private WarFoundryObject LoadFileObjectFromElement(ZipFile file, XmlElement elem) 223 public void CompleteLoading(Race race)
192 { 224 {
193 WarFoundryObject obj = null; 225 if (race.IsFullyLoaded)
226 {
227 LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID);
228 return;
229 }
230
231 XmlDocument extra = GetExtraData(race);
232 XmlNode elem = extra.LastChild;
233 XmlNode colNode = elem.FirstChild;
234
235 Dictionary<string, UnitType> unitTypes = new Dictionary<string, UnitType>();
236
237 foreach (XmlElement node in colNode.ChildNodes)
238 {
239 UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
240 unitTypes.Add(type.ID, type);
241 }
242
243 colNode = colNode.NextSibling;
244
245 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value)
246 {
247 foreach (XmlElement node in colNode.ChildNodes)
248 {
249 race.AddCategory(CreateCategoryFromElement(node));
250 }
194 251
195 if (elem.Name.Equals(WarFoundryXmlElementName.SYSTEM_ELEMENT.Value)) 252 colNode = colNode.NextSibling;
196 { 253 }
197 logger.Debug("Create GameSystem");
198 obj = CreateSystemFromElement(file, elem);
199 }
200 else if (elem.Name.Equals(WarFoundryXmlElementName.RACE_ELEMENT.Value))
201 {
202 logger.Debug("Create Race");
203 obj = CreateRaceFromElement(file, elem);
204 }
205
206 return obj;
207 }*/
208
209 public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
210 {
211 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
212 254
213 if (!obj.IsFullyLoaded) 255 Dictionary<string, EquipmentItem> raceEquipment = new Dictionary<string, EquipmentItem>();
214 { 256
215 XmlDocument extra = extraData[obj]; 257 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value)
216 258 {
217 if (obj is GameSystem) 259 foreach (XmlElement node in colNode.ChildNodes)
218 { 260 {
219 GameSystem system = (GameSystem)obj; 261 EquipmentItem item = CreateEquipmentItemFromElement(node, race);
220 XmlNode elem = extra.LastChild; 262 raceEquipment.Add(item.ID, item);
221
222 XmlNode catsElem = elem.FirstChild;
223 Category[] cats;
224 List<Category> catList = new List<Category>();
225 WarFoundryObject tempObj;
226
227 foreach (XmlElement cat in catsElem.ChildNodes)
228 {
229 tempObj = CreateObjectFromElement(cat);
230
231 if (tempObj is Category)
232 {
233 catList.Add((Category)tempObj);
234 }
235 else
236 {
237 LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml);
238 }
239 }
240
241 cats = catList.ToArray();
242 LogNotifier.DebugFormat(GetType(), "Found {0} categories", cats.Length);
243
244 XmlElement statsElem = (XmlElement)catsElem.NextSibling;
245 LoadSystemStatsFromElement(statsElem, system);
246 string defaultStatsID = statsElem.GetAttribute("defaultStats");
247
248 LogNotifier.DebugFormat(GetType(), "Complete loading of {0}", system.Name);
249 system.Categories = cats;
250 system.StandardSystemStatsID = defaultStatsID;
251 } 263 }
252 else if (obj is Race) 264 }
253 { 265
254 Race race = (Race)obj; 266 Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
255 XmlNode elem = extra.LastChild; 267 //TODO: Load abilities
256 XmlNode colNode = elem.FirstChild; 268
257 269 race.SetUnitTypes(unitTypes);
258 Dictionary<string, UnitType> unitTypes = new Dictionary<string, UnitType>(); 270 race.SetEquipmentItems(raceEquipment);
259 271 race.SetAbilities(raceAbilities);
260 foreach (XmlElement node in colNode.ChildNodes) 272 race.SetAsFullyLoaded();
261 { 273 LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name);
262 UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
263 unitTypes.Add(type.ID, type);
264 }
265
266 colNode = colNode.NextSibling;
267 List<Category> catOverrides = new List<Category>();
268
269 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value)
270 {
271 foreach (XmlElement node in colNode.ChildNodes)
272 {
273 catOverrides.Add(CreateCategoryFromElement(node));
274 }
275
276 colNode = colNode.NextSibling;
277 }
278
279 Dictionary<string, EquipmentItem> raceEquipment = new Dictionary<string, EquipmentItem>();
280
281 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value)
282 {
283 foreach (XmlElement node in colNode.ChildNodes)
284 {
285 EquipmentItem item = CreateEquipmentItemFromElement(node, race);
286 raceEquipment.Add(item.ID, item);
287 }
288 }
289
290 Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
291 //TODO: Load abilities
292
293 LogNotifier.DebugFormat(GetType(), "Complete loading of {0}", race.Name);
294 race.Categories = catOverrides.ToArray();
295 race.SetUnitTypes(unitTypes);
296 race.SetEquipmentItems(raceEquipment);
297 race.SetAbilities(raceAbilities);
298 }
299 }
300 else
301 {
302 LogNotifier.Debug(GetType(), "Object is already fully loaded");
303 }
304 } 274 }
305 275
306 protected XmlDocument CreateXmlDocumentFromStream(Stream stream) 276 protected XmlDocument CreateXmlDocumentFromStream(Stream stream)
307 { 277 {
308 XmlDocument doc = new XmlDocument(); 278 XmlDocument doc = new XmlDocument();