comparison API/Factories/AbstractNativeWarFoundryFactory.cs @ 417:2a36ebb7b6a9

Re #358: Handle factory.CreateObjectsFromFile where GetFileAsSupportedType returns null * Refactor out methods for file loading * Add "if zip file not null" check around newly refactored method
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Sep 2011 20:36:06 +0100
parents c70973b65090
children 86725e88052e
comparison
equal deleted inserted replaced
416:c70973b65090 417:2a36ebb7b6a9
13 using IBBoard.Lang; 13 using IBBoard.Lang;
14 using IBBoard.Logging; 14 using IBBoard.Logging;
15 using IBBoard.Xml; 15 using IBBoard.Xml;
16 using IBBoard.WarFoundry.API.Objects; 16 using IBBoard.WarFoundry.API.Objects;
17 using ICSharpCode.SharpZipLib.Zip; 17 using ICSharpCode.SharpZipLib.Zip;
18 using IBBoard.Collections;
18 19
19 namespace IBBoard.WarFoundry.API.Factories 20 namespace IBBoard.WarFoundry.API.Factories
20 { 21 {
21 /// <summary> 22 /// <summary>
22 /// Base abstract class for all factories that load native WarFoundry data. 23 /// Base abstract class for all factories that load native WarFoundry data.
29 } 30 }
30 31
31 protected override ZipFile GetFileAsSupportedType (FileInfo file) 32 protected override ZipFile GetFileAsSupportedType (FileInfo file)
32 { 33 {
33 ZipFile zip = null; 34 ZipFile zip = null;
34 string ext = file.Extension; 35 string ext = file.Extension.ToLower();
35 36
36 if (ext == "race" || ext == "army" || ext == "system") 37 if (ext == ".race" || ext == ".army" || ext == ".system")
37 { 38 {
38 try 39 try
39 { 40 {
40 zip = new ZipFile(file.FullName); 41 zip = new ZipFile(file.FullName);
41 } 42 }
79 80
80 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) 81 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file)
81 { 82 {
82 ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>(); 83 ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();
83 84
85 if (file != null)
86 {
87 DoCreateObjectsFromZip(file, objects);
88 }
89
90 return objects;
91 }
92
93 private void DoCreateObjectsFromZip(ZipFile file, ICollection<IWarFoundryObject> objects)
94 {
84 try 95 try
85 { 96 {
86 if (CheckCanFindSystemFileContent(file)) 97 CreateSystemObjectsFromZip (file, objects);
87 { 98 CreateRaceObjectsFromZip (file, objects);
88 foreach (GameSystem system in CreateGameSystemsFromFile(file)) 99 CreateArmyObjectsFromZip (file, objects);
89 {
90 OnGameSystemLoaded(system);
91 objects.Add(system);
92 }
93 }
94
95 if (CheckCanFindRaceFileContent(file))
96 {
97 foreach(Race race in CreateRacesFromFile(file))
98 {
99 OnRaceLoaded(race);
100 objects.Add(race);
101 }
102 }
103
104 if (CheckCanFindArmyFileContent(file))
105 {
106 foreach (Army army in CreateArmiesFromFile(file))
107 {
108 OnArmyLoaded(army);
109 objects.Add(army);
110 }
111 }
112 } 100 }
113 finally 101 finally
114 { 102 {
115 file.Close(); 103 file.Close();
116 } 104 }
117 105 }
118 return objects; 106
107 private void CreateSystemObjectsFromZip (ZipFile file, ICollection<IWarFoundryObject> objects)
108 {
109 if (CheckCanFindSystemFileContent(file))
110 {
111 foreach (GameSystem system in CreateGameSystemsFromFile(file))
112 {
113 OnGameSystemLoaded(system);
114 objects.Add(system);
115 }
116 }
117 }
118
119 void CreateRaceObjectsFromZip (ZipFile file, ICollection<IWarFoundryObject> objects)
120 {
121 if (CheckCanFindRaceFileContent(file))
122 {
123 foreach(Race race in CreateRacesFromFile(file))
124 {
125 OnRaceLoaded(race);
126 objects.Add(race);
127 }
128 }
129 }
130
131 void CreateArmyObjectsFromZip (ZipFile file, ICollection<IWarFoundryObject> objects)
132 {
133 if (CheckCanFindArmyFileContent(file))
134 {
135 foreach (Army army in CreateArmiesFromFile(file))
136 {
137 OnArmyLoaded(army);
138 objects.Add(army);
139 }
140 }
119 } 141 }
120 142
121 protected ICollection<Army> CreateArmiesFromFile(ZipFile file) 143 protected ICollection<Army> CreateArmiesFromFile(ZipFile file)
122 { 144 {
123 ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file); 145 ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file);