-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cs
More file actions
170 lines (146 loc) · 5.94 KB
/
Copy pathGame.cs
File metadata and controls
170 lines (146 loc) · 5.94 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using XmlData;
namespace GameSaveInfo {
public class Game : AXmlDataEntry, IComparable<Game> {
public string Name { get; protected set; }
public string Title { get; protected set; }
private XmlElement TitleElement;
public string Comment { get; protected set; }
public bool IsDeprecated { get; protected set; }
public GameType Type { get; protected set; }
public List<GameVersion> Versions = new List<GameVersion>();
protected Game(XmlDocument doc) : base(doc) { }
public Game(string name, string title, string comment, bool deprecated, GameType type, XmlDocument doc)
: this(doc) {
this.Name = name;
this.Title = title;
this.Comment = comment;
this.IsDeprecated = deprecated;
this.Type = type;
}
public Game(XmlElement element)
: base(element) {
}
protected virtual GameVersion createVersion(Game parent, XmlElement element) {
return new GameVersion(parent, element);
}
public override string ElementName {
get { return Type.ToString().ToLower(); }
}
public static string prepareGameName(string title) {
if (String.IsNullOrEmpty(title))
return "";
string name = Regex.Replace(title, @"[^A-Za-z0-9 ]+", "");
name = name.Replace("&", "And");
StringBuilder build = new StringBuilder();
foreach (string sub in name.Split(' ')) {
if (sub.Length > 0) {
build.Append(sub.Substring(0, 1).ToUpper());
build.Append(sub.Substring(1));
}
}
return build.ToString();
}
public GameVersion addVersion(String os, String platform, String release) {
GameVersion ver = new GameVersion(this, os, platform, release);
this.xml.AppendChild(ver.XML);
this.Versions.Add(ver);
return ver;
}
public void addVersion(GameVersion version) {
this.XML.AppendChild(version.XML);
this.Versions.Add(version);
}
public bool hasVersion(String os, String platform, String release) {
return getVersion(os,platform,release) != null;
}
public GameVersion getVersion(String os, String platform, String release) {
GameVersion candidate = null;
foreach (GameVersion version in this.Versions) {
if (version.ID.Matches(os, platform, null, null, release, null)) {
if (candidate == null || version.ID.Revision > candidate.ID.Revision) {
candidate = version;
}
}
}
return candidate;
}
protected override XmlElement WriteData(XmlElement element) {
// This outputs little more than what's necessary to create a custom game entry
// Once in the file, the xml wil not need to be re-generated, so it won't need to be outputted again
// This way manual updates to the xml file won't be lost ;)
addAtribute(element, "name", Name);
if (IsDeprecated)
addAtribute(element, "deprecated", "true");
element.AppendChild(createElement("title", Title));
if (!String.IsNullOrEmpty(Comment))
element.AppendChild(createElement("comment", Comment));
foreach (GameVersion ver in Versions) {
XmlElement ele = ver.XML;
if (ele != null)
element.AppendChild(ele);
}
return element;
}
protected override void LoadData(XmlElement element) {
switch (element.Name) {
case "system":
Type = GameType.system;
break;
case "game":
Type = GameType.game;
break;
case "mod":
Type = GameType.mod;
break;
case "expansion":
Type = GameType.expansion;
break;
default:
throw new NotSupportedException(element.Name);
}
foreach (XmlAttribute attrib in element.Attributes) {
switch (attrib.Name) {
case "name":
Name = attrib.Value;
break;
case "deprecated":
IsDeprecated = Boolean.Parse(attrib.Value);
break;
case "follows":
case "for":
case "submitted":
case "added":
case "updated":
break;
default:
throw new NotSupportedException(attrib.Name);
}
}
foreach (XmlElement sub in element.ChildNodes) {
switch (sub.Name) {
case "title":
Title = sub.InnerText;
TitleElement = sub;
break;
case "version":
GameVersion version = createVersion(this, sub);
Versions.Add(version);
break;
case "comment":
Comment = sub.InnerText;
break;
default:
throw new NotSupportedException(sub.Name);
}
}
}
public int CompareTo(Game file) {
return this.Name.CompareTo(file.Name);
}
}
}