-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCPU.cs
401 lines (334 loc) · 12.7 KB
/
CPU.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
namespace kOS
{
public class CPU : ExecutionContext
{
public object Parent;
public enum Modes { READY, STARVED, OFF };
public Modes Mode = Modes.READY;
public String Context;
public Archive archive;
public BindingManager bindingManager;
public float SessionTime;
public int ClockSpeed = 5;
public TerminalButton[] ButtonStates = new TerminalButton[FUNCTION_BUTTON_COUNT + 1];
public bool InternalDisplayEnabled = true;
private Dictionary<String, Variable> variables = new Dictionary<String, Variable>();
private Volume selectedVolume = null;
private List<Volume> volumes = new List<Volume>();
private List<kOSExternalFunction> externalFunctions = new List<kOSExternalFunction>();
public override Vessel Vessel { get { return ((kOSProcessor)Parent).vessel; } }
public override Dictionary<String, Variable> Variables { get { return variables; } }
public override List<Volume> Volumes { get { return volumes; } }
public override List<kOSExternalFunction> ExternalFunctions { get { return externalFunctions; } }
public static kOSRunType RunType = kOSRunType.KSP;
public enum kOSRunType { KSP, WINFORMS };
public static int FUNCTION_BUTTON_COUNT = 7;
public override Volume SelectedVolume
{
get { return selectedVolume; }
set { selectedVolume = value; }
}
public CPU(object parent, string context)
{
this.Parent = parent;
this.Context = context;
for (int i = 0; i < FUNCTION_BUTTON_COUNT; i++) ButtonStates[i] = new TerminalButton(this, i);
bindingManager = new BindingManager(this, Context);
if (context == "ksp")
{
RunType = kOSRunType.KSP;
archive = new Archive(Vessel);
Volumes.Add(archive);
}
else
{
RunType = kOSRunType.WINFORMS;
}
}
public double testFunction(double x, double y) { return x * y; }
public void RegisterkOSExternalFunction(object[] parameters)
{
if (parameters.Count() == 4)
{
var name = (String)parameters[0];
var parent = parameters[1];
var methodName = (String)parameters[2];
var parameterCount = (int)parameters[3];
RegisterkOSExternalFunction(name, parent, methodName, parameterCount);
}
}
public void RegisterkOSExternalFunction(String name, object parent, String methodName, int parameterCount)
{
externalFunctions.Add(new kOSExternalFunction(name.ToUpper(), parent, methodName, parameterCount));
}
public override object CallExternalFunction(string name, string[] parameters)
{
bool callFound = false;
bool callAndParamCountFound = false;
foreach (var function in ExternalFunctions)
{
if (function.Name == name.ToUpper())
{
callFound = true;
if (function.ParameterCount == parameters.Count())
{
callAndParamCountFound = true;
Type t = function.Parent.GetType();
var method = t.GetMethod(function.MethodName);
// Attempt to cast the strings to types that the target method is expecting
var parameterInfoArray = method.GetParameters();
object[] convertedParams = new object[parameters.Length];
for (var i = 0; i < parameters.Length; i++)
{
Type paramType = parameterInfoArray[i].ParameterType;
String value = parameters[i];
object converted = null;
if (paramType == typeof(String))
{
converted = parameters[i];
}
else if (paramType == typeof(float))
{
float flt;
if (float.TryParse(value, out flt)) converted = flt;
}
else if (paramType == typeof(double))
{
double dbl;
if (double.TryParse(value, out dbl)) converted = dbl;
}
else if (paramType == typeof(int))
{
int itgr;
if (int.TryParse(value, out itgr)) converted = itgr;
}
else if (paramType == typeof(long))
{
long lng;
if (long.TryParse(value, out lng)) converted = lng;
}
else if (paramType == typeof(bool))
{
bool bln;
if (bool.TryParse(value, out bln)) converted = bln;
}
if (converted == null) throw new kOSException("Parameter types don't match");
convertedParams[i] = converted;
}
return method.Invoke(function.Parent, convertedParams);
}
}
}
if (!callFound) throw new kOSException("External function '" + name + "' not found");
else if (!callAndParamCountFound) throw new kOSException("Wrong number of arguments for '" + name + "'");
return null;
}
public override bool FindExternalFunction(string name)
{
foreach (var function in ExternalFunctions)
{
if (function.Name == name.ToUpper())
{
return true;
}
}
return false;
}
public void Boot()
{
Mode = Modes.READY;
Push(new InterpreterBootup(this));
if (Volumes.Count > 1)
SelectedVolume = Volumes[1];
else
SelectedVolume = Volumes[0];
}
public bool IsAlive()
{
var partState = ((kOSProcessor)this.Parent).part.State;
if (partState == PartStates.DEAD)
{
Mode = Modes.OFF;
return false;
}
return true;
}
public void AttachHardDisk(Harddisk hardDisk)
{
Volumes.Add(hardDisk);
SelectedVolume = hardDisk;
}
public override void VerifyMount()
{
selectedVolume.CheckRange();
}
internal void ProcessElectricity(Part part, float time)
{
if (Mode == Modes.OFF) return;
var electricReq = 0.01f * time;
var result = part.RequestResource("ElectricCharge", electricReq) / electricReq;
var newMode = (result < 0.5f) ? Modes.STARVED : Modes.READY;
if (newMode == Modes.READY && Mode == Modes.STARVED)
{
Boot();
}
Mode = newMode;
}
public override bool SwitchToVolume(int volID)
{
if (Volumes.Count > volID)
{
var newVolume = Volumes[volID];
if (newVolume.CheckRange())
{
SelectedVolume = newVolume;
return true;
}
else
{
throw new kOSException("Volume disconnected - out of range");
}
}
return false;
}
public override bool SwitchToVolume(string targetVolume)
{
foreach (Volume volume in Volumes)
{
if (volume.Name.ToUpper() == targetVolume.ToUpper())
{
if (volume.CheckRange())
{
SelectedVolume = volume;
return true;
}
else
{
throw new kOSException("Volume disconnected - out of range");
}
}
}
return false;
}
public override BoundVariable CreateBoundVariable(string varName)
{
varName = varName.ToLower();
if (FindVariable(varName) == null)
{
variables.Add(varName, new BoundVariable());
return (BoundVariable)variables[varName];
}
else
{
throw new kOSException("Cannot bind " + varName + "; name already taken.");
}
}
public override void Update(float time)
{
bindingManager.Update(time);
SessionTime += time;
for (var i = 0; i < ClockSpeed; i++)
{
base.Update(time / (float)ClockSpeed);
}
if (Mode == Modes.STARVED)
{
ChildContext = null;
}
else if (Mode == Modes.OFF)
{
ChildContext = null;
}
// After booting
if (ChildContext == null)
{
Push(new ImmediateMode(this));
}
}
public override void SendMessage(SystemMessage message)
{
switch (message)
{
case SystemMessage.SHUTDOWN:
ChildContext = null;
Mode = Modes.OFF;
break;
case SystemMessage.RESTART:
ChildContext = null;
Boot();
break;
default:
base.SendMessage(message);
break;
}
}
internal void UpdateVolumeMounts(List<Volume> attachedVolumes)
{
// Remove volumes that are no longer attached
foreach (Volume volume in new List<Volume>(volumes))
{
if (!(volume is Archive) && !attachedVolumes.Contains(volume))
{
volumes.Remove(volume);
}
}
// Add volumes that have become attached
foreach (Volume volume in attachedVolumes)
{
if (!volumes.Contains(volume))
{
volumes.Add(volume);
}
}
}
public override void OnSave(ConfigNode node)
{
ConfigNode contextNode = new ConfigNode("context");
// Save variables
if (Variables.Count > 0)
{
ConfigNode varNode = new ConfigNode("variables");
foreach (var kvp in Variables)
{
if (!(kvp.Value is BoundVariable))
{
varNode.AddValue(kvp.Key, File.EncodeLine(kvp.Value.Value.ToString()));
}
}
contextNode.AddNode(varNode);
}
if (ChildContext != null)
{
ChildContext.OnSave(contextNode);
}
node.AddNode(contextNode);
}
public override void OnLoad(ConfigNode node)
{
foreach (ConfigNode contextNode in node.GetNodes("context"))
{
foreach (ConfigNode varNode in contextNode.GetNodes("variables"))
{
foreach (ConfigNode.Value value in varNode.values)
{
var newVar = CreateVariable(value.name);
newVar.Value = new Expression(File.DecodeLine(value.value), this).GetValue();
}
}
}
}
public override string GetVolumeBestIdentifier(Volume SelectedVolume)
{
int localIndex = volumes.IndexOf(SelectedVolume);
if (!String.IsNullOrEmpty(SelectedVolume.Name)) return "#" + localIndex + ": \"" + SelectedVolume.Name + "\"";
else return "#" + localIndex;
}
}
}