From 931f4ee9c89cc750e4c4ccaa615fda83ac6781f0 Mon Sep 17 00:00:00 2001 From: wins1ey Date: Wed, 5 Jun 2024 20:29:21 +0100 Subject: [PATCH] Update runtime so process is defined as variable Define `process = "{game_name}"` in startup instead of calling `process()` at the top of the auto splitter. The auto splitter script will now keep running if the game is closed and will try and find the new game process before continuing, instead of running the entire script again from the start. --- src/auto-splitter.c | 20 +++++++++++++++++--- src/process.c | 2 +- src/process.h | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/auto-splitter.c b/src/auto-splitter.c index 9d43518..baa4c07 100644 --- a/src/auto-splitter.c +++ b/src/auto-splitter.c @@ -262,6 +262,16 @@ void startup(lua_State* L) maps_cache_cycles_value = maps_cache_cycles; } lua_pop(L, 1); // Remove 'mapsCacheCycles' from the stack + + lua_getglobal(L, "process"); + if (lua_isstring(L, -1)) { + process.name = lua_tostring(L, -1); + lua_pop(L, 1); + if (process.name != NULL) { + find_process_id(L); + } + } + lua_pop(L, 1); // Remove 'process' from the stack } bool update(lua_State* L) @@ -381,8 +391,6 @@ void run_auto_splitter() lua_State* L = luaL_newstate(); luaL_openlibs(L); disable_functions(L, disabled_functions); - lua_pushcfunction(L, find_process_id); - lua_setglobal(L, "process"); lua_pushcfunction(L, read_address); lua_setglobal(L, "readAddress"); lua_pushcfunction(L, getPid); @@ -423,6 +431,7 @@ void run_auto_splitter() bool reset_exists = lua_function_exists(L, "reset"); bool on_reset_exists = lua_function_exists(L, "onReset"); bool update_exists = lua_function_exists(L, "update"); + bool init_exists = lua_function_exists(L, "init"); if (startup_exists) { startup(L); @@ -435,8 +444,13 @@ void run_auto_splitter() struct timespec clock_start; clock_gettime(CLOCK_MONOTONIC, &clock_start); - if (!atomic_load(&auto_splitter_enabled) || strcmp(current_file, auto_splitter_file) != 0 || !process_exists() || process.pid == 0) { + if (!atomic_load(&auto_splitter_enabled) || strcmp(current_file, auto_splitter_file) != 0) { break; + } else if (!process_exists() || process.pid == 0) { + find_process_id(L); + if (init_exists) { + call_va(L, "init", ""); + } } run_auto_splitter_cycle( diff --git a/src/process.c b/src/process.c index fc8d7a7..0385da5 100644 --- a/src/process.c +++ b/src/process.c @@ -6,6 +6,7 @@ #include #include +#include #include #include "auto-splitter.h" @@ -104,7 +105,6 @@ void stock_process_id(const char* pid_command) int find_process_id(lua_State* L) { - process.name = lua_tostring(L, 1); char command[256]; printf("\033[2J\033[1;1H"); // Clear the console snprintf(command, sizeof(command), "pgrep \"%.*s\"", (int)strnlen(process.name, 15), process.name); diff --git a/src/process.h b/src/process.h index 27a5fa7..e351ab5 100644 --- a/src/process.h +++ b/src/process.h @@ -5,6 +5,7 @@ #include #include +#include #include struct game_process {