Skip to content

Commit

Permalink
Fix crashes caused by createBuilding with engineRequestModel (#3330)
Browse files Browse the repository at this point in the history
Bugs fixed:
1) Crash occurs when a resource that uses createBuilding with engineRequestModel is stopped
2) Destroyed building leaves grass
  • Loading branch information
TheNormalnij authored Apr 3, 2024
1 parent 229389a commit 6245a68
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Client/game_sa/CPoolsSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "CWorldSA.h"
#include "CKeyGenSA.h"
#include "CFileLoaderSA.h"
#include "CPtrNodeSingleListSA.h"

extern CGameSA* pGame;

Expand Down Expand Up @@ -410,6 +411,14 @@ void CPoolsSA::RemoveBuilding(CBuilding* pBuilding)
// Remove building from world
pGame->GetWorld()->Remove(pInterface, CBuildingPool_Destructor);

// Remove building from cover list
CPtrNodeSingleListSAInterface<CBuildingSAInterface>* coverList = reinterpret_cast<CPtrNodeSingleListSAInterface<CBuildingSAInterface>*>(0xC1A2B8);
coverList->RemoveItem(pInterface);

// Remove plant
using CPlantColEntry_Remove = CEntitySAInterface* (*)(CEntitySAInterface*);
((CPlantColEntry_Remove)0x5DBEF0)(pInterface);

// Remove col reference
auto modelInfo = pGame->GetModelInfo(pBuilding->GetModelIndex());
modelInfo->RemoveColRef();
Expand Down
45 changes: 45 additions & 0 deletions Client/game_sa/CPtrNodeSingleListSA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CPtrNodeSingleListSA.cpp
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once

template <class T>
struct CPtrNodeSingleLink
{
T* pItem;
CPtrNodeSingleLink* pNext;
};

template <class T>
class CPtrNodeSingleListSAInterface
{
public:
void RemoveItem(T* item);
void RemoveAllItems();

private:
CPtrNodeSingleLink<T>* m_pList;
};

template <class T>
void CPtrNodeSingleListSAInterface<T>::RemoveItem(T* item)
{
using CPtrNodeSingleList_RemoveItem_t = void(__thiscall*)(CPtrNodeSingleListSAInterface<T> * pLinkList, void* item);
((CPtrNodeSingleList_RemoveItem_t)0x533610)(this, item);
}

template <class T>
void CPtrNodeSingleListSAInterface<T>::RemoveAllItems()
{
while (m_pList)
{
RemoveItem(m_pList->pItem);
}
}

0 comments on commit 6245a68

Please sign in to comment.