Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Source2 CVar support #135

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
68 changes: 35 additions & 33 deletions core/metamod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,33 @@ static bool is_game_init = false;
static bool vsp_load_requested = false;
static bool vsp_loaded = false;
static game_dll_t gamedll_info;
static MetamodSourceConVar *metamod_version = NULL;
static MetamodSourceConVar *mm_pluginsfile = NULL;
static MetamodSourceConVar *mm_basedir = NULL;

static MetamodSourceConVar<const char*> metamod_version("metamod_version",
ConVarFlag_Notify|ConVarFlag_SpOnly,
"Metamod:Source Version",
METAMOD_VERSION
);

static MetamodSourceConVar<const char*> mm_pluginsfile("mm_pluginsfile",
ConVarFlag_SpOnly,
"Metamod:Source Plugins File",
#if defined WIN32 || defined _WIN32
"addons\\metamod\\metaplugins.ini"
#else
"addons/metamod/metaplugins.ini"
#endif
);

static MetamodSourceConVar<const char*> mm_basedir("mm_basedir",
ConVarFlag_SpOnly,
"Metamod:Source Base Folder",
#if defined __linux__ || defined __APPLE__
"addons/metamod"
#else
"addons\\metamod"
#endif
);

static CreateInterfaceFn engine_factory = NULL;
static CreateInterfaceFn physics_factory = NULL;
static CreateInterfaceFn filesystem_factory = NULL;
Expand Down Expand Up @@ -180,12 +204,12 @@ static class ProviderCallbacks : public IMetamodSourceProviderCallbacks
sizeof(filepath),
"%s/%s",
mod_path.c_str(),
provider->GetConVarString(mm_pluginsfile));
mm_pluginsfile.GetValue());
g_Metamod.PathFormat(vdfpath,
sizeof(vdfpath),
"%s/%s",
mod_path.c_str(),
provider->GetConVarString(mm_basedir));
mm_basedir.GetValue());
mm_LoadPlugins(filepath, vdfpath);
}
else
Expand Down Expand Up @@ -438,15 +462,15 @@ DoInitialPluginLoads()
const char *mmBaseDir = provider->GetCommandLineValue("mm_basedir", NULL);
if (!pluginFile)
{
pluginFile = provider->GetConVarString(mm_pluginsfile);
pluginFile = mm_pluginsfile.GetValue();
if (pluginFile == NULL)
{
pluginFile = "addons/metamod/metaplugins.ini";
}
}
if (!mmBaseDir)
{
mmBaseDir = provider->GetConVarString(mm_basedir);
mmBaseDir = mm_basedir.GetValue();
if (mmBaseDir == NULL)
{
mmBaseDir = "addons/metamod";
Expand All @@ -471,30 +495,8 @@ mm_StartupMetamod(bool is_vsp_load)
METAMOD_VERSION,
is_vsp_load ? "V" : "");

metamod_version = provider->CreateConVar("metamod_version",
METAMOD_VERSION,
"Metamod:Source Version",
ConVarFlag_Notify|ConVarFlag_SpOnly);

provider->SetConVarString(metamod_version, buffer);

mm_pluginsfile = provider->CreateConVar("mm_pluginsfile",
#if defined WIN32 || defined _WIN32
"addons\\metamod\\metaplugins.ini",
#else
"addons/metamod/metaplugins.ini",
#endif
"Metamod:Source Plugins File",
ConVarFlag_SpOnly);

mm_basedir = provider->CreateConVar("mm_basedir",
#if defined __linux__ || defined __APPLE__
"addons/metamod",
#else
"addons\\metamod",
#endif
"Metamod:Source Base Folder",
ConVarFlag_SpOnly);
// TO-DO
// ConVar register here....

g_bIsVspBridged = is_vsp_load;

Expand Down Expand Up @@ -881,12 +883,12 @@ const char *MetamodSource::GetGameBinaryPath()

const char *MetamodSource::GetPluginsFile()
{
return provider->GetConVarString(mm_pluginsfile);
return mm_pluginsfile.GetValue();
}

const char *MetamodSource::GetVDFDir()
{
return provider->GetConVarString(mm_basedir);
return mm_basedir.GetValue();
}

bool MetamodSource::RegisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
Expand Down
129 changes: 129 additions & 0 deletions core/metamod_convar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#pragma once

#include <string>
#include <cstdint>

#include <tier1/convar.h>

template<typename T = const char*>
class MetamodSourceConVar;

enum EMetaConVarType : int16_t
{
EMetaConVarType_Invalid = -1,
EMetaConVarType_Bool,
EMetaConVarType_Int16,
EMetaConVarType_UInt16,
EMetaConVarType_Int32,
EMetaConVarType_UInt32,
EMetaConVarType_Int64,
EMetaConVarType_UInt64,
EMetaConVarType_Float32,
EMetaConVarType_Float64,
EMetaConVarType_String,
EMetaConVarType_Color,
EMetaConVarType_Vector2,
EMetaConVarType_Vector3,
EMetaConVarType_Vector4,
EMetaConVarType_Qangle,
EMetaConVarType_MAX
};

template<typename T>
constexpr EMetaConVarType TranslateMetaConVarType();

struct MetamodConVarCreation_t
{
std::string m_name;
std::string m_help;
int32_t m_flags;

EMetaConVarType m_type;

template<typename T>
T& DefaultValue() { return *reinterpret_cast<T*>(m_defaultValue); }
template<typename T>
T& MinValue() { return *reinterpret_cast<T*>(m_minValue); }
template<typename T>
T& MaxValue() { return *reinterpret_cast<T*>(m_maxValue); }

uint8_t m_defaultValue[sizeof(CVValue_t)];
uint8_t m_minValue[sizeof(CVValue_t)];
uint8_t m_maxValue[sizeof(CVValue_t)];

bool m_hasDefault;
bool m_hasMin;
bool m_hasMax;

void* m_changeCallback;
void** m_conVar;

template<typename T>
MetamodConVarCreation_t(const char* name, int32_t flags, const char* help, const T& value, void* cb = nullptr) :
m_name(name),
m_help(help),
m_flags(flags),
m_type(TranslateMetaConVarType<T>()),
m_hasDefault(true),
m_changeCallback(cb)
{
DefaultValue<T>() = value;
}

template<typename T>
MetamodConVarCreation_t(const char* name, int32_t flags, const char* help, const T& value, bool min, const T& minValue, bool max, const T& maxValue, void* cb = nullptr) :
m_name(name),
m_help(help),
m_flags(flags),
m_type(TranslateMetaConVarType<T>()),
m_hasDefault(true),
m_hasMin(min),
m_hasMax(max),
m_changeCallback(cb)
{
DefaultValue<T>() = value;
MinValue<T>() = minValue;
MaxValue<T>() = maxValue;
}
};

// For backwards compatibility
template<typename T>
class MetamodSourceConVar
{
private:
#if defined META_IS_SOURCE2
using FnConVarChangeCallback_t = void(*)(ConVar<T>* ref, const CSplitScreenSlot nSlot, const T* pNewValue, const T* pOldValue);
ConVar<T>* m_ConVar;
#else
using FnConVarChangeCallback_t = void(*)(IConVar* var, const char* pOldValue, float flOldValue);
ConVar* m_ConVar;
#endif

public:

MetamodSourceConVar(const char* name, int32_t flags, const char* help, const T& value, FnConVarChangeCallback_t cb = nullptr);
MetamodSourceConVar(const char* name, int32_t flags, const char* help, const T& value, bool min, const T& minValue, bool max, const T& maxValue, FnConVarChangeCallback_t cb = nullptr);

inline const T GetValue( ) const;
inline const T GetDefaultValue( ) const;
inline const T GetMinValue( ) const;
inline const T GetMaxValue( ) const;

inline void SetDefaultValue( const T& value );
inline void SetMinValue( const T& value );
inline void SetMaxValue( const T& value );

inline void RemoveDefaultValue( );
inline void RemoveMinValue( );
inline void RemoveMaxValue( );

inline void GetStringValue( char* dst, size_t len, int index = 0 ) const;
inline void GetStringDefaultValue( char* dst, size_t len ) const;
inline void GetStringMinValue( char* dst, size_t len ) const;
inline void GetStringMaxValue( char* dst, size_t len ) const;

int yes[offset_of<MetamodSourceConVar<T>, &MetamodSourceConVar<T>::yes>()];
};

#include "metamod_convar.hxx"
Loading
Loading