-
Notifications
You must be signed in to change notification settings - Fork 14
/
ArcadiaHook.cs
349 lines (325 loc) · 11.5 KB
/
ArcadiaHook.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
using Godot;
using System;
using System.Collections.Generic;
using Path = System.IO.Path;
using clojure.lang;
namespace Arcadia
{
public class Boot {
private static bool _initialized = false;
static void DisableSpecChecking()
{
System.Environment.SetEnvironmentVariable("CLOJURE_SPEC_CHECK_ASSERTS", "false");
System.Environment.SetEnvironmentVariable("CLOJURE_SPEC_SKIP_MACROS", "true");
System.Environment.SetEnvironmentVariable("clojure.spec.check-asserts", "false");
System.Environment.SetEnvironmentVariable("clojure.spec.skip-macros", "true");
}
public static void AddSourcePaths()
{
var env = System.Environment.GetEnvironmentVariable("CLOJURE_LOAD_PATH");
var SourcePaths = Util.Invoke(RT.var("arcadia.internal.config", "get-config-key"), "source-paths");
foreach(string SourcePath in RT.toArray(SourcePaths))
{
env = env + Path.PathSeparator + Path.Combine(System.IO.Directory.GetCurrentDirectory(), SourcePath);
}
System.Environment.SetEnvironmentVariable("CLOJURE_LOAD_PATH", env);
}
public static void SetClojureLoadPath()
{
System.Environment.SetEnvironmentVariable(
"CLOJURE_LOAD_PATH",
Path.Combine(System.IO.Directory.GetCurrentDirectory(), "ArcadiaGodot", "Source") +
Path.PathSeparator+
Path.Combine(System.IO.Directory.GetCurrentDirectory(), "ArcadiaGodot", "Clojure"));
}
public static void SetClojureLoadPathWithDLLs()
{
System.Environment.SetEnvironmentVariable(
"CLOJURE_LOAD_PATH",
Path.Combine(System.IO.Directory.GetCurrentDirectory(), "ArcadiaGodot", "Source") +
Path.PathSeparator +
Path.Combine(System.IO.Directory.GetCurrentDirectory(), "ArcadiaGodot", "Clojure") +
Path.PathSeparator +
Path.Combine(System.IO.Directory.GetCurrentDirectory(), "dlls"));
}
public static void Initialize()
{
if (!_initialized)
{
_initialized = true;
GD.Print("Starting Arcadia..");
DisableSpecChecking();
SetClojureLoadPathWithDLLs();
RT.load("clojure/core");
RT.load("arcadia/internal/namespace");
if (OS.IsDebugBuild()) {
RT.load("arcadia/repl");
Util.Invoke(RT.var("arcadia.repl", "launch"), null);
RT.load("arcadia/internal/config");
AddSourcePaths();
if (RT.booleanCast(Util.Invoke(RT.var("arcadia.internal.config", "get-config-key"), "reload-on-change")))
{
var watcher = new CrossPlatformArcadiaWatcher(false);
}
}
GD.Print("Arcadia loaded!");
}
}
}
public class ArcadiaHook : Node
{
public Node target;
public System.Object state;
private Dictionary<string, IFn> _enter_tree_fns = new Dictionary<string, IFn>();
private Dictionary<string, IFn> _exit_tree_fns = new Dictionary<string, IFn>();
private Dictionary<string, IFn> _ready_fns = new Dictionary<string, IFn>();
private Dictionary<string, IFn> _tree_ready_fns = new Dictionary<string, IFn>();
private Dictionary<string, IFn> _process_fns = new Dictionary<string, IFn>();
private Dictionary<string, IFn> _physics_process_fns = new Dictionary<string, IFn>();
private Dictionary<string, IFn> _input_fns = new Dictionary<string, IFn>();
private Dictionary<string, IFn> _unhandled_input_fns = new Dictionary<string, IFn>();
[Export]
public string ready_fn = "";
[Export]
public string tree_ready_fn = "";
[Export]
public string enter_tree_fn = "";
[Export]
public string exit_tree_fn = "";
[Export]
public string process_fn = "";
[Export]
public string physics_process_fn = "";
[Export]
public string input_fn = "";
[Export]
public string unhandled_input_fn = "";
public void RemoveAll(){
_enter_tree_fns.Clear();
_exit_tree_fns.Clear();
_ready_fns.Clear();
_tree_ready_fns.Clear();
_process_fns.Clear();
_physics_process_fns.Clear();
_input_fns.Clear();
_unhandled_input_fns.Clear();
}
public void Add(string type, string k, IFn v){
switch (type)
{
case "_enter_tree":
_enter_tree_fns[k] = v;
break;
case "_exit_tree":
_exit_tree_fns[k] = v;
break;
case "_ready":
_ready_fns[k] = v;
break;
case "_tree_ready":
_tree_ready_fns[k] = v;
break;
case "_process":
_process_fns[k] = v;
break;
case "_physics_process":
_physics_process_fns[k] = v;
break;
case "_input":
_input_fns[k] = v;
break;
case "_unhandled_input":
_unhandled_input_fns[k] = v;
break;
default:
throw new System.ArgumentException("unknown hook type: "+type, "type");
}
}
public void Remove(string type, string k){
switch (type)
{
case "_enter_tree_fns":
if (_enter_tree_fns.ContainsKey(k))
{
_enter_tree_fns.Remove(k);
}
break;
case "_exit_tree_fns":
if (_exit_tree_fns.ContainsKey(k))
{
_exit_tree_fns.Remove(k);
}
break;
case "_ready_fns":
if (_ready_fns.ContainsKey(k))
{
_ready_fns.Remove(k);
}
break;
case "_tree_ready_fns":
if (_tree_ready_fns.ContainsKey(k))
{
_tree_ready_fns.Remove(k);
}
break;
case "_process":
if (_process_fns.ContainsKey(k))
{
_process_fns.Remove(k);
}
break;
case "_physics_process_fns":
if (_physics_process_fns.ContainsKey(k))
{
_physics_process_fns.Remove(k);
}
break;
case "_input_fns":
if (_input_fns.ContainsKey(k))
{
_input_fns.Remove(k);
}
break;
case "_unhandled_input_fns":
if (_unhandled_input_fns.ContainsKey(k))
{
_unhandled_input_fns.Remove(k);
}
break;
default:
throw new System.ArgumentException("unknown hook type", "type");
}
}
public void AddEditorHook(string type, string field){
IFn fn = (IFn)Util.Invoke(RT.var("arcadia.internal.namespace", "eval-ifn"), field);
if (fn != null)
{
Add(type, "editor", fn);
}
}
// overridable methods
public override void _EnterTree()
{
Boot.Initialize();
target = this.GetParent();
AddEditorHook("_enter_tree", enter_tree_fn);
AddEditorHook("_exit_tree", exit_tree_fn);
AddEditorHook("_ready", ready_fn);
AddEditorHook("_tree_ready", tree_ready_fn);
AddEditorHook("_process", process_fn);
AddEditorHook("_physics_process", physics_process_fn);
AddEditorHook("_input", input_fn);
AddEditorHook("_unhandled_input", unhandled_input_fn);
foreach (var item in _enter_tree_fns)
{
try
{
item.Value.invoke(target, Keyword.intern(item.Key));
}
catch (Exception err)
{
GD.PrintErr(err);
}
}
}
public override void _ExitTree()
{
foreach (var item in _exit_tree_fns)
{
try
{
item.Value.invoke(target, Keyword.intern(item.Key));
}
catch (Exception err)
{
GD.PrintErr(err);
}
}
}
public override void _Ready()
{
foreach (var item in _ready_fns)
{
try
{
item.Value.invoke(target, Keyword.intern(item.Key));
}
catch (Exception err)
{
GD.PrintErr(err);
}
}
CallDeferred("_TreeReady");
}
public void _TreeReady()
{
foreach (var item in _tree_ready_fns)
{
try
{
item.Value.invoke(target, Keyword.intern(item.Key));
}
catch (Exception err)
{
GD.PrintErr(err);
}
}
}
public override void _Process(float delta)
{
foreach (var item in _process_fns)
{
try
{
item.Value.invoke(target, Keyword.intern(item.Key), delta);
}
catch (Exception err)
{
GD.PrintErr(err);
}
}
}
public override void _PhysicsProcess(float delta)
{
foreach (var item in _physics_process_fns)
{
try
{
item.Value.invoke(target, Keyword.intern(item.Key), delta);
}
catch (Exception err)
{
GD.PrintErr(err);
}
}
}
public override void _Input(InputEvent e)
{
foreach (var item in _input_fns)
{
try
{
item.Value.invoke(target, Keyword.intern(item.Key), e);
}
catch (Exception err)
{
GD.PrintErr(err);
}
}
}
public override void _UnhandledInput(InputEvent e)
{
foreach (var item in _unhandled_input_fns)
{
try
{
item.Value.invoke(target, Keyword.intern(item.Key), e);
}
catch (Exception err)
{
GD.PrintErr(err);
}
}
}
}
}