Skip to content

Commit

Permalink
Add source2 convar/concommand support (#203)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicholas Hastings <[email protected]>
  • Loading branch information
GAMMACASE and psychonic authored Feb 15, 2025
1 parent 4fe356b commit a0ef306
Show file tree
Hide file tree
Showing 20 changed files with 398 additions and 103 deletions.
1 change: 1 addition & 0 deletions AMBuildScript
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class MMSConfig(object):
'-Wall',
'-Werror',
'-Wno-uninitialized',
'-Wno-sign-compare',
'-Wno-unused',
'-Wno-switch',
'-msse',
Expand Down
8 changes: 3 additions & 5 deletions core/AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ for sdk_target in MMS.sdk_targets:
binary.sources += [
'provider/source2/provider_source2.cpp',
]
binary.custom = [builder.tools.Protoc(protoc = sdk_target.protoc, sources = [
os.path.join(sdk['path'], 'common', 'network_connection.proto'),
])]
else:
binary.sources += [
'provider/source/provider_source.cpp',
'provider/source/provider_source_console.cpp',
'vsp_bridge.cpp'
]

if sdk['name'] == 'cs2':
binary.custom = [builder.tools.Protoc(protoc = sdk_target.protoc, sources = [
os.path.join(sdk['path'], 'common', 'network_connection.proto'),
])]

if cxx.target.arch == 'x86':
binary.sources += ['sourcehook/sourcehook_hookmangen_x86.cpp']
Expand Down
49 changes: 49 additions & 0 deletions core/ISmmAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,19 @@
class CGlobalVars;
struct edict_t;
class ConCommandBase;
typedef ConCommandBase ProviderConVar;
typedef ConCommandBase ProviderConCommand;
#else
#include <eiface.h>

#if defined META_IS_SOURCE2
typedef ConVarRefAbstract ProviderConVar;
typedef ConCommandRef ProviderConCommand;
class ConCommandBase;
#else
typedef ConCommandBase ProviderConVar;
typedef ConCommandBase ProviderConCommand;
#endif
#endif

#include <ISmmPlugin.h>
Expand Down Expand Up @@ -130,6 +141,8 @@ namespace SourceMM

/**
* @brief Registers a ConCommandBase.
*
* @deprecated since 2.1
*
* @param plugin Parent plugin API pointer.
* @param pCommand ConCommandBase to register.
Expand All @@ -139,6 +152,8 @@ namespace SourceMM

/**
* @brief Unregisters a ConCommandBase.
*
* @deprecated since 2.1
*
* @param plugin Parent plugin API pointer.
* @param pCommand ConCommandBase to unlink.
Expand Down Expand Up @@ -400,6 +415,40 @@ namespace SourceMM
size_t maxlength,
const char *format,
va_list ap) =0;

/**
* @brief Registers a ConCommand.
*
* @param plugin Parent plugin API pointer.
* @param pCommand ConCommand to register.
* @return True if successful, false otherwise.
*/
virtual bool RegisterConCommand(ISmmPlugin *plugin, ProviderConCommand *pCommand) =0;

/**
* @brief Registers a ConVar.
*
* @param plugin Parent plugin API pointer.
* @param pCvar ConVar to register.
* @return True if successful, false otherwise.
*/
virtual bool RegisterConVar(ISmmPlugin *plugin, ProviderConVar *pCvar) =0;

/**
* @brief Unregisters a ConCommand.
*
* @param plugin Parent plugin API pointer.
* @param pCommand ConCommand to unlink.
*/
virtual void UnregisterConCommand(ISmmPlugin *plugin, ProviderConCommand *pCommand) =0;

/**
* @brief Unregisters a ConVar.
*
* @param plugin Parent plugin API pointer.
* @param pCvar ConVar to unlink.
*/
virtual void UnregisterConVar(ISmmPlugin *plugin, ProviderConVar *pCvar) =0;
};
}

Expand Down
48 changes: 46 additions & 2 deletions core/ISmmPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ namespace SourceMM
* @brief Called when Metamod:Source is about to remove a concommand or
* convar. This can also be called if ISmmAPI::UnregisterConCmdBase is
* used by a plugin.
*
* @deprecated since PLAPI 17
*
* @param id Id of the plugin that created the concommand or
* convar.
Expand All @@ -428,6 +430,30 @@ namespace SourceMM
virtual void OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand)
{
}

/**
* @brief Called when Metamod:Source is about to remove a concommand.
* This can also be called if ISmmAPI::UnregisterConCommand is
* used by a plugin.
*
* @param id Id of the plugin that created the concommand.
* @param pCommand Pointer to concommand that is being removed.
*/
virtual void OnUnlinkConCommand(PluginId id, ProviderConCommand *pCommand)
{
}

/**
* @brief Called when Metamod:Source is about to remove a convar.
* This can also be called if ISmmAPI::UnregisterConVar is
* used by a plugin.
*
* @param id Id of the plugin that created the convar.
* @param pVar Pointer to convar that is being removed.
*/
virtual void OnUnlinkConVar(PluginId id, ProviderConVar *pVar)
{
}
};
}

Expand Down Expand Up @@ -492,12 +518,30 @@ using namespace SourceMM;
g_PLID = id;

#define META_LOG g_SMAPI->LogMsg
#define META_CONPRINT g_SMAPI->ConPrint
#define META_CONPRINTF g_SMAPI->ConPrintf

#if defined META_IS_SOURCE2
#define META_UNREGCMD(name) g_SMAPI->UnregisterConCommand( g_PLAPI, name##_command )
#define META_UNREGCVAR(var) g_SMAPI->UnregisterConVar( g_PLAPI, var )

#define META_CONVAR_REGISTER(flags) ConVar_Register( flags, \
[]( ConVarRefAbstract *ref ) { g_SMAPI->RegisterConVar( g_PLAPI, ref ); }, \
[]( ConCommandRef *ref ) { g_SMAPI->RegisterConCommand( g_PLAPI, ref ); } \
)
#else
// @deprecated since 2.1
#define META_REGCMD(name) g_SMAPI->RegisterConCommandBase(g_PLAPI, name##_command)
// @deprecated since 2.1
#define META_REGCVAR(var) g_SMAPI->RegisterConCommandBase(g_PLAPI, var)
// @deprecated since 2.1
#define META_UNREGCMD(name) g_SMAPI->UnregisterConCommandBase(g_PLAPI, name##_command)
// @deprecated since 2.1
#define META_UNREGCVAR(var) g_SMAPI->UnregisterConCommandBase(g_PLAPI, var)
#define META_CONPRINT g_SMAPI->ConPrint
#define META_CONPRINTF g_SMAPI->ConPrintf

#define META_REGBASECMD(var) (var->IsCommand() ? g_SMAPI->RegisterConCommand(g_PLAPI, var) : g_SMAPI->RegisterConVar(g_PLAPI, var))
#define META_UNREGBASECMD(var) (var->IsCommand() ? g_SMAPI->UnregisterConCommand(g_PLAPI, var) : g_SMAPI->UnregisterConVar(g_PLAPI, var))
#endif

/* Probably should use this up above someday */
#define CONCMD_VARNAME(name) name##_command
Expand Down
2 changes: 1 addition & 1 deletion core/ISmmPluginExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
#define SOURCE_ENGINE_MCV 27 /**< Military Conflict: Vietnam */
#define SOURCE_ENGINE_CS2 28 /**< Counter-Strike 2 */

#define METAMOD_PLAPI_VERSION 16 /**< Version of this header file */
#define METAMOD_PLAPI_VERSION 17 /**< Version of this header file */
#define METAMOD_PLAPI_NAME "ISmmPlugin" /**< Name of the plugin interface */

namespace SourceMM
Expand Down
117 changes: 95 additions & 22 deletions core/metamod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,33 +919,48 @@ const char *MetamodSource::GetVDFDir()
bool MetamodSource::RegisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
{
if (provider->IsConCommandBaseACommand(pCommand))
{
g_PluginMngr.AddPluginCmd(plugin, pCommand);
}
return RegisterConCommand(plugin, (ProviderConCommand *)pCommand);
else
{
g_PluginMngr.AddPluginCvar(plugin, pCommand);
}

return provider->RegisterConCommandBase(pCommand);
return RegisterConVar(plugin, (ProviderConVar *)pCommand);
}

void MetamodSource::UnregisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
{
if (provider->IsConCommandBaseACommand(pCommand))
{
g_PluginMngr.RemovePluginCmd(plugin, pCommand);
}
UnregisterConCommand(plugin, (ProviderConCommand *)pCommand);
else
{
g_PluginMngr.RemovePluginCvar(plugin, pCommand);
}
UnregisterConVar(plugin, (ProviderConVar *)pCommand);
}

bool MetamodSource::RegisterConCommand(ISmmPlugin *plugin, ProviderConCommand *pCommand)
{
g_PluginMngr.AddPluginCmd(plugin, pCommand);
return provider->RegisterConCommand(pCommand);
}

bool MetamodSource::RegisterConVar(ISmmPlugin *plugin, ProviderConVar *pVar)
{
g_PluginMngr.AddPluginCvar(plugin, pVar);
return provider->RegisterConVar(pVar);
}

void MetamodSource::UnregisterConCommand(ISmmPlugin *plugin, ProviderConCommand *pCommand)
{
CPluginManager::CPlugin *pOrig = g_PluginMngr.FindByAPI(plugin);

g_PluginMngr.RemovePluginCmd(plugin, pCommand);
UnregisterConCommand(pOrig ? pOrig->m_Id : 0, pCommand);
}

void MetamodSource::UnregisterConVar(ISmmPlugin *plugin, ProviderConVar *pVar)
{
CPluginManager::CPlugin *pOrig = g_PluginMngr.FindByAPI(plugin);
UnregisterConCommandBase(pOrig ? pOrig->m_Id : 0, pCommand);

g_PluginMngr.RemovePluginCvar(plugin, pVar);
UnregisterConVar(pOrig ? pOrig->m_Id : 0, pVar);
}

void MetamodSource::UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand)
void MetamodSource::UnregisterConCommand(PluginId id, ProviderConCommand *pCommand)
{
PluginIter iter;
CPluginManager::CPlugin *pPlugin;
Expand All @@ -963,16 +978,74 @@ void MetamodSource::UnregisterConCommandBase(PluginId id, ConCommandBase *pComma
{
continue;
}
for (event=pPlugin->m_Events.begin();
event!=pPlugin->m_Events.end();
event++)

if(pPlugin->m_API->GetApiVersion() < 17)
{
pML = (*event);
pML->OnUnlinkConCommandBase(id, pCommand);
for(event=pPlugin->m_Events.begin();
event != pPlugin->m_Events.end();
event++)
{
pML = (*event);
pML->OnUnlinkConCommandBase(id, (ConCommandBase *)pCommand);
}
}
else
{
for(event=pPlugin->m_Events.begin();
event != pPlugin->m_Events.end();
event++)
{
pML = (*event);
pML->OnUnlinkConCommand(id, pCommand);
}
}
}

return provider->UnregisterConCommand(pCommand);
}

void MetamodSource::UnregisterConVar(PluginId id, ProviderConVar *pVar)
{
PluginIter iter;
CPluginManager::CPlugin *pPlugin;
List<IMetamodListener *>::iterator event;
IMetamodListener *pML;
for(iter=g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++)
{
pPlugin = (*iter);
if(pPlugin->m_Status < Pl_Paused)
{
continue;
}
/* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
if(pPlugin->m_API->GetApiVersion() < 12)
{
continue;
}

if(pPlugin->m_API->GetApiVersion() < 17)
{
for(event=pPlugin->m_Events.begin();
event != pPlugin->m_Events.end();
event++)
{
pML = (*event);
pML->OnUnlinkConCommandBase(id, (ConCommandBase *)pVar);
}
}
else
{
for(event=pPlugin->m_Events.begin();
event != pPlugin->m_Events.end();
event++)
{
pML = (*event);
pML->OnUnlinkConVar(id, pVar);
}
}
}

return provider->UnregisterConCommandBase(pCommand);
return provider->UnregisterConVar(pVar);
}

int MetamodSource::GetUserMessageCount()
Expand Down
9 changes: 7 additions & 2 deletions core/metamod.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ using namespace SourceMM;
#define SOURCEMM_VERSION SVN_FILE_VERSION_STRING
#define SOURCEMM_DATE __DATE__
#define METAMOD_API_MAJOR 2 /* increase this on a breaking change */
#define METAMOD_API_MINOR 0 /* increase this on a non-breaking API change */
#define METAMOD_API_MINOR 1 /* increase this on a non-breaking API change */

class MetamodSource : public ISmmAPI
{
Expand All @@ -62,6 +62,10 @@ class MetamodSource : public ISmmAPI
CGlobalVars *GetCGlobals() override;
bool RegisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand) override;
void UnregisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand) override;
bool RegisterConCommand(ISmmPlugin *plugin, ProviderConCommand *pCommand) override;
bool RegisterConVar(ISmmPlugin *plugin, ProviderConVar *pCvar) override;
void UnregisterConCommand(ISmmPlugin *plugin, ProviderConCommand *pCommand) override;
void UnregisterConVar(ISmmPlugin *plugin, ProviderConVar *pCvar) override;
void ConPrint(const char *str) override;
void ConPrintf(const char *fmt, ...) override;
void GetApiVersions(int &major, int &minor, int &plvers, int &plmin) override;
Expand Down Expand Up @@ -91,7 +95,8 @@ class MetamodSource : public ISmmAPI
const char *GetGameBinaryPath();
const char *GetPluginsFile();
const char *GetVDFDir();
void UnregisterConCommandBase(PluginId id, ConCommandBase *pCommand);
void UnregisterConCommand(PluginId id, ProviderConCommand *pCommand);
void UnregisterConVar(PluginId id, ProviderConVar *pVar);
void NotifyVSPListening(IServerPluginCallbacks *callbacks, int version);
const char* GetGameDLLInterfaceName() const;
void SetGameDLLInfo(CreateInterfaceFn serverFactory, const char *pGameDllIfaceName, int version, bool loaded);
Expand Down
4 changes: 2 additions & 2 deletions core/metamod_console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ bool Command_Meta(IMetamodSourceCommandInfo *info)
else
{
CONMSG("Console commands for %s:\n", pl->m_API->GetName());
List<ConCommandBase *>::iterator ci;
List<ProviderConCommand *>::iterator ci;
size_t count = 0;

for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
Expand Down Expand Up @@ -277,7 +277,7 @@ bool Command_Meta(IMetamodSourceCommandInfo *info)
else
{
CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
List<ConCommandBase *>::iterator ci;
List<ProviderConVar *>::iterator ci;
size_t count = 0;

for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
Expand Down
Loading

0 comments on commit a0ef306

Please sign in to comment.