comparison api/Factories/Xml/WarFoundryXmlFactory.cs @ 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 b9b8b0e60c31
children
comparison
equal deleted inserted replaced
311:5434e648379c 312:3854c26073c4
15 using IBBoard.Logging; 15 using IBBoard.Logging;
16 using IBBoard.Xml; 16 using IBBoard.Xml;
17 using IBBoard.WarFoundry.API.Requirements; 17 using IBBoard.WarFoundry.API.Requirements;
18 using IBBoard.WarFoundry.API.Objects; 18 using IBBoard.WarFoundry.API.Objects;
19 using ICSharpCode.SharpZipLib.Zip; 19 using ICSharpCode.SharpZipLib.Zip;
20 using System.Text.RegularExpressions;
20 21
21 namespace IBBoard.WarFoundry.API.Factories.Xml 22 namespace IBBoard.WarFoundry.API.Factories.Xml
22 { 23 {
23 /// <summary> 24 /// <summary>
24 /// The WarFoundryXmlFactory loads WarFoundry classes from the native "XML in a zip" file format. Files are validated using the schema for the file type, so structurally invalid files should be identified at initial load. 25 /// The WarFoundryXmlFactory loads WarFoundry classes from the native "XML in a zip" file format. Files are validated using the schema for the file type, so structurally invalid files should be identified at initial load.
62 return armyFactory; 63 return armyFactory;
63 } 64 }
64 65
65 protected override bool CheckCanFindArmyFileContent(ZipFile file) 66 protected override bool CheckCanFindArmyFileContent(ZipFile file)
66 { 67 {
67 return file.FindEntry("data.armyx", true) > -1; 68 return FindEntries(file, "*.armyx").Count > 0;
68 } 69 }
69 70
70 protected override bool CheckCanFindSystemFileContent(ZipFile file) 71 protected override bool CheckCanFindSystemFileContent(ZipFile file)
71 { 72 {
72 return file.FindEntry("data.systemx", true) > -1; 73 return FindEntries(file, "*.systemx").Count > 0;
73 } 74 }
74 75
75 protected override bool CheckCanFindRaceFileContent(ZipFile file) 76 protected override bool CheckCanFindRaceFileContent(ZipFile file)
76 { 77 {
77 return file.FindEntry("data.racex", true) > -1; 78 return FindEntries(file, "*.racex").Count > 0;
78 } 79 }
79 80
80 protected override Stream GetArmyDataStream(ZipFile file) 81 protected override ICollection<ZipEntry> GetArmyZipEntries(ZipFile file)
81 { 82 {
82 return file.GetInputStream(file.FindEntry("data.armyx", true)); 83 return FindEntries(file, "*.armyx");
84 }
85
86 private ICollection<ZipEntry> FindEntries(ZipFile file, string wildcardPattern)
87 {
88 Regex re = new Regex("^" + Regex.Escape(wildcardPattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", RegexOptions.IgnoreCase | RegexOptions.Singleline);
89 ICollection<ZipEntry> entries = new List<ZipEntry>();
90
91 foreach (ZipEntry entry in file)
92 {
93 if (re.IsMatch(entry.Name))
94 {
95 entries.Add(entry);
96 }
97 }
98
99 return entries;
83 } 100 }
84 101
85 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) 102 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream)
86 { 103 {
87 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT); 104 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT);
100 } 117 }
101 118
102 return elem; 119 return elem;
103 } 120 }
104 121
105 protected override Stream GetGameSystemDataStream (ZipFile file) 122 protected override ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file)
106 { 123 {
107 return file.GetInputStream(file.FindEntry("data.systemx", true)); 124 return FindEntries(file, "*.systemx");
108 } 125 }
109 126
110 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) 127 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream)
111 { 128 {
112 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT); 129 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT);
113 LogNotifier.Debug(GetType(), "Create GameSystem"); 130 LogNotifier.Debug(GetType(), "Create GameSystem");
114 return gameSystemFactory.CreateSystemFromElement(file, elem); 131 return gameSystemFactory.CreateSystemFromElement(file, elem);
115 } 132 }
116 133
117 protected override Stream GetRaceDataStream (ZipFile file) 134 protected override ICollection<ZipEntry> GetRaceZipEntries(ZipFile file)
118 { 135 {
119 return file.GetInputStream(file.FindEntry("data.racex", true)); 136 return FindEntries(file, "*.racex");
120 } 137 }
121 138
122 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream) 139 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream)
123 { 140 {
124 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); 141 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);