GUI: improve system detection algorithm (maybe)

This commit is contained in:
tildearrow 2022-12-22 13:10:08 -05:00
parent c8612b7e8a
commit eab679e9d3
5 changed files with 52 additions and 8 deletions

View file

@ -54,6 +54,10 @@ String DivConfig::toBase64() {
return taEncodeBase64(data); return taEncodeBase64(data);
} }
const std::map<String,String>& DivConfig::configMap() {
return conf;
}
void DivConfig::parseLine(const char* line) { void DivConfig::parseLine(const char* line) {
String key=""; String key="";
String value=""; String value="";
@ -171,6 +175,15 @@ String DivConfig::getString(String key, String fallback) const {
return fallback; return fallback;
} }
bool DivConfig::has(String key) {
try {
conf.at(key);
} catch (std::out_of_range& e) {
return false;
}
return true;
}
void DivConfig::set(String key, bool value) { void DivConfig::set(String key, bool value) {
if (value) { if (value) {
conf[key]="true"; conf[key]="true";

View file

@ -35,6 +35,9 @@ class DivConfig {
String toBase64(); String toBase64();
bool save(const char* path); bool save(const char* path);
// get the map
const std::map<String,String>& configMap();
// get a config value // get a config value
bool getBool(String key, bool fallback) const; bool getBool(String key, bool fallback) const;
int getInt(String key, int fallback) const; int getInt(String key, int fallback) const;
@ -42,6 +45,9 @@ class DivConfig {
double getDouble(String key, double fallback) const; double getDouble(String key, double fallback) const;
String getString(String key, String fallback) const; String getString(String key, String fallback) const;
// check for existence
bool has(String key);
// set a config value // set a config value
void set(String key, bool value); void set(String key, bool value);
void set(String key, int value); void set(String key, int value);

View file

@ -592,12 +592,14 @@ void FurnaceGUI::updateWindowTitle() {
void FurnaceGUI::autoDetectSystem() { void FurnaceGUI::autoDetectSystem() {
std::map<DivSystem,int> sysCountMap; std::map<DivSystem,int> sysCountMap;
std::map<DivSystem,DivConfig> sysConfMap;
for (int i=0; i<e->song.systemLen; i++) { for (int i=0; i<e->song.systemLen; i++) {
try { try {
sysCountMap.at(e->song.system[i])++; sysCountMap.at(e->song.system[i])++;
} catch (std::exception& ex) { } catch (std::exception& ex) {
sysCountMap[e->song.system[i]]=1; sysCountMap[e->song.system[i]]=1;
} }
sysConfMap[e->song.system[i]]=e->song.systemFlags[i];
} }
logV("sysCountMap:"); logV("sysCountMap:");
@ -607,16 +609,20 @@ void FurnaceGUI::autoDetectSystem() {
bool isMatch=false; bool isMatch=false;
std::map<DivSystem,int> defCountMap; std::map<DivSystem,int> defCountMap;
std::map<DivSystem,DivConfig> defConfMap;
for (FurnaceGUISysCategory& i: sysCategories) { for (FurnaceGUISysCategory& i: sysCategories) {
for (FurnaceGUISysDef& j: i.systems) { for (FurnaceGUISysDef& j: i.systems) {
defCountMap.clear(); defCountMap.clear();
for (size_t k=0; k<j.definition.size(); k+=4) { defConfMap.clear();
if (j.definition[k]==0) break; for (FurnaceGUISysDefChip& k: j.orig) {
try { try {
defCountMap.at((DivSystem)j.definition[k])++; defCountMap.at(k.sys)++;
} catch (std::exception& ex) { } catch (std::exception& ex) {
defCountMap[(DivSystem)j.definition[k]]=1; defCountMap[k.sys]=1;
} }
DivConfig dc;
dc.loadFromMemory(k.flags);
defConfMap[k.sys]=dc;
} }
if (defCountMap.size()!=sysCountMap.size()) continue; if (defCountMap.size()!=sysCountMap.size()) continue;
isMatch=true; isMatch=true;
@ -630,6 +636,18 @@ void FurnaceGUI::autoDetectSystem() {
isMatch=false; isMatch=false;
break; break;
} }
DivConfig& sysDC=sysConfMap.at(k.first);
for (std::pair<String,String> l: defConfMap.at(k.first).configMap()) {
if (!sysDC.has(l.first)) {
isMatch=false;
break;
}
if (sysDC.getString(l.first,"")!=l.second) {
isMatch=false;
break;
}
}
if (!isMatch) break;
} catch (std::exception& ex) { } catch (std::exception& ex) {
isMatch=false; isMatch=false;
break; break;
@ -1825,6 +1843,7 @@ void FurnaceGUI::openFileDialog(FurnaceGUIFileDialogs type) {
int FurnaceGUI::save(String path, int dmfVersion) { int FurnaceGUI::save(String path, int dmfVersion) {
SafeWriter* w; SafeWriter* w;
logD("saving file...");
if (dmfVersion) { if (dmfVersion) {
if (dmfVersion<24) dmfVersion=24; if (dmfVersion<24) dmfVersion=24;
w=e->saveDMF(dmfVersion); w=e->saveDMF(dmfVersion);
@ -1833,11 +1852,14 @@ int FurnaceGUI::save(String path, int dmfVersion) {
} }
if (w==NULL) { if (w==NULL) {
lastError=e->getLastError(); lastError=e->getLastError();
logE("couldn't save! %s",lastError);
return 3; return 3;
} }
logV("opening file for writing...");
FILE* outFile=ps_fopen(path.c_str(),"wb"); FILE* outFile=ps_fopen(path.c_str(),"wb");
if (outFile==NULL) { if (outFile==NULL) {
lastError=strerror(errno); lastError=strerror(errno);
logE("couldn't save! %s",lastError);
w->finish(); w->finish();
return 1; return 1;
} }
@ -1918,6 +1940,7 @@ int FurnaceGUI::save(String path, int dmfVersion) {
showWarning(e->getWarnings(),GUI_WARN_GENERIC); showWarning(e->getWarnings(),GUI_WARN_GENERIC);
} }
pushRecentFile(path); pushRecentFile(path);
logD("save complete.");
return 0; return 0;
} }
@ -4070,7 +4093,6 @@ bool FurnaceGUI::loop() {
} }
break; break;
case GUI_FILE_SAVE: { case GUI_FILE_SAVE: {
logD("saving: %s",copyOfName.c_str());
bool saveWasSuccessful=true; bool saveWasSuccessful=true;
if (save(copyOfName,0)>0) { if (save(copyOfName,0)>0) {
showError(fmt::sprintf("Error while saving file! (%s)",lastError)); showError(fmt::sprintf("Error while saving file! (%s)",lastError));
@ -5057,6 +5079,7 @@ bool FurnaceGUI::loop() {
} }
logD("saving backup..."); logD("saving backup...");
SafeWriter* w=e->saveFur(true); SafeWriter* w=e->saveFur(true);
logV("writing file...");
if (w!=NULL) { if (w!=NULL) {
FILE* outFile=ps_fopen(backupPath.c_str(),"wb"); FILE* outFile=ps_fopen(backupPath.c_str(),"wb");
@ -5071,6 +5094,7 @@ bool FurnaceGUI::loop() {
w->finish(); w->finish();
} }
} }
logD("backup saved.");
backupTimer=30.0; backupTimer=30.0;
return true; return true;
}); });

View file

@ -948,6 +948,7 @@ struct FurnaceGUISysDef {
const char* name; const char* name;
const char* extra; const char* extra;
String definition; String definition;
std::vector<FurnaceGUISysDefChip> orig;
FurnaceGUISysDef(const char* n, std::initializer_list<FurnaceGUISysDefChip> def, const char* e=NULL); FurnaceGUISysDef(const char* n, std::initializer_list<FurnaceGUISysDefChip> def, const char* e=NULL);
}; };

View file

@ -330,7 +330,7 @@ void FurnaceGUI::initSystemPresets() {
); );
ENTRY( ENTRY(
"MSX", { "MSX", {
CH(DIV_SYSTEM_AY8910, 64, 0, "chipType=1") CH(DIV_SYSTEM_AY8910, 64, 0, "clockSel=0\nchipType=1")
} }
); );
ENTRY( ENTRY(
@ -2611,9 +2611,9 @@ void FurnaceGUI::initSystemPresets() {
FurnaceGUISysDef::FurnaceGUISysDef(const char* n, std::initializer_list<FurnaceGUISysDefChip> def, const char* e): FurnaceGUISysDef::FurnaceGUISysDef(const char* n, std::initializer_list<FurnaceGUISysDefChip> def, const char* e):
name(n), name(n),
extra(e) { extra(e) {
std::vector<FurnaceGUISysDefChip> uncompiled=def; orig=def;
int index=0; int index=0;
for (FurnaceGUISysDefChip& i: uncompiled) { for (FurnaceGUISysDefChip& i: orig) {
definition+=fmt::sprintf( definition+=fmt::sprintf(
"id%d=%d\nvol%d=%d\npan%d=%d\nflags%d=%s\n", "id%d=%d\nvol%d=%d\npan%d=%d\nflags%d=%s\n",
index, index,