Mercurial > repos > snowblizz-super-API-ideas
changeset 312:3854c26073c4
Re #253: Allow multiple data files in a single zip
* Rebuild file loading to start to allow multiple files in a native file
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 26 Feb 2011 20:15:12 +0000 |
parents | 5434e648379c |
children | f00a57369aaa |
files | api/Factories/AbstractNativeWarFoundryFactory.cs api/Factories/Xml/WarFoundryXmlFactory.cs |
diffstat | 2 files changed, 76 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Factories/AbstractNativeWarFoundryFactory.cs Wed Feb 23 20:44:37 2011 +0000 +++ b/api/Factories/AbstractNativeWarFoundryFactory.cs Sat Feb 26 20:15:12 2011 +0000 @@ -76,22 +76,21 @@ protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) { - ICollection<IWarFoundryObject> objects = null; - IWarFoundryObject obj = null; + ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>(); try { if (CheckCanFindSystemFileContent(file)) { - obj = CreateGameSystemFromFile(file); + AddAll(objects, CreateGameSystemsFromFile(file)); } - else if (CheckCanFindRaceFileContent(file)) + if (CheckCanFindRaceFileContent(file)) { - obj = CreateRaceFromFile(file); + AddAll(objects, CreateRacesFromFile(file)); } - else if (CheckCanFindArmyFileContent(file)) + if (CheckCanFindArmyFileContent(file)) { - obj = CreateArmyFromFile(file); + AddAll(objects, CreateArmiesFromFile(file)); } } finally @@ -99,64 +98,72 @@ file.Close(); } - if (obj!=null) - { - objects = new List<IWarFoundryObject>(); - objects.Add(obj); - } - return objects; } - protected Army CreateArmyFromFile(ZipFile file) + private void AddAll <NEW_ITEM_TYPE>(ICollection<IWarFoundryObject> collection, ICollection<NEW_ITEM_TYPE> newItems) where NEW_ITEM_TYPE : IWarFoundryObject { - Stream dataStream = GetArmyDataStream(file); - - try + foreach (IWarFoundryObject obj in newItems) { - return CreateArmyFromStream(file, dataStream); - } - finally - { - dataStream.Close(); + collection.Add(obj); } } - protected abstract Stream GetArmyDataStream(ZipFile file); - protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); - - protected Race CreateRaceFromFile(ZipFile file) + protected ICollection<Army> CreateArmiesFromFile(ZipFile file) { - Stream dataStream = GetRaceDataStream(file); + ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file); + ICollection<Army> armies = new List<Army>(); - try + foreach (ZipEntry entry in dataStreams) { - return CreateRaceFromStream(file, dataStream); + using (Stream dataStream = file.GetInputStream(entry)) + { + armies.Add(CreateArmyFromStream(file, dataStream)); + } } - finally - { - dataStream.Close(); - } + + return armies; } - protected abstract Stream GetRaceDataStream(ZipFile file); + protected abstract ICollection<ZipEntry> GetArmyZipEntries(ZipFile file); + protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); + + protected ICollection<Race> CreateRacesFromFile(ZipFile file) + { + ICollection<ZipEntry> dataStreams = GetRaceZipEntries(file); + ICollection<Race> races = new List<Race>(); + + foreach (ZipEntry entry in dataStreams) + { + using (Stream dataStream = file.GetInputStream(entry)) + { + races.Add(CreateRaceFromStream(file, dataStream)); + } + } + + return races; + } + + protected abstract ICollection<ZipEntry> GetRaceZipEntries(ZipFile file); protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); - protected GameSystem CreateGameSystemFromFile(ZipFile file) + protected ICollection<GameSystem> CreateGameSystemsFromFile(ZipFile file) { - Stream dataStream = GetGameSystemDataStream(file); + ICollection<ZipEntry> dataStreams = GetGameSystemZipEntries(file); + ICollection<GameSystem> systems = new List<GameSystem>(); - try + foreach (ZipEntry entry in dataStreams) { - return CreateGameSystemFromStream(file, dataStream); + using (Stream dataStream = file.GetInputStream(entry)) + { + systems.Add(CreateGameSystemFromStream(file, dataStream)); + } } - finally - { - dataStream.Close(); - } + + return systems; } - protected abstract Stream GetGameSystemDataStream(ZipFile file); + protected abstract ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file); protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); public override bool Equals (object o)
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs Wed Feb 23 20:44:37 2011 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs Sat Feb 26 20:15:12 2011 +0000 @@ -17,6 +17,7 @@ using IBBoard.WarFoundry.API.Requirements; using IBBoard.WarFoundry.API.Objects; using ICSharpCode.SharpZipLib.Zip; +using System.Text.RegularExpressions; namespace IBBoard.WarFoundry.API.Factories.Xml { @@ -64,22 +65,38 @@ protected override bool CheckCanFindArmyFileContent(ZipFile file) { - return file.FindEntry("data.armyx", true) > -1; + return FindEntries(file, "*.armyx").Count > 0; } protected override bool CheckCanFindSystemFileContent(ZipFile file) { - return file.FindEntry("data.systemx", true) > -1; + return FindEntries(file, "*.systemx").Count > 0; } protected override bool CheckCanFindRaceFileContent(ZipFile file) { - return file.FindEntry("data.racex", true) > -1; + return FindEntries(file, "*.racex").Count > 0; + } + + protected override ICollection<ZipEntry> GetArmyZipEntries(ZipFile file) + { + return FindEntries(file, "*.armyx"); } - protected override Stream GetArmyDataStream(ZipFile file) + private ICollection<ZipEntry> FindEntries(ZipFile file, string wildcardPattern) { - return file.GetInputStream(file.FindEntry("data.armyx", true)); + Regex re = new Regex("^" + Regex.Escape(wildcardPattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", RegexOptions.IgnoreCase | RegexOptions.Singleline); + ICollection<ZipEntry> entries = new List<ZipEntry>(); + + foreach (ZipEntry entry in file) + { + if (re.IsMatch(entry.Name)) + { + entries.Add(entry); + } + } + + return entries; } protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) @@ -102,9 +119,9 @@ return elem; } - protected override Stream GetGameSystemDataStream (ZipFile file) + protected override ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file) { - return file.GetInputStream(file.FindEntry("data.systemx", true)); + return FindEntries(file, "*.systemx"); } protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) @@ -114,9 +131,9 @@ return gameSystemFactory.CreateSystemFromElement(file, elem); } - protected override Stream GetRaceDataStream (ZipFile file) + protected override ICollection<ZipEntry> GetRaceZipEntries(ZipFile file) { - return file.GetInputStream(file.FindEntry("data.racex", true)); + return FindEntries(file, "*.racex"); } protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream)