# HG changeset patch # User IBBoard # Date 1341691292 -3600 # Node ID 1ed2f3ab5e358e99ab4a6ac7d036fc1b187d2370 # Parent 81f32062c9fa4c4fb016b80d2622beebbc1a3051 Re #419: Remove assumptions of a file-based install * Swap API to using new "loadable object" and "loadable object source" wrappers to allow file-based or memory-based loading diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/AbstractWarFoundryLoader.cs --- a/API/AbstractWarFoundryLoader.cs Mon Jun 25 21:04:02 2012 +0100 +++ b/API/AbstractWarFoundryLoader.cs Sat Jul 07 21:01:32 2012 +0100 @@ -8,6 +8,7 @@ using IBBoard.IO; using IBBoard.WarFoundry.API.Factories; using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.API.Loading; namespace IBBoard.WarFoundry.API { @@ -16,7 +17,7 @@ /// public abstract class AbstractWarFoundryLoader { - private ICollection directories; + private ICollection directories; private ICollection factories; private ICollection nonNativeFactories; private Dictionary> loadedObjects; @@ -28,8 +29,8 @@ protected AbstractWarFoundryLoader() { - directories = new List(); - directories.Add(new DirectoryInfo(WarFoundryLoader.DEFAULT_USER_DATA_DIR)); + directories = new List(); + directories.Add(new LoadableObjectSourceDirectory(WarFoundryLoader.DEFAULT_USER_DATA_DIR)); factories = new List(); nonNativeFactories = new List(); loadedObjects = new Dictionary>(); @@ -41,11 +42,23 @@ /// /// The to add to the list for searching for data files /// + [Obsolete("Use load sources")] public void AddLoadDirectory(DirectoryInfo directory) { - if (!directories.Contains(directory)) + AddLoadSource(new LoadableObjectSourceDirectory(directory)); + } + + /// + /// Adds a load source to the set of sources that objects will be loaded from + /// + /// + /// The load source. + /// + public void AddLoadSource(ILoadableObjectSource loadSource) + { + if (!directories.Contains(loadSource)) { - directories.Add(directory); + directories.Add(loadSource); } } @@ -55,11 +68,17 @@ /// /// A /// + [Obsolete("Use load sources")] public void RemoveLoadDirectory(DirectoryInfo directory) + { + RemoveLoadSource(new LoadableObjectSourceDirectory(directory)); + } + + public void RemoveLoadSource(ILoadableObjectSource loadSource) { - if (directories.Contains(directory)) + if (directories.Contains(loadSource)) { - directories.Remove(directory); + directories.Remove(loadSource); } } @@ -129,8 +148,8 @@ { OnFileLoadingStarted(); PrepareForFileLoad(); - Dictionary loadableRaces = new Dictionary(); - Dictionary loadableGameSystems = new Dictionary(); + Dictionary loadableRaces = new Dictionary(); + Dictionary loadableGameSystems = new Dictionary(); List failedLoads = FillLoadableFiles(loadableRaces, loadableGameSystems); failedLoads.AddRange(LoadGameSystems(loadableGameSystems)); failedLoads.AddRange(LoadRaces(loadableRaces)); @@ -165,29 +184,24 @@ //Do nothing special } - private List FillLoadableFiles(Dictionary loadableRaces, Dictionary loadableGameSystems) + private List FillLoadableFiles(Dictionary loadableRaces, Dictionary loadableGameSystems) { List fails = new List(); - foreach (DirectoryInfo directory in directories) + foreach (ILoadableObjectSource directory in directories) { - directory.Refresh(); - - if (directory.Exists) - { - List directoryFails = FillLoadableFilesForDirectory(loadableRaces, loadableGameSystems, directory); - fails.AddRange(directoryFails); - } + List directoryFails = FillLoadableFilesForDirectory(loadableRaces, loadableGameSystems, directory); + fails.AddRange(directoryFails); } return fails; } - private List FillLoadableFilesForDirectory(Dictionary loadableRaces, Dictionary loadableGameSystems, DirectoryInfo directory) + private List FillLoadableFilesForDirectory(Dictionary loadableRaces, Dictionary loadableGameSystems, ILoadableObjectSource directory) { List fails = new List(); - foreach (FileInfo file in directory.GetFiles()) + foreach (ILoadableObject file in directory.GetLoadableObjects()) { IWarFoundryFactory factory = GetGameSystemLoadingFactoryForFile(file); @@ -210,16 +224,11 @@ } } } - - foreach (DirectoryInfo subdir in directory.GetDirectories()) - { - fails.AddRange(FillLoadableFilesForDirectory(loadableRaces, loadableGameSystems, subdir)); - } return fails; } - private IWarFoundryFactory GetGameSystemLoadingFactoryForFile(FileInfo file) + private IWarFoundryFactory GetGameSystemLoadingFactoryForFile(ILoadableObject file) { IWarFoundryFactory loadingFactory = null; @@ -247,7 +256,7 @@ return loadingFactory; } - private IWarFoundryFactory GetRaceLoadingFactoryForFile(FileInfo file) + private IWarFoundryFactory GetRaceLoadingFactoryForFile(ILoadableObject file) { IWarFoundryFactory loadingFactory = null; @@ -275,12 +284,12 @@ return loadingFactory; } - private List LoadGameSystems(Dictionary gameSystemFiles) + private List LoadGameSystems(Dictionary gameSystemFiles) { List fails = new List(); - foreach (FileInfo file in gameSystemFiles.Keys) + foreach (ILoadableObject file in gameSystemFiles.Keys) { FileLoadFailure failure = null; @@ -307,11 +316,11 @@ return fails; } - private List LoadRaces(Dictionary raceFiles) + private List LoadRaces(Dictionary raceFiles) { List fails = new List(); - foreach (FileInfo file in raceFiles.Keys) + foreach (ILoadableObject file in raceFiles.Keys) { FileLoadFailure failure = null; @@ -338,7 +347,7 @@ return fails; } - private bool LoadObject(FileInfo file, IWarFoundryFactory factory) + private bool LoadObject(ILoadableObject file, IWarFoundryFactory factory) { factory.RaceLoaded += StoreRace; factory.GameSystemLoaded += StoreGameSystem; @@ -351,13 +360,18 @@ /// 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 + /// A for the file to attempt to load /// /// /// An ICollection of IWarFoundryObjects loaded from file /// public ICollection LoadFile(FileInfo file) { + return LoadObject(new LoadableFileObject(file)); + } + + public ICollection LoadObject(ILoadableObject file) + { IWarFoundryFactory loadFactory = null; ICollection objs = LoadFileWithNonNativeFactories(file, out loadFactory); @@ -378,7 +392,7 @@ return objs; } - private ICollection LoadFileWithNonNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) + private ICollection LoadFileWithNonNativeFactories(ILoadableObject file, out IWarFoundryFactory loadFactory) { ICollection objs = null; loadFactory = null; @@ -405,7 +419,7 @@ return objs; } - private ICollection LoadFileWithNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) + private ICollection LoadFileWithNativeFactories(ILoadableObject file, out IWarFoundryFactory loadFactory) { ICollection objs = null; loadFactory = null; @@ -626,6 +640,11 @@ public Army LoadArmy(FileInfo file) { + return LoadArmy(new LoadableFileObject(file)); + } + + public Army LoadArmy(ILoadableObject file) + { IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file); Army loadedArmy = null; @@ -648,7 +667,7 @@ return loadedArmy; } - private IWarFoundryFactory GetArmyLoadingFactoryForFile(FileInfo file) + private IWarFoundryFactory GetArmyLoadingFactoryForFile(ILoadableObject file) { IWarFoundryFactory loadingFactory = null; diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Factories/AbstractNativeWarFoundryFactory.cs --- a/API/Factories/AbstractNativeWarFoundryFactory.cs Mon Jun 25 21:04:02 2012 +0100 +++ b/API/Factories/AbstractNativeWarFoundryFactory.cs Sat Jul 07 21:01:32 2012 +0100 @@ -14,6 +14,7 @@ using IBBoard.WarFoundry.API.Objects; using ICSharpCode.SharpZipLib.Zip; using IBBoard.Collections; +using IBBoard.WarFoundry.API.Loading; namespace IBBoard.WarFoundry.API.Factories { @@ -27,16 +28,17 @@ //Do nothing - just make the constructor non-public } - protected override ZipFile GetFileAsSupportedType(FileInfo file) + protected override ZipFile GetFileAsSupportedType(ILoadableObject file) { - ZipFile zip = null; - string ext = file.Extension.ToLower(); + ZipFile zip = null; + string name = file.Name; + string ext = file.Name.Substring(name.LastIndexOf('.')).ToLower(); if (ext == ".race" || ext == ".army" || ext == ".system") { try { - zip = new ZipFile(file.FullName); + zip = new ZipFile(file.GetStream()); } catch (ZipException) { diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Factories/AbstractNonNativeFileExtensionWarFoundryFactory.cs --- a/API/Factories/AbstractNonNativeFileExtensionWarFoundryFactory.cs Mon Jun 25 21:04:02 2012 +0100 +++ b/API/Factories/AbstractNonNativeFileExtensionWarFoundryFactory.cs Sat Jul 07 21:01:32 2012 +0100 @@ -5,10 +5,11 @@ using System.Collections.Generic; using System.IO; using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.API.Loading; namespace IBBoard.WarFoundry.API.Factories { - public abstract class AbstractNonNativeFileExtensionWarFoundryFactory : AbstractNonNativeWarFoundryFactory + public abstract class AbstractNonNativeFileExtensionWarFoundryFactory : AbstractNonNativeWarFoundryFactory { protected abstract string ArmyFileExtension { get; } @@ -16,38 +17,38 @@ protected abstract string GameSystemFileExtension { get; } - protected override bool CheckCanHandleFileFormat(FileInfo file) + protected override bool CheckCanHandleFileFormat(ILoadableObject file) { return CheckCanHandleFileAsArmy(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsGameSystem(file); } - protected override bool CheckCanHandleFileAsArmy(FileInfo file) + protected override bool CheckCanHandleFileAsArmy(ILoadableObject file) { return ArmyFileExtension != null && file.Name.ToLower().EndsWith(ArmyFileExtension); } - protected override bool CheckCanHandleFileAsRace(FileInfo file) + protected override bool CheckCanHandleFileAsRace(ILoadableObject file) { return RaceFileExtension != null && file.Name.ToLower().EndsWith(RaceFileExtension); } - protected override bool CheckCanHandleFileAsGameSystem(FileInfo file) + protected override bool CheckCanHandleFileAsGameSystem(ILoadableObject file) { return GameSystemFileExtension != null && file.Name.ToLower().EndsWith(GameSystemFileExtension); } - protected override FileInfo GetFileAsSupportedType(FileInfo file) + protected override ILoadableObject GetFileAsSupportedType(ILoadableObject file) { return file; } - protected abstract Army CreateArmyFromFile(FileInfo file); + protected abstract Army CreateArmyFromFile(ILoadableObject file); - protected abstract Race CreateRaceFromFile(FileInfo file); + protected abstract Race CreateRaceFromFile(ILoadableObject file); - protected abstract GameSystem CreateGameSystemFromFile(FileInfo file); + protected abstract GameSystem CreateGameSystemFromFile(ILoadableObject file); - protected override ICollection DoCreateObjectsFromFile(FileInfo file) + protected override ICollection DoCreateObjectsFromFile(ILoadableObject file) { IWarFoundryObject obj = null; diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Factories/AbstractNonNativeWarFoundryFactory.cs --- a/API/Factories/AbstractNonNativeWarFoundryFactory.cs Mon Jun 25 21:04:02 2012 +0100 +++ b/API/Factories/AbstractNonNativeWarFoundryFactory.cs Sat Jul 07 21:01:32 2012 +0100 @@ -10,6 +10,6 @@ { public abstract class AbstractNonNativeWarFoundryFactory : AbstractWarFoundryFactory, INonNativeWarFoundryFactory { - public abstract string NonNativeDataType { get; } + public abstract string NonNativeDataType { get; } } } diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Factories/AbstractWarFoundryFactory.cs --- a/API/Factories/AbstractWarFoundryFactory.cs Mon Jun 25 21:04:02 2012 +0100 +++ b/API/Factories/AbstractWarFoundryFactory.cs Sat Jul 07 21:01:32 2012 +0100 @@ -6,6 +6,7 @@ using System.IO; using System.Collections.Generic; using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.API.Loading; namespace IBBoard.WarFoundry.API.Factories { @@ -23,7 +24,7 @@ obj.SetAsFullyLoaded(); } - public bool CanHandleFileFormat (FileInfo file) + public bool CanHandleFileFormat(ILoadableObject file) { FILE_TYPE typedFile = GetFileAsSupportedType(file); bool canHandle = typedFile != null && CheckCanHandleFileFormat(typedFile); @@ -36,7 +37,7 @@ return canHandle; } - public bool CanHandleFileAsRace(FileInfo file) + public bool CanHandleFileAsRace(ILoadableObject file) { FILE_TYPE typedFile = GetFileAsSupportedType(file); bool canHandle = typedFile != null && CheckCanHandleFileAsRace(typedFile); @@ -49,7 +50,7 @@ return canHandle; } - public bool CanHandleFileAsGameSystem(FileInfo file) + public bool CanHandleFileAsGameSystem(ILoadableObject file) { FILE_TYPE typedFile = GetFileAsSupportedType(file); bool canHandle = typedFile != null && CheckCanHandleFileAsGameSystem(typedFile); @@ -62,7 +63,7 @@ return canHandle; } - public bool CanHandleFileAsArmy(FileInfo file) + public bool CanHandleFileAsArmy(ILoadableObject file) { FILE_TYPE typedFile = GetFileAsSupportedType(file); bool canHandle = typedFile != null && CheckCanHandleFileAsArmy(typedFile); @@ -81,16 +82,16 @@ } /// - /// Converts the object in to the appropriate type for this class so that it can perform its checks. If no conversion is required (the test can be performed on a object) the object should be returned with no modification. + /// Converts the object in to the appropriate type for this class so that it can perform its checks. If no conversion is required (the test can be performed on a object) the object should be returned with no modification. /// If the file is not of supported type the null should be returned. /// /// - /// A to get the supported source object from. + /// A to get the supported source object from. /// /// - /// An object of type that has been converted from the input object, or null if the conversion cannot be made. + /// An object of type that has been converted from the input object, or null if the conversion cannot be made. /// - protected abstract FILE_TYPE GetFileAsSupportedType(FileInfo file); + protected abstract FILE_TYPE GetFileAsSupportedType(ILoadableObject file); /// /// Checks whether the factory thinks it can load data from the file in its paramaterised type. @@ -137,7 +138,7 @@ protected abstract bool CheckCanHandleFileAsArmy(FILE_TYPE file); - public ICollection CreateObjectsFromFile(FileInfo file) + public ICollection CreateObjectsFromFile(ILoadableObject file) { return DoCreateObjectsFromFile(GetFileAsSupportedType(file)); } diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Factories/DummyWarFoundryFactory.cs --- a/API/Factories/DummyWarFoundryFactory.cs Mon Jun 25 21:04:02 2012 +0100 +++ b/API/Factories/DummyWarFoundryFactory.cs Sat Jul 07 21:01:32 2012 +0100 @@ -6,6 +6,7 @@ using IBBoard.WarFoundry.API.Objects; using System.IO; using System.Collections.Generic; +using IBBoard.WarFoundry.API.Loading; namespace IBBoard.WarFoundry.API.Factories { @@ -33,27 +34,27 @@ obj.SetAsFullyLoaded(); } - public bool CanHandleFileFormat(FileInfo file) + public bool CanHandleFileFormat(ILoadableObject file) { return false; } - public bool CanHandleFileAsRace(FileInfo file) + public bool CanHandleFileAsRace(ILoadableObject file) { return false; } - public bool CanHandleFileAsGameSystem(FileInfo file) + public bool CanHandleFileAsGameSystem(ILoadableObject file) { return false; } - public bool CanHandleFileAsArmy(FileInfo file) + public bool CanHandleFileAsArmy(ILoadableObject file) { return false; } - public ICollection CreateObjectsFromFile(FileInfo file) + public ICollection CreateObjectsFromFile(ILoadableObject file) { return new List(); } diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Factories/IWarFoundryFactory.cs --- a/API/Factories/IWarFoundryFactory.cs Mon Jun 25 21:04:02 2012 +0100 +++ b/API/Factories/IWarFoundryFactory.cs Sat Jul 07 21:01:32 2012 +0100 @@ -6,6 +6,7 @@ using System.IO; using System.Collections.Generic; using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.API.Loading; namespace IBBoard.WarFoundry.API.Factories { @@ -29,45 +30,45 @@ /// Checks if the factory thinks it can handle the supplied file. Checks can be performed on file extension or some basic check of file content, or some other method. /// /// - /// A for the file to check support for. + /// A for the file to check support for. /// /// /// true if the file appears to be supported for loading by this factory, else returns false /// - bool CanHandleFileFormat(FileInfo file); + bool CanHandleFileFormat(ILoadableObject file); /// /// Checks if the factory thinks it can handle the supplied file as a Race. Checks can be performed on file extension or some basic check of file content, or some other method. /// /// - /// A for the file to check support for as a file containing Race information. + /// A for the file to check support for as a file containing Race information. /// /// /// true if the file appears to be supported for loading by this factory as a Race, else returns false /// - bool CanHandleFileAsRace(FileInfo file); + bool CanHandleFileAsRace(ILoadableObject file); /// /// Checks if the factory thinks it can handle the supplied file as a GameSystem. Checks can be performed on file extension or some basic check of file content, or some other method. /// /// - /// A for the file to check support for as a file containing GameSystem information. + /// A for the file to check support for as a file containing GameSystem information. /// /// /// true if the file appears to be supported for loading by this factory as a GameSystem, else returns false /// - bool CanHandleFileAsGameSystem(FileInfo file); + bool CanHandleFileAsGameSystem(ILoadableObject file); /// /// Checks if the factory thinks it can handle the supplied file as a Army. Checks can be performed on file extension or some basic check of file content, or some other method. /// /// - /// A for the file to check support for as a file containing Army information. + /// A for the file to check support for as a file containing Army information. /// /// /// true if the file appears to be supported for loading by this factory as a Army, else returns false /// - bool CanHandleFileAsArmy(FileInfo file); + bool CanHandleFileAsArmy(ILoadableObject file); /// /// Reads the data from the supplied file and returns it as a collection of loadable objects. In addition, it fires the appropriate XxxLoaded event @@ -76,11 +77,11 @@ /// May throw a if the file is supported by the Factory but the content is invalid. /// /// - /// A for the file to load data from + /// A for the file to load data from /// /// /// A of s that were loaded from the file /// - ICollection CreateObjectsFromFile(FileInfo file); + ICollection CreateObjectsFromFile(ILoadableObject file); } } diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/FileLoadFailure.cs --- a/API/FileLoadFailure.cs Mon Jun 25 21:04:02 2012 +0100 +++ b/API/FileLoadFailure.cs Sat Jul 07 21:01:32 2012 +0100 @@ -6,6 +6,7 @@ using System.IO; using IBBoard.Lang; using IBBoard.WarFoundry.API.Factories; +using IBBoard.WarFoundry.API.Loading; namespace IBBoard.WarFoundry.API { @@ -14,7 +15,7 @@ /// public class FileLoadFailure { - private FileInfo failedFile; + private ILoadableObject failedFile; private IWarFoundryFactory loadingFactory; private string defaultMessage; private string messageTranslationID; @@ -25,7 +26,7 @@ /// Constructor for a failed file load where no factory was found. Translatable messages can be providied through a translationID or skipped by passing null. /// /// - /// The that failed to load + /// The that failed to load /// /// /// A message about the failure in English - used as a default fall-back message. @@ -33,7 +34,7 @@ /// /// The ID of a translation for the message. /// - public FileLoadFailure(FileInfo file, string message, string translationID) : this (file, null, message, "") + public FileLoadFailure(ILoadableObject file, string message, string translationID) : this (file, null, message, "") { } @@ -41,7 +42,7 @@ /// Constructor for a failed file load where a factory was identified as supporting the file but failed to load it. Translatable messages can be providied through a translationID or skipped by passing null. /// /// - /// The that failed to load + /// The that failed to load /// /// /// The that failed to load the file @@ -52,7 +53,7 @@ /// /// The ID of a translation for the message. /// - public FileLoadFailure(FileInfo file, IWarFoundryFactory factory, string message, string translationID) : this(file, factory, message, translationID, null) + public FileLoadFailure(ILoadableObject file, IWarFoundryFactory factory, string message, string translationID) : this(file, factory, message, translationID, null) { } @@ -60,7 +61,7 @@ /// Constructor for a failed file load where a factory was identified as supporting the file but an exception occurred while loading it. Translatable messages can be providied through a translationID or skipped by passing null. /// /// - /// The that failed to load + /// The that failed to load /// /// /// The that failed to load the file @@ -74,7 +75,7 @@ /// /// The that occurred to cause the load to fail /// - public FileLoadFailure(FileInfo file, IWarFoundryFactory factory, string message, string translationID, Exception exception) + public FileLoadFailure(ILoadableObject file, IWarFoundryFactory factory, string message, string translationID, Exception exception) { failedFile = file; loadingFactory = factory; @@ -83,7 +84,7 @@ cause = exception; } - public FileInfo FailedFile + public ILoadableObject FailedFile { get { @@ -97,14 +98,14 @@ { if (message == null) { - string fileName = FailedFile.FullName; + string fileName = FailedFile.Name; string factoryType = (loadingFactory == null ? "" : loadingFactory.GetType().Name); if (String.IsNullOrEmpty(messageTranslationID)) { message = String.Format(defaultMessage, fileName, factoryType); - } + } else - { + { message = Translation.GetTranslation(messageTranslationID, defaultMessage, fileName, factoryType); } } diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Loading/ILoadableObject.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Loading/ILoadableObject.cs Sat Jul 07 21:01:32 2012 +0100 @@ -0,0 +1,27 @@ +// This file (ILoadableObject.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 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.IO; + +namespace IBBoard.WarFoundry.API.Loading +{ + /// + /// Interface for objects that are the source of WarFoundryObjects that can be loaded by a Factory. + /// Examples include files and streams. + /// + public interface ILoadableObject + { + string Name { get; } + + /// + /// Gets the stream for the loadable object that the WarFoundryObject can be loaded from. This stream will be + /// created each time the method is called and must be disposed of by the calling function. + /// + /// + /// The stream for the source. + /// + Stream GetStream(); + } +} + diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Loading/ILoadableObjectSource.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Loading/ILoadableObjectSource.cs Sat Jul 07 21:01:32 2012 +0100 @@ -0,0 +1,21 @@ +// This file (ILoadableObjectSource.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 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; + + +namespace IBBoard.WarFoundry.API.Loading +{ + public interface ILoadableObjectSource + { + /// + /// Gets the loadable objects that this source can provide + /// + /// + /// The loadable objects. + /// + ICollection GetLoadableObjects(); + } +} + diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Loading/LoadableFileObject.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Loading/LoadableFileObject.cs Sat Jul 07 21:01:32 2012 +0100 @@ -0,0 +1,37 @@ +// This file (LoadableFileObject.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 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.IO; + +namespace IBBoard.WarFoundry.API.Loading +{ + public class LoadableFileObject : ILoadableObject + { + private FileInfo file; + + public LoadableFileObject(string filePath) : this(new FileInfo(filePath)) + { + //Do nothing extra + } + + public LoadableFileObject(FileInfo file) + { + this.file = file; + } + + public string Name + { + get + { + return file.FullName; + } + } + + public Stream GetStream() + { + return new FileStream(file.FullName, FileMode.Open); + } + } +} + diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Loading/LoadableObjectSourceDirectory.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Loading/LoadableObjectSourceDirectory.cs Sat Jul 07 21:01:32 2012 +0100 @@ -0,0 +1,75 @@ +// This file (LoadableObjectSourceDirectory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 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.IO; +using System.Collections.Generic; + +namespace IBBoard.WarFoundry.API.Loading +{ + public class LoadableObjectSourceDirectory : ILoadableObjectSource + { + private DirectoryInfo dir; + + public LoadableObjectSourceDirectory(string directory) : this(new DirectoryInfo(directory)) + { + //Do nothing extra + } + + public LoadableObjectSourceDirectory(DirectoryInfo directory) + { + dir = directory; + } + + public ICollection GetLoadableObjects() + { + ICollection loadables; + dir.Refresh(); + + if (dir.Exists) + { + loadables = GetLoadableObjectsFromDirectory(dir); + } + else + { + loadables = new List(); + } + + return loadables; + } + + private ICollection GetLoadableObjectsFromDirectory(DirectoryInfo directory) + { + List loadables = new List(); + + foreach (FileInfo file in directory.GetFiles()) + { + loadables.Add(new LoadableFileObject(file)); + } + + foreach (DirectoryInfo subdir in directory.GetDirectories()) + { + loadables.AddRange(GetLoadableObjectsFromDirectory(subdir)); + } + + return loadables; + } + + public override bool Equals(object obj) + { + if (obj == null || !obj.GetType().Equals(GetType())) + { + return false; + } + + LoadableObjectSourceDirectory other = (LoadableObjectSourceDirectory)obj; + return this.dir.FullName.Equals(other.dir.FullName); + } + + public override int GetHashCode() + { + return dir.FullName.GetHashCode() + 17; + } + } +} + diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Loading/LoadableObjectSourceResourceSet.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Loading/LoadableObjectSourceResourceSet.cs Sat Jul 07 21:01:32 2012 +0100 @@ -0,0 +1,61 @@ +// This file (LoadableObjectSourceResourceSet.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 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.Reflection; +using System.Collections.Generic; + +namespace IBBoard.WarFoundry.API.Loading +{ + public class LoadableObjectSourceResourceSet : ILoadableObjectSource + { + private ICollection loadables; + private Assembly assm; + private string[] resIDs; + + public LoadableObjectSourceResourceSet(Assembly assembly, params string[] resourceIDs) + { + assm = assembly; + resIDs = resourceIDs; + } + + public ICollection GetLoadableObjects() + { + if (loadables == null) + { + loadables = new List(); + + foreach (string resourceID in resIDs) + { + loadables.Add(new LoadableResourceObject(assm, resourceID)); + } + } + + return loadables; + } + + public override bool Equals(object obj) + { + if (obj == null || !obj.GetType().Equals(GetType())) + { + return false; + } + + LoadableObjectSourceResourceSet other = (LoadableObjectSourceResourceSet)obj; + return this.assm == other.assm && Arrays.Difference(this.resIDs, other.resIDs).Length == 0; + } + + public override int GetHashCode() + { + int hash = assm.GetHashCode(); + + foreach (string resID in resIDs) + { + hash += resID.GetHashCode(); + } + + return hash; + } + } +} + diff -r 81f32062c9fa -r 1ed2f3ab5e35 API/Loading/LoadableResourceObject.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Loading/LoadableResourceObject.cs Sat Jul 07 21:01:32 2012 +0100 @@ -0,0 +1,35 @@ +// This file (LoadableResourceObject.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 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.Reflection; +using System.IO; + +namespace IBBoard.WarFoundry.API.Loading +{ + public class LoadableResourceObject : ILoadableObject + { + private Assembly assm; + private string resID; + + public LoadableResourceObject(Assembly srcAssembly, string resourceID) + { + assm = srcAssembly; + resID = resourceID; + } + + public string Name + { + get + { + return String.Format("{0}: {1}", assm.FullName, resID); + } + } + + public Stream GetStream() + { + return assm.GetManifestResourceStream(resID); + } + } +} + diff -r 81f32062c9fa -r 1ed2f3ab5e35 IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Mon Jun 25 21:04:02 2012 +0100 +++ b/IBBoard.WarFoundry.API.csproj Sat Jul 07 21:01:32 2012 +0100 @@ -33,9 +33,9 @@ true - true + True full - false + False bin\Debug\ DEBUG;TRACE prompt @@ -43,7 +43,7 @@ pdbonly - true + True bin\Release\ TRACE prompt @@ -162,6 +162,12 @@ + + + + + + @@ -197,34 +203,35 @@ + - false + False - false + False - false + False - false + False - false + False - false + False - false + False - false + False - false + False \ No newline at end of file