Skip to content

Commit

Permalink
Disallow factory cloning (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzzer authored Jul 25, 2024
1 parent 10fb154 commit 9aead65
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
9 changes: 9 additions & 0 deletions extension/natives/entityfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ cell_t CPluginEntityFactory_Ctor(IPluginContext * context, const cell_t * params
IPluginFunction *postConstructor = context->GetFunctionById(params[2]);
IPluginFunction *onRemove = context->GetFunctionById(params[3]);

if (params[0] >= 4 && params[4] != 0) {
// This factory needs to be created for another plugin
HandleError error = HandleError_None;
plugin = plsys->PluginFromHandle(static_cast<Handle_t>(params[4]), &error);
if (error != HandleError_None || plugin == nullptr) {
return context->ThrowNativeError("Could not create entity factory with the given plugin handle %d (error %d)", params[4], error);
}
}

CPluginEntityFactory* factory = new CPluginEntityFactory(plugin, classname, postConstructor, onRemove);
return factory->m_Handle;
}
Expand Down
5 changes: 5 additions & 0 deletions extension/pluginentityfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ bool CPluginEntityFactories::Init( IGameConfig* config, char* error, size_t maxl
m_hookIds.push_back(SH_ADD_HOOK(IEntityFactoryDictionary, GetCannonicalName, factoryDictionary, SH_MEMBER(this, &CPluginEntityFactories::Hook_GetCannonicalName), false));
}

HandleAccess security;
handlesys->InitAccessDefaults(nullptr, &security);
// Disallow this handle type from being cloned
security.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;

m_FactoryType = g_PluginEntityFactoryHandle = handlesys->CreateType( "PluginEntityFactory", this, 0, nullptr, nullptr, myself->GetIdentity(), nullptr );
if ( !m_FactoryType )
{
Expand Down
2 changes: 1 addition & 1 deletion product.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.2
1.11.3
4 changes: 3 additions & 1 deletion scripting/include/cbasenpc/entityfactory.inc
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ methodmap CEntityFactory < Handle
* Perform any needed cleanup for your entity here.
* @param error Invalid handle, classname is NULL or empty, or out of
* memory.
* @param plugin Optional Handle to another plugin to mark as the owner.
* If no owner is passed, the owner is the calling plugin.
*/
public native CEntityFactory(const char[] classname, CEntityFactoryPostConstructor postConstructor=INVALID_FUNCTION, CEntityFactoryOnRemoveCallback onRemove=INVALID_FUNCTION);
public native CEntityFactory(const char[] classname, CEntityFactoryPostConstructor postConstructor=INVALID_FUNCTION, CEntityFactoryOnRemoveCallback onRemove=INVALID_FUNCTION, Handle plugin = INVALID_HANDLE);

/**
* Instructs the factory to use CBaseNPC (NextBot) as the base class.
Expand Down

0 comments on commit 9aead65

Please sign in to comment.