Skip to content

Commit

Permalink
Add engineSetPoolCapacity support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Jan 29, 2025
1 parent 53ad82a commit 36f88bc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
47 changes: 47 additions & 0 deletions Client/game_sa/CDynamicPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,53 @@ class CDynamicPool
return size;
}

bool SetCapacity(std::size_t newSize) {
if (newSize == 0)
return false;

std::size_t currentSize = GetCapacity();

if (currentSize == newSize)
return false;
else if (currentSize < newSize)
{
// Grown
while (currentSize < newSize)
{
try
{
auto* nextPart = AllocateNewPart();
currentSize += nextPart->GetCapacity();
}
catch (const std::bad_alloc& ex)
{
return false;
}
}
}
else
{
// Shrink
while (true)
{
auto* part = m_poolParts.back().get();
if (part->GetUsedSize() != 0)
return false;

currentSize -= part->GetCapacity();
if (currentSize < newSize)
return false;

m_poolParts.pop_back();

if (currentSize == newSize)
return true;
}
}

return true;
}

private:
CDynamicPoolPart<PoolObjT>* AllocateNewPart()
{
Expand Down
2 changes: 1 addition & 1 deletion Client/game_sa/CPoolsSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CPoolsSA : public CPools
CBuildingsPool& GetBuildingsPool() noexcept override { return m_BuildingsPool; };
CDummyPool& GetDummyPool() noexcept { return m_DummyPool; };
CTxdPool& GetTxdPool() noexcept { return m_TxdPool; };
CPtrNodeSingleLinkPoolSA& GetPtrNodeSingleLinkPool() noexcept { return m_PtrNodeSingleLinkPool; };
CPtrNodeSingleLinkPool& GetPtrNodeSingleLinkPool() noexcept override { return m_PtrNodeSingleLinkPool; };

private:
// Pools
Expand Down
9 changes: 6 additions & 3 deletions Client/game_sa/CPtrNodeSingleLinkPoolSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
#include "CPoolSAInterface.h"
#include "CDynamicPool.h"
#include "CPtrNodeSingleListSA.h"
#include <game/CPtrNodeSingleLinkPool.h>

class CPtrNodeSingleLinkPoolSA
class CPtrNodeSingleLinkPoolSA final : public CPtrNodeSingleLinkPool
{
public:
using pool_item_t = CPtrNodeSingleLink<void*>;
using pool_t = CDynamicPool<pool_item_t, PoolGrownByHalfStrategy<MAX_POINTER_SINGLE_LINKS>>;

CPtrNodeSingleLinkPoolSA();

int GetCapacity() const { return m_customPool->GetCapacity(); }
int GetUsedSize() const { return m_customPool->GetUsedSize(); }
std::size_t GetCapacity() const override { return m_customPool->GetCapacity(); }
std::size_t GetUsedSize() const override { return m_customPool->GetUsedSize(); }

bool Resize(std::size_t newSize) override { return m_customPool->SetCapacity(newSize); };

static auto* GetPoolInstance() { return m_customPool; }
static void StaticSetHooks();
Expand Down
5 changes: 5 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <game/CColPoint.h>
#include <game/CObjectGroupPhysicalProperties.h>
#include <game/CStreaming.h>
#include <game/CPtrNodeSingleLinkPool.h>
#include <lua/CLuaFunctionParser.h>
#include "CLuaEngineDefs.h"

Expand Down Expand Up @@ -2505,6 +2506,10 @@ bool CLuaEngineDefs::EngineSetPoolCapacity(lua_State* luaVM, ePools pool, size_t
{
return m_pBuildingManager->SetPoolCapacity(newSize);
}
case ePools::POINTER_SINGLE_LINK_POOL:
{
return g_pGame->GetPools()->GetPtrNodeSingleLinkPool().Resize(newSize);
}
default:
throw std::invalid_argument("Can not change this pool capacity");
}
Expand Down
2 changes: 2 additions & 0 deletions Client/sdk/game/CPools.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "CBuildingsPool.h"
#include "CDummyPool.h"
#include "CTxdPool.h"
#include "CPtrNodeSingleLinkPool.h"

class CClientEntity;
class CEntity;
Expand Down Expand Up @@ -111,4 +112,5 @@ class CPools
virtual CBuildingsPool& GetBuildingsPool() noexcept = 0;
virtual CDummyPool& GetDummyPool() noexcept = 0;
virtual CTxdPool& GetTxdPool() noexcept = 0;
virtual CPtrNodeSingleLinkPool& GetPtrNodeSingleLinkPool() noexcept = 0;
};
19 changes: 19 additions & 0 deletions Client/sdk/game/CPtrNodeSingleLinkPool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: sdk/game/CPtrNodeSingleLinkPool.h
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once

class CPtrNodeSingleLinkPool
{
public:
virtual bool Resize(std::size_t size) = 0;
virtual std::size_t GetCapacity() const = 0;
virtual std::size_t GetUsedSize() const = 0;
};

0 comments on commit 36f88bc

Please sign in to comment.