try to search for gamestates in directory with binary before resorting to LD_LIBRARY_PATHs

This commit is contained in:
Sebastian Krzyszkowiak 2017-09-20 18:11:29 +02:00
parent c34c13739d
commit a3cda7ba50
2 changed files with 20 additions and 1 deletions

View file

@ -200,7 +200,7 @@ SYMBOL_INTERNAL bool OpenGamestate(struct Game* game, struct Gamestate* gamestat
PrintConsole(game, "Opening gamestate \"%s\"...", gamestate->name); PrintConsole(game, "Opening gamestate \"%s\"...", gamestate->name);
char libname[1024]; char libname[1024];
snprintf(libname, 1024, "libsuperderpy-%s-%s" LIBRARY_EXTENSION, game->name, gamestate->name); snprintf(libname, 1024, "libsuperderpy-%s-%s" LIBRARY_EXTENSION, game->name, gamestate->name);
gamestate->handle = dlopen(libname, RTLD_NOW); gamestate->handle = dlopen(AddGarbage(game, GetLibraryPath(game, libname)), RTLD_NOW);
if (!gamestate->handle) { if (!gamestate->handle) {
FatalError(game, false, "Error while opening gamestate \"%s\": %s", gamestate->name, dlerror()); // TODO: move out FatalError(game, false, "Error while opening gamestate \"%s\": %s", gamestate->name, dlerror()); // TODO: move out
return false; return false;
@ -407,3 +407,21 @@ SYMBOL_INTERNAL void DrawTimelines(struct Game* game) {
tmp = tmp->next; tmp = tmp->next;
} }
} }
SYMBOL_INTERNAL char* GetLibraryPath(struct Game* game, char* filename) {
char* result = NULL;
ALLEGRO_PATH* path = al_get_standard_path(ALLEGRO_EXENAME_PATH);
al_set_path_filename(path, filename);
if (al_filename_exists(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP))) {
result = strdup(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
} else {
al_append_path_component(path, "gamestates");
if (al_filename_exists(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP))) {
result = strdup(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
} else {
result = strdup(filename);
}
}
al_destroy_path(path);
return result;
}

View file

@ -86,5 +86,6 @@ bool OpenGamestate(struct Game* game, struct Gamestate* gamestate);
bool LinkGamestate(struct Game* game, struct Gamestate* gamestate); bool LinkGamestate(struct Game* game, struct Gamestate* gamestate);
void CloseGamestate(struct Game* game, struct Gamestate* gamestate); void CloseGamestate(struct Game* game, struct Gamestate* gamestate);
struct Gamestate* AllocateGamestate(struct Game* game, const char* name); struct Gamestate* AllocateGamestate(struct Game* game, const char* name);
char* GetLibraryPath(struct Game* game, char* filename);
#endif #endif