diff --git a/src/dev/chat.c b/src/dev/chat.c index 015fb275..61386c54 100644 --- a/src/dev/chat.c +++ b/src/dev/chat.c @@ -76,7 +76,7 @@ bool exec_dev_chat_command(char* command) { } // Area - s32 area = -1; + s32 area = 1; if (sscanf(paramArea, "%d", &area) <= 0) { char message[256]; snprintf(message, 256, "Invalid [AREA] parameter: %s", paramArea); @@ -85,7 +85,7 @@ bool exec_dev_chat_command(char* command) { } // Act - s32 act = -1; + s32 act = 1; if (sscanf(paramAct, "%d", &act) <= 0) { char message[256]; snprintf(message, 256, "Invalid [ACT] parameter: %s", paramAct); @@ -108,10 +108,32 @@ bool exec_dev_chat_command(char* command) { return true; } + if (strcmp("/lua", command) == 0) { + djui_chat_message_create("Missing parameter: [LUA]"); + return true; + } + + if (str_starts_with("/lua ", command)) { + smlua_exec_str(&command[5]); + return true; + } + + if (strcmp("/luaf", command) == 0) { + djui_chat_message_create("Missing parameter: [FILENAME]"); + return true; + } + + if (str_starts_with("/luaf ", command)) { + smlua_exec_file(&command[6]); + return true; + } + return false; } void dev_display_chat_commands(void) { djui_chat_message_create("/warp [LEVEL] [AREA] [ACT] - Level can be either a numeric value or a shorthand name"); + djui_chat_message_create("/lua [LUA] - Execute Lua code from a string"); + djui_chat_message_create("/luaf [FILENAME] - Execute Lua code from a file"); } #endif \ No newline at end of file diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index 91b22421..06eefc5e 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -46,7 +46,16 @@ int smlua_pcall(lua_State* L, int nargs, int nresults, UNUSED int errfunc) { return rc; } -static void smlua_exec_str(const char* str) { +void smlua_exec_file(const char* path) { + lua_State* L = gLuaState; + if (luaL_dofile(L, path) != LUA_OK) { + LOG_LUA("Failed to load lua file '%s'.", path); + LOG_LUA("%s", smlua_to_string(L, lua_gettop(L))); + } + lua_pop(L, lua_gettop(L)); +} + +void smlua_exec_str(const char* str) { lua_State* L = gLuaState; if (luaL_dostring(L, str) != LUA_OK) { LOG_LUA("Failed to load lua string."); @@ -279,6 +288,11 @@ void smlua_init(void) { gLuaActiveMod = NULL; gLuaLoadingMod = NULL; } + +#ifdef DEVELOPMENT + // autoexec + smlua_exec_file("autoexec.lua"); +#endif } void smlua_update(void) { diff --git a/src/pc/lua/smlua.h b/src/pc/lua/smlua.h index 286fcdcf..0f55695b 100644 --- a/src/pc/lua/smlua.h +++ b/src/pc/lua/smlua.h @@ -41,6 +41,8 @@ extern struct Mod* gLuaLastHookMod; void smlua_mod_error(void); int smlua_error_handler(UNUSED lua_State* L); int smlua_pcall(lua_State* L, int nargs, int nresults, int errfunc); +void smlua_exec_file(const char* path); +void smlua_exec_str(const char* str); void smlua_init(void); void smlua_update(void);