-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
337 lines (291 loc) · 9.43 KB
/
Parser.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
public class DialogueEngine
{
public Node currentNode;
private bool inOption;
private string currentOption;
private Dictionary<string, int> options = new Dictionary<string, int>();
private Dictionary<string, int> GetAllOptionNames(Node root)
{
Dictionary<string, int> optionNames = new Dictionary<string, int>();
void Traverse(Node node)
{
if (node == null) return;
if (node.Type == "option" && !optionNames.ContainsKey(node.Name))
{
optionNames.Add(node.Name, -1);
}
foreach (var nextNode in node.Next)
{
Traverse(nextNode);
}
}
Traverse(root);
return optionNames;
}
public DialogueEngine(Node root)
{
currentNode = root;
options = GetAllOptionNames(root);
}
public EngineNode GetNext()
{
if (inOption) throw new Exception("Must SelectOption() before getting next node");
if (currentNode.Next.Count == 0) return null;
currentNode = currentNode.Next[0];
EngineNode engineNode = CraftNode(ref currentNode);
return engineNode;
}
EngineNode CraftNode(ref Node currentNode)
{
EngineNode returnNode = new EngineNode();
switch (currentNode.Type)
{
case "scene":
returnNode.Type = DialogueType.scene;
returnNode.Name = currentNode.Content;
break;
case "end":
returnNode.Type = DialogueType.end;
break;
case "choice":
List<Tuple<string, int>> choices = currentNode.Content.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(part => part.Split(','))
.Select(subParts => Tuple.Create(subParts[0].Trim(), int.Parse(subParts[1].Trim()))).ToList();
Node through = currentNode.Next.Last();
foreach (Tuple<string, int> choice in choices)
{
if (options[choice.Item1] == choice.Item2)
{
through = currentNode.Next[choice.Item2-1];
break;
}
}
currentNode = through;
return CraftNode(ref currentNode);
case "option":
returnNode.Type = DialogueType.option;
returnNode.Choices = currentNode.Content.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(op => op.Trim()).ToList();
currentOption = currentNode.Name;
inOption = true;
break;
case "person_say":
returnNode.Type = DialogueType.person_say;
returnNode.Name = currentNode.Name;
returnNode.Content = currentNode.Content;
break;
case "person_think":
returnNode.Type = DialogueType.person_think;
returnNode.Name = currentNode.Name;
returnNode.Content = currentNode.Content;
break;
}
return returnNode;
}
public void SelectOption(int pos)
{
if (!inOption) return;
options[currentOption] = pos;
inOption = false;
}
}
public enum DialogueType
{
scene,
option,
end,
choice,
person_say,
person_think
}
public class EngineNode
{
public DialogueType Type;
public string Name = "";
public string Content = "";
public List<string> Choices = new List<string>();
}
public class Node
{
public string Type { get; set; }
public string Name { get; set; }
public string Content { get; set; }
public List<Node> Next { get; set; }
public Node()
{
Type = string.Empty;
Name = string.Empty;
Content = string.Empty;
Next = new List<Node>();
}
public override string ToString()
{
return $"{Type} | {Name} | {Content} | {Next.Count}";
}
}
class Parser
{
private List<string> lines;
private List<string> optionNames;
private int currentLine;
private List<Node> looseNodes;
public Parser(List<string> lines)
{
this.lines = lines;
this.optionNames = new List<string>();
this.currentLine = 0;
this.looseNodes = new List<Node>();
}
private List<string> GetIds(string line)
{
var ids = Regex.Split(line, @"<|>").Where((_, index) => index % 2 != 0).ToList();
return ids;
}
private string GetContent(string line)
{
return line.Substring(line.LastIndexOf('>') + 1).Trim();
}
public Node ParseFull()
{
Node root = Parse();
Node FixNodes(Node node)
{
if (node == null) return null;
int i = 0;
while (i < node.Next.Count)
{
Node nextNode = node.Next[i];
if (string.IsNullOrEmpty(nextNode.Type) || nextNode.Type == "dead")
{
node.Next.RemoveAt(i);
node.Next.InsertRange(i, nextNode.Next);
}
else
{
FixNodes(nextNode);
i++;
}
}
return node;
}
return FixNodes(root);
}
private Node Parse()
{
Node currentNode = new Node();
Node root = currentNode;
int looseTarget = -1;
while (currentLine < lines.Count)
{
string line = lines[currentLine];
List<string> ids = GetIds(line);
if (ids.Count == 0) break;
if (ids.Count == 1)
{
switch (ids[0])
{
case "scene":
currentNode.Type = "scene";
currentNode.Content = GetContent(line);
break;
case "option":
currentNode.Type = "option";
currentNode.Name = GetContent(line);
currentNode.Content = ParseOption();
optionNames.Add(currentNode.Name);
break;
case "END":
currentNode.Type = "end";
return root;
case "choices":
currentLine++;
currentNode.Type = "choice";
(int looseTargetDec, List<List<string>> options) = GetChoiceEnd();
looseTarget = looseTargetDec;
currentNode.Content = string.Join("; ", options.Select(opt => string.Join(",", opt)));
foreach (var _ in options)
{
Node x = Parse();
currentNode.Next.Add(x);
}
break;
case "//":
currentLine++;
currentNode.Type = "dead";
looseNodes.Add(currentNode);
return root;
case "/":
break;
default:
currentNode.Type = "person_say";
currentNode.Name = ids[0];
currentNode.Content = GetContent(line);
break;
}
}
else
{
if (optionNames.Contains(ids[0]))
{
currentLine++;
continue;
}
currentNode.Type = "person_think";
currentNode.Name = ids[0];
currentNode.Content = GetContent(line);
}
if (currentLine == looseTarget)
{
foreach (var end in looseNodes)
{
end.Next.Add(currentNode);
}
}
Node nextNode = new Node();
currentNode.Next.Add(nextNode);
currentNode = nextNode;
currentLine++;
}
return root;
}
private string ParseOption()
{
currentLine++;
string content = "";
while (GetIds(lines[currentLine])[0] != "/")
{
content += GetContent(lines[currentLine]) + "; ";
currentLine++;
}
return content;
}
private (int, List<List<string>>) GetChoiceEnd()
{
int tracker = currentLine;
List<List<string>> options = new List<List<string>>();
options.Add(GetIds(lines[tracker]));
int inOption = 1;
int blockLevel = 1;
while (blockLevel != 0)
{
tracker++;
string lineId = GetIds(lines[tracker]).First();
if (lineId == "choices" || lineId == "option")
blockLevel++;
else if (lineId == "/")
blockLevel--;
if (optionNames.Contains(lineId))
{
inOption++;
if (inOption == 1)
options.Add(GetIds(lines[tracker]));
}
if (lineId == "//")
inOption--;
}
return (tracker + 1, options);
}
}