-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPythonEngine.cs
More file actions
142 lines (125 loc) · 5.07 KB
/
PythonEngine.cs
File metadata and controls
142 lines (125 loc) · 5.07 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
using IronPython.Hosting;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using SkylinesPythonShared;
using SkylinesPythonShared.API;
using SkylinesRemotePython.API;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace SkylinesRemotePython
{
public class PythonEngine
{
private ClientHandler client;
private ScriptEngine _engine;
private ScriptScope _scope;
private ObjectStorage _cachedObjects;
private EngineAPI _engineAPI;
internal GameAPI _gameAPI { get; private set; }
[ThreadStatic]
internal static PythonEngine Instance;
public PythonEngine(ClientHandler client)
{
this.client = client;
Instance = this;
_engine = Python.CreateEngine();
_scope = _engine.CreateScope();
_cachedObjects = new ObjectStorage(client);
_gameAPI = new GameAPI(client, _scope);
_engineAPI = new EngineAPI(client);
PrepareStaticLocals();
var outputStream = new MemoryStream();
var outputStreamWriter = new TcpStreamWriter(outputStream, client);
_engine.Runtime.IO.SetOutput(outputStream, outputStreamWriter);
}
public void RunScript(object obj)
{
RunScriptMessage msg = (RunScriptMessage)obj;
PrepareDynamicLocals(msg.clipboard);
_engine.SetSearchPaths(msg.searchPaths.ToList());
try
{
var source = _engine.CreateScriptSourceFromString(msg.script, SourceCodeKind.Statements);
var compiled = source.Compile();
try
{
compiled.Execute(_scope);
client.SendMsg(null, "c_script_end");
}
catch(Exception ex)
{
string details = "";
try {
var frame = PythonOps.GetDynamicStackFrames(ex).First();
string fileName = frame.GetFileName();
details = " (" + (fileName == "<string>" ? "script" : fileName) + ": line " + frame.GetFileLineNumber() + ")";
} catch { }
client.SendMsg(ex.Message + details, "c_exception");
}
}
catch(Exception ex)
{
client.SendMsg(ex.Message, "c_failed_to_compile");
}
}
private void PrepareStaticLocals()
{
SetStaticLocal(typeof(Vector));
SetStaticLocal(typeof(Point));
SetStaticLocal(typeof(NetOptions));
SetStaticLocal(typeof(NaturalResourceCell));
SetStaticLocal("vector_xz", new Func<double, double, Vector>(Vector.vector_xz));
SetStaticLocal("print_list", new Action<IEnumerable>(_gameAPI.print_list));
MethodInfo method = typeof(GameAPI).GetMethod("help", BindingFlags.Public | BindingFlags.Instance);
SetStaticLocal("help", Delegate.CreateDelegate(typeof(GameAPI.__HelpDeleg), _gameAPI, method));
SetStaticLocal("help_all", new Action(_gameAPI.help_all));
SetStaticLocal("list_globals", new Action(_gameAPI.list_globals));
SetStaticLocal("g", _gameAPI);
SetStaticLocal("game", _gameAPI);
SetStaticLocal("engine", _engineAPI);
}
private void SetStaticLocal(Type type)
{
object obj = DynamicHelpers.GetPythonTypeFromType(type);
_scope.SetVariable(type.Name, obj);
}
private void SetStaticLocal(string name, object obj)
{
_scope.SetVariable(name, obj);
}
private void PrepareDynamicLocals(object[] arr)
{
List<object> res = new List<object>();
object obj;
for(int i = 0; i < arr.Length; i++) {
if(arr[i] is Vector) {
obj = new Point((Vector)arr[i]);
} else if(arr[i] is NetNodeData d1) {
obj = ObjectStorage.Instance.Nodes.SaveData(d1);
} else if (arr[i] is NetSegmentData d2) {
obj = ObjectStorage.Instance.Segments.SaveData(d2);
} else if (arr[i] is BuildingData d3) {
obj = ObjectStorage.Instance.Buildings.SaveData(d3);
} else if (arr[i] is PropData d4) {
obj = ObjectStorage.Instance.Props.SaveData(d4);
} else if (arr[i] is TreeData d5) {
obj = ObjectStorage.Instance.Trees.SaveData(d5);
} else {
throw new Exception($"Unknown object {arr[i].GetType()}");
}
res.Add(obj);
}
_scope.SetVariable("cba", res);
if (res.Count > 0) {
_scope.SetVariable("cb", res[0]);
}
}
}
}