Only save custom GOL data if it changed

This commit is contained in:
Tamás Bálint Misius 2023-01-21 22:01:42 +01:00
parent 4f0c365e05
commit a438584871
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2

View File

@ -310,11 +310,13 @@ void GameModel::BuildMenus()
auto customGOLTypes = prefs.Get("CustomGOL.Types", std::vector<ByteString>{}); auto customGOLTypes = prefs.Get("CustomGOL.Types", std::vector<ByteString>{});
std::vector<ByteString> validatedCustomLifeTypes; std::vector<ByteString> validatedCustomLifeTypes;
std::vector<Simulation::CustomGOLData> newCustomGol; std::vector<Simulation::CustomGOLData> newCustomGol;
bool removedAny = false;
for (auto gol : customGOLTypes) for (auto gol : customGOLTypes)
{ {
auto parts = gol.FromUtf8().PartitionBy(' '); auto parts = gol.FromUtf8().PartitionBy(' ');
if (parts.size() != 4) if (parts.size() != 4)
{ {
removedAny = true;
continue; continue;
} }
Simulation::CustomGOLData gd; Simulation::CustomGOLData gd;
@ -324,11 +326,13 @@ void GameModel::BuildMenus()
auto &colour2String = parts[3]; auto &colour2String = parts[3];
if (!ValidateGOLName(gd.nameString)) if (!ValidateGOLName(gd.nameString))
{ {
removedAny = true;
continue; continue;
} }
gd.rule = ParseGOLString(gd.ruleString); gd.rule = ParseGOLString(gd.ruleString);
if (gd.rule == -1) if (gd.rule == -1)
{ {
removedAny = true;
continue; continue;
} }
try try
@ -338,13 +342,17 @@ void GameModel::BuildMenus()
} }
catch (std::exception &) catch (std::exception &)
{ {
removedAny = true;
continue; continue;
} }
newCustomGol.push_back(gd); newCustomGol.push_back(gd);
validatedCustomLifeTypes.push_back(gol); validatedCustomLifeTypes.push_back(gol);
} }
if (removedAny)
{
// All custom rules that fail validation will be removed // All custom rules that fail validation will be removed
prefs.Set("CustomGOL.Types", validatedCustomLifeTypes); prefs.Set("CustomGOL.Types", validatedCustomLifeTypes);
}
for (auto &gd : newCustomGol) for (auto &gd : newCustomGol)
{ {
Tool * tempTool = new ElementTool(PT_LIFE|PMAPID(gd.rule), gd.nameString, "Custom GOL type: " + gd.ruleString, PIXR(gd.colour1), PIXG(gd.colour1), PIXB(gd.colour1), "DEFAULT_PT_LIFECUST_"+gd.nameString.ToAscii(), NULL); Tool * tempTool = new ElementTool(PT_LIFE|PMAPID(gd.rule), gd.nameString, "Custom GOL type: " + gd.ruleString, PIXR(gd.colour1), PIXG(gd.colour1), PIXB(gd.colour1), "DEFAULT_PT_LIFECUST_"+gd.nameString.ToAscii(), NULL);
@ -1654,7 +1662,10 @@ bool GameModel::RemoveCustomGOLType(const ByteString &identifier)
else else
newCustomGOLTypes.push_back(gol); newCustomGOLTypes.push_back(gol);
} }
if (removedAny)
{
prefs.Set("CustomGOL.Types", newCustomGOLTypes); prefs.Set("CustomGOL.Types", newCustomGOLTypes);
}
BuildMenus(); BuildMenus();
return removedAny; return removedAny;
} }