Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/AbstractNativeWarFoundryFactory.cs @ 256:958ecd7b2844
Re #270: Add multiple categories to API
* Add Categories property
* Add method to add Category
* Tie up Categories with MainCategory for default behaviour
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 17 May 2010 20:03:05 +0000 |
parents | ca2905c9b225 |
children | 3854c26073c4 |
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 | |
236
ca2905c9b225
Fixes #252: Remove need for text in zip file comments
IBBoard <dev@ibboard.co.uk>
parents:
223
diff
changeset
|
25 { |
82 | 26 protected AbstractNativeWarFoundryFactory() |
0 | 27 { |
82 | 28 //Do nothing - just make the constructor non-public |
0 | 29 } |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
7
diff
changeset
|
30 |
0 | 31 protected override ZipFile GetFileAsSupportedType (FileInfo file) |
32 { | |
33 ZipFile zip = null; | |
34 | |
35 try | |
36 { | |
37 zip = new ZipFile(file.FullName); | |
38 } | |
7
895c8a2378a1
Code cleanup - remove warning about unused exception
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
39 catch(ZipException) |
0 | 40 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
41 //Silently dispose as per spec for the method |
0 | 42 } |
27 | 43 catch (IOException) |
44 { | |
45 //Silently dispose as per spec for the method | |
46 } | |
0 | 47 |
48 return zip; | |
49 } | |
50 | |
51 protected override bool CheckCanHandleFileFormat (ZipFile file) | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
52 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
53 return CheckCanHandleFileAsGameSystem(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsArmy(file); |
0 | 54 } |
55 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
56 protected override bool CheckCanHandleFileAsGameSystem(ZipFile file) |
0 | 57 { |
236
ca2905c9b225
Fixes #252: Remove need for text in zip file comments
IBBoard <dev@ibboard.co.uk>
parents:
223
diff
changeset
|
58 return CheckCanFindSystemFileContent(file); |
0 | 59 } |
60 | |
61 protected abstract bool CheckCanFindSystemFileContent(ZipFile file); | |
62 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
63 protected override bool CheckCanHandleFileAsRace(ZipFile file) |
0 | 64 { |
236
ca2905c9b225
Fixes #252: Remove need for text in zip file comments
IBBoard <dev@ibboard.co.uk>
parents:
223
diff
changeset
|
65 return CheckCanFindRaceFileContent(file); |
0 | 66 } |
67 | |
68 protected abstract bool CheckCanFindRaceFileContent(ZipFile file); | |
69 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
70 protected override bool CheckCanHandleFileAsArmy(ZipFile file) |
0 | 71 { |
236
ca2905c9b225
Fixes #252: Remove need for text in zip file comments
IBBoard <dev@ibboard.co.uk>
parents:
223
diff
changeset
|
72 return CheckCanFindArmyFileContent(file); |
0 | 73 } |
74 | |
75 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); | |
76 | |
77 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) | |
78 { | |
79 ICollection<IWarFoundryObject> objects = null; | |
200 | 80 IWarFoundryObject obj = null; |
81 | |
82 try | |
83 { | |
236
ca2905c9b225
Fixes #252: Remove need for text in zip file comments
IBBoard <dev@ibboard.co.uk>
parents:
223
diff
changeset
|
84 if (CheckCanFindSystemFileContent(file)) |
200 | 85 { |
86 obj = CreateGameSystemFromFile(file); | |
87 } | |
236
ca2905c9b225
Fixes #252: Remove need for text in zip file comments
IBBoard <dev@ibboard.co.uk>
parents:
223
diff
changeset
|
88 else if (CheckCanFindRaceFileContent(file)) |
200 | 89 { |
90 obj = CreateRaceFromFile(file); | |
91 } | |
236
ca2905c9b225
Fixes #252: Remove need for text in zip file comments
IBBoard <dev@ibboard.co.uk>
parents:
223
diff
changeset
|
92 else if (CheckCanFindArmyFileContent(file)) |
200 | 93 { |
94 obj = CreateArmyFromFile(file); | |
95 } | |
96 } | |
97 finally | |
98 { | |
99 file.Close(); | |
0 | 100 } |
101 | |
102 if (obj!=null) | |
103 { | |
104 objects = new List<IWarFoundryObject>(); | |
105 objects.Add(obj); | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
106 } |
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 finally |
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 dataStream.Close(); |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
122 } |
0 | 123 } |
124 | |
125 protected abstract Stream GetArmyDataStream(ZipFile file); | |
126 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); | |
127 | |
128 protected Race CreateRaceFromFile(ZipFile file) | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
129 { |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
130 Stream dataStream = GetRaceDataStream(file); |
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
131 |
0 | 132 try |
133 { | |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
134 return CreateRaceFromStream(file, dataStream); |
0 | 135 } |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
136 finally |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
137 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
138 dataStream.Close(); |
0 | 139 } |
140 } | |
141 | |
142 protected abstract Stream GetRaceDataStream(ZipFile file); | |
143 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); | |
144 | |
145 protected GameSystem CreateGameSystemFromFile(ZipFile file) | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
146 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
147 Stream dataStream = GetGameSystemDataStream(file); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
148 |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
149 try |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
150 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
151 return CreateGameSystemFromStream(file, dataStream); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
152 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
153 finally |
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 dataStream.Close(); |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
156 } |
0 | 157 } |
158 | |
159 protected abstract Stream GetGameSystemDataStream(ZipFile file); | |
82 | 160 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); |
0 | 161 |
162 public override bool Equals (object o) | |
163 { | |
164 if (o == this) | |
165 { | |
166 return true; | |
167 } | |
168 else if (o == null || !(this.GetType().Equals(o.GetType()))) | |
169 { | |
170 return false; | |
171 } | |
172 | |
173 return true; | |
174 } | |
175 | |
176 public override int GetHashCode () | |
177 { | |
178 return GetType().FullName.GetHashCode(); | |
82 | 179 } |
180 } | |
181 } |