-
Notifications
You must be signed in to change notification settings - Fork 13
/
Script.cs
352 lines (306 loc) · 10.6 KB
/
Script.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
using Nes3D.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using XLua;
namespace Nes3D.Engine3D
{
#if USE_UNI_LUA
using LuaAPI = UniLua.Lua;
using RealStatePtr = UniLua.ILuaState;
using LuaCSFunction = UniLua.CSharpFunctionDelegate;
#else
using LuaAPI = XLua.LuaDLL.Lua;
using LuaCSFunction = XLua.LuaDLL.lua_CSFunction;
using RealStatePtr = System.IntPtr;
#endif
public partial class Script
{
#region static
private enum Function
{ Start, Update, UpdateS, End, Count };
public static readonly char[] separatos = { ',' };
private static string[] FNames;
public const string AllTag = "*";
public static LuaEnv session;
public static event Action<string> onNewError;
public static event Action<string> onNewInput;
public static event Action<string> onNewOutput;
public static event Action onClear;
public static void Init()
{
SettingManager.Ins.PropertyChanged += OnSettingChanged;
const string initScript = @"Vector3 = CS.UnityEngine.Vector3
Vector2 = CS.UnityEngine.Vector2
Color = CS.UnityEngine.Color
IntVector2 = CS.Nes3D.Utils.IntVector2
IntVector3 = CS.Nes3D.Utils.IntVector3
";
FNames = new string[(int)Function.Count];
for (int i = 0; i < FNames.Length; i++)
FNames[i] = ((Function)i).ToString();
session = new LuaEnv();
// overide default implementation of print function
LuaAPI.lua_pushstdcallcfunction(session.L, Print);
LuaAPI.xlua_setglobal(session.L, "print");
DoString(initScript);
AssignSetting();
session.Global.Set("gamepad", Gamepads.Ins);
session.Global.Set("gamepad1", Gamepads.Ins[0]);
session.Global.Set("gamepad2", Gamepads.Ins[1]);
session.Global.Set("Script", ScriptManager.Ins);
session.Global.Set("Frame", FrameManager.Ins);
session.Global.Set("clear", new Action(Clear));
}
private static void AssignSetting()
{
session.Global.Set("Setting", SettingManager.Ins);
session.Global.Set("Light", SettingManager.Ins.Light);
session.Global.Set("Layer", SettingManager.Ins.Layer);
session.Global.Set("Clipping", SettingManager.Ins.Clipping);
}
private static void OnSettingChanged(object sender, PropertyChangedEventArgs args)
{
if (ReferenceEquals(args.PropertyName, SettingManager.LightId)
|| ReferenceEquals(args.PropertyName, SettingManager.LayerId)
|| ReferenceEquals(args.PropertyName, SettingManager.ClippingId)
)
AssignSetting();
}
public static void DoString(string text)
{
try
{
onNewInput?.Invoke(text);
session.DoString(text, "console");
}
catch (LuaException e)
{
onNewError?.Invoke(e.Message);
}
}
private static void Clear()
{
onClear();
}
[MonoPInvokeCallback(typeof(LuaCSFunction))]
private static int Print(RealStatePtr L)
{
try
{
int n = LuaAPI.lua_gettop(L);
string s = String.Empty;
if (0 != LuaAPI.xlua_getglobal(L, "tostring"))
{
return LuaAPI.luaL_error(L, "can not get tostring in print:");
}
for (int i = 1; i <= n; i++)
{
LuaAPI.lua_pushvalue(L, -1); /* function to be called */
LuaAPI.lua_pushvalue(L, i); /* value to print */
if (0 != LuaAPI.lua_pcall(L, 1, 1, 0))
{
return LuaAPI.lua_error(L);
}
s += LuaAPI.lua_tostring(L, -1);
if (i != n) s += "\t";
LuaAPI.lua_pop(L, 1); /* pop result */
}
onNewOutput.Invoke(s);
return 0;
}
catch (System.Exception e)
{
return LuaAPI.luaL_error(L, "c# exception in print:" + e);
}
}
#endregion static
private Action start, update, end, updateShape;
public string name;
public bool enable;
public bool error;
private string source;
public string Source
{
get { return source; }
set
{
try
{
source = value;
LoadSource();
RunStart();
}
catch (LuaException e)
{
error = true;
start = update = null;
updateShape = null;
onNewError?.Invoke(e.Message);
}
}
}
private void LoadSource()
{
error = false;
try
{
onNewInput?.Invoke(name);
session.DoString(source, name);
session.Global.Get(FNames[0], out start);
session.Global.Get(FNames[1], out update);
session.Global.Get(FNames[2], out updateShape);
session.Global.Get(FNames[3], out end);
}
catch (Exception e)
{
error = true;
start = update = updateShape = end = null;
onNewError?.Invoke(e.Message);
}
}
public string Name
{
get { return name; }
set
{
if (!name.Equals(value))
{
ScriptManager.Ins.ChangeItemKey(this, value);
name = value;
}
}
}
private string tagStr;
public List<string> tags;
public string TagStr
{
get { return tagStr; }
set
{
tagStr = value;
CreateTags();
Relink();
}
}
private void CreateTags()
{
if (tags == null)
tags = new List<string>();
else
tags.Clear();
foreach (var s in tagStr.Split(separatos))
tags.Add(s.Trim());
}
public Script(string _name, string _source, string _tagStr, bool _enable)
{
name = _name;
enable = _enable;
source = _source;
start = end = update = null;
TagStr = _tagStr;
Source = _source;
}
public override string ToString()
{
return "Script " + name + " with Tags: " + tagStr;
}
private void UpdateContext()
{
session.Global.Set("self", this);
session.Global.Set("frame", FrameManager.Ins.frameCounter);
session.Global.Set("time", FrameManager.Ins.time);
}
public void RunStart()
{
if (start != null)
if (enable && !error)
try
{
UpdateContext();
start();
}
catch (LuaException e)
{
error = true;
onNewError(e.Message);
}
}
public void RunEnd()
{
if (end != null)
if (enable && !error)
try
{
UpdateContext();
end();
}
catch (LuaException e)
{
error = true;
onNewError(e.Message);
}
}
public void RunUpdate()
{
if (update != null)
if (enable & !error)
try
{
UpdateContext();
update();
}
catch (LuaException e)
{
error = true;
onNewError(e.Message);
}
}
public void RunUpdateShape(Pattern3D pattern)
{
if (updateShape != null)
if (enable & !error)
try
{
UpdateContext();
session.Global.Set("shape", pattern);
session.Global.Set("shape2D", pattern.Shape2D);
updateShape();
}
catch (LuaException e)
{
error = true;
onNewError(e.Message);
}
}
public void Delete()
{
ScriptManager.Ins.Remove(this);
}
public void Relink()
{
foreach (Pattern p in PatternManager.patterns)
foreach (Pattern3D p3D in p.pattern3DList)
p3D.LinkToScript(this);
foreach (Pattern p in PatternManager.tempPatterns)
foreach (Pattern3D p3D in p.pattern3DList)
p3D.LinkToScript(this);
}
public void UnLink()
{
foreach (Pattern p in PatternManager.patterns)
foreach (Pattern3D p3D in p.pattern3DList)
p3D.linkedScript.Remove(this);
foreach (Pattern p in PatternManager.tempPatterns)
foreach (Pattern3D p3D in p.pattern3DList)
p3D.linkedScript.Remove(this);
}
public void Adjust(string _name, string code, string _tagStr)
{
Source = code;
TagStr = _tagStr;
name = _name;
}
}
}