Create config with sane defaults if not already present (#71)

* Create config with sane defaults if not present

* isleapp: Use binary mode for iniFP

Windows treats newlines differently then on *nix systems. Using binary mode makes this consistent.

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>

* isleapp: Remove extern "C"

It seems like it's fine including these libraries without extern "C"

* isleapp: Use boolean return in LoadConfig

This to check if the config loaded correctly or not.
Seems to be a good solution to the file loading issue.

Maybe there's a better solution
SetupWindow failing because of a config erroring seems a bit odd.

* isleapp: Remove that dumb use after free.

I forgot you don't specify a filename you specify a *file pointer*

* isleapp: Fix iniFP to be in proper scope

This also fixes the missing parameter in `iniparser_dump_ini`

* .gitignore: Exclude the kate .cache folder

* isleapp: Match prototype of LoadConfig in header

* isleapp: Fix formating to comply with .clang-format

* isleapp: Don't create new config if manually set

---------

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>
This commit is contained in:
AllMeatball 2025-05-13 00:25:16 +00:00 committed by GitHub
parent dc8b044b50
commit c48935a64b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 63 additions and 3 deletions

3
.gitignore vendored
View File

@ -27,3 +27,6 @@ ISLE.EXE
LEGO1.DLL LEGO1.DLL
.DS_Store .DS_Store
# Kate - Text
/.cache

View File

@ -32,7 +32,9 @@
#define SDL_MAIN_USE_CALLBACKS #define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <errno.h>
#include <iniparser.h> #include <iniparser.h>
#include <stdlib.h>
#include <time.h> #include <time.h>
DECOMP_SIZE_ASSERT(IsleApp, 0x8c) DECOMP_SIZE_ASSERT(IsleApp, 0x8c)
@ -444,7 +446,10 @@ MxU8 IsleApp::MapMouseButtonFlagsToModifier(SDL_MouseButtonFlags p_flags)
// FUNCTION: ISLE 0x4023e0 // FUNCTION: ISLE 0x4023e0
MxResult IsleApp::SetupWindow() MxResult IsleApp::SetupWindow()
{ {
LoadConfig(); if (!LoadConfig()) {
return FAILURE;
}
SetupVideoFlags( SetupVideoFlags(
m_fullScreen, m_fullScreen,
m_flipSurfaces, m_flipSurfaces,
@ -519,7 +524,7 @@ MxResult IsleApp::SetupWindow()
} }
// FUNCTION: ISLE 0x4028d0 // FUNCTION: ISLE 0x4028d0
void IsleApp::LoadConfig() bool IsleApp::LoadConfig()
{ {
char* prefPath = SDL_GetPrefPath("isledecomp", "isle"); char* prefPath = SDL_GetPrefPath("isledecomp", "isle");
char* iniConfig; char* iniConfig;
@ -540,6 +545,56 @@ void IsleApp::LoadConfig()
dictionary* dict = iniparser_load(iniConfig); dictionary* dict = iniparser_load(iniConfig);
// [library:config]
// Load sane defaults if dictionary failed to load
if (!dict) {
if (m_iniPath) {
SDL_Log("Invalid config path '%s'", m_iniPath);
return false;
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading sane defaults");
FILE* iniFP = fopen(iniConfig, "wb");
if (!iniFP) {
SDL_LogError(
SDL_LOG_CATEGORY_APPLICATION,
"Failed to write config at '%s': %s",
iniConfig,
strerror(errno)
);
return false;
}
dict = iniparser_load(iniConfig);
iniparser_set(dict, "isle", NULL);
iniparser_set(dict, "isle:diskpath", SDL_GetBasePath());
iniparser_set(dict, "isle:cdpath", MxOmni::GetCD());
iniparser_set(dict, "isle:mediapath", SDL_GetBasePath());
iniparser_set(dict, "isle:savepath", prefPath);
iniparser_set(dict, "isle:Flip Surfaces", m_flipSurfaces ? "true" : "false");
iniparser_set(dict, "isle:Full Screen", m_fullScreen ? "true" : "false");
iniparser_set(dict, "isle:Wide View Angle", m_wideViewAngle ? "true" : "false");
iniparser_set(dict, "isle:3DSound", m_use3dSound ? "true" : "false");
iniparser_set(dict, "isle:Music", m_useMusic ? "true" : "false");
iniparser_set(dict, "isle:UseJoystick", m_useJoystick ? "true" : "false");
iniparser_set(dict, "isle:JoystickIndex", m_joystickIndex ? "true" : "false");
iniparser_set(dict, "isle:Draw Cursor", m_drawCursor ? "true" : "false");
iniparser_set(dict, "isle:Back Buffers in Video RAM", "-1");
iniparser_set(dict, "isle:Island Quality", "1");
iniparser_set(dict, "isle:Island Texture", "1");
iniparser_dump_ini(dict, iniFP);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "New config written at '%s'", iniConfig);
fclose(iniFP);
}
const char* hdPath = iniparser_getstring(dict, "isle:diskpath", SDL_GetBasePath()); const char* hdPath = iniparser_getstring(dict, "isle:diskpath", SDL_GetBasePath());
m_hdPath = new char[strlen(hdPath) + 1]; m_hdPath = new char[strlen(hdPath) + 1];
strcpy(m_hdPath, hdPath); strcpy(m_hdPath, hdPath);
@ -597,6 +652,8 @@ void IsleApp::LoadConfig()
iniparser_freedict(dict); iniparser_freedict(dict);
delete[] iniConfig; delete[] iniConfig;
SDL_free(prefPath); SDL_free(prefPath);
return true;
} }
// FUNCTION: ISLE 0x402c20 // FUNCTION: ISLE 0x402c20

View File

@ -36,7 +36,7 @@ class IsleApp {
); );
MxResult SetupWindow(); MxResult SetupWindow();
void LoadConfig(); bool LoadConfig();
void Tick(); void Tick();
void SetupCursor(Cursor p_cursor); void SetupCursor(Cursor p_cursor);