Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/AbstractNativeWarFoundryFactory.cs @ 15:306558904c2a
Re #1 - LGPL license all code
* Add LGPL header to all files
* Add COPYING.LGPL and COPYING.GPL with content of license
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 25 Jan 2009 14:50:36 +0000 |
parents | 0770e5cbba7c |
children | e7de5c96f5c2 |
rev | line source |
---|---|
15 | 1 // This file (AbstractNativeWarFoundryFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. |
0 | 2 // |
15 | 3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. |
0 | 4 |
5 using System; | |
6 using System.IO; | |
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; | |
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 | |
25 { | |
26 protected static readonly string SYSTEM_ZIP_IDENTIFIER = "WarFoundry_System"; | |
27 protected static readonly string RACE_ZIP_IDENTIFIER = "WarFoundry_Race"; | |
28 protected static readonly string ARMY_ZIP_IDENTIFIER = "WarFoundry_Army"; | |
29 | |
30 protected AbstractNativeWarFoundryFactory() | |
31 { | |
32 //Do nothing - just make the constructor non-public | |
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 } |
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 { |
58 return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file); | |
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 { |
65 return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file); | |
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 { |
72 return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file); | |
73 } | |
74 | |
75 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); | |
76 | |
77 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) | |
78 { | |
79 ICollection<IWarFoundryObject> objects = null; | |
80 string comment = file.ZipFileComment; | |
81 IWarFoundryObject obj = null; | |
82 | |
83 if (comment.StartsWith(SYSTEM_ZIP_IDENTIFIER)) | |
84 { | |
85 obj = CreateGameSystemFromFile(file); | |
86 } | |
87 else if (comment.StartsWith(RACE_ZIP_IDENTIFIER)) | |
88 { | |
89 obj = CreateRaceFromFile(file); | |
90 } | |
91 else if (comment.StartsWith(ARMY_ZIP_IDENTIFIER)) | |
92 { | |
93 obj = CreateArmyFromFile(file); | |
94 } | |
95 | |
96 if (obj!=null) | |
97 { | |
98 objects = new List<IWarFoundryObject>(); | |
99 objects.Add(obj); | |
100 } | |
101 | |
102 return objects; | |
103 } | |
104 | |
105 protected Army CreateArmyFromFile(ZipFile file) | |
106 { | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
107 return CreateArmyFromStream(file, GetArmyDataStream(file)); |
0 | 108 } |
109 | |
110 protected abstract Stream GetArmyDataStream(ZipFile file); | |
111 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); | |
112 | |
113 protected Race CreateRaceFromFile(ZipFile file) | |
114 { | |
115 try | |
116 { | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
117 return CreateRaceFromStream(file, GetRaceDataStream(file)); |
0 | 118 } |
119 catch (InvalidFileException ex) | |
120 { | |
121 throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex); | |
122 } | |
123 } | |
124 | |
125 protected abstract Stream GetRaceDataStream(ZipFile file); | |
126 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); | |
127 | |
128 protected GameSystem CreateGameSystemFromFile(ZipFile file) | |
129 { | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
130 return CreateGameSystemFromStream(file, GetGameSystemDataStream(file)); |
0 | 131 } |
132 | |
133 protected abstract Stream GetGameSystemDataStream(ZipFile file); | |
134 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); | |
135 | |
136 public override bool Equals (object o) | |
137 { | |
138 if (o == this) | |
139 { | |
140 return true; | |
141 } | |
142 else if (o == null || !(this.GetType().Equals(o.GetType()))) | |
143 { | |
144 return false; | |
145 } | |
146 | |
147 return true; | |
148 } | |
149 | |
150 public override int GetHashCode () | |
151 { | |
152 return GetType().FullName.GetHashCode(); | |
153 } | |
154 } | |
155 } |