Skip to content

Commit

Permalink
Fix gamedata based entity factory (#37)
Browse files Browse the repository at this point in the history
Due to the nature of member function ptrs on linux, the extension would auto offset the class object ptr allocated by the game. Leading to misaligned memory.

On the topic of misaligned memory, we weren't really guarding against it. So just in the event the engine changes how memory is allocated, or that plugin provides an object size that doesn't allow for 4 bytes aligned memory, we fix that before allocating anything.
  • Loading branch information
Kenzzer authored Mar 14, 2023
1 parent b5503ae commit b0f9d4b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
9 changes: 6 additions & 3 deletions extension/pluginentityfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@ IServerNetworkable* CPluginEntityFactory::RecursiveCreate(const char* classname,

size_t entitySize = pCreatingFactory->GetEntitySize();

static MCall<void> ctorCall;

IEntityFactory *pBaseFactory = GetBaseFactory();
if (pBaseFactory)
{
Expand Down Expand Up @@ -929,7 +931,7 @@ IServerNetworkable* CPluginEntityFactory::RecursiveCreate(const char* classname,
{
bIsInstantiating = true;

CBaseEntityHack* pEnt = (CBaseEntityHack*)engine->PvAllocEntPrivateData(entitySize);
CBaseEntityHack* pEnt = (CBaseEntityHack*)engine->PvAllocEntPrivateData(entitySize + ((4 - (entitySize % 4)) % 4));
CBaseEntityHack::CBaseEntity_Ctor(pEnt, m_Derive.m_bBaseEntityServerOnly);
pEnt->PostConstructor(classname);
pNet = pEnt->NetworkProp();
Expand All @@ -938,8 +940,9 @@ IServerNetworkable* CPluginEntityFactory::RecursiveCreate(const char* classname,
{
bIsInstantiating = true;

CBaseEntityHack* pEnt = (CBaseEntityHack*)engine->PvAllocEntPrivateData(entitySize);
(pEnt->*(m_Derive.m_pConstructorFunc))();
CBaseEntityHack* pEnt = (CBaseEntityHack*)engine->PvAllocEntPrivateData(entitySize + ((4 - (entitySize % 4)) % 4));
ctorCall.Init((void*)m_Derive.m_pConstructorFunc);
ctorCall(pEnt);
pEnt->PostConstructor(classname);
pNet = pEnt->NetworkProp();
}
Expand Down
4 changes: 1 addition & 3 deletions extension/pluginentityfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ class CPluginEntityFactory : public IEntityFactory, public IEntityDataMapContain
DERIVETYPE_MAX
};

typedef void* (CBaseEntity::*RawEntityConstructor)(void);

struct PluginEntityFactoryDeriveInfo_t
{
PluginEntityFactoryDeriveType_t m_DeriveFrom;
Expand All @@ -182,7 +180,7 @@ class CPluginEntityFactory : public IEntityFactory, public IEntityDataMapContain
{
PluginEntityFactoryBaseClass_t m_BaseType;
Handle_t m_BaseFactoryHandle;
RawEntityConstructor m_pConstructorFunc;
uintptr_t m_pConstructorFunc;
};

std::string m_iBaseClassname;
Expand Down
2 changes: 1 addition & 1 deletion product.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.3
1.8.4

0 comments on commit b0f9d4b

Please sign in to comment.