Mercurial > repos > snowblizz-super-API-ideas
changeset 19:57451981545c
Re #10 and re #11 - Refactor code and document code
* Split LoadFile method out in to smaller methods
* Document possible exception from IWarFoundryFactory object loading method
* Catch exception in WarFoundryLoader file loading
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 21 Feb 2009 14:39:15 +0000 |
parents | 3c228f093a71 |
children | b7c93a5821cd |
files | api/Factories/IWarFoundryFactory.cs api/WarFoundryLoader.cs |
diffstat | 2 files changed, 45 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Factories/IWarFoundryFactory.cs Sat Feb 21 14:23:53 2009 +0000 +++ b/api/Factories/IWarFoundryFactory.cs Sat Feb 21 14:39:15 2009 +0000 @@ -65,6 +65,7 @@ /// <summary> /// Reads the data from the supplied file and returns it as a collection of loadable objects. + /// May throw a <see cref=" IBBoard.IO.InvalidFileException"/> if the file is supported by the Factory but the content is invalid. /// </summary> /// <param name="file"> /// A <see cref="FileInfo"/> for the file to load data from
--- a/api/WarFoundryLoader.cs Sat Feb 21 14:23:53 2009 +0000 +++ b/api/WarFoundryLoader.cs Sat Feb 21 14:39:15 2009 +0000 @@ -345,9 +345,41 @@ /// </returns> public ICollection<IWarFoundryObject> LoadFile(FileInfo file) { - ICollection<IWarFoundryObject> objs = null; + 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.Message); + } + + if (objs!=null) + { + AddLoadedObjects(objs, loadFactory); + } + else + { + LogNotifier.Debug(GetType(), "Could not load "+file.FullName); + 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"); @@ -370,10 +402,18 @@ } } - if (objs == null) + 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)) @@ -388,17 +428,7 @@ } } } - - if (objs!=null) - { - AddLoadedObjects(objs, loadFactory); - } - else - { - LogNotifier.Debug(GetType(), "Loading of "+file.FullName+" failed"); - objs = new List<IWarFoundryObject>(); - } - + return objs; }