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
|
/*-- Extensible callback system --*/
#strict 2
static const CallbackTarget_Bind = -1;
static const CallbackTarget_Global = -2;
static const CallbackTarget_Scenario = -3;
// NOTE: fast callbacks (fast = true for ObjectCallback or DefinitionCallback) use ObjectCall/DefinitionCall directly for greater performance if possible (refs == 0 and GetLength(args) <= 8 for CallA()) and thus must be real functions of the desired object/definition. safe mode is ignored for fast callbacks
global func GlobalCallback(string name) { return [CallbackTarget_Global, name]; }
global func ScenarioCallback(string name) { return [CallbackTarget_Scenario, name]; }
global func ObjectCallback(string name, bool fast, object target) { return [target || this || FatalError("ObjectCallback without target object"), name, fast]; }
global func DefinitionCallback(string name, bool fast, id target) { return [target || GetID() || FatalError("DefinitionCallback without target definition"), name, fast]; }
global func Callback(string name, target) { return [target || this || GetID() || CallbackTarget_Global, name]; }
global func BindCallback(callback, array binding) { return [CallbackTarget_Bind, callback, binding]; }
static const BindCallback_Bind_Value = -1;
static const BindCallback_Bind_Context = -2;
static const BindCallback_Bind_ContextDef = -3;
static const BindCallback_Bind_CallbackResult = -4;
static const BindCallback_Bind_ArgumentArrayPart = -5;
static const BindCallback_Bind_Reference = -6;
global func Bind(value) { return [BindCallback_Bind_Value, value]; }
global func Bind_Context() { return [BindCallback_Bind_Context]; }
global func Bind_ContextDef() { return [BindCallback_Bind_ContextDef]; }
global func Bind_CallbackResult(callback) { return [BindCallback_Bind_CallbackResult, callback]; }
global func Bind_ArgumentArrayPart(int argument, part)
{
if(GetType(part) != C4V_Array)
{
part = [part];
}
return [BindCallback_Bind_ArgumentArrayPart, argument, part];
}
global func Bind_Reference(array scopedVar) { return [BindCallback_Bind_Reference, scopedVar]; }
global func Call(callback)
{
if(GetType(callback) == C4V_String)
{
return _inherited(callback, ...);
}
else
{
return CallA(callback, CreateFilledArray(...));
}
}
global func CallA(callback, args, bool safe, array refs)
{
if(GetType(callback) == C4V_String)
{
callback = [this, callback, true];
}
if(GetType(callback) != C4V_Array)
{
return _inherited(callback, args, ...);
}
var target = callback[0], function = callback[1];
if((GetType(target) == C4V_C4Object || GetType(target) == C4V_C4ID) && GetLength(callback) == 3 && callback[2] && (!refs || refs == []) && GetLength(args) <= 8) // fast callback
{
if(GetType(target) == C4V_C4Object)
{
return ObjectCall(target, function, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
}
else if(GetType(target) == C4V_C4ID)
{
return DefinitionCall(target, function, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
}
}
refs = refs || [];
if(target == CallbackTarget_Scenario)
{
return GameCall(function, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
}
else if(target == CallbackTarget_Bind)
{
return CallA(function, BindArgs(callback[2], args, safe, refs), safe, refs);
}
if(GetType(target) == C4V_C4Object || GetType(target) == C4V_C4ID || target == CallbackTarget_Global)
{
var safeModifier = "";
if(safe)
{
safeModifier = "~";
}
var argsStr = "", pos = 0;
for(var arg in args)
{
if(pos > 0)
{
argsStr = Format("%s, ", argsStr);
}
if(refs[pos])
{
argsStr = Format("%sScopedVar(%s)", argsStr, Serialize(arg));
}
else
{
argsStr = Format("%s%s", argsStr, Serialize(arg));
}
++pos;
}
if(target == CallbackTarget_Global)
{
return GlobalEval(Format("%s(%s)", function, argsStr));
}
else if(GetType(target) == C4V_C4Object || GetType(target) == C4V_C4ID)
{
return eval(Format("%s->%s%s(%s)", Serialize(target), safeModifier, function, argsStr));
}
}
else
{
if(this)
{
return this->~CallCustom(callback, args, ...);
}
else if(GetID())
{
return GetID()->~CallCustom(callback, args, ...);
}
else
{
return CallCustom(callback, args, ...);
}
}
}
global func CallCustom() { return _inherited(...); }
global func BindArgs_ArgumentArrayPart(array binding, array args, bool safe)
{
var ret = args[binding[0]];
for(var i = 1; i < GetLength(binding); ++i)
{
if(!safe || GetType(ret) == C4V_Array)
{
ret = ret[binding[i]];
}
else
{
return 0;
}
}
return ret;
}
global func BindCustomArgs() { return _inherited(...); } // this allows "overloading" this function even if the "overloading" function is loaded before
global func BindArgs(array binding, array args, bool safe, array& refs)
{
refs = [];
var ret = CreateArray(GetLength(binding)), pos = 0;
for(var bind in binding)
{
if(GetType(bind) == C4V_Int || GetType(bind) == C4V_Any && bind >= 0)
{
ret[pos] = args[bind];
}
else if(GetType(bind) == C4V_Array && GetLength(bind))
{
if(bind[0] == BindCallback_Bind_Value)
{
ret[pos] = bind[1];
}
else if(bind[0] == BindCallback_Bind_Context)
{
ret[pos] = this;
}
else if(bind[0] == BindCallback_Bind_ContextDef)
{
ret[pos] = GetID();
}
else if(bind[0] == BindCallback_Bind_CallbackResult)
{
ret[pos] = CallA(bind[1], args, safe);
}
else if(bind[0] == BindCallback_Bind_ArgumentArrayPart)
{
ret[pos] = BindArgs_ArgumentArrayPart(bind[2], args[bind[1]], safe);
}
else if(bind[0] == BindCallback_Bind_Reference)
{
refs[pos] = true;
ret[pos] = bind[1];
}
else if(GetType(bind[0]) == C4V_Int && bind[0] < 0)
{
if(this)
{
ret[pos] = this->~BindCustomArgs(bind, args, safe);
}
else if(GetID())
{
ret[pos] = GetID()->~BindCustomArgs(bind, args, safe);
}
else
{
ret[pos] = BindCustomArgs(bind, args, safe);
}
}
else
{
ret[pos] = BindArgs(bind, args, safe);
}
}
else
{
FatalError(Format("BindArgs: incorrect binding %v", bind));
}
++pos;
}
return ret;
}
global func GlobalEval(string code)
{
var effect = AddEffect("IntGlobalEval", 0, 1);
var ret = EffectCall(0, effect, "Eval", code);
RemoveEffect(0, 0, effect);
return ret;
}
global func FxIntGlobalEvalEval(object target, int effectNumber, string code)
{
return eval(code);
}
global func foreach(array arr, callback)
{
var i = 0;
for(var val in arr)
{
CallA(callback, [val, i++]);
}
}
global func CheckCallback(callback)
{
if(GetType(callback) == C4V_String)
{
return true;
}
if(GetType(callback) == C4V_Array)
{
if(GetType(callback[0]) == C4V_Int)
{
if(callback[0] == CallbackTarget_Bind && GetLength(callback) == 3)
{
if(CheckCallback(callback[1]) && CheckBindCallbackBinding(callback[2]))
{
return true;
}
}
else if(callback[0] == CallbackTarget_Global || callback[0] == CallbackTarget_Scenario)
{
if(GetLength(callback) == 2 && GetType(callback[1]) == C4V_String && callback[1] != "")
{
return true;
}
}
}
else if(GetType(callback[0]) == C4V_C4ID || GetType(callback[0]) == C4V_C4Object)
{
if(GetLength(callback) == 2 || (GetLength(callback) == 3 && (!callback[2] || GetType(callback[2]) == C4V_Bool))) // length 2 for callbacks created by Callback() or Object/DefinitionCallback() before fast was introduced
{
if(GetType(callback[1]) == C4V_String && callback[1] != "")
{
return true;
}
}
}
}
if(this)
{
return this->~CheckCustomCallback(callback, ...);
}
else if(GetID())
{
return GetID()->~CheckCustomCallback(callback, ...);
}
else
{
return CheckCustomCallback(callback, ...);
}
}
// to silence the error if no ScopedVars are included
global func CheckScopedVar() { return _inherited(...); }
global func CheckCustomCallback() { return _inherited(...); }
global func CheckBindCallbackBinding(array binding)
{
for(var b in binding)
{
if(b && GetType(b) != C4V_Int)
{
if(GetType(b) == C4V_Array)
{
if(b[0] == BindCallback_Bind_Value)
{
if(GetLength(b) != 2)
{
return false;
}
}
else if(b[0] == BindCallback_Bind_Context || b[0] == BindCallback_Bind_ContextDef)
{
continue;
}
else if(b[0] == BindCallback_Bind_CallbackResult)
{
if(GetLength(b) != 2 || !CheckCallback(b[1]))
{
return false;
}
}
else if(b[0] == BindCallback_Bind_ArgumentArrayPart)
{
if(GetLength(b) != 3)
{
return false;
}
if(b[1] && (GetType(b[1]) != C4V_Int || b[1] < 0))
{
return false;
}
for(var part in b[2])
{
if(part && (GetType(part) != C4V_Int || part < 0))
{
return false;
}
}
}
else if(b[0] == BindCallback_Bind_Reference)
{
if(GetLength(b) != 2 || !CheckScopedVar(b[1]))
{
return false;
}
}
else if(GetType(b[0]) == C4V_Int && b[0] < 0)
{
var ret;
if(this)
{
ret = this->~CheckBindCallbackCustomBinding(b);
}
else if(GetID())
{
ret = GetID()->~CheckBindCallbackCustomBinding(b);
}
else
{
ret = CheckBindCallbackCustomBinding(b);
}
if(!ret)
{
return false;
}
}
else
{
if(!CheckBindCallbackBinding(b))
{
return false;
}
}
}
else
{
return false;
}
}
else if(b < 0)
{
return false;
}
}
return true;
}
global func CheckBindCallbackCustomBinding() { return _inherited(...); }
|