-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLgScript.cpp
567 lines (528 loc) · 13.7 KB
/
LgScript.cpp
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/******************************************************************************
* LgScript.cpp
*
* This file is part of LgScript
* Copyright (C) 2011 Tom N Harris <telliamed@whoopdedo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*****************************************************************************/
#define LUAX_INLINE
#include "luax.h"
#include "LgScript.h"
#include "LgInterpreter.h"
#include "LgMultiParm.h"
#include "LgMessage.h"
#include "ScriptModule.h"
#include "mod/ctype.h"
using namespace Lgs;
using namespace luax;
void* LgScript::operator new(size_t s, State& L)
{
return L.newUserdata(s);
}
void LgScript::operator delete(void*, State&)
{
// allow GC
}
void LgScript::operator delete(void*)
{
// allow GC
}
const char LgScript::s_ClassName[] = "LgScript";
const Registry LgScript::Methods[] = {
{"SendMessage", SendMessageMethod},
{"PostMessage", PostMessageMethod},
{"SetTimedMessage", SetTimedMessageMethod},
{"KillTimedMessage", KillTimedMessageMethod},
{"IsScriptDataSet", IsScriptDataSetMethod},
{"GetScriptData", GetScriptDataMethod},
{"SetScriptData", SetScriptDataMethod},
{"ClearScriptData", ClearScriptDataMethod},
{NULL, NULL}
};
void LgScript::Init(State& S)
{
S.newMetatable(s_ClassName).registerLib(Methods).pop();
}
void LgScript::Environment(State& S)
{
// Stack: env
S.createTable(0, 3); // env mt
S.createTable().setField("upperindex");
S.push(NewIndexEnvMethod).setField("__newindex");
S.copy(LUA_GLOBALSINDEX).setField("__index");
S.setMetatable(); // metatable(env) = mt
}
int LgScript::ReleaseMethod(Handle L)
{
State S(L);
LgScript* pScript = Check(S, 1);
delete pScript;
return 0;
}
void LgScript::DisposeRef(void)
{
char szName[256];
int iObjId;
try
{
strncpy(szName, Name(), sizeof(szName));
iObjId = ObjId();
if (m_iLastMsg >= 0)
EndScript();
m_pInterpreter->UnloadScript(Name(), ObjId());
}
catch (...)
{
ScriptModule::MPrintf("!!! Unhandled exception in destructor [%s:%d]\n",
szName, iObjId);
}
}
LgScript::~LgScript()
{
}
LgScript::LgScript(ScriptInterpreter* pInterpreter, const char* pszName, int iObjId)
: cScript(pszName, iObjId),
m_pInterpreter(pInterpreter),
m_iLastMsg(-1), m_bFailed(false)
{
// Script object will be at top of stack
State S(pInterpreter->Lua());
int iScript = S.getTop();
int iMT = S.createTableI(0, 4); // script mt
S.push(ReleaseMethod).setField("__gc");
S.createTable(0, 3) // script mt index
.copy().setMetatable(); // metatable(index) = index
S.push(pszName).setField("classname"); // index.classname = pszName
S.push(iObjId).setField("objid"); // index.objid = iObjId
S.copy(iScript) // script mt index script
.getMetatable(s_ClassName)
.push(IndexMethod, 2) // script mt index indexfunc<script,methods>
.setField("__index"); // script mt index
S.setField("__index", iMT); // script mt
S.push(true) // tag closure for quicker access
.push(SetScriptDataMethod, 1)
.setField("__newindex", iMT);
S.push(s_ClassName).setField("class", iMT);
S.setMetatable(iScript); // script
}
STDMETHODIMP LgScript::ReceiveMessage(sScrMsg* pMsg, sMultiParm* pReply, eScrTraceAction eTrace)
{
cScript::ReceiveMessage(pMsg, pReply, eTrace);
long iRet = 0;
if (!m_bFailed) try
{
if (m_iLastMsg < 0)
BeginScript();
if (strcasecmp(pMsg->message, "EndScript") != 0)
{
// Skip EndScript from the script manager
// so we can guarantee it is always the last message.
iRet = DispatchMessage(pMsg, static_cast<cMultiParm*>(pReply));
}
}
catch (std::exception& e)
{
ScriptModule::MPrintf("!!! Unexpected exception in %s [%s:%d]: %s\n",
pMsg->message, Name(), ObjId(), e.what());
ScriptModule::MPrintf("!!! Bailing on script.\n");
m_bFailed = true;
iRet = -1;
}
catch (...)
{
ScriptModule::MPrintf("!!! Unhandled exception in %s [%s:%d]\n",
pMsg->message, Name(), ObjId());
ScriptModule::MPrintf("!!! Bailing on script.\n");
m_bFailed = true;
iRet = -1;
}
m_iLastMsg = pMsg->time;
return iRet;
}
long LgScript::DispatchMessage(sScrMsg* pMsg, cMultiParm* pReply)
{
try
{
Frame S(m_pInterpreter->Lua());
S->checkStack(LUA_MINSTACK);
int iCache = m_pInterpreter->Instance(S);
if (S->push(Userdata<IScript>(this)).rawGet(iCache).isNil())
{
ScriptModule::MPrintf("!!! Can't find script instance %p [%s:%d]\n",
this, Name(), ObjId());
ScriptModule::MPrintf("!!! Bailing on script.\n");
m_bFailed = true;
return 1;
}
LookupMessage(S, pMsg->message);
//S->push(pMsg->message).getTable();
if (S->isFunction())
{
m_pInterpreter->PushTraceback(S, this);
S->insert(); // screnv traceback function
ScriptMessage(S).push(pMsg); // screnv traceback function message
// Message references cannot be kept outside of the message handler.
sScrMsg** pScrMsg = S->toUserdata(Userdata<sScrMsg*>(), -1);
try
{
S->fCall(1, 1, -3);
if (pReply)
*pReply = ScriptMultiParm(S);
}
catch (Exception&)
{
if (*pScrMsg)
*pScrMsg = NULL;
// Need to do this in case there is an open linkset.
S->gcCollect();
}
if (*pScrMsg)
*pScrMsg = NULL;
}
}
catch (Exception&)
{
m_pInterpreter->Lua().gcCollect();
throw;
}
return 0;
}
void LgScript::LookupMessage(State& S, const char* pszMessage)
{
int iScript = S.getTop();
S.getMetatable(iScript).getField("upperindex");
Buffer upper(S);
for (const char* psz = pszMessage; *psz; psz++)
upper.add(toupper(*psz));
upper.push();
if (!S.rawGet().isNil())
{
S.getTable(iScript);
}
S.insert(iScript+1).setTop(iScript+1);
}
void LgScript::BeginScript(void)
{
ScriptModule::MPrintf("=== BeginScript [%s:%d]\n",
Name(), ObjId());
}
void LgScript::EndScript(void)
{
sScrMsg* msg = new sScrMsg();
msg->message = "EndScript";
msg->time = m_iLastMsg;
try
{
DispatchMessage(msg, NULL);
}
catch (Exception&)
{ }
msg->Release();
ScriptModule::MPrintf("=== EndScript [%s:%d]\n",
Name(), ObjId());
}
cMultiParm LgScript::SendMessage(int iDest, const char* pszMessage,
const cMultiParm& mpData1,
const cMultiParm& mpData2,
const cMultiParm& mpData3)
{
cMultiParm mpReply;
g_pScriptManager->SendMessage2(mpReply, ObjId(), iDest, pszMessage, mpData1, mpData2, mpData3);
return mpReply;
}
void LgScript::PostMessage(int iDest, const char* pszMessage,
const cMultiParm& mpData1,
const cMultiParm& mpData2,
const cMultiParm& mpData3,
unsigned long flags)
{
ScriptModule::PostMessage(ObjId(), iDest, pszMessage, mpData1, mpData2, mpData3, flags);
}
tScrTimer LgScript::SetTimedMessage(const char* pszName, unsigned long iTime, eScrTimedMsgKind eType,
const cMultiParm& mpData)
{
return g_pScriptManager->SetTimedMessage2(ObjId(), pszName, iTime, eType, mpData);
}
void LgScript::KillTimedMessage(tScrTimer hTimer)
{
g_pScriptManager->KillTimedMessage(hTimer);
}
bool LgScript::IsScriptDataSet(const char* pszName)
{
sScrDatumTag tag;
tag.objId = ObjId();
tag.pszClass = Name();
tag.pszName = pszName;
return g_pScriptManager->IsScriptDataSet(&tag);
}
cMultiParm LgScript::GetScriptData(const char* pszName)
{
cMultiParm mpRet;
sScrDatumTag tag;
tag.objId = ObjId();
tag.pszClass = Name();
tag.pszName = pszName;
g_pScriptManager->GetScriptData(&tag, &mpRet);
return mpRet;
}
long LgScript::SetScriptData(const char* pszName, const cMultiParm& mpData)
{
sScrDatumTag tag;
tag.objId = ObjId();
tag.pszClass = Name();
tag.pszName = pszName;
return g_pScriptManager->SetScriptData(&tag, &mpData);
}
cMultiParm LgScript::ClearScriptData(const char* pszName)
{
cMultiParm mpRet;
sScrDatumTag tag;
tag.objId = ObjId();
tag.pszClass = Name();
tag.pszName = pszName;
g_pScriptManager->ClearScriptData(&tag, &mpRet);
return mpRet;
}
LgScript* LgScript::Check(State& S, int arg)
{
void* pv = S.toUserdata(arg);
if (pv == NULL)
return NULL;
if (!S.isNone(Upvalue(1)))
{
// closure guarantees success
return reinterpret_cast<LgScript*>(pv);
}
bool bIsScript = false;
if (!S.getMetatable(arg).isNil())
{
bIsScript = !S.getField("class").isNil() &&
(0 == strcmp(S.asString(), s_ClassName));
S.pop();
}
S.pop();
return bIsScript ? reinterpret_cast<LgScript*>(pv) : NULL;
}
int LgScript::IndexMethod(Handle L)
{
State S(L);
// upvalue(1) -> script
// upvalue(2) -> method table
// stack: metatable key
if (!S.copy(2).rawGet(Upvalue(2)).isNil()) // mt key val
return 1;
S.pop();
LgScript* pScript = S.toUserdata(Userdata<LgScript>(),Upvalue(1));
if (!S.isString(2))
{
S.push(Nil());
return 1;
}
const char* pszKey = S.asString(2);
ScriptMultiParm(S).push(pScript->GetScriptData(pszKey));
return 1;
}
int LgScript::SendMessageMethod(Handle L)
{
State S(L);
// Stack: (script|objid) dest message ...
LgScript* pScript = Check(S, 1);
S.setTop(6);
if (pScript)
{
// TODO: object userdata
int iTo = S.checkInteger(2);
const char* pszMsg = S.checkString(3,NULL);
ScriptMultiParm mp1(S), mp2(S), mp3(S);
ScriptMultiParm(S).push(
pScript->SendMessage(iTo, pszMsg,
mp1.pop(4), mp2.pop(5), mp3.pop(6)));
return 1;
}
else
{
// TODO: object userdata
int iFrom = S.checkInteger(1);
int iTo = S.checkInteger(2);
const char* pszMsg = S.checkString(3,NULL);
ScriptMultiParm mp1(S), mp2(S), mp3(S);
cMultiParm mpReply;
g_pScriptManager->SendMessage2(mpReply, iFrom, iTo, pszMsg,
mp1.pop(4), mp2.pop(5), mp3.pop(6));
ScriptMultiParm(S).push(mpReply);
return 1;
}
return 0;
}
int LgScript::PostMessageMethod(Handle L)
{
static const char* const MsgFlags[] = {
"1", "2", "sendtoproxy", "posttoowner", NULL };
State S(L);
// Stack: (script|objid) dest message ...
LgScript* pScript = Check(S, 1);
S.setTop(7);
if (pScript)
{
// TODO: object userdata
int iTo = S.checkInteger(2);
const char* pszMsg = S.checkString(3,NULL);
unsigned long flags = S.checkFlags(7, MsgFlags, "");
ScriptMultiParm mp1(S), mp2(S), mp3(S);
pScript->PostMessage(iTo, pszMsg,
mp1.pop(4), mp2.pop(5), mp3.pop(6), flags);
return 0;
}
else
{
// TODO: object userdata
int iFrom = S.checkInteger(1);
int iTo = S.checkInteger(2);
const char* pszMsg = S.checkString(3,NULL);
unsigned long flags = S.checkFlags(7, MsgFlags, "");
ScriptMultiParm mp1(S), mp2(S), mp3(S);
ScriptModule::PostMessage(iFrom, iTo, pszMsg,
mp1.pop(4), mp2.pop(5), mp3.pop(6), flags);
return 0;
}
return 0;
}
static eScrTimedMsgKind getTimedMessageType(State& S, int arg)
{
static const char* const TimerTypes[] = {
"oneshot", "periodic", NULL };
if (S.isNil(arg) || S.isBoolean(arg))
return S.toBoolean(arg) ? kSTM_Periodic : kSTM_OneShot;
eScrTimedMsgKind type;
switch (S.checkOption(arg, TimerTypes, "oneshot"))
{
default: type = kSTM_OneShot; break;
case 1: type = kSTM_Periodic; break;
}
return type;
}
int LgScript::SetTimedMessageMethod(Handle L)
{
State S(L);
// Stack: (script|objid) message time type? data?
tScrTimer hTimer;
LgScript* pScript = Check(S, 1);
S.setTop(5);
if (pScript)
{
const char* pszMsg = S.checkString(2,NULL);
int iTime = S.checkInteger(3);
ScriptMultiParm mp(S);
hTimer = pScript->SetTimedMessage(pszMsg, iTime,
getTimedMessageType(S, 4), mp.pop(5));
}
else
{
// TODO: object userdata
int iTo = S.checkInteger(1);
const char* pszMsg = S.checkString(2,NULL);
int iTime = S.checkInteger(3);
ScriptMultiParm mp(S);
hTimer = g_pScriptManager->SetTimedMessage2(iTo, pszMsg, iTime,
getTimedMessageType(S, 4), mp.pop(5));
}
S.push(reinterpret_cast<int>(hTimer));
return 1;
}
int LgScript::KillTimedMessageMethod(Handle L)
{
State S(L);
// Stack: (script timer) or (timer)
tScrTimer hTimer;
if (Check(S, 1))
hTimer = reinterpret_cast<tScrTimer>(S.checkInteger(2));
else
hTimer = reinterpret_cast<tScrTimer>(S.checkInteger(1));
if (hTimer)
g_pScriptManager->KillTimedMessage(hTimer);
return 0;
}
int LgScript::IsScriptDataSetMethod(Handle L)
{
State S(L);
// Stack: script key
LgScript* pScript = Check(S, 1);
if (!pScript)
{
return S.argError(1, "not a script");
}
const char* pszKey = S.checkString(2,NULL);
S.push(bool(pScript->IsScriptDataSet(pszKey)));
return 1;
}
int LgScript::GetScriptDataMethod(Handle L)
{
State S(L);
// Stack: script key
LgScript* pScript = Check(S, 1);
if (!pScript)
{
return S.argError(1, "not a script");
}
const char* pszKey = S.checkString(2,NULL);
ScriptMultiParm(S).push(pScript->GetScriptData(pszKey));
return 1;
}
int LgScript::SetScriptDataMethod(Handle L)
{
State S(L);
// Stack: script key value
LgScript* pScript = Check(S, 1);
if (!pScript)
{
return S.argError(1, "not a script");
}
const char* pszKey = S.checkString(2,NULL);
S.setTop(3);
pScript->SetScriptData(pszKey, ScriptMultiParm(S).pop(3));
return 0;
}
int LgScript::ClearScriptDataMethod(Handle L)
{
State S(L);
// Stack: script key
LgScript* pScript = Check(S, 1);
if (!pScript)
{
return S.argError(1, "not a script");
}
const char* pszKey = S.checkString(2,NULL);
pScript->ClearScriptData(pszKey);
return 0;
}
int LgScript::NewIndexEnvMethod(Handle L)
{
State S(L);
// Stack: screnv key value
S.rawSet(1, 2, 3);
if (S.isFunction(3) && S.isString(2))
{
size_t len;
const char* psz = S.asString(2, &len);
S.getMetatable(1).getField("upperindex"); // screnv key value mt upper
Buffer upper(S);
while (len--)
upper.add(toupper(*psz++));
upper.push();
S.copy(2).rawSet();
}
return 0;
}