Mercurial > repos > snowblizz-super-API-ideas
changeset 283:cd657faa0c05
* Merge branch into trunk (branch contained some fixes that should have been made on trunk)
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 20 Oct 2010 19:26:57 +0000 |
parents | cd082d7bd4d9 |
children | 7ecc6cb804e8 |
files | AssemblyInfo.cs IBBoard.WarFoundry.API.csproj api/Factories/Xml/WarFoundryXmlArmyParser.cs api/Factories/Xml/WarFoundryXmlElementName.cs api/Factories/Xml/WarFoundryXmlRaceFactory.cs api/Objects/Race.cs schemas/race.xsd |
diffstat | 7 files changed, 464 insertions(+), 400 deletions(-) [+] |
line wrap: on
line diff
--- a/AssemblyInfo.cs Sat Sep 04 10:26:46 2010 +0000 +++ b/AssemblyInfo.cs Wed Oct 20 19:26:57 2010 +0000 @@ -30,7 +30,7 @@ // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("0.0.1.*")] +[assembly: AssemblyVersion("0.1.0.*")] // // In order to sign your assembly you must specify a key to use. Refer to the
--- a/IBBoard.WarFoundry.API.csproj Sat Sep 04 10:26:46 2010 +0000 +++ b/IBBoard.WarFoundry.API.csproj Wed Oct 20 19:26:57 2010 +0000 @@ -1,15 +1,38 @@ -<?xml version="1.0" encoding="utf-8"?> -<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <ProductVersion>8.0.50727</ProductVersion> + <ProductVersion>9.0.30729</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{951E6C7A-7FBA-4F68-9D9E-F48618BB9626}</ProjectGuid> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>IBBoard.WarFoundry.API</RootNamespace> <AssemblyName>IBBoard.WarFoundry.API</AssemblyName> + <FileUpgradeFlags> + </FileUpgradeFlags> + <OldToolsVersion>2.0</OldToolsVersion> + <UpgradeBackupLocation> + </UpgradeBackupLocation> + <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> + <TargetFrameworkSubset> + </TargetFrameworkSubset> + <PublishUrl>publish\</PublishUrl> + <Install>true</Install> + <InstallFrom>Disk</InstallFrom> + <UpdateEnabled>false</UpdateEnabled> + <UpdateMode>Foreground</UpdateMode> + <UpdateInterval>7</UpdateInterval> + <UpdateIntervalUnits>Days</UpdateIntervalUnits> + <UpdatePeriodically>false</UpdatePeriodically> + <UpdateRequired>false</UpdateRequired> + <MapFileExtensions>true</MapFileExtensions> + <ApplicationRevision>0</ApplicationRevision> + <ApplicationVersion>1.0.0.%2a</ApplicationVersion> + <IsWebBootstrapper>false</IsWebBootstrapper> + <UseApplicationTrust>false</UseApplicationTrust> + <BootstrapperEnabled>true</BootstrapperEnabled> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -37,6 +60,7 @@ </Target> --> <ItemGroup> + <None Include="app.config" /> <None Include="COPYING" /> <Compile Include="api\Objects\ICostedWarFoundryObject.cs" /> <Compile Include="api\Commands\CreateAndAddUnitCommand.cs" /> @@ -162,12 +186,6 @@ <Content Include="libs\ICSharpCode.SharpZipLib.dll" /> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\IBBoard\IBBoard.csproj"> - <Project>{5DFD64F6-FC2B-4B4F-B92E-483BAC468105}</Project> - <Name>IBBoard</Name> - </ProjectReference> - </ItemGroup> - <ItemGroup> <Reference Include="System.Xml" /> <Reference Include="System" /> <Reference Include="ICSharpCode.SharpZipLib, Version=0.85.5.452, Culture=neutral, PublicKeyToken=1b03e6acf1164f73"> @@ -175,4 +193,42 @@ <HintPath>libs\ICSharpCode.SharpZipLib.dll</HintPath> </Reference> </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\IBBoard\IBBoard.csproj"> + <Project>{5DFD64F6-FC2B-4B4F-B92E-483BAC468105}</Project> + <Name>IBBoard</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <BootstrapperPackage Include="Microsoft.Net.Client.3.5"> + <Visible>False</Visible> + <ProductName>.NET Framework Client Profile</ProductName> + <Install>false</Install> + </BootstrapperPackage> + <BootstrapperPackage Include="Microsoft.Net.Framework.2.0"> + <Visible>False</Visible> + <ProductName>.NET Framework 2.0 %28x86%29</ProductName> + <Install>false</Install> + </BootstrapperPackage> + <BootstrapperPackage Include="Microsoft.Net.Framework.3.0"> + <Visible>False</Visible> + <ProductName>.NET Framework 3.0 %28x86%29</ProductName> + <Install>false</Install> + </BootstrapperPackage> + <BootstrapperPackage Include="Microsoft.Net.Framework.3.5"> + <Visible>False</Visible> + <ProductName>.NET Framework 3.5</ProductName> + <Install>false</Install> + </BootstrapperPackage> + <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1"> + <Visible>False</Visible> + <ProductName>.NET Framework 3.5 SP1</ProductName> + <Install>true</Install> + </BootstrapperPackage> + <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1"> + <Visible>False</Visible> + <ProductName>Windows Installer 3.1</ProductName> + <Install>true</Install> + </BootstrapperPackage> + </ItemGroup> </Project> \ No newline at end of file
--- a/api/Factories/Xml/WarFoundryXmlArmyParser.cs Sat Sep 04 10:26:46 2010 +0000 +++ b/api/Factories/Xml/WarFoundryXmlArmyParser.cs Wed Oct 20 19:26:57 2010 +0000 @@ -106,33 +106,24 @@ foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem")) { string equipID = elem.GetAttribute("id"); - bool isCustom = XmlTools.GetBoolValueFromAttribute(elem, "isCustomEquipment"); - - if (!isCustom) - { - UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID); - - if (item == null) - { - throw new RequiredDataMissingException(file.Name, "Equipment Item", equipID); - } + UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID); - double amount = XmlTools.GetDoubleValueFromAttribute(elem, "amount"); - string equipTypeString = elem.GetAttribute("amountType"); + if (item == null) + { + throw new RequiredDataMissingException(file.Name, "Equipment Item", equipID); + } - if (equipTypeString == "ratio") - { - unit.SetEquipmentRatio(item, amount); - } - else - { - //amount should be a whole number, so do type-cast rounding - unit.SetEquipmentAmount(item, (int) amount); - } + double amount = XmlTools.GetDoubleValueFromAttribute(elem, "amount"); + string equipTypeString = elem.GetAttribute("amountType"); + + if (equipTypeString == "ratio") + { + unit.SetEquipmentRatio(item, amount); } else { - //TODO: Load custom equipment + //amount should be a whole number, so do type-cast rounding + unit.SetEquipmentAmount(item, (int) amount); } } }
--- a/api/Factories/Xml/WarFoundryXmlElementName.cs Sat Sep 04 10:26:46 2010 +0000 +++ b/api/Factories/Xml/WarFoundryXmlElementName.cs Wed Oct 20 19:26:57 2010 +0000 @@ -1,66 +1,67 @@ -// This file (WarFoundryXmlElementName.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. -// -// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. - -using System; -using System.Collections.Generic; -using IBBoard.Xml; - -namespace IBBoard.WarFoundry.API.Factories.Xml -{ - /// <summary> - /// An enumeration class for valid WarFoundry XML elements, designed to imitate Java's extensible complex object enumerations. - /// </summary> - public class WarFoundryXmlElementName : IXmlElementName, IExtendedEnum<string> - { - public static WarFoundryXmlElementName SYSTEM_ELEMENT = new WarFoundryXmlElementName("SYSTEM_ELEMENT", "system"); - public static WarFoundryXmlElementName ARMY_ELEMENT = new WarFoundryXmlElementName("ARMY_ELEMENT", "army"); - public static WarFoundryXmlElementName RACE_ELEMENT = new WarFoundryXmlElementName("RACE_ELEMENT", "race"); - public static WarFoundryXmlElementName CATEGORIES_ELEMENT = new WarFoundryXmlElementName("CATEGORIES_ELEMENT", "categories"); - public static WarFoundryXmlElementName CATEGORY_ELEMENT = new WarFoundryXmlElementName("CATEGORY_ELEMENT", "cat"); - public static WarFoundryXmlElementName UNITTYPES_ELEMENT = new WarFoundryXmlElementName("UNITTYPES_ELEMENT", "units"); - public static WarFoundryXmlElementName UNITTYPE_ELEMENT = new WarFoundryXmlElementName("UNITTYPE_ELEMENT", "unit"); - public static WarFoundryXmlElementName RACE_EQUIPMENT_ITEMS_ELEMENT = new WarFoundryXmlElementName("RACE_EQUIPMENT_ITEMS_ELEMENT", "equipment"); - public static WarFoundryXmlElementName RACE_EQUIPMENT_ITEM_ELEMENT = new WarFoundryXmlElementName("RACE_EQUIPMENT_ITEMS_ELEMENT", "equipmentItem"); - - private static ICollection<WarFoundryXmlElementName> enumValues; - private string name; - private string val; - - private WarFoundryXmlElementName(string elemName, string elemVal) - { - name = elemName; - val = elemVal; - } - - public string Name - { - get { - return name; - } - } - - public string Value - { - get { - return val; - } - } - - /// <summary> - /// Gets an ICollection of the values so that they can be looped over like a standard enumeration. - /// </summary> - /// <returns> - /// A <see cref="ICollection`1"/> of all of the static 'enumeration' values of the class. - /// </returns> - public static ICollection<WarFoundryXmlElementName> GetEnumValues() - { - if (enumValues == null) - { - enumValues = new WarFoundryXmlElementName[]{SYSTEM_ELEMENT, ARMY_ELEMENT, RACE_ELEMENT, CATEGORIES_ELEMENT, CATEGORY_ELEMENT, UNITTYPES_ELEMENT, UNITTYPE_ELEMENT}; - } - - return enumValues; - } - } -} +// This file (WarFoundryXmlElementName.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; +using System.Collections.Generic; +using IBBoard.Xml; + +namespace IBBoard.WarFoundry.API.Factories.Xml +{ + /// <summary> + /// An enumeration class for valid WarFoundry XML elements, designed to imitate Java's extensible complex object enumerations. + /// </summary> + public class WarFoundryXmlElementName : IXmlElementName, IExtendedEnum<string> + { + public static WarFoundryXmlElementName SYSTEM_ELEMENT = new WarFoundryXmlElementName("SYSTEM_ELEMENT", "system"); + public static WarFoundryXmlElementName ARMY_ELEMENT = new WarFoundryXmlElementName("ARMY_ELEMENT", "army"); + public static WarFoundryXmlElementName RACE_ELEMENT = new WarFoundryXmlElementName("RACE_ELEMENT", "race"); + public static WarFoundryXmlElementName ARMY_DEFAULTNAME_ELEMENT = new WarFoundryXmlElementName("ARMY_DEFAULTNAME_ELEMENT", "defaultName"); + public static WarFoundryXmlElementName CATEGORIES_ELEMENT = new WarFoundryXmlElementName("CATEGORIES_ELEMENT", "categories"); + public static WarFoundryXmlElementName CATEGORY_ELEMENT = new WarFoundryXmlElementName("CATEGORY_ELEMENT", "cat"); + public static WarFoundryXmlElementName UNITTYPES_ELEMENT = new WarFoundryXmlElementName("UNITTYPES_ELEMENT", "units"); + public static WarFoundryXmlElementName UNITTYPE_ELEMENT = new WarFoundryXmlElementName("UNITTYPE_ELEMENT", "unit"); + public static WarFoundryXmlElementName RACE_EQUIPMENT_ITEMS_ELEMENT = new WarFoundryXmlElementName("RACE_EQUIPMENT_ITEMS_ELEMENT", "equipment"); + public static WarFoundryXmlElementName RACE_EQUIPMENT_ITEM_ELEMENT = new WarFoundryXmlElementName("RACE_EQUIPMENT_ITEMS_ELEMENT", "equipmentItem"); + + private static ICollection<WarFoundryXmlElementName> enumValues; + private string name; + private string val; + + private WarFoundryXmlElementName(string elemName, string elemVal) + { + name = elemName; + val = elemVal; + } + + public string Name + { + get { + return name; + } + } + + public string Value + { + get { + return val; + } + } + + /// <summary> + /// Gets an ICollection of the values so that they can be looped over like a standard enumeration. + /// </summary> + /// <returns> + /// A <see cref="ICollection`1"/> of all of the static 'enumeration' values of the class. + /// </returns> + public static ICollection<WarFoundryXmlElementName> GetEnumValues() + { + if (enumValues == null) + { + enumValues = new WarFoundryXmlElementName[]{SYSTEM_ELEMENT, ARMY_ELEMENT, RACE_ELEMENT, CATEGORIES_ELEMENT, CATEGORY_ELEMENT, UNITTYPES_ELEMENT, UNITTYPE_ELEMENT}; + } + + return enumValues; + } + } +}
--- a/api/Objects/Race.cs Sat Sep 04 10:26:46 2010 +0000 +++ b/api/Objects/Race.cs Wed Oct 20 19:26:57 2010 +0000 @@ -1,302 +1,317 @@ -// This file (Race.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard. -// -// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. - -using System; -using System.Collections.Generic; -using System.IO; -using System.Xml; -using IBBoard.IO; -using IBBoard.WarFoundry.API.Factories; - -namespace IBBoard.WarFoundry.API.Objects -{ - public class Race : WarFoundryStagedLoadingObject - { - public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault"; - - private string subID; - private GameSystem system; - private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat; - private Dictionary<string, UnitType> unitTypes = new Dictionary<string,UnitType>(); - private Dictionary<string, EquipmentItem> equipment = new Dictionary<string,EquipmentItem>(); - private Dictionary<string, Ability> abilities = new Dictionary<string,Ability>(); - private Dictionary<string, Category> categories = new Dictionary<string,Category>(); - private Dictionary<string, UnitMemberType> memberTypes = new Dictionary<string, UnitMemberType>(); - - public Race(string raceID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : this(raceID, "", raceName, gameSystem, creatingFactory) - { - } - - public Race(string raceID, string raceSubID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : base(raceID + (raceSubID!="" ? "_"+raceSubID : ""), raceName, creatingFactory) - { - subID = (raceSubID == null ? "" : raceSubID); - system = gameSystem; - } - - public string SubID - { - get { return subID; } - set { subID = (value == null ? "" : value.Trim()); } - } - - public GameSystem GameSystem - { - get { return system; } - set - { - if (value == null) - { - throw new ArgumentException("Game system for a race cannot be null"); - } - - system = value; - } - } - - public void AddCategory(Category cat) - { - categories[cat.ID] = cat; - } - - /// <summary> - /// Gets a category from its ID. Attempts to get the category from the race's overrides, or else it falls back to getting the Game System's category with that ID. - /// </summary> - /// <param name="id"> - /// The ID of the category to get - /// </param> - /// <returns> - /// The <code>Category</code> with the specified ID, or null if one doesn't exist. - /// </returns> - public Category GetCategory(string id) - { - EnsureFullyLoaded(); - Category cat = null; - categories.TryGetValue(id, out cat); - - if (cat == null) - { - cat = GameSystem.GetCategory(id); - } - - return cat; - } - - public Category[] Categories - { - get - { - EnsureFullyLoaded(); - Category[] cats; - - if (!HasCategoryOverrides()) - { - cats = GameSystem.Categories; - } - else - { - cats = DictionaryUtils.ToArray<string, Category>(categories); - } - - return cats; - } - } - - public bool HasCategoryOverrides() - { - EnsureFullyLoaded(); - return categories.Count > 0; - } - - public void AddEquipmentItem(EquipmentItem item) - { - //TODO: Throw DuplicateItemException - equipment.Add(item.ID, item); - } - - public EquipmentItem GetEquipmentItem(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(equipment, id); - } - - public List<EquipmentItem> GetEquipmentList() - { - EnsureFullyLoaded(); - List<EquipmentItem> items = new List<EquipmentItem>(); - - foreach (EquipmentItem item in equipment.Values) - { - items.Add(item); - } - - return items; - } - - public void AddUnitType(UnitType type) - { - CacheUnitType(type); - unitTypes.Add(type.ID, type); - } - - public UnitType[] GetUnitTypes(Category cat) - { - EnsureFullyLoaded(); - BuildUnitTypesByCategoryCache(); - Dictionary<string, UnitType> unitTypesDictionary; - unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); - UnitType[] unitTypesArray; - - if (unitTypesDictionary == null) - { - unitTypesArray = new UnitType[0]; - } - else - { - unitTypesArray = DictionaryUtils.ToArray<string, UnitType>(unitTypesDictionary); - } - - return unitTypesArray; - } - - private void CacheUnitType(UnitType unit) - { - BuildUnitTypesByCategoryCache(); - - foreach (Category cat in unit.Categories) - { - Dictionary<string, UnitType> catUnitTypes = DictionaryUtils.GetValue(unitTypesByCat, cat); - - if (catUnitTypes == null) - { - throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category ({2})", unit.ID, unit.Name, cat.ID)); - } - - catUnitTypes.Add(unit.ID, unit); - } - } - - private void BuildUnitTypesByCategoryCache() - { - if (unitTypesByCat == null) - { - DoBuildUnitTypesByCategoryCache(); - } - } - - private void DoBuildUnitTypesByCategoryCache() - { - unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>(); - - foreach (Category category in Categories) - { - unitTypesByCat.Add(category, new Dictionary<string, UnitType>()); - } - - foreach (UnitType unit in unitTypes.Values) - { - CacheUnitType(unit); - } - } - - public UnitType GetUnitType(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(unitTypes, id); - } - - public List<Ability> GetAbilityList() - { - EnsureFullyLoaded(); - List<Ability> items = new List<Ability>(); - items.AddRange(abilities.Values); - return items; - } - - public void AddAbility(Ability newAbility) - { - //TODO: Throw DuplicateItemException - abilities.Add(newAbility.ID, newAbility); - } - - [Obsolete("Use AddAbility method instead")] - public void SetAbilities(Dictionary<string, Ability> newAbilities) - { - foreach (Ability ability in newAbilities.Values) - { - AddAbility(ability); - } - } - - public Ability GetAbility(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(abilities, id); - } - - protected virtual Dictionary<string, UnitType> RaceUnitTypes - { - get { return RaceRawUnitTypes; } - set { RaceRawUnitTypes = value; } - } - - protected virtual Dictionary<string, EquipmentItem> RaceEquipment - { - get { return RaceRawEquipment; } - set { RaceRawEquipment = value; } - } - - protected virtual Dictionary<string, Ability> RaceAbilities - { - get { return RaceRawAbilities; } - set { RaceRawAbilities = value; } - } - - protected Dictionary<string, UnitType> RaceRawUnitTypes - { - get { return unitTypes; } - set { unitTypes = value; } - } - - protected Dictionary<string, EquipmentItem> RaceRawEquipment - { - get { return equipment; } - set { equipment = value; } - } - - protected Dictionary<string, Ability> RaceRawAbilities - { - get { return abilities; } - set { abilities = value; } - } - - public void AddUnitMemberType(UnitMemberType memberType) - { - memberTypes[memberType.ID] = memberType; - } - - /// <summary> - /// Gets a unit member type by its ID. - /// </summary> - /// <param name="id"> - /// The ID of the unit member type to get - /// </param> - /// <returns> - /// The <code>UnitMemberType</code> with the specified ID, or null if one doesn't exist. - /// </returns> - public UnitMemberType GetUnitMemberType(string id) - { - EnsureFullyLoaded(); - return DictionaryUtils.GetValue(memberTypes, id); - } - - public UnitMemberType[] UnitMemberTypes - { - get - { - EnsureFullyLoaded(); - return DictionaryUtils.ToArray(memberTypes); - } - } - } -} +// This file (Race.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard. +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Xml; +using IBBoard.IO; +using IBBoard.WarFoundry.API.Factories; + +namespace IBBoard.WarFoundry.API.Objects +{ + public class Race : WarFoundryStagedLoadingObject + { + public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault"; + + private string subID; + private GameSystem system; + private string defaultArmyName = ""; + private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat; + private Dictionary<string, UnitType> unitTypes = new Dictionary<string,UnitType>(); + private Dictionary<string, EquipmentItem> equipment = new Dictionary<string,EquipmentItem>(); + private Dictionary<string, Ability> abilities = new Dictionary<string,Ability>(); + private Dictionary<string, Category> categories = new Dictionary<string,Category>(); + private Dictionary<string, UnitMemberType> memberTypes = new Dictionary<string, UnitMemberType>(); + + public Race(string raceID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : this(raceID, "", raceName, gameSystem, creatingFactory) + { + } + + public Race(string raceID, string raceSubID, string raceName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) : base(raceID + (raceSubID != "" ? "_" + raceSubID : ""), raceName, creatingFactory) + { + subID = (raceSubID == null ? "" : raceSubID); + system = gameSystem; + } + + public string SubID + { + get { return subID; } + set { subID = (value == null ? "" : value.Trim()); } + } + + public GameSystem GameSystem + { + get { return system; } + set + { + if (value == null) + { + throw new ArgumentException("Game system for a race cannot be null"); + } + + system = value; + } + } + + public string ArmyDefaultName + { + get { return defaultArmyName; } + set + { + if (value == null) + { + throw new ArgumentException("No default army name"); + } + + defaultArmyName = value; + } + } + + public void AddCategory(Category cat) + { + categories[cat.ID] = cat; + } + + /// <summary> + /// Gets a category from its ID. Attempts to get the category from the race's overrides, or else it falls back to getting the Game System's category with that ID. + /// </summary> + /// <param name="id"> + /// The ID of the category to get + /// </param> + /// <returns> + /// The <code>Category</code> with the specified ID, or null if one doesn't exist. + /// </returns> + public Category GetCategory(string id) + { + EnsureFullyLoaded(); + Category cat = null; + categories.TryGetValue(id, out cat); + + if (cat == null) + { + cat = GameSystem.GetCategory(id); + } + + return cat; + } + + public Category[] Categories + { + get + { + EnsureFullyLoaded(); + Category[] cats; + + if (!HasCategoryOverrides()) + { + cats = GameSystem.Categories; + } + else + { + cats = DictionaryUtils.ToArray<string, Category>(categories); + } + + return cats; + } + } + + public bool HasCategoryOverrides() + { + EnsureFullyLoaded(); + return categories.Count > 0; + } + + public void AddEquipmentItem(EquipmentItem item) + { + //TODO: Throw DuplicateItemException + equipment.Add(item.ID, item); + } + + public EquipmentItem GetEquipmentItem(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(equipment, id); + } + + public List<EquipmentItem> GetEquipmentList() + { + EnsureFullyLoaded(); + List<EquipmentItem> items = new List<EquipmentItem>(); + + foreach (EquipmentItem item in equipment.Values) + { + items.Add(item); + } + + return items; + } + + public void AddUnitType(UnitType type) + { + CacheUnitType(type); + unitTypes.Add(type.ID, type); + } + + public UnitType[] GetUnitTypes(Category cat) + { + EnsureFullyLoaded(); + BuildUnitTypesByCategoryCache(); + Dictionary<string, UnitType> unitTypesDictionary; + unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); + UnitType[] unitTypesArray; + + if (unitTypesDictionary == null) + { + unitTypesArray = new UnitType[0]; + } + else + { + unitTypesArray = DictionaryUtils.ToArray<string, UnitType>(unitTypesDictionary); + } + + return unitTypesArray; + } + + private void CacheUnitType(UnitType unit) + { + BuildUnitTypesByCategoryCache(); + + foreach (Category cat in unit.Categories) + { + Dictionary<string, UnitType> catUnitTypes = DictionaryUtils.GetValue(unitTypesByCat, cat); + + if (catUnitTypes == null) + { + throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category ({2})", unit.ID, unit.Name, cat.ID)); + } + + catUnitTypes.Add(unit.ID, unit); + } + } + + private void BuildUnitTypesByCategoryCache() + { + if (unitTypesByCat == null) + { + DoBuildUnitTypesByCategoryCache(); + } + } + + private void DoBuildUnitTypesByCategoryCache() + { + unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>(); + + foreach (Category category in Categories) + { + unitTypesByCat.Add(category, new Dictionary<string, UnitType>()); + } + + foreach (UnitType unit in unitTypes.Values) + { + CacheUnitType(unit); + } + } + + public UnitType GetUnitType(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(unitTypes, id); + } + + public List<Ability> GetAbilityList() + { + EnsureFullyLoaded(); + List<Ability> items = new List<Ability>(); + items.AddRange(abilities.Values); + return items; + } + + public void AddAbility(Ability newAbility) + { + //TODO: Throw DuplicateItemException + abilities.Add(newAbility.ID, newAbility); + } + + [Obsolete("Use AddAbility method instead")] + public void SetAbilities(Dictionary<string, Ability> newAbilities) + { + foreach (Ability ability in newAbilities.Values) + { + AddAbility(ability); + } + } + + public Ability GetAbility(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(abilities, id); + } + + protected virtual Dictionary<string, UnitType> RaceUnitTypes + { + get { return RaceRawUnitTypes; } + set { RaceRawUnitTypes = value; } + } + + protected virtual Dictionary<string, EquipmentItem> RaceEquipment + { + get { return RaceRawEquipment; } + set { RaceRawEquipment = value; } + } + + protected virtual Dictionary<string, Ability> RaceAbilities + { + get { return RaceRawAbilities; } + set { RaceRawAbilities = value; } + } + + protected Dictionary<string, UnitType> RaceRawUnitTypes + { + get { return unitTypes; } + set { unitTypes = value; } + } + + protected Dictionary<string, EquipmentItem> RaceRawEquipment + { + get { return equipment; } + set { equipment = value; } + } + + protected Dictionary<string, Ability> RaceRawAbilities + { + get { return abilities; } + set { abilities = value; } + } + + public void AddUnitMemberType(UnitMemberType memberType) + { + memberTypes[memberType.ID] = memberType; + } + + /// <summary> + /// Gets a unit member type by its ID. + /// </summary> + /// <param name="id"> + /// The ID of the unit member type to get + /// </param> + /// <returns> + /// The <code>UnitMemberType</code> with the specified ID, or null if one doesn't exist. + /// </returns> + public UnitMemberType GetUnitMemberType(string id) + { + EnsureFullyLoaded(); + return DictionaryUtils.GetValue(memberTypes, id); + } + + public UnitMemberType[] UnitMemberTypes + { + get + { + EnsureFullyLoaded(); + return DictionaryUtils.ToArray(memberTypes); + } + } + } +}
--- a/schemas/race.xsd Sat Sep 04 10:26:46 2010 +0000 +++ b/schemas/race.xsd Wed Oct 20 19:26:57 2010 +0000 @@ -67,6 +67,7 @@ <xs:attribute name="id" type="xs:string" use="required" /> <xs:attribute name="subid" type="xs:string" default=""/> <xs:attribute name="name" type="xs:string" use="required"/> + <xs:attribute name="defaultArmyName" type="xs:string" default="" /> <xs:attribute name="system" type="xs:string" use="required"/> <xs:anyAttribute processContents="lax"/> </xs:complexType>