config rotation + redundancy for layout

also load default layout if it fails to load
This commit is contained in:
tildearrow 2023-04-05 19:08:04 -05:00
parent e16fdf0626
commit 6f2c9535bc
3 changed files with 67 additions and 9 deletions

View File

@ -837,6 +837,8 @@ CODE
#include <stdint.h> // 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<file_data_size; j++) {
if (file_data[j]!='\r' && file_data[j]!='\n' && file_data[j]!=' ') {
viable=true;
break;
}
}
if (!viable) {
IM_FREE(file_data);
file_data=NULL;
file_data_size=0;
} else {
break;
}
}
} else {
file_data=(char*)ImFileLoadToMemory(ini_filename, "rb", &file_data_size);
}
if (!file_data)
return false;
LoadIniSettingsFromMemory(file_data, (size_t)file_data_size);
@ -12539,13 +12573,35 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size)
g.SettingsHandlers[handler_n].ApplyAllFn(&g, &g.SettingsHandlers[handler_n]);
}
bool ImGui::SaveIniSettingsToDisk(const char* ini_filename)
bool ImGui::SaveIniSettingsToDisk(const char* ini_filename, bool redundancy)
{
ImGuiContext& g = *GImGui;
g.SettingsDirtyTimer = 0.0f;
if (!ini_filename)
return false;
if (redundancy) {
char oldPath[4096];
char newPath[4096];
if (fileExists(ini_filename)==1) {
for (int i=4; i>=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;
}

View File

@ -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

View File

@ -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)));
}
}