Skip to content

Commit ab48e06

Browse files
committed
honor the $XDG_CACHE_HOME env variable
1 parent 67244d9 commit ab48e06

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

Descent3/init.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,13 +1985,7 @@ void SetupTempDirectory(void) {
19851985
if (t_arg) {
19861986
Descent3_temp_directory = GameArgs[t_arg + 1];
19871987
} else {
1988-
std::error_code ec;
1989-
std::filesystem::path tempPath = std::filesystem::temp_directory_path(ec);
1990-
if (ec) {
1991-
Error("Could not find temporary directory: \"%s\"", ec.message().c_str());
1992-
exit(1);
1993-
}
1994-
Descent3_temp_directory = tempPath / "Descent3" / "cache";
1988+
Descent3_temp_directory = ddio_GetTempPath();
19951989
}
19961990

19971991
std::error_code ec;

ddio/ddio.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,10 @@ bool ddio_CreateLockFile(const std::filesystem::path &dir);
426426
*/
427427
bool ddio_DeleteLockFile(const std::filesystem::path &dir);
428428

429+
/**
430+
* Gets path where to write temporary/cache files.
431+
* @return path where user can write cache files.
432+
*/
433+
std::filesystem::path ddio_GetTempPath();
434+
429435
#endif

ddio/file.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,30 @@ std::filesystem::path ddio_GetBasePath() {
214214
std::filesystem::path result = std::filesystem::canonical(exe_path);
215215
return result;
216216
}
217+
218+
std::filesystem::path ddio_GetTempPath() {
219+
std::filesystem::path result;
220+
221+
#if defined(POSIX)
222+
const char *envr = SDL_getenv("XDG_CACHE_HOME");
223+
if (envr) {
224+
result = std::filesystem::path(envr) / "Descent3";
225+
} else {
226+
envr = SDL_getenv("HOME");
227+
if (envr) {
228+
result = std::filesystem::path(envr) / ".cache" / "Descent3";
229+
} else {
230+
#endif
231+
std::error_code ec;
232+
std::filesystem::path tempPath = std::filesystem::temp_directory_path(ec);
233+
if (ec) {
234+
Error("Could not find temporary directory: \"%s\"", ec.message().c_str());
235+
exit(1);
236+
}
237+
result = tempPath / "Descent3" / "cache";
238+
#if defined(POSIX)
239+
}
240+
}
241+
#endif
242+
return result;
243+
}

0 commit comments

Comments
 (0)