1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2026-05-08 08:11:21 +00:00

config/save file default location under Linux is now according to the XDG standard

This commit is contained in:
Zeno Rogue
2026-04-17 16:02:36 +02:00
parent 12f2e37961
commit d0f53270f7
2 changed files with 35 additions and 11 deletions

View File

@@ -61,13 +61,37 @@ EX void initializeCLI() {
#endif
#ifdef FHS
static string sbuf, cbuf;
if(getenv("HOME")) {
sbuf = getenv("HOME"); sbuf += "/."; sbuf += scorefile;
cbuf = getenv("HOME"); cbuf += "/."; cbuf += conffile;
scorefile = sbuf;
conffile = cbuf.c_str();
}
auto try_place = [&] (string env, string suffix, string& which_file, bool only_file) {
char *res = getenv(env.c_str());
if(!res) return false;
string buf = res; buf += suffix; buf += "/hyperrogue";
if(only_file && file_exists(buf + "/" + which_file)) {
which_file = buf + "/" + which_file;
return true;
}
if(!file_exists(buf)) system(("mkdir -p " +buf).c_str());
which_file = buf + "/" + which_file;
return true;
};
auto try_dot = [&] (string& which_file) {
char *res = getenv("HOME");
string buf = res;
buf += "/."; buf += which_file;
if(!file_exists(buf)) return false;
which_file = buf;
return true;
};
if(try_dot(scorefile)) {}
else if(try_place("XDG_DATA_HOME", "", scorefile, true)) {}
else if(try_place("HOME", "/.local/share", scorefile, false)) {}
else if(try_place("XDG_DATA_HOME", "", scorefile, false)) {}
if(try_dot(conffile)) {}
else if(try_place("XDG_CONFIG_HOME", "", conffile, true)) {}
else if(try_place("HOME", "/.config", conffile, true)) {}
else if(try_place("XDG_CONFIG_HOME", "", conffile, false)) {}
#endif
}

View File

@@ -918,7 +918,7 @@ EX shared_ptr<custom_parameter> param_colortable(colortable& val, const paramete
EX ld bounded_mine_percentage = 0.1;
EX int bounded_mine_quantity, bounded_mine_max;
EX const char *conffile = "hyperrogue.ini";
EX string conffile = "hyperrogue.ini";
/* extra space if more geometries are added */
EX array<ld, gGUARD+64> sightranges;
@@ -2058,7 +2058,7 @@ EX debugflag debug_init_config = {"init_config", true};
EX void saveConfig() {
indenter_finish(debug_init_config, "saveConfig");
FILE *f = fopen(conffile, "wt");
FILE *f = fopen(conffile.c_str(), "wt");
if(!f) {
addMessage(s0 + "Could not open the config file: " + conffile);
return;
@@ -2131,7 +2131,7 @@ EX void loadConfig() {
indenter_finish(debug_init_config, "loadConfig");
vid.xres = 9999; vid.yres = 9999; vid.framelimit = 999;
FILE *f = fopen(conffile, "rt");
FILE *f = fopen(conffile.c_str(), "rt");
if(f) {
int err;
int fs;
@@ -4185,7 +4185,7 @@ EX void resetConfigMenu() {
if(uni == 'd') {
resetConfig();
unlink(conffile);
unlink(conffile.c_str());
popScreen();
}
else if(uni == 'y') {