Skip to content

Commit

Permalink
Update runtime so process is defined as variable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wins1ey committed Jun 5, 2024
1 parent a42a53a commit 931f4ee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/auto-splitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string.h>
#include <unistd.h>

#include <lualib.h>
#include <luajit.h>

#include "auto-splitter.h"
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdbool.h>
#include <stdint.h>

#include <lualib.h>
#include <luajit.h>

struct game_process {
Expand Down

0 comments on commit 931f4ee

Please sign in to comment.