# HG changeset patch # User IBBoard # Date 1315337766 -3600 # Node ID 2a36ebb7b6a9e1aeb40132a2c2e71bbc23634c48 # Parent c70973b65090c99c8237b4474a47e7e2b2ac64f7 Re #358: Handle factory.CreateObjectsFromFile where GetFileAsSupportedType returns null * Refactor out methods for file loading * Add "if zip file not null" check around newly refactored method diff -r c70973b65090 -r 2a36ebb7b6a9 API/Factories/AbstractNativeWarFoundryFactory.cs --- a/API/Factories/AbstractNativeWarFoundryFactory.cs Sun Sep 04 20:43:44 2011 +0100 +++ b/API/Factories/AbstractNativeWarFoundryFactory.cs Tue Sep 06 20:36:06 2011 +0100 @@ -15,6 +15,7 @@ using IBBoard.Xml; using IBBoard.WarFoundry.API.Objects; using ICSharpCode.SharpZipLib.Zip; +using IBBoard.Collections; namespace IBBoard.WarFoundry.API.Factories { @@ -31,9 +32,9 @@ protected override ZipFile GetFileAsSupportedType (FileInfo file) { ZipFile zip = null; - string ext = file.Extension; + string ext = file.Extension.ToLower(); - if (ext == "race" || ext == "army" || ext == "system") + if (ext == ".race" || ext == ".army" || ext == ".system") { try { @@ -81,41 +82,62 @@ { ICollection objects = new List(); + if (file != null) + { + DoCreateObjectsFromZip(file, objects); + } + + return objects; + } + + private void DoCreateObjectsFromZip(ZipFile file, ICollection objects) + { try { - if (CheckCanFindSystemFileContent(file)) - { - foreach (GameSystem system in CreateGameSystemsFromFile(file)) - { - OnGameSystemLoaded(system); - objects.Add(system); - } - } - - if (CheckCanFindRaceFileContent(file)) - { - foreach(Race race in CreateRacesFromFile(file)) - { - OnRaceLoaded(race); - objects.Add(race); - } - } - - if (CheckCanFindArmyFileContent(file)) - { - foreach (Army army in CreateArmiesFromFile(file)) - { - OnArmyLoaded(army); - objects.Add(army); - } - } + CreateSystemObjectsFromZip (file, objects); + CreateRaceObjectsFromZip (file, objects); + CreateArmyObjectsFromZip (file, objects); } finally { file.Close(); } - - return objects; + } + + private void CreateSystemObjectsFromZip (ZipFile file, ICollection objects) + { + if (CheckCanFindSystemFileContent(file)) + { + foreach (GameSystem system in CreateGameSystemsFromFile(file)) + { + OnGameSystemLoaded(system); + objects.Add(system); + } + } + } + + void CreateRaceObjectsFromZip (ZipFile file, ICollection objects) + { + if (CheckCanFindRaceFileContent(file)) + { + foreach(Race race in CreateRacesFromFile(file)) + { + OnRaceLoaded(race); + objects.Add(race); + } + } + } + + void CreateArmyObjectsFromZip (ZipFile file, ICollection objects) + { + if (CheckCanFindArmyFileContent(file)) + { + foreach (Army army in CreateArmiesFromFile(file)) + { + OnArmyLoaded(army); + objects.Add(army); + } + } } protected ICollection CreateArmiesFromFile(ZipFile file)