From 6f2c9535bcfe7ad78cafaf15c1b2a5fb3787d5cf Mon Sep 17 00:00:00 2001 From: tildearrow Date: Wed, 5 Apr 2023 19:08:04 -0500 Subject: [PATCH] config rotation + redundancy for layout also load default layout if it fails to load --- extern/imgui_patched/imgui.cpp | 63 ++++++++++++++++++++++++++++++++-- extern/imgui_patched/imgui.h | 4 +-- src/gui/gui.cpp | 9 ++--- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/extern/imgui_patched/imgui.cpp b/extern/imgui_patched/imgui.cpp index 19316ae2..4d57efc0 100644 --- a/extern/imgui_patched/imgui.cpp +++ b/extern/imgui_patched/imgui.cpp @@ -837,6 +837,8 @@ CODE #include // intptr_t #endif +#include "../../src/fileutils.h" + // [Windows] On non-Visual Studio compilers, we default to IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS unless explicitly enabled #if defined(_WIN32) && !defined(_MSC_VER) && !defined(IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) #define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS @@ -12457,10 +12459,42 @@ void ImGui::ClearIniSettings() g.SettingsHandlers[handler_n].ClearAllFn(&g, &g.SettingsHandlers[handler_n]); } -bool ImGui::LoadIniSettingsFromDisk(const char* ini_filename) +bool ImGui::LoadIniSettingsFromDisk(const char* ini_filename, bool redundancy) { size_t file_data_size = 0; - char* file_data = (char*)ImFileLoadToMemory(ini_filename, "rb", &file_data_size); + char* file_data = NULL; + + if (redundancy) { + char fileName[4096]; + + for (int i=0; i<5; i++) { + bool viable=false; + if (i>0) { + snprintf(fileName,4095,"%s.%d",ini_filename,i); + } else { + strncpy(fileName,ini_filename,4095); + } + file_data=(char*)ImFileLoadToMemory(fileName, "rb", &file_data_size); + if (!file_data) continue; + + for (size_t j=0; j=0; i--) { + if (i>0) { + snprintf(oldPath,4095,"%s.%d",ini_filename,i); + } else { + strncpy(oldPath,ini_filename,4095); + } + snprintf(newPath,4095,"%s.%d",ini_filename,i+1); + + if (i>=4) { + deleteFile(oldPath); + } else { + moveFiles(oldPath,newPath); + } + } + } + } + size_t ini_data_size = 0; const char* ini_data = SaveIniSettingsToMemory(&ini_data_size); ImFileHandle f = ImFileOpen(ini_filename, "wt"); @@ -12554,6 +12610,7 @@ bool ImGui::SaveIniSettingsToDisk(const char* ini_filename) bool areEqual=ImFileWrite(ini_data, sizeof(char), ini_data_size, f)==ini_data_size; IM_ASSERT_USER_ERROR(areEqual, "ImFileWrite failed to write file!"); ImFileClose(f); + if (!areEqual && redundancy) deleteFile(ini_filename); return areEqual; } diff --git a/extern/imgui_patched/imgui.h b/extern/imgui_patched/imgui.h index ac6b77dc..7aa62aa6 100644 --- a/extern/imgui_patched/imgui.h +++ b/extern/imgui_patched/imgui.h @@ -950,9 +950,9 @@ namespace ImGui // - The disk functions are automatically called if io.IniFilename != NULL (default is "imgui.ini"). // - Set io.IniFilename to NULL to load/save manually. Read io.WantSaveIniSettings description about handling .ini saving manually. // - Important: default value "imgui.ini" is relative to current working dir! Most apps will want to lock this to an absolute path (e.g. same path as executables). - IMGUI_API bool LoadIniSettingsFromDisk(const char* ini_filename); // call after CreateContext() and before the first call to NewFrame(). NewFrame() automatically calls LoadIniSettingsFromDisk(io.IniFilename). + IMGUI_API bool LoadIniSettingsFromDisk(const char* ini_filename, bool redundancy=false); // call after CreateContext() and before the first call to NewFrame(). NewFrame() automatically calls LoadIniSettingsFromDisk(io.IniFilename). IMGUI_API void LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size=0); // call after CreateContext() and before the first call to NewFrame() to provide .ini data from your own data source. - IMGUI_API bool SaveIniSettingsToDisk(const char* ini_filename); // this is automatically called (if io.IniFilename is not empty) a few seconds after any modification that should be reflected in the .ini file (and also by DestroyContext). + IMGUI_API bool SaveIniSettingsToDisk(const char* ini_filename, bool redundancy=false); // this is automatically called (if io.IniFilename is not empty) a few seconds after any modification that should be reflected in the .ini file (and also by DestroyContext). IMGUI_API const char* SaveIniSettingsToMemory(size_t* out_ini_size = NULL); // return a zero-terminated string with the .ini data which you can save by your own mean. call when io.WantSaveIniSettings is set, then save data by your own mean and clear io.WantSaveIniSettings. // Debug Utilities diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 53792d95..2c6ecaeb 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -2786,7 +2786,7 @@ void FurnaceGUI::editOptions(bool topMenu) { void FurnaceGUI::toggleMobileUI(bool enable, bool force) { if (mobileUI!=enable || force) { if (!mobileUI && enable) { - if (!ImGui::SaveIniSettingsToDisk(finalLayoutPath)) { + if (!ImGui::SaveIniSettingsToDisk(finalLayoutPath,true)) { reportError(fmt::sprintf("could NOT save layout! %s",strerror(errno))); } } @@ -2798,8 +2798,9 @@ void FurnaceGUI::toggleMobileUI(bool enable, bool force) { fileDialog->mobileUI=true; } else { ImGui::GetIO().IniFilename=NULL; - if (!ImGui::LoadIniSettingsFromDisk(finalLayoutPath)) { + if (!ImGui::LoadIniSettingsFromDisk(finalLayoutPath,true)) { reportError(fmt::sprintf("could NOT load layout! %s",strerror(errno))); + ImGui::LoadIniSettingsFromMemory(defaultLayout); } ImGui::GetIO().ConfigFlags&=~ImGuiConfigFlags_InertialScrollEnable; ImGui::GetIO().ConfigFlags&=~ImGuiConfigFlags_NoHoverColors; @@ -5029,7 +5030,7 @@ bool FurnaceGUI::loop() { ImGui::CloseCurrentPopup(); if (!mobileUI) { ImGui::LoadIniSettingsFromMemory(defaultLayout); - if (!ImGui::SaveIniSettingsToDisk(finalLayoutPath)) { + if (!ImGui::SaveIniSettingsToDisk(finalLayoutPath,true)) { reportError(fmt::sprintf("could NOT save layout! %s",strerror(errno))); } } @@ -5913,7 +5914,7 @@ bool FurnaceGUI::init() { void FurnaceGUI::commitState() { if (!mobileUI) { - if (!ImGui::SaveIniSettingsToDisk(finalLayoutPath)) { + if (!ImGui::SaveIniSettingsToDisk(finalLayoutPath,true)) { reportError(fmt::sprintf("could NOT save layout! %s",strerror(errno))); } }