-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptFile.cs
More file actions
336 lines (297 loc) · 10.4 KB
/
ScriptFile.cs
File metadata and controls
336 lines (297 loc) · 10.4 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace PresentationPrompter
{
public static class ScriptFile
{
public static ScriptLoadResult Load(string path)
{
if (!File.Exists(path))
return null;
try
{
if (path.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
return LoadFromJson(path);
else
return LoadFromTxt(path);
}
catch
{
return null;
}
}
public static ScriptLoadResult LoadFromTxt(string path)
{
string[] lines = File.ReadAllLines(path, Encoding.UTF8);
if (lines.Length < 2)
return null;
var result = new ScriptLoadResult();
result.Title = lines[0];
result.Steps = new ScriptStep[lines.Length - 1];
for (int i = 1; i < lines.Length; i++)
result.Steps[i - 1] = new ScriptStep(lines[i]);
return result;
}
public static ScriptLoadResult LoadFromJson(string path)
{
string json = File.ReadAllText(path, Encoding.UTF8);
var reader = new JsonReader(json);
var obj = reader.ParseObject();
var result = new ScriptLoadResult();
result.Title = obj.GetString("title", "");
var stepsArr = obj.GetArray("steps");
if (stepsArr == null || stepsArr.Count == 0)
return null;
result.Steps = new ScriptStep[stepsArr.Count];
for (int i = 0; i < stepsArr.Count; i++)
{
var stepObj = stepsArr.GetObject(i);
result.Steps[i] = new ScriptStep
{
Text = stepObj.GetString("text", ""),
Command = stepObj.GetString("command", ""),
AutoRun = stepObj.GetBool("autoRun", false),
Delay = stepObj.GetInt("delay", 0)
};
}
return result;
}
public static void SaveJson(string path, string title, ScriptStep[] steps)
{
var sb = new StringBuilder();
sb.AppendLine("{");
sb.AppendLine(" \"title\": " + EscapeJsonString(title) + ",");
sb.AppendLine(" \"steps\": [");
for (int i = 0; i < steps.Length; i++)
{
var s = steps[i];
sb.Append(" { \"text\": " + EscapeJsonString(s.Text));
sb.Append(", \"command\": " + EscapeJsonString(s.Command));
sb.Append(", \"autoRun\": " + (s.AutoRun ? "true" : "false"));
sb.Append(", \"delay\": " + s.Delay.ToString());
sb.Append(" }");
if (i < steps.Length - 1)
sb.Append(",");
sb.AppendLine();
}
sb.AppendLine(" ]");
sb.AppendLine("}");
File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
}
public static string ExpandVariables(string command, int stepIndex, string title, int stepCount)
{
if (string.IsNullOrEmpty(command))
return command;
return command
.Replace("{step}", (stepIndex + 1).ToString())
.Replace("{title}", title)
.Replace("{stepCount}", stepCount.ToString());
}
private static string EscapeJsonString(string s)
{
if (s == null) return "\"\"";
sb.Length = 0;
sb.Append('"');
foreach (char c in s)
{
switch (c)
{
case '"': sb.Append("\\\""); break;
case '\\': sb.Append("\\\\"); break;
case '\n': sb.Append("\\n"); break;
case '\r': sb.Append("\\r"); break;
case '\t': sb.Append("\\t"); break;
default: sb.Append(c); break;
}
}
sb.Append('"');
return sb.ToString();
}
private static readonly StringBuilder sb = new StringBuilder();
}
public class ScriptLoadResult
{
public string Title;
public ScriptStep[] Steps;
}
#region Minimal JSON Reader
internal class JsonReader
{
private readonly string _json;
private int _pos;
public JsonReader(string json) { _json = json; _pos = 0; }
public JsonObject ParseObject()
{
SkipWhitespace();
Expect('{');
SkipWhitespace();
var obj = new JsonObject();
while (_pos < _json.Length && _json[_pos] != '}')
{
SkipWhitespace();
if (_json[_pos] == '}')
break;
string key = ReadString();
SkipWhitespace();
Expect(':');
SkipWhitespace();
object val = ReadValue();
obj.Set(key, val);
SkipWhitespace();
if (_pos < _json.Length && _json[_pos] == ',')
_pos++;
}
Expect('}');
return obj;
}
public JsonArray ParseArray()
{
SkipWhitespace();
Expect('[');
SkipWhitespace();
var arr = new JsonArray();
while (_pos < _json.Length && _json[_pos] != ']')
{
SkipWhitespace();
if (_json[_pos] == ']')
break;
arr.Add(ReadValue());
SkipWhitespace();
if (_pos < _json.Length && _json[_pos] == ',')
_pos++;
}
Expect(']');
return arr;
}
private object ReadValue()
{
SkipWhitespace();
if (_pos >= _json.Length) return null;
char c = _json[_pos];
if (c == '"') return ReadString();
if (c == '{') return ParseObject();
if (c == '[') return ParseArray();
if (c == 't') { _pos += 4; return true; }
if (c == 'f') { _pos += 5; return false; }
if (c == 'n') { _pos += 4; return null; }
if (c == '-' || (c >= '0' && c <= '9')) return ReadNumber();
return null;
}
private string ReadString()
{
Expect('"');
var sb = new StringBuilder();
while (_pos < _json.Length)
{
char c = _json[_pos++];
if (c == '"') return sb.ToString();
if (c == '\\')
{
if (_pos >= _json.Length) break;
char e = _json[_pos++];
switch (e)
{
case '"': sb.Append('"'); break;
case '\\': sb.Append('\\'); break;
case 'n': sb.Append('\n'); break;
case 'r': sb.Append('\r'); break;
case 't': sb.Append('\t'); break;
default: sb.Append(e); break;
}
}
else
sb.Append(c);
}
return sb.ToString();
}
private object ReadNumber()
{
int start = _pos;
if (_pos < _json.Length && _json[_pos] == '-') _pos++;
while (_pos < _json.Length && _json[_pos] >= '0' && _json[_pos] <= '9') _pos++;
if (_pos < _json.Length && _json[_pos] == '.')
{
_pos++;
while (_pos < _json.Length && _json[_pos] >= '0' && _json[_pos] <= '9') _pos++;
double d;
if (double.TryParse(_json.Substring(start, _pos - start),
System.Globalization.NumberStyles.Float,
System.Globalization.CultureInfo.InvariantCulture, out d))
return d;
return 0.0;
}
int n;
if (int.TryParse(_json.Substring(start, _pos - start), out n))
return n;
return 0;
}
private void SkipWhitespace()
{
while (_pos < _json.Length && char.IsWhiteSpace(_json[_pos]))
_pos++;
}
private void Expect(char c)
{
if (_pos < _json.Length && _json[_pos] == c)
_pos++;
}
}
internal class JsonObject
{
private readonly Dictionary<string, object> _data = new Dictionary<string, object>();
public void Set(string key, object val) { _data[key] = val; }
public string GetString(string key, string defaultVal = "")
{
object val;
if (_data.TryGetValue(key, out val) && val is string)
return (string)val;
return defaultVal;
}
public bool GetBool(string key, bool defaultVal = false)
{
object val;
if (_data.TryGetValue(key, out val) && val is bool)
return (bool)val;
return defaultVal;
}
public int GetInt(string key, int defaultVal = 0)
{
object val;
if (_data.TryGetValue(key, out val))
{
if (val is int) return (int)val;
if (val is double) return (int)(double)val;
}
return defaultVal;
}
public JsonArray GetArray(string key)
{
object val;
if (_data.TryGetValue(key, out val) && val is JsonArray)
return (JsonArray)val;
return null;
}
public JsonObject GetObject(string key)
{
object val;
if (_data.TryGetValue(key, out val) && val is JsonObject)
return (JsonObject)val;
return null;
}
}
internal class JsonArray
{
private readonly List<object> _items = new List<object>();
public int Count { get { return _items.Count; } }
public void Add(object item) { _items.Add(item); }
public JsonObject GetObject(int index)
{
if (index >= 0 && index < _items.Count && _items[index] is JsonObject)
return (JsonObject)_items[index];
return null;
}
}
#endregion
}