diff --git a/ISLE/android/config.cpp b/ISLE/android/config.cpp index e93cb02e..4883a239 100644 --- a/ISLE/android/config.cpp +++ b/ISLE/android/config.cpp @@ -21,7 +21,6 @@ void Android_SetupDefaultConfigOverrides(dictionary* p_dictionary) iniparser_set(p_dictionary, "isle:diskpath", data); iniparser_set(p_dictionary, "isle:cdpath", data); iniparser_set(p_dictionary, "isle:mediapath", data); - iniparser_set(p_dictionary, "isle:savepath", savedata.GetData()); // Default to Virtual Mouse diff --git a/ISLE/ios/config.cpp b/ISLE/ios/config.cpp index ee9349fa..22a17657 100644 --- a/ISLE/ios/config.cpp +++ b/ISLE/ios/config.cpp @@ -1,5 +1,7 @@ #include "config.h" +#include "mxstring.h" + #include #include #include @@ -11,15 +13,13 @@ void IOS_SetupDefaultConfigOverrides(dictionary* p_dictionary) // Use DevelopmentFiles path for disk and cd paths // It's good to use that path since user can easily // connect through SMB and copy the files - const char* documentFolder = SDL_GetUserFolder(SDL_FOLDER_DOCUMENTS); - char* diskPath = new char[strlen(documentFolder) + strlen("isle") + 1](); - strcpy(diskPath, documentFolder); - strcat(diskPath, "isle"); + MxString documentFolder = SDL_GetUserFolder(SDL_FOLDER_DOCUMENTS); + documentFolder += "isle"; - if (!SDL_GetPathInfo(diskPath, NULL)) { - SDL_CreateDirectory(diskPath); + if (!SDL_GetPathInfo(documentFolder.GetData(), NULL)) { + SDL_CreateDirectory(documentFolder.GetData()); } - iniparser_set(p_dictionary, "isle:diskpath", diskPath); - iniparser_set(p_dictionary, "isle:cdpath", diskPath); + iniparser_set(p_dictionary, "isle:diskpath", documentFolder.GetData()); + iniparser_set(p_dictionary, "isle:cdpath", documentFolder.GetData()); } diff --git a/ISLE/isleapp.cpp b/ISLE/isleapp.cpp index 0d3eee28..1b1af42a 100644 --- a/ISLE/isleapp.cpp +++ b/ISLE/isleapp.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #define SDL_MAIN_USE_CALLBACKS @@ -172,6 +173,7 @@ IsleApp::IsleApp() LegoOmni::CreateInstance(); + m_mediaPath = NULL; m_iniPath = NULL; m_maxLod = RealtimeView::GetUserMaxLOD(); m_maxAllowedExtras = m_islandQuality <= 1 ? 10 : 20; @@ -198,25 +200,11 @@ IsleApp::~IsleApp() MxOmni::DestroyInstance(); } - if (m_hdPath) { - delete[] m_hdPath; - } - - if (m_cdPath) { - delete[] m_cdPath; - } - - if (m_deviceId) { - delete[] m_deviceId; - } - - if (m_savePath) { - delete[] m_savePath; - } - - if (m_mediaPath) { - delete[] m_mediaPath; - } + SDL_free(m_hdPath); + SDL_free(m_cdPath); + SDL_free(m_deviceId); + SDL_free(m_savePath); + SDL_free(m_mediaPath); } // FUNCTION: ISLE 0x401260 @@ -1032,39 +1020,32 @@ bool IsleApp::LoadConfig() #ifdef IOS const char* prefPath = SDL_GetUserFolder(SDL_FOLDER_DOCUMENTS); #elif defined(ANDROID) - // SDL_GetAndroidExternalStoragePath() returns without a trailing / resulting in "filesisle.ini" :( - const char* androidPath = SDL_GetAndroidExternalStoragePath(); - size_t len = SDL_strlen(androidPath) + 2; - char* prefPath = new char[len]; - SDL_strlcpy(prefPath, androidPath, len); - SDL_strlcat(prefPath, "/", len); -#else - char* prefPath = SDL_GetPrefPath("isledecomp", "isle"); -#endif - char* iniConfig; - -#ifdef __EMSCRIPTEN__ + MxString androidPath = MxString(SDL_GetAndroidExternalStoragePath()) + "/"; + const char* prefPath = androidPath.GetData(); +#elif defined(EMSCRIPTEN) if (m_iniPath && !Emscripten_SetupConfig(m_iniPath)) { m_iniPath = NULL; } + char* prefPath = SDL_GetPrefPath("isledecomp", "isle"); +#else + char* prefPath = SDL_GetPrefPath("isledecomp", "isle"); #endif + MxString iniConfig; if (m_iniPath) { - iniConfig = new char[strlen(m_iniPath) + 1]; - strcpy(iniConfig, m_iniPath); + iniConfig = m_iniPath; } else if (prefPath) { - iniConfig = new char[strlen(prefPath) + strlen("isle.ini") + 1](); - strcat(iniConfig, prefPath); - strcat(iniConfig, "isle.ini"); + iniConfig = prefPath; + iniConfig += "isle.ini"; } else { - iniConfig = new char[strlen("isle.ini") + 1]; - strcpy(iniConfig, "isle.ini"); + iniConfig = "isle.ini"; } - SDL_Log("Reading configuration from \"%s\"", iniConfig); - dictionary* dict = iniparser_load(iniConfig); + SDL_Log("Reading configuration from \"%s\"", iniConfig.GetData()); + + dictionary* dict = iniparser_load(iniConfig.GetData()); // [library:config] // Load sane defaults if dictionary failed to load @@ -1075,20 +1056,20 @@ bool IsleApp::LoadConfig() } SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading sane defaults"); - FILE* iniFP = fopen(iniConfig, "wb"); + FILE* iniFP = fopen(iniConfig.GetData(), "wb"); if (!iniFP) { SDL_LogError( SDL_LOG_CATEGORY_APPLICATION, "Failed to write config at '%s': %s", - iniConfig, + iniConfig.GetData(), strerror(errno) ); return false; } char buf[32]; - dict = iniparser_load(iniConfig); + dict = iniparser_load(iniConfig.GetData()); iniparser_set(dict, "isle", NULL); iniparser_set(dict, "isle:diskpath", SDL_GetBasePath()); @@ -1143,8 +1124,9 @@ bool IsleApp::LoadConfig() #ifdef ANDROID Android_SetupDefaultConfigOverrides(dict); #endif + iniparser_dump_ini(dict, iniFP); - SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "New config written at '%s'", iniConfig); + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "New config written at '%s'", iniConfig.GetData()); fclose(iniFP); } @@ -1152,20 +1134,10 @@ bool IsleApp::LoadConfig() Emscripten_SetupDefaultConfigOverrides(dict); #endif - const char* hdPath = iniparser_getstring(dict, "isle:diskpath", SDL_GetBasePath()); - m_hdPath = new char[strlen(hdPath) + 1]; - strcpy(m_hdPath, hdPath); - MxOmni::SetHD(m_hdPath); - - const char* cdPath = iniparser_getstring(dict, "isle:cdpath", MxOmni::GetCD()); - m_cdPath = new char[strlen(cdPath) + 1]; - strcpy(m_cdPath, cdPath); - MxOmni::SetCD(m_cdPath); - - const char* mediaPath = iniparser_getstring(dict, "isle:mediapath", hdPath); - m_mediaPath = new char[strlen(mediaPath) + 1]; - strcpy(m_mediaPath, mediaPath); - + MxOmni::SetHD((m_hdPath = SDL_strdup(iniparser_getstring(dict, "isle:diskpath", SDL_GetBasePath())))); + MxOmni::SetCD((m_cdPath = SDL_strdup(iniparser_getstring(dict, "isle:cdpath", MxOmni::GetCD())))); + m_savePath = SDL_strdup(iniparser_getstring(dict, "isle:savepath", prefPath)); + m_mediaPath = SDL_strdup(iniparser_getstring(dict, "isle:mediapath", m_hdPath)); m_flipSurfaces = iniparser_getboolean(dict, "isle:Flip Surfaces", m_flipSurfaces); m_fullScreen = iniparser_getboolean(dict, "isle:Full Screen", m_fullScreen); m_exclusiveFullScreen = iniparser_getboolean(dict, "isle:Exclusive Full Screen", m_exclusiveFullScreen); @@ -1212,17 +1184,9 @@ bool IsleApp::LoadConfig() const char* deviceId = iniparser_getstring(dict, "isle:3D Device ID", NULL); if (deviceId != NULL) { - m_deviceId = new char[strlen(deviceId) + 1]; - strcpy(m_deviceId, deviceId); + m_deviceId = SDL_strdup(deviceId); } - // [library:config] - // The original game does not save any data if no savepath is given. - // Instead, we use SDLs prefPath as a default fallback and always save data. - const char* savePath = iniparser_getstring(dict, "isle:savepath", prefPath); - m_savePath = new char[strlen(savePath) + 1]; - strcpy(m_savePath, savePath); - #ifdef EXTENSIONS for (const char* key : Extensions::availableExtensions) { if (iniparser_getboolean(dict, key, 0)) { @@ -1242,11 +1206,12 @@ bool IsleApp::LoadConfig() #endif iniparser_freedict(dict); - delete[] iniConfig; -#ifndef IOS - SDL_free(prefPath); -#endif + [](auto path) { + if constexpr (std::is_same_v) { + SDL_free(path); + } + }(prefPath); return true; } diff --git a/ISLE/isleapp.h b/ISLE/isleapp.h index 470a5a72..0e0700e8 100644 --- a/ISLE/isleapp.h +++ b/ISLE/isleapp.h @@ -101,7 +101,7 @@ class IsleApp { MxFloat m_cursorSensitivity; void DisplayArgumentHelp(const char* p_execName); - char* m_iniPath; + const char* m_iniPath; MxFloat m_maxLod; MxU32 m_maxAllowedExtras; MxTransitionManager::TransitionType m_transitionType; diff --git a/LEGO1/omni/include/mxstring.h b/LEGO1/omni/include/mxstring.h index b4b25262..fad2e4ad 100644 --- a/LEGO1/omni/include/mxstring.h +++ b/LEGO1/omni/include/mxstring.h @@ -9,7 +9,7 @@ // SIZE 0x10 class MxString : public MxCore { public: - MxString(); + LEGO1_EXPORT MxString(); MxString(const MxString& p_str); LEGO1_EXPORT MxString(const char* p_str); MxString(const char* p_str, MxU16 p_maxlen);