From 40dcb60bd748459082a0117bb5deb5aba1169a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Noel?= Date: Mon, 7 Oct 2024 13:08:36 +0200 Subject: [PATCH] honor the $XDG_CACHE_HOME env variable --- Descent3/init.cpp | 8 +------- ddio/ddio.h | 6 ++++++ ddio/file.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Descent3/init.cpp b/Descent3/init.cpp index fcec5fce7..05810858f 100644 --- a/Descent3/init.cpp +++ b/Descent3/init.cpp @@ -1957,13 +1957,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 d747f81b7..78a827be6 100644 --- a/ddio/ddio.h +++ b/ddio/ddio.h @@ -412,4 +412,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 367736883..53641bebf 100644 --- a/ddio/file.cpp +++ b/ddio/file.cpp @@ -192,3 +192,30 @@ std::filesystem::path ddio_GetTmpFileName(const std::filesystem::path &basedir, mem_free(random_name); return result; } + +std::filesystem::path ddio_GetTempPath() { + std::filesystem::path result; + +#if defined(POSIX) + 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; +}