comparison API/DefaultWarFoundryLoader.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
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 private bool loaded = false;
19 private bool loading = false;
20
21 public DefaultWarFoundryLoader()
22 {
23 systemsTable = new Dictionary<string,GameSystem>();
24 racesTable = new Dictionary<string,Dictionary<string,Dictionary<string,Race>>>();
25 }
26
27 protected override void PrepareForFileLoad()
28 {
29 loading = true;
30 systemsTable.Clear();
31 racesTable.Clear();
32 }
33
34 protected override void FinishFileLoad()
35 {
36 loaded = true;
37 loading = false;
38 }
39
40 protected override GameSystem GetExistingSystemForSystem(GameSystem system)
41 {
42 return DictionaryUtils.GetValue(systemsTable, system.ID.ToLower());
43 }
44
45 protected override void DoStoreGameSystem(GameSystem system)
46 {
47 systemsTable[system.ID.ToLower()] = system;
48 }
49
50 protected override void DoStoreRace(Race race)
51 {
52 Dictionary<string, Dictionary<string, Race>> systemRaces;
53
54 string systemID = race.GameSystem.ID.ToLower();
55 racesTable.TryGetValue(systemID, out systemRaces);
56
57 if (systemRaces == null)
58 {
59 systemRaces = new Dictionary<string,Dictionary<string,Race>>();
60 racesTable.Add(systemID, systemRaces);
61 }
62
63 Dictionary<string, Race> subRaces;
64 systemRaces.TryGetValue(race.ID.ToLower(), out subRaces);
65
66 if (subRaces == null)
67 {
68 subRaces = new Dictionary<string,Race>();
69 systemRaces.Add(race.ID.ToLower(), subRaces);
70 }
71
72 string subID = race.SubID.ToLower();
73
74 if (subRaces.ContainsKey(subID))
75 {
76 Race existingRace = subRaces[subID];
77
78 if (!race.Equals(existingRace))
79 {
80 //TODO: Raise an event to say we got a different duplicate
81 //We can't just fail, because failing is for completely unhandled files, not for objects in a file
82 }
83 }
84 else
85 {
86 subRaces.Add(race.SubID.ToLower(), race);
87 }
88 }
89
90 public override GameSystem[] GetGameSystems()
91 {
92 if (!loaded && !loading)
93 {
94 LoadFiles();
95 }
96
97 return DictionaryUtils.ToArray<string, GameSystem>(systemsTable);
98 }
99
100 public override GameSystem GetGameSystem(string systemID)
101 {
102 if (!loaded && !loading)
103 {
104 LoadFiles();
105 }
106
107 GameSystem system;
108 systemsTable.TryGetValue(systemID.ToLower(), out system);
109 return system;
110 }
111
112 protected internal override void RemoveGameSystem(GameSystem system)
113 {
114 systemsTable.Remove(system.ID.ToLower());
115 }
116
117 public override Race[] GetRaces(GameSystem system)
118 {
119 return GetRaces(system.ID);
120 }
121
122 /// <summary>
123 /// Gets an array of the races for a game system by ID.
124 /// </summary>
125 /// <param name="systemID">
126 /// The <see cref="System.String"/> ID of the game system to get races for
127 /// </param>
128 /// <returns>
129 /// An array of <see cref="Race"/>s for the specified game system
130 /// </returns>
131 public Race[] GetRaces(string systemID)
132 {
133 if (!loaded && !loading)
134 {
135 LoadFiles();
136 }
137
138 systemID = systemID.ToLower();
139 Dictionary<string, Dictionary<string, Race>> system;
140 racesTable.TryGetValue(systemID, out system);
141
142 if (system == null)
143 {
144 return new Race[0];
145 }
146
147 int count = 0;
148
149 foreach (Dictionary<string, Race> racesDict in system.Values)
150 {
151 count += racesDict.Count;
152 }
153
154 Race[] races = new Race[count];
155 int i = 0;
156
157 foreach (string raceID in system.Keys)
158 {
159 foreach (string raceSubId in system[raceID].Keys)
160 {
161 races[i++] = GetRace(systemID, raceID, raceSubId);
162 }
163 }
164
165 return races;
166 }
167
168 public override Race GetRace(GameSystem system, string raceID)
169 {
170 return GetRace(system.ID, raceID);
171 }
172
173 /// <summary>
174 /// Gets a single race for a given game system by ID of the game system and race.
175 /// </summary>
176 /// <param name="systemID">
177 /// The <see cref="System.String"/> ID of the game system that the race is part of.
178 /// </param>
179 /// <param name="raceID">
180 /// The <see cref="System.String"/> ID for the race to load.
181 /// </param>
182 /// <returns>
183 /// 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.
184 /// </returns>
185 public Race GetRace(string systemID, string raceID)
186 {
187 return GetRace(systemID, raceID, "");
188 }
189
190 public override Race GetRace(GameSystem system, string raceID, string raceSubID)
191 {
192 return GetRace(system.ID, raceID, raceSubID);
193 }
194
195 /// <summary>
196 /// Gets a single race for a given game system by the game system's ID and the race's ID and sub-race ID.
197 /// </summary>
198 /// <param name="systemID">
199 /// The <see cref="System.String"/> ID of the game system that the race is part of.
200 /// </param>
201 /// <param name="raceID">
202 /// The <see cref="System.String"/> ID for the race to load.
203 /// </param>
204 /// <param name="raceSubID">
205 /// A <see cref="System.String"/>
206 /// </param>
207 /// <returns>
208 /// A <see cref="Race"/>
209 /// </returns>
210 public Race GetRace(string systemID, string raceID, string raceSubID)
211 {
212 if (!loaded && !loading)
213 {
214 LoadFiles();
215 }
216
217 Race race = null;
218
219 Dictionary<string, Race> subraces = GetRaceTable(systemID, raceID);
220
221 if (subraces != null)
222 {
223 subraces.TryGetValue(raceSubID.ToLower(), out race);
224 }
225
226 return race;
227 }
228
229 private Dictionary<string, Race> GetRaceTable(string systemID, string raceID)
230 {
231 Dictionary<string, Dictionary<string, Race>> races;
232 racesTable.TryGetValue(systemID.ToLower(), out races);
233 Dictionary<string, Race> subraces = null;
234
235 if (races != null)
236 {
237 races.TryGetValue(raceID.ToLower(), out subraces);
238 }
239
240 return subraces;
241 }
242
243 protected internal override void RemoveRace(Race race)
244 {
245 Dictionary<string, Race> subraces = GetRaceTable(race.GameSystem.ID, race.ID);
246
247 if (subraces != null)
248 {
249 subraces.Remove(race.SubID.ToLower());
250 }
251 }
252
253 public override string[] GetGameSystemIDs()
254 {
255 if (!loaded && !loading)
256 {
257 LoadFiles();
258 }
259
260 return DictionaryUtils.ToKeyArray(systemsTable);
261 }
262
263 public override string[] GetSystemRaceIDs(GameSystem system)
264 {
265 return GetSystemRaceIDs(system.ID);
266 }
267
268 /// <summary>
269 /// Gets the IDs of all of the races of a specified game system.
270 /// </summary>
271 /// <param name="systemID">
272 /// The <see cref="System.String"/> ID of the game system to get the available races for.
273 /// </param>
274 /// <returns>
275 /// An array of <see cref="System.String"/>s representing the IDs of the races of the specified game system.
276 /// </returns>
277 public string[] GetSystemRaceIDs(string systemID)
278 {
279 if (!loaded && !loading)
280 {
281 LoadFiles();
282 }
283
284 Dictionary<string, Dictionary<string, Race>> races = racesTable[systemID.ToLower()];
285
286 if (races == null)
287 {
288 return new string[0];
289 }
290 else
291 {
292 string[] keys = new string[races.Keys.Count];
293 int i = 0;
294
295 foreach (string key in races.Keys)
296 {
297 keys[i++] = key;
298 }
299
300 return keys;
301 }
302 }
303 }
304 }