From f13fe3d36b100893a61460665fa0fde241cdd1d9 Mon Sep 17 00:00:00 2001 From: catsoften Date: Thu, 29 Jul 2021 11:20:13 -0400 Subject: [PATCH] Prevent property tool from being used with bad values (#791) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tamás Bálint Misius --- src/gui/game/PropertyTool.cpp | 26 ++++++++++++++++++-------- src/gui/game/Tool.h | 1 + 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/gui/game/PropertyTool.cpp b/src/gui/game/PropertyTool.cpp index a52b95185..ed660a319 100644 --- a/src/gui/game/PropertyTool.cpp +++ b/src/gui/game/PropertyTool.cpp @@ -53,7 +53,7 @@ public: Simulation *sim; std::vector properties; PropertyWindow(PropertyTool *tool_, Simulation *sim); - void SetProperty(); + void SetProperty(bool warn); void OnDraw() override; void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override; void OnTryExit(ExitMethod method) override; @@ -81,7 +81,7 @@ sim(sim_) if (textField->GetText().length()) { CloseActiveWindow(); - SetProperty(); + SetProperty(true); SelfDestruct(); } } }); @@ -103,14 +103,17 @@ sim(sim_) textField->SetText(Client::Ref().GetPrefString("Prop.Value", "")); AddComponent(textField); FocusComponent(textField); + SetProperty(false); MakeActiveWindow(); } -void PropertyWindow::SetProperty() +void PropertyWindow::SetProperty(bool warn) { + tool->validProperty = false; if(property->GetOption().second!=-1 && textField->GetText().length() > 0) { + tool->validProperty = true; String value = textField->GetText().ToUpper(); try { switch(properties[property->GetOption().second].Type) @@ -174,7 +177,9 @@ void PropertyWindow::SetProperty() if (properties[property->GetOption().second].Name == "type" && (v < 0 || v >= PT_NUM || !sim->elements[v].Enabled)) { - new ErrorMessage("Could not set property", "Invalid particle type"); + tool->validProperty = false; + if (warn) + new ErrorMessage("Could not set property", "Invalid particle type"); return; } @@ -214,14 +219,18 @@ void PropertyWindow::SetProperty() } break; default: - new ErrorMessage("Could not set property", "Invalid property"); + tool->validProperty = false; + if (warn) + new ErrorMessage("Could not set property", "Invalid property"); return; } tool->propOffset = properties[property->GetOption().second].Offset; tool->propType = properties[property->GetOption().second].Type; tool->changeType = properties[property->GetOption().second].Name == "type"; } catch (const std::exception& ex) { - new ErrorMessage("Could not set property", "Invalid value provided"); + tool->validProperty = false; + if (warn) + new ErrorMessage("Could not set property", "Invalid value provided"); return; } Client::Ref().SetPref("Prop.Type", property->GetOption().second); @@ -258,7 +267,7 @@ void PropertyTool::OpenWindow(Simulation *sim) void PropertyTool::SetProperty(Simulation *sim, ui::Point position) { - if(position.X<0 || position.X>XRES || position.Y<0 || position.Y>YRES) + if(position.X<0 || position.X>XRES || position.Y<0 || position.Y>YRES || !validProperty) return; int i = sim->pmap[position.Y][position.X]; if(!i) @@ -379,5 +388,6 @@ void PropertyTool::DrawRect(Simulation *sim, Brush *cBrush, ui::Point position, void PropertyTool::DrawFill(Simulation *sim, Brush *cBrush, ui::Point position) { - sim->flood_prop(position.X, position.Y, propOffset, propValue, propType); + if (validProperty) + sim->flood_prop(position.X, position.Y, propOffset, propValue, propType); } diff --git a/src/gui/game/Tool.h b/src/gui/game/Tool.h index 382d9ba90..c74883083 100644 --- a/src/gui/game/Tool.h +++ b/src/gui/game/Tool.h @@ -92,6 +92,7 @@ public: PropertyValue propValue; bool changeType; size_t propOffset; + bool validProperty; void OpenWindow(Simulation *sim); virtual ~PropertyTool() {}