From 8e024bcec0063df2889abebae2a11752f0b5b5b6 Mon Sep 17 00:00:00 2001 From: Ion Agorria Date: Fri, 7 Jun 2024 15:59:11 +0200 Subject: [PATCH] Fix BuildingInstaller race condition --- Source/UserInterface/Installer.cpp | 62 ++++++++++++++++++------------ Source/UserInterface/Installer.h | 5 +-- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/Source/UserInterface/Installer.cpp b/Source/UserInterface/Installer.cpp index c48af4dd..814bb7b6 100644 --- a/Source/UserInterface/Installer.cpp +++ b/Source/UserInterface/Installer.cpp @@ -35,6 +35,7 @@ terBuildingInstaller::~terBuildingInstaller() void terBuildingInstaller::Clear() { + MTAuto auto_lock(&lock); CancelObject(); delete[] BaseBuff; @@ -73,24 +74,33 @@ void terBuildingInstaller::InitObject(const AttributeBase* attr) void terBuildingInstaller::ConstructObject(terPlayer* player) { - if(player && valid() && Attribute->ID != UNIT_ATTRIBUTE_FRAME){ - if(player->frame()) - player->frame()->commandOutcoming(UnitCommand(COMMAND_ID_BUILDING_START, Vect3f(Position.x,Position.y,Angle), Attribute->ID, COMMAND_SELECTED_MODE_NEGATIVE)); - Clear(); - } + if (player && valid()) { + MTAuto auto_lock(&lock); + if (Attribute && Attribute->ID != UNIT_ATTRIBUTE_FRAME) { + if(player->frame()) { + player->frame()->commandOutcoming(UnitCommand( + COMMAND_ID_BUILDING_START, + Vect3f(Position.x, Position.y, Angle), Attribute->ID, + COMMAND_SELECTED_MODE_NEGATIVE + )); + } + Clear(); + } + } } void terBuildingInstaller::CancelObject() { - Attribute = 0; + MTAuto auto_lock(&lock); + Attribute = nullptr; - if(ObjectPoint){ + if (ObjectPoint) { ObjectPoint->Release(); - ObjectPoint = 0; + ObjectPoint = nullptr; } - valid_ = 0; - visible_ = 0; + valid_ = false; + visible_ = false; connection_icon_->quant(); } @@ -144,13 +154,14 @@ class terScanGroundLineBuffOp void terBuildingInstaller::SetBuildPosition(const Vect3f& position,float angle, terPlayer* player) { - valid_ = 1; - visible_ = 0; + MTAuto auto_lock(&lock); + valid_ = true; + visible_ = false; old_build_position=position; old_build_angle=angle; old_build_player=player; - if(ObjectPoint){ - visible_ = 1; + if (ObjectPoint && Attribute) { + visible_ = true; Position = to3D(position, vMap.hZeroPlast - Attribute->logicObjectBound.min.z); Angle = angle; @@ -195,17 +206,16 @@ void terBuildingInstaller::SetBuildPosition(const Vect3f& position,float angle, connection_icon_->quant(); - if(Attribute->ID != UNIT_ATTRIBUTE_FRAME && player){ + if (Attribute->ID != UNIT_ATTRIBUTE_FRAME && player) { bool connected = false; - if(!Attribute->ConnectionRadius){ - MTAuto lock(universe()->EnergyRegionLocker()); + if(Attribute->ConnectionRadius <= 0){ + MTAuto elock(universe()->EnergyRegionLocker()); GenShapeLineOp op; scanPolyByLineOp(&points[0], points.size(), op); connected = player->energyColumn().intersected(op.shape()); //if(!connected) // valid_ = 0; - } - else{ + } else { CUNITS_LOCK(player); const UnitList& unit_list=player->units(); UnitList::const_iterator ui; @@ -216,15 +226,15 @@ void terBuildingInstaller::SetBuildPosition(const Vect3f& position,float angle, } if(!connected){ - valid_ = 0; + valid_ = false; Vect3f pos = position; pos.z += Attribute->boundRadius*Attribute->iconDistanceFactor; connection_icon_->show(pos); } } - } - else - valid_ = 0; + } else { + valid_ = false; + } } void terBuildingInstaller::InitTexture() @@ -252,9 +262,10 @@ void terBuildingInstaller::InitTexture() void terBuildingInstaller::SetBuildPosition(const Vect2f& mousePos, terPlayer* player) { + MTAuto auto_lock(&lock); valid_ = 1; visible_ = 0; - if(ObjectPoint){ + if (ObjectPoint && Attribute) { Vect3f v; Vect3f pos,dir; terCamera->GetCamera()->GetWorldRay(pos_set = mousePos, pos, dir); @@ -287,7 +298,8 @@ void terBuildingInstaller::ChangeBuildAngle(float dA, terPlayer* player) void terBuildingInstaller::ShowCircle() { - if(ObjectPoint){ + MTAuto auto_lock(&lock); + if (ObjectPoint && Attribute) { if(Attribute->ZeroLayerRadius) terCircleShowGraph(Position, Attribute->ZeroLayerRadius, circleColors.zeroLayerRadius); if(Attribute->ConnectionRadius){ diff --git a/Source/UserInterface/Installer.h b/Source/UserInterface/Installer.h index 9570a087..4524a33f 100644 --- a/Source/UserInterface/Installer.h +++ b/Source/UserInterface/Installer.h @@ -26,13 +26,12 @@ class terBuildingInstaller //For AI void SetBuildPosition(const Vect3f& position, float angle, terPlayer* player); - terUnitAttributeID attributeID() const { return Attribute ? static_cast(Attribute->ID) : UNIT_ATTRIBUTE_NONE; } - bool inited() const { return ObjectPoint != 0; } bool valid() const { return valid_; } bool buildingInArea() const { return buildingInArea_; } private: + MTSection lock; const AttributeBase* Attribute = nullptr; Vect3f Position; float Angle = 0.0f; @@ -66,4 +65,4 @@ class terBuildingInstaller bool checkScriptInstructions(); }; -#endif //_INSTALLER_H_ \ No newline at end of file +#endif //_INSTALLER_H_