/
lua_glue.c
1638 lines (1359 loc) · 49.3 KB
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.
*/
9
#include "universal.h"
10
#include "lua_glue.h"
11
#include "platform.h"
12
13
14
15
#include "fileio.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
16
#include "gui.h"
17
18
19
20
21
#define MOJOSETUP_NAMESPACE "MojoSetup"
static lua_State *luaState = NULL;
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
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;
38
char *retval = (char *) scratchbuf_128k;
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
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
// 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);
} // set_string
// !!! 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
145
146
147
148
149
150
151
152
153
154
static inline int retvalLightUserData(lua_State *L, void *data)
{
if (data != NULL)
lua_pushlightuserdata(L, data);
else
lua_pushnil(L);
return 1;
} // retvalLightUserData
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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
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
211
212
213
214
215
216
217
static int luahook_stackwalk(lua_State *L)
{
const char *errstr = lua_tostring(L, 1);
lua_Debug ldbg;
int i = 0;
if (errstr != NULL)
218
logDebug("%s\n", errstr);
219
220
logDebug("Lua stack backtrace:");
221
222
223
224
// start at 1 to skip this function.
for (i = 1; lua_getstack(L, i, &ldbg); i++)
{
225
226
227
char *ptr = (char *) scratchbuf_128k;
size_t len = sizeof (scratchbuf_128k);
int bw = snprintfcat(&ptr, &len, "#%d", i-1);
228
229
230
const int maxspacing = 4;
int spacing = maxspacing - bw;
while (spacing-- > 0)
231
snprintfcat(&ptr, &len, " ");
232
233
234
if (!lua_getinfo(L, "nSl", &ldbg))
{
235
236
snprintfcat(&ptr, &len, "???\n");
logDebug((const char *) scratchbuf_128k);
237
238
239
240
continue;
} // if
if (ldbg.namewhat[0])
241
snprintfcat(&ptr, &len, "%s ", ldbg.namewhat);
242
243
if ((ldbg.name) && (ldbg.name[0]))
244
snprintfcat(&ptr, &len, "function %s ()", ldbg.name);
245
246
247
else
{
if (strcmp(ldbg.what, "main") == 0)
248
snprintfcat(&ptr, &len, "mainline of chunk");
249
else if (strcmp(ldbg.what, "tail") == 0)
250
snprintfcat(&ptr, &len, "tail call");
251
else
252
snprintfcat(&ptr, &len, "unidentifiable function");
253
254
} // if
255
256
257
258
logDebug((const char *) scratchbuf_128k);
ptr = (char *) scratchbuf_128k;
len = sizeof (scratchbuf_128k);
259
for (spacing = 0; spacing < maxspacing; spacing++)
260
snprintfcat(&ptr, &len, " ");
261
262
if (strcmp(ldbg.what, "C") == 0)
263
snprintfcat(&ptr, &len, "in native code");
264
else if (strcmp(ldbg.what, "tail") == 0)
265
snprintfcat(&ptr, &len, "in Lua code");
266
267
else if ( (strcmp(ldbg.source, "=?") == 0) && (ldbg.currentline == 0) )
snprintfcat(&ptr, &len, "in Lua code (debug info stripped)");
268
269
else
{
270
snprintfcat(&ptr, &len, "in Lua code at %s", ldbg.short_src);
271
if (ldbg.currentline != -1)
272
snprintfcat(&ptr, &len, ":%d", ldbg.currentline);
273
} // else
274
logDebug((const char *) scratchbuf_128k);
275
276
} // for
277
return retvalString(L, errstr ? errstr : "");
278
279
280
} // luahook_stackwalk
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
// 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;
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;
else if (strcmp(buf, "exit") == 0)
break;
else if (strcmp(buf, "bt") == 0)
strcpy(buf, "MojoSetup.stackwalk()");
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
} // while
lua_pop(L, 1);
printf("exiting debugger...\n");
#endif
343
344
return 0;
345
346
347
348
349
350
351
352
353
} // luahook_debugger
void MojoLua_debugger(void)
{
luahook_debugger(luaState);
} // MojoLua_debugger
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
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
379
boolean MojoLua_runFile(const char *name)
380
{
381
MojoArchive *ar = GBaseArchive; // in case we want to generalize later.
382
const MojoArchiveEntry *entinfo;
383
boolean retval = false;
384
385
char clua[128]; // compiled filename.
char ulua[128]; // uncompiled filename.
386
387
388
int rc = 0;
MojoInput *io = NULL;
389
if (snprintf(clua, sizeof(clua), "scripts/%s.luac", name) >= sizeof (clua))
390
return false;
391
392
if (snprintf(ulua, sizeof(ulua), "scripts/%s.lua", name) >= sizeof (ulua))
393
394
return false;
395
if (ar->enumerate(ar))
396
{
397
while ((io == NULL) && ((entinfo = ar->enumNext(ar)) != NULL))
398
{
399
400
401
402
403
404
boolean match = false;
if (entinfo->type != MOJOARCHIVE_ENTRY_FILE)
continue;
match = (strcmp(entinfo->filename, clua) == 0);
405
406
407
408
409
410
#if !DISABLE_LUA_PARSER
if (!match)
match = (strcmp(entinfo->filename, ulua) == 0);
#endif
if (match)
411
io = ar->openCurrentEntry(ar);
412
} // while
413
414
415
416
} // if
if (io != NULL)
{
417
418
char *realfname = (char *) xmalloc(strlen(entinfo->filename) + 2);
sprintf(realfname, "@%s", entinfo->filename);
419
lua_pushcfunction(luaState, luahook_stackwalk);
420
421
rc = lua_load(luaState, MojoLua_reader, io, realfname);
free(realfname);
422
423
424
425
426
427
io->close(io);
if (rc != 0)
lua_error(luaState);
else
{
428
// Call new chunk on top of the stack (lua_pcall will pop it off).
429
430
431
432
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.
433
} // if
434
lua_pop(luaState, 1); // dump stackwalker.
435
436
437
438
439
440
} // if
return retval;
} // MojoLua_runFile
441
442
443
void MojoLua_collectGarbage(void)
{
lua_State *L = luaState;
444
uint32 ticks = 0;
445
446
447
448
int pre = 0;
int post = 0;
pre = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
449
logDebug("Collecting garbage (currently using %d bytes).", pre);
450
ticks = MojoPlatform_ticks();
451
lua_gc (L, LUA_GCCOLLECT, 0);
452
profile("Garbage collection", ticks);
453
post = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
454
logDebug("Now using %d bytes (%d bytes savings).\n", post, pre - post);
455
456
457
} // MojoLua_collectGarbage
458
459
460
461
462
463
464
465
466
// You can trigger the garbage collector with more control in the standard
// Lua runtime, but this notes profiling and statistics via logDebug().
static int luahook_collectgarbage(lua_State *L)
{
MojoLua_collectGarbage();
return 0;
} // luahook_collectgarbage
467
468
469
470
471
// Since localization is kept in Lua tables, I stuck this in the Lua glue.
const char *translate(const char *str)
{
const char *retval = str;
472
if (luaState != NULL) // No translations before Lua is initialized.
473
{
474
if (lua_checkstack(luaState, 3))
475
{
476
477
478
int popcount = 0;
lua_getglobal(luaState, MOJOSETUP_NAMESPACE); popcount++;
if (lua_istable(luaState, -1)) // namespace is sane?
479
{
480
481
lua_getfield(luaState, -1, "translations"); popcount++;
if (lua_istable(luaState, -1)) // translation table is sane?
482
{
483
484
485
486
487
const char *tr = NULL;
lua_getfield(luaState, -1, str); popcount++;
tr = lua_tostring(luaState, -1);
if (tr != NULL) // translated for this locale?
{
488
489
490
char *dst = (char *) scratchbuf_128k;
xstrncpy(dst, tr, sizeof(scratchbuf_128k));
retval = dst;
491
} // if
492
} // if
493
} // if
494
lua_pop(luaState, popcount); // remove our stack salsa.
495
496
497
498
499
500
501
} // if
} // if
return retval;
} // translate
502
503
504
// 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.")
505
// This will also handle cleanup of half-written installations.
506
507
508
// Doesn't actually return.
static int luahook_fatal(lua_State *L)
{
509
510
511
512
const char *errstr = lua_tostring(L, 1);
if (errstr == NULL)
errstr = _("Unknown error");
return fatal(errstr); // doesn't actually return.
513
} // luahook_fatal
514
515
516
517
518
519
520
// 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);
521
return retvalBoolean(L, MojoLua_runFile(fname));
522
523
524
} // luahook_runfile
525
526
527
528
// Lua interface to translate().
static int luahook_translate(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
529
return retvalString(L, translate(str));
530
531
532
} // luahook_translate
533
534
535
536
537
538
539
static int luahook_ticks(lua_State *L)
{
lua_pushnumber(L, MojoPlatform_ticks());
return 1;
} // luahook_ticks
540
541
542
543
544
545
static int luahook_msgbox(lua_State *L)
{
if (GGui != NULL)
{
const char *title = luaL_checkstring(L, 1);
const char *text = luaL_checkstring(L, 2);
546
GGui->msgbox(title, text);
547
548
549
550
551
552
553
554
555
556
557
558
} // 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);
559
560
const boolean defval = lua_toboolean(L, 3);
rc = GGui->promptyn(title, text, defval);
561
562
} // if
563
564
return retvalBoolean(L, rc);
} // luahook_promptyn
565
566
567
568
569
570
571
572
573
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);
574
575
const boolean defval = lua_toboolean(L, 3);
rc = GGui->promptynan(title, text, defval);
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
} // 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
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
static int luahook_logwarning(lua_State *L)
{
logWarning(luaL_checkstring(L, 1));
return 0;
} // luahook_logwarning
static int luahook_logerror(lua_State *L)
{
logError(luaL_checkstring(L, 1));
return 0;
} // luahook_logerror
static int luahook_loginfo(lua_State *L)
{
logInfo(luaL_checkstring(L, 1));
return 0;
} // luahook_loginfo
static int luahook_logdebug(lua_State *L)
{
logDebug(luaL_checkstring(L, 1));
return 0;
} // luahook_logdebug
static int luahook_cmdline(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
623
return retvalBoolean(L, cmdline(arg));
624
625
626
627
628
} // luahook_cmdline
static int luahook_cmdlinestr(lua_State *L)
{
629
const int argc = lua_gettop(L);
630
const char *arg = luaL_checkstring(L, 1);
631
632
const char *envr = (argc < 2) ? NULL : luaL_checkstring(L, 2);
const char *deflt = (argc < 3) ? NULL : luaL_checkstring(L, 3);
633
return retvalString(L, cmdlinestr(arg, envr, deflt));
634
635
636
} // luahook_cmdlinestr
637
638
639
640
641
642
643
static int luahook_truncatenum(lua_State *L)
{
const lua_Number dbl = lua_tonumber(L, 1);
return retvalNumber(L, (lua_Number) ((int64) dbl));
} // luahook_truncatenum
644
645
646
647
648
649
650
651
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
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
693
694
static boolean writeCallback(uint32 ticks, int64 justwrote, int64 bw,
int64 total, void *data)
695
{
696
697
698
699
700
701
702
703
704
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);
705
lua_pushnumber(L, (lua_Number) justwrote);
706
707
lua_pushnumber(L, (lua_Number) bw);
lua_pushnumber(L, (lua_Number) total);
708
lua_call(L, 4, 1);
709
710
711
712
713
retval = lua_toboolean(L, -1);
lua_pop(L, 1);
} // if
return retval;
} // writeCallback
714
715
716
717
718
// !!! FIXME: push this into Lua, make things fatal.
static int luahook_writefile(lua_State *L)
{
719
720
MojoArchive *archive = (MojoArchive *) lua_touserdata(L, 1);
const char *path = luaL_checkstring(L, 2);
721
722
int retval = 0;
boolean rc = false;
723
uint16 perms = archive->prevEnum.perms;
724
MojoChecksums sums;
725
726
MojoInput *in = archive->openCurrentEntry(archive);
if (in != NULL)
727
{
728
729
730
731
732
733
734
735
if (!lua_isnil(L, 3))
{
boolean valid = false;
const char *permstr = luaL_checkstring(L, 3);
perms = MojoPlatform_makePermissions(permstr, &valid);
if (!valid)
fatal(_("BUG: '%s' is not a valid permission string"), permstr);
} // if
736
737
rc = MojoInput_toPhysicalFile(in, path, perms, &sums,
writeCallback, L);
738
739
} // if
740
741
742
743
retval += retvalBoolean(L, rc);
if (rc)
retval += retvalChecksums(L, &sums);
return retval;
744
745
746
} // luahook_writefile
747
748
749
750
751
752
753
754
755
756
757
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
758
759
760
761
static int luahook_download(lua_State *L)
{
const char *url = luaL_checkstring(L, 1);
const char *dst = luaL_checkstring(L, 2);
762
763
764
int retval = 0;
boolean rc = false;
MojoChecksums sums;
765
766
MojoInput *in = MojoInput_fromURL(url);
if (in != NULL)
767
768
769
770
771
772
773
774
775
776
{
// !!! FIXME: Unix-specific permissions thing here.
rc = MojoInput_toPhysicalFile(in, dst, 0644, &sums,
writeCallback, L);
} // if
retval += retvalBoolean(L, rc);
if (rc)
retval += retvalChecksums(L, &sums);
return retval;
777
778
779
} // luahook_download
780
781
782
static int luahook_archive_fromdir(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
783
return retvalLightUserData(L, MojoArchive_newFromDirectory(path));
784
785
786
} // luahook_archive_fromdir
787
788
789
790
791
792
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)
793
archive = MojoArchive_newFromInput(io, path);
794
return retvalLightUserData(L, archive);
795
796
797
798
799
800
801
802
803
} // 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)
804
archive = MojoArchive_newFromInput(io, ar->prevEnum.filename);
805
return retvalLightUserData(L, archive);
806
807
808
} // luahook_archive_fromentry
809
810
811
static int luahook_archive_enumerate(lua_State *L)
{
MojoArchive *archive = (MojoArchive *) lua_touserdata(L, 1);
812
return retvalBoolean(L, archive->enumerate(archive));
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
} // 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");
837
set_number(L, (lua_Number) entinfo->filesize, "filesize");
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
set_string(L, typestr, "type");
} // else
return 1;
} // luahook_archive_enumnext
static int luahook_archive_close(lua_State *L)
{
MojoArchive *archive = (MojoArchive *) lua_touserdata(L, 1);
archive->close(archive);
return 0;
} // luahook_archive_close
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
static int luahook_platform_unlink(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
return retvalBoolean(L, MojoPlatform_unlink(path));
} // luahook_platform_unlink
static int luahook_platform_exists(lua_State *L)
{
const char *dir = luaL_checkstring(L, 1);
const char *fname = lua_tostring(L, 2); // can be nil.
return retvalBoolean(L, MojoPlatform_exists(dir, fname));
} // luahook_platform_exists
868
869
870
871
872
873
874
875
876
877
878
static int luahook_platform_writable(lua_State *L)
{
const char *fname = luaL_checkstring(L, 1);
return retvalBoolean(L, MojoPlatform_writable(fname));
} // luahook_platform_writable
static int luahook_platform_isdir(lua_State *L)
{
const char *dir = luaL_checkstring(L, 1);
return retvalBoolean(L, MojoPlatform_isdir(dir));
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
} // luahook_platform_isdir
static int luahook_platform_issymlink(lua_State *L)
{
const char *fname = luaL_checkstring(L, 1);
return retvalBoolean(L, MojoPlatform_issymlink(fname));
} // luahook_platform_issymlink
static int luahook_platform_isfile(lua_State *L)
{
const char *fname = luaL_checkstring(L, 1);
return retvalBoolean(L, MojoPlatform_isfile(fname));
} // luahook_platform_isfile
894
895
896
897
898
899
900
901
902
903
904
905
static int luahook_platform_symlink(lua_State *L)
{
const char *src = luaL_checkstring(L, 1);
const char *dst = luaL_checkstring(L, 2);
return retvalBoolean(L, MojoPlatform_symlink(src, dst));
} // luahook_platform_symlink
static int luahook_platform_mkdir(lua_State *L)
{
906
const int argc = lua_gettop(L);
907
const char *dir = luaL_checkstring(L, 1);
908
uint16 perms = 0;
909
if ( (argc < 2) || (lua_isnil(L, 2)) )
910
911
912
913
914
915
916
917
918
919
perms = MojoPlatform_defaultDirPerms();
else
{
boolean valid = false;
const char *permstr = luaL_checkstring(L, 2);
perms = MojoPlatform_makePermissions(permstr, &valid);
if (!valid)
fatal(_("BUG: '%s' is not a valid permission string"), permstr);
} // if
return retvalBoolean(L, MojoPlatform_mkdir(dir, perms));
920
921
922
923
924
925
926
927
928
929
930
931
932
933
} // luahook_platform_mkdir
static int luahook_movefile(lua_State *L)
{
boolean retval = false;
const char *src = luaL_checkstring(L, 1);
const char *dst = luaL_checkstring(L, 2);
retval = MojoPlatform_rename(src, dst);
if (!retval)
{
MojoInput *in = MojoInput_newFromFile(src);
if (in != NULL)
{
934
935
uint16 perms = 0;
MojoPlatform_perms(src, &perms);
936
retval = MojoInput_toPhysicalFile(in, dst, perms, NULL, NULL, NULL);
937
938
939
940
941
942
943
944
945
946
947
948
949
if (retval)
{
retval = MojoPlatform_unlink(src);
if (!retval)
MojoPlatform_unlink(dst); // oh well.
} // if
} // if
} // if
return retvalBoolean(L, retval);
} // luahook_movefile
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
static void prepareSplash(MojoGuiSplash *splash, const char *fname)
{
MojoInput *io = NULL;
int64 len = 0;
memset(splash, '\0', sizeof (*splash));
if (fname == NULL)
return;
io = MojoInput_newFromArchivePath(GBaseArchive, fname);
if (io == NULL)
return;
len = io->length(io);
if ((len > 0) && (len < 0xFFFFFFFF))
{
const uint32 size = (uint32) len;
uint8 *data = (uint8 *) xmalloc(size);
if (io->read(io, data, size) == len)
{
splash->rgba = decodeImage(data, size, &splash->w, &splash->h);
if (splash->rgba != NULL)
splash->position = MOJOGUI_SPLASH_TOP; // !!! FIXME: others?
} // if
free(data);
} // if
io->close(io);
} // prepareSplash
982
static int luahook_gui_start(lua_State *L)
983
984
{
const char *title = luaL_checkstring(L, 1);
985
986
987
988
989
990
991
992
993
994
const char *splashfname = lua_tostring(L, 2);
boolean rc = false;
MojoGuiSplash splash;
prepareSplash(&splash, splashfname);
rc = GGui->start(title, &splash);
if (splash.rgba != NULL)
free((void *) splash.rgba);
return retvalBoolean(L, rc);
995
} // luahook_gui_start
996
997
998
static const uint8 *loadFile(const char *fname, size_t *len)
999
{
1000
uint8 *retval = NULL;