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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*
* MIT License
*
* Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Reflection;
using ShiftOS.Objects.ShiftFS;
using DynamicLua;
using System.Dynamic;
using Newtonsoft.Json;
using System.Windows.Forms;
using System.Threading;
namespace ShiftOS.Engine.Scripting
{
public class LuaInterpreter
{
dynamic Lua = new DynamicLua.DynamicLua();
public bool Running = true;
public LuaInterpreter()
{
SetupAPIs();
Application.ApplicationExit += (o, a) =>
{
Running = false;
};
}
public void SetupAPIs()
{
//This temporary proxy() method will be used by the API prober.
Lua.proxy = new Func<string, dynamic>((objName) =>
{
var asm = Assembly.GetExecutingAssembly();
foreach (var type in asm.GetTypes())
{
if (type.Name == objName)
{
dynamic dynObj = Activator.CreateInstance(type);
return dynObj;
}
}
throw new Exception("{CLASS_NOT_FOUND}");
});
var thisasm = Assembly.GetExecutingAssembly();
foreach (var type in thisasm.GetTypes())
{
foreach (var attr in type.GetCustomAttributes(false))
{
if (attr is ExposedAttribute)
{
var eattr = attr as ExposedAttribute;
Lua($"{eattr.Name} = proxy(\"{type.Name}\")");
}
}
}
//Now we can null out the proxy() method as it can cause security risks.
Lua.isRunning = new Func<bool>(() => { return this.Running; });
Lua.proxy = null;
Lua.invokeOnWorkerThread = new Action<string>((func) =>
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
Lua(func + "()");
}));
});
Lua.invokeOnNewThread = new Action<string>((func) =>
{
var t = new Thread(new ThreadStart(() =>
{
Lua(func + "()");
}));
t.IsBackground = true;
t.Start();
});
}
public void ExecuteFile(string file)
{
if (Utils.FileExists(file))
{
Execute(Utils.ReadAllText(file));
}
else
{
throw new Exception("The file \"" + file + "\" was not found on the system.");
}
}
public void Execute(string lua)
{
Console.WriteLine("");
Lua(lua);
Console.WriteLine($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ");
}
}
public class LuaVirus : Virus
{
public override string Signature
{
get
{
return signature;
}
}
public override int ThreatLevel
{
get
{
return _threatlevel;
}
}
public override string Type
{
get
{
return "lua";
}
}
private LuaInterpreter interpreter = null;
private string signature = "unknown";
private int _threatlevel = 0;
public LuaVirus(string script, int threatLevel) : base()
{
_threatlevel = threatLevel;
interpreter = new LuaInterpreter();
Action<string> mud_message = new Action<string>((sig) =>
{
signature = sig;
});
ServerManager.MessageReceived += (msg) =>
{
if(msg.Name == "mud_virus_signature")
{
mud_message?.Invoke(msg.Contents);
mud_message = null;
}
};
ServerManager.SendMessage("mud_scanvirus", script);
_script = script;
}
private string _script = "";
public override void Activate()
{
interpreter.Execute(_script);
}
public override void Deactivate()
{
interpreter.Running = false;
interpreter = null;
}
}
[Exposed("consts")]
public class Constants
{
public readonly DockStyle dock_none = DockStyle.None;
public readonly DockStyle dock_top = DockStyle.Top;
public readonly DockStyle dock_bottom = DockStyle.Bottom;
public readonly DockStyle dock_left = DockStyle.Left;
public readonly DockStyle dock_right = DockStyle.Right;
public readonly DockStyle dock_fill = DockStyle.Fill;
public readonly AnchorStyles anchor_none = 0x00;
public readonly AnchorStyles anchor_top = AnchorStyles.Top;
public readonly AnchorStyles anchor_bottom = AnchorStyles.Bottom;
public readonly AnchorStyles anchor_left = AnchorStyles.Left;
public readonly AnchorStyles anchor_right = AnchorStyles.Right;
}
[Exposed("shiftos")]
public class CoreAPI
{
public void sleep(int ms) { Thread.Sleep(ms); }
public bool isRunning() { return !SaveSystem.ShuttingDown; }
public int random(int min, int max)
{
return new Random().Next(min, max);
}
public void info(string title, string message)
{
Infobox.Show(title, message);
}
public void executeCommand(string cmd)
{
TerminalBackend.InvokeCommand(cmd);
}
public void shutdown()
{
TerminalBackend.InvokeCommand("sys.shutdown");
}
}
[Exposed("shiftorium")]
public class ShiftoriumAPI
{
public dynamic[] getAvailable()
{
return Shiftorium.GetAvailable();
}
public dynamic[] getAll()
{
return Shiftorium.GetDefaults().ToArray();
}
public bool upgradeInstalled(string id)
{
return Shiftorium.UpgradeInstalled(id);
}
public bool buy(ShiftoriumUpgrade upgrade)
{
foreach (var upg in getAvailable())
{
if (upg.ID == upgrade && SaveSystem.CurrentSave.Codepoints >= upg.Cost)
{
return true;
}
}
return false;
}
}
[Exposed("gui")]
public class GuiAPI
{
public void addControl(dynamic parent, dynamic child)
{
parent.Controls.Add(child);
}
public dynamic size(int width, int height)
{
return new System.Drawing.Size(width, height);
}
public dynamic point(int x, int y)
{
return new System.Drawing.Point(x, y);
}
public dynamic rect(dynamic p, dynamic s)
{
return new System.Drawing.Rectangle(p, s);
}
public dynamic rect(int x, int y, int w, int h)
{
return new System.Drawing.Rectangle(x, y, w, h);
}
public dynamic panel()
{
return new Panel();
}
public dynamic label()
{
return new Label();
}
public dynamic button()
{
return new Button();
}
public dynamic listbox()
{
return new ListBox();
}
public dynamic combobox()
{
return new ComboBox();
}
public dynamic textbox()
{
return new TextBox();
}
public dynamic window()
{
return new Form();
}
}
[Exposed("color")]
public class ColorAPI
{
public dynamic fromRgb(int r, int g, int b)
{
return System.Drawing.Color.FromArgb(r, g, b);
}
public dynamic fromArgb(int a, int r, int g, int b)
{
return System.Drawing.Color.FromArgb(a, r, g, b);
}
public dynamic fromName(string name)
{
return System.Drawing.Color.FromName(name);
}
}
[Exposed("fonts")]
public class FontsAPI
{
public readonly int style_regular = 0x00;
public readonly int style_bold = 0x01;
public readonly int style_italic = 0x02;
public readonly int style_underline = 0x04;
public readonly int style_strike = 0x08;
public dynamic create(string name, float size, int style)
{
return new System.Drawing.Font(name, size, (System.Drawing.FontStyle)style);
}
}
[Exposed("json")]
public class JsonAPI
{
public string serialize(dynamic dynObj)
{
return JsonConvert.SerializeObject(dynObj);
}
public dynamic deserialize(string json)
{
return JsonConvert.DeserializeObject<dynamic>(json);
}
}
[Exposed("fs")]
public class FilesystemAPI
{
public void writeAllText(string target, string contents)
{
Utils.WriteAllText(target, contents);
}
public string readAllText(string file)
{
return Utils.ReadAllText(file);
}
public bool fileExists(string file)
{
return Utils.FileExists(file);
}
public bool directoryExists(string dir)
{
return Utils.DirectoryExists(dir);
}
public void createDirectory(string dir)
{
Utils.CreateDirectory(dir);
}
//experimental - no idea how arrays will be handled in Lua...
public string[] getFiles(string dir)
{
return Utils.GetFiles(dir);
}
public string[] getDirectories(string dir)
{
return Utils.GetDirectories(dir);
}
public string mount(string mfsFile)
{
Utils.MountPersistent(mfsFile);
return $"{Utils.Mounts.Count - 1}:";
}
public void unmount(int driveNum)
{
Utils.Mounts.RemoveAt(driveNum);
}
}
public class PythonInterpreter
{
ScriptEngine python;
ScriptScope pyScope;
ScriptSource pySource;
public PythonInterpreter()
{
python = Python.CreateEngine();
pyScope = python.CreateScope();
AddExposedObjects();
}
private void AddExposedObjects()
{
var asm = Assembly.GetExecutingAssembly();
foreach(var type in asm.GetTypes())
{
foreach(var attr in type.GetCustomAttributes(false))
{
if(attr is ExposedAttribute)
{
var eattr = attr as ExposedAttribute;
dynamic dynObj = Activator.CreateInstance(type);
pyScope.SetVariable(eattr.Name, dynObj);
}
}
}
}
public void ExecuteFile(string file)
{
if(Utils.FileExists(file))
{
Execute(Utils.ReadAllText(file));
}
else
{
throw new Exception("The file \"" + file + "\" was not found on the system.");
}
}
public void Execute(string py)
{
Console.WriteLine("");
pySource = python.CreateScriptSourceFromString(py, Microsoft.Scripting.SourceCodeKind.AutoDetect);
pySource.Execute(pyScope);
Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ");
}
}
public class ExposedAttribute : Attribute
{
/// <summary>
/// Marks the following class as exposed to the ShiftOS Scripting System.
/// </summary>
/// <param name="objName">The object name that the Scripting System should use as the variable name for scripts.</param>
public ExposedAttribute(string objName)
{
Name = objName;
}
public string Name { get; set; }
}
}
|