From f54200a7dd4c7b4360c5da44400f500f99cf5436 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 29 May 2025 17:59:33 -0500 Subject: [PATCH] cwd works correctly for when running from a different folder --- scripts/engine.js | 8 ++++- source/prosperon.c | 83 +++++++++++++++++++++++++++++----------------- 2 files changed, 60 insertions(+), 31 deletions(-) diff --git a/scripts/engine.js b/scripts/engine.js index 9dcf10ce..10611d97 100644 --- a/scripts/engine.js +++ b/scripts/engine.js @@ -90,6 +90,11 @@ var js = use_embed('js') var io = use_embed('io') +if (!io.exists('.cell')) { + console_mod.print("No cell directory found. Make one.\n"); + os.exit(1); +} + io.mount("scripts") var RESPATH = 'scripts/resources.js' @@ -724,7 +729,8 @@ function enet_check() //enet_check(); // Finally, run the program -var prog = io.slurp(prosperon.args.program) +var prog = resources.find_script(prosperon.args.program) +prog = io.slurp(prog) var prog_script = `(function ${prosperon.args.program.name()}($_) { ${prog} })` var val = js.eval(prosperon.args.program, prog_script)($_) if (val) diff --git a/source/prosperon.c b/source/prosperon.c index c856e01e..2ceed8d8 100644 --- a/source/prosperon.c +++ b/source/prosperon.c @@ -159,7 +159,25 @@ static void free_zip(void) int prosperon_mount_core(void) { size_t size; - FILE *f = fopen(prosperon, "rb"); + char exe_path[PATH_MAX]; + + // Get the full path of the executable + const char *base_dir = PHYSFS_getBaseDir(); + if (base_dir) { + snprintf(exe_path, sizeof(exe_path), "%s%s", base_dir, PHYSFS_getDirSeparator()); + + // Extract just the executable name from argv[0] + const char *exe_name = strrchr(prosperon, '/'); + if (!exe_name) exe_name = strrchr(prosperon, '\\'); + if (exe_name) exe_name++; else exe_name = prosperon; + + strncat(exe_path, exe_name, sizeof(exe_path) - strlen(exe_path) - 1); + } else { + strncpy(exe_path, prosperon, sizeof(exe_path) - 1); + exe_path[sizeof(exe_path) - 1] = '\0'; + } + + FILE *f = fopen(exe_path, "rb"); if (!f) return perror("fopen"), 0; if (fseek(f, 0, SEEK_END) != 0) return perror("fseek"), fclose(f), 0; size = ftell(f); @@ -779,28 +797,6 @@ static int crank_actor(void *data) return 0; } -static void signal_handler(int sig) -{ - const char *str = NULL; - switch (sig) { - case SIGABRT: str = "SIGABRT"; break; - case SIGFPE: str = "SIGFPE"; break; - case SIGILL: str = "SIGILL"; break; - case SIGINT: str = "SIGINT"; break; - case SIGSEGV: str = "SIGSEGV"; break; - case SIGTERM: str = "SIGTERM"; break; - } - if (!str) return; - - /* Push a quit event to the SDL event queue */ - SDL_Event quit_event; - quit_event.type = SDL_EVENT_QUIT; - SDL_PushEvent(&quit_event); - - SDL_Quit(); - if (sig == SIGTERM || sig == SIGINT) exit(1); -} - static void exit_handler(void) { SDL_SetAtomicInt(&engine_shutdown, 1); @@ -816,6 +812,23 @@ static void exit_handler(void) SDL_WaitThread(runners[i], &status); SDL_Quit(); + exit(0); +} + +static void signal_handler(int sig) +{ + const char *str = NULL; + switch (sig) { + case SIGABRT: str = "SIGABRT"; break; + case SIGFPE: str = "SIGFPE"; break; + case SIGILL: str = "SIGILL"; break; + case SIGINT: str = "SIGINT"; break; + case SIGSEGV: str = "SIGSEGV"; break; + case SIGTERM: str = "SIGTERM"; break; + } + if (!str) return; + + exit_handler(); } // Assume these helper functions exist or need to be implemented @@ -1511,19 +1524,29 @@ int main(int argc, char **argv) prosperon = argv[0]; PHYSFS_init(argv[0]); - const char *base = PHYSFS_getBaseDir(); - PHYSFS_setWriteDir(base); - if (new_cwd) - PHYSFS_mount(new_cwd, NULL, 0); - else - PHYSFS_mount(base, NULL, 0); - + + // Mount the current working directory where the command was called from + char cwd[PATH_MAX]; + + if (!new_cwd) { + new_cwd = SDL_GetCurrentDirectory(); + if (!new_cwd) { + printf("error: %s\n", SDL_GetError()); + exit(1); + } + } + int mounted = prosperon_mount_core(); if (!mounted) mounted = PHYSFS_mount("core.zip", NULL, 0); if (!mounted) { printf("Could not mount core. Reason: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); return 1; } + + PHYSFS_mount(new_cwd, NULL, 0); + PHYSFS_setWriteDir(new_cwd); + + SDL_free(new_cwd); queue_mutex = SDL_CreateMutex(); queue_cond = SDL_CreateCondition();