Skip to content

Latest commit

 

History

History
303 lines (245 loc) · 8.61 KB

lua_glue.c

File metadata and controls

303 lines (245 loc) · 8.61 KB
 
Dec 2, 2006
Dec 2, 2006
1
#include "universal.h"
Dec 3, 2006
Dec 3, 2006
2
#include "platform.h"
Dec 2, 2006
Dec 2, 2006
3
4
5
6
#include "fileio.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
Dec 6, 2006
Dec 6, 2006
7
#include "gui.h"
Dec 2, 2006
Dec 2, 2006
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#define MOJOSETUP_NAMESPACE "MojoSetup"
static lua_State *luaState = NULL;
// 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;
const char *retval = (const char *) scratchbuf_128k;
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
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
boolean MojoLua_runFile(const char *basefname)
{
boolean retval = false;
size_t alloclen = strlen(basefname) + 16;
char *fname = (char *) xmalloc(alloclen);
int rc = 0;
MojoInput *io = NULL;
// !!! FIXME: change this API to accept multiple filenames to search for,
// !!! FIXME: since two full enumerations to discover the filename is
// !!! FIXME: missing is just wasteful.
STUBBED("See FIXME above.");
snprintf(fname, alloclen, "lua/%s.luac", basefname);
io = MojoInput_newFromArchivePath(GBaseArchive, fname);
#if !DISABLE_LUA_PARSER
if (io == NULL)
{
snprintf(fname, alloclen, "lua/%s.lua", basefname);
io = MojoInput_newFromArchivePath(GBaseArchive, fname);
} // if
#endif
if (io != NULL)
{
rc = lua_load(luaState, MojoLua_reader, io, fname);
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
free(fname);
return retval;
} // MojoLua_runFile
Dec 3, 2006
Dec 3, 2006
74
75
76
void MojoLua_collectGarbage(void)
{
lua_State *L = luaState;
Dec 4, 2006
Dec 4, 2006
77
uint32 ticks = 0;
Dec 3, 2006
Dec 3, 2006
78
79
80
81
82
83
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
84
ticks = MojoPlatform_ticks();
Dec 3, 2006
Dec 3, 2006
85
lua_gc (L, LUA_GCCOLLECT, 0);
Dec 4, 2006
Dec 4, 2006
86
87
ticks = MojoPlatform_ticks() - ticks;
printf("Collection finished (took %d milliseconds).\n", (int) ticks);
Dec 3, 2006
Dec 3, 2006
88
89
90
91
92
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
93
94
95
96
97
// 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
98
if (luaState != NULL) // No translations before Lua is initialized.
Dec 2, 2006
Dec 2, 2006
99
{
Dec 6, 2006
Dec 6, 2006
100
if (lua_checkstack(luaState, 3))
Dec 2, 2006
Dec 2, 2006
101
{
Dec 6, 2006
Dec 6, 2006
102
103
104
int popcount = 0;
lua_getglobal(luaState, MOJOSETUP_NAMESPACE); popcount++;
if (lua_istable(luaState, -1)) // namespace is sane?
Dec 2, 2006
Dec 2, 2006
105
{
Dec 6, 2006
Dec 6, 2006
106
107
lua_getfield(luaState, -1, "translations"); popcount++;
if (lua_istable(luaState, -1)) // translation table is sane?
Dec 4, 2006
Dec 4, 2006
108
{
Dec 6, 2006
Dec 6, 2006
109
110
111
112
113
114
115
116
const char *tr = NULL;
lua_getfield(luaState, -1, str); popcount++;
tr = lua_tostring(luaState, -1);
if (tr != NULL) // translated for this locale?
{
xstrncpy(scratchbuf_128k, tr, sizeof(scratchbuf_128k));
retval = scratchbuf_128k;
} // if
Dec 4, 2006
Dec 4, 2006
117
} // if
Dec 2, 2006
Dec 2, 2006
118
} // if
Dec 6, 2006
Dec 6, 2006
119
lua_pop(luaState, popcount); // remove our stack salsa.
Dec 2, 2006
Dec 2, 2006
120
121
122
123
124
125
126
127
128
129
} // 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
130
131
132
const char *errstr = lua_tostring(L, 1);
if (errstr == NULL)
errstr = _("Unknown Lua error");
Dec 2, 2006
Dec 2, 2006
133
134
135
136
return fatal(errstr); // doesn't actually return.
} // MojoLua_panic
Dec 6, 2006
Dec 6, 2006
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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
151
152
153
154
155
156
157
158
159
160
// 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
161
162
163
164
165
166
167
168
169
// 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
170
171
172
173
174
175
176
static int luahook_ticks(lua_State *L)
{
lua_pushnumber(L, MojoPlatform_ticks());
return 1;
} // luahook_ticks
Dec 6, 2006
Dec 6, 2006
177
178
179
180
181
182
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
183
GGui->msgbox(title, text);
Dec 6, 2006
Dec 6, 2006
184
185
186
187
188
189
190
191
192
193
194
195
} // 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
196
rc = GGui->promptyn(title, text);
Dec 6, 2006
Dec 6, 2006
197
198
199
200
201
202
203
} // if
lua_pushboolean(L, rc);
return 1;
} // luahook_msgbox
Dec 2, 2006
Dec 2, 2006
204
205
206
207
208
209
210
211
// 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
212
213
214
215
216
217
218
219
// 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 2, 2006
Dec 2, 2006
220
221
boolean MojoLua_initLua(void)
{
Dec 3, 2006
Dec 3, 2006
222
char locale[16];
Dec 4, 2006
Dec 4, 2006
223
224
char ostype[64];
char osversion[64];
Dec 2, 2006
Dec 2, 2006
225
Dec 3, 2006
Dec 3, 2006
226
227
if (!MojoPlatform_locale(locale, sizeof (locale)))
xstrncpy(locale, "???", sizeof (locale));
Dec 4, 2006
Dec 4, 2006
228
229
230
231
232
233
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
234
Dec 2, 2006
Dec 2, 2006
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
luaState = luaL_newstate(); // !!! FIXME: define our own allocator?
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
252
// Set up initial C functions, etc we want to expose to Lua code...
Dec 3, 2006
Dec 3, 2006
253
set_cfunc(luaState, luahook_runfile, "runfile");
Dec 4, 2006
Dec 4, 2006
254
set_cfunc(luaState, luahook_translate, "translate");
Dec 5, 2006
Dec 5, 2006
255
set_cfunc(luaState, luahook_ticks, "ticks");
Dec 6, 2006
Dec 6, 2006
256
257
258
set_cfunc(luaState, luahook_fatal, "fatal");
set_cfunc(luaState, luahook_msgbox, "msgbox");
set_cfunc(luaState, luahook_promptyn, "promptyn");
Dec 3, 2006
Dec 3, 2006
259
set_string(luaState, locale, "locale");
Dec 4, 2006
Dec 4, 2006
260
261
262
263
set_string(luaState, PLATFORM_NAME, "platform");
set_string(luaState, PLATFORM_ARCH, "arch");
set_string(luaState, ostype, "ostype");
set_string(luaState, osversion, "osversion");
Dec 6, 2006
Dec 6, 2006
264
lua_setglobal(luaState, MOJOSETUP_NAMESPACE);
Dec 2, 2006
Dec 2, 2006
265
Dec 3, 2006
Dec 3, 2006
266
// Set up localization table, if possible.
Dec 3, 2006
Dec 3, 2006
267
MojoLua_runFile("localization");
Dec 3, 2006
Dec 3, 2006
268
Dec 3, 2006
Dec 3, 2006
269
270
271
// Transfer control to Lua to setup some APIs and state...
if (!MojoLua_runFile("mojosetup_init"))
return false;
Dec 2, 2006
Dec 2, 2006
272
Dec 3, 2006
Dec 3, 2006
273
// ...and run the installer-specific config file.
Dec 3, 2006
Dec 3, 2006
274
275
276
if (!MojoLua_runFile("config"))
return false;
Dec 3, 2006
Dec 3, 2006
277
278
279
280
281
282
283
284
285
286
287
288
// 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
return true;
} // MojoLua_initLua
void MojoLua_deinitLua(void)
{
if (luaState != NULL)
{
lua_close(luaState);
luaState = NULL;
} // if
} // MojoLua_deinitLua
// end of lua_glue.c ...