remove the assumption that game is ran from cwd at root

This commit is contained in:
2024-10-07 13:22:25 +03:00
parent 6cbfc8a5fb
commit 93aa8ff2b4
5 changed files with 37 additions and 10 deletions

View File

@ -232,7 +232,7 @@ static void resolve_pack_dependencies(const char *pack_name) {
}
char *dep_pack_name;
if (SDL_asprintf(&dep_pack_name, "%s.btw", dep_name.u.s) == -1) {
if (SDL_asprintf(&dep_pack_name, "%s%s.btw", ctx.base_dir, dep_name.u.s) == -1) {
CRY("resolve_pack_dependencies()", "Allocation error");
SDL_free(dep_name.u.s);
goto ERR_DEP_PATH_ALLOC_FAIL;
@ -249,12 +249,21 @@ static void resolve_pack_dependencies(const char *pack_name) {
continue;
}
if (!PHYSFS_mount(dep_source.u.s, "/", true))
char *dep_source_path;
if (SDL_asprintf(&dep_source_path, "%s%s", ctx.base_dir, dep_source.u.s) == -1) {
CRY("resolve_pack_dependencies()", "Allocation error");
SDL_free(dep_name.u.s);
SDL_free(dep_pack_name);
goto ERR_DEP_PATH_ALLOC_FAIL;
}
if (!PHYSFS_mount(dep_source_path, "/", true))
CRY("Cannot load pack", "Nothing is given to work with");
log_info("Pack loaded: %s\n", dep_source.u.s);
SDL_free(dep_source.u.s);
SDL_free(dep_source_path);
} else
log_info("Pack loaded: %s\n", dep_pack_name);
@ -638,6 +647,7 @@ static void clean_up(void) {
toml_free(ctx.config_table);
PHYSFS_deinit();
SDL_free(ctx.base_dir);
SDL_GL_DeleteContext(ctx.gl_context);
SDL_Quit();
}
@ -652,6 +662,7 @@ static void reset_state(void) {
int enter_loop(int argc, char **argv) {
ctx.argc = argc;
ctx.argv = argv;
ctx.base_dir = SDL_GetBasePath();
/* needs to be done before anything else so config can be loaded */
/* TODO: ANDROID: see the warning in physicsfs PHYSFS_init docs/header */
@ -699,11 +710,17 @@ int enter_loop(int argc, char **argv) {
/* data path not explicitly specified, look into convention defined places */
if (!data_dir_mounted) {
/* try mouning data folder first, relative to executable root */
if (!PHYSFS_mount("data", NULL, true)) {
if (!PHYSFS_mount("data.btw", NULL, true))
char *full_path;
SDL_asprintf(&full_path, "%sdata", ctx.base_dir);
if (!PHYSFS_mount(full_path, NULL, true)) {
SDL_free(full_path);
SDL_asprintf(&full_path, "%sdata.btw", ctx.base_dir);
if (!PHYSFS_mount(full_path, NULL, true))
SDL_free(full_path);
CRY_PHYSFS("Cannot find data.btw or data directory in root. Please create them or specify with --data-dir parameter.");
return EXIT_FAILURE;
}
SDL_free(full_path);
}
if (!initialize())