Mercurial > repos > snowblizz-super-API-ideas
changeset 383:8981fc45fe17 default-army-name
Re #153: Default name for armies
Hopefully API code added as well
author | snowblizz |
---|---|
date | Tue, 07 Sep 2010 11:53:22 +0000 |
parents | c035afa4a42c |
children | b21a85c079f5 |
files | IBBoard.WarFoundry.API.csproj api/AbstractWarFoundryLoader.cs api/Factories/Xml/WarFoundryXmlElementName.cs api/Factories/Xml/WarFoundryXmlRaceFactory.cs api/Objects/Race.cs schemas/race.xsd |
diffstat | 6 files changed, 1090 insertions(+), 1061 deletions(-) [+] |
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.csproj Sat Jul 31 15:26:53 2010 +0000 +++ b/IBBoard.WarFoundry.API.csproj Tue Sep 07 11:53:22 2010 +0000 @@ -1,15 +1,21 @@ -<?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> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <UpgradeBackupLocation> + </UpgradeBackupLocation> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -162,12 +168,10 @@ <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="IBBoard, Version=1.0.3896.41664, Culture=neutral, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\IBBoard\bin\Release\IBBoard.dll</HintPath> + </Reference> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> <Reference Include="System" />
--- a/api/AbstractWarFoundryLoader.cs Sat Jul 31 15:26:53 2010 +0000 +++ b/api/AbstractWarFoundryLoader.cs Tue Sep 07 11:53:22 2010 +0000 @@ -1,685 +1,685 @@ -// This file (AbstractWarFoundryLoader.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 System.IO; -using IBBoard.Collections; -using IBBoard.IO; -using IBBoard.Logging; -using IBBoard.WarFoundry.API.Factories; -using IBBoard.WarFoundry.API.Objects; - -namespace IBBoard.WarFoundry.API -{ - /// <summary> - /// The base abstract implementation of a WarFoundry file loader - /// </summary> - public abstract class AbstractWarFoundryLoader - { - private ICollection<DirectoryInfo> directories; - private ICollection<INativeWarFoundryFactory> factories; - private ICollection<INonNativeWarFoundryFactory> nonNativeFactories; - private Dictionary<IWarFoundryFactory, SimpleSet<IWarFoundryObject>> loadedObjects; - public delegate void FileLoadingCompleteDelegate(List<FileLoadFailure> failures); - public event MethodInvoker FileLoadingStarted; - public event FileLoadingCompleteDelegate FileLoadingFinished; - - protected AbstractWarFoundryLoader() - { - directories = new List<DirectoryInfo>(); - factories = new List<INativeWarFoundryFactory>(); - nonNativeFactories = new List<INonNativeWarFoundryFactory>(); - loadedObjects = new Dictionary<IWarFoundryFactory,SimpleSet<IWarFoundryObject>>(); - } - - /// <summary> - /// Adds a directory to the collection of directories that will be searched for WarFoundry data files. - /// </summary> - /// <param name="directory"> - /// The <see cref="DirectoryInfo"/> to add to the list for searching for data files - /// </param> - public void AddLoadDirectory(DirectoryInfo directory) - { - if (!directories.Contains(directory)) - { - directories.Add(directory); - } - } - - /// <summary> - /// Removes a directory from the collection of directories that will be searched for WarFoundry data files. - /// </summary> - /// <param name="directory"> - /// A <see cref="DirectoryInfo"/> - /// </param> - public void RemoveLoadDirectory(DirectoryInfo directory) - { - if (directories.Contains(directory)) - { - directories.Remove(directory); - } - } - - /// <summary> - /// Registers a <see cref="INativeWarFoundryFactory"/> as a factory that can parse native data files. - /// </summary> - /// <param name="factory"> - /// The <see cref="INativeWarFoundryFactory"/> to register to parse native data files. - /// </param> - public void RegisterFactory(INativeWarFoundryFactory factory) - { - if (!factories.Contains(factory)) - { - factories.Add(factory); - } - } - - /// <summary> - /// Unregisters a <see cref="INativeWarFoundryFactory"/> so that it will no longer be used to try to parse native data files. - /// </summary> - /// <param name="factory"> - /// The <see cref="INativeWarFoundryFactory"/> to remove from the collection of factories that are used to try to parse native data files. - /// </param> - public void UnregisterFactory(INativeWarFoundryFactory factory) - { - if (factories.Contains(factory)) - { - factories.Remove(factory); - } - } - - /// <summary> - /// Registers a <see cref="INonNativeWarFoundryFactory"/> so that it will be used to try to parse non-native data files from other applications. - /// </summary> - /// <param name="factory"> - /// The <see cref="INonNativeWarFoundryFactory"/> to register to parse non-native data files. - /// </param> - public void RegisterNonNativeFactory(INonNativeWarFoundryFactory factory) - { - if (!nonNativeFactories.Contains(factory)) - { - nonNativeFactories.Add(factory); - } - } - - /// <summary> - /// Unregisters a <see cref="INonNativeWarFoundryFactory"/> so that it will no longer be used to try to parse non-native data files from other applications. - /// </summary> - /// <param name="factory"> - /// The <see cref="INonNativeWarFoundryFactory"/> to remove from the collection of factories that are used to try to parse non-native data files. - /// </param> - public void UnregisterNonNativeFactory(INonNativeWarFoundryFactory factory) - { - if (nonNativeFactories.Contains(factory)) - { - nonNativeFactories.Remove(factory); - } - } - - /// <summary> - /// Loads all of the data files in the registered directories. - /// </summary> - /// <returns> - /// A <see cref="List"/> of <see cref="FileLoadFailure"/> for files that failed to load - /// </returns> - public List<FileLoadFailure> LoadFiles() - { - PrepareForFileLoad(); - Dictionary<FileInfo, IWarFoundryFactory> loadableRaces = new Dictionary<FileInfo, IWarFoundryFactory>(); - Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems = new Dictionary<FileInfo, IWarFoundryFactory>(); - List<FileLoadFailure> failedLoads = FillLoadableFiles(loadableRaces, loadableGameSystems); - failedLoads.AddRange(LoadGameSystems(loadableGameSystems)); - failedLoads.AddRange(LoadRaces(loadableRaces)); - OnFileLoadingFinished(failedLoads); - return failedLoads; - } - - private void OnFileLoadingFinished(List<FileLoadFailure> failures) - { - if (FileLoadingFinished!=null) - { - FileLoadingFinished(failures); - } - } - - protected abstract void PrepareForFileLoad(); - - private List<FileLoadFailure> FillLoadableFiles(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems) - { - List<FileLoadFailure> fails = new List<FileLoadFailure>(); - - foreach (DirectoryInfo directory in directories) +// This file (AbstractWarFoundryLoader.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 System.IO; +using IBBoard.Collections; +using IBBoard.IO; +using IBBoard.Logging; +using IBBoard.WarFoundry.API.Factories; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API +{ + /// <summary> + /// The base abstract implementation of a WarFoundry file loader + /// </summary> + public abstract class AbstractWarFoundryLoader + { + private ICollection<DirectoryInfo> directories; + private ICollection<INativeWarFoundryFactory> factories; + private ICollection<INonNativeWarFoundryFactory> nonNativeFactories; + private Dictionary<IWarFoundryFactory, SimpleSet<IWarFoundryObject>> loadedObjects; + public delegate void FileLoadingCompleteDelegate(List<FileLoadFailure> failures); + public event MethodInvoker FileLoadingStarted; + public event FileLoadingCompleteDelegate FileLoadingFinished; + + protected AbstractWarFoundryLoader() + { + directories = new List<DirectoryInfo>(); + factories = new List<INativeWarFoundryFactory>(); + nonNativeFactories = new List<INonNativeWarFoundryFactory>(); + loadedObjects = new Dictionary<IWarFoundryFactory,SimpleSet<IWarFoundryObject>>(); + } + + /// <summary> + /// Adds a directory to the collection of directories that will be searched for WarFoundry data files. + /// </summary> + /// <param name="directory"> + /// The <see cref="DirectoryInfo"/> to add to the list for searching for data files + /// </param> + public void AddLoadDirectory(DirectoryInfo directory) + { + if (!directories.Contains(directory)) + { + directories.Add(directory); + } + } + + /// <summary> + /// Removes a directory from the collection of directories that will be searched for WarFoundry data files. + /// </summary> + /// <param name="directory"> + /// A <see cref="DirectoryInfo"/> + /// </param> + public void RemoveLoadDirectory(DirectoryInfo directory) + { + if (directories.Contains(directory)) + { + directories.Remove(directory); + } + } + + /// <summary> + /// Registers a <see cref="INativeWarFoundryFactory"/> as a factory that can parse native data files. + /// </summary> + /// <param name="factory"> + /// The <see cref="INativeWarFoundryFactory"/> to register to parse native data files. + /// </param> + public void RegisterFactory(INativeWarFoundryFactory factory) + { + if (!factories.Contains(factory)) + { + factories.Add(factory); + } + } + + /// <summary> + /// Unregisters a <see cref="INativeWarFoundryFactory"/> so that it will no longer be used to try to parse native data files. + /// </summary> + /// <param name="factory"> + /// The <see cref="INativeWarFoundryFactory"/> to remove from the collection of factories that are used to try to parse native data files. + /// </param> + public void UnregisterFactory(INativeWarFoundryFactory factory) + { + if (factories.Contains(factory)) + { + factories.Remove(factory); + } + } + + /// <summary> + /// Registers a <see cref="INonNativeWarFoundryFactory"/> so that it will be used to try to parse non-native data files from other applications. + /// </summary> + /// <param name="factory"> + /// The <see cref="INonNativeWarFoundryFactory"/> to register to parse non-native data files. + /// </param> + public void RegisterNonNativeFactory(INonNativeWarFoundryFactory factory) + { + if (!nonNativeFactories.Contains(factory)) + { + nonNativeFactories.Add(factory); + } + } + + /// <summary> + /// Unregisters a <see cref="INonNativeWarFoundryFactory"/> so that it will no longer be used to try to parse non-native data files from other applications. + /// </summary> + /// <param name="factory"> + /// The <see cref="INonNativeWarFoundryFactory"/> to remove from the collection of factories that are used to try to parse non-native data files. + /// </param> + public void UnregisterNonNativeFactory(INonNativeWarFoundryFactory factory) + { + if (nonNativeFactories.Contains(factory)) + { + nonNativeFactories.Remove(factory); + } + } + + /// <summary> + /// Loads all of the data files in the registered directories. + /// </summary> + /// <returns> + /// A <see cref="List"/> of <see cref="FileLoadFailure"/> for files that failed to load + /// </returns> + public List<FileLoadFailure> LoadFiles() + { + PrepareForFileLoad(); + Dictionary<FileInfo, IWarFoundryFactory> loadableRaces = new Dictionary<FileInfo, IWarFoundryFactory>(); + Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems = new Dictionary<FileInfo, IWarFoundryFactory>(); + List<FileLoadFailure> failedLoads = FillLoadableFiles(loadableRaces, loadableGameSystems); + failedLoads.AddRange(LoadGameSystems(loadableGameSystems)); + failedLoads.AddRange(LoadRaces(loadableRaces)); + OnFileLoadingFinished(failedLoads); + return failedLoads; + } + + private void OnFileLoadingFinished(List<FileLoadFailure> failures) + { + if (FileLoadingFinished!=null) + { + FileLoadingFinished(failures); + } + } + + protected abstract void PrepareForFileLoad(); + + private List<FileLoadFailure> FillLoadableFiles(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems) + { + List<FileLoadFailure> fails = new List<FileLoadFailure>(); + + foreach (DirectoryInfo directory in directories) + { + directory.Refresh(); + + if (directory.Exists) + { + List<FileLoadFailure> directoryFails = FillLoadableFilesForDirectory(loadableRaces, loadableGameSystems, directory); + fails.AddRange(directoryFails); + } + else + { + LogNotifier.WarnFormat(GetType(), "Load for {0} failed because directory didn't exist", directory.FullName); + } + } + + return fails; + } + + private List<FileLoadFailure> FillLoadableFilesForDirectory(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems, DirectoryInfo directory) + { + List<FileLoadFailure> fails = new List<FileLoadFailure>(); + LogNotifier.Debug(GetType(), "Load from "+directory.FullName); + + foreach (FileInfo file in directory.GetFiles()) + { + IWarFoundryFactory factory = GetGameSystemLoadingFactoryForFile(file); + + if (factory != null) + { + loadableGameSystems.Add(file, factory); + } + else + { + factory = GetRaceLoadingFactoryForFile(file); + + if (factory!=null) + { + loadableRaces.Add(file, factory); + } + else + { + FileLoadFailure failure = new FileLoadFailure(file, "File not handled as a Race or Game System definition: {0}", "FileNotHandled"); + fails.Add(failure); + LogNotifier.Info(GetType(), failure.Message); + } + } + } + + return fails; + } + + private IWarFoundryFactory GetGameSystemLoadingFactoryForFile(FileInfo file) + { + IWarFoundryFactory loadingFactory = null; + + foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) + { + if (factory.CanHandleFileAsGameSystem(file)) + { + loadingFactory = factory; + break; + } + } + + if (loadingFactory == null) + { + foreach (INativeWarFoundryFactory factory in factories) + { + if (factory.CanHandleFileAsGameSystem(file)) + { + loadingFactory = factory; + break; + } + } + } + + return loadingFactory; + } + + private IWarFoundryFactory GetRaceLoadingFactoryForFile(FileInfo file) + { + IWarFoundryFactory loadingFactory = null; + + foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) + { + if (factory.CanHandleFileAsRace(file)) + { + loadingFactory = factory; + break; + } + } + + if (loadingFactory == null) + { + foreach (INativeWarFoundryFactory factory in factories) + { + if (factory.CanHandleFileAsRace(file)) + { + loadingFactory = factory; + break; + } + } + } + + return loadingFactory; + } + + private List<FileLoadFailure> LoadGameSystems(Dictionary<FileInfo, IWarFoundryFactory> gameSystemFiles) + { + List<FileLoadFailure> fails = new List<FileLoadFailure>(); + + + foreach (FileInfo file in gameSystemFiles.Keys) + { + FileLoadFailure failure = null; + + try + { + bool loaded = LoadObject(file, gameSystemFiles[file]); + + if (!loaded) + { + failure = new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as GameSystem using {1}"); + } + } + catch (Exception ex) + { + failure = new FileLoadFailure(file, null, ex.Message, null, ex); + } + + if (failure!=null) + { + fails.Add(failure); + LogNotifier.Warn(GetType(), failure.Message, failure.Exception); + } + } + + return fails; + } + + private List<FileLoadFailure> LoadRaces(Dictionary<FileInfo, IWarFoundryFactory> raceFiles) + { + List<FileLoadFailure> fails = new List<FileLoadFailure>(); + + foreach (FileInfo file in raceFiles.Keys) + { + FileLoadFailure failure = null; + + try + { + bool loaded = LoadObject(file, raceFiles[file]); + + if (!loaded) + { + failure = new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}"); + } + } + catch (Exception ex) + { + failure = new FileLoadFailure(file, null, ex.Message, null, ex); + } + + if (failure!=null) + { + fails.Add(failure); + LogNotifier.Warn(GetType(), failure.Message, failure.Exception); + } + } + + return fails; + } + + private bool LoadObject(FileInfo file, IWarFoundryFactory factory) + { + bool loaded = false; + + LogNotifier.DebugFormat(GetType(), "Loading {0} using {1}", file.FullName, factory.GetType().Name); + ICollection<IWarFoundryObject> objects = factory.CreateObjectsFromFile(file); + + if (objects.Count > 0) { - directory.Refresh(); - - if (directory.Exists) - { - List<FileLoadFailure> directoryFails = FillLoadableFilesForDirectory(loadableRaces, loadableGameSystems, directory); - fails.AddRange(directoryFails); - } - else - { - LogNotifier.WarnFormat(GetType(), "Load for {0} failed because directory didn't exist", directory.FullName); - } - } - - return fails; - } - - private List<FileLoadFailure> FillLoadableFilesForDirectory(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems, DirectoryInfo directory) - { - List<FileLoadFailure> fails = new List<FileLoadFailure>(); - LogNotifier.Debug(GetType(), "Load from "+directory.FullName); - - foreach (FileInfo file in directory.GetFiles()) - { - IWarFoundryFactory factory = GetGameSystemLoadingFactoryForFile(file); - - if (factory != null) - { - loadableGameSystems.Add(file, factory); - } - else - { - factory = GetRaceLoadingFactoryForFile(file); - - if (factory!=null) - { - loadableRaces.Add(file, factory); - } - else - { - FileLoadFailure failure = new FileLoadFailure(file, "File not handled as a Race or Game System definition: {0}", "FileNotHandled"); - fails.Add(failure); - LogNotifier.Info(GetType(), failure.Message); - } - } - } - - return fails; - } - - private IWarFoundryFactory GetGameSystemLoadingFactoryForFile(FileInfo file) - { - IWarFoundryFactory loadingFactory = null; - - foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) - { - if (factory.CanHandleFileAsGameSystem(file)) - { - loadingFactory = factory; - break; - } - } - - if (loadingFactory == null) - { - foreach (INativeWarFoundryFactory factory in factories) - { - if (factory.CanHandleFileAsGameSystem(file)) - { - loadingFactory = factory; - break; - } - } - } - - return loadingFactory; - } - - private IWarFoundryFactory GetRaceLoadingFactoryForFile(FileInfo file) - { - IWarFoundryFactory loadingFactory = null; - - foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) - { - if (factory.CanHandleFileAsRace(file)) - { - loadingFactory = factory; - break; - } - } - - if (loadingFactory == null) - { - foreach (INativeWarFoundryFactory factory in factories) - { - if (factory.CanHandleFileAsRace(file)) - { - loadingFactory = factory; - break; - } - } - } - - return loadingFactory; - } - - private List<FileLoadFailure> LoadGameSystems(Dictionary<FileInfo, IWarFoundryFactory> gameSystemFiles) - { - List<FileLoadFailure> fails = new List<FileLoadFailure>(); - - - foreach (FileInfo file in gameSystemFiles.Keys) - { - FileLoadFailure failure = null; - - try - { - bool loaded = LoadObject(file, gameSystemFiles[file]); - - if (!loaded) - { - failure = new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as GameSystem using {1}"); - } - } - catch (Exception ex) - { - failure = new FileLoadFailure(file, null, ex.Message, null, ex); - } - - if (failure!=null) - { - fails.Add(failure); - LogNotifier.Warn(GetType(), failure.Message, failure.Exception); - } - } - - return fails; - } - - private List<FileLoadFailure> LoadRaces(Dictionary<FileInfo, IWarFoundryFactory> raceFiles) - { - List<FileLoadFailure> fails = new List<FileLoadFailure>(); - - foreach (FileInfo file in raceFiles.Keys) - { - FileLoadFailure failure = null; - - try - { - bool loaded = LoadObject(file, raceFiles[file]); - - if (!loaded) - { - failure = new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}"); - } - } - catch (Exception ex) - { - failure = new FileLoadFailure(file, null, ex.Message, null, ex); - } - - if (failure!=null) - { - fails.Add(failure); - LogNotifier.Warn(GetType(), failure.Message, failure.Exception); - } - } - - return fails; - } - - private bool LoadObject(FileInfo file, IWarFoundryFactory factory) - { - bool loaded = false; - - LogNotifier.DebugFormat(GetType(), "Loading {0} using {1}", file.FullName, factory.GetType().Name); - ICollection<IWarFoundryObject> objects = factory.CreateObjectsFromFile(file); - - if (objects.Count > 0) - { - AddLoadedObjects(objects, factory); - loaded = true; - } - - return loaded; - } - - - /// <summary> - /// Loads a single file through the registered WarFoundryFactories, if a factory exists that supports the file format. - /// </summary> - /// <param name="file"> - /// A <see cref="FileInfo"/> for the file to attempt to load - /// </param> - /// <returns> - /// An ICollection of IWarFoundryObjects loaded from <code>file</code> - /// </returns> - public ICollection<IWarFoundryObject> LoadFile(FileInfo file) - { - ICollection<IWarFoundryObject> objs = null; - IWarFoundryFactory loadFactory = null; - - try - { - objs = LoadFileWithNonNativeFactories(file, out loadFactory); - - if (objs == null) - { - objs = LoadFileWithNativeFactories(file, out loadFactory); - } - } - catch (InvalidFileException ex) - { - LogNotifier.Error(GetType(), file.FullName+" failed to load", ex); - } - - if (objs!=null) - { - AddLoadedObjects(objs, loadFactory); - } - else - { - objs = new List<IWarFoundryObject>(); - } - - return objs; - } - - private ICollection<IWarFoundryObject> LoadFileWithNonNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) - { - ICollection<IWarFoundryObject> objs = null; - loadFactory = null; - - if (nonNativeFactories.Count > 0) - { - LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as a non-native file"); - - foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) - { - bool canLoad = factory.CanHandleFileFormat(file); - LogNotifier.Debug(GetType(), "Load using "+factory.GetType().FullName+"? " + (canLoad ? "yes" : "no")); - - if (canLoad) - { - objs = factory.CreateObjectsFromFile(file); - - if (objs!=null) - { - loadFactory = factory; - break; - } - } - } - } - - return objs; - } - - private ICollection<IWarFoundryObject> LoadFileWithNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) - { - ICollection<IWarFoundryObject> objs = null; - loadFactory = null; - - if (factories.Count > 0) - { - LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as native file"); - - foreach (INativeWarFoundryFactory factory in factories) - { - if (factory.CanHandleFileFormat(file)) - { - objs = factory.CreateObjectsFromFile(file); - - if (objs!=null) - { - loadFactory = factory; - break; - } - } - } - } - - return objs; - } - - private void AddLoadedObjects(ICollection<IWarFoundryObject> loadedObjs, IWarFoundryFactory factory) - { - SimpleSet<IWarFoundryObject> objs; - loadedObjects.TryGetValue(factory, out objs); - - if (objs == null) - { - objs = new SimpleSet<IWarFoundryObject>(); - loadedObjects.Add(factory, objs); - } - - objs.AddRange(loadedObjs); - StoreObjects(loadedObjs); - } - - private void StoreObjects(ICollection<IWarFoundryObject> loadedObjects) - { - foreach (IWarFoundryObject loadedObject in loadedObjects) - { - if (loadedObject is GameSystem) - { - StoreGameSystem((GameSystem)loadedObject); - } - else if (loadedObject is Race) - { - StoreRace((Race)loadedObject); - } - } - } - - protected void StoreGameSystem(GameSystem system) - { - GameSystem existingSystem = GetExistingSystemForSystem(system); - - if (existingSystem!=null) - { - if (!system.Equals(existingSystem)) - { - //TODO: Raise an event to say we got a different duplicate - //We can't just fail, because failing is for completely unhandled files, not for objects in a file - } - } - else - { - DoStoreGameSystem(system); - } - } - - /// <summary> - /// Gets a game system that has already been loaded that duplicates the supplied game system's ID, if one exists. - /// </summary> - /// <param name="system"> - /// The <see cref="GameSystem"/> to find pre-existing duplicates of - /// </param> - /// <returns> - /// <code>null</code> if no existing duplicate exists, else the duplicate <see cref="GameSystem"/> - /// </returns> - protected abstract GameSystem GetExistingSystemForSystem(GameSystem system); - - /// <summary> - /// Stores a GameSystem in the loader's relevant storage structure - /// </summary> - /// <param name="system"> - /// The loaded <see cref="GameSystem"/> to store - /// </param> - protected abstract void DoStoreGameSystem(GameSystem system); - - protected void StoreRace(Race race) - { - if (race.GameSystem == null) - { - throw new InvalidOperationException("Race cannot have null game system. Game system should be loaded before race."); - } - - DoStoreRace(race); - } - - /// <summary> - /// Performs the implementation specific storage of a race - /// </summary> - /// <param name="race"> - /// The <see cref="Race"/> to store - /// </param> - protected abstract void DoStoreRace(Race race); - - /// <summary> - /// Gets all <see cref="GameSystem"/>s that are currently available, determined by those that can be loaded with the current <see cref="IWarFoundryFactory"/>s. - /// </summary> - /// <returns> - /// An array of <see cref="GameSystem"/>s that are currently available. - /// </returns> - public abstract GameSystem[] GetGameSystems(); - - /// <summary> - /// Gets a single <see cref="GameSystem"/> with a given ID. - /// </summary> - /// <param name="systemID"> - /// The ID of the <see cref="GameSystem"/> to get, as a <see cref="System.String"/>. - /// </param> - /// <returns> - /// The <see cref="GameSystem"/> with the given ID, or <code>null</code> if one doesn't exist. - /// </returns> - public abstract GameSystem GetGameSystem(string systemID); - - /// <summary> - /// Removes a loaded <see cref="GameSystem"/>. Used when a GameSystem fails to complete loading - /// </summary> - /// <param name="system">The GameSystem to remove</param> - protected internal abstract void RemoveGameSystem(GameSystem system); - - /// <summary> - /// Gets an array of the races for the specified <see cref="GameSystem"/>. - /// </summary> - /// <param name="system"> - /// The <see cref="GameSystem"/> to get the available races for. - /// </param> - /// <returns> - /// An array of <see cref="Race"/>s for the <see cref="GameSystem"/> - /// </returns> - public abstract Race[] GetRaces(GameSystem system); - - /// <summary> - /// Gets a single race for a given <see cref="GameSystem"/> by ID of the race. - /// </summary> - /// <param name="system"> - /// The <see cref="GameSystem"/> that the race is part of. - /// </param> - /// <param name="raceID"> - /// A <see cref="System.String"/> ID for the race to load. - /// </param> - /// <returns> - /// A <see cref="Race"/> with the specified ID from the <see cref="GameSystem"/>, or <code>null</code> if one doesn't exist. - /// </returns> - public abstract Race GetRace(GameSystem system, string raceID); - - /// <summary> - /// Gets a single race for a given <see cref="GameSystem"/> by the race's ID and sub-race ID. - /// </summary> - /// <param name="system"> - /// The <see cref="GameSystem"/> that the race is part of. - /// </param> - /// <param name="raceID"> - /// The <see cref="System.String"/> ID for the race to load. - /// </param> - /// <param name="raceSubID"> - /// A <see cref="System.String"/> - /// </param> - /// <returns> - /// A <see cref="Race"/> - /// </returns> - public abstract Race GetRace(GameSystem system, string raceID, string raceSubID); - - protected internal abstract void RemoveRace(Race race); - - /// <summary> - /// Gets the IDs of all of the game systems currently available. - /// </summary> - /// <returns> - /// An array of <see cref="System.String"/>s representing the IDs of the game systems. - /// </returns> - public virtual string[] GetGameSystemIDs() - { - GameSystem[] systems = GetGameSystems(); - return GetWarFoundryObjectIDs(systems); - } - - protected string[] GetWarFoundryObjectIDs(WarFoundryObject[] objs) - { - int objCount = objs.Length; - string[] keys = new string[objCount]; - - for (int i = 0; i < objCount; i++) - { - keys[i] = objs[i].ID; - } - - return keys; - } - - /// <summary> - /// Gets the IDs of all of the races of a specified game system. - /// </summary> - /// <param name="system"> - /// The <see cref="GameSystem"/> to get the available races for. - /// </param> - /// <returns> - /// An array of <see cref="System.String"/>s representing the IDs of the races of the specified game system. - /// </returns> - public virtual string[] GetSystemRaceIDs(GameSystem system) - { - Race[] races = GetRaces(system); - return GetWarFoundryObjectIDs(races); - } - - public Army LoadArmy(FileInfo file) - { - IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file); - Army loadedArmy = null; - - if (factory != null) - { - ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(file); - - if (objs.Count == 1) - { - foreach (IWarFoundryObject systemCount in objs) - { - if (systemCount is Army) - { - loadedArmy = (Army) systemCount; - } - } - } - } - - return loadedArmy; - } - - private IWarFoundryFactory GetArmyLoadingFactoryForFile(FileInfo file) - { - IWarFoundryFactory loadingFactory = null; - - foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) - { - if (factory.CanHandleFileAsArmy(file)) - { - loadingFactory = factory; - break; - } - } - - if (loadingFactory == null) - { - foreach (INativeWarFoundryFactory factory in factories) - { - if (factory.CanHandleFileAsArmy(file)) - { - loadingFactory = factory; - break; - } - } - } - - return loadingFactory; - } - } -} + AddLoadedObjects(objects, factory); + loaded = true; + } + + return loaded; + } + + + /// <summary> + /// Loads a single file through the registered WarFoundryFactories, if a factory exists that supports the file format. + /// </summary> + /// <param name="file"> + /// A <see cref="FileInfo"/> for the file to attempt to load + /// </param> + /// <returns> + /// An ICollection of IWarFoundryObjects loaded from <code>file</code> + /// </returns> + public ICollection<IWarFoundryObject> LoadFile(FileInfo file) + { + ICollection<IWarFoundryObject> objs = null; + IWarFoundryFactory loadFactory = null; + + try + { + objs = LoadFileWithNonNativeFactories(file, out loadFactory); + + if (objs == null) + { + objs = LoadFileWithNativeFactories(file, out loadFactory); + } + } + catch (InvalidFileException ex) + { + LogNotifier.Error(GetType(), file.FullName+" failed to load", ex); + } + + if (objs!=null) + { + AddLoadedObjects(objs, loadFactory); + } + else + { + objs = new List<IWarFoundryObject>(); + } + + return objs; + } + + private ICollection<IWarFoundryObject> LoadFileWithNonNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) + { + ICollection<IWarFoundryObject> objs = null; + loadFactory = null; + + if (nonNativeFactories.Count > 0) + { + LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as a non-native file"); + + foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) + { + bool canLoad = factory.CanHandleFileFormat(file); + LogNotifier.Debug(GetType(), "Load using "+factory.GetType().FullName+"? " + (canLoad ? "yes" : "no")); + + if (canLoad) + { + objs = factory.CreateObjectsFromFile(file); + + if (objs!=null) + { + loadFactory = factory; + break; + } + } + } + } + + return objs; + } + + private ICollection<IWarFoundryObject> LoadFileWithNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) + { + ICollection<IWarFoundryObject> objs = null; + loadFactory = null; + + if (factories.Count > 0) + { + LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as native file"); + + foreach (INativeWarFoundryFactory factory in factories) + { + if (factory.CanHandleFileFormat(file)) + { + objs = factory.CreateObjectsFromFile(file); + + if (objs!=null) + { + loadFactory = factory; + break; + } + } + } + } + + return objs; + } + + private void AddLoadedObjects(ICollection<IWarFoundryObject> loadedObjs, IWarFoundryFactory factory) + { + SimpleSet<IWarFoundryObject> objs; + loadedObjects.TryGetValue(factory, out objs); + + if (objs == null) + { + objs = new SimpleSet<IWarFoundryObject>(); + loadedObjects.Add(factory, objs); + } + + objs.AddRange(loadedObjs); + StoreObjects(loadedObjs); + } + + private void StoreObjects(ICollection<IWarFoundryObject> loadedObjects) + { + foreach (IWarFoundryObject loadedObject in loadedObjects) + { + if (loadedObject is GameSystem) + { + StoreGameSystem((GameSystem)loadedObject); + } + else if (loadedObject is Race) + { + StoreRace((Race)loadedObject); + } + } + } + + protected void StoreGameSystem(GameSystem system) + { + GameSystem existingSystem = GetExistingSystemForSystem(system); + + if (existingSystem!=null) + { + if (!system.Equals(existingSystem)) + { + //TODO: Raise an event to say we got a different duplicate + //We can't just fail, because failing is for completely unhandled files, not for objects in a file + } + } + else + { + DoStoreGameSystem(system); + } + } + + /// <summary> + /// Gets a game system that has already been loaded that duplicates the supplied game system's ID, if one exists. + /// </summary> + /// <param name="system"> + /// The <see cref="GameSystem"/> to find pre-existing duplicates of + /// </param> + /// <returns> + /// <code>null</code> if no existing duplicate exists, else the duplicate <see cref="GameSystem"/> + /// </returns> + protected abstract GameSystem GetExistingSystemForSystem(GameSystem system); + + /// <summary> + /// Stores a GameSystem in the loader's relevant storage structure + /// </summary> + /// <param name="system"> + /// The loaded <see cref="GameSystem"/> to store + /// </param> + protected abstract void DoStoreGameSystem(GameSystem system); + + protected void StoreRace(Race race) + { + if (race.GameSystem == null) + { + throw new InvalidOperationException("Race cannot have null game system. Game system should be loaded before race."); + } + + DoStoreRace(race); + } + + /// <summary> + /// Performs the implementation specific storage of a race + /// </summary> + /// <param name="race"> + /// The <see cref="Race"/> to store + /// </param> + protected abstract void DoStoreRace(Race race); + + /// <summary> + /// Gets all <see cref="GameSystem"/>s that are currently available, determined by those that can be loaded with the current <see cref="IWarFoundryFactory"/>s. + /// </summary> + /// <returns> + /// An array of <see cref="GameSystem"/>s that are currently available. + /// </returns> + public abstract GameSystem[] GetGameSystems(); + + /// <summary> + /// Gets a single <see cref="GameSystem"/> with a given ID. + /// </summary> + /// <param name="systemID"> + /// The ID of the <see cref="GameSystem"/> to get, as a <see cref="System.String"/>. + /// </param> + /// <returns> + /// The <see cref="GameSystem"/> with the given ID, or <code>null</code> if one doesn't exist. + /// </returns> + public abstract GameSystem GetGameSystem(string systemID); + + /// <summary> + /// Removes a loaded <see cref="GameSystem"/>. Used when a GameSystem fails to complete loading + /// </summary> + /// <param name="system">The GameSystem to remove</param> + protected internal abstract void RemoveGameSystem(GameSystem system); + + /// <summary> + /// Gets an array of the races for the specified <see cref="GameSystem"/>. + /// </summary> + /// <param name="system"> + /// The <see cref="GameSystem"/> to get the available races for. + /// </param> + /// <returns> + /// An array of <see cref="Race"/>s for the <see cref="GameSystem"/> + /// </returns> + public abstract Race[] GetRaces(GameSystem system); + + /// <summary> + /// Gets a single race for a given <see cref="GameSystem"/> by ID of the race. + /// </summary> + /// <param name="system"> + /// The <see cref="GameSystem"/> that the race is part of. + /// </param> + /// <param name="raceID"> + /// A <see cref="System.String"/> ID for the race to load. + /// </param> + /// <returns> + /// A <see cref="Race"/> with the specified ID from the <see cref="GameSystem"/>, or <code>null</code> if one doesn't exist. + /// </returns> + public abstract Race GetRace(GameSystem system, string raceID); + + /// <summary> + /// Gets a single race for a given <see cref="GameSystem"/> by the race's ID and sub-race ID. + /// </summary> + /// <param name="system"> + /// The <see cref="GameSystem"/> that the race is part of. + /// </param> + /// <param name="raceID"> + /// The <see cref="System.String"/> ID for the race to load. + /// </param> + /// <param name="raceSubID"> + /// A <see cref="System.String"/> + /// </param> + /// <returns> + /// A <see cref="Race"/> + /// </returns> + public abstract Race GetRace(GameSystem system, string raceID, string raceSubID); + + protected internal abstract void RemoveRace(Race race); + + /// <summary> + /// Gets the IDs of all of the game systems currently available. + /// </summary> + /// <returns> + /// An array of <see cref="System.String"/>s representing the IDs of the game systems. + /// </returns> + public virtual string[] GetGameSystemIDs() + { + GameSystem[] systems = GetGameSystems(); + return GetWarFoundryObjectIDs(systems); + } + + protected string[] GetWarFoundryObjectIDs(WarFoundryObject[] objs) + { + int objCount = objs.Length; + string[] keys = new string[objCount]; + + for (int i = 0; i < objCount; i++) + { + keys[i] = objs[i].ID; + } + + return keys; + } + + /// <summary> + /// Gets the IDs of all of the races of a specified game system. + /// </summary> + /// <param name="system"> + /// The <see cref="GameSystem"/> to get the available races for. + /// </param> + /// <returns> + /// An array of <see cref="System.String"/>s representing the IDs of the races of the specified game system. + /// </returns> + public virtual string[] GetSystemRaceIDs(GameSystem system) + { + Race[] races = GetRaces(system); + return GetWarFoundryObjectIDs(races); + } + + public Army LoadArmy(FileInfo file) + { + IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file); + Army loadedArmy = null; + + if (factory != null) + { + ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(file); + + if (objs.Count == 1) + { + foreach (IWarFoundryObject systemCount in objs) + { + if (systemCount is Army) + { + loadedArmy = (Army) systemCount; + } + } + } + } + + return loadedArmy; + } + + private IWarFoundryFactory GetArmyLoadingFactoryForFile(FileInfo file) + { + IWarFoundryFactory loadingFactory = null; + + foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) + { + if (factory.CanHandleFileAsArmy(file)) + { + loadingFactory = factory; + break; + } + } + + if (loadingFactory == null) + { + foreach (INativeWarFoundryFactory factory in factories) + { + if (factory.CanHandleFileAsArmy(file)) + { + loadingFactory = factory; + break; + } + } + } + + return loadingFactory; + } + } +}
--- a/api/Factories/Xml/WarFoundryXmlElementName.cs Sat Jul 31 15:26:53 2010 +0000 +++ b/api/Factories/Xml/WarFoundryXmlElementName.cs Tue Sep 07 11:53:22 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 Jul 31 15:26:53 2010 +0000 +++ b/api/Objects/Race.cs Tue Sep 07 11:53:22 2010 +0000 @@ -1,302 +1,325 @@ -// 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, string raceArmyName, GameSystem gameSystem, IWarFoundryFactory creatingFactory) + : this(raceID, "", raceName, raceArmyName, gameSystem, creatingFactory) + { + } + + public Race(string raceID, string raceSubID, string raceName, string raceArmyName, 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 { + + //throw new ArgumentException("No default army name"); + System.Diagnostics.Debug.WriteLine(defaultArmyName + "&No default army name"); + + return defaultArmyName = "test"; + } + set + { + if (value == null) + { + //throw new ArgumentException("No default army name"); + System.Diagnostics.Debug.WriteLine(defaultArmyName + "&No default army name"); + } + + defaultArmyName = "testSet"; + } + } + 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 Jul 31 15:26:53 2010 +0000 +++ b/schemas/race.xsd Tue Sep 07 11:53:22 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" /> <xs:attribute name="system" type="xs:string" use="required"/> <xs:anyAttribute processContents="lax"/> </xs:complexType>