Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/AbstractWarFoundryFactory.cs @ 313:f00a57369aaa
Re #253: Allow multiple data files in a single zip
* Add event-based mechanism to allow GameSystem to be registered before Race is loaded from a single zip
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 27 Feb 2011 15:54:13 +0000 |
parents | 3e287b6b5244 |
children |
rev | line source |
---|---|
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
28
diff
changeset
|
1 // This file (AbstractWarFoundryFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard. |
0 | 2 // |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
28
diff
changeset
|
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. |
0 | 4 |
5 using System; | |
6 using System.IO; | |
7 using System.Collections.Generic; | |
8 using IBBoard.WarFoundry.API.Objects; | |
9 | |
10 namespace IBBoard.WarFoundry.API.Factories | |
11 { | |
12 public abstract class AbstractWarFoundryFactory<FILE_TYPE> : IWarFoundryFactory | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
13 { |
313
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
14 public event SingleArgMethodInvoker<GameSystem> GameSystemLoaded; |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
15 |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
16 public event SingleArgMethodInvoker<Race> RaceLoaded; |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
17 |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
18 public event SingleArgMethodInvoker<Army> ArmyLoaded; |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
19 |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
20 public virtual void CompleteLoading(IWarFoundryStagedLoadObject obj) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
21 { |
120
fcbc3beea498
* Change default factory behaviour to pretend it has loaded files without changing anything
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
22 //Pretend we've fully loaded, as this will probably be standard for non-native factories and some native factories |
fcbc3beea498
* Change default factory behaviour to pretend it has loaded files without changing anything
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
23 obj.SetAsFullyLoaded(); |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
24 } |
313
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
25 |
0 | 26 public bool CanHandleFileFormat (FileInfo file) |
27 { | |
200 | 28 FILE_TYPE typedFile = GetFileAsSupportedType(file); |
29 bool canHandle = typedFile != null && CheckCanHandleFileFormat(typedFile); | |
30 | |
31 if (typedFile != null) | |
32 { | |
33 CleanUpFileAsSupportedType(typedFile); | |
34 } | |
35 | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
120
diff
changeset
|
36 return canHandle; |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
37 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
38 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
39 public bool CanHandleFileAsRace(FileInfo file) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
40 { |
200 | 41 FILE_TYPE typedFile = GetFileAsSupportedType(file); |
42 bool canHandle = typedFile != null && CheckCanHandleFileAsRace(typedFile); | |
43 | |
44 if (typedFile != null) | |
45 { | |
46 CleanUpFileAsSupportedType(typedFile); | |
47 } | |
48 | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
120
diff
changeset
|
49 return canHandle; |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
50 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
51 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
52 public bool CanHandleFileAsGameSystem(FileInfo file) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
53 { |
200 | 54 FILE_TYPE typedFile = GetFileAsSupportedType(file); |
55 bool canHandle = typedFile != null && CheckCanHandleFileAsGameSystem(typedFile); | |
56 | |
57 if (typedFile != null) | |
58 { | |
59 CleanUpFileAsSupportedType(typedFile); | |
60 } | |
61 | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
120
diff
changeset
|
62 return canHandle; |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
63 } |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
64 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
65 public bool CanHandleFileAsArmy(FileInfo file) |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
66 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
67 FILE_TYPE typedFile = GetFileAsSupportedType(file); |
200 | 68 bool canHandle = typedFile != null && CheckCanHandleFileAsArmy(typedFile); |
69 | |
70 if (typedFile != null) | |
71 { | |
72 CleanUpFileAsSupportedType(typedFile); | |
73 } | |
74 | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
120
diff
changeset
|
75 return canHandle; |
200 | 76 } |
77 | |
78 protected virtual void CleanUpFileAsSupportedType(FILE_TYPE typedFile) | |
79 { | |
80 //Do nothing by default | |
0 | 81 } |
82 | |
83 /// <summary> | |
84 /// Converts the <see cref="FileInfo"/> object in to the appropriate type for this class so that it can perform its checks. If no conversion is required (the test can be performed on a <see cref="FileInfo"/> object) the object should be returned with no modification. | |
28 | 85 /// If the file is not of supported type the <code>null</code> should be returned. |
0 | 86 /// </summary> |
87 /// <param name="file"> | |
88 /// A <see cref="FileInfo"/> to get the supported source object from. | |
89 /// </param> | |
90 /// <returns> | |
91 /// An object of type <see cref="FILE_TYPE"/> that has been converted from the input <see cref="FileInfo"/> object, or <code>null</code> if the conversion cannot be made. | |
92 /// </returns> | |
93 protected abstract FILE_TYPE GetFileAsSupportedType(FileInfo file); | |
94 | |
95 /// <summary> | |
96 /// Checks whether the factory thinks it can load data from the file in its paramaterised type. | |
97 /// </summary> | |
98 /// <param name="file"> | |
99 /// An object of the converted <see cref="FILE_TYPE"/> to check support for | |
100 /// </param> | |
101 /// <returns> | |
102 /// <code>true</code> if the factory thinks it can support the file, else <code>false</code> | |
103 /// </returns> | |
104 protected abstract bool CheckCanHandleFileFormat(FILE_TYPE file); | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
105 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
106 /// <summary> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
107 /// Checks whether the factory thinks it can load data from the file in its paramaterised type as a Race object. |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
108 /// </summary> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
109 /// <param name="file"> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
110 /// An object of the converted <see cref="FILE_TYPE"/> to check support for |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
111 /// </param> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
112 /// <returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
113 /// <code>true</code> if the factory thinks it can support the file as a Race, else <code>false</code> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
114 /// </returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
115 protected abstract bool CheckCanHandleFileAsRace(FILE_TYPE file); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
116 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
117 /// <summary> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
118 /// Checks whether the factory thinks it can load data from the file in its paramaterised type as a GameSystem object. |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
119 /// </summary> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
120 /// <param name="file"> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
121 /// An object of the converted <see cref="FILE_TYPE"/> to check support for |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
122 /// </param> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
123 /// <returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
124 /// <code>true</code> if the factory thinks it can support the file as a GameSystem, else <code>false</code> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
125 /// </returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
126 protected abstract bool CheckCanHandleFileAsGameSystem(FILE_TYPE file); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
127 |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
128 /// <summary> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
129 /// Checks whether the factory thinks it can load data from the file in its paramaterised type as an Army object. |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
130 /// </summary> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
131 /// <param name="file"> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
132 /// An object of the converted <see cref="FILE_TYPE"/> to check support for |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
133 /// </param> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
134 /// <returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
135 /// <code>true</code> if the factory thinks it can support the file as a Army, else <code>false</code> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
136 /// </returns> |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
137 protected abstract bool CheckCanHandleFileAsArmy(FILE_TYPE file); |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
138 |
0 | 139 |
140 public ICollection<IWarFoundryObject> CreateObjectsFromFile(FileInfo file) | |
141 { | |
142 return DoCreateObjectsFromFile(GetFileAsSupportedType(file)); | |
143 } | |
144 | |
145 /// <summary> | |
146 /// Reads the data from the supplied converted <see cref="FILE_TYPE"/> object and returns it as a collection of loadable objects. | |
313
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
147 /// In addition, the method fires the appropriate XxxLoaded event for each object created for asynchronous use. |
0 | 148 /// </summary> |
149 /// <param name="file"> | |
150 /// An object of the converted <see cref="FILE_TYPE"/> for the file to load data from | |
151 /// </param> | |
152 /// <returns> | |
153 /// A <see cref="ICollection`1"/> of <see cref="IWarFoundryObject"/>s that were loaded from the file object | |
154 /// </returns> | |
155 protected abstract ICollection<IWarFoundryObject> DoCreateObjectsFromFile(FILE_TYPE file); | |
313
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
156 |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
157 protected void OnGameSystemLoaded(GameSystem system) |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
158 { |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
159 if (GameSystemLoaded != null) |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
160 { |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
161 GameSystemLoaded(system); |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
162 } |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
163 } |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
164 |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
165 protected void OnRaceLoaded(Race race) |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
166 { |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
167 if (RaceLoaded != null) |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
168 { |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
169 RaceLoaded(race); |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
170 } |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
171 } |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
172 |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
173 protected void OnArmyLoaded(Army army) |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
174 { |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
175 if (ArmyLoaded != null) |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
176 { |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
177 ArmyLoaded(army); |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
178 } |
f00a57369aaa
Re #253: Allow multiple data files in a single zip
IBBoard <dev@ibboard.co.uk>
parents:
200
diff
changeset
|
179 } |
0 | 180 } |
181 } |