Skip to content

Latest commit

 

History

History
384 lines (315 loc) · 11.3 KB

lua_glue.c

File metadata and controls

384 lines (315 loc) · 11.3 KB
 
Dec 2, 2006
Dec 2, 2006
1
#include "universal.h"
Dec 9, 2006
Dec 9, 2006
2
#include "lua_glue.h"
Dec 3, 2006
Dec 3, 2006
3
#include "platform.h"
Dec 2, 2006
Dec 2, 2006
4
5
6
7
#include "fileio.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
Dec 6, 2006
Dec 6, 2006
8
#include "gui.h"
Dec 2, 2006
Dec 2, 2006
9
10
11
12
13
#define MOJOSETUP_NAMESPACE "MojoSetup"
static lua_State *luaState = NULL;
Dec 9, 2006
Dec 9, 2006
14
15
16
17
18
19
20
21
22
23
24
25
// 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
26
27
28
29
// 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
30
char *retval = (char *) scratchbuf_128k;
Dec 2, 2006
Dec 2, 2006
31
32
33
34
35
36
37
38
39
40
41
42
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
Dec 3, 2006
Dec 3, 2006
43
44
boolean MojoLua_runFile(const char *basefname)
{
Dec 9, 2006
Dec 9, 2006
45
46
MojoArchive *ar = GBaseArchive; // in case we want to generalize later.
const MojoArchiveEntryInfo *entinfo;
Dec 3, 2006
Dec 3, 2006
47
boolean retval = false;
Dec 9, 2006
Dec 9, 2006
48
49
char clua[128]; // compiled filename.
char ulua[128]; // uncompiled filename.
Dec 3, 2006
Dec 3, 2006
50
51
52
int rc = 0;
MojoInput *io = NULL;
Dec 9, 2006
Dec 9, 2006
53
54
if (snprintf(clua, sizeof (clua), "%s.luac", basefname) >= sizeof (clua))
return false;
Dec 3, 2006
Dec 3, 2006
55
Dec 9, 2006
Dec 9, 2006
56
57
58
59
if (snprintf(ulua, sizeof (ulua), "%s.lua", basefname) >= sizeof (ulua))
return false;
if (ar->enumerate(ar, "lua"))
Dec 3, 2006
Dec 3, 2006
60
{
Dec 9, 2006
Dec 9, 2006
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
while ((entinfo = ar->enumNext(ar)) != NULL)
{
boolean match = (strcmp(entinfo->filename, clua) == 0);
#if !DISABLE_LUA_PARSER
if (!match)
match = (strcmp(entinfo->filename, ulua) == 0);
#endif
if (match)
{
if (entinfo->type == MOJOARCHIVE_ENTRY_FILE)
io = ar->openCurrentEntry(ar);
break;
} // if
} // while
Dec 3, 2006
Dec 3, 2006
76
77
78
79
} // if
if (io != NULL)
{
Dec 9, 2006
Dec 9, 2006
80
rc = lua_load(luaState, MojoLua_reader, io, entinfo->filename);
Dec 3, 2006
Dec 3, 2006
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
io->close(io);
if (rc != 0)
lua_error(luaState);
else
{
// !!! FIXME: use pcall instead so we can get error backtraces and localize.
// Call new chunk on top of the stack (lua_call will pop it off).
lua_call(luaState, 0, 0); // return values are dumped.
retval = true; // if this didn't panic, we succeeded.
} // if
} // if
return retval;
} // MojoLua_runFile
Dec 3, 2006
Dec 3, 2006
98
99
100
void MojoLua_collectGarbage(void)
{
lua_State *L = luaState;
Dec 4, 2006
Dec 4, 2006
101
uint32 ticks = 0;
Dec 3, 2006
Dec 3, 2006
102
103
104
105
106
107
int pre = 0;
int post = 0;
STUBBED("logging!");
pre = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
printf("Collecting garbage (currently using %d bytes).\n", pre);
Dec 4, 2006
Dec 4, 2006
108
ticks = MojoPlatform_ticks();
Dec 3, 2006
Dec 3, 2006
109
lua_gc (L, LUA_GCCOLLECT, 0);
Dec 4, 2006
Dec 4, 2006
110
111
ticks = MojoPlatform_ticks() - ticks;
printf("Collection finished (took %d milliseconds).\n", (int) ticks);
Dec 3, 2006
Dec 3, 2006
112
113
114
115
116
post = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
printf("Now using %d bytes (%d bytes savings).\n", post, pre - post);
} // MojoLua_collectGarbage
Dec 2, 2006
Dec 2, 2006
117
118
119
120
121
// 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
122
if (luaState != NULL) // No translations before Lua is initialized.
Dec 2, 2006
Dec 2, 2006
123
{
Dec 6, 2006
Dec 6, 2006
124
if (lua_checkstack(luaState, 3))
Dec 2, 2006
Dec 2, 2006
125
{
Dec 6, 2006
Dec 6, 2006
126
127
128
int popcount = 0;
lua_getglobal(luaState, MOJOSETUP_NAMESPACE); popcount++;
if (lua_istable(luaState, -1)) // namespace is sane?
Dec 2, 2006
Dec 2, 2006
129
{
Dec 6, 2006
Dec 6, 2006
130
131
lua_getfield(luaState, -1, "translations"); popcount++;
if (lua_istable(luaState, -1)) // translation table is sane?
Dec 4, 2006
Dec 4, 2006
132
{
Dec 6, 2006
Dec 6, 2006
133
134
135
136
137
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
138
139
140
char *dst = (char *) scratchbuf_128k;
xstrncpy(dst, tr, sizeof(scratchbuf_128k));
retval = dst;
Dec 6, 2006
Dec 6, 2006
141
} // if
Dec 4, 2006
Dec 4, 2006
142
} // if
Dec 2, 2006
Dec 2, 2006
143
} // if
Dec 6, 2006
Dec 6, 2006
144
lua_pop(luaState, popcount); // remove our stack salsa.
Dec 2, 2006
Dec 2, 2006
145
146
147
148
149
150
151
152
153
154
} // if
} // if
return retval;
} // translate
// Hook into our error handling in case Lua throws a runtime error.
static int MojoLua_panic(lua_State *L)
{
Dec 3, 2006
Dec 3, 2006
155
156
const char *errstr = lua_tostring(L, 1);
if (errstr == NULL)
Dec 9, 2006
Dec 9, 2006
157
errstr = _("Unknown error");
Dec 2, 2006
Dec 2, 2006
158
159
160
161
return fatal(errstr); // doesn't actually return.
} // MojoLua_panic
Dec 6, 2006
Dec 6, 2006
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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.")
// Doesn't actually return.
static int luahook_fatal(lua_State *L)
{
const char *err = luaL_checkstring(L, 1);
fatal(err);
return 0;
} // luahook_runfile
Dec 3, 2006
Dec 3, 2006
176
177
178
179
180
181
182
183
184
185
// 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);
lua_pushboolean(L, MojoLua_runFile(fname));
return 1;
} // luahook_runfile
Dec 4, 2006
Dec 4, 2006
186
187
188
189
190
191
192
193
194
// Lua interface to translate().
static int luahook_translate(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
lua_pushstring(L, translate(str));
return 1;
} // luahook_translate
Dec 5, 2006
Dec 5, 2006
195
196
197
198
199
200
201
static int luahook_ticks(lua_State *L)
{
lua_pushnumber(L, MojoPlatform_ticks());
return 1;
} // luahook_ticks
Dec 6, 2006
Dec 6, 2006
202
203
204
205
206
207
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
208
GGui->msgbox(title, text);
Dec 6, 2006
Dec 6, 2006
209
210
211
212
213
214
215
216
217
218
219
220
} // 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);
Dec 7, 2006
Dec 7, 2006
221
rc = GGui->promptyn(title, text);
Dec 6, 2006
Dec 6, 2006
222
223
224
225
226
227
228
} // if
lua_pushboolean(L, rc);
return 1;
} // luahook_msgbox
Dec 2, 2006
Dec 2, 2006
229
230
231
232
233
234
235
236
// Sets t[sym]=f, where t is on the top of the Lua stack.
static inline void set_cfunc(lua_State *L, lua_CFunction f, const char *sym)
{
lua_pushcfunction(luaState, f);
lua_setfield(luaState, -2, sym);
} // set_cfunc
Dec 3, 2006
Dec 3, 2006
237
238
239
240
241
242
243
// Sets t[sym]=f, where t is on the top of the Lua stack.
static inline void set_string(lua_State *L, const char *str, const char *sym)
{
lua_pushstring(luaState, str);
lua_setfield(luaState, -2, sym);
} // set_string
Dec 9, 2006
Dec 9, 2006
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
static inline void set_string_array(lua_State *L, int argc, const char **argv,
const char *sym)
{
int i;
lua_newtable(luaState);
for (i = 0; i < argc; i++)
{
lua_pushinteger(luaState, i+1); // lua is option base 1!
lua_pushstring(luaState, argv[i]);
lua_settable(luaState, -3);
} // for
lua_setfield(luaState, -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
Dec 3, 2006
Dec 3, 2006
274
Dec 2, 2006
Dec 2, 2006
275
276
boolean MojoLua_initLua(void)
{
Dec 3, 2006
Dec 3, 2006
277
char locale[16];
Dec 4, 2006
Dec 4, 2006
278
279
char ostype[64];
char osversion[64];
Dec 2, 2006
Dec 2, 2006
280
Dec 3, 2006
Dec 3, 2006
281
282
if (!MojoPlatform_locale(locale, sizeof (locale)))
xstrncpy(locale, "???", sizeof (locale));
Dec 4, 2006
Dec 4, 2006
283
284
285
286
287
288
if (!MojoPlatform_osType(ostype, sizeof (ostype)))
xstrncpy(ostype, "???", sizeof (ostype));
if (!MojoPlatform_osVersion(osversion, sizeof (osversion)))
xstrncpy(osversion, "???", sizeof (osversion));
assert(luaState == NULL);
Dec 3, 2006
Dec 3, 2006
289
Dec 9, 2006
Dec 9, 2006
290
luaState = lua_newstate(MojoLua_alloc, NULL);
Dec 2, 2006
Dec 2, 2006
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
if (luaState == NULL)
return false;
lua_atpanic(luaState, MojoLua_panic);
if (!lua_checkstack(luaState, 20)) // Just in case.
{
lua_close(luaState);
luaState = NULL;
return false;
} // if
luaL_openlibs(luaState);
// Build MojoSetup namespace for Lua to access and fill in C bridges...
lua_newtable(luaState);
Dec 3, 2006
Dec 3, 2006
307
// Set up initial C functions, etc we want to expose to Lua code...
Dec 3, 2006
Dec 3, 2006
308
set_cfunc(luaState, luahook_runfile, "runfile");
Dec 4, 2006
Dec 4, 2006
309
set_cfunc(luaState, luahook_translate, "translate");
Dec 5, 2006
Dec 5, 2006
310
set_cfunc(luaState, luahook_ticks, "ticks");
Dec 6, 2006
Dec 6, 2006
311
312
313
set_cfunc(luaState, luahook_fatal, "fatal");
set_cfunc(luaState, luahook_msgbox, "msgbox");
set_cfunc(luaState, luahook_promptyn, "promptyn");
Dec 3, 2006
Dec 3, 2006
314
set_string(luaState, locale, "locale");
Dec 4, 2006
Dec 4, 2006
315
316
317
318
set_string(luaState, PLATFORM_NAME, "platform");
set_string(luaState, PLATFORM_ARCH, "arch");
set_string(luaState, ostype, "ostype");
set_string(luaState, osversion, "osversion");
Dec 7, 2006
Dec 7, 2006
319
set_string(luaState, GBuildVer, "buildver");
Dec 9, 2006
Dec 9, 2006
320
321
set_string(luaState, GLuaLicense, "lualicense");
set_string_array(luaState, GArgc, GArgv, "commandLine");
Dec 6, 2006
Dec 6, 2006
322
lua_setglobal(luaState, MOJOSETUP_NAMESPACE);
Dec 2, 2006
Dec 2, 2006
323
Dec 3, 2006
Dec 3, 2006
324
// Set up localization table, if possible.
Dec 3, 2006
Dec 3, 2006
325
MojoLua_runFile("localization");
Dec 3, 2006
Dec 3, 2006
326
Dec 3, 2006
Dec 3, 2006
327
328
329
// Transfer control to Lua to setup some APIs and state...
if (!MojoLua_runFile("mojosetup_init"))
return false;
Dec 2, 2006
Dec 2, 2006
330
Dec 3, 2006
Dec 3, 2006
331
// ...and run the installer-specific config file.
Dec 3, 2006
Dec 3, 2006
332
333
334
if (!MojoLua_runFile("config"))
return false;
Dec 3, 2006
Dec 3, 2006
335
336
337
338
339
340
341
342
343
344
345
346
// We don't need the MojoSetup.schema namespace anymore. Make it
// eligible for garbage collection.
lua_getglobal(luaState, MOJOSETUP_NAMESPACE);
if (lua_istable(luaState, -1))
{
lua_pushnil(luaState);
lua_setfield(luaState, -2, "schema");
} // if
lua_pop(luaState, 1);
MojoLua_collectGarbage(); // get rid of old init crap we don't need.
Dec 2, 2006
Dec 2, 2006
347
348
349
350
351
352
353
354
355
356
357
358
359
return true;
} // MojoLua_initLua
void MojoLua_deinitLua(void)
{
if (luaState != NULL)
{
lua_close(luaState);
luaState = NULL;
} // if
} // MojoLua_deinitLua
Dec 9, 2006
Dec 9, 2006
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const char *GLuaLicense =
"Copyright (C) 1994-2006 Lua.org, PUC-Rio.\n"
"\n"
"Permission is hereby granted, free of charge, to any person obtaining a copy\n"
"of this software and associated documentation files (the \"Software\"), to deal\n"
"in the Software without restriction, including without limitation the rights\n"
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
"copies of the Software, and to permit persons to whom the Software is\n"
"furnished to do so, subject to the following conditions:\n"
"\n"
"The above copyright notice and this permission notice shall be included in\n"
"all copies or substantial portions of the Software.\n"
"\n"
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n"
"THE SOFTWARE.\n"
"\n";
Dec 2, 2006
Dec 2, 2006
383
// end of lua_glue.c ...