Reworked the platform-dependent executable paths. Quoting the command line for Discord game invites. (#188)

This commit is contained in:
Radek Krzyśków 2024-12-28 00:05:54 +01:00 committed by GitHub
parent 12c7540daf
commit dc538f4f50
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 79 additions and 52 deletions

View file

@ -29,7 +29,7 @@ extern "C" {
#endif #endif
#define DYNOS_VERSION "1.0" #define DYNOS_VERSION "1.0"
#define DYNOS_EXE_FOLDER sys_exe_path() #define DYNOS_EXE_FOLDER sys_exe_path_dir()
#define DYNOS_USER_FOLDER fs_get_write_path("") #define DYNOS_USER_FOLDER fs_get_write_path("")
#define DYNOS_RES_FOLDER "dynos" #define DYNOS_RES_FOLDER "dynos"
#define DYNOS_PACKS_FOLDER DYNOS_RES_FOLDER "/packs" #define DYNOS_PACKS_FOLDER DYNOS_RES_FOLDER "/packs"

View file

@ -458,8 +458,8 @@ static void crash_handler(const int signalNum, siginfo_t *info, UNUSED ucontext_
// Load symbols // Load symbols
char filename[256] = { 0 }; char filename[256] = { 0 };
const char *exe_path = sys_exe_path(); const char *exe_path = sys_exe_path_dir();
if (NULL != exe_path) { if (exe_path[0] != '\0') {
snprintf(filename, 256, "%s/%s", exe_path, "coop.map"); snprintf(filename, 256, "%s/%s", exe_path, "coop.map");
} else { } else {
snprintf(filename, 256, "%s", "coop.map"); snprintf(filename, 256, "%s", "coop.map");

View file

@ -2,16 +2,15 @@
#include "pc/djui/djui.h" #include "pc/djui/djui.h"
#include "pc/crash_handler.h" #include "pc/crash_handler.h"
#include "pc/debuglog.h" #include "pc/debuglog.h"
#include "pc/platform.h"
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32)
#include <windows.h> #include <minwindef.h>
#include <winuser.h>
#else #else
#include <unistd.h>
#define MAX_PATH 1024 #define MAX_PATH 1024
#endif #endif
#define MAX_LAUNCH_CMD (MAX_PATH + 12) #define MAX_LAUNCH_CMD (MAX_PATH + 2)
#define APPLICATION_ID_COOPDX 1159627283506679839 #define APPLICATION_ID_COOPDX 1159627283506679839
@ -54,26 +53,17 @@ static void get_oauth2_token_callback(UNUSED void* data, enum EDiscordResult res
static void register_launch_command(void) { static void register_launch_command(void) {
char cmd[MAX_LAUNCH_CMD] = { 0 }; char cmd[MAX_LAUNCH_CMD] = { 0 };
int rc;
#if defined(_WIN32) || defined(_WIN64) const char *exe_path = sys_exe_path_file();
HMODULE hModule = GetModuleHandle(NULL); if (exe_path[0] == '\0') { return; }
if (hModule == NULL) {
LOG_ERROR("unable to retrieve absolute path!"); #if defined(_WIN32)
return; snprintf(cmd, MAX_LAUNCH_CMD, "\"%s\"", exe_path); // argv[0] double-quoted
}
GetModuleFileName(hModule, cmd, sizeof(cmd));
#else #else
char pidpath[MAX_LAUNCH_CMD] = { 0 }; snprintf(cmd, MAX_LAUNCH_CMD, "'%s'", exe_path); // argv[0] single-quoted
char fullpath[MAX_LAUNCH_CMD] = { 0 };
snprintf(pidpath, MAX_LAUNCH_CMD - 1, "/proc/%d/exe", getpid());
rc = readlink(pidpath, fullpath, MAX_LAUNCH_CMD - 1);
if (rc <= 0) {
LOG_ERROR("unable to retrieve absolute path! rc = %d", rc);
return;
}
snprintf(cmd, MAX_LAUNCH_CMD, "%s", fullpath);
#endif #endif
rc = app.activities->register_command(app.activities, cmd);
int rc = app.activities->register_command(app.activities, cmd);
if (rc != DiscordResult_Ok) { if (rc != DiscordResult_Ok) {
LOG_ERROR("register command failed %d", rc); LOG_ERROR("register command failed %d", rc);
return; return;

View file

@ -5,7 +5,6 @@
#ifdef _WIN32 #ifdef _WIN32
#define DISCORD_ID_FORMAT "%lld" #define DISCORD_ID_FORMAT "%lld"
#include <Windows.h>
#else #else
#define DISCORD_ID_FORMAT "%ld" #define DISCORD_ID_FORMAT "%ld"
#endif #endif

View file

@ -19,7 +19,7 @@ bool djui_language_init(char* lang) {
// construct path // construct path
char path[SYS_MAX_PATH] = ""; char path[SYS_MAX_PATH] = "";
if (!lang || lang[0] == '\0') { lang = "English"; } if (!lang || lang[0] == '\0') { lang = "English"; }
snprintf(path, SYS_MAX_PATH, "%s/lang/%s.ini", sys_exe_path(), lang); snprintf(path, SYS_MAX_PATH, "%s/lang/%s.ini", sys_exe_path_dir(), lang);
// load // load
sLang = ini_load(path); sLang = ini_load(path);

View file

@ -90,7 +90,7 @@ void djui_panel_language_create(struct DjuiBase* caller) {
{ {
// construct lang path // construct lang path
char lpath[SYS_MAX_PATH] = ""; char lpath[SYS_MAX_PATH] = "";
snprintf(lpath, SYS_MAX_PATH, "%s/lang", sys_exe_path()); snprintf(lpath, SYS_MAX_PATH, "%s/lang", sys_exe_path_dir());
// open directory // open directory
struct dirent* dir = NULL; struct dirent* dir = NULL;

View file

@ -441,7 +441,7 @@ void djui_panel_player_create(struct DjuiBase* caller) {
djui_selectionbox_create(body, DLANG(PLAYER, MODEL), characterChoices, CT_MAX, &configPlayerModel, djui_panel_player_value_changed); djui_selectionbox_create(body, DLANG(PLAYER, MODEL), characterChoices, CT_MAX, &configPlayerModel, djui_panel_player_value_changed);
player_palettes_reset(); player_palettes_reset();
player_palettes_read(sys_exe_path(), true); player_palettes_read(sys_exe_path_dir(), true);
player_palettes_read(fs_get_write_path(PALETTES_DIRECTORY), false); player_palettes_read(fs_get_write_path(PALETTES_DIRECTORY), false);
char* palettePresets[MAX_PRESET_PALETTES + 1] = { DLANG(PALETTE, CUSTOM) }; char* palettePresets[MAX_PRESET_PALETTES + 1] = { DLANG(PALETTE, CUSTOM) };

View file

@ -255,7 +255,7 @@ const char *fs_convert_path(char *buf, const size_t bufsiz, const char *path) {
// ! means "executable directory" // ! means "executable directory"
if (path[0] == '!') { if (path[0] == '!') {
if (snprintf(buf, bufsiz, "%s%s", sys_exe_path(), path + 1) < 0) { if (snprintf(buf, bufsiz, "%s%s", sys_exe_path_dir(), path + 1) < 0) {
return ""; return "";
} }
} else { } else {

View file

@ -301,7 +301,7 @@ void mods_refresh_local(void) {
if (hasUserPath) { mods_load(&gLocalMods, userModPath, true); } if (hasUserPath) { mods_load(&gLocalMods, userModPath, true); }
char defaultModsPath[SYS_MAX_PATH] = { 0 }; char defaultModsPath[SYS_MAX_PATH] = { 0 };
snprintf(defaultModsPath, SYS_MAX_PATH, "%s/%s", sys_exe_path(), MOD_DIRECTORY); snprintf(defaultModsPath, SYS_MAX_PATH, "%s/%s", sys_exe_path_dir(), MOD_DIRECTORY);
mods_load(&gLocalMods, defaultModsPath, false); mods_load(&gLocalMods, defaultModsPath, false);
// sort // sort

View file

@ -5,14 +5,19 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#ifdef _WIN32 #if defined(_WIN32)
#include <windows.h> #include <windows.h>
#include <shlobj.h> #include <shlobj.h>
#include <shlwapi.h> #include <shlwapi.h>
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#else
#include <unistd.h>
#endif #endif
#include "cliopts.h" #include "cliopts.h"
#include "fs/fs.h" #include "fs/fs.h"
#include "debuglog.h"
#include "configfile.h" #include "configfile.h"
/* these are not available on some platforms, so might as well */ /* these are not available on some platforms, so might as well */
@ -247,17 +252,31 @@ const char *sys_user_path(void)
return sys_windows_short_path_from_wcs(shortPath, SYS_MAX_PATH, widePath) ? shortPath : NULL; return sys_windows_short_path_from_wcs(shortPath, SYS_MAX_PATH, widePath) ? shortPath : NULL;
} }
const char *sys_exe_path(void) const char *sys_exe_path_dir(void)
{
static char path[SYS_MAX_PATH];
if ('\0' != path[0]) { return path; }
const char *exeFilepath = sys_exe_path_file();
char *lastSeparator = strrchr(exeFilepath, '\\');
if (lastSeparator != NULL) {
size_t count = (size_t)(lastSeparator - exeFilepath);
strncpy(path, exeFilepath, count);
}
return path;
}
const char *sys_exe_path_file(void)
{ {
static char shortPath[SYS_MAX_PATH] = { 0 }; static char shortPath[SYS_MAX_PATH] = { 0 };
if ('\0' != shortPath[0]) { return shortPath; } if ('\0' != shortPath[0]) { return shortPath; }
WCHAR widePath[SYS_MAX_PATH]; WCHAR widePath[SYS_MAX_PATH];
if (0 == GetModuleFileNameW(NULL, widePath, SYS_MAX_PATH)) { return NULL; } if (0 == GetModuleFileNameW(NULL, widePath, SYS_MAX_PATH)) {
LOG_ERROR("unable to retrieve absolute path.");
WCHAR *lastBackslash = wcsrchr(widePath, L'\\'); return shortPath;
if (NULL != lastBackslash) { *lastBackslash = L'\0'; } }
else { return NULL; }
return sys_windows_short_path_from_wcs(shortPath, SYS_MAX_PATH, widePath) ? shortPath : NULL; return sys_windows_short_path_from_wcs(shortPath, SYS_MAX_PATH, widePath) ? shortPath : NULL;
} }
@ -307,20 +326,38 @@ const char *sys_user_path(void) {
return path; return path;
} }
const char *sys_exe_path(void) { const char *sys_exe_path_dir(void) {
static char path[SYS_MAX_PATH] = { 0 }; static char path[SYS_MAX_PATH];
if ('\0' != path[0]) { return path; } if ('\0' != path[0]) { return path; }
char *sdlPath = SDL_GetBasePath(); const char *exeFilepath = sys_exe_path_file();
if (sdlPath && sdlPath[0]) { char *lastSeparator = strrchr(exeFilepath, '/');
// use the SDL path if it exists if (lastSeparator != NULL) {
const unsigned int len = strlen(sdlPath); size_t count = (size_t)(lastSeparator - exeFilepath);
snprintf(path, sizeof(path), "%s", sdlPath); strncpy(path, exeFilepath, count);
path[sizeof(path)-1] = 0;
SDL_free(sdlPath);
if (path[len-1] == '/' || path[len-1] == '\\')
path[len-1] = 0; // strip the trailing separator
} }
return path;
}
const char *sys_exe_path_file(void) {
static char path[SYS_MAX_PATH];
if ('\0' != path[0]) { return path; }
#if defined(__APPLE__)
uint32_t bufsize = SYS_MAX_PATH;
int res = _NSGetExecutablePath(path, &bufsize);
#else
char procPath[SYS_MAX_PATH];
snprintf(procPath, SYS_MAX_PATH, "/proc/%d/exe", getpid());
ssize_t res = readlink(procPath, path, SYS_MAX_PATH);
#endif
if (res <= 0) {
LOG_ERROR("unable to retrieve absolute path.");
}
return path; return path;
} }

View file

@ -20,7 +20,8 @@ bool sys_windows_short_path_from_wcs(char *destPath, size_t destSize, const wcha
bool sys_windows_short_path_from_mbs(char* destPath, size_t destSize, const char *mbsLongPath); bool sys_windows_short_path_from_mbs(char* destPath, size_t destSize, const char *mbsLongPath);
#endif #endif
const char *sys_user_path(void); const char *sys_user_path(void);
const char *sys_exe_path(void); const char *sys_exe_path_dir(void);
const char *sys_exe_path_file(void);
const char *sys_file_extension(const char *fpath); const char *sys_file_extension(const char *fpath);
const char *sys_file_name(const char *fpath); const char *sys_file_name(const char *fpath);
void sys_swap_backslashes(char* buffer); void sys_swap_backslashes(char* buffer);

View file

@ -97,7 +97,7 @@ void legacy_folder_handler(void) {
bool main_rom_handler(void) { bool main_rom_handler(void) {
if (scan_path_for_rom(fs_get_write_path(""))) { return true; } if (scan_path_for_rom(fs_get_write_path(""))) { return true; }
scan_path_for_rom(sys_exe_path()); scan_path_for_rom(sys_exe_path_dir());
return gRomIsValid; return gRomIsValid;
} }