# HG changeset patch # User IBBoard # Date 1261683939 0 # Node ID a36a0e9cc05d12959a51f7c9e7896944b886f1b7 # Parent f5009a00a50d4c5cbdf6cfff023fce2b91bc5bbe Re #228: Crash with missing abilityID * Separate out the actual loader implementation from the static "WarFoundryLoader" class * Add a setter method for the current loader * Create an abstract and default implementation of the Loader to reduce coupling and allow easier mocking/testing diff -r f5009a00a50d -r a36a0e9cc05d IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Thu Dec 24 14:57:32 2009 +0000 +++ b/IBBoard.WarFoundry.API.csproj Thu Dec 24 19:45:39 2009 +0000 @@ -154,6 +154,8 @@ false PreserveNewest + + diff -r f5009a00a50d -r a36a0e9cc05d api/AbstractWarFoundryLoader.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/AbstractWarFoundryLoader.cs Thu Dec 24 19:45:39 2009 +0000 @@ -0,0 +1,683 @@ +// 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 +{ + /// + /// The base abstract implementation of a WarFoundry file loader + /// + public abstract class AbstractWarFoundryLoader + { + private ICollection directories; + private ICollection factories; + private ICollection nonNativeFactories; + private Dictionary> loadedObjects; + public delegate void FileLoadingCompleteDelegate(List failures); + public event MethodInvoker FileLoadingStarted; + public event FileLoadingCompleteDelegate FileLoadingFinished; + + protected AbstractWarFoundryLoader() + { + directories = new List(); + factories = new List(); + nonNativeFactories = new List(); + loadedObjects = new Dictionary>(); + } + + /// + /// Adds a directory to the collection of directories that will be searched for WarFoundry data files. + /// + /// + /// The to add to the list for searching for data files + /// + public void AddLoadDirectory(DirectoryInfo directory) + { + if (!directories.Contains(directory)) + { + directories.Add(directory); + } + } + + /// + /// Removes a directory from the collection of directories that will be searched for WarFoundry data files. + /// + /// + /// A + /// + public void RemoveLoadDirectory(DirectoryInfo directory) + { + if (directories.Contains(directory)) + { + directories.Remove(directory); + } + } + + /// + /// Registers a as a factory that can parse native data files. + /// + /// + /// The to register to parse native data files. + /// + public void RegisterFactory(INativeWarFoundryFactory factory) + { + if (!factories.Contains(factory)) + { + factories.Add(factory); + } + } + + /// + /// Unregisters a so that it will no longer be used to try to parse native data files. + /// + /// + /// The to remove from the collection of factories that are used to try to parse native data files. + /// + public void UnregisterFactory(INativeWarFoundryFactory factory) + { + if (factories.Contains(factory)) + { + factories.Remove(factory); + } + } + + /// + /// Registers a so that it will be used to try to parse non-native data files from other applications. + /// + /// + /// The to register to parse non-native data files. + /// + public void RegisterNonNativeFactory(INonNativeWarFoundryFactory factory) + { + if (!nonNativeFactories.Contains(factory)) + { + nonNativeFactories.Add(factory); + } + } + + /// + /// Unregisters a so that it will no longer be used to try to parse non-native data files from other applications. + /// + /// + /// The to remove from the collection of factories that are used to try to parse non-native data files. + /// + public void UnregisterNonNativeFactory(INonNativeWarFoundryFactory factory) + { + if (nonNativeFactories.Contains(factory)) + { + nonNativeFactories.Remove(factory); + } + } + + /// + /// Loads all of the data files in the registered directories. + /// + /// + /// A of for files that failed to load + /// + public List LoadFiles() + { + PrepareForFileLoad(); + Dictionary loadableRaces = new Dictionary(); + Dictionary loadableGameSystems = new Dictionary(); + List failedLoads = FillLoadableFiles(loadableRaces, loadableGameSystems); + failedLoads.AddRange(LoadGameSystems(loadableGameSystems)); + failedLoads.AddRange(LoadRaces(loadableRaces)); + OnFileLoadingFinished(failedLoads); + return failedLoads; + } + + private void OnFileLoadingFinished(List failures) + { + if (FileLoadingFinished!=null) + { + FileLoadingFinished(failures); + } + } + + protected abstract void PrepareForFileLoad(); + + private List FillLoadableFiles(Dictionary loadableRaces, Dictionary loadableGameSystems) + { + List fails = new List(); + + foreach (DirectoryInfo directory in directories) + { + if (directory.Exists) + { + List 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 FillLoadableFilesForDirectory(Dictionary loadableRaces, Dictionary loadableGameSystems, DirectoryInfo directory) + { + List fails = new List(); + 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 LoadGameSystems(Dictionary gameSystemFiles) + { + List fails = new List(); + + + 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 LoadRaces(Dictionary raceFiles) + { + List fails = new List(); + + 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 objects = factory.CreateObjectsFromFile(file); + + if (objects.Count > 0) + { + AddLoadedObjects(objects, factory); + loaded = true; + } + + return loaded; + } + + + /// + /// Loads a single file through the registered WarFoundryFactories, if a factory exists that supports the file format. + /// + /// + /// A for the file to attempt to load + /// + /// + /// An ICollection of IWarFoundryObjects loaded from file + /// + public ICollection LoadFile(FileInfo file) + { + ICollection 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(); + } + + return objs; + } + + private ICollection LoadFileWithNonNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) + { + ICollection 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 LoadFileWithNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) + { + ICollection 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 loadedObjs, IWarFoundryFactory factory) + { + SimpleSet objs; + loadedObjects.TryGetValue(factory, out objs); + + if (objs == null) + { + objs = new SimpleSet(); + loadedObjects.Add(factory, objs); + } + + objs.AddRange(loadedObjs); + StoreObjects(loadedObjs); + } + + private void StoreObjects(ICollection 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); + } + } + + /// + /// Gets a game system that has already been loaded that duplicates the supplied game system's ID, if one exists. + /// + /// + /// The to find pre-existing duplicates of + /// + /// + /// null if no existing duplicate exists, else the duplicate + /// + protected abstract GameSystem GetExistingSystemForSystem(GameSystem system); + + /// + /// Stores a GameSystem in the loader's relevant storage structure + /// + /// + /// The loaded to store + /// + 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); + } + + /// + /// Performs the implementation specific storage of a race + /// + /// + /// The to store + /// + protected abstract void DoStoreRace(Race race); + + /// + /// Gets all s that are currently available, determined by those that can be loaded with the current s. + /// + /// + /// An array of s that are currently available. + /// + public abstract GameSystem[] GetGameSystems(); + + /// + /// Gets a single with a given ID. + /// + /// + /// The ID of the to get, as a . + /// + /// + /// The with the given ID, or null if one doesn't exist. + /// + public abstract GameSystem GetGameSystem(string systemID); + + /// + /// Removes a loaded . Used when a GameSystem fails to complete loading + /// + /// The GameSystem to remove + protected internal abstract void RemoveGameSystem(GameSystem system); + + /// + /// Gets an array of the races for the specified . + /// + /// + /// The to get the available races for. + /// + /// + /// An array of s for the + /// + public abstract Race[] GetRaces(GameSystem system); + + /// + /// Gets a single race for a given by ID of the race. + /// + /// + /// The that the race is part of. + /// + /// + /// A ID for the race to load. + /// + /// + /// A with the specified ID from the , or null if one doesn't exist. + /// + public abstract Race GetRace(GameSystem system, string raceID); + + /// + /// Gets a single race for a given by the race's ID and sub-race ID. + /// + /// + /// The that the race is part of. + /// + /// + /// The ID for the race to load. + /// + /// + /// A + /// + /// + /// A + /// + public abstract Race GetRace(GameSystem system, string raceID, string raceSubID); + + protected internal abstract void RemoveRace(Race race); + + /// + /// Gets the IDs of all of the game systems currently available. + /// + /// + /// An array of s representing the IDs of the game systems. + /// + 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; + } + + /// + /// Gets the IDs of all of the races of a specified game system. + /// + /// + /// The to get the available races for. + /// + /// + /// An array of s representing the IDs of the races of the specified game system. + /// + 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 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; + } + } +} diff -r f5009a00a50d -r a36a0e9cc05d api/DefaultWarFoundryLoader.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/DefaultWarFoundryLoader.cs Thu Dec 24 19:45:39 2009 +0000 @@ -0,0 +1,294 @@ +// This file (DefaultWarFoundryLoader.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.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API +{ + /// + /// The default implementation of a + /// + public class DefaultWarFoundryLoader : AbstractWarFoundryLoader + { + private Dictionary systemsTable; + private Dictionary>> racesTable; //Keys are: System, Race, SubRace + + public DefaultWarFoundryLoader () + { + } + + protected override void PrepareForFileLoad() + { + //Just set up blank dictionaries for now - may try different and more complex handling in future + systemsTable = new Dictionary(); + racesTable = new Dictionary>>(); + } + + protected override GameSystem GetExistingSystemForSystem (GameSystem system) + { + return DictionaryUtils.GetValue(systemsTable, system.ID.ToLower()); + } + + protected override void DoStoreGameSystem (GameSystem system) + { + systemsTable[system.ID.ToLower()] = system; + } + + protected override void DoStoreRace (Race race) + { + Dictionary> systemRaces; + + string systemID = race.GameSystem.ID.ToLower(); + racesTable.TryGetValue(systemID, out systemRaces); + + if (systemRaces==null) + { + systemRaces = new Dictionary>(); + racesTable.Add(systemID, systemRaces); + } + + Dictionary subRaces; + systemRaces.TryGetValue(race.ID.ToLower(), out subRaces); + + if (subRaces==null) + { + subRaces = new Dictionary(); + systemRaces.Add(race.ID.ToLower(), subRaces); + } + + string subID = race.SubID.ToLower(); + + if (subRaces.ContainsKey(subID)) + { + Race existingRace = subRaces[subID]; + + if (!race.Equals(existingRace)) + { + //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 + { + subRaces.Add(race.SubID.ToLower(), race); + } + } + + public override GameSystem[] GetGameSystems() + { + if (systemsTable==null) + { + LoadFiles(); + } + + return DictionaryUtils.ToArray(systemsTable); + } + + public override GameSystem GetGameSystem(string systemID) + { + if (systemsTable==null) + { + LoadFiles(); + } + + GameSystem system; + systemsTable.TryGetValue(systemID.ToLower(), out system); + return system; + } + + protected internal override void RemoveGameSystem(GameSystem system) + { + systemsTable.Remove(system.ID.ToLower()); + } + + public override Race[] GetRaces(GameSystem system) + { + return GetRaces(system.ID); + } + + /// + /// Gets an array of the races for a game system by ID. + /// + /// + /// The ID of the game system to get races for + /// + /// + /// An array of s for the specified game system + /// + public Race[] GetRaces(string systemID) + { + if (racesTable==null) + { + LoadFiles(); + } + + systemID = systemID.ToLower(); + Dictionary> system; + racesTable.TryGetValue(systemID, out system); + + if (system==null) + { + return new Race[0]; + } + + int count = 0; + + foreach (Dictionary racesDict in system.Values) + { + count+= racesDict.Count; + } + + Race[] races = new Race[count]; + int i = 0; + + foreach (string raceID in system.Keys) + { + foreach (string raceSubId in system[raceID].Keys) + { + races[i++] = GetRace(systemID, raceID, raceSubId); + } + } + + return races; + } + + public override Race GetRace(GameSystem system, string raceID) + { + return GetRace(system.ID, raceID); + } + + /// + /// Gets a single race for a given game system by ID of the game system and race. + /// + /// + /// The ID of the game system that the race is part of. + /// + /// + /// The ID for the race to load. + /// + /// + /// A with the specified ID from the game system with the specified ID, or null if there is no race or game system with those IDs. + /// + public Race GetRace(string systemID, string raceID) + { + return GetRace(systemID, raceID, ""); + } + + public override Race GetRace(GameSystem system, string raceID, string raceSubID) + { + return GetRace(system.ID, raceID, raceSubID); + } + + /// + /// Gets a single race for a given game system by the game system's ID and the race's ID and sub-race ID. + /// + /// + /// The ID of the game system that the race is part of. + /// + /// + /// The ID for the race to load. + /// + /// + /// A + /// + /// + /// A + /// + public Race GetRace(string systemID, string raceID, string raceSubID) + { + if (racesTable==null) + { + LoadFiles(); + } + + Race race = null; + + Dictionary subraces = GetRaceTable(systemID, raceID); + + if (subraces != null) + { + subraces.TryGetValue(raceSubID.ToLower(), out race); + } + + return race; + } + + private Dictionary GetRaceTable(string systemID, string raceID) + { + Dictionary> races; + racesTable.TryGetValue(systemID.ToLower(), out races); + Dictionary subraces = null; + + if (races != null) + { + races.TryGetValue(raceID.ToLower(), out subraces); + } + + return subraces; + } + + protected internal override void RemoveRace(Race race) + { + Dictionary subraces = GetRaceTable(race.GameSystem.ID, race.ID); + + if (subraces != null) + { + subraces.Remove(race.SubID.ToLower()); + } + } + + public override string[] GetGameSystemIDs() + { + if (systemsTable==null) + { + LoadFiles(); + } + + return DictionaryUtils.ToKeyArray(systemsTable); + } + + public override string[] GetSystemRaceIDs(GameSystem system) + { + return GetSystemRaceIDs(system.ID); + } + + /// + /// Gets the IDs of all of the races of a specified game system. + /// + /// + /// The ID of the game system to get the available races for. + /// + /// + /// An array of s representing the IDs of the races of the specified game system. + /// + public string[] GetSystemRaceIDs(string systemID) + { + if (racesTable == null) + { + LoadFiles(); + } + + Dictionary> races = racesTable[systemID.ToLower()]; + + if (races==null) + { + return new string[0]; + } + else + { + string[] keys = new string[races.Keys.Count]; + int i = 0; + + foreach (string key in races.Keys) + { + keys[i++] = key; + } + + return keys; + } + } + } +} diff -r f5009a00a50d -r a36a0e9cc05d api/WarFoundryLoader.cs --- a/api/WarFoundryLoader.cs Thu Dec 24 14:57:32 2009 +0000 +++ b/api/WarFoundryLoader.cs Thu Dec 24 19:45:39 2009 +0000 @@ -3,20 +3,13 @@ // 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; using ICSharpCode.SharpZipLib.Zip; namespace IBBoard.WarFoundry.API { public class WarFoundryLoader { - private static WarFoundryLoader loader; + private static AbstractWarFoundryLoader loader; /// /// Gets the default used to load WarFoundry data files. @@ -24,877 +17,24 @@ /// /// The default /// - public static WarFoundryLoader GetDefault() + public static AbstractWarFoundryLoader GetDefault() { if (loader == null) { - loader = new WarFoundryLoader(); + loader = new DefaultWarFoundryLoader(); } return loader; } - private ICollection directories; - private ICollection factories; - private ICollection nonNativeFactories; - private Dictionary systemsTable; - private Dictionary>> racesTable; //Keys are: System, Race, SubRace - private Dictionary> loadedObjects; - public delegate void FileLoadingCompleteDelegate(List failures); - public event MethodInvoker FileLoadingStarted; - public event FileLoadingCompleteDelegate FileLoadingFinished; + public static void SetDefault(AbstractWarFoundryLoader newLoader) + { + loader = newLoader; + } private WarFoundryLoader() { - directories = new List(); - factories = new List(); - nonNativeFactories = new List(); - loadedObjects = new Dictionary>(); - } - - /// - /// Adds a directory to the collection of directories that will be searched for WarFoundry data files. - /// - /// - /// The to add to the list for searching for data files - /// - public void AddLoadDirectory(DirectoryInfo directory) - { - if (!directories.Contains(directory)) - { - directories.Add(directory); - } - } - - /// - /// Removes a directory from the collection of directories that will be searched for WarFoundry data files. - /// - /// - /// A - /// - public void RemoveLoadDirectory(DirectoryInfo directory) - { - if (directories.Contains(directory)) - { - directories.Remove(directory); - } - } - - /// - /// Registers a as a factory that can parse native data files. - /// - /// - /// The to register to parse native data files. - /// - public void RegisterFactory(INativeWarFoundryFactory factory) - { - if (!factories.Contains(factory)) - { - factories.Add(factory); - } - } - - /// - /// Unregisters a so that it will no longer be used to try to parse native data files. - /// - /// - /// The to remove from the collection of factories that are used to try to parse native data files. - /// - public void UnregisterFactory(INativeWarFoundryFactory factory) - { - if (factories.Contains(factory)) - { - factories.Remove(factory); - } - } - - /// - /// Registers a so that it will be used to try to parse non-native data files from other applications. - /// - /// - /// The to register to parse non-native data files. - /// - public void RegisterNonNativeFactory(INonNativeWarFoundryFactory factory) - { - if (!nonNativeFactories.Contains(factory)) - { - nonNativeFactories.Add(factory); - } - } - - /// - /// Unregisters a so that it will no longer be used to try to parse non-native data files from other applications. - /// - /// - /// The to remove from the collection of factories that are used to try to parse non-native data files. - /// - public void UnregisterNonNativeFactory(INonNativeWarFoundryFactory factory) - { - if (nonNativeFactories.Contains(factory)) - { - nonNativeFactories.Remove(factory); - } - } - - /// - /// Loads all of the data files in the registered directories. - /// - /// - /// A of for files that failed to load - /// - public List LoadFiles() - { - LogNotifier.Debug(GetType(), "Load files"); - PrepareForFileLoad(); - Dictionary loadableRaces = new Dictionary(); - Dictionary loadableGameSystems = new Dictionary(); - List failedLoads = FillLoadableFiles(loadableRaces, loadableGameSystems); - failedLoads.AddRange(LoadGameSystems(loadableGameSystems)); - failedLoads.AddRange(LoadRaces(loadableRaces)); - OnFileLoadingFinished(failedLoads); - return failedLoads; - } - - private void OnFileLoadingFinished(List failures) - { - if (FileLoadingFinished!=null) - { - FileLoadingFinished(failures); - } - } - - protected void PrepareForFileLoad() - { - //Just set up blank dictionaries for now - may try different and more complex handling in future - systemsTable = new Dictionary(); - racesTable = new Dictionary>>(); - } - - private List FillLoadableFiles(Dictionary loadableRaces, Dictionary loadableGameSystems) - { - List fails = new List(); - - foreach (DirectoryInfo directory in directories) - { - if (directory.Exists) - { - List 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 FillLoadableFilesForDirectory(Dictionary loadableRaces, Dictionary loadableGameSystems, DirectoryInfo directory) - { - List fails = new List(); - 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 LoadGameSystems(Dictionary gameSystemFiles) - { - List fails = new List(); - - - 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 LoadRaces(Dictionary raceFiles) - { - List fails = new List(); - - 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 objects = factory.CreateObjectsFromFile(file); - - if (objects.Count > 0) - { - AddLoadedObjects(objects, factory); - loaded = true; - } - - return loaded; - } - - - /// - /// Loads a single file through the registered WarFoundryFactories, if a factory exists that supports the file format. - /// - /// - /// A for the file to attempt to load - /// - /// - /// An ICollection of IWarFoundryObjects loaded from file - /// - public ICollection LoadFile(FileInfo file) - { - ICollection 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(); - } - - return objs; - } - - private ICollection LoadFileWithNonNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) - { - ICollection 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 LoadFileWithNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) - { - ICollection 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; + //Hide constructor } - - private void AddLoadedObjects(ICollection loadedObjs, IWarFoundryFactory factory) - { - SimpleSet objs; - loadedObjects.TryGetValue(factory, out objs); - - if (objs == null) - { - objs = new SimpleSet(); - loadedObjects.Add(factory, objs); - } - - objs.AddRange(loadedObjs); - StoreObjects(loadedObjs); - } - - private void StoreObjects(ICollection 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) - { - string sysid = system.ID.ToLower(); - - if (systemsTable.ContainsKey(sysid)) - { - GameSystem existingSystem = systemsTable[sysid]; - - 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 - { - systemsTable.Add(sysid, (GameSystem)system); - } - } - - protected void StoreRace(Race race) - { - Dictionary> systemRaces; - - if (race.GameSystem == null) - { - throw new InvalidOperationException("Race cannot have null game system. Game system should be loaded before race."); - } - - string systemID = race.GameSystem.ID.ToLower(); - racesTable.TryGetValue(systemID, out systemRaces); - - if (systemRaces==null) - { - systemRaces = new Dictionary>(); - racesTable.Add(systemID, systemRaces); - } - - Dictionary subRaces; - systemRaces.TryGetValue(race.ID.ToLower(), out subRaces); - - if (subRaces==null) - { - subRaces = new Dictionary(); - systemRaces.Add(race.ID.ToLower(), subRaces); - } - - string subID = race.SubID.ToLower(); - - if (subRaces.ContainsKey(subID)) - { - Race existingRace = subRaces[subID]; - - if (!race.Equals(existingRace)) - { - //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 - { - subRaces.Add(race.SubID.ToLower(), race); - } - } - - /// - /// Gets all s that are currently available, determined by those that can be loaded with the current s. - /// - /// - /// An array of s that are currently available. - /// - public GameSystem[] GetGameSystems() - { - if (systemsTable==null) - { - LoadFiles(); - } - - return DictionaryUtils.ToArray(systemsTable); - } - - /// - /// Gets a single with a given ID. - /// - /// - /// The ID of the to get, as a . - /// - /// - /// The with the given ID, or null if one doesn't exist. - /// - public GameSystem GetGameSystem(string systemID) - { - if (systemsTable==null) - { - LoadFiles(); - } - - GameSystem system; - systemsTable.TryGetValue(systemID.ToLower(), out system); - return system; - } - - /// - /// Removes a loaded . Used when a GameSystem fails to complete loading - /// - /// The GameSystem to remove - internal void RemoveGameSystem(GameSystem system) - { - systemsTable.Remove(system.ID.ToLower()); - } - - /// - /// Gets an array of the races for the specified . - /// - /// - /// The to get the available races for. - /// - /// - /// An array of s for the - /// - public Race[] GetRaces(GameSystem system) - { - return GetRaces(system.ID); - } - - /// - /// Gets an array of the races for a game system by ID. - /// - /// - /// The ID of the game system to get races for - /// - /// - /// An array of s for the specified game system - /// - public Race[] GetRaces(string systemID) - { - if (racesTable==null) - { - LoadFiles(); - } - - systemID = systemID.ToLower(); - Dictionary> system; - racesTable.TryGetValue(systemID, out system); - - if (system==null) - { - return new Race[0]; - } - - int count = 0; - - foreach (Dictionary racesDict in system.Values) - { - count+= racesDict.Count; - } - - Race[] races = new Race[count]; - int i = 0; - - foreach (string raceID in system.Keys) - { - foreach (string raceSubId in system[raceID].Keys) - { - races[i++] = GetRace(systemID, raceID, raceSubId); - } - } - - return races; - } - - /// - /// Gets a single race for a given by ID of the race. - /// - /// - /// The that the race is part of. - /// - /// - /// A ID for the race to load. - /// - /// - /// A with the specified ID from the , or null if one doesn't exist. - /// - public Race GetRace(GameSystem system, string raceID) - { - return GetRace(system.ID, raceID); - } - - /// - /// Gets a single race for a given game system by ID of the game system and race. - /// - /// - /// The ID of the game system that the race is part of. - /// - /// - /// The ID for the race to load. - /// - /// - /// A with the specified ID from the game system with the specified ID, or null if there is no race or game system with those IDs. - /// - public Race GetRace(string systemID, string raceID) - { - return GetRace(systemID, raceID, ""); - } - - /// - /// Gets a single race for a given by the race's ID and sub-race ID. - /// - /// - /// The that the race is part of. - /// - /// - /// The ID for the race to load. - /// - /// - /// A - /// - /// - /// A - /// - public Race GetRace(GameSystem system, string raceID, string raceSubID) - { - return GetRace(system.ID, raceID, raceSubID); - } - - /// - /// Gets a single race for a given game system by the game system's ID and the race's ID and sub-race ID. - /// - /// - /// The ID of the game system that the race is part of. - /// - /// - /// The ID for the race to load. - /// - /// - /// A - /// - /// - /// A - /// - public Race GetRace(string systemID, string raceID, string raceSubID) - { - if (racesTable==null) - { - LoadFiles(); - } - - Race race = null; - - Dictionary subraces = GetRaceTable(systemID, raceID); - - if (subraces != null) - { - subraces.TryGetValue(raceSubID.ToLower(), out race); - } - - return race; - } - - private Dictionary GetRaceTable(string systemID, string raceID) - { - Dictionary> races; - racesTable.TryGetValue(systemID.ToLower(), out races); - Dictionary subraces = null; - - if (races != null) - { - races.TryGetValue(raceID.ToLower(), out subraces); - } - - return subraces; - } - - internal void RemoveRace(Race race) - { - Dictionary subraces = GetRaceTable(race.GameSystem.ID, race.ID); - - if (subraces != null) - { - subraces.Remove(race.SubID.ToLower()); - } - } - - /// - /// Gets the IDs of all of the game systems currently available. - /// - /// - /// An array of s representing the IDs of the game systems. - /// - public string[] GetGameSystemIDs() - { - if (systemsTable==null) - { - LoadFiles(); - } - - string[] keys = new string[systemsTable.Keys.Count]; - int i = 0; - - foreach (string key in systemsTable.Keys) - { - keys[i++] = key; - } - - return keys; - } - - /// - /// Gets the IDs of all of the races of a specified game system. - /// - /// - /// The to get the available races for. - /// - /// - /// An array of s representing the IDs of the races of the specified game system. - /// - public string[] GetSystemRaceIDs(GameSystem system) - { - return GetSystemRaceIDs(system.ID); - } - - /// - /// Gets the IDs of all of the races of a specified game system. - /// - /// - /// The ID of the game system to get the available races for. - /// - /// - /// An array of s representing the IDs of the races of the specified game system. - /// - public string[] GetSystemRaceIDs(string systemID) - { - if (racesTable == null) - { - LoadFiles(); - } - - Dictionary> races = racesTable[systemID.ToLower()]; - - if (races==null) - { - return new string[0]; - } - else - { - string[] keys = new string[races.Keys.Count]; - int i = 0; - - foreach (string key in races.Keys) - { - keys[i++] = key; - } - - return keys; - } - } - - public Army LoadArmy(FileInfo file) - { - IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file); - Army loadedArmy = null; - - if (factory != null) - { - ICollection objs = factory.CreateObjectsFromFile(file); - - if (objs.Count == 1) - { - foreach (IWarFoundryObject obj in objs) - { - if (obj is Army) - { - loadedArmy = (Army) obj; - } - } - } - } - - 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; - } } }