comparison api/Factories/Xml/WarFoundryXmlRaceFactory.cs @ 195:11bad32cfa06

* Implement new equipment limit loading * Check for potential null when getting limit object for element no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Fri, 30 Oct 2009 20:31:00 +0000
parents 2d0c4248a915
children c4cf4c7db7d5
comparison
equal deleted inserted replaced
194:1412a42190a1 195:11bad32cfa06
177 private static AbstractLimit GetMaxLimit (XmlElement equipSlot) 177 private static AbstractLimit GetMaxLimit (XmlElement equipSlot)
178 { 178 {
179 XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(equipSlot, "race:maxLimit/*[1]"); 179 XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(equipSlot, "race:maxLimit/*[1]");
180 return GetLimitFromElement(limitElem); 180 return GetLimitFromElement(limitElem);
181 } 181 }
182
183 182
184 private static AbstractLimit GetLimitFromElement(XmlElement limitElem) 183 private static AbstractLimit GetLimitFromElement(XmlElement limitElem)
185 { 184 {
186 AbstractLimit limit = null; 185 AbstractLimit limit = null;
187 186
188 switch(limitElem.LocalName) 187 if (limitElem != null)
189 { 188 {
190 case "percentageLimit": 189 switch (limitElem.LocalName)
191 double limitPercent = XmlTools.GetDoubleValueFromAttribute (limitElem, "limit"); 190 {
192 bool roundUp = limitElem.GetAttribute("round").Equals("up"); 191 case "percentageLimit":
193 limit = new SimpleRoundedPercentageLimit(limitPercent, roundUp); 192 double limitPercent = XmlTools.GetDoubleValueFromAttribute(limitElem, "limit");
194 break; 193 bool roundUp = limitElem.GetAttribute("round").Equals("up");
195 case "sizeConstrainedLimit": 194 limit = new SimpleRoundedPercentageLimit(limitPercent, roundUp);
196 limit = new NumericSizeConstrainedLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit")); 195 break;
197 break; 196 case "sizeConstrainedLimit":
198 case "absoluteLimit": 197 limit = new NumericSizeConstrainedLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit"));
199 limit = new AbsoluteNumericLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit")); 198 break;
200 break; 199 case "absoluteLimit":
201 case "unitSizeLimit": 200 limit = new AbsoluteNumericLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit"));
202 limit = new NumericSizeConstrainedLimit(); 201 break;
203 break; 202 case "unitSizeLimit":
204 default: 203 limit = new NumericSizeConstrainedLimit();
205 //TODO: Warn of missing handler for when we've extended the limit list 204 break;
206 break; 205 default:
206 //TODO: Warn of missing handler for when we've extended the limit list
207 break;
208 }
207 } 209 }
208 210
209 return limit; 211 return limit;
210 } 212 }
211 213
257 else 259 else
258 { 260 {
259 throw new InvalidFileException("Attribute 'equipmentSlot' of unit equipment item " + id + " for " + type.Name + " was not a valid slot name"); 261 throw new InvalidFileException("Attribute 'equipmentSlot' of unit equipment item " + id + " for " + type.Name + " was not a valid slot name");
260 } 262 }
261 } 263 }
264
265 AbstractLimit limit = GetMaxLimit(equip);
266
267 if (limit != null)
268 {
269 unitEquipItem.MaxLimit = limit;
270 }
271
272 limit = GetMinLimit(equip);
273
274 if (limit != null)
275 {
276 unitEquipItem.MinLimit = limit;
277 }
262 278
263 unitEquipItem.RoundNumberUp = equip.GetAttribute("roundDirection").Equals("up"); 279 unitEquipItem.RoundNumberUp = equip.GetAttribute("roundDirection").Equals("up");
264 280
265 try 281 try
266 { 282 {
291 } 307 }
292 else 308 else
293 { 309 {
294 throw new InvalidFileException("Equipment item with ID '" + id + "' was required by " + type.Name + " but was not found"); 310 throw new InvalidFileException("Equipment item with ID '" + id + "' was required by " + type.Name + " but was not found");
295 } 311 }
296 } 312 }
297 313 }
314
315 private static AbstractLimit GetMinLimit(XmlElement elem)
316 {
317 XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:minLimit/*[1]");
318 return GetLimitFromElement(limitElem);
298 } 319 }
299 320
300 private void LoadAbilitiesForUnitType(XmlElement elem, UnitType type) 321 private void LoadAbilitiesForUnitType(XmlElement elem, UnitType type)
301 { 322 {
302 foreach (XmlElement ability in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitAbilities/race:unitAbility")) 323 foreach (XmlElement ability in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitAbilities/race:unitAbility"))