Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/AbstractNativeWarFoundryFactory.cs @ 194:1412a42190a1
* Fix mutex group clash checking
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 30 Oct 2009 20:23:03 +0000 |
parents | 1d13820b3d96 |
children | 70ba3bee0c2e |
rev | line source |
---|---|
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
1 // This file (AbstractNativeWarFoundryFactory.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:
82
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; | |
82 | 6 using System.IO; |
0 | 7 using System.Xml; |
8 using System.Xml.Schema; | |
9 using System.Collections.Generic; | |
10 using System.Text; | |
11 using IBBoard; | |
12 using IBBoard.IO; | |
13 using IBBoard.Lang; | |
14 using IBBoard.Logging; | |
15 using IBBoard.Xml; | |
16 using IBBoard.WarFoundry.API.Objects; | |
82 | 17 using ICSharpCode.SharpZipLib.Zip; |
18 | |
19 namespace IBBoard.WarFoundry.API.Factories | |
20 { | |
21 /// <summary> | |
22 /// Base abstract class for all factories that load native WarFoundry data. | |
23 /// </summary> | |
24 public abstract class AbstractNativeWarFoundryFactory : AbstractWarFoundryFactory<ZipFile>, INativeWarFoundryFactory | |
0 | 25 { |
115 | 26 public static readonly string SYSTEM_ZIP_IDENTIFIER = "WarFoundry_System"; |
27 public static readonly string RACE_ZIP_IDENTIFIER = "WarFoundry_Race"; | |
28 public static readonly string ARMY_ZIP_IDENTIFIER = "WarFoundry_Army"; | |
0 | 29 |
82 | 30 protected AbstractNativeWarFoundryFactory() |
0 | 31 { |
82 | 32 //Do nothing - just make the constructor non-public |
0 | 33 } |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
7
diff
changeset
|
34 |
0 | 35 protected override ZipFile GetFileAsSupportedType (FileInfo file) |
36 { | |
37 ZipFile zip = null; | |
38 | |
39 try | |
40 { | |
41 zip = new ZipFile(file.FullName); | |
42 } | |
7
895c8a2378a1
Code cleanup - remove warning about unused exception
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
43 catch(ZipException) |
0 | 44 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
45 //Silently dispose as per spec for the method |
0 | 46 } |
27 | 47 catch (IOException) |
48 { | |
49 //Silently dispose as per spec for the method | |
50 } | |
0 | 51 |
52 return zip; | |
53 } | |
54 | |
55 protected override bool CheckCanHandleFileFormat (ZipFile file) | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
56 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
57 return CheckCanHandleFileAsGameSystem(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsArmy(file); |
0 | 58 } |
59 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
60 protected override bool CheckCanHandleFileAsGameSystem(ZipFile file) |
0 | 61 { |
62 return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file); | |
63 } | |
64 | |
65 protected abstract bool CheckCanFindSystemFileContent(ZipFile file); | |
66 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
67 protected override bool CheckCanHandleFileAsRace(ZipFile file) |
0 | 68 { |
69 return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file); | |
70 } | |
71 | |
72 protected abstract bool CheckCanFindRaceFileContent(ZipFile file); | |
73 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
74 protected override bool CheckCanHandleFileAsArmy(ZipFile file) |
0 | 75 { |
76 return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file); | |
77 } | |
78 | |
79 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); | |
80 | |
81 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) | |
82 { | |
83 ICollection<IWarFoundryObject> objects = null; | |
84 string comment = file.ZipFileComment; | |
85 IWarFoundryObject obj = null; | |
86 | |
87 if (comment.StartsWith(SYSTEM_ZIP_IDENTIFIER)) | |
88 { | |
89 obj = CreateGameSystemFromFile(file); | |
90 } | |
91 else if (comment.StartsWith(RACE_ZIP_IDENTIFIER)) | |
92 { | |
93 obj = CreateRaceFromFile(file); | |
94 } | |
95 else if (comment.StartsWith(ARMY_ZIP_IDENTIFIER)) | |
96 { | |
97 obj = CreateArmyFromFile(file); | |
98 } | |
99 | |
100 if (obj!=null) | |
101 { | |
102 objects = new List<IWarFoundryObject>(); | |
103 objects.Add(obj); | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
104 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
105 |
150
b36cc4af435b
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
148
diff
changeset
|
106 file.Close(); |
0 | 107 |
108 return objects; | |
109 } | |
110 | |
111 protected Army CreateArmyFromFile(ZipFile file) | |
112 { | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
113 Stream dataStream = GetArmyDataStream(file); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
114 |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
115 try |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
116 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
117 return CreateArmyFromStream(file, dataStream); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
118 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
119 catch (InvalidFileException ex) |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
120 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
121 throw new InvalidFileException("Data file " + file.Name + " was not a valid army file", ex); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
122 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
123 finally |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
124 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
125 dataStream.Close(); |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
126 } |
0 | 127 } |
128 | |
129 protected abstract Stream GetArmyDataStream(ZipFile file); | |
130 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); | |
131 | |
132 protected Race CreateRaceFromFile(ZipFile file) | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
133 { |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
134 Stream dataStream = GetRaceDataStream(file); |
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
135 |
0 | 136 try |
137 { | |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
138 return CreateRaceFromStream(file, dataStream); |
0 | 139 } |
140 catch (InvalidFileException ex) | |
141 { | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
142 throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
143 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
144 finally |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
145 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
146 dataStream.Close(); |
0 | 147 } |
148 } | |
149 | |
150 protected abstract Stream GetRaceDataStream(ZipFile file); | |
151 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); | |
152 | |
153 protected GameSystem CreateGameSystemFromFile(ZipFile file) | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
154 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
155 Stream dataStream = GetGameSystemDataStream(file); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
156 |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
157 try |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
158 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
159 return CreateGameSystemFromStream(file, dataStream); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
160 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
161 catch (InvalidFileException ex) |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
162 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
163 throw new InvalidFileException("Data file " + file.Name + " was not a valid game system file", ex); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
164 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
165 finally |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
166 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
167 dataStream.Close(); |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
168 } |
0 | 169 } |
170 | |
171 protected abstract Stream GetGameSystemDataStream(ZipFile file); | |
82 | 172 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); |
0 | 173 |
174 public override bool Equals (object o) | |
175 { | |
176 if (o == this) | |
177 { | |
178 return true; | |
179 } | |
180 else if (o == null || !(this.GetType().Equals(o.GetType()))) | |
181 { | |
182 return false; | |
183 } | |
184 | |
185 return true; | |
186 } | |
187 | |
188 public override int GetHashCode () | |
189 { | |
190 return GetType().FullName.GetHashCode(); | |
82 | 191 } |
192 } | |
193 } |