forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExecutionContext.cs
406 lines (325 loc) · 11.5 KB
/
ExecutionContext.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
namespace kOS
{
public enum ExecutionState { NEW, DONE, WAIT };
public enum SpecialKey { HOME, END, DELETE };
public enum SystemMessage { CLEARSCREEN, SHUTDOWN, RESTART };
public class ExecutionContext
{
public static int COLUMNS = 50;
public static int ROWS = 36;
public CPU Cpu;
public Queue<Command> Queue = new Queue<Command>();
public String buffer;
public ExecutionContext ParentContext = null;
public ExecutionContext ChildContext = null;
public ExecutionState State = ExecutionState.NEW;
public int Line = 0;
public virtual Volume SelectedVolume
{
get { return ParentContext != null ? ParentContext.SelectedVolume : null; }
set { if (ParentContext != null) ParentContext.SelectedVolume = value; }
}
public virtual Vessel Vessel { get { return ParentContext != null ? ParentContext.Vessel : null; } }
public virtual List<Volume> Volumes { get { return ParentContext != null ? ParentContext.Volumes : null; } }
public virtual Dictionary<String, Variable> Variables { get { return ParentContext != null ? ParentContext.Variables : null; } }
public virtual List<kOSExternalFunction> ExternalFunctions { get { return ParentContext != null ? ParentContext.ExternalFunctions : null; } }
public Dictionary<String, Expression> Locks = new Dictionary<string, Expression>();
public List<Command> CommandLocks = new List<Command>();
public ExecutionContext()
{
}
public ExecutionContext(ExecutionContext parent)
{
this.ParentContext = parent;
}
public virtual void VerifyMount()
{
if (ParentContext != null) ParentContext.VerifyMount();
}
public bool KeyInput(char c)
{
if (ChildContext != null) return ChildContext.Type(c);
return Type(c);
}
public virtual bool Type(char c)
{
if (ChildContext != null) return ChildContext.Type(c);
return false;
}
public virtual bool SpecialKey(kOSKeys key)
{
if (ChildContext != null) return ChildContext.SpecialKey(key);
return false;
}
public virtual char[,] GetBuffer()
{
return (ChildContext != null) ? ChildContext.GetBuffer() : null;
}
public virtual void StdOut(String text)
{
if (ParentContext != null) ParentContext.StdOut(text);
}
public virtual void Put(String text, int x, int y)
{
if (ParentContext != null) ParentContext.Put(text, x, y);
}
public virtual void Update(float time)
{
// Process Command locks
foreach (Command command in new List<Command>(CommandLocks))
{
command.Update(time);
}
if (ChildContext != null)
{
if (ChildContext.State == ExecutionState.DONE)
{
ChildContext = null;
}
else
{
ChildContext.Update(time);
}
}
}
public virtual void Push(ExecutionContext newChild)
{
ChildContext = newChild;
}
public virtual bool Break()
{
if (ParentContext != null) return ParentContext.Break();
return false;
}
public Variable FindVariable(string varName)
{
varName = varName.ToLower();
Variable v = Variables.ContainsKey(varName) ? Variables[varName] : null;
if (v == null && ParentContext != null)
{
v = ParentContext.FindVariable(varName);
}
if (v != null && Locks.ContainsKey(varName.ToUpper()))
{
v.Value = Locks[varName.ToUpper()].GetValue();
}
return v;
}
public Variable CreateVariable(string varName)
{
varName = varName.ToLower();
var v = new Variable();
Variables.Add(varName, v);
return v;
}
public Variable FindOrCreateVariable(string varName)
{
varName = varName.ToLower();
Variable v = FindVariable(varName);
if (v == null)
{
v = CreateVariable(varName);
}
return v;
}
public virtual BoundVariable CreateBoundVariable(string varName)
{
return ParentContext.CreateBoundVariable(varName);
}
public virtual bool SwitchToVolume(int volID)
{
if (ParentContext != null) return ParentContext.SwitchToVolume(volID);
return false;
}
public virtual bool SwitchToVolume(String volName)
{
if (ParentContext != null) return ParentContext.SwitchToVolume(volName);
return false;
}
public virtual Volume GetVolume(object volID)
{
if (volID is int)
{
if (Volumes.Count > (int)volID) return Volumes[(int)volID];
}
else if (volID is String)
{
String volName = volID.ToString().ToUpper();
foreach (Volume targetVolume in Volumes)
{
if (targetVolume.Name.ToUpper() == volName)
{
return targetVolume;
}
}
int outVal;
if (int.TryParse((String)volID, out outVal))
{
if (Volumes.Count > outVal) return Volumes[outVal];
}
}
throw new kOSException("Volume '" + volID.ToString() + "' not found");
}
public ExecutionContext GetDeepestChildContext()
{
return ChildContext == null ? this : ChildContext.GetDeepestChildContext();
}
public T FindClosestParentOfType<T>() where T : ExecutionContext
{
if (this is T) return (T)this;
else if (ParentContext == null) return null;
else return ParentContext.FindClosestParentOfType<T>();
}
public void UpdateLock(String name)
{
Expression e = GetLock(name);
if (e != null)
{
var v = FindVariable(name);
v.Value = e.GetValue();
}
}
public virtual Expression GetLock(String name)
{
if (Locks.ContainsKey(name.ToUpper()))
{
return Locks[name.ToUpper()];
}
else
{
return ParentContext == null ? null : ParentContext.GetLock(name);
}
}
public virtual void Lock(Command command)
{
CommandLocks.Add(command);
}
public virtual void Lock(String name, Expression expression)
{
name = name.ToLower();
FindOrCreateVariable(name);
if (!Locks.ContainsKey(name.ToUpper()))
{
Locks.Add(name.ToUpper(), expression);
}
}
public virtual void Unlock(Command command)
{
CommandLocks.Remove(command);
if (ParentContext != null) ParentContext.Unlock(command);
}
public virtual void Unlock(String name)
{
name = name.ToLower();
if (Locks.ContainsKey(name.ToUpper()))
{
Locks.Remove(name.ToUpper());
}
else if (ParentContext != null)
{
ParentContext.Unlock(name);
}
}
public virtual void UnlockAll()
{
Locks.Clear();
if (ParentContext != null) ParentContext.UnlockAll();
}
public bool parseNext(ref string buffer, out string cmd, ref int lineCount, out int lineStart)
{
lineStart = -1;
for (int i = 0; i < buffer.Length; i++)
{
string c = buffer.Substring(i, 1);
if (lineStart < 0 && Regex.Match(c, "\\S").Success) lineStart = lineCount;
else if (c == "\n") lineCount++;
if (c == "\"")
{
i = Utils.FindEndOfString(buffer, i + 1);
if (i == -1)
{
cmd = "";
return false;
}
}
else if (c == ".")
{
int pTest;
if (i == buffer.Length - 1 || int.TryParse(buffer.Substring(i + 1, 1), out pTest) == false)
{
cmd = buffer.Substring(0, i);
buffer = buffer.Substring(i + 1).Trim();
return true;
}
}
else if (c == "{")
{
i = Utils.BraceMatch(buffer, i);
if (i == -1)
{
cmd = "";
return false;
}
else
{
// Do you see a period after this right brace? If not, let's just pretend there is one ok?
if (!buffer.Substring(i + 1).StartsWith("."))
{
cmd = buffer.Substring(0, i + 1);
buffer = buffer.Substring(i + 1).Trim();
return true;
}
}
}
}
cmd = "";
return false;
}
public virtual void SendMessage(SystemMessage message)
{
if (ParentContext != null) ParentContext.SendMessage(message);
}
public virtual int GetCursorX()
{
return ChildContext != null ? ChildContext.GetCursorX() : -1;
}
public virtual int GetCursorY()
{
return ChildContext != null ? ChildContext.GetCursorY() : -1;
}
public virtual object CallExternalFunction(String name, string[] parameters)
{
if (ParentContext != null) return ParentContext.CallExternalFunction(name, parameters);
return null;
}
public virtual bool FindExternalFunction(String name)
{
if (ParentContext != null) return ParentContext.FindExternalFunction(name);
return false;
}
public virtual void OnSave(ConfigNode node)
{
ConfigNode contextNode = new ConfigNode("context");
contextNode.AddValue("context-type", this.GetType().ToString());
if (ChildContext != null)
{
ChildContext.OnSave(contextNode);
}
node.AddNode(contextNode);
}
public virtual void OnLoad(ConfigNode node)
{
}
public virtual string GetVolumeBestIdentifier(Volume SelectedVolume)
{
if (ParentContext != null) return ParentContext.GetVolumeBestIdentifier(SelectedVolume);
return "";
}
}
}