forked from gocha/snes9x-rr
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathluasav.cpp
666 lines (591 loc) · 18.5 KB
/
luasav.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#include "luasav.h"
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "lstate.h"
}
#include "snes9x.h"
#include "s9xlua.h"
#include <vector>
#include <algorithm>
#ifdef WIN32
#include "win32/wsnes9x.h" // for GUI.hWnd & MessageBox
#endif
// can't remember what the best way of doing this is...
#if defined(i386) || defined(__i386) || defined(__i386__) || defined(M_I86) || defined(_M_IX86) || defined(WIN32)
#define IS_LITTLE_ENDIAN
#endif
// push a value's bytes onto the output stack
template<typename T>
void PushBinaryItem(T item, std::vector<unsigned char>& output)
{
unsigned char* buf = (unsigned char*)&item;
#ifdef IS_LITTLE_ENDIAN
for(int i = sizeof(T); i; i--)
output.push_back(*buf++);
#else
int vecsize = output.size();
for(int i = sizeof(T); i; i--)
output.insert(output.begin() + vecsize, *buf++);
#endif
}
// read a value from the byte stream and advance the stream by its size
template<typename T>
T AdvanceByteStream(const unsigned char*& data, unsigned int& remaining)
{
#ifdef IS_LITTLE_ENDIAN
T rv = *(T*)data;
data += sizeof(T);
#else
T rv; unsigned char* rvptr = (unsigned char*)&rv;
for(int i = sizeof(T)-1; i>=0; i--)
rvptr[i] = *data++;
#endif
remaining -= sizeof(T);
return rv;
}
// advance the byte stream by a certain size without reading a value
void AdvanceByteStream(const unsigned char*& data, unsigned int& remaining, int amount)
{
data += amount;
remaining -= amount;
}
#define LUAEXT_TLONG 30 // 0x1E // 4-byte signed integer
#define LUAEXT_TUSHORT 31 // 0x1F // 2-byte unsigned integer
#define LUAEXT_TSHORT 32 // 0x20 // 2-byte signed integer
#define LUAEXT_TBYTE 33 // 0x21 // 1-byte unsigned integer
#define LUAEXT_TNILS 34 // 0x22 // multiple nils represented by a 4-byte integer (warning: becomes multiple stack entities)
#define LUAEXT_TTABLE 0x40 // 0x40 through 0x4F // tables of different sizes:
#define LUAEXT_BITS_1A 0x01 // size of array part fits in a 1-byte unsigned integer
#define LUAEXT_BITS_2A 0x02 // size of array part fits in a 2-byte unsigned integer
#define LUAEXT_BITS_4A 0x03 // size of array part fits in a 4-byte unsigned integer
#define LUAEXT_BITS_1H 0x04 // size of hash part fits in a 1-byte unsigned integer
#define LUAEXT_BITS_2H 0x08 // size of hash part fits in a 2-byte unsigned integer
#define LUAEXT_BITS_4H 0x0C // size of hash part fits in a 4-byte unsigned integer
#define BITMATCH(x,y) (((x) & (y)) == (y))
static void PushNils(std::vector<unsigned char>& output, int& nilcount)
{
int count = nilcount;
nilcount = 0;
static const int minNilsWorthEncoding = 6; // because a LUAEXT_TNILS entry is 5 bytes
if(count < minNilsWorthEncoding)
{
for(int i = 0; i < count; i++)
output.push_back(LUA_TNIL);
}
else
{
output.push_back(LUAEXT_TNILS);
PushBinaryItem<uint32>(count, output);
}
}
static std::vector<const void*> s_tableAddressStack; // prevents infinite recursion of a table within a table (when cycle is found, print something like table:parent)
static std::vector<const void*> s_metacallStack; // prevents infinite recursion if something's __tostring returns another table that contains that something (when cycle is found, print the inner result without using __tostring)
static void LuaStackToBinaryConverter(lua_State* L, int i, std::vector<unsigned char>& output)
{
int type = lua_type(L, i);
// the first byte of every serialized item says what Lua type it is
output.push_back(type & 0xFF);
switch(type)
{
default:
{
char errmsg [1024];
sprintf(errmsg, "values of type \"%s\" are not allowed to be returned from registered save functions.\r\n", luaL_typename(L,i));
S9xMessage (S9X_ERROR, 0, errmsg);
}
break;
case LUA_TNIL:
// no information necessary beyond the type
break;
case LUA_TBOOLEAN:
// serialize as 0 or 1
output.push_back(lua_toboolean(L,i));
break;
case LUA_TSTRING:
// serialize as a 0-terminated string of characters
{
const char* str = lua_tostring(L,i);
while(*str)
output.push_back(*str++);
output.push_back('\0');
}
break;
case LUA_TNUMBER:
{
double num = (double)lua_tonumber(L,i);
int32 inum = (int32)lua_tointeger(L,i);
if(num != inum)
{
PushBinaryItem(num, output);
}
else
{
if((inum & ~0xFF) == 0)
type = LUAEXT_TBYTE;
else if((uint16)(inum & 0xFFFF) == inum)
type = LUAEXT_TUSHORT;
else if((int16)(inum & 0xFFFF) == inum)
type = LUAEXT_TSHORT;
else
type = LUAEXT_TLONG;
output.back() = type;
switch(type)
{
case LUAEXT_TLONG:
PushBinaryItem<int32>(static_cast<int32>(inum), output);
break;
case LUAEXT_TUSHORT:
PushBinaryItem<uint16>(static_cast<uint16>(inum), output);
break;
case LUAEXT_TSHORT:
PushBinaryItem<int16>(static_cast<int16>(inum), output);
break;
case LUAEXT_TBYTE:
output.push_back(static_cast<uint8>(inum));
break;
}
}
}
break;
case LUA_TTABLE:
// serialize as a type that describes how many bytes are used for storing the counts,
// followed by the number of array entries if any, then the number of hash entries if any,
// then a Lua value per array entry, then a (key,value) pair of Lua values per hashed entry
// note that the structure of table references are not faithfully serialized (yet)
{
int outputTypeIndex = output.size() - 1;
int arraySize = 0;
int hashSize = 0;
if(lua_checkstack(L, 4) && std::find(s_tableAddressStack.begin(), s_tableAddressStack.end(), lua_topointer(L,i)) == s_tableAddressStack.end())
{
s_tableAddressStack.push_back(lua_topointer(L,i));
struct Scope { ~Scope(){ s_tableAddressStack.pop_back(); } } scope;
bool wasnil = false;
int nilcount = 0;
arraySize = lua_objlen(L, i);
int arrayValIndex = lua_gettop(L) + 1;
for(int j = 1; j <= arraySize; j++)
{
lua_rawgeti(L, i, j);
bool isnil = lua_isnil(L, arrayValIndex);
if(isnil)
nilcount++;
else
{
if(wasnil)
PushNils(output, nilcount);
LuaStackToBinaryConverter(L, arrayValIndex, output);
}
lua_pop(L, 1);
wasnil = isnil;
}
if(wasnil)
PushNils(output, nilcount);
if(arraySize)
lua_pushinteger(L, arraySize); // before first key
else
lua_pushnil(L); // before first key
int keyIndex = lua_gettop(L);
int valueIndex = keyIndex + 1;
while(lua_next(L, i))
{
// assert(lua_type(L, keyIndex) && "nil key in Lua table, impossible");
// assert(lua_type(L, valueIndex) && "nil value in Lua table, impossible");
LuaStackToBinaryConverter(L, keyIndex, output);
LuaStackToBinaryConverter(L, valueIndex, output);
lua_pop(L, 1);
hashSize++;
}
}
int outputType = LUAEXT_TTABLE;
if(arraySize & 0xFFFF0000)
outputType |= LUAEXT_BITS_4A;
else if(arraySize & 0xFF00)
outputType |= LUAEXT_BITS_2A;
else if(arraySize & 0xFF)
outputType |= LUAEXT_BITS_1A;
if(hashSize & 0xFFFF0000)
outputType |= LUAEXT_BITS_4H;
else if(hashSize & 0xFF00)
outputType |= LUAEXT_BITS_2H;
else if(hashSize & 0xFF)
outputType |= LUAEXT_BITS_1H;
output[outputTypeIndex] = outputType;
int insertIndex = outputTypeIndex;
if(BITMATCH(outputType,LUAEXT_BITS_4A) || BITMATCH(outputType,LUAEXT_BITS_2A) || BITMATCH(outputType,LUAEXT_BITS_1A))
output.insert(output.begin() + (++insertIndex), arraySize & 0xFF);
if(BITMATCH(outputType,LUAEXT_BITS_4A) || BITMATCH(outputType,LUAEXT_BITS_2A))
output.insert(output.begin() + (++insertIndex), (arraySize & 0xFF00) >> 8);
if(BITMATCH(outputType,LUAEXT_BITS_4A))
output.insert(output.begin() + (++insertIndex), (arraySize & 0x00FF0000) >> 16),
output.insert(output.begin() + (++insertIndex), (arraySize & 0xFF000000) >> 24);
if(BITMATCH(outputType,LUAEXT_BITS_4H) || BITMATCH(outputType,LUAEXT_BITS_2H) || BITMATCH(outputType,LUAEXT_BITS_1H))
output.insert(output.begin() + (++insertIndex), hashSize & 0xFF);
if(BITMATCH(outputType,LUAEXT_BITS_4H) || BITMATCH(outputType,LUAEXT_BITS_2H))
output.insert(output.begin() + (++insertIndex), (hashSize & 0xFF00) >> 8);
if(BITMATCH(outputType,LUAEXT_BITS_4H))
output.insert(output.begin() + (++insertIndex), (hashSize & 0x00FF0000) >> 16),
output.insert(output.begin() + (++insertIndex), (hashSize & 0xFF000000) >> 24);
} break;
}
}
// complements LuaStackToBinaryConverter
void BinaryToLuaStackConverter(lua_State* L, const unsigned char*& data, unsigned int& remaining)
{
// assert(s_dbg_dataSize - (data - s_dbg_dataStart) == remaining);
unsigned char type = AdvanceByteStream<unsigned char>(data, remaining);
switch(type)
{
default:
{
char errmsg [1024];
if(type <= 10 && type != LUA_TTABLE)
sprintf(errmsg, "values of type \"%s\" are not allowed to be loaded into registered load functions. The save state's Lua save data file might be corrupted.\r\n", lua_typename(L,type));
else
sprintf(errmsg, "The save state's Lua save data file seems to be corrupted.\r\n");
S9xMessage (S9X_ERROR, 0, errmsg);
}
break;
case LUA_TNIL:
lua_pushnil(L);
break;
case LUA_TBOOLEAN:
lua_pushboolean(L, AdvanceByteStream<uint8>(data, remaining));
break;
case LUA_TSTRING:
lua_pushstring(L, (const char*)data);
AdvanceByteStream(data, remaining, strlen((const char*)data) + 1);
break;
case LUA_TNUMBER:
lua_pushnumber(L, AdvanceByteStream<double>(data, remaining));
break;
case LUAEXT_TLONG:
lua_pushinteger(L, AdvanceByteStream<int32>(data, remaining));
break;
case LUAEXT_TUSHORT:
lua_pushinteger(L, AdvanceByteStream<uint16>(data, remaining));
break;
case LUAEXT_TSHORT:
lua_pushinteger(L, AdvanceByteStream<int16>(data, remaining));
break;
case LUAEXT_TBYTE:
lua_pushinteger(L, AdvanceByteStream<uint8>(data, remaining));
break;
case LUAEXT_TTABLE:
case LUAEXT_TTABLE | LUAEXT_BITS_1A:
case LUAEXT_TTABLE | LUAEXT_BITS_2A:
case LUAEXT_TTABLE | LUAEXT_BITS_4A:
case LUAEXT_TTABLE | LUAEXT_BITS_1H:
case LUAEXT_TTABLE | LUAEXT_BITS_2H:
case LUAEXT_TTABLE | LUAEXT_BITS_4H:
case LUAEXT_TTABLE | LUAEXT_BITS_1A | LUAEXT_BITS_1H:
case LUAEXT_TTABLE | LUAEXT_BITS_2A | LUAEXT_BITS_1H:
case LUAEXT_TTABLE | LUAEXT_BITS_4A | LUAEXT_BITS_1H:
case LUAEXT_TTABLE | LUAEXT_BITS_1A | LUAEXT_BITS_2H:
case LUAEXT_TTABLE | LUAEXT_BITS_2A | LUAEXT_BITS_2H:
case LUAEXT_TTABLE | LUAEXT_BITS_4A | LUAEXT_BITS_2H:
case LUAEXT_TTABLE | LUAEXT_BITS_1A | LUAEXT_BITS_4H:
case LUAEXT_TTABLE | LUAEXT_BITS_2A | LUAEXT_BITS_4H:
case LUAEXT_TTABLE | LUAEXT_BITS_4A | LUAEXT_BITS_4H:
{
unsigned int arraySize = 0;
if(BITMATCH(type,LUAEXT_BITS_4A) || BITMATCH(type,LUAEXT_BITS_2A) || BITMATCH(type,LUAEXT_BITS_1A))
arraySize |= AdvanceByteStream<uint8>(data, remaining);
if(BITMATCH(type,LUAEXT_BITS_4A) || BITMATCH(type,LUAEXT_BITS_2A))
arraySize |= ((uint16)AdvanceByteStream<uint8>(data, remaining)) << 8;
if(BITMATCH(type,LUAEXT_BITS_4A))
arraySize |= ((uint32)AdvanceByteStream<uint8>(data, remaining)) << 16,
arraySize |= ((uint32)AdvanceByteStream<uint8>(data, remaining)) << 24;
unsigned int hashSize = 0;
if(BITMATCH(type,LUAEXT_BITS_4H) || BITMATCH(type,LUAEXT_BITS_2H) || BITMATCH(type,LUAEXT_BITS_1H))
hashSize |= AdvanceByteStream<uint8>(data, remaining);
if(BITMATCH(type,LUAEXT_BITS_4H) || BITMATCH(type,LUAEXT_BITS_2H))
hashSize |= ((uint16)AdvanceByteStream<uint8>(data, remaining)) << 8;
if(BITMATCH(type,LUAEXT_BITS_4H))
hashSize |= ((uint32)AdvanceByteStream<uint8>(data, remaining)) << 16,
hashSize |= ((uint32)AdvanceByteStream<uint8>(data, remaining)) << 24;
lua_checkstack(L, 8);
lua_createtable(L, arraySize, hashSize);
unsigned int n = 1;
while(n <= arraySize)
{
if(*data == LUAEXT_TNILS)
{
AdvanceByteStream(data, remaining, 1);
n += AdvanceByteStream<uint32>(data, remaining);
}
else
{
BinaryToLuaStackConverter(L, data, remaining); // push value
lua_rawseti(L, -2, n); // table[n] = value
n++;
}
}
for(unsigned int h = 1; h <= hashSize; h++)
{
BinaryToLuaStackConverter(L, data, remaining); // push key
BinaryToLuaStackConverter(L, data, remaining); // push value
lua_rawset(L, -3); // table[key] = value
}
}
break;
}
}
static const unsigned char luaBinaryMajorVersion = 9;
static const unsigned char luaBinaryMinorVersion = 1;
unsigned char* LuaStackToBinary(lua_State* L, unsigned int& size)
{
int n = lua_gettop(L);
if(n == 0)
return NULL;
std::vector<unsigned char> output;
output.push_back(luaBinaryMajorVersion);
output.push_back(luaBinaryMinorVersion);
for(int i = 1; i <= n; i++)
LuaStackToBinaryConverter(L, i, output);
unsigned char* rv = new unsigned char [output.size()];
memcpy(rv, &output.front(), output.size());
size = output.size();
return rv;
}
void BinaryToLuaStack(lua_State* L, const unsigned char* data, unsigned int size, unsigned int itemsToLoad)
{
unsigned char major = *data++;
unsigned char minor = *data++;
size -= 2;
if(luaBinaryMajorVersion != major || luaBinaryMinorVersion != minor)
return;
while(size > 0 && itemsToLoad > 0)
{
BinaryToLuaStackConverter(L, data, size);
itemsToLoad--;
}
}
// saves Lua stack into a record and pops it
void LuaSaveData::SaveRecord(lua_State* L, unsigned int key)
{
if(!L)
return;
Record* cur = new Record();
cur->key = key;
cur->data = LuaStackToBinary(L, cur->size);
cur->next = NULL;
lua_settop(L,0);
if(cur->size <= 0)
{
delete cur;
return;
}
Record* last = recordList;
while(last && last->next)
last = last->next;
if(last)
last->next = cur;
else
recordList = cur;
}
// pushes a record's data onto the Lua stack
void LuaSaveData::LoadRecord(struct lua_State* L, unsigned int key, unsigned int itemsToLoad) const
{
if(!L)
return;
Record* cur = recordList;
while(cur)
{
if(cur->key == key)
{
// s_dbg_dataStart = cur->data;
// s_dbg_dataSize = cur->size;
BinaryToLuaStack(L, cur->data, cur->size, itemsToLoad);
return;
}
cur = cur->next;
}
}
// saves part of the Lua stack (at the given index) into a record and does NOT pop anything
void LuaSaveData::SaveRecordPartial(struct lua_State* L, unsigned int key, int idx)
{
if(!L)
return;
if(idx < 0)
idx += lua_gettop(L)+1;
Record* cur = new Record();
cur->key = key;
cur->next = NULL;
if(idx <= lua_gettop(L))
{
std::vector<unsigned char> output;
output.push_back(luaBinaryMajorVersion);
output.push_back(luaBinaryMinorVersion);
LuaStackToBinaryConverter(L, idx, output);
unsigned char* rv = new unsigned char [output.size()];
memcpy(rv, &output.front(), output.size());
cur->size = output.size();
cur->data = rv;
}
if(cur->size <= 0)
{
delete cur;
return;
}
Record* last = recordList;
while(last && last->next)
last = last->next;
if(last)
last->next = cur;
else
recordList = cur;
}
void fwriteint(unsigned int value, FILE* file)
{
for(int i=0;i<4;i++)
{
int w = value & 0xFF;
fwrite(&w, 1, 1, file);
value >>= 8;
}
}
void freadint(unsigned int& value, FILE* file)
{
int rv = 0;
for(int i=0;i<4;i++)
{
int r = 0;
fread(&r, 1, 1, file);
rv |= r << (i*8);
}
value = rv;
}
// writes all records to an already-open file
void LuaSaveData::ExportRecords(void* fileV) const
{
FILE* file = (FILE*)fileV;
if(!file)
return;
Record* cur = recordList;
while(cur)
{
fwriteint(cur->key, file);
fwriteint(cur->size, file);
fwrite(cur->data, cur->size, 1, file);
cur = cur->next;
}
}
// reads records from an already-open file
void LuaSaveData::ImportRecords(void* fileV)
{
FILE* file = (FILE*)fileV;
if(!file)
return;
ClearRecords();
Record rec;
Record* cur = &rec;
Record* last = NULL;
while(1)
{
freadint(cur->key, file);
freadint(cur->size, file);
if(feof(file) || ferror(file))
break;
cur->data = new unsigned char [cur->size];
fread(cur->data, cur->size, 1, file);
Record* next = new Record();
memcpy(next, cur, sizeof(Record));
next->next = NULL;
if(last)
last->next = next;
else
recordList = next;
last = next;
}
}
void LuaSaveData::ClearRecords()
{
Record* cur = recordList;
while(cur)
{
Record* del = cur;
cur = cur->next;
delete[] del->data;
delete del;
}
recordList = NULL;
}
void CallRegisteredLuaSaveFunctions(int savestateNumber, LuaSaveData& saveData)
{
lua_State* L = S9xGetLuaState();
if(L)
{
lua_settop(L, 0);
lua_getfield(L, LUA_REGISTRYINDEX, LUA_SAVE_CALLBACK_STRING);
if (lua_isfunction(L, -1))
{
lua_pushinteger(L, savestateNumber);
int ret = lua_pcall(L, 1, LUA_MULTRET, 0);
if (ret != 0) {
// This is grounds for trashing the function
lua_pushnil(L);
lua_setfield(L, LUA_REGISTRYINDEX, LUA_SAVE_CALLBACK_STRING);
#ifdef WIN32
MessageBox(GUI.hWnd, lua_tostring(L, -1), "Lua Error in SAVE function", MB_OK);
#else
fprintf(stderr, "Lua error in registersave function: %s\n", lua_tostring(L, -1));
#endif
}
saveData.SaveRecord(L, LUA_DATARECORDKEY);
}
else
{
lua_pop(L, 1);
}
}
}
void CallRegisteredLuaLoadFunctions(int savestateNumber, const LuaSaveData& saveData)
{
lua_State* L = S9xGetLuaState();
if(L)
{
lua_settop(L, 0);
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOAD_CALLBACK_STRING);
if (lua_isfunction(L, -1))
{
// since the scriptdata can be very expensive to load
// (e.g. the registered save function returned some huge tables)
// check the number of parameters the registered load function expects
// and don't bother loading the parameters it wouldn't receive anyway
int numParamsExpected = (L->top - 1)->value.gc->cl.l.p->numparams; // NOTE: if this line crashes, that means your Lua headers are out of sync with your Lua lib
if(numParamsExpected) numParamsExpected--; // minus one for the savestate number we always pass in
int prevGarbage = lua_gc(L, LUA_GCCOUNT, 0);
lua_pushinteger(L, savestateNumber);
saveData.LoadRecord(L, LUA_DATARECORDKEY, numParamsExpected);
int n = lua_gettop(L) - 1;
int ret = lua_pcall(L, n, 0, 0);
if (ret != 0) {
// This is grounds for trashing the function
lua_pushnil(L);
lua_setfield(L, LUA_REGISTRYINDEX, LUA_LOAD_CALLBACK_STRING);
#ifdef WIN32
MessageBox(GUI.hWnd, lua_tostring(L, -1), "Lua Error in LOAD function", MB_OK);
#else
fprintf(stderr, "Lua error in registerload function: %s\n", lua_tostring(L, -1));
#endif
}
else
{
int newGarbage = lua_gc(L, LUA_GCCOUNT, 0);
if(newGarbage - prevGarbage > 50)
{
// now seems to be a very good time to run the garbage collector
// it might take a while now but that's better than taking 10 whiles 9 loads from now
lua_gc(L, LUA_GCCOLLECT, 0);
}
}
}
else
{
lua_pop(L, 1);
}
}
}