-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
287 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#pragma once | ||
|
||
template<> constexpr EMetaConVarType TranslateMetaConVarType<bool>( void ) { return EMetaConVarType_Bool; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<int16_t>( void ) { return EMetaConVarType_Int16; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<uint16_t>( void ) { return EMetaConVarType_UInt16; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<int32_t>( void ) { return EMetaConVarType_Int32; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<uint32_t>( void ) { return EMetaConVarType_UInt32; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<int64_t>( void ) { return EMetaConVarType_Int64; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<uint64_t>( void ) { return EMetaConVarType_UInt64; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<float>( void ) { return EMetaConVarType_Float32; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<double>( void ) { return EMetaConVarType_Float64; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<const char*>( void ){ return EMetaConVarType_String; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<Color>( void ) { return EMetaConVarType_Color; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<Vector2D>( void ) { return EMetaConVarType_Vector2; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<Vector>( void ) { return EMetaConVarType_Vector3; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<Vector4D>( void ) { return EMetaConVarType_Vector4; } | ||
template<> constexpr EMetaConVarType TranslateMetaConVarType<QAngle>( void ) { return EMetaConVarType_Qangle; } | ||
|
||
template<typename T> | ||
MetamodSourceConVar<T>::MetamodSourceConVar(const char* name, int32_t flags, const char* help, const T& value, FnConVarChangeCallback_t cb) : | ||
m_ConVar(nullptr) | ||
{ | ||
MetamodConVarCreation_t create(name, flags, help, value, reinterpret_cast<void*>(cb)); | ||
} | ||
|
||
template<typename T> | ||
MetamodSourceConVar<T>::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) : | ||
m_ConVar(nullptr) | ||
{ | ||
MetamodConVarCreation_t create(name, flags, help, value, cb, min, minValue, max, maxValue, reinterpret_cast<void*>(cb)); | ||
} | ||
|
||
#if defined META_IS_SOURCE2 | ||
template<typename T> | ||
inline const T MetamodSourceConVar<T>::GetValue() const | ||
{ | ||
return m_ConVar->GetValue(); | ||
} | ||
|
||
template<typename T> | ||
inline const T MetamodSourceConVar<T>::GetDefaultValue( ) const | ||
{ | ||
return m_ConVar->GetDefaultValue( ); | ||
} | ||
|
||
template<typename T> | ||
inline const T MetamodSourceConVar<T>::GetMinValue( ) const | ||
{ | ||
return m_ConVar->GetMinValue( ); | ||
} | ||
|
||
template<typename T> | ||
inline const T MetamodSourceConVar<T>::GetMaxValue( ) const | ||
{ | ||
return m_ConVar->GetMaxValue( ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::SetDefaultValue( const T& value ) | ||
{ | ||
m_ConVar->SetDefaultValue( value ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::SetMinValue( const T& value ) | ||
{ | ||
m_ConVar->SetMinValue( value ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::SetMaxValue( const T& value ) | ||
{ | ||
m_ConVar->SetMaxValue( value ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::RemoveDefaultValue( ) | ||
{ | ||
m_ConVar->RemoveDefaultValue( ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::RemoveMinValue( ) | ||
{ | ||
m_ConVar->RemoveMinValue( ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::RemoveMaxValue( ) | ||
{ | ||
m_ConVar->RemoveMaxValue( ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::GetStringValue( char* dst, size_t len, int index ) const | ||
{ | ||
m_ConVar->GetStringValue( dst, len, index ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::GetStringDefaultValue( char* dst, size_t len ) const | ||
{ | ||
m_ConVar->GetStringDefaultValue( dst, len ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::GetStringMinValue( char* dst, size_t len ) const | ||
{ | ||
m_ConVar->GetStringMinValue( dst, len ); | ||
} | ||
|
||
template<typename T> | ||
inline void MetamodSourceConVar<T>::GetStringMaxValue( char* dst, size_t len ) const | ||
{ | ||
m_ConVar->GetStringMaxValue( dst, len ); | ||
} | ||
#else | ||
|
||
|
||
|
||
#endif |
Oops, something went wrong.