Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Factories/AbstractNativeWarFoundryFactory.cs @ 14:0770e5cbba7c
Closes #21 - File loading in order
* Reworked LoadFiles to smaller methods for readability (also re #10) and structure
* Now determine expected load return before loading then load all "expected GameSystem" before "expected Race"
* Make "can load as race/game system/army" methods public in interface
Re #22 - Get errored file loading
* Created FileLoadFailure class and made LoadFiles return a list of them
Also
* Some code cleanup
* Change to DictionaryUtils calls
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 25 Jan 2009 14:03:20 +0000 |
parents | 613bc5eaac59 |
children | 306558904c2a |
rev | line source |
---|---|
0 | 1 // WarFoundryXmlFactory.cs |
2 // | |
3 // Copyright (C) 2007 IBBoard | |
4 // | |
5 // This library is free software; you can redistribute it and/or | |
6 // modify it under the terms of the GNU Lesser General Public | |
7 // License version 2.1 of the License as published by the Free | |
8 // Software Foundation. | |
9 // | |
10 // This library is distributed in the hope that it will be useful, | |
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 // Lesser General Public License for more details. | |
14 // | |
15 // You should have received a copy of the GNU Lesser General Public | |
16 // License along with this library; if not, write to the Free Software | |
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 // | |
19 // | |
20 | |
21 using System; | |
22 using System.IO; | |
23 using System.Xml; | |
24 using System.Xml.Schema; | |
25 using System.Collections.Generic; | |
26 using System.Text; | |
27 using IBBoard; | |
28 using IBBoard.IO; | |
29 using IBBoard.Lang; | |
30 using IBBoard.Logging; | |
31 using IBBoard.Xml; | |
32 using IBBoard.WarFoundry.API.Objects; | |
33 using ICSharpCode.SharpZipLib.Zip; | |
34 | |
35 namespace IBBoard.WarFoundry.API.Factories | |
36 { | |
37 /// <summary> | |
38 /// Base abstract class for all factories that load native WarFoundry data. | |
39 /// </summary> | |
40 public abstract class AbstractNativeWarFoundryFactory : AbstractWarFoundryFactory<ZipFile>, INativeWarFoundryFactory | |
41 { | |
42 protected static readonly string SYSTEM_ZIP_IDENTIFIER = "WarFoundry_System"; | |
43 protected static readonly string RACE_ZIP_IDENTIFIER = "WarFoundry_Race"; | |
44 protected static readonly string ARMY_ZIP_IDENTIFIER = "WarFoundry_Army"; | |
45 | |
46 protected AbstractNativeWarFoundryFactory() | |
47 { | |
48 //Do nothing - just make the constructor non-public | |
49 } | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
7
diff
changeset
|
50 |
0 | 51 protected override ZipFile GetFileAsSupportedType (FileInfo file) |
52 { | |
53 ZipFile zip = null; | |
54 | |
55 try | |
56 { | |
57 zip = new ZipFile(file.FullName); | |
58 } | |
7
895c8a2378a1
Code cleanup - remove warning about unused exception
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
59 catch(ZipException) |
0 | 60 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
61 //Silently dispose as per spec for the method |
0 | 62 } |
63 | |
64 return zip; | |
65 } | |
66 | |
67 protected override bool CheckCanHandleFileFormat (ZipFile file) | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
68 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
69 return CheckCanHandleFileAsGameSystem(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsArmy(file); |
0 | 70 } |
71 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
72 protected override bool CheckCanHandleFileAsGameSystem(ZipFile file) |
0 | 73 { |
74 return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file); | |
75 } | |
76 | |
77 protected abstract bool CheckCanFindSystemFileContent(ZipFile file); | |
78 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
79 protected override bool CheckCanHandleFileAsRace(ZipFile file) |
0 | 80 { |
81 return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file); | |
82 } | |
83 | |
84 protected abstract bool CheckCanFindRaceFileContent(ZipFile file); | |
85 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
86 protected override bool CheckCanHandleFileAsArmy(ZipFile file) |
0 | 87 { |
88 return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file); | |
89 } | |
90 | |
91 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); | |
92 | |
93 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) | |
94 { | |
95 ICollection<IWarFoundryObject> objects = null; | |
96 string comment = file.ZipFileComment; | |
97 IWarFoundryObject obj = null; | |
98 | |
99 if (comment.StartsWith(SYSTEM_ZIP_IDENTIFIER)) | |
100 { | |
101 obj = CreateGameSystemFromFile(file); | |
102 } | |
103 else if (comment.StartsWith(RACE_ZIP_IDENTIFIER)) | |
104 { | |
105 obj = CreateRaceFromFile(file); | |
106 } | |
107 else if (comment.StartsWith(ARMY_ZIP_IDENTIFIER)) | |
108 { | |
109 obj = CreateArmyFromFile(file); | |
110 } | |
111 | |
112 if (obj!=null) | |
113 { | |
114 objects = new List<IWarFoundryObject>(); | |
115 objects.Add(obj); | |
116 } | |
117 | |
118 return objects; | |
119 } | |
120 | |
121 protected Army CreateArmyFromFile(ZipFile file) | |
122 { | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
123 return CreateArmyFromStream(file, GetArmyDataStream(file)); |
0 | 124 } |
125 | |
126 protected abstract Stream GetArmyDataStream(ZipFile file); | |
127 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); | |
128 | |
129 protected Race CreateRaceFromFile(ZipFile file) | |
130 { | |
131 try | |
132 { | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
133 return CreateRaceFromStream(file, GetRaceDataStream(file)); |
0 | 134 } |
135 catch (InvalidFileException ex) | |
136 { | |
137 throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex); | |
138 } | |
139 } | |
140 | |
141 protected abstract Stream GetRaceDataStream(ZipFile file); | |
142 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); | |
143 | |
144 protected GameSystem CreateGameSystemFromFile(ZipFile file) | |
145 { | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
146 return CreateGameSystemFromStream(file, GetGameSystemDataStream(file)); |
0 | 147 } |
148 | |
149 protected abstract Stream GetGameSystemDataStream(ZipFile file); | |
150 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); | |
151 | |
152 public override bool Equals (object o) | |
153 { | |
154 if (o == this) | |
155 { | |
156 return true; | |
157 } | |
158 else if (o == null || !(this.GetType().Equals(o.GetType()))) | |
159 { | |
160 return false; | |
161 } | |
162 | |
163 return true; | |
164 } | |
165 | |
166 public override int GetHashCode () | |
167 { | |
168 return GetType().FullName.GetHashCode(); | |
169 } | |
170 } | |
171 } |