Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/WarFoundryLoader.cs @ 15:306558904c2a
Re #1 - LGPL license all code
* Add LGPL header to all files
* Add COPYING.LGPL and COPYING.GPL with content of license
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 25 Jan 2009 14:50:36 +0000 |
parents | 0770e5cbba7c |
children | bf518618eb69 |
rev | line source |
---|---|
15 | 1 // This file (WarFoundryLoader.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. |
0 | 2 // |
15 | 3 // The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license. |
0 | 4 |
5 using System; | |
6 using System.Collections.Generic; | |
7 using System.IO; | |
8 using IBBoard.Collections; | |
9 using IBBoard.IO; | |
10 using IBBoard.Logging; | |
11 using IBBoard.WarFoundry.API.Factories; | |
12 using IBBoard.WarFoundry.API.Objects; | |
13 using ICSharpCode.SharpZipLib.Zip; | |
14 | |
15 namespace IBBoard.WarFoundry.API | |
16 { | |
17 public class WarFoundryLoader | |
18 { | |
19 private static WarFoundryLoader loader; | |
20 | |
21 /// <summary> | |
22 /// Gets the default <see cref="WarFoundryLoader"/> used to load WarFoundry data files. | |
23 /// </summary> | |
24 /// <returns> | |
25 /// The default <see cref="WarFoundryLoader"/> | |
26 /// </returns> | |
27 public static WarFoundryLoader GetDefault() | |
28 { | |
29 if (loader == null) | |
30 { | |
31 loader = new WarFoundryLoader(); | |
32 } | |
33 | |
34 return loader; | |
35 } | |
36 | |
37 private ICollection<DirectoryInfo> directories; | |
38 private ICollection<INativeWarFoundryFactory> factories; | |
39 private ICollection<INonNativeWarFoundryFactory> nonNativeFactories; | |
40 private Dictionary<string, GameSystem> systemsTable; | |
41 private Dictionary<string, Dictionary<string, Dictionary<string, Race>>> racesTable; //Keys are: System, Race, SubRace | |
42 private Dictionary<IWarFoundryFactory, SimpleSet<IWarFoundryObject>> loadedObjects; | |
43 | |
44 protected WarFoundryLoader() | |
45 { | |
46 directories = new List<DirectoryInfo>(); | |
47 factories = new List<INativeWarFoundryFactory>(); | |
48 nonNativeFactories = new List<INonNativeWarFoundryFactory>(); | |
49 loadedObjects = new Dictionary<IWarFoundryFactory,SimpleSet<IWarFoundryObject>>(); | |
50 } | |
51 | |
52 /// <summary> | |
53 /// Adds a directory to the collection of directories that will be searched for WarFoundry data files. | |
54 /// </summary> | |
55 /// <param name="directory"> | |
56 /// The <see cref="DirectoryInfo"/> to add to the list for searching for data files | |
57 /// </param> | |
58 public void AddLoadDirectory(DirectoryInfo directory) | |
59 { | |
60 if (!directories.Contains(directory)) | |
61 { | |
62 directories.Add(directory); | |
63 } | |
64 } | |
65 | |
66 /// <summary> | |
67 /// Removes a directory from the collection of directories that will be searched for WarFoundry data files. | |
68 /// </summary> | |
69 /// <param name="directory"> | |
70 /// A <see cref="DirectoryInfo"/> | |
71 /// </param> | |
72 public void RemoveLoadDirectory(DirectoryInfo directory) | |
73 { | |
74 if (directories.Contains(directory)) | |
75 { | |
76 directories.Remove(directory); | |
77 } | |
78 } | |
79 | |
80 /// <summary> | |
81 /// Registers a <see cref="INativeWarFoundryFactory"/> as a factory that can parse native data files. | |
82 /// </summary> | |
83 /// <param name="factory"> | |
84 /// The <see cref="INativeWarFoundryFactory"/> to register to parse native data files. | |
85 /// </param> | |
86 public void RegisterFactory(INativeWarFoundryFactory factory) | |
87 { | |
88 if (!factories.Contains(factory)) | |
89 { | |
90 factories.Add(factory); | |
91 } | |
92 } | |
93 | |
94 /// <summary> | |
95 /// Unregisters a <see cref="INativeWarFoundryFactory"/> so that it will no longer be used to try to parse native data files. | |
96 /// </summary> | |
97 /// <param name="factory"> | |
98 /// The <see cref="INativeWarFoundryFactory"/> to remove from the collection of factories that are used to try to parse native data files. | |
99 /// </param> | |
100 public void UnregisterFactory(INativeWarFoundryFactory factory) | |
101 { | |
102 if (factories.Contains(factory)) | |
103 { | |
104 factories.Remove(factory); | |
105 } | |
106 } | |
107 | |
108 /// <summary> | |
109 /// Registers a <see cref="INonNativeWarFoundryFactory"/> so that it will be used to try to parse non-native data files from other applications. | |
110 /// </summary> | |
111 /// <param name="factory"> | |
112 /// The <see cref="INonNativeWarFoundryFactory"/> to register to parse non-native data files. | |
113 /// </param> | |
114 public void RegisterNonNativeFactory(INonNativeWarFoundryFactory factory) | |
115 { | |
116 if (!nonNativeFactories.Contains(factory)) | |
117 { | |
118 nonNativeFactories.Add(factory); | |
119 } | |
120 } | |
121 | |
122 /// <summary> | |
123 /// Unregisters a <see cref="INonNativeWarFoundryFactory"/> so that it will no longer be used to try to parse non-native data files from other applications. | |
124 /// </summary> | |
125 /// <param name="factory"> | |
126 /// The <see cref="INonNativeWarFoundryFactory"/> to remove from the collection of factories that are used to try to parse non-native data files. | |
127 /// </param> | |
128 public void UnregisterNonNativeFactory(INonNativeWarFoundryFactory factory) | |
129 { | |
130 if (nonNativeFactories.Contains(factory)) | |
131 { | |
132 nonNativeFactories.Remove(factory); | |
133 } | |
134 } | |
135 | |
136 /// <summary> | |
137 /// Loads all of the data files in the registered directories. | |
138 /// </summary> | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
139 /// <returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
140 /// A <see cref="Dictionary"/> of files that failed to load mapped against the message that their failure returned |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
141 /// </returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
142 public List<FileLoadFailure> LoadFiles() |
0 | 143 { |
144 LogNotifier.Debug(GetType(), "Load files"); | |
145 PrepareForFileLoad(); | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
146 Dictionary<FileInfo, IWarFoundryFactory> loadableRaces = new Dictionary<FileInfo, IWarFoundryFactory>(); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
147 Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems = new Dictionary<FileInfo, IWarFoundryFactory>(); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
148 List<FileLoadFailure> failedLoads = FillLoadableFiles(loadableRaces, loadableGameSystems); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
149 failedLoads.AddRange(LoadGameSystems(loadableGameSystems)); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
150 failedLoads.AddRange(LoadRaces(loadableRaces)); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
151 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
152 LogNotifier.Debug(GetType(), failedLoads.Count + " failed file loads"); |
0 | 153 |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
154 return failedLoads; |
0 | 155 } |
156 | |
157 protected void PrepareForFileLoad() | |
158 { | |
159 //Just set up blank dictionaries for now - may try different and more complex handling in future | |
160 systemsTable = new Dictionary<string,GameSystem>(); | |
161 racesTable = new Dictionary<string,Dictionary<string,Dictionary<string,Race>>>(); | |
162 } | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
163 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
164 private List<FileLoadFailure> FillLoadableFiles(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
165 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
166 List<FileLoadFailure> fails = new List<FileLoadFailure>(); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
167 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
168 foreach (DirectoryInfo directory in directories) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
169 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
170 if (directory.Exists) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
171 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
172 List<FileLoadFailure> directoryFails = FillLoadableFilesForDirectory(loadableRaces, loadableGameSystems, directory); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
173 fails.AddRange(directoryFails); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
174 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
175 else |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
176 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
177 LogNotifier.WarnFormat(GetType(), "Load for {0} failed because directory didn't exist", directory.FullName); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
178 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
179 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
180 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
181 return fails; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
182 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
183 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
184 private List<FileLoadFailure> FillLoadableFilesForDirectory(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems, DirectoryInfo directory) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
185 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
186 List<FileLoadFailure> fails = new List<FileLoadFailure>(); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
187 LogNotifier.Debug(GetType(), "Load from "+directory.FullName); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
188 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
189 foreach (FileInfo file in directory.GetFiles()) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
190 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
191 IWarFoundryFactory factory = GetGameSystemRaceLoadingFactoryForFile(file); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
192 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
193 if (factory != null) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
194 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
195 loadableGameSystems.Add(file, factory); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
196 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
197 else |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
198 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
199 factory = GetRaceLoadingFactoryForFile(file); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
200 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
201 if (factory!=null) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
202 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
203 loadableRaces.Add(file, factory); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
204 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
205 else |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
206 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
207 fails.Add(new FileLoadFailure(file, "FileNotHandled", "File not handled as a Race or Game System definition: {0}")); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
208 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
209 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
210 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
211 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
212 return fails; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
213 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
214 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
215 private IWarFoundryFactory GetGameSystemRaceLoadingFactoryForFile(FileInfo file) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
216 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
217 IWarFoundryFactory loadingFactory = null; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
218 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
219 foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
220 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
221 if (factory.CanHandleFileAsGameSystem(file)) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
222 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
223 loadingFactory = factory; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
224 break; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
225 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
226 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
227 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
228 if (loadingFactory == null) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
229 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
230 foreach (INativeWarFoundryFactory factory in factories) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
231 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
232 if (factory.CanHandleFileAsGameSystem(file)) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
233 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
234 loadingFactory = factory; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
235 break; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
236 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
237 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
238 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
239 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
240 return loadingFactory; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
241 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
242 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
243 private IWarFoundryFactory GetRaceLoadingFactoryForFile(FileInfo file) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
244 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
245 IWarFoundryFactory loadingFactory = null; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
246 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
247 foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
248 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
249 if (factory.CanHandleFileAsRace(file)) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
250 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
251 loadingFactory = factory; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
252 break; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
253 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
254 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
255 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
256 if (loadingFactory == null) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
257 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
258 foreach (INativeWarFoundryFactory factory in factories) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
259 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
260 if (factory.CanHandleFileAsRace(file)) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
261 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
262 loadingFactory = factory; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
263 break; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
264 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
265 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
266 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
267 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
268 return loadingFactory; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
269 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
270 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
271 private List<FileLoadFailure> LoadGameSystems(Dictionary<FileInfo, IWarFoundryFactory> gameSystemFiles) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
272 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
273 List<FileLoadFailure> fails = new List<FileLoadFailure>(); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
274 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
275 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
276 foreach (FileInfo file in gameSystemFiles.Keys) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
277 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
278 try |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
279 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
280 bool loaded = LoadObject(file, gameSystemFiles[file]); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
281 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
282 if (!loaded) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
283 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
284 fails.Add(new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}")); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
285 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
286 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
287 catch (Exception ex) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
288 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
289 fails.Add(new FileLoadFailure(file, ex.Message)); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
290 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
291 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
292 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
293 return fails; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
294 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
295 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
296 private List<FileLoadFailure> LoadRaces(Dictionary<FileInfo, IWarFoundryFactory> raceFiles) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
297 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
298 List<FileLoadFailure> fails = new List<FileLoadFailure>(); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
299 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
300 foreach (FileInfo file in raceFiles.Keys) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
301 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
302 try |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
303 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
304 bool loaded = LoadObject(file, raceFiles[file]); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
305 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
306 if (!loaded) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
307 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
308 fails.Add(new FileLoadFailure(file, "FileLoadFailed", "Failed to load {0} as Race using {1}")); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
309 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
310 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
311 catch (Exception ex) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
312 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
313 fails.Add(new FileLoadFailure(file, ex.Message)); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
314 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
315 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
316 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
317 return fails; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
318 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
319 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
320 private bool LoadObject(FileInfo file, IWarFoundryFactory factory) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
321 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
322 bool loaded = false; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
323 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
324 LogNotifier.DebugFormat(GetType(), "Loading {0} using {1}", file.FullName, factory.GetType().Name); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
325 ICollection<IWarFoundryObject> objects = factory.CreateObjectsFromFile(file); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
326 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
327 if (objects.Count > 0) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
328 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
329 AddLoadedObjects(objects, factory); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
330 loaded = true; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
331 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
332 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
333 return loaded; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
334 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
335 |
0 | 336 |
337 /// <summary> | |
338 /// Loads a single file through the registered WarFoundryFactories, if a factory exists that supports the file format. | |
339 /// </summary> | |
340 /// <param name="file"> | |
341 /// A <see cref="FileInfo"/> for the file to attempt to load | |
342 /// </param> | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
343 /// <returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
344 /// An ICollection of IWarFoundryObjects loaded from <code>file</code> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
345 /// </returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
346 public ICollection<IWarFoundryObject> LoadFile(FileInfo file) |
0 | 347 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
348 ICollection<IWarFoundryObject> objs = null; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
349 IWarFoundryFactory loadFactory = null; |
0 | 350 |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
351 if (nonNativeFactories.Count > 0) |
0 | 352 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
353 LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as a non-native file"); |
0 | 354 |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
355 foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) |
0 | 356 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
357 bool canLoad = factory.CanHandleFileFormat(file); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
358 LogNotifier.Debug(GetType(), "Load using "+factory.GetType().FullName+"? " + (canLoad ? "yes" : "no")); |
0 | 359 |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
360 if (canLoad) |
0 | 361 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
362 objs = factory.CreateObjectsFromFile(file); |
0 | 363 |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
364 if (objs!=null) |
0 | 365 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
366 loadFactory = factory; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
367 break; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
368 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
369 } |
0 | 370 } |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
371 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
372 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
373 if (objs == null) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
374 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
375 LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as native file"); |
0 | 376 |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
377 foreach (INativeWarFoundryFactory factory in factories) |
0 | 378 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
379 if (factory.CanHandleFileFormat(file)) |
0 | 380 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
381 objs = factory.CreateObjectsFromFile(file); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
382 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
383 if (objs!=null) |
0 | 384 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
385 loadFactory = factory; |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
386 break; |
0 | 387 } |
388 } | |
389 } | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
390 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
391 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
392 if (objs!=null) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
393 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
394 AddLoadedObjects(objs, loadFactory); |
0 | 395 } |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
396 else |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
397 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
398 LogNotifier.Debug(GetType(), "Loading of "+file.FullName+" failed"); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
399 objs = new List<IWarFoundryObject>(); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
400 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
401 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
402 return objs; |
0 | 403 } |
404 | |
405 private void AddLoadedObjects(ICollection<IWarFoundryObject> loadedObjs, IWarFoundryFactory factory) | |
406 { | |
407 SimpleSet<IWarFoundryObject> objs; | |
408 loadedObjects.TryGetValue(factory, out objs); | |
409 | |
410 if (objs == null) | |
411 { | |
412 objs = new SimpleSet<IWarFoundryObject>(); | |
413 loadedObjects.Add(factory, objs); | |
414 } | |
415 | |
416 objs.AddRange(loadedObjs); | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
417 StoreObjects(loadedObjs); |
0 | 418 } |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
419 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
420 private void StoreObjects(ICollection<IWarFoundryObject> loadedObjects) |
0 | 421 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
422 foreach (IWarFoundryObject loadedObject in loadedObjects) |
0 | 423 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
424 if (loadedObject is GameSystem) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
425 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
426 StoreGameSystem((GameSystem)loadedObject); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
427 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
428 else if (loadedObject is Race) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
429 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
430 StoreRace((Race)loadedObject); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
431 } |
0 | 432 } |
433 } | |
434 | |
435 protected virtual ZipFile MakeZipFile(FileInfo file) | |
436 { | |
437 return new ZipFile(file.FullName); | |
438 } | |
439 | |
440 protected void StoreGameSystem(GameSystem system) | |
441 { | |
442 string sysid = system.ID.ToLower(); | |
443 | |
444 if (systemsTable.ContainsKey(sysid)) | |
445 { | |
446 LogNotifier.WarnFormat(GetType(), "System {0} ({1}) has already been loaded. Duplicate file ({3}) discarded", system.Name, system.ID, system.SourceFile.FullName); | |
447 } | |
448 else | |
449 { | |
450 systemsTable.Add(sysid, (GameSystem)system); | |
451 } | |
452 } | |
453 | |
454 protected void StoreRace(Race race) | |
455 { | |
456 Dictionary<string, Dictionary<string, Race>> systemRaces; | |
457 | |
458 if (race.GameSystem == null) | |
459 { | |
460 throw new InvalidOperationException("Race cannot have null game system. Game system should be loaded before race."); | |
461 } | |
462 | |
463 string systemID = race.GameSystem.ID.ToLower(); | |
464 racesTable.TryGetValue(systemID, out systemRaces); | |
465 | |
466 if (systemRaces==null) | |
467 { | |
468 systemRaces = new Dictionary<string,Dictionary<string,Race>>(); | |
469 racesTable.Add(systemID, systemRaces); | |
470 } | |
471 | |
472 Dictionary<string, Race> subRaces; | |
473 systemRaces.TryGetValue(race.ID.ToLower(), out subRaces); | |
474 | |
475 if (subRaces==null) | |
476 { | |
477 subRaces = new Dictionary<string,Race>(); | |
478 systemRaces.Add(race.ID.ToLower(), subRaces); | |
479 } | |
480 | |
481 if (subRaces.ContainsKey(race.SubID.ToLower())) | |
482 { | |
483 LogNotifier.WarnFormat(GetType(), "Race {0} ({1} - {2}) for system {3} ({4}) has already been loaded. Duplicate file ({5}) discarded", race.Name, race.ID, race.SubID, race.GameSystem.ID, race.GameSystem.Name, race.SourceFile.Name); | |
484 race = null; | |
485 } | |
486 else | |
487 { | |
488 subRaces.Add(race.SubID.ToLower(), race); | |
489 } | |
490 } | |
491 | |
492 /// <summary> | |
493 /// Gets all <see cref="GameSystem"/>s that are currently available, determined by those that can be loaded with the current <see cref="IWarFoundryFactory"/>s. | |
494 /// </summary> | |
495 /// <returns> | |
496 /// An array of <see cref="GameSystem"/>s that are currently available. | |
497 /// </returns> | |
498 public GameSystem[] GetGameSystems() | |
499 { | |
500 if (systemsTable==null) | |
501 { | |
502 LoadFiles(); | |
503 } | |
504 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
505 return DictionaryUtils.ToArray<string, GameSystem>(systemsTable); |
0 | 506 } |
507 | |
508 /// <summary> | |
509 /// Gets a single <see cref="GameSystem"/> with a given ID. | |
510 /// </summary> | |
511 /// <param name="systemID"> | |
512 /// The ID of the <see cref="GameSystem"/> to get, as a <see cref="System.String"/>. | |
513 /// </param> | |
514 /// <returns> | |
515 /// The <see cref="GameSystem"/> with the given ID, or <code>null</code> if one doesn't exist. | |
516 /// </returns> | |
517 public GameSystem GetGameSystem(string systemID) | |
518 { | |
519 if (systemsTable==null) | |
520 { | |
521 LoadFiles(); | |
522 } | |
523 | |
524 GameSystem system; | |
525 systemsTable.TryGetValue(systemID.ToLower(), out system); | |
526 return system; | |
527 } | |
528 | |
529 /// <summary> | |
530 /// Gets an array of the races for the specified <see cref="GameSystem"/>. | |
531 /// </summary> | |
532 /// <param name="system"> | |
533 /// The <see cref="GameSystem"/> to get the available races for. | |
534 /// </param> | |
535 /// <returns> | |
536 /// An array of <see cref="Race"/>s for the <see cref="GameSystem"/> | |
537 /// </returns> | |
538 public Race[] GetRaces(GameSystem system) | |
539 { | |
540 return GetRaces(system.ID); | |
541 } | |
542 | |
543 /// <summary> | |
544 /// Gets an array of the races for a game system by ID. | |
545 /// </summary> | |
546 /// <param name="systemID"> | |
547 /// The <see cref="System.String"/> ID of the game system to get races for | |
548 /// </param> | |
549 /// <returns> | |
550 /// An array of <see cref="Race"/>s for the specified game system | |
551 /// </returns> | |
552 public Race[] GetRaces(string systemID) | |
553 { | |
554 if (racesTable==null) | |
555 { | |
556 LoadFiles(); | |
557 } | |
558 | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
5
diff
changeset
|
559 systemID = systemID.ToLower(); |
0 | 560 Dictionary<string, Dictionary<string, Race>> system; |
561 racesTable.TryGetValue(systemID, out system); | |
562 | |
563 if (system==null) | |
564 { | |
565 return new Race[0]; | |
566 } | |
567 | |
568 int count = 0; | |
569 | |
570 foreach (Dictionary<string, Race> racesDict in system.Values) | |
571 { | |
572 count+= racesDict.Count; | |
573 } | |
574 | |
575 Race[] races = new Race[count]; | |
576 int i = 0; | |
577 | |
578 foreach (string raceID in system.Keys) | |
579 { | |
580 foreach (string raceSubId in system[raceID].Keys) | |
581 { | |
582 races[i++] = GetRace(systemID, raceID, raceSubId); | |
583 } | |
584 } | |
585 | |
586 return races; | |
587 } | |
588 | |
589 /// <summary> | |
590 /// Gets a single race for a given <see cref="GameSystem"/> by ID of the race. | |
591 /// </summary> | |
592 /// <param name="system"> | |
593 /// The <see cref="GameSystem"/> that the race is part of. | |
594 /// </param> | |
595 /// <param name="raceID"> | |
596 /// A <see cref="System.String"/> ID for the race to load. | |
597 /// </param> | |
598 /// <returns> | |
599 /// A <see cref="Race"/> with the specified ID from the <see cref="GameSystem"/>, or <code>null</code> if one doesn't exist. | |
600 /// </returns> | |
601 public Race GetRace(GameSystem system, string raceID) | |
602 { | |
603 return GetRace(system.ID, raceID); | |
604 } | |
605 | |
606 /// <summary> | |
607 /// Gets a single race for a given game system by ID of the game system and race. | |
608 /// </summary> | |
609 /// <param name="systemID"> | |
610 /// The <see cref="System.String"/> ID of the game system that the race is part of. | |
611 /// </param> | |
612 /// <param name="raceID"> | |
613 /// The <see cref="System.String"/> ID for the race to load. | |
614 /// </param> | |
615 /// <returns> | |
616 /// A <see cref="Race"/> with the specified ID from the game system with the specified ID, or <code>null</code> if there is no race or game system with those IDs. | |
617 /// </returns> | |
618 public Race GetRace(string systemID, string raceID) | |
619 { | |
620 return GetRace(systemID, raceID, ""); | |
621 } | |
622 | |
623 /// <summary> | |
624 /// Gets a single race for a given <see cref="GameSystem"/> by the race's ID and sub-race ID. | |
625 /// </summary> | |
626 /// <param name="system"> | |
627 /// The <see cref="GameSystem"/> that the race is part of. | |
628 /// </param> | |
629 /// <param name="raceID"> | |
630 /// The <see cref="System.String"/> ID for the race to load. | |
631 /// </param> | |
632 /// <param name="raceSubID"> | |
633 /// A <see cref="System.String"/> | |
634 /// </param> | |
635 /// <returns> | |
636 /// A <see cref="Race"/> | |
637 /// </returns> | |
638 public Race GetRace(GameSystem system, string raceID, string raceSubID) | |
639 { | |
640 return GetRace(system.ID, raceID, raceSubID); | |
641 } | |
642 | |
643 /// <summary> | |
644 /// Gets a single race for a given game system by the game system's ID and the race's ID and sub-race ID. | |
645 /// </summary> | |
646 /// <param name="systemID"> | |
647 /// The <see cref="System.String"/> ID of the game system that the race is part of. | |
648 /// </param> | |
649 /// <param name="raceID"> | |
650 /// The <see cref="System.String"/> ID for the race to load. | |
651 /// </param> | |
652 /// <param name="raceSubID"> | |
653 /// A <see cref="System.String"/> | |
654 /// </param> | |
655 /// <returns> | |
656 /// A <see cref="Race"/> | |
657 /// </returns> | |
658 public Race GetRace(string systemID, string raceID, string raceSubID) | |
659 { | |
660 if (racesTable==null) | |
661 { | |
662 LoadFiles(); | |
663 } | |
664 | |
665 Race race = null; | |
666 | |
667 systemID = systemID.ToLower(); | |
668 | |
669 Dictionary<string, Dictionary<string, Race>> races; | |
670 racesTable.TryGetValue(systemID, out races); | |
671 | |
672 if (races!=null) | |
673 { | |
674 Dictionary<string, Race> subraces; | |
675 races.TryGetValue(raceID, out subraces); | |
676 | |
677 if (subraces!=null) | |
678 { | |
679 subraces.TryGetValue(raceSubID, out race); | |
680 } | |
681 } | |
682 | |
683 return race; | |
684 } | |
685 | |
686 /// <summary> | |
687 /// Gets the IDs of all of the game systems currently available. | |
688 /// </summary> | |
689 /// <returns> | |
690 /// An array of <see cref="System.String"/>s representing the IDs of the game systems. | |
691 /// </returns> | |
692 public string[] GetGameSystemIDs() | |
693 { | |
694 if (systemsTable==null) | |
695 { | |
696 LoadFiles(); | |
697 } | |
698 | |
699 string[] keys = new string[systemsTable.Keys.Count]; | |
700 int i = 0; | |
701 | |
702 foreach (string key in systemsTable.Keys) | |
703 { | |
704 keys[i++] = key; | |
705 } | |
706 | |
707 return keys; | |
708 } | |
709 | |
710 /// <summary> | |
711 /// Gets the IDs of all of the races of a specified game system. | |
712 /// </summary> | |
713 /// <param name="system"> | |
714 /// The <see cref="GameSystem"/> to get the available races for. | |
715 /// </param> | |
716 /// <returns> | |
717 /// An array of <see cref="System.String"/>s representing the IDs of the races of the specified game system. | |
718 /// </returns> | |
719 public string[] GetSystemRaceIDs(GameSystem system) | |
720 { | |
721 return GetSystemRaceIDs(system.ID); | |
722 } | |
723 | |
724 /// <summary> | |
725 /// Gets the IDs of all of the races of a specified game system. | |
726 /// </summary> | |
727 /// <param name="systemID"> | |
728 /// The <see cref="System.String"/> ID of the game system to get the available races for. | |
729 /// </param> | |
730 /// <returns> | |
731 /// An array of <see cref="System.String"/>s representing the IDs of the races of the specified game system. | |
732 /// </returns> | |
733 public string[] GetSystemRaceIDs(string systemID) | |
734 { | |
735 if (racesTable == null) | |
736 { | |
737 LoadFiles(); | |
738 } | |
739 | |
740 Dictionary<string, Dictionary<string, Race>> races = racesTable[systemID.ToLower()]; | |
741 | |
742 if (races==null) | |
743 { | |
744 return new string[0]; | |
745 } | |
746 else | |
747 { | |
748 string[] keys = new string[races.Keys.Count]; | |
749 int i = 0; | |
750 | |
751 foreach (string key in races.Keys) | |
752 { | |
753 keys[i++] = key; | |
754 } | |
755 | |
756 return keys; | |
757 } | |
758 } | |
759 } | |
760 } |