-
Notifications
You must be signed in to change notification settings - Fork 1
/
TreeParser.cs
206 lines (184 loc) · 6.51 KB
/
TreeParser.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using AVTTLoaderStandalone.Properties;
namespace AVTTLoaderStandalone
{
/// <summary>
/// Parser and writer for tree.cfg files
/// </summary>
class TreeParser
{
/// <summary>
/// The file path to the tree.cfg
/// </summary>
public string Path { get; private set; }
/// <summary>
/// The tree bound to this parser instance
/// </summary>
public Tree Tree { get; set; }
/// <summary>
/// Constructs a TreeParser instance bound to a specific file
/// </summary>
/// <param name="path">The full path to the tree.cfg</param>
public TreeParser(string path)
{
Path = path;
Tree = Load(path);
}
/// <summary>
/// Parses a tree.cfg file into a Tree class
/// </summary>
/// <param name="path">Full path to tree.cfg</param>
/// <returns>A Tree class with Nodes corresponding the tree.cfg data</returns>
private Tree Load(string path)
{
var treeString = "";
using (var reader = new StreamReader(path))
{
string line;
while ((line = reader.ReadLine()) != null)
{
treeString = treeString + line + Environment.NewLine;
}
}
var tree = new Tree(path);
var nodes = tree.Nodes;
var n = 0;
var b = false;
foreach (var str in Regex.Split(treeString, Environment.NewLine))
{
switch (str.Trim())
{
case "{":
n += 1;
b = true;
break;
case "}":
n -= 1;
break;
}
switch (n)
{
case 1:
if (b) { nodes.Add(new Node()); nodes.Last().Parts = new List<Attribute>(); }
if (str.Contains('=') == false)
break;
var ss = new Attribute(str.Trim());
switch (ss.Item)
{
case "name":
nodes.Last().Name.Value = ss.Value;
break;
case "techID":
nodes.Last().TechId.Value = ss.Value;
break;
case "pos":
nodes.Last().Pos.Value = ss.Value;
break;
case "icon":
nodes.Last().Icon.Value = ss.Value;
break;
case "cost":
nodes.Last().Cost.Value = ss.Value;
break;
case "title":
nodes.Last().Title.Value = ss.Value;
break;
case "description":
nodes.Last().Description.Value = ss.Value;
break;
case "anyParent":
nodes.Last().AnyParent.Value = ss.Value;
break;
case "hideIfEmpty":
nodes.Last().HideIfEmpty.Value = ss.Value;
break;
case "parents":
nodes.Last().Parents.Value = ss.Value;
break;
}
break;
case 2:
if (str.Contains('=') == false)
break;
nodes.Last().Parts.Add(new Attribute(str.Trim()));
break;
}
b = false;
}
return tree;
}
/// <summary>
/// Saves a Tree object to the file specified
/// </summary>
/// <param name="fileName">The filename to save it to</param>
private void Save(string fileName)
{
var nodes = Tree.Nodes;
var newLine = Environment.NewLine;
var cfgOutput = "";
var file = new StreamWriter(fileName);
foreach (var node in nodes)
{
cfgOutput += "NODE" + newLine + "{" + newLine;
foreach (var a in node.Attributes)
{
cfgOutput += "\t" + a.Item + " = " + a.Value + newLine;
}
cfgOutput += "\tPARTS" + newLine + "\t{" + newLine;
foreach (var part in node.Parts)
{
cfgOutput += "\t\t" + part.Item + " = " + part.Value + newLine;
}
cfgOutput += "\t}" + newLine + "}" + newLine;
}
file.Write(cfgOutput);
file.Close();
}
/// <summary>
/// Saves the tree to the filename property of the tree
/// </summary>
public void Save()
{
Save(Tree.FilePath);
}
/// <summary>
/// Saves a Tree object to the file specified. Overrides the default file name property of the tree.
/// </summary>
/// <param name="path">The file path to save it to</param>
public void SaveTo(string path)
{
Save(path);
}
public void Backup(string filename)
{
if (File.Exists(filename)) return;
var reader = new StreamReader(Path);
var writer = new StreamWriter(filename);
string line;
while ((line = reader.ReadLine()) != null)
{
writer.WriteLine(line);
}
reader.Close();
writer.Close();
}
public void Backup()
{
Backup(Settings.Default.BackupLocation);
}
public void Restore(string backupFile)
{
Tree = Load(backupFile);
Tree.FilePath = Path;
Save();
}
public void Restore()
{
Restore(Settings.Default.BackupLocation);
}
}
}