forked from iamgreaser/iceball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.c
344 lines (290 loc) · 9.74 KB
/
lua.c
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
/*
This file is part of Iceball.
Iceball is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Iceball is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Iceball. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
struct icelua_entry {
int (*fn) (lua_State *L);
char *name;
};
lua_State *lstate_client = NULL;
lua_State *lstate_server = NULL;
// helper functions
int icelua_assert_stack(lua_State *L, int smin, int smax)
{
int top = lua_gettop(L);
if(smin != -1 && top < smin)
return luaL_error(L, "expected at least %d arguments, got %d", smin, top);
if(smax != -1 && top > smax)
return luaL_error(L, "expected at most %d arguments, got %d", smax, top);
return top;
}
int icelua_force_get_integer(lua_State *L, int table, char *name)
{
lua_getfield(L, table, name);
if(!lua_isnumber(L, -1))
return luaL_error(L, "expected integer for \"%s\", got something else", name);
int ret = lua_tointeger(L, -1);
lua_pop(L, 1);
return ret;
}
#include "lua_fetch.h"
#include "lua_base.h"
#include "lua_camera.h"
#include "lua_image.h"
#include "lua_input.h"
#include "lua_json.h"
#include "lua_map.h"
#include "lua_model.h"
#include "lua_net.h"
// common functions
// client functions
// server functions
struct icelua_entry icelua_client[] = {
{icelua_fn_client_mouse_lock_set, "mouse_lock_set"},
{icelua_fn_client_mouse_visible_set, "mouse_visible_set"},
{icelua_fn_client_map_fog_get, "map_fog_get"},
{icelua_fn_client_map_fog_set, "map_fog_set"},
{icelua_fn_client_camera_point, "camera_point"},
{icelua_fn_client_camera_move_local, "camera_move_local"},
{icelua_fn_client_camera_move_global, "camera_move_global"},
{icelua_fn_client_camera_move_to, "camera_move_to"},
{icelua_fn_client_camera_get_pos, "camera_get_pos"},
{icelua_fn_client_camera_get_forward, "camera_get_forward"},
{icelua_fn_client_screen_get_dims, "screen_get_dims"},
{icelua_fn_client_model_render_bone_global, "model_render_bone_global"},
{icelua_fn_client_model_render_bone_local, "model_render_bone_local"},
{icelua_fn_client_img_blit, "img_blit"},
{NULL, NULL}
};
struct icelua_entry icelua_server[] = {
{NULL, NULL}
};
struct icelua_entry icelua_common[] = {
{icelua_fn_common_fetch_start, "fetch_start"},
{icelua_fn_common_fetch_poll, "fetch_poll"},
{icelua_fn_common_fetch_block, "fetch_block"},
{icelua_fn_common_map_load, "map_load"},
{icelua_fn_common_map_new, "map_new"},
{icelua_fn_common_map_free, "map_free"},
{icelua_fn_common_map_get, "map_get"},
{icelua_fn_common_map_set, "map_set"},
{icelua_fn_common_map_save, "map_save"},
{icelua_fn_common_map_get_dims, "map_get_dims"},
{icelua_fn_common_map_pillar_get, "map_pillar_get"},
{icelua_fn_common_map_pillar_set, "map_pillar_set"},
{icelua_fn_common_model_new, "model_new"},
{icelua_fn_common_model_load_pmf, "model_load_pmf"},
{icelua_fn_common_model_save_pmf, "model_save_pmf"},
{icelua_fn_common_model_free, "model_free"},
{icelua_fn_common_model_len, "model_len"},
{icelua_fn_common_model_bone_new, "model_bone_new"},
{icelua_fn_common_model_bone_free, "model_bone_free"},
{icelua_fn_common_model_bone_get, "model_bone_get"},
{icelua_fn_common_model_bone_set, "model_bone_set"},
{icelua_fn_common_model_bone_find, "model_bone_find"},
{icelua_fn_common_img_load, "img_load"},
{icelua_fn_common_img_new, "img_new"},
{icelua_fn_common_img_pixel_set, "img_pixel_set"},
{icelua_fn_common_img_free, "img_free"},
{icelua_fn_common_img_get_dims, "img_get_dims"},
{icelua_fn_common_json_parse, "json_parse"},
{icelua_fn_common_json_load, "json_load"},
{icelua_fn_common_net_pack, "net_pack"},
{icelua_fn_common_net_unpack, "net_unpack"},
{icelua_fn_common_net_send, "net_send"},
{icelua_fn_common_net_recv, "net_recv"},
{NULL, NULL}
};
struct icelua_entry icelua_common_client[] = {
{NULL, NULL}
};
struct icelua_entry icelua_common_server[] = {
{NULL, NULL}
};
void icelua_loadfuncs(lua_State *L, char *table, struct icelua_entry *fnlist)
{
if(L == NULL)
return;
lua_getglobal(L, table);
while(fnlist->fn != NULL)
{
lua_pushcfunction(L, fnlist->fn);
lua_setfield (L, -2, fnlist->name);
fnlist++;
}
lua_pop(L, 1);
}
void icelua_loadbasefuncs(lua_State *L)
{
if(L == NULL)
return;
// load base library
// TODO: whitelist the functions by spawning a new environment.
// this is harder than it sounds.
lua_pushcfunction(L, luaopen_base);
lua_call(L, 0, 0);
// here's the other three
lua_pushcfunction(L, luaopen_string);
lua_call(L, 0, 0);
lua_pushcfunction(L, luaopen_math);
lua_call(L, 0, 0);
lua_pushcfunction(L, luaopen_table);
lua_call(L, 0, 0);
// overwrite dofile/loadfile.
lua_pushcfunction(L, icelua_fn_base_loadfile);
lua_setglobal(L, "loadfile");
lua_pushcfunction(L, icelua_fn_base_dofile);
lua_setglobal(L, "dofile");
}
int icelua_init(void)
{
int i, argct;
// create states
lstate_client = (boot_mode & 1 ? luaL_newstate() : NULL);
lstate_server = (boot_mode & 2 ? luaL_newstate() : NULL);
// create tables
if(lstate_client != NULL)
{
lua_newtable(lstate_client);
lua_setglobal(lstate_client, "client");
lua_newtable(lstate_client);
lua_setglobal(lstate_client, "common");
lua_pushvalue(lstate_client, LUA_GLOBALSINDEX);
lua_setglobal(lstate_client, "_G");
}
if(lstate_server != NULL)
{
lua_newtable(lstate_server);
lua_setglobal(lstate_server, "server");
lua_newtable(lstate_server);
lua_setglobal(lstate_server, "common");
lua_pushvalue(lstate_server, LUA_GLOBALSINDEX);
lua_setglobal(lstate_server, "_G");
}
// load stuff into them
icelua_loadfuncs(lstate_client, "client", icelua_client);
icelua_loadfuncs(lstate_server, "server", icelua_server);
icelua_loadfuncs(lstate_client, "client", icelua_common);
icelua_loadfuncs(lstate_server, "server", icelua_common);
icelua_loadfuncs(lstate_client, "common", icelua_common);
icelua_loadfuncs(lstate_server, "common", icelua_common);
icelua_loadfuncs(lstate_client, "client", icelua_common_client);
icelua_loadfuncs(lstate_server, "server", icelua_common_server);
icelua_loadfuncs(lstate_client, "common", icelua_common_client);
icelua_loadfuncs(lstate_server, "common", icelua_common_server);
// load some lua base libraries
icelua_loadbasefuncs(lstate_client);
icelua_loadbasefuncs(lstate_server);
// shove some pathnames in
{
lua_getglobal(lstate_client, "common");
lua_getglobal(lstate_client, "client");
lua_pushstring(lstate_client, mod_basedir+4);
lua_setfield(lstate_client, -2, "base_dir");
lua_pop(lstate_client, 1);
lua_pushstring(lstate_client, mod_basedir+4);
lua_setfield(lstate_client, -2, "base_dir");
lua_pop(lstate_client, 1);
lua_getglobal(lstate_server, "common");
lua_getglobal(lstate_server, "server");
lua_pushstring(lstate_server, mod_basedir+4);
lua_setfield(lstate_server, -2, "base_dir");
lua_pop(lstate_server, 1);
lua_pushstring(lstate_server, mod_basedir+4);
lua_setfield(lstate_server, -2, "base_dir");
lua_pop(lstate_server, 1);
}
/*
NOTE:
to call stuff, use lua_pcall.
DO NOT use lua_call! if it fails, it will TERMINATE the program!
*/
// quick test
// TODO: set up a "convert/filter file path" function
// TODO: split the client/server inits
char xpath[128];
snprintf(xpath, 128, "%s/main_server.lua", mod_basedir);
if((lstate_server != NULL) && luaL_loadfile(lstate_server, xpath) != 0)
{
printf("ERROR loading server Lua: %s\n", lua_tostring(lstate_server, -1));
return 1;
}
argct = (main_largstart == -1 || (main_largstart >= main_argc)
? 0
: main_argc - main_largstart);
if(lstate_server != NULL)
{
for(i = 0; i < argct; i++)
lua_pushstring(lstate_server, main_argv[i+main_largstart]);
if(lua_pcall(lstate_server, argct, 0, 0) != 0)
{
printf("ERROR running server Lua: %s\n", lua_tostring(lstate_server, -1));
lua_pop(lstate_server, 1);
return 1;
}
}
if(lstate_client != NULL)
{
snprintf(xpath, 128, "%s/main_client.lua", mod_basedir);
lua_pushcfunction(lstate_client, icelua_fn_common_fetch_block);
lua_pushstring(lstate_client, "lua");
lua_pushstring(lstate_client, xpath);
printf("Now loading client; please wait!\n");
if(lua_pcall(lstate_client, 2, 1, 0) != 0)
{
printf("ERROR fetching client Lua: %s\n", lua_tostring(lstate_client, -1));
lua_pop(lstate_client, 1);
return 1;
}
printf("Client loaded! Initialising...\n");
for(i = 0; i < argct; i++)
lua_pushstring(lstate_client, main_argv[i+main_largstart]);
if(lua_pcall(lstate_client, argct, 0, 0) != 0)
{
printf("ERROR running client Lua: %s\n", lua_tostring(lstate_client, -1));
lua_pop(lstate_client, 1);
return 1;
}
printf("Done!\n");
boot_mode |= 4;
}
// dispatch initial connect
if(lstate_server != NULL && lstate_client != NULL)
{
lua_getglobal(lstate_server, "server");
lua_getfield(lstate_server, -1, "hook_connect");
lua_remove(lstate_server, -2);
if(!lua_isnil(lstate_server, -1))
{
lua_pushboolean(lstate_server, 1);
lua_newtable(lstate_server);
lua_pushstring(lstate_server, "local");
lua_setfield(lstate_server, -2, "proto");
lua_pushnil(lstate_server);
lua_setfield(lstate_server, -2, "addr");
if(lua_pcall(lstate_server, 2, 0, 0) != 0)
{
printf("ERROR running server Lua (hook_connect): %s\n", lua_tostring(lstate_client, -1));
lua_pop(lstate_client, 2);
return 1;
}
} else {
lua_pop(lstate_server, 1);
}
}
return 0;
}
void icelua_deinit(void)
{
// TODO!
}