changeset 257:435eb28b4549

Re #270: Add multiple categories to API * Add section for multiple categories to schema * Add category loading to factory Also: * Do more sensible check on whether we need to set MainCategory when adding category to unit type
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 May 2010 19:59:17 +0000
parents 958ecd7b2844
children 3b395ab8784d
files api/Factories/Xml/WarFoundryXmlRaceFactory.cs api/Objects/UnitType.cs schemas/race.xsd
diffstat 3 files changed, 35 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Mon May 17 20:03:05 2010 +0000
+++ b/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Tue May 18 19:59:17 2010 +0000
@@ -160,8 +160,9 @@
 				throw new InvalidFileException(ex.Message, ex);
 			}
 
+			Race race = type.Race;
 			string mainCatID = elem.GetAttribute("cat");
-			Category cat = type.Race.GetCategory(mainCatID);
+			Category cat = race.GetCategory(mainCatID);
 			
 			if (cat == null)
 			{
@@ -169,6 +170,22 @@
 			}
 			
 			type.MainCategory = cat;
+			
+			XmlNodeList unitCategories = WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitCategories/race:unitCategory");
+			
+			foreach (XmlElement unitCategory in unitCategories)
+			{
+				string catID = unitCategory.GetAttribute("catID");
+				Category unitCat = race.GetCategory(catID);
+				
+				if (unitCat == null)
+				{
+					throw new InvalidFileException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, catID));
+				}
+				
+				type.AddCategory(unitCat);
+			}
+			
 			XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:stats");
 			
 			if (statsElement!=null)
@@ -182,7 +199,7 @@
 			foreach (XmlElement unitMemberRef in unitMemberReferences)
 			{
 				string typeID = unitMemberRef.GetAttribute("typeID");
-				UnitMemberType unitMemberType = type.Race.GetUnitMemberType(typeID);
+				UnitMemberType unitMemberType = race.GetUnitMemberType(typeID);
 				type.AddUnitMemberType(unitMemberType);
 			}
 		}
--- a/api/Objects/UnitType.cs	Mon May 17 20:03:05 2010 +0000
+++ b/api/Objects/UnitType.cs	Tue May 18 19:59:17 2010 +0000
@@ -112,7 +112,7 @@
 			{
 				cats.Add(cat);
 				
-				if (cats.Count == 1)
+				if (MainCategory == null)
 				{
 					MainCategory = cat;
 				}
--- a/schemas/race.xsd	Mon May 17 20:03:05 2010 +0000
+++ b/schemas/race.xsd	Tue May 18 19:59:17 2010 +0000
@@ -202,6 +202,21 @@
 				</xs:sequence>
 			</xs:complexType>
 		</xs:element>
+		<xs:element name="unitCategories" minOccurs="0">
+			<xs:complexType>
+				<xs:sequence>
+					<xs:element name="unitCategory" maxOccurs="unbounded">
+						<xs:complexType>
+							<xs:sequence>
+								<xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
+							</xs:sequence>
+							<xs:attribute name="catID" type="xs:string" use="required"/>
+							<xs:anyAttribute processContents="lax"/>						
+						</xs:complexType>
+					</xs:element>
+				</xs:sequence>
+			</xs:complexType>
+		</xs:element>
 		<xs:element name="notes" type="xs:string" minOccurs="0" />
 		<xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
 	</xs:sequence>