cwd works correctly for when running from a different folder
Some checks failed
Build and Deploy / build-macos (push) Failing after 7s
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled

This commit is contained in:
2025-05-29 17:59:33 -05:00
parent 9ae2357493
commit f54200a7dd
2 changed files with 60 additions and 31 deletions

View File

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

View File

@@ -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,12 +1524,17 @@ 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);
@@ -1525,6 +1543,11 @@ int main(int argc, char **argv)
return 1;
}
PHYSFS_mount(new_cwd, NULL, 0);
PHYSFS_setWriteDir(new_cwd);
SDL_free(new_cwd);
queue_mutex = SDL_CreateMutex();
queue_cond = SDL_CreateCondition();
actors_mutex = SDL_CreateMutex();