diff --git a/Descent3/init.cpp b/Descent3/init.cpp index c719ee373..5f549fb7f 100644 --- a/Descent3/init.cpp +++ b/Descent3/init.cpp @@ -1985,13 +1985,7 @@ void SetupTempDirectory(void) { if (t_arg) { Descent3_temp_directory = GameArgs[t_arg + 1]; } else { - std::error_code ec; - std::filesystem::path tempPath = std::filesystem::temp_directory_path(ec); - if (ec) { - Error("Could not find temporary directory: \"%s\"", ec.message().c_str()); - exit(1); - } - Descent3_temp_directory = tempPath / "Descent3" / "cache"; + Descent3_temp_directory = ddio_GetTempPath(); } std::error_code ec; diff --git a/ddio/ddio.h b/ddio/ddio.h index ff2038741..fa45a6a2a 100644 --- a/ddio/ddio.h +++ b/ddio/ddio.h @@ -426,4 +426,10 @@ bool ddio_CreateLockFile(const std::filesystem::path &dir); */ bool ddio_DeleteLockFile(const std::filesystem::path &dir); +/** + * Gets path where to write temporary/cache files. + * @return path where user can write cache files. + */ +std::filesystem::path ddio_GetTempPath(); + #endif diff --git a/ddio/file.cpp b/ddio/file.cpp index 9d6c25d31..fec7d2fd5 100644 --- a/ddio/file.cpp +++ b/ddio/file.cpp @@ -214,3 +214,30 @@ std::filesystem::path ddio_GetBasePath() { std::filesystem::path result = std::filesystem::canonical(exe_path); return result; } + +std::filesystem::path ddio_GetTempPath() { + std::filesystem::path result; + +#if defined(POSIX) + const char *envr = SDL_getenv("XDG_CACHE_HOME"); + if (envr) { + result = std::filesystem::path(envr) / "Descent3"; + } else { + envr = SDL_getenv("HOME"); + if (envr) { + result = std::filesystem::path(envr) / ".cache" / "Descent3"; + } else { +#endif + std::error_code ec; + std::filesystem::path tempPath = std::filesystem::temp_directory_path(ec); + if (ec) { + Error("Could not find temporary directory: \"%s\"", ec.message().c_str()); + exit(1); + } + result = tempPath / "Descent3" / "cache"; +#if defined(POSIX) + } + } +#endif + return result; +}