mirror of
https://github.com/coop-deluxe/sm64coopdx.git
synced 2024-11-25 21:45:12 +00:00
Sandboxed Lua scripts
This commit is contained in:
parent
0626c77048
commit
348d1509a1
4 changed files with 1149 additions and 1108 deletions
1106
mods/extended-moveset.lua
Normal file
1106
mods/extended-moveset.lua
Normal file
File diff suppressed because it is too large
Load diff
1108
mods/test.lua
1108
mods/test.lua
File diff suppressed because it is too large
Load diff
|
@ -12,6 +12,7 @@
|
|||
#include "behavior_table.h"
|
||||
|
||||
#ifdef DEBUG
|
||||
#include "pc/lua/smlua.h"
|
||||
|
||||
static u8 warpToLevel = LEVEL_BOB;
|
||||
static u8 warpToArea = 27;
|
||||
|
@ -29,6 +30,7 @@ static u8 warpToArea = 27;
|
|||
#define SCANCODE_7 0x08
|
||||
#define SCANCODE_8 0x09
|
||||
#define SCANCODE_9 0x0A
|
||||
#define SCANCODE_F5 0x3f
|
||||
|
||||
static void debug_breakpoint_here(void) {
|
||||
// create easy breakpoint position for debugging
|
||||
|
@ -130,6 +132,11 @@ static void debug_suicide(void) {
|
|||
gMarioStates[0].hurtCounter = 31;
|
||||
}
|
||||
|
||||
static void debug_reload_lua(void) {
|
||||
smlua_shutdown();
|
||||
smlua_init();
|
||||
}
|
||||
|
||||
static void debug_spawn_object(void) {
|
||||
struct Object* box = spawn_object(gMarioStates[0].marioObj, MODEL_BREAKABLE_BOX_SMALL, bhvBreakableBoxSmall);
|
||||
network_set_sync_id(box);
|
||||
|
@ -149,6 +156,7 @@ void debug_keyboard_on_key_down(int scancode) {
|
|||
case SCANCODE_8: debug_spawn_object(); break;
|
||||
case SCANCODE_9: debug_warp_to(); break;
|
||||
case SCANCODE_0: debug_suicide(); break;
|
||||
case SCANCODE_F5: debug_reload_lua(); break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,12 +5,42 @@ lua_State* gLuaState = NULL;
|
|||
static void smlua_execfile(char* path) {
|
||||
lua_State* L = gLuaState;
|
||||
if (luaL_dofile(L, path) != LUA_OK) {
|
||||
LOG_LUA("LUA: Failed to load lua file.");
|
||||
LOG_LUA("LUA: Failed to load lua file '%s'.", path);
|
||||
puts(lua_tostring(L, lua_gettop(L)));
|
||||
}
|
||||
lua_pop(L, lua_gettop(L));
|
||||
}
|
||||
|
||||
static void smlua_load_script(char* path) {
|
||||
lua_State* L = gLuaState;
|
||||
if (luaL_loadfile(L, path) != LUA_OK) {
|
||||
LOG_LUA("LUA: Failed to load lua script '%s'.", path);
|
||||
puts(lua_tostring(L, lua_gettop(L)));
|
||||
return;
|
||||
}
|
||||
|
||||
lua_newtable(L); // create _ENV tables
|
||||
lua_newtable(L); // create metatable
|
||||
lua_getglobal(L, "_G"); // get global table
|
||||
|
||||
// set global as the metatable
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
// push to registry with path as name (must be unique)
|
||||
lua_setfield(L, LUA_REGISTRYINDEX, path);
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, path);
|
||||
lua_setupvalue(L, 1, 1); // set upvalue (_ENV)
|
||||
|
||||
// run chunks
|
||||
if (lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) {
|
||||
LOG_LUA("LUA: Failed to execute lua script '%s'.", path);
|
||||
puts(lua_tostring(L, lua_gettop(L)));
|
||||
smlua_dump_stack();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void smlua_init_mario_states(void) {
|
||||
lua_State* L = gLuaState;
|
||||
lua_newtable(L);
|
||||
|
@ -46,7 +76,8 @@ void smlua_init(void) {
|
|||
|
||||
smlua_execfile("mods/constants.lua");
|
||||
smlua_init_mario_states();
|
||||
smlua_execfile("mods/test.lua");
|
||||
smlua_load_script("mods/extended-moveset.lua");
|
||||
smlua_load_script("mods/test.lua");
|
||||
}
|
||||
|
||||
void smlua_update(void) {
|
||||
|
|
Loading…
Reference in a new issue