From b04ddaa20a48d56657c0d0b58a3ebad656a4a42b Mon Sep 17 00:00:00 2001 From: tildearrow Date: Fri, 29 Mar 2024 19:56:16 -0500 Subject: [PATCH] preparations for user presets now we need save --- src/gui/gui.cpp | 2 +- src/gui/gui.h | 12 +-- src/gui/newSong.cpp | 6 +- src/gui/presets.cpp | 183 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 189 insertions(+), 14 deletions(-) diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 0bb5093df..343cbd151 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -648,7 +648,7 @@ void FurnaceGUI::autoDetectSystemIter(std::vector& category, b it->second++; } DivConfig dc; - dc.loadFromMemory(k.flags); + dc.loadFromMemory(k.flags.c_str()); defConfMap[k.sys]=dc; } if (defCountMap.size()==sysCountMap.size()) { diff --git a/src/gui/gui.h b/src/gui/gui.h index 6bf282eaf..f73f7fed0 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -1231,22 +1231,24 @@ struct Gradient2D { struct FurnaceGUISysDefChip { DivSystem sys; - float vol, pan; - const char* flags; - FurnaceGUISysDefChip(DivSystem s, float v, float p, const char* f): + float vol, pan, panFR; + String flags; + FurnaceGUISysDefChip(DivSystem s, float v, float p, const char* f, float pf=0.0): sys(s), vol(v), pan(p), + panFR(pf), flags(f) {} }; struct FurnaceGUISysDef { - const char* name; - const char* extra; + String name; + String extra; String definition; std::vector orig; std::vector subDefs; FurnaceGUISysDef(const char* n, std::initializer_list def, const char* e=NULL); + FurnaceGUISysDef(const char* n, const char* def, DivEngine* e); }; struct FurnaceGUISysCategory { diff --git a/src/gui/newSong.cpp b/src/gui/newSong.cpp index 79ea9e9a8..213ba3aab 100644 --- a/src/gui/newSong.cpp +++ b/src/gui/newSong.cpp @@ -53,7 +53,7 @@ void FurnaceGUI::drawSysDefs(std::vector& category, bool& acce } if (ImGui::IsItemHovered()) isHovered=true; } else if (i.subDefs.empty()) { - ImGui::TextUnformatted(i.name); + ImGui::TextUnformatted(i.name.c_str()); if (ImGui::IsItemHovered()) isHovered=true; } if (treeNode) { @@ -134,10 +134,10 @@ void FurnaceGUI::drawNewSong() { } } std::sort(newSongSearchResults.begin(),newSongSearchResults.end(),[](const FurnaceGUISysDef& a, const FurnaceGUISysDef& b) { - return strcmp(a.name,b.name)<0; + return strcmp(a.name.c_str(),b.name.c_str())<0; }); auto lastItem=std::unique(newSongSearchResults.begin(),newSongSearchResults.end(),[](const FurnaceGUISysDef& a, const FurnaceGUISysDef& b) { - return strcmp(a.name,b.name)==0; + return a.name==b.name; }); newSongSearchResults.erase(lastItem,newSongSearchResults.end()); } diff --git a/src/gui/presets.cpp b/src/gui/presets.cpp index 5157957f3..9cb370b4f 100644 --- a/src/gui/presets.cpp +++ b/src/gui/presets.cpp @@ -19,6 +19,7 @@ #include "gui.h" #include "../baseutils.h" +#include "../fileutils.h" #include // add system configurations here. @@ -3100,7 +3101,7 @@ void FurnaceGUI::initSystemPresets() { FurnaceGUISysDef::FurnaceGUISysDef(const char* n, std::initializer_list def, const char* e): name(n), - extra(e) { + extra((e==NULL)?"":e) { orig=def; int index=0; for (FurnaceGUISysDefChip& i: orig) { @@ -3117,16 +3118,188 @@ FurnaceGUISysDef::FurnaceGUISysDef(const char* n, std::initializer_listsystemFromFileFur(id),vol,pan,flags.c_str(),panFR)); + } + // extract extra + extra=conf.toString(); +} + // functions for loading/saving user presets -bool loadUserPresets(bool redundancy) { +#ifdef _WIN32 +#define PRESETS_FILE "\\presets.cfg" +#else +#define PRESETS_FILE "/presets.cfg" +#endif + +#define REDUNDANCY_NUM_ATTEMPTS 5 +#define CHECK_BUF_SIZE 8192 + +bool FurnaceGUI::loadUserPresets(bool redundancy) { + String path=e->getConfigPath()+PRESETS_FILE; + String line; + logD("opening user presets: %s",path); + + FILE* f=NULL; + + if (redundancy) { + unsigned char* readBuf=new unsigned char[CHECK_BUF_SIZE]; + size_t readBufLen=0; + for (int i=0; i0) { + line=fmt::sprintf("%s.%d",path,i); + } else { + line=path; + } + logV("trying: %s",line); + + // try to open config + f=ps_fopen(line.c_str(),"rb"); + // check whether we could open it + if (f==NULL) { + logV("fopen(): %s",strerror(errno)); + continue; + } + + // check whether there's something + while (!feof(f)) { + readBufLen=fread(readBuf,1,CHECK_BUF_SIZE,f); + if (ferror(f)) { + logV("fread(): %s",strerror(errno)); + break; + } + + for (size_t j=0; jsystems.push_back(FurnaceGUISysDef(key.c_str(),value.c_str(),e)); + } + } + + fclose(f); return true; } -bool saveUserPresets(bool redundancy) { +bool FurnaceGUI::saveUserPresets(bool redundancy) { return true; -} \ No newline at end of file +}