Skip to content

Commit

Permalink
begin the convar api work
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzzer committed Oct 12, 2023
1 parent 36cc78a commit fbcc565
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 12 deletions.
66 changes: 66 additions & 0 deletions core/IPluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,72 @@
*/

#include <ISmmPluginExt.h>
#include <string>

#if defined META_IS_SOURCE2
#include <icvar.h>
#include <tier1/convar.h>
class ConCommandBase
{
public:
ConCommandBase(const ConVarCreation_t& creation, int64_t nAdditionalFlags, const ConVarHandle& handle) :
m_bIsCvar(true),
m_Name(creation.m_pszName),
m_nAdditionalFlags(nAdditionalFlags),
m_Handle(handle),
m_Creation(creation)
{}

ConCommandBase(const ConCommandCreation_t& creation, int64_t nAdditionalFlags, const ConCommandHandle& handle) :
m_bIsCvar(false),
m_Name(creation.m_pszName),
m_nAdditionalFlags(nAdditionalFlags),
m_Handle(handle),
m_Creation(creation)
{}

const char* GetName() const { return m_Name.c_str(); }
bool IsConVar() const { return m_bIsCvar; }

ConVarHandle GetConVar() { return m_Handle.cvar; }
ConCommandHandle GetConCommand() { return m_Handle.cmd; }

ConVarCreation_t& GetConVarCreation() { return m_Creation.cvar; }
ConCommandCreation_t& GetConCommandCreation() { return m_Creation.cmd; }

int64_t GetAdditionalFlags() const { return m_nAdditionalFlags; }
protected:
bool m_bIsCvar;
std::string m_Name;
int64_t m_nAdditionalFlags;

union ConCommandBaseHandle {
ConCommandBaseHandle(ConVarHandle handle) :
cvar(handle)
{}

ConCommandBaseHandle(ConCommandHandle handle) :
cmd(handle)
{}

ConVarHandle cvar;
ConCommandHandle cmd;
} m_Handle;

union ConCommandBaseCreation {
ConCommandBaseCreation(const ConVarCreation_t& handle) :
cvar(handle)
{}

ConCommandBaseCreation(const ConCommandCreation_t& handle) :
cmd(handle)
{}

ConVarCreation_t cvar;
ConCommandCreation_t cmd;
} m_Creation;
};
#endif

namespace SourceMM
{
Expand Down
57 changes: 45 additions & 12 deletions core/provider/source2/provider_source2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ SH_DECL_HOOK5_void(IEngineServiceMgr, SwitchToLoop, SH_NOATTRIB, 0, const char *
SH_DECL_HOOK2_void(INetworkGameServer, Init, SH_NOATTRIB, 0, const GameSessionConfiguration_t &, const char *);
SH_DECL_HOOK3(INetworkGameServer, StartChangeLevel, SH_NOATTRIB, 0, CUtlVector<INetworkGameClient *> *, const char *, const char *, void *);
SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, CPlayerSlot, const CCommand&);
SH_DECL_HOOK4_void(ICvar, RegisterConVar, SH_NOATTRIB, 0, const ConVarCreation_t&, int64_t, ConVarHandle*, CConVarBaseData**);
SH_DECL_HOOK2(ICvar, RegisterConCommand, SH_NOATTRIB, 0, ConCommandHandle, const ConCommandCreation_t&, int64_t);

#ifdef SHOULD_OVERRIDE_ALLOWDEDICATED_SERVER
SH_DECL_HOOK1(ISource2ServerConfig, AllowDedicatedServers, const, 0, bool, EUniverse);
Expand Down Expand Up @@ -149,6 +151,9 @@ void Source2Provider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory,

SH_ADD_HOOK(INetworkServerService, StartupServer, netservice, SH_MEMBER(this, &Source2Provider::Hook_StartupServer_Post), true);
SH_ADD_HOOK(IEngineServiceMgr, SwitchToLoop, enginesvcmgr, SH_MEMBER(this, &Source2Provider::Hook_SwitchToLoop), false);

SH_ADD_HOOK(ICvar, RegisterConVar, icvar, SH_MEMBER(this, &Source2Provider::Hook_RegisterConVar), true);
SH_ADD_HOOK(ICvar, RegisterConCommand, icvar, SH_MEMBER(this, &Source2Provider::Hook_RegisterConCommand), true);
}

void Source2Provider::Notify_DLLShutdown_Pre()
Expand Down Expand Up @@ -282,27 +287,34 @@ void Source2Provider::SetConVarString(MetamodSourceConVar *convar, const char* s

bool Source2Provider::IsConCommandBaseACommand(ConCommandBase* pCommand)
{
#ifdef S2_CONVAR_UNFINISHED
return pCommand->IsCommand();
#else
return false;
#endif
return !pCommand->IsConVar();
}

bool Source2Provider::RegisterConCommandBase(ConCommandBase* pCommand)
{
#ifdef S2_CONVAR_UNFINISHED
return g_SMConVarAccessor.Register(pCommand);
#else
if (pCommand->IsConVar())
{
auto& creation = pCommand->GetConVarCreation();
icvar->RegisterConVar(creation, pCommand->GetAdditionalFlags(), creation.m_pHandle, creation.m_pConVarData);
}
else
{
auto& creation = pCommand->GetConCommandCreation();
*creation.m_pHandle = icvar->RegisterConCommand(creation, pCommand->GetAdditionalFlags());
}
return true;
#endif
}

void Source2Provider::UnregisterConCommandBase(ConCommandBase* pCommand)
{
#ifdef S2_CONVAR_UNFINISHED
return g_SMConVarAccessor.Unregister(pCommand);
#endif
if (pCommand->IsConVar())
{
icvar->UnregisterConVar(pCommand->GetConVar());
}
else
{
icvar->UnregisterConCommand(pCommand->GetConCommand());
}
}

MetamodSourceConVar* Source2Provider::CreateConVar(const char* name,
Expand Down Expand Up @@ -430,3 +442,24 @@ void Source2Provider::Hook_ClientCommand(CPlayerSlot nSlot, const CCommand& _cmd

RETURN_META(MRES_IGNORED);
}

void Source2Provider::Hook_RegisterConVar(const ConVarCreation_t& data, int64_t flags, ConVarHandle* handle, CConVarBaseData**)
{
if (!handle || !handle->IsValid()) {
RETURN_META(MRES_IGNORED);
}

ConCommandBase base(data, flags, *handle);
RETURN_META(MRES_IGNORED);
}

ConCommandHandle Source2Provider::Hook_RegisterConCommand(const ConCommandCreation_t& data, int64_t flags)
{
ConCommandHandle handle = META_RESULT_ORIG_RET(ConCommandHandle);
if (!handle.IsValid()) {
RETURN_META_VALUE(MRES_IGNORED, handle);
}

ConCommandBase base(data, flags, handle);
RETURN_META_VALUE(MRES_IGNORED, handle);
}
2 changes: 2 additions & 0 deletions core/provider/source2/provider_source2.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Source2Provider : public BaseProvider
CUtlVector<INetworkGameClient *> *Hook_StartChangeLevel(const char*, const char*, void*);
void Hook_SwitchToLoop(const char *pszLoopName, KeyValues *pKV, uint32 nId, const char *pszUnk, bool bUnk);
void Hook_ClientCommand(CPlayerSlot nSlot, const CCommand& args);
void Hook_RegisterConVar(const ConVarCreation_t&, int64_t, ConVarHandle*, CConVarBaseData**);
ConCommandHandle Hook_RegisterConCommand(const ConCommandCreation_t&, int64_t);
private:
IFileSystem* baseFs = nullptr;
std::string sLastMap;
Expand Down

0 comments on commit fbcc565

Please sign in to comment.