Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison api/DefaultWarFoundryLoader.cs @ 233:a36a0e9cc05d
Re #228: Crash with missing abilityID
* Separate out the actual loader implementation from the static "WarFoundryLoader" class
* Add a setter method for the current loader
* Create an abstract and default implementation of the Loader to reduce coupling and allow easier mocking/testing
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Thu, 24 Dec 2009 19:45:39 +0000 |
parents | |
children | 5ed39187b0e3 |
comparison
equal
deleted
inserted
replaced
232:f5009a00a50d | 233:a36a0e9cc05d |
---|---|
1 // This file (DefaultWarFoundryLoader.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard | |
2 // | |
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. | |
4 | |
5 using System; | |
6 using System.Collections.Generic; | |
7 using IBBoard.WarFoundry.API.Objects; | |
8 | |
9 namespace IBBoard.WarFoundry.API | |
10 { | |
11 /// <summary> | |
12 /// The default implementation of a <see cref="AbstractWarFoundryLoader"/> | |
13 /// </summary> | |
14 public class DefaultWarFoundryLoader : AbstractWarFoundryLoader | |
15 { | |
16 private Dictionary<string, GameSystem> systemsTable; | |
17 private Dictionary<string, Dictionary<string, Dictionary<string, Race>>> racesTable; //Keys are: System, Race, SubRace | |
18 | |
19 public DefaultWarFoundryLoader () | |
20 { | |
21 } | |
22 | |
23 protected override void PrepareForFileLoad() | |
24 { | |
25 //Just set up blank dictionaries for now - may try different and more complex handling in future | |
26 systemsTable = new Dictionary<string,GameSystem>(); | |
27 racesTable = new Dictionary<string,Dictionary<string,Dictionary<string,Race>>>(); | |
28 } | |
29 | |
30 protected override GameSystem GetExistingSystemForSystem (GameSystem system) | |
31 { | |
32 return DictionaryUtils.GetValue(systemsTable, system.ID.ToLower()); | |
33 } | |
34 | |
35 protected override void DoStoreGameSystem (GameSystem system) | |
36 { | |
37 systemsTable[system.ID.ToLower()] = system; | |
38 } | |
39 | |
40 protected override void DoStoreRace (Race race) | |
41 { | |
42 Dictionary<string, Dictionary<string, Race>> systemRaces; | |
43 | |
44 string systemID = race.GameSystem.ID.ToLower(); | |
45 racesTable.TryGetValue(systemID, out systemRaces); | |
46 | |
47 if (systemRaces==null) | |
48 { | |
49 systemRaces = new Dictionary<string,Dictionary<string,Race>>(); | |
50 racesTable.Add(systemID, systemRaces); | |
51 } | |
52 | |
53 Dictionary<string, Race> subRaces; | |
54 systemRaces.TryGetValue(race.ID.ToLower(), out subRaces); | |
55 | |
56 if (subRaces==null) | |
57 { | |
58 subRaces = new Dictionary<string,Race>(); | |
59 systemRaces.Add(race.ID.ToLower(), subRaces); | |
60 } | |
61 | |
62 string subID = race.SubID.ToLower(); | |
63 | |
64 if (subRaces.ContainsKey(subID)) | |
65 { | |
66 Race existingRace = subRaces[subID]; | |
67 | |
68 if (!race.Equals(existingRace)) | |
69 { | |
70 //TODO: Raise an event to say we got a different duplicate | |
71 //We can't just fail, because failing is for completely unhandled files, not for objects in a file | |
72 } | |
73 } | |
74 else | |
75 { | |
76 subRaces.Add(race.SubID.ToLower(), race); | |
77 } | |
78 } | |
79 | |
80 public override GameSystem[] GetGameSystems() | |
81 { | |
82 if (systemsTable==null) | |
83 { | |
84 LoadFiles(); | |
85 } | |
86 | |
87 return DictionaryUtils.ToArray<string, GameSystem>(systemsTable); | |
88 } | |
89 | |
90 public override GameSystem GetGameSystem(string systemID) | |
91 { | |
92 if (systemsTable==null) | |
93 { | |
94 LoadFiles(); | |
95 } | |
96 | |
97 GameSystem system; | |
98 systemsTable.TryGetValue(systemID.ToLower(), out system); | |
99 return system; | |
100 } | |
101 | |
102 protected internal override void RemoveGameSystem(GameSystem system) | |
103 { | |
104 systemsTable.Remove(system.ID.ToLower()); | |
105 } | |
106 | |
107 public override Race[] GetRaces(GameSystem system) | |
108 { | |
109 return GetRaces(system.ID); | |
110 } | |
111 | |
112 /// <summary> | |
113 /// Gets an array of the races for a game system by ID. | |
114 /// </summary> | |
115 /// <param name="systemID"> | |
116 /// The <see cref="System.String"/> ID of the game system to get races for | |
117 /// </param> | |
118 /// <returns> | |
119 /// An array of <see cref="Race"/>s for the specified game system | |
120 /// </returns> | |
121 public Race[] GetRaces(string systemID) | |
122 { | |
123 if (racesTable==null) | |
124 { | |
125 LoadFiles(); | |
126 } | |
127 | |
128 systemID = systemID.ToLower(); | |
129 Dictionary<string, Dictionary<string, Race>> system; | |
130 racesTable.TryGetValue(systemID, out system); | |
131 | |
132 if (system==null) | |
133 { | |
134 return new Race[0]; | |
135 } | |
136 | |
137 int count = 0; | |
138 | |
139 foreach (Dictionary<string, Race> racesDict in system.Values) | |
140 { | |
141 count+= racesDict.Count; | |
142 } | |
143 | |
144 Race[] races = new Race[count]; | |
145 int i = 0; | |
146 | |
147 foreach (string raceID in system.Keys) | |
148 { | |
149 foreach (string raceSubId in system[raceID].Keys) | |
150 { | |
151 races[i++] = GetRace(systemID, raceID, raceSubId); | |
152 } | |
153 } | |
154 | |
155 return races; | |
156 } | |
157 | |
158 public override Race GetRace(GameSystem system, string raceID) | |
159 { | |
160 return GetRace(system.ID, raceID); | |
161 } | |
162 | |
163 /// <summary> | |
164 /// Gets a single race for a given game system by ID of the game system and race. | |
165 /// </summary> | |
166 /// <param name="systemID"> | |
167 /// The <see cref="System.String"/> ID of the game system that the race is part of. | |
168 /// </param> | |
169 /// <param name="raceID"> | |
170 /// The <see cref="System.String"/> ID for the race to load. | |
171 /// </param> | |
172 /// <returns> | |
173 /// 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. | |
174 /// </returns> | |
175 public Race GetRace(string systemID, string raceID) | |
176 { | |
177 return GetRace(systemID, raceID, ""); | |
178 } | |
179 | |
180 public override Race GetRace(GameSystem system, string raceID, string raceSubID) | |
181 { | |
182 return GetRace(system.ID, raceID, raceSubID); | |
183 } | |
184 | |
185 /// <summary> | |
186 /// Gets a single race for a given game system by the game system's ID and the race's ID and sub-race ID. | |
187 /// </summary> | |
188 /// <param name="systemID"> | |
189 /// The <see cref="System.String"/> ID of the game system that the race is part of. | |
190 /// </param> | |
191 /// <param name="raceID"> | |
192 /// The <see cref="System.String"/> ID for the race to load. | |
193 /// </param> | |
194 /// <param name="raceSubID"> | |
195 /// A <see cref="System.String"/> | |
196 /// </param> | |
197 /// <returns> | |
198 /// A <see cref="Race"/> | |
199 /// </returns> | |
200 public Race GetRace(string systemID, string raceID, string raceSubID) | |
201 { | |
202 if (racesTable==null) | |
203 { | |
204 LoadFiles(); | |
205 } | |
206 | |
207 Race race = null; | |
208 | |
209 Dictionary<string, Race> subraces = GetRaceTable(systemID, raceID); | |
210 | |
211 if (subraces != null) | |
212 { | |
213 subraces.TryGetValue(raceSubID.ToLower(), out race); | |
214 } | |
215 | |
216 return race; | |
217 } | |
218 | |
219 private Dictionary<string, Race> GetRaceTable(string systemID, string raceID) | |
220 { | |
221 Dictionary<string, Dictionary<string, Race>> races; | |
222 racesTable.TryGetValue(systemID.ToLower(), out races); | |
223 Dictionary<string, Race> subraces = null; | |
224 | |
225 if (races != null) | |
226 { | |
227 races.TryGetValue(raceID.ToLower(), out subraces); | |
228 } | |
229 | |
230 return subraces; | |
231 } | |
232 | |
233 protected internal override void RemoveRace(Race race) | |
234 { | |
235 Dictionary<string, Race> subraces = GetRaceTable(race.GameSystem.ID, race.ID); | |
236 | |
237 if (subraces != null) | |
238 { | |
239 subraces.Remove(race.SubID.ToLower()); | |
240 } | |
241 } | |
242 | |
243 public override string[] GetGameSystemIDs() | |
244 { | |
245 if (systemsTable==null) | |
246 { | |
247 LoadFiles(); | |
248 } | |
249 | |
250 return DictionaryUtils.ToKeyArray(systemsTable); | |
251 } | |
252 | |
253 public override string[] GetSystemRaceIDs(GameSystem system) | |
254 { | |
255 return GetSystemRaceIDs(system.ID); | |
256 } | |
257 | |
258 /// <summary> | |
259 /// Gets the IDs of all of the races of a specified game system. | |
260 /// </summary> | |
261 /// <param name="systemID"> | |
262 /// The <see cref="System.String"/> ID of the game system to get the available races for. | |
263 /// </param> | |
264 /// <returns> | |
265 /// An array of <see cref="System.String"/>s representing the IDs of the races of the specified game system. | |
266 /// </returns> | |
267 public string[] GetSystemRaceIDs(string systemID) | |
268 { | |
269 if (racesTable == null) | |
270 { | |
271 LoadFiles(); | |
272 } | |
273 | |
274 Dictionary<string, Dictionary<string, Race>> races = racesTable[systemID.ToLower()]; | |
275 | |
276 if (races==null) | |
277 { | |
278 return new string[0]; | |
279 } | |
280 else | |
281 { | |
282 string[] keys = new string[races.Keys.Count]; | |
283 int i = 0; | |
284 | |
285 foreach (string key in races.Keys) | |
286 { | |
287 keys[i++] = key; | |
288 } | |
289 | |
290 return keys; | |
291 } | |
292 } | |
293 } | |
294 } |