Bring back some dev commands and add autoexec.lua

Make an autoexec.lua file in the same directory as your exe folder and the game will execute it if DEVELOPMENT=1
This commit is contained in:
Agent X 2023-12-06 19:20:58 -05:00
parent 6e14336bf1
commit bdccde902e
3 changed files with 41 additions and 3 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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);