forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loader.cs
214 lines (192 loc) · 6.69 KB
/
Loader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using GPSRCmdGen.Containers;
namespace GPSRCmdGen
{
/// <summary>
/// Helper class for loading and storing Xml serialized data
/// </summary>
public static class Loader
{
#region Variables
/// <summary>
/// Stores the path of the executable file
/// </summary>
private static readonly string exePath;
/// <summary>
/// Stores the namespace strings for serialized objects
/// </summary>
private static readonly XmlSerializerNamespaces ns;
#endregion
#region Constructor
/// <summary>
/// Initializes the <see cref="GPSRCmdGen.Loader"/> class.
/// </summary>
static Loader(){
Loader.exePath = AppDomain.CurrentDomain.BaseDirectory;
Loader.ns = new XmlSerializerNamespaces();
Loader.ns.Add ("", "");
}
#endregion
#region Properties
/// <summary>
/// Gets the path of the executable file
/// </summary>
public static string ExePath{get {return Loader.exePath;} }
#endregion
#region Methods
/// <summary>
/// Gets a full path for the given filename using the executable
/// file path as base path path.
/// </summary>
/// <param name="fileName">The name of the file to combine into a path</param>
/// <returns>A full path for the given fileName.</returns>
public static string GetPath(string fileName){
return Path.Combine (Loader.exePath, fileName);
}
/// <summary>
/// Gets a full path for the given filename using the executable
/// file path as base path path and a subdirectory.
/// </summary>
/// <param name="subdir">The name of the subdirectory that will contain the file</param>
/// <param name="fileName">The name of the file to combine into a path</param>
/// <returns>A full path for the given fileName.</returns>
public static string GetPath(string subdir, string fileName){
return Path.Combine (Path.Combine(Loader.exePath, subdir), fileName);
}
/// <summary>
/// Loads an array of T objects from a XML file.
/// </summary>
/// <param name="filePath">The path of the XML file</param>
/// <typeparam name="T">The type of objects contained in the file.</typeparam>
/// <returns>The array of T objects in the XML file</returns>
public static List<T> LoadArray<T>(string filePath)
{
T[] array = null;
List<T> list = new List<T>();
using (StreamReader reader = new StreamReader(filePath, ASCIIEncoding.UTF8))
{
XmlSerializer serializer = new XmlSerializer (typeof(T[]));
array = (T[])serializer.Deserialize(reader);
reader.Close();
}
if(array != null)
list = new List<T>(array);
return list;
}
/// <summary>
/// Loads an object from a XML file.
/// </summary>
/// <param name="filePath">The path of the XML file</param>
/// <typeparam name="T">The type of object to load.</typeparam>
/// <returns>The object in the XML file</returns>
public static T Load<T>(string filePath)
{
T item;
using (StreamReader reader = new StreamReader(filePath, ASCIIEncoding.UTF8))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
item = (T)serializer.Deserialize(reader);
reader.Close();
}
return item;
}
/// <summary>
/// Stores an object into a XML file.
/// </summary>
/// <param name="filePath">The path of the XML file to store the objectt within.</param>
/// <param name="o">The object to serialize and save.</param>
public static void Save(string filePath, object o){
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
settings.Indent = true;
settings.OmitXmlDeclaration = false;
using (StreamWriter stream = new StreamWriter(filePath))
{
using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
{
XmlSerializer serializer = new XmlSerializer(o.GetType());
serializer.Serialize(xmlWriter, o, ns);
xmlWriter.Close();
}
stream.Close();
}
}
/// <summary>
/// Loads a list of Grammar objects from the grammars subdirectory.
/// </summary>
/// <returns>A list of Grammar objects</returns>
public static List<Grammar> LoadGrammars ()
{
Grammar grammar;
string grammarsPath = GetPath ("grammars");
string[] gfs = Directory.GetFiles (grammarsPath, "*.txt", SearchOption.TopDirectoryOnly);
List<Grammar> grammars = new List<Grammar> (gfs.Length);
foreach (string gf in gfs) {
grammar = Grammar.LoadFromFile (gf);
if (grammar != null)
grammars.Add (grammar);
}
if (grammars.Count < 1)
throw new Exception ("No grammars could be loaded");
return grammars;
}
/// <summary>
/// Loads the set of Locations grouped by room from the Locations.xml file.
/// </summary>
/// <returns>A LocationManager that contains the set of objects and categories</returns>
public static LocationManager LoadLocations(string filePath)
{
RoomContainer container = Load<RoomContainer>(filePath);
if (container == null)
throw new Exception("No objects found");
LocationManager manager = LocationManager.Instance;
foreach (Room r in container.Rooms)
manager.Add(r);
return manager;
}
/// <summary>
/// Loads the set of GPSRObjects and Categories from the Objects.xml file.
/// </summary>
/// <returns>A GPSRObjectManager that contains the set of objects and categories</returns>
public static GPSRObjectManager LoadObjects (string filePath)
{
CategoryContainer container = Load<CategoryContainer> (filePath);
if (container == null)
throw new Exception ("No objects found");
GPSRObjectManager manager = GPSRObjectManager.Instance;
foreach (Category c in container.Categories)
manager.Add (c);
return manager;
}
/// <summary>
/// Serializes the specified list of T objects into a string.
/// </summary>
/// <param name="list">The list to serialize.</param>
/// <typeparam name="T">The type of serializable objects of the list.</typeparam>
/// <returns>A string containing the XML representation of the list.</returns>
public static string Serialize<T>(List<T> list)
{
string serialized;
T[] array = list.ToArray ();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
settings.Indent = true;
settings.OmitXmlDeclaration = false;
using (StringWriter textWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings)) {
XmlSerializer serializer = new XmlSerializer (typeof(T[]));
serializer.Serialize (xmlWriter, array, ns);
}
serialized = textWriter.ToString ();
}
return serialized;
}
#endregion
}
}