config: add shared openw3d.conf path handling and --ini override#104
config: add shared openw3d.conf path handling and --ini override#104caseychaos1212 merged 4 commits intow3dhub:mainfrom
Conversation
| #ifdef _WIN32 | ||
| char path[MAX_PATH] = { 0 }; | ||
| DWORD length = GetCurrentDirectoryA(ARRAY_SIZE(path), path); | ||
| if (length > 0 && length < ARRAY_SIZE(path)) { | ||
| return std::filesystem::path(path); | ||
| } | ||
| #else | ||
| char path[4096] = { 0 }; | ||
| if (getcwd(path, sizeof(path)) != NULL) { | ||
| return std::filesystem::path(path); | ||
| } |
There was a problem hiding this comment.
Perhaps use the OPENW3D_WIN32 and OPENW3D_SDL3 macros?
There is std::filesystem::current_path to get the working directory so the unix getcwd is not needed.
| #ifdef _WIN32 | ||
| char path[32768] = { 0 }; | ||
| DWORD length = GetModuleFileNameA(NULL, path, ARRAY_SIZE(path)); | ||
| if (length > 0 && length < ARRAY_SIZE(path)) { | ||
| return std::filesystem::path(path).parent_path(); | ||
| } | ||
| #else | ||
| char path[4096] = { 0 }; | ||
| const ssize_t length = readlink("/proc/self/exe", path, sizeof(path) - 1); | ||
| if (length > 0) { | ||
| path[length] = '\0'; | ||
| return std::filesystem::path(path).parent_path(); | ||
| } | ||
| #endif |
There was a problem hiding this comment.
Let's also use the OPENW3D_* macros here.
For the SDL3 case, I think SDL_GetBasePath returns what we want.
/proc/self/exe only works on Linux and not on the BSDs, macOS, etc
| #ifdef _WIN32 | ||
| std::filesystem::path appdata_path; | ||
| HMODULE shfolder = LoadLibraryA("shfolder.dll"); | ||
| if (shfolder != NULL) { | ||
| typedef HRESULT(__stdcall *SHGetFolderPathAType)(HWND, int, HANDLE, DWORD, LPSTR); | ||
| SHGetFolderPathAType get_folder_path = | ||
| (SHGetFolderPathAType)GetProcAddress(shfolder, "SHGetFolderPathA"); | ||
| if (get_folder_path != NULL) { | ||
| char path[MAX_PATH] = { 0 }; | ||
| if (get_folder_path(NULL, CSIDL_APPDATA, NULL, 0, path) == S_OK) { | ||
| appdata_path = std::filesystem::path(path); | ||
| } | ||
| } | ||
| FreeLibrary(shfolder); | ||
| } | ||
|
|
||
| return appdata_path; | ||
| #else | ||
| return std::filesystem::path(); | ||
| #endif |
There was a problem hiding this comment.
Same story about OPENW3D_*. SDL3 has SDL_GetUserFolder which provides what we needs.
| #ifdef _WIN32 | ||
| std::filesystem::path config_dir = Get_Windows_AppData_Directory(); | ||
| if (!config_dir.empty()) { | ||
| return config_dir / "OpenW3D" / W3D_CONF_FILENAME; | ||
| } | ||
| #else | ||
| const char *xdg_config_home = std::getenv("XDG_CONFIG_HOME"); | ||
| if (xdg_config_home != NULL && xdg_config_home[0] != '\0') { | ||
| return Make_Absolute_Path(xdg_config_home) / "OpenW3D" / W3D_CONF_FILENAME; | ||
| } | ||
|
|
||
| const char *home = std::getenv("HOME"); | ||
| if (home != NULL && home[0] != '\0') { | ||
| return Make_Absolute_Path(home) / ".config" / "OpenW3D" / W3D_CONF_FILENAME; | ||
| } | ||
| #endif |
There was a problem hiding this comment.
OPENW3d_* yada yada.
SDL_GetPrefPath returns a path for storing preferences (=configs).
…ed the Unix-specific getcwd and /proc/self/exe code, and now use SDL_GetBasePath() and SDL_GetPrefPath() for the SDL3 path cases. Get_Current_Directory() now just uses std::filesystem::current_path().
rm5248
left a comment
There was a problem hiding this comment.
this looks pretty reasonable to me, I don't have any concerns currently.
madebr
left a comment
There was a problem hiding this comment.
So happy the --gamedir option works again as designed!
I've only a small remark about the default ini location.
| return appdata_path.empty() ? std::filesystem::path() : appdata_path / "OpenW3D"; | ||
| #elif defined(OPENW3D_SDL3) | ||
| char *pref_path = SDL_GetPrefPath("OpenW3D", "OpenW3D"); |
There was a problem hiding this comment.
There's a difference between the default openw3d.conf path:
OPENRW_WIN32: C:\Users\<user>\AppData\Roaming\OpenW3D\openw3d.conf
OPENRW_SDL3: C:\Users\<user>\AppData\Roaming\OpenW3D\OpenW3D\openw3d.conf
Perhaps it should be W3DHub\OpenW3D\openw3d.conf for all?
OmniBlade
left a comment
There was a problem hiding this comment.
Just style nitpicks from me.
|
|
||
| namespace | ||
| { | ||
| constexpr char kConfigOrganization[] = "W3DHub"; |
There was a problem hiding this comment.
This code base uses CAPS_CASE for constants and doesn't use hungarian notation for the most part.
|
|
||
| std::filesystem::path Get_User_Config_Directory() | ||
| { | ||
| #if defined(OPENW3D_WIN32) |
There was a problem hiding this comment.
General style in the code base is not to indent preprocessor directives and infact you didn't earlier in the file.
This follows up the INI-backed config work by moving
openw3d.confpathresolution into shared
wwlibcode and by teaching the game and legacywwconfigentry points to honor an explicit--ini PATHoverride.What this changes:
wwlib--ini PATHinrenegadeandwwconfig--ini PATHOPENW3D_CONFIG_INI/RENEGADE_CONFIG_INIopenw3d.confnext to the executable if it already exists--inipath before any--gamedirhandling can change the working directoryrenegadelauncheswwconfig.exe