Fix nullptr deref when flooding deco

Broken in ff4500620e, where the code that constructs DecorationTools got moved from GameModel::BuildMenus to GameModel::InitTools. BuildMenus used to run fairly late, while InitTools is run fairly early, before GameModel's view member is initialized. This wasn't a problem before f52e047037, since which DecorationTools access Renderer through GameView, rather than directly.
This commit is contained in:
Tamás Bálint Misius
2024-11-04 18:03:57 +01:00
parent 875f92b2a6
commit 8620ea13a4
5 changed files with 13 additions and 8 deletions

View File

@@ -83,12 +83,11 @@ GameController::GameController():
HasDone(false)
{
gameView = new GameView();
gameModel = new GameModel();
gameModel = new GameModel(gameView); // mvc is a joke
gameModel->BuildQuickOptionMenu(this);
gameView->AttachController(this);
gameModel->AddObserver(gameView);
gameModel->view = gameView; // mvc is a joke
gameView->SetDebugHUD(GlobalPrefs::Ref().Get("Renderer.DebugMode", false));

View File

@@ -50,7 +50,7 @@ HistoryEntry::~HistoryEntry()
// so the default dtor for ~HistoryEntry cannot be generated.
}
GameModel::GameModel():
GameModel::GameModel(GameView *newView):
activeMenu(SC_POWDERS),
currentBrush(0),
currentUser(0, ""),
@@ -61,7 +61,8 @@ GameModel::GameModel():
colour(255, 0, 0, 255),
edgeMode(EDGE_VOID),
ambientAirTemp(R_TEMP + 273.15f),
decoSpace(DECOSPACE_SRGB)
decoSpace(DECOSPACE_SRGB),
view(newView)
{
sim = new Simulation();
sim->useLuaCallbacks = true;

View File

@@ -133,8 +133,10 @@ private:
std::optional<int> queuedVote;
bool threadedRendering = false;
GameView *view;
public:
GameModel();
GameModel(GameView *newView);
~GameModel();
void Tick();
@@ -302,5 +304,8 @@ public:
void BeforeSim();
void AfterSim();
GameView *view = nullptr;
GameView *GetView() const
{
return view;
}
};

View File

@@ -23,7 +23,7 @@ void SampleTool::Draw(Simulation * sim, Brush const &brush, ui::Point position)
{
if(gameModel.GetColourSelectorVisibility())
{
pixel colour = gameModel.view->GetPixelUnderMouse();
pixel colour = gameModel.GetView()->GetPixelUnderMouse();
gameModel.SetColourSelectorColour(RGB<uint8_t>::Unpack(colour).WithAlpha(0xFF));
}
else

View File

@@ -831,7 +831,7 @@ static int floodDeco(lua_State *L)
auto *lsi = GetLSI();
// hilariously broken, intersects with console and all Lua graphics
auto &rendererFrame = lsi->gameModel->view->GetRendererFrame();
auto &rendererFrame = lsi->gameModel->GetView()->GetRendererFrame();
auto loc = RGB<uint8_t>::Unpack(rendererFrame[{ x, y }]);
lsi->sim->ApplyDecorationFill(rendererFrame, x, y, r, g, b, a, loc.Red, loc.Green, loc.Blue);
return 0;