Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison api/Factories/AbstractNativeWarFoundryFactory.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 | ca2905c9b225 |
children | f00a57369aaa |
comparison
equal
deleted
inserted
replaced
311:5434e648379c | 312:3854c26073c4 |
---|---|
74 | 74 |
75 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); | 75 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); |
76 | 76 |
77 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) | 77 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) |
78 { | 78 { |
79 ICollection<IWarFoundryObject> objects = null; | 79 ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>(); |
80 IWarFoundryObject obj = null; | |
81 | 80 |
82 try | 81 try |
83 { | 82 { |
84 if (CheckCanFindSystemFileContent(file)) | 83 if (CheckCanFindSystemFileContent(file)) |
85 { | 84 { |
86 obj = CreateGameSystemFromFile(file); | 85 AddAll(objects, CreateGameSystemsFromFile(file)); |
87 } | 86 } |
88 else if (CheckCanFindRaceFileContent(file)) | 87 if (CheckCanFindRaceFileContent(file)) |
89 { | 88 { |
90 obj = CreateRaceFromFile(file); | 89 AddAll(objects, CreateRacesFromFile(file)); |
91 } | 90 } |
92 else if (CheckCanFindArmyFileContent(file)) | 91 if (CheckCanFindArmyFileContent(file)) |
93 { | 92 { |
94 obj = CreateArmyFromFile(file); | 93 AddAll(objects, CreateArmiesFromFile(file)); |
95 } | 94 } |
96 } | 95 } |
97 finally | 96 finally |
98 { | 97 { |
99 file.Close(); | 98 file.Close(); |
100 } | 99 } |
101 | 100 |
102 if (obj!=null) | |
103 { | |
104 objects = new List<IWarFoundryObject>(); | |
105 objects.Add(obj); | |
106 } | |
107 | |
108 return objects; | 101 return objects; |
109 } | 102 } |
110 | 103 |
111 protected Army CreateArmyFromFile(ZipFile file) | 104 private void AddAll <NEW_ITEM_TYPE>(ICollection<IWarFoundryObject> collection, ICollection<NEW_ITEM_TYPE> newItems) where NEW_ITEM_TYPE : IWarFoundryObject |
112 { | 105 { |
113 Stream dataStream = GetArmyDataStream(file); | 106 foreach (IWarFoundryObject obj in newItems) |
114 | |
115 try | |
116 { | 107 { |
117 return CreateArmyFromStream(file, dataStream); | 108 collection.Add(obj); |
118 } | |
119 finally | |
120 { | |
121 dataStream.Close(); | |
122 } | 109 } |
123 } | 110 } |
124 | 111 |
125 protected abstract Stream GetArmyDataStream(ZipFile file); | 112 protected ICollection<Army> CreateArmiesFromFile(ZipFile file) |
113 { | |
114 ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file); | |
115 ICollection<Army> armies = new List<Army>(); | |
116 | |
117 foreach (ZipEntry entry in dataStreams) | |
118 { | |
119 using (Stream dataStream = file.GetInputStream(entry)) | |
120 { | |
121 armies.Add(CreateArmyFromStream(file, dataStream)); | |
122 } | |
123 } | |
124 | |
125 return armies; | |
126 } | |
127 | |
128 protected abstract ICollection<ZipEntry> GetArmyZipEntries(ZipFile file); | |
126 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); | 129 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); |
127 | 130 |
128 protected Race CreateRaceFromFile(ZipFile file) | 131 protected ICollection<Race> CreateRacesFromFile(ZipFile file) |
129 { | 132 { |
130 Stream dataStream = GetRaceDataStream(file); | 133 ICollection<ZipEntry> dataStreams = GetRaceZipEntries(file); |
134 ICollection<Race> races = new List<Race>(); | |
131 | 135 |
132 try | 136 foreach (ZipEntry entry in dataStreams) |
133 { | 137 { |
134 return CreateRaceFromStream(file, dataStream); | 138 using (Stream dataStream = file.GetInputStream(entry)) |
139 { | |
140 races.Add(CreateRaceFromStream(file, dataStream)); | |
141 } | |
135 } | 142 } |
136 finally | 143 |
137 { | 144 return races; |
138 dataStream.Close(); | |
139 } | |
140 } | 145 } |
141 | 146 |
142 protected abstract Stream GetRaceDataStream(ZipFile file); | 147 protected abstract ICollection<ZipEntry> GetRaceZipEntries(ZipFile file); |
143 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); | 148 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); |
144 | 149 |
145 protected GameSystem CreateGameSystemFromFile(ZipFile file) | 150 protected ICollection<GameSystem> CreateGameSystemsFromFile(ZipFile file) |
146 { | 151 { |
147 Stream dataStream = GetGameSystemDataStream(file); | 152 ICollection<ZipEntry> dataStreams = GetGameSystemZipEntries(file); |
153 ICollection<GameSystem> systems = new List<GameSystem>(); | |
148 | 154 |
149 try | 155 foreach (ZipEntry entry in dataStreams) |
150 { | 156 { |
151 return CreateGameSystemFromStream(file, dataStream); | 157 using (Stream dataStream = file.GetInputStream(entry)) |
158 { | |
159 systems.Add(CreateGameSystemFromStream(file, dataStream)); | |
160 } | |
152 } | 161 } |
153 finally | 162 |
154 { | 163 return systems; |
155 dataStream.Close(); | |
156 } | |
157 } | 164 } |
158 | 165 |
159 protected abstract Stream GetGameSystemDataStream(ZipFile file); | 166 protected abstract ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file); |
160 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); | 167 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); |
161 | 168 |
162 public override bool Equals (object o) | 169 public override bool Equals (object o) |
163 { | 170 { |
164 if (o == this) | 171 if (o == this) |