Skip to content

Latest commit

 

History

History
1903 lines (1573 loc) · 57.6 KB

lua_glue.c

File metadata and controls

1903 lines (1573 loc) · 57.6 KB
 
May 12, 2007
May 12, 2007
1
2
3
4
5
6
7
8
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
Dec 2, 2006
Dec 2, 2006
9
#include "universal.h"
Dec 9, 2006
Dec 9, 2006
10
#include "lua_glue.h"
Dec 3, 2006
Dec 3, 2006
11
#include "platform.h"
Dec 2, 2006
Dec 2, 2006
12
13
14
15
#include "fileio.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
Dec 6, 2006
Dec 6, 2006
16
#include "gui.h"
Dec 2, 2006
Dec 2, 2006
17
18
19
20
21
#define MOJOSETUP_NAMESPACE "MojoSetup"
static lua_State *luaState = NULL;
Dec 9, 2006
Dec 9, 2006
22
23
24
25
26
27
28
29
30
31
32
33
// Allocator interface for internal Lua use.
static void *MojoLua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
if (nsize == 0)
{
free(ptr);
return NULL;
} // if
return xrealloc(ptr, nsize);
} // MojoLua_alloc
Dec 2, 2006
Dec 2, 2006
34
35
36
37
// Read data from a MojoInput when loading Lua code.
static const char *MojoLua_reader(lua_State *L, void *data, size_t *size)
{
MojoInput *in = (MojoInput *) data;
Dec 9, 2006
Dec 9, 2006
38
char *retval = (char *) scratchbuf_128k;
Dec 2, 2006
Dec 2, 2006
39
40
41
42
43
44
45
46
47
48
49
50
int64 br = in->read(in, scratchbuf_128k, sizeof (scratchbuf_128k));
if (br <= 0) // eof or error? (lua doesn't care which?!)
{
br = 0;
retval = NULL;
} // if
*size = (size_t) br;
return retval;
} // MojoLua_reader
May 2, 2007
May 2, 2007
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
// Sets t[sym]=f, where t is on the top of the Lua stack.
// !!! FIXME: why is this a different naming convention?
static inline void set_cfunc(lua_State *L, lua_CFunction f, const char *sym)
{
lua_pushcfunction(L, f);
lua_setfield(L, -2, sym);
} // set_cfunc
// Sets t[sym]=f, where t is on the top of the Lua stack.
// !!! FIXME: why is this a different naming convention?
static inline void set_cptr(lua_State *L, void *ptr, const char *sym)
{
lua_pushlightuserdata(L, ptr);
lua_setfield(L, -2, sym);
} // set_cfunc
// Sets t[sym]=f, where t is on the top of the Lua stack.
// !!! FIXME: why is this a different naming convention?
static inline void set_string(lua_State *L, const char *str, const char *sym)
{
if (str == NULL)
lua_pushnil(L);
else
lua_pushstring(L, str);
lua_setfield(L, -2, sym);
} // set_string
// Sets t[sym]=f, where t is on the top of the Lua stack.
// !!! FIXME: why is this a different naming convention?
static inline void set_number(lua_State *L, lua_Number x, const char *sym)
{
lua_pushnumber(L, x);
lua_setfield(L, -2, sym);
Jan 24, 2008
Jan 24, 2008
87
} // set_number
May 2, 2007
May 2, 2007
88
Jan 20, 2008
Jan 20, 2008
89
90
91
92
93
94
95
// Sets t[sym]=f, where t is on the top of the Lua stack.
// !!! FIXME: why is this a different naming convention?
static inline void set_integer(lua_State *L, lua_Integer x, const char *sym)
{
lua_pushinteger(L, x);
lua_setfield(L, -2, sym);
Jan 24, 2008
Jan 24, 2008
96
97
98
99
100
101
102
103
104
105
106
} // set_integer
// Sets t[sym]=f, where t is on the top of the Lua stack.
// !!! FIXME: why is this a different naming convention?
static inline void set_boolean(lua_State *L, boolean x, const char *sym)
{
lua_pushboolean(L, x);
lua_setfield(L, -2, sym);
} // set_boolean
Jan 20, 2008
Jan 20, 2008
107
May 2, 2007
May 2, 2007
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
// !!! FIXME: why is this a different naming convention?
static inline void set_string_array(lua_State *L, int argc, const char **argv,
const char *sym)
{
int i;
lua_newtable(L);
for (i = 0; i < argc; i++)
{
lua_pushinteger(L, i+1); // lua is option base 1!
lua_pushstring(L, argv[i]);
lua_settable(L, -3);
} // for
lua_setfield(L, -2, sym);
} // set_string_array
void MojoLua_setString(const char *str, const char *sym)
{
lua_getglobal(luaState, MOJOSETUP_NAMESPACE);
set_string(luaState, str, sym);
lua_pop(luaState, 1);
} // MojoLua_setString
void MojoLua_setStringArray(int argc, const char **argv, const char *sym)
{
lua_getglobal(luaState, MOJOSETUP_NAMESPACE);
set_string_array(luaState, argc, argv, sym);
lua_pop(luaState, 1);
} // MojoLua_setStringArray
Apr 22, 2007
Apr 22, 2007
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
static inline int retvalString(lua_State *L, const char *str)
{
if (str != NULL)
lua_pushstring(L, str);
else
lua_pushnil(L);
return 1;
} // retvalString
static inline int retvalBoolean(lua_State *L, boolean b)
{
lua_pushboolean(L, b);
return 1;
} // retvalBoolean
static inline int retvalNumber(lua_State *L, lua_Number n)
{
lua_pushnumber(L, n);
return 1;
} // retvalNumber
May 7, 2007
May 7, 2007
164
165
166
167
168
169
170
171
172
173
static inline int retvalLightUserData(lua_State *L, void *data)
{
if (data != NULL)
lua_pushlightuserdata(L, data);
else
lua_pushnil(L);
return 1;
} // retvalLightUserData
May 27, 2007
May 27, 2007
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
static int retvalChecksums(lua_State *L, const MojoChecksums *sums)
{
lua_newtable(L);
#if SUPPORT_CRC32
{
char buf[64];
snprintf(buf, sizeof (buf), "%X", (unsigned int) sums->crc32);
set_string(L, buf, "crc32");
}
#endif
#if SUPPORT_MD5
{
char buf[64];
const uint8 *dig = sums->md5;
snprintf(buf, sizeof (buf), "%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
(int) dig[0], (int) dig[1], (int) dig[2], (int) dig[3],
(int) dig[4], (int) dig[5], (int) dig[6], (int) dig[7],
(int) dig[8], (int) dig[9], (int) dig[10], (int) dig[11],
(int) dig[12], (int) dig[13], (int) dig[14], (int) dig[15]);
set_string(L, buf, "md5");
}
#endif
#if SUPPORT_SHA1
{
char buf[64];
const uint8 *dig = sums->sha1;
snprintf(buf, sizeof (buf), "%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
(int) dig[0], (int) dig[1], (int) dig[2], (int) dig[3],
(int) dig[4], (int) dig[5], (int) dig[6], (int) dig[7],
(int) dig[8], (int) dig[9], (int) dig[10], (int) dig[11],
(int) dig[12], (int) dig[13], (int) dig[14], (int) dig[15],
(int) dig[16], (int) dig[17], (int) dig[18], (int) dig[19]);
set_string(L, buf, "sha1");
}
#endif
return 1;
} // retvalChecksums
Dec 10, 2006
Dec 10, 2006
217
218
219
220
221
222
223
224
225
226
227
228
229
static inline int snprintfcat(char **ptr, size_t *len, const char *fmt, ...)
{
int bw = 0;
va_list ap;
va_start(ap, fmt);
bw = vsnprintf(*ptr, *len, fmt, ap);
va_end(ap);
*ptr += bw;
*len -= bw;
return bw;
} // snprintfcat
Dec 10, 2006
Dec 10, 2006
230
231
232
233
234
235
236
static int luahook_stackwalk(lua_State *L)
{
const char *errstr = lua_tostring(L, 1);
lua_Debug ldbg;
int i = 0;
if (errstr != NULL)
Jan 12, 2008
Jan 12, 2008
237
logDebug("%0", errstr);
Dec 10, 2006
Dec 10, 2006
238
Dec 10, 2006
Dec 10, 2006
239
logDebug("Lua stack backtrace:");
Dec 10, 2006
Dec 10, 2006
240
241
242
243
// start at 1 to skip this function.
for (i = 1; lua_getstack(L, i, &ldbg); i++)
{
Dec 10, 2006
Dec 10, 2006
244
245
246
char *ptr = (char *) scratchbuf_128k;
size_t len = sizeof (scratchbuf_128k);
int bw = snprintfcat(&ptr, &len, "#%d", i-1);
Dec 10, 2006
Dec 10, 2006
247
248
249
const int maxspacing = 4;
int spacing = maxspacing - bw;
while (spacing-- > 0)
Dec 10, 2006
Dec 10, 2006
250
snprintfcat(&ptr, &len, " ");
Dec 10, 2006
Dec 10, 2006
251
252
253
if (!lua_getinfo(L, "nSl", &ldbg))
{
Dec 10, 2006
Dec 10, 2006
254
snprintfcat(&ptr, &len, "???\n");
Jan 12, 2008
Jan 12, 2008
255
logDebug("%0", (const char *) scratchbuf_128k);
Dec 10, 2006
Dec 10, 2006
256
257
258
259
continue;
} // if
if (ldbg.namewhat[0])
Dec 10, 2006
Dec 10, 2006
260
snprintfcat(&ptr, &len, "%s ", ldbg.namewhat);
Dec 10, 2006
Dec 10, 2006
261
262
if ((ldbg.name) && (ldbg.name[0]))
Dec 10, 2006
Dec 10, 2006
263
snprintfcat(&ptr, &len, "function %s ()", ldbg.name);
Dec 10, 2006
Dec 10, 2006
264
265
266
else
{
if (strcmp(ldbg.what, "main") == 0)
Dec 10, 2006
Dec 10, 2006
267
snprintfcat(&ptr, &len, "mainline of chunk");
Dec 10, 2006
Dec 10, 2006
268
else if (strcmp(ldbg.what, "tail") == 0)
Dec 10, 2006
Dec 10, 2006
269
snprintfcat(&ptr, &len, "tail call");
Dec 10, 2006
Dec 10, 2006
270
else
Dec 10, 2006
Dec 10, 2006
271
snprintfcat(&ptr, &len, "unidentifiable function");
Dec 10, 2006
Dec 10, 2006
272
273
} // if
Jan 12, 2008
Jan 12, 2008
274
logDebug("%0", (const char *) scratchbuf_128k);
Dec 10, 2006
Dec 10, 2006
275
276
277
ptr = (char *) scratchbuf_128k;
len = sizeof (scratchbuf_128k);
Dec 10, 2006
Dec 10, 2006
278
for (spacing = 0; spacing < maxspacing; spacing++)
Dec 10, 2006
Dec 10, 2006
279
snprintfcat(&ptr, &len, " ");
Dec 10, 2006
Dec 10, 2006
280
281
if (strcmp(ldbg.what, "C") == 0)
Dec 10, 2006
Dec 10, 2006
282
snprintfcat(&ptr, &len, "in native code");
Dec 10, 2006
Dec 10, 2006
283
else if (strcmp(ldbg.what, "tail") == 0)
Dec 10, 2006
Dec 10, 2006
284
snprintfcat(&ptr, &len, "in Lua code");
Dec 10, 2006
Dec 10, 2006
285
286
else if ( (strcmp(ldbg.source, "=?") == 0) && (ldbg.currentline == 0) )
snprintfcat(&ptr, &len, "in Lua code (debug info stripped)");
Dec 10, 2006
Dec 10, 2006
287
288
else
{
Dec 11, 2006
Dec 11, 2006
289
snprintfcat(&ptr, &len, "in Lua code at %s", ldbg.short_src);
Dec 10, 2006
Dec 10, 2006
290
if (ldbg.currentline != -1)
Dec 10, 2006
Dec 10, 2006
291
snprintfcat(&ptr, &len, ":%d", ldbg.currentline);
Dec 10, 2006
Dec 10, 2006
292
} // else
Jan 12, 2008
Jan 12, 2008
293
logDebug("%0", (const char *) scratchbuf_128k);
Dec 10, 2006
Dec 10, 2006
294
295
} // for
Apr 22, 2007
Apr 22, 2007
296
return retvalString(L, errstr ? errstr : "");
Dec 10, 2006
Dec 10, 2006
297
298
299
} // luahook_stackwalk
Dec 11, 2006
Dec 11, 2006
300
301
302
303
304
305
306
307
308
309
310
311
// This just lets you punch in one-liners and Lua will run them as individual
// chunks, but you can completely access all Lua state, including calling C
// functions and altering tables. At this time, it's more of a "console"
// than a debugger. You can do "p MojoLua_debugger()" from gdb to launch this
// from a breakpoint in native code, or call MojoSetup.debugger() to launch
// it from Lua code (with stacktrace intact, too: type 'bt' to see it).
static int luahook_debugger(lua_State *L)
{
#if DISABLE_LUA_PARSER
logError("Lua debugger is disabled in this build (no parser).");
#else
int origtop;
Oct 23, 2008
Oct 23, 2008
312
const MojoSetupLogLevel origloglevel = MojoLog_logLevel;
Dec 11, 2006
Dec 11, 2006
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
lua_pushcfunction(luaState, luahook_stackwalk);
origtop = lua_gettop(L);
printf("Quick and dirty Lua debugger. Type 'exit' to quit.\n");
while (true)
{
char *buf = (char *) scratchbuf_128k;
int len = 0;
printf("> ");
fflush(stdout);
if (fgets(buf, sizeof (scratchbuf_128k), stdin) == NULL)
{
printf("\n\n fgets() on stdin failed: ");
break;
} // if
len = (int) (strlen(buf) - 1);
while ( (len >= 0) && ((buf[len] == '\n') || (buf[len] == '\r')) )
buf[len--] = '\0';
if (strcmp(buf, "q") == 0)
break;
Oct 23, 2008
Oct 23, 2008
337
338
else if (strcmp(buf, "quit") == 0)
break;
Dec 11, 2006
Dec 11, 2006
339
340
341
else if (strcmp(buf, "exit") == 0)
break;
else if (strcmp(buf, "bt") == 0)
Oct 23, 2008
Oct 23, 2008
342
343
{
MojoLog_logLevel = MOJOSETUP_LOG_EVERYTHING;
Dec 11, 2006
Dec 11, 2006
344
strcpy(buf, "MojoSetup.stackwalk()");
Oct 23, 2008
Oct 23, 2008
345
} // else if
Dec 11, 2006
Dec 11, 2006
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
if ( (luaL_loadstring(L, buf) != 0) ||
(lua_pcall(luaState, 0, LUA_MULTRET, -2) != 0) )
{
printf("%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
} // if
else
{
printf("Returned %d values.\n", lua_gettop(L) - origtop);
while (lua_gettop(L) != origtop)
{
// !!! FIXME: dump details of values to stdout here.
lua_pop(L, 1);
} // while
printf("\n");
} // else
Oct 23, 2008
Oct 23, 2008
363
364
MojoLog_logLevel = origloglevel;
Dec 11, 2006
Dec 11, 2006
365
366
367
368
369
} // while
lua_pop(L, 1);
printf("exiting debugger...\n");
#endif
Dec 12, 2006
Dec 12, 2006
370
371
return 0;
Dec 11, 2006
Dec 11, 2006
372
373
374
375
376
377
378
379
380
} // luahook_debugger
void MojoLua_debugger(void)
{
luahook_debugger(luaState);
} // MojoLua_debugger
May 7, 2007
May 7, 2007
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
boolean MojoLua_callProcedure(const char *funcname)
{
boolean called = false;
lua_State *L = luaState;
int popcount = 0;
if (L != NULL)
{
lua_getglobal(L, MOJOSETUP_NAMESPACE); popcount++;
if (lua_istable(L, -1)) // namespace is sane?
{
lua_getfield(L, -1, funcname); popcount++;
if (lua_isfunction(L, -1))
{
lua_call(L, 0, 0);
called = true;
} // if
} // if
lua_pop(L, popcount);
} // if
return called;
} // MojoLua_callProcedure
Jan 20, 2008
Jan 20, 2008
406
boolean MojoLua_runFileFromDir(const char *dir, const char *name)
Dec 3, 2006
Dec 3, 2006
407
{
Dec 9, 2006
Dec 9, 2006
408
MojoArchive *ar = GBaseArchive; // in case we want to generalize later.
Feb 26, 2008
Feb 26, 2008
409
const MojoArchiveEntry *entinfo = NULL;
Dec 3, 2006
Dec 3, 2006
410
boolean retval = false;
Jan 20, 2008
Jan 20, 2008
411
412
char *clua = format("%0/%1.luac", dir, name); // compiled filename.
char *ulua = format("%0/%1.lua", dir, name); // uncompiled filename.
Dec 3, 2006
Dec 3, 2006
413
414
415
int rc = 0;
MojoInput *io = NULL;
May 4, 2007
May 4, 2007
416
if (ar->enumerate(ar))
Dec 3, 2006
Dec 3, 2006
417
{
May 4, 2007
May 4, 2007
418
while ((io == NULL) && ((entinfo = ar->enumNext(ar)) != NULL))
Dec 9, 2006
Dec 9, 2006
419
{
May 4, 2007
May 4, 2007
420
421
422
423
424
425
boolean match = false;
if (entinfo->type != MOJOARCHIVE_ENTRY_FILE)
continue;
match = (strcmp(entinfo->filename, clua) == 0);
Dec 9, 2006
Dec 9, 2006
426
427
428
429
430
431
#if !DISABLE_LUA_PARSER
if (!match)
match = (strcmp(entinfo->filename, ulua) == 0);
#endif
if (match)
May 4, 2007
May 4, 2007
432
io = ar->openCurrentEntry(ar);
Dec 9, 2006
Dec 9, 2006
433
} // while
Dec 3, 2006
Dec 3, 2006
434
435
} // if
Jan 20, 2008
Jan 20, 2008
436
437
438
free(ulua);
free(clua);
Dec 3, 2006
Dec 3, 2006
439
440
if (io != NULL)
{
May 4, 2007
May 4, 2007
441
442
char *realfname = (char *) xmalloc(strlen(entinfo->filename) + 2);
sprintf(realfname, "@%s", entinfo->filename);
Dec 10, 2006
Dec 10, 2006
443
lua_pushcfunction(luaState, luahook_stackwalk);
Dec 11, 2006
Dec 11, 2006
444
445
rc = lua_load(luaState, MojoLua_reader, io, realfname);
free(realfname);
Dec 3, 2006
Dec 3, 2006
446
447
448
449
450
451
io->close(io);
if (rc != 0)
lua_error(luaState);
else
{
Dec 11, 2006
Dec 11, 2006
452
// Call new chunk on top of the stack (lua_pcall will pop it off).
Dec 10, 2006
Dec 10, 2006
453
454
455
456
if (lua_pcall(luaState, 0, 0, -2) != 0) // retvals are dumped.
lua_error(luaState); // error on stack has debug info.
else
retval = true; // if this didn't panic, we succeeded.
Dec 3, 2006
Dec 3, 2006
457
} // if
Dec 10, 2006
Dec 10, 2006
458
lua_pop(luaState, 1); // dump stackwalker.
Dec 3, 2006
Dec 3, 2006
459
460
461
} // if
return retval;
Jan 20, 2008
Jan 20, 2008
462
463
464
465
466
467
} // MojoLua_runFileFromDir
boolean MojoLua_runFile(const char *name)
{
return MojoLua_runFileFromDir("scripts", name);
Dec 3, 2006
Dec 3, 2006
468
469
470
} // MojoLua_runFile
Dec 3, 2006
Dec 3, 2006
471
472
473
void MojoLua_collectGarbage(void)
{
lua_State *L = luaState;
Dec 4, 2006
Dec 4, 2006
474
uint32 ticks = 0;
Dec 3, 2006
Dec 3, 2006
475
476
477
int pre = 0;
int post = 0;
Jul 7, 2009
Jul 7, 2009
478
479
480
481
482
lua_getglobal(L, MOJOSETUP_NAMESPACE);
if (lua_istable(L, -1)) // namespace is sane?
set_integer(L, 0, "garbagecounter");
lua_pop(L, 1);
Dec 3, 2006
Dec 3, 2006
483
pre = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
Jan 12, 2008
Jan 12, 2008
484
logDebug("Collecting garbage (currently using %0 bytes).", numstr(pre));
Dec 4, 2006
Dec 4, 2006
485
ticks = MojoPlatform_ticks();
Dec 3, 2006
Dec 3, 2006
486
lua_gc (L, LUA_GCCOLLECT, 0);
Dec 10, 2006
Dec 10, 2006
487
profile("Garbage collection", ticks);
Dec 3, 2006
Dec 3, 2006
488
post = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
Jan 12, 2008
Jan 12, 2008
489
490
logDebug("Now using %0 bytes (%1 bytes savings).",
numstr(post), numstr(pre - post));
Dec 3, 2006
Dec 3, 2006
491
492
493
} // MojoLua_collectGarbage
Dec 11, 2006
Dec 11, 2006
494
// You can trigger the garbage collector with more control in the standard
Jul 7, 2009
Jul 7, 2009
495
496
// Lua runtime, but this notes profiling and statistics via logDebug(),
// and resets MojoSetup.garbagecounter to zero.
Dec 11, 2006
Dec 11, 2006
497
498
499
500
501
502
503
static int luahook_collectgarbage(lua_State *L)
{
MojoLua_collectGarbage();
return 0;
} // luahook_collectgarbage
Dec 2, 2006
Dec 2, 2006
504
505
506
507
508
// Since localization is kept in Lua tables, I stuck this in the Lua glue.
const char *translate(const char *str)
{
const char *retval = str;
Dec 6, 2006
Dec 6, 2006
509
if (luaState != NULL) // No translations before Lua is initialized.
Dec 2, 2006
Dec 2, 2006
510
{
Dec 6, 2006
Dec 6, 2006
511
if (lua_checkstack(luaState, 3))
Dec 2, 2006
Dec 2, 2006
512
{
Dec 6, 2006
Dec 6, 2006
513
514
515
int popcount = 0;
lua_getglobal(luaState, MOJOSETUP_NAMESPACE); popcount++;
if (lua_istable(luaState, -1)) // namespace is sane?
Dec 2, 2006
Dec 2, 2006
516
{
Dec 6, 2006
Dec 6, 2006
517
518
lua_getfield(luaState, -1, "translations"); popcount++;
if (lua_istable(luaState, -1)) // translation table is sane?
Dec 4, 2006
Dec 4, 2006
519
{
Dec 6, 2006
Dec 6, 2006
520
521
522
523
524
const char *tr = NULL;
lua_getfield(luaState, -1, str); popcount++;
tr = lua_tostring(luaState, -1);
if (tr != NULL) // translated for this locale?
{
Dec 8, 2006
Dec 8, 2006
525
526
527
char *dst = (char *) scratchbuf_128k;
xstrncpy(dst, tr, sizeof(scratchbuf_128k));
retval = dst;
Dec 6, 2006
Dec 6, 2006
528
} // if
Dec 4, 2006
Dec 4, 2006
529
} // if
Dec 2, 2006
Dec 2, 2006
530
} // if
Dec 6, 2006
Dec 6, 2006
531
lua_pop(luaState, popcount); // remove our stack salsa.
Dec 2, 2006
Dec 2, 2006
532
533
534
535
536
537
538
} // if
} // if
return retval;
} // translate
Jan 12, 2008
Jan 12, 2008
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
// Lua interface to format().
static int luahook_format(lua_State *L)
{
const int argc = lua_gettop(L);
const char *fmt = luaL_checkstring(L, 1);
char *formatted = NULL;
char *s[10];
int i;
assert(argc <= 11); // fmt, plus %0 through %9.
for (i = 0; i < STATICARRAYLEN(s); i++)
{
const char *str = NULL;
if ((i+2) <= argc)
str = lua_tostring(L, i+2);
s[i] = (str == NULL) ? NULL : xstrdup(str);
} // for
// I think this is legal (but probably not moral) C code.
formatted = format(fmt,s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],s[9]);
for (i = 0; i < STATICARRAYLEN(s); i++)
free(s[i]);
lua_pushstring(L, formatted);
free(formatted);
return 1;
} // luahook_format
Dec 6, 2006
Dec 6, 2006
570
571
572
// Use this instead of Lua's error() function if you don't have a
// programatic error, so you don't get stack callback stuff:
// MojoSetup.fatal("You need the base game to install this expansion pack.")
May 7, 2007
May 7, 2007
573
// This will also handle cleanup of half-written installations.
Dec 6, 2006
Dec 6, 2006
574
575
576
// Doesn't actually return.
static int luahook_fatal(lua_State *L)
{
Dec 20, 2006
Dec 20, 2006
577
const char *errstr = lua_tostring(L, 1);
Jan 12, 2008
Jan 12, 2008
578
579
580
if (errstr == NULL)
return fatal(NULL); // doesn't actually return.
return fatal("%0", errstr); // doesn't actually return.
Dec 10, 2006
Dec 10, 2006
581
} // luahook_fatal
Dec 6, 2006
Dec 6, 2006
582
583
Dec 3, 2006
Dec 3, 2006
584
585
586
587
588
// Lua interface to MojoLua_runFile(). This is needed instead of Lua's
// require(), since it can access scripts inside an archive.
static int luahook_runfile(lua_State *L)
{
const char *fname = luaL_checkstring(L, 1);
Apr 22, 2007
Apr 22, 2007
589
return retvalBoolean(L, MojoLua_runFile(fname));
Dec 3, 2006
Dec 3, 2006
590
591
592
} // luahook_runfile
Jan 20, 2008
Jan 20, 2008
593
594
595
596
597
598
599
600
601
602
// Lua interface to MojoLua_runFileFromDir(). This is needed instead of Lua's
// require(), since it can access scripts inside an archive.
static int luahook_runfilefromdir(lua_State *L)
{
const char *dir = luaL_checkstring(L, 1);
const char *fname = luaL_checkstring(L, 2);
return retvalBoolean(L, MojoLua_runFileFromDir(dir, fname));
} // luahook_runfile
Dec 4, 2006
Dec 4, 2006
603
604
605
606
// Lua interface to translate().
static int luahook_translate(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
Apr 22, 2007
Apr 22, 2007
607
return retvalString(L, translate(str));
Dec 4, 2006
Dec 4, 2006
608
609
610
} // luahook_translate
Dec 5, 2006
Dec 5, 2006
611
612
static int luahook_ticks(lua_State *L)
{
Jan 25, 2008
Jan 25, 2008
613
return retvalNumber(L, MojoPlatform_ticks());
Dec 5, 2006
Dec 5, 2006
614
615
616
} // luahook_ticks
Jan 25, 2008
Jan 25, 2008
617
618
619
620
621
622
623
static int luahook_launchbrowser(lua_State *L)
{
const char *url = luaL_checkstring(L, 1);
return retvalBoolean(L, MojoPlatform_launchBrowser(url));
} // luahook_launchbrowser
Apr 25, 2009
Apr 25, 2009
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
static int luahook_verifyproductkey(lua_State *L)
{
boolean retval = false;
// ATTENTION: you can use this function to do your CD key verification
// in C code. Just remove the #if 0, and then process the ASCII string
// in (key), setting (retval) to (true) if the key is valid, and (false)
// if it isn't. Then when filling in a Setup.ProductKey in your
// config.lua, set (verify) to "MojoSetup.verifyproductkey".
#if 0
const char *key = luaL_checkstring(L, 1);
#endif
return retvalBoolean(L, retval);
} // luahook_verifyproductkey
Dec 6, 2006
Dec 6, 2006
641
642
643
644
645
646
static int luahook_msgbox(lua_State *L)
{
if (GGui != NULL)
{
const char *title = luaL_checkstring(L, 1);
const char *text = luaL_checkstring(L, 2);
Dec 7, 2006
Dec 7, 2006
647
GGui->msgbox(title, text);
Dec 6, 2006
Dec 6, 2006
648
649
650
651
652
653
654
655
656
657
658
659
} // if
return 0;
} // luahook_msgbox
static int luahook_promptyn(lua_State *L)
{
boolean rc = false;
if (GGui != NULL)
{
const char *title = luaL_checkstring(L, 1);
const char *text = luaL_checkstring(L, 2);
Jul 2, 2007
Jul 2, 2007
660
661
const boolean defval = lua_toboolean(L, 3);
rc = GGui->promptyn(title, text, defval);
Dec 6, 2006
Dec 6, 2006
662
663
} // if
Apr 22, 2007
Apr 22, 2007
664
665
return retvalBoolean(L, rc);
} // luahook_promptyn
Dec 6, 2006
Dec 6, 2006
666
667
May 17, 2007
May 17, 2007
668
669
670
671
672
673
674
static int luahook_promptynan(lua_State *L)
{
MojoGuiYNAN rc = MOJOGUI_NO;
if (GGui != NULL)
{
const char *title = luaL_checkstring(L, 1);
const char *text = luaL_checkstring(L, 2);
Jul 2, 2007
Jul 2, 2007
675
676
const boolean defval = lua_toboolean(L, 3);
rc = GGui->promptynan(title, text, defval);
May 17, 2007
May 17, 2007
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
} // if
// Never localize these strings!
switch (rc)
{
case MOJOGUI_YES: return retvalString(L, "yes");
case MOJOGUI_NO: return retvalString(L, "no");
case MOJOGUI_ALWAYS: return retvalString(L, "always");
case MOJOGUI_NEVER: return retvalString(L, "never");
} // switch
assert(false && "BUG: unhandled case in switch statement");
return 0; // shouldn't hit this.
} // luahook_promptynan
Dec 10, 2006
Dec 10, 2006
693
694
static int luahook_logwarning(lua_State *L)
{
Jan 12, 2008
Jan 12, 2008
695
logWarning("%0", luaL_checkstring(L, 1));
Dec 10, 2006
Dec 10, 2006
696
697
698
699
700
701
return 0;
} // luahook_logwarning
static int luahook_logerror(lua_State *L)
{
Jan 12, 2008
Jan 12, 2008
702
logError("%0", luaL_checkstring(L, 1));
Dec 10, 2006
Dec 10, 2006
703
704
705
706
707
708
return 0;
} // luahook_logerror
static int luahook_loginfo(lua_State *L)
{
Jan 12, 2008
Jan 12, 2008
709
logInfo("%0", luaL_checkstring(L, 1));
Dec 10, 2006
Dec 10, 2006
710
711
712
713
714
715
return 0;
} // luahook_loginfo
static int luahook_logdebug(lua_State *L)
{
Jan 12, 2008
Jan 12, 2008
716
logDebug("%0", luaL_checkstring(L, 1));
Dec 10, 2006
Dec 10, 2006
717
718
719
720
721
722
723
return 0;
} // luahook_logdebug
static int luahook_cmdline(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
Apr 22, 2007
Apr 22, 2007
724
return retvalBoolean(L, cmdline(arg));
Dec 10, 2006
Dec 10, 2006
725
726
727
728
729
} // luahook_cmdline
static int luahook_cmdlinestr(lua_State *L)
{
Dec 27, 2006
Dec 27, 2006
730
const int argc = lua_gettop(L);
Dec 10, 2006
Dec 10, 2006
731
const char *arg = luaL_checkstring(L, 1);
Feb 20, 2008
Feb 20, 2008
732
733
const char *envr = (argc < 2) ? NULL : lua_tostring(L, 2); // may be nil
const char *deflt = (argc < 3) ? NULL : lua_tostring(L, 3); // may be nil
Apr 22, 2007
Apr 22, 2007
734
return retvalString(L, cmdlinestr(arg, envr, deflt));
Dec 10, 2006
Dec 10, 2006
735
736
737
} // luahook_cmdlinestr
May 8, 2007
May 8, 2007
738
739
740
741
742
743
744
static int luahook_truncatenum(lua_State *L)
{
const lua_Number dbl = lua_tonumber(L, 1);
return retvalNumber(L, (lua_Number) ((int64) dbl));
} // luahook_truncatenum
May 2, 2007
May 2, 2007
745
746
747
748
749
750
751
752
static int luahook_wildcardmatch(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
const char *pattern = luaL_checkstring(L, 2);
return retvalBoolean(L, wildcardMatch(str, pattern));
} // luahook_wildcardmatch
Jan 21, 2008
Jan 21, 2008
753
754
755
756
757
758
759
760
761
762
// Do a regular C strcmp(), don't let the locale get in the way like it does
// in Lua's string comparison operators.
static int luahook_strcmp(lua_State *L)
{
const char *a = luaL_checkstring(L, 1);
const char *b = luaL_checkstring(L, 2);
return retvalNumber(L, strcmp(a, b));
} // luahook_strcmp
Apr 22, 2007
Apr 22, 2007
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
static int luahook_findmedia(lua_State *L)
{
// Let user specify overrides of directories to act as drives.
// This is good if for some reason MojoSetup can't find them, or they
// want to pretend a copy on the filesystem is really a CD, etc.
// Put your anti-piracy crap in your OWN program. :)
//
// You can specify with command lines or environment variables, command
// lines taking precedence:
// MOJOSETUP_MEDIA0=/my/patch ./installer --media1=/over/there
//
// --media and MOJOSETUP_MEDIA are checked first, then --media0 and
// MOJOSETUP_MEDIA0, etc until you find the media or run out of
// overrides.
//
// After the overrides, we ask the platform layer to find the media.
const char *unique = luaL_checkstring(L, 1);
const char *override = cmdlinestr("media", "MOJOSETUP_MEDIA", NULL);
char *physical = NULL;
char cmdbuf[64];
char envrbuf[64];
int i = 0;
do
{
if ( (override) && (MojoPlatform_exists(override, unique)) )
return retvalString(L, override);
snprintf(cmdbuf, sizeof (cmdbuf), "media%d", i);
snprintf(envrbuf, sizeof (envrbuf), "MOJOSETUP_MEDIA%d", i);
} while ((override = cmdlinestr(cmdbuf, envrbuf, NULL)) != NULL);
// No override. Try platform layer for real media...
physical = MojoPlatform_findMedia(unique);
retvalString(L, physical); // may push nil.
free(physical);
return 1;
} // luahook_findmedia
May 8, 2007
May 8, 2007
804
805
static boolean writeCallback(uint32 ticks, int64 justwrote, int64 bw,
int64 total, void *data)
May 1, 2007
May 1, 2007
806
{
May 7, 2007
May 7, 2007
807
808
809
810
811
812
813
814
815
boolean retval = false;
lua_State *L = (lua_State *) data;
// Lua callback is on top of stack...
if (lua_isnil(L, -1))
retval = true;
else
{
lua_pushvalue(L, -1);
lua_pushnumber(L, (lua_Number) ticks);
May 8, 2007
May 8, 2007
816
lua_pushnumber(L, (lua_Number) justwrote);
May 7, 2007
May 7, 2007
817
818
lua_pushnumber(L, (lua_Number) bw);
lua_pushnumber(L, (lua_Number) total);
May 8, 2007
May 8, 2007
819
lua_call(L, 4, 1);
May 7, 2007
May 7, 2007
820
821
822
823
824
retval = lua_toboolean(L, -1);
lua_pop(L, 1);
} // if
return retval;
} // writeCallback
May 1, 2007
May 1, 2007
825
826
827
// !!! FIXME: push this into Lua, make things fatal.
Jan 17, 2008
Jan 17, 2008
828
static int do_writefile(lua_State *L, MojoInput *in, uint16 perms)
May 1, 2007
May 1, 2007
829
{
May 2, 2007
May 2, 2007
830
const char *path = luaL_checkstring(L, 2);
May 27, 2007
May 27, 2007
831
832
833
int retval = 0;
boolean rc = false;
MojoChecksums sums;
Jan 16, 2008
Jan 16, 2008
834
835
int64 maxbytes = -1;
May 7, 2007
May 7, 2007
836
if (in != NULL)
May 1, 2007
May 1, 2007
837
{
May 18, 2007
May 18, 2007
838
839
840
841
842
843
if (!lua_isnil(L, 3))
{
boolean valid = false;
const char *permstr = luaL_checkstring(L, 3);
perms = MojoPlatform_makePermissions(permstr, &valid);
if (!valid)
Jan 12, 2008
Jan 12, 2008
844
fatal(_("BUG: '%0' is not a valid permission string"), permstr);
May 18, 2007
May 18, 2007
845
} // if
Jan 16, 2008
Jan 16, 2008
846
847
848
849
850
851
if (!lua_isnil(L, 4))
maxbytes = luaL_checkinteger(L, 4);
rc = MojoInput_toPhysicalFile(in, path, perms, &sums, maxbytes,
writeCallback, L);
May 1, 2007
May 1, 2007
852
853
} // if
May 27, 2007
May 27, 2007
854
855
856
857
retval += retvalBoolean(L, rc);
if (rc)
retval += retvalChecksums(L, &sums);
return retval;
Jan 17, 2008
Jan 17, 2008
858
859
860
861
862
863
864
865
866
} // do_writefile
static int luahook_writefile(lua_State *L)
{
MojoArchive *archive = (MojoArchive *) lua_touserdata(L, 1);
uint16 perms = archive->prevEnum.perms;
MojoInput *in = archive->openCurrentEntry(archive);
return do_writefile(L, in, perms);
May 1, 2007
May 1, 2007
867
868
869
} // luahook_writefile
Jan 17, 2008
Jan 17, 2008
870
871
872
static int luahook_download(lua_State *L)
{
const char *src = luaL_checkstring(L, 1);
Jan 17, 2008
Jan 17, 2008
873
MojoInput *in = MojoInput_newFromURL(src);
Jan 17, 2008
Jan 17, 2008
874
875
876
877
return do_writefile(L, in, MojoPlatform_defaultFilePerms());
} // luahook_download
Jan 17, 2008
Jan 17, 2008
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
static int luahook_copyfile(lua_State *L)
{
const char *src = luaL_checkstring(L, 1);
MojoInput *in = MojoInput_newFromFile(src);
return do_writefile(L, in, MojoPlatform_defaultFilePerms());
} // luahook_copyfile
static int luahook_stringtofile(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
MojoInput *in = NULL;
size_t len = 0;
str = lua_tolstring(L, 1, &len);
in = MojoInput_newFromMemory((const uint8 *) str, (uint32) len, 1);
assert(in != NULL); // xmalloc() would fatal(), should not return NULL.
return do_writefile(L, in, MojoPlatform_defaultFilePerms());
} // luahook_stringtofile
May 18, 2007
May 18, 2007
898
899
900
901
902
903
904
905
906
907
908
static int luahook_isvalidperms(lua_State *L)
{
boolean valid = false;
const char *permstr = NULL;
if (!lua_isnil(L, 1))
permstr = luaL_checkstring(L, 1);
MojoPlatform_makePermissions(permstr, &valid);
return retvalBoolean(L, valid);
} // luahook_isvalidperms
Jan 20, 2008
Jan 20, 2008
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
static int do_checksum(lua_State *L, MojoInput *in)
{
MojoChecksumContext ctx;
MojoChecksums sums;
int64 br = 0;
MojoChecksum_init(&ctx);
while (1)
{
br = in->read(in, scratchbuf_128k, sizeof (scratchbuf_128k));
if (br <= 0)
break;
MojoChecksum_append(&ctx, scratchbuf_128k, (uint32) br);
} // while
MojoChecksum_finish(&ctx, &sums);
in->close(in);
return (br < 0) ? 0 : retvalChecksums(L, &sums);
} // do_checksum
static int luahook_checksum(lua_State *L)
{
const char *fname = luaL_checkstring(L, 1);
MojoInput *in = MojoInput_newFromFile(fname);
return do_checksum(L, in);
} // luahook_checksum
May 1, 2007
May 1, 2007
941
942
943
static int luahook_archive_fromdir(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
May 7, 2007
May 7, 2007
944
return retvalLightUserData(L, MojoArchive_newFromDirectory(path));
May 1, 2007
May 1, 2007
945
946
947
} // luahook_archive_fromdir
May 2, 2007
May 2, 2007
948
949
950
951
952
953
static int luahook_archive_fromfile(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
MojoInput *io = MojoInput_newFromFile(path);
MojoArchive *archive = NULL;
if (io != NULL)
May 5, 2007
May 5, 2007
954
archive = MojoArchive_newFromInput(io, path);
May 7, 2007
May 7, 2007
955
return retvalLightUserData(L, archive);
May 2, 2007
May 2, 2007
956
957
958
959
960
961
962
963
964
} // luahook_archive_fromfile
static int luahook_archive_fromentry(lua_State *L)
{
MojoArchive *ar = (MojoArchive *) lua_touserdata(L, 1);
MojoInput *io = ar->openCurrentEntry(ar);
MojoArchive *archive = NULL;
if (io != NULL)
May 5, 2007
May 5, 2007
965
archive = MojoArchive_newFromInput(io, ar->prevEnum.filename);
May 7, 2007
May 7, 2007
966
return retvalLightUserData(L, archive);
May 2, 2007
May 2, 2007
967
968
969
} // luahook_archive_fromentry
May 2, 2007
May 2, 2007
970
971
972
static int luahook_archive_enumerate(lua_State *L)
{
MojoArchive *archive = (MojoArchive *) lua_touserdata(L, 1);
May 4, 2007
May 4, 2007
973
return retvalBoolean(L, archive->enumerate(archive));
May 2, 2007
May 2, 2007
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
} // luahook_archive_enumerate
static int luahook_archive_enumnext(lua_State *L)
{
MojoArchive *archive = (MojoArchive *) lua_touserdata(L, 1);
const MojoArchiveEntry *entinfo = archive->enumNext(archive);
if (entinfo == NULL)
lua_pushnil(L);
else
{
const char *typestr = NULL;
if (entinfo->type == MOJOARCHIVE_ENTRY_FILE)
typestr = "file";
else if (entinfo->type == MOJOARCHIVE_ENTRY_DIR)
typestr = "dir";
else if (entinfo->type == MOJOARCHIVE_ENTRY_SYMLINK)
typestr = "symlink";
else
typestr = "unknown";
lua_newtable(L);
set_string(L, entinfo->filename, "filename");
set_string(L, entinfo->linkdest, "linkdest");
Sep 25, 2007
Sep 25, 2007
998
set_number(L, (lua_Number) entinfo->filesize, "filesize");
May 2, 2007
May 2, 2007
999
1000
set_string(L, typestr, "type");
} // else