Merge pull request #1376 from RadWolfie/settings-ini-hotfix

Settings class hotfix
This commit is contained in:
RadWolfie 2018-08-08 16:22:09 -05:00 committed by GitHub
commit 99fb143a04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 55 deletions

View File

@ -38,8 +38,7 @@
#include "Settings.hpp" #include "Settings.hpp"
#include "CxbxKrnl/Emu.h" #include "CxbxKrnl/Emu.h"
#include "CxbxKrnl/EmuShared.h" #include "CxbxKrnl/EmuShared.h"
#include <filesystem> #include <experimental/filesystem>
#include <fstream>
// TODO: Implement Qt support when real CPU emulation is available. // TODO: Implement Qt support when real CPU emulation is available.
#ifndef QT_VERSION // NOTE: Non-Qt will be using current directory for data #ifndef QT_VERSION // NOTE: Non-Qt will be using current directory for data
@ -167,7 +166,6 @@ bool Settings::Init()
#ifdef RETRO_API_VERSION // TODO: Change me to #ifndef QT_VERSION #ifdef RETRO_API_VERSION // TODO: Change me to #ifndef QT_VERSION
// Can only have one option without Qt. // Can only have one option without Qt.
saveFile = GenerateCurrentDirectoryStr(); saveFile = GenerateCurrentDirectoryStr();
saveFile.append(szSettings_settings_file);
#else // Only support for Qt compile build. #else // Only support for Qt compile build.
int iRet = MessageBox(nullptr, szSettings_save_user_option_message, "Cxbx-Reloaded", MB_YESNOCANCEL | MB_ICONQUESTION); int iRet = MessageBox(nullptr, szSettings_save_user_option_message, "Cxbx-Reloaded", MB_YESNOCANCEL | MB_ICONQUESTION);
@ -198,16 +196,8 @@ bool Settings::Init()
#endif #endif
saveFile.append(szSettings_settings_file); saveFile.append(szSettings_settings_file);
// If the config file does not exists, create a blank one // Call LoadConfig, this will load the config, applying defaults for any missing fields
// We can't call Save here because that overrides default values with false! bRet = LoadConfig();
if (!std::experimental::filesystem::exists(saveFile)) {
std::ofstream ofs(saveFile, std::ofstream::out);
ofs << "\n";
ofs.close();
}
// Call LoadUserConfig, this will load the config, applying defaults for any missing fields
bRet = LoadUserConfig();
if (!bRet) { if (!bRet) {
MessageBox(nullptr, szSettings_setup_error, "Cxbx-Reloaded", MB_OK); MessageBox(nullptr, szSettings_setup_error, "Cxbx-Reloaded", MB_OK);
@ -245,9 +235,6 @@ bool Settings::LoadUserConfig()
bool Settings::LoadFile(std::string file_path) bool Settings::LoadFile(std::string file_path)
{ {
bool bRet;
const char* si_data;
int iStatus;
std::list<CSimpleIniA::Entry> si_list; std::list<CSimpleIniA::Entry> si_list;
std::list<CSimpleIniA::Entry>::iterator si_list_iterator; std::list<CSimpleIniA::Entry>::iterator si_list_iterator;
@ -258,6 +245,17 @@ bool Settings::LoadFile(std::string file_path)
} }
m_file_path = file_path; m_file_path = file_path;
return LoadConfig();
}
bool Settings::LoadConfig()
{
bool bRet;
const char* si_data;
int iStatus;
std::list<CSimpleIniA::Entry> si_list;
std::list<CSimpleIniA::Entry>::iterator si_list_iterator;
// ==== GUI Begin =========== // ==== GUI Begin ===========
m_gui.CxbxDebugMode = (DebugMode)m_si.GetLongValue(section_gui, sect_gui_keys.CxbxDebugMode, /*Default=*/DM_NONE); m_gui.CxbxDebugMode = (DebugMode)m_si.GetLongValue(section_gui, sect_gui_keys.CxbxDebugMode, /*Default=*/DM_NONE);
@ -607,56 +605,47 @@ void Settings::SyncToEmulator()
g_EmuShared->SetStorageLocation(GetDataLocation().c_str()); g_EmuShared->SetStorageLocation(GetDataLocation().c_str());
} }
void Settings::Verify() void verifyDebugFilePath(DebugMode& debug_mode, std::string& file_path)
{ {
// Prevent using an incorrect path from the registry if the debug folders have been moved // Prevent using an incorrect path from the registry if the debug folders have been moved
char szDebugPath[MAX_PATH]; std::string szDebugPath;
char szDebugName[MAX_PATH]; std::string szDebugName;
if (m_gui.CxbxDebugMode == DM_FILE) { if (debug_mode == DM_FILE) {
if(m_gui.szCxbxDebugFile.size() == 0) { if(file_path.size() == 0 || file_path.size() >= MAX_PATH) {
m_gui.CxbxDebugMode = DM_NONE; debug_mode = DM_NONE;
} }
else { else {
std::strcpy(szDebugName, strrchr(m_gui.szCxbxDebugFile.c_str(), '\\')); szDebugName = file_path.substr(file_path.find_last_of("\\/"), std::string::npos);
if(m_gui.szCxbxDebugFile.size() < std::strlen(szDebugName)) { if(file_path.size() < szDebugName.size()) {
m_gui.szCxbxDebugFile = ""; file_path = "";
m_gui.CxbxDebugMode = DM_NONE; debug_mode = DM_NONE;
} }
else { else {
std::strncpy(szDebugPath, m_gui.szCxbxDebugFile.c_str(), m_gui.szCxbxDebugFile.size() - std::strlen(szDebugName)); szDebugPath = file_path.substr(0, file_path.size() - szDebugName.size());
if(std::experimental::filesystem::exists(szDebugPath) == false) { if(std::experimental::filesystem::exists(szDebugPath) == false) {
m_gui.szCxbxDebugFile = ""; file_path = "";
m_gui.CxbxDebugMode = DM_NONE; debug_mode = DM_NONE;
} }
} }
} }
} }
}
if (m_core.KrnlDebugMode == DM_FILE) { void Settings::Verify()
{
std::string szKrnlDebug = m_core.szKrnlDebug; // Temporary placeholder until m_core.szKrnlDebug is replace to std::string.
if(std::strlen(m_core.szKrnlDebug) == 0) { verifyDebugFilePath(m_gui.CxbxDebugMode, m_gui.szCxbxDebugFile);
m_core.KrnlDebugMode = DM_NONE;
}
else {
std::strcpy(szDebugName, strrchr(m_core.szKrnlDebug, '\\'));
if(std::strlen(m_core.szKrnlDebug) < std::strlen(szDebugName)) { verifyDebugFilePath(m_core.KrnlDebugMode, szKrnlDebug);
memset(m_core.szKrnlDebug, '\0', MAX_PATH);
m_core.KrnlDebugMode = DM_NONE;
}
else {
std::strncpy(szDebugPath, m_core.szKrnlDebug, std::strlen(m_core.szKrnlDebug) - std::strlen(szDebugName));
if(std::experimental::filesystem::exists(szDebugPath) == false) { // Set to null string once if contain invalid path.
memset(m_core.szKrnlDebug, '\0', MAX_PATH); if (m_core.szKrnlDebug[0] != '\0' && szKrnlDebug.size() == 0) {
m_core.KrnlDebugMode = DM_NONE; std::memset(m_core.szKrnlDebug, 0, MAX_PATH);
}
}
}
} }
} }

View File

@ -123,6 +123,7 @@ public:
bool Init(); bool Init();
bool LoadUserConfig(); bool LoadUserConfig();
bool LoadFile(std::string file_path); bool LoadFile(std::string file_path);
bool LoadConfig();
bool Save(std::string file_path = ""); bool Save(std::string file_path = "");
void Delete(); void Delete();
void SyncToEmulator(); void SyncToEmulator();

View File

@ -157,13 +157,7 @@ extern volatile bool g_bPrintfOn;
#define CxbxSetThreadName(Name) #define CxbxSetThreadName(Name)
#endif #endif
// NOTE: The reason filesystem is here is for force compress "experimental" namespace // NOTE: #include <filesystem> didn't work plus C++ 17 is still using experimental filesystem.
// to work with same old and new filesystem's functions. Untested, yet should work fine. #include <experimental/filesystem>
#include <filesystem>
#ifdef _EXPERIMENTAL_FILESYSTEM_
namespace std {
namespace filesystem = std::experimental::filesystem;
}
#endif
#endif #endif