This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheaterStructure.cs
258 lines (244 loc) · 9.27 KB
/
TheaterStructure.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Insurgency_theater_editor
{
public struct TheaterBlock
{
public bool IsContainer {
get {
return (Value == null);
}
}
public string Key { get; private set; }
public string Value { get; private set; }
public List<TheaterBlock> Childs { get; private set; }
public int Level { get; private set; }
public int Type;
public int Line;
public TheaterBlock(string key, string value, int level)
{
Key = key;
Value = value;
Childs = null;
Level = level;
Line = Type = 0;
}
public TheaterBlock(string key, IEnumerable<TheaterBlock> childs, int level)
{
Key = key;
Value = null;
Childs = new List<TheaterBlock>(childs);
Level = level;
Line = Type = 0;
}
}
public class TheaterStructure
{
public static readonly string[] CATEGORIS = { "ammo", "explosives", "player_settings", "weapons", "weapon_upgrades", "player_gear", "player_templates", "core", "teams" };
public List<TheaterBlock> Root;
public TimeSpan ParsingTimeSpan;
public TheaterStructure()
{
Root = new List<TheaterBlock>();
}
public TheaterStructure(string theater) : this()
{
Parse(theater);
}
public IEnumerator<TheaterBlock> GetEnumerator()
{
Stack<TheaterBlock> stack = new Stack<TheaterBlock>();
for (int i = Root.Count - 1; i >= 0; --i)
{
stack.Push(Root[i]);
}
while(stack.Count > 0)
{
TheaterBlock block = stack.Pop();
yield return block;
if (block.IsContainer)
{
for(int i = block.Childs.Count - 1; i >= 0; --i)
{
stack.Push(block.Childs[i]);
}
}
}
}
public void Parse(string theater)
{
Stopwatch FunctionTimer = new Stopwatch();
FunctionTimer.Start();
Tokenize(theater);
ValidateToken();
FunctionTimer.Stop();
ParsingTimeSpan = FunctionTimer.Elapsed;
}
public void Tokenize(string theater)
{
int currentLine = 1;
theater = theater.Trim();
Stack<List<TheaterBlock>> stack = new Stack<List<TheaterBlock>>();
stack.Push(Root);
string key = null;
StringBuilder builder = new StringBuilder();
bool buildingIdentifier = false;
bool isComment = false;
while(!string.IsNullOrEmpty(theater))
{
char c = theater[0];
theater = theater.Substring(1);
switch (c)
{
case '/':
if (theater[0] == '/')
{
isComment = true;
theater = theater.Substring(1);
}
break;
case '\"':
if (isComment)
break;
if (buildingIdentifier == false)
{
buildingIdentifier = true;
} else
{
buildingIdentifier = false;
if (key == null)
{
key = builder.ToString();
}
else
{
string value = builder.ToString();
TheaterBlock blk = new TheaterBlock(key, value, stack.Count - 1);
blk.Line = currentLine;
stack.Peek().Add(blk);
key = null;
}
builder.Clear();
}
break;
case '{':
if (isComment)
break;
if (buildingIdentifier)
throw new Exception("Invalid syntax(" + currentLine + "): Cannot use brace as identifier.");
if (key == null)
throw new Exception("Invalid syntax(" + currentLine + "): No identifier for this collection found.");
TheaterBlock block = new TheaterBlock(key, new List<TheaterBlock>(), stack.Count - 1);
block.Line = currentLine;
key = null;
stack.Peek().Add(block);
stack.Push(block.Childs);
break;
case '}':
if (isComment)
break;
if (buildingIdentifier)
throw new Exception("Invalid syntax(" + currentLine + "): Cannot use brace as identifier.");
stack.Pop();
break;
case ' ':
if (isComment)
break;
if (buildingIdentifier)
{
builder.Append(c);
break;
}
break;
case '\t':
if (isComment)
break;
if (buildingIdentifier)
throw new Exception("Invalid syntax(" + currentLine + "): Cannot use tab character as identifier.");
break;
case '\n':
isComment = false;
currentLine++;
if (buildingIdentifier)
throw new Exception("Invalid syntax(" + currentLine + "): Cannot use new line character as identifier.");
break;
default:
if (isComment)
break;
if (buildingIdentifier)
{
builder.Append(c);
break;
}
break;
}
}
if (stack.Count != 1)
{
throw new Exception("Invalid syntax(" + currentLine + "): Number of braces does not match.");
}
}
public void ValidateToken()
{
int level = 0;
for (var iter = GetEnumerator(); iter.MoveNext();)
{
TheaterBlock block = iter.Current;
if (block.IsContainer)
{
switch(level)
{
case 0:
if (block.Key.CompareTo("theater") != 0)
{
throw new Exception("Syntax error(" + block.Line + "): First collection must be \"theater\".");
}
break;
case 1:
bool contain = false;
foreach(string cat in CATEGORIS)
{
if (block.Key.CompareTo(cat) == 0)
{
contain = true;
break;
}
}
if (!contain)
{
StringBuilder builder = new StringBuilder("Syntax error(");
builder.Append(block.Line);
builder.Append("): Second collection must be one of the next.\n");
foreach (string cat in CATEGORIS)
{
builder.Append(cat);
builder.Append(", ");
}
builder.Remove(builder.Length - 2, 2);
throw new Exception(builder.ToString());
}
break;
default:
break;
}
level++;
} else
{
switch(block.Key)
{
case "#base":
if (level > 0)
throw new Exception("Syntax error(" + block.Line + "): \"#base\" must be top of file.");
break;
default:
break;
}
}
}
}
}
}