From 21fe945ca7ccc144e11c1a3869ae286ae81b701a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 20 Aug 2022 20:54:25 +0200 Subject: [PATCH] Make file link absolute, add `fs_is_relative_path` This fixes links not opening for relative paths, as links like `file://temp/skins` cannot be resolved by the shell. --- CMakeLists.txt | 2 +- src/base/system.cpp | 24 +++++++++++++++++++++++- src/base/system.h | 9 +++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8b11ba58..9ecc33c3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -721,7 +721,7 @@ endif() if(TARGET_OS STREQUAL "windows") set(PLATFORM_CLIENT) set(PLATFORM_CLIENT_LIBS opengl32 winmm) - set(PLATFORM_LIBS version ws2_32) # Windows sockets + set(PLATFORM_LIBS shlwapi version ws2_32) # Windows sockets elseif(TARGET_OS STREQUAL "mac") find_library(CARBON Carbon) find_library(COCOA Cocoa) diff --git a/src/base/system.cpp b/src/base/system.cpp index a7e94447b..331855ced 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -68,6 +68,7 @@ #include #include #include +#include #include #else #error NOT IMPLEMENTED @@ -2309,6 +2310,17 @@ int fs_is_dir(const char *path) #endif } +int fs_is_relative_path(const char *path) +{ +#if defined(CONF_FAMILY_WINDOWS) + WCHAR wPath[IO_MAX_PATH_LENGTH]; + MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, std::size(wPath)); + return PathIsRelativeW(wPath) ? 1 : 0; +#else + return path[0] == '/' ? 0 : 1; // yes, it's that simple +#endif +} + int fs_chdir(const char *path) { if(fs_is_dir(path)) @@ -3866,8 +3878,18 @@ int open_file(const char *path) #if defined(CONF_PLATFORM_MACOS) return open_link(path); #else + // Create a file link so the path can contain forward and + // backward slashes. But the file link must be absolute. char buf[512]; - str_format(buf, sizeof(buf), "file://%s", path); + char workingDir[IO_MAX_PATH_LENGTH]; + if(fs_is_relative_path(path)) + { + fs_getcwd(workingDir, sizeof(workingDir)); + str_append(workingDir, "/", sizeof(workingDir)); + } + else + workingDir[0] = '\0'; + str_format(buf, sizeof(buf), "file://%s%s", workingDir, path); return open_link(buf); #endif } diff --git a/src/base/system.h b/src/base/system.h index a20aac955..379a7b044 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -1821,6 +1821,15 @@ int fs_storage_path(const char *appname, char *path, int max); */ int fs_is_dir(const char *path); +/* + Function: fs_is_relative_path + Checks whether a given path is relative or absolute. + + Returns: + Returns 1 if relative, 0 if absolute. +*/ +int fs_is_relative_path(const char *path); + /* Function: fs_chdir Changes current working directory