view api/Factories/AbstractNativeWarFoundryFactory.cs @ 104:2f3cafb69799

Re #121: Migrate to AGPL license * Update all API files to AGPL license * Include AGPL license and remove GPL/LGPL documents * Fix copyright dates where they're known
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Aug 2009 10:36:50 +0000
parents 3ea0ab04352b
children d0c60b3204c1
line source
1 // This file (AbstractNativeWarFoundryFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard.
2 //
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.
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;
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";
30 protected AbstractNativeWarFoundryFactory()
31 {
32 //Do nothing - just make the constructor non-public
33 }
35 protected override ZipFile GetFileAsSupportedType (FileInfo file)
36 {
37 ZipFile zip = null;
39 try
40 {
41 zip = new ZipFile(file.FullName);
42 }
43 catch(ZipException)
44 {
45 //Silently dispose as per spec for the method
46 }
47 catch (IOException)
48 {
49 //Silently dispose as per spec for the method
50 }
52 return zip;
53 }
55 protected override bool CheckCanHandleFileFormat (ZipFile file)
56 {
57 return CheckCanHandleFileAsGameSystem(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsArmy(file);
58 }
60 protected override bool CheckCanHandleFileAsGameSystem(ZipFile file)
61 {
62 return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file);
63 }
65 protected abstract bool CheckCanFindSystemFileContent(ZipFile file);
67 protected override bool CheckCanHandleFileAsRace(ZipFile file)
68 {
69 return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file);
70 }
72 protected abstract bool CheckCanFindRaceFileContent(ZipFile file);
74 protected override bool CheckCanHandleFileAsArmy(ZipFile file)
75 {
76 return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file);
77 }
79 protected abstract bool CheckCanFindArmyFileContent(ZipFile file);
81 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file)
82 {
83 ICollection<IWarFoundryObject> objects = null;
84 string comment = file.ZipFileComment;
85 IWarFoundryObject obj = null;
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 }
100 if (obj!=null)
101 {
102 objects = new List<IWarFoundryObject>();
103 objects.Add(obj);
104 }
106 return objects;
107 }
109 protected Army CreateArmyFromFile(ZipFile file)
110 {
111 return CreateArmyFromStream(file, GetArmyDataStream(file));
112 }
114 protected abstract Stream GetArmyDataStream(ZipFile file);
115 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
117 protected Race CreateRaceFromFile(ZipFile file)
118 {
119 try
120 {
121 return CreateRaceFromStream(file, GetRaceDataStream(file));
122 }
123 catch (InvalidFileException ex)
124 {
125 throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex);
126 }
127 }
129 protected abstract Stream GetRaceDataStream(ZipFile file);
130 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream);
132 protected GameSystem CreateGameSystemFromFile(ZipFile file)
133 {
134 return CreateGameSystemFromStream(file, GetGameSystemDataStream(file));
135 }
137 protected abstract Stream GetGameSystemDataStream(ZipFile file);
138 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream);
140 public override bool Equals (object o)
141 {
142 if (o == this)
143 {
144 return true;
145 }
146 else if (o == null || !(this.GetType().Equals(o.GetType())))
147 {
148 return false;
149 }
151 return true;
152 }
154 public override int GetHashCode ()
155 {
156 return GetType().FullName.GetHashCode();
157 }
158 }
159 }