diff options
author | Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> | 2023-12-27 00:32:01 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-27 01:32:01 +0100 |
commit | f5ab6fb5e8be7b73e6003d4145081d5e0c0ce287 (patch) | |
tree | 90f2c6a4885dbd181799e2325cf33588697674e1 /primedev/core/convar | |
parent | bb8ed59f6891b1196c5f5bbe7346cd171c8215fa (diff) | |
download | NorthstarLauncher-f5ab6fb5e8be7b73e6003d4145081d5e0c0ce287.tar.gz NorthstarLauncher-f5ab6fb5e8be7b73e6003d4145081d5e0c0ce287.zip |
Folder restructuring from primedev (#624)v1.21.2-rc3v1.21.2
Copies of over the primedev folder structure for easier cherry-picking of further changes
Co-authored-by: F1F7Y <filip.bartos07@proton.me>
Diffstat (limited to 'primedev/core/convar')
-rw-r--r-- | primedev/core/convar/concommand.cpp | 157 | ||||
-rw-r--r-- | primedev/core/convar/concommand.h | 139 | ||||
-rw-r--r-- | primedev/core/convar/convar.cpp | 534 | ||||
-rw-r--r-- | primedev/core/convar/convar.h | 194 | ||||
-rw-r--r-- | primedev/core/convar/cvar.cpp | 26 | ||||
-rw-r--r-- | primedev/core/convar/cvar.h | 38 |
6 files changed, 1088 insertions, 0 deletions
diff --git a/primedev/core/convar/concommand.cpp b/primedev/core/convar/concommand.cpp new file mode 100644 index 00000000..41f54c76 --- /dev/null +++ b/primedev/core/convar/concommand.cpp @@ -0,0 +1,157 @@ +#include "concommand.h" +#include "shared/misccommands.h" +#include "engine/r2engine.h" + +#include "plugins/pluginbackend.h" +#include "plugins/plugin_abi.h" + +#include <iostream> + +//----------------------------------------------------------------------------- +// Purpose: Returns true if this is a command +// Output : bool +//----------------------------------------------------------------------------- +bool ConCommand::IsCommand(void) const +{ + return true; +} + +//----------------------------------------------------------------------------- +// Purpose: Returns true if this is a command +// Output : bool +//----------------------------------------------------------------------------- +bool ConCommandBase::IsCommand(void) const +{ + return true; +} + +//----------------------------------------------------------------------------- +// Purpose: Has this cvar been registered +// Output : Returns true on success, false on failure. +//----------------------------------------------------------------------------- +bool ConCommandBase::IsRegistered(void) const +{ + return m_bRegistered; +} + +//----------------------------------------------------------------------------- +// Purpose: Test each ConCommand query before execution. +// Input : *pCommandBase - nFlags +// Output : False if execution is permitted, true if not. +//----------------------------------------------------------------------------- +bool ConCommandBase::IsFlagSet(int nFlags) const +{ + return m_nFlags & nFlags; +} + +//----------------------------------------------------------------------------- +// Purpose: Checks if ConCommand has requested flags. +// Input : nFlags - +// Output : True if ConCommand has nFlags. +//----------------------------------------------------------------------------- +bool ConCommandBase::HasFlags(int nFlags) +{ + return m_nFlags & nFlags; +} + +//----------------------------------------------------------------------------- +// Purpose: Add's flags to ConCommand. +// Input : nFlags - +//----------------------------------------------------------------------------- +void ConCommandBase::AddFlags(int nFlags) +{ + m_nFlags |= nFlags; +} + +//----------------------------------------------------------------------------- +// Purpose: Removes flags from ConCommand. +// Input : nFlags - +//----------------------------------------------------------------------------- +void ConCommandBase::RemoveFlags(int nFlags) +{ + m_nFlags &= ~nFlags; +} + +//----------------------------------------------------------------------------- +// Purpose: Returns current flags. +// Output : int +//----------------------------------------------------------------------------- +int ConCommandBase::GetFlags(void) const +{ + return m_nFlags; +} + +//----------------------------------------------------------------------------- +// Purpose: +// Output : const ConCommandBase +//----------------------------------------------------------------------------- +ConCommandBase* ConCommandBase::GetNext(void) const +{ + return m_pNext; +} + +//----------------------------------------------------------------------------- +// Purpose: Returns the ConCommandBase help text. +// Output : const char* +//----------------------------------------------------------------------------- +const char* ConCommandBase::GetHelpText(void) const +{ + return m_pszHelpString; +} + +//----------------------------------------------------------------------------- +// Purpose: Copies string using local new/delete operators +// Input : *szFrom - +// Output : char +//----------------------------------------------------------------------------- +char* ConCommandBase::CopyString(const char* szFrom) const +{ + size_t nLen; + char* szTo; + + nLen = strlen(szFrom); + if (nLen <= 0) + { + szTo = new char[1]; + szTo[0] = 0; + } + else + { + szTo = new char[nLen + 1]; + memmove(szTo, szFrom, nLen + 1); + } + return szTo; +} + +typedef void (*ConCommandConstructorType)( + ConCommand* newCommand, const char* name, FnCommandCallback_t callback, const char* helpString, int flags, void* parent); +ConCommandConstructorType ConCommandConstructor; + +void RegisterConCommand(const char* name, FnCommandCallback_t callback, const char* helpString, int flags) +{ + spdlog::info("Registering ConCommand {}", name); + + // no need to free this ever really, it should exist as long as game does + ConCommand* newCommand = new ConCommand; + ConCommandConstructor(newCommand, name, callback, helpString, flags, nullptr); +} + +void RegisterConCommand( + const char* name, FnCommandCallback_t callback, const char* helpString, int flags, FnCommandCompletionCallback completionCallback) +{ + spdlog::info("Registering ConCommand {}", name); + + // no need to free this ever really, it should exist as long as game does + ConCommand* newCommand = new ConCommand; + ConCommandConstructor(newCommand, name, callback, helpString, flags, nullptr); + newCommand->m_pCompletionCallback = completionCallback; +} + +ON_DLL_LOAD("engine.dll", ConCommand, (CModule module)) +{ + ConCommandConstructor = module.Offset(0x415F60).RCast<ConCommandConstructorType>(); + AddMiscConCommands(); + + g_pPluginCommunicationhandler->m_sEngineData.ConCommandConstructor = + reinterpret_cast<PluginConCommandConstructorType>(ConCommandConstructor); +} diff --git a/primedev/core/convar/concommand.h b/primedev/core/convar/concommand.h new file mode 100644 index 00000000..71a82fec --- /dev/null +++ b/primedev/core/convar/concommand.h @@ -0,0 +1,139 @@ +#pragma once + +// From Source SDK +class ConCommandBase; +class IConCommandBaseAccessor +{ +public: + // Flags is a combination of FCVAR flags in cvar.h. + // hOut is filled in with a handle to the variable. + virtual bool RegisterConCommandBase(ConCommandBase* pVar) = 0; +}; + +class CCommand +{ +public: + CCommand() = delete; + + int64_t ArgC() const; + const char** ArgV() const; + const char* ArgS() const; // All args that occur after the 0th arg, in string form + const char* GetCommandString() const; // The entire command in string form, including the 0th arg + const char* operator[](int nIndex) const; // Gets at arguments + const char* Arg(int nIndex) const; // Gets at arguments + + static int MaxCommandLength(); + +private: + enum + { + COMMAND_MAX_ARGC = 64, + COMMAND_MAX_LENGTH = 512, + }; + + int64_t m_nArgc; + int64_t m_nArgv0Size; + char m_pArgSBuffer[COMMAND_MAX_LENGTH]; + char m_pArgvBuffer[COMMAND_MAX_LENGTH]; + const char* m_ppArgv[COMMAND_MAX_ARGC]; +}; + +inline int CCommand::MaxCommandLength() +{ + return COMMAND_MAX_LENGTH - 1; +} +inline int64_t CCommand::ArgC() const +{ + return m_nArgc; +} +inline const char** CCommand::ArgV() const +{ + return m_nArgc ? (const char**)m_ppArgv : NULL; +} +inline const char* CCommand::ArgS() const +{ + return m_nArgv0Size ? &m_pArgSBuffer[m_nArgv0Size] : ""; +} +inline const char* CCommand::GetCommandString() const +{ + return m_nArgc ? m_pArgSBuffer : ""; +} +inline const char* CCommand::Arg(int nIndex) const +{ + // FIXME: Many command handlers appear to not be particularly careful + // about checking for valid argc range. For now, we're going to + // do the extra check and return an empty string if it's out of range + if (nIndex < 0 || nIndex >= m_nArgc) + return ""; + return m_ppArgv[nIndex]; +} +inline const char* CCommand::operator[](int nIndex) const +{ + return Arg(nIndex); +} + +//----------------------------------------------------------------------------- +// Called when a ConCommand needs to execute +//----------------------------------------------------------------------------- +typedef void (*FnCommandCallback_t)(const CCommand& command); + +#define COMMAND_COMPLETION_MAXITEMS 64 +#define COMMAND_COMPLETION_ITEM_LENGTH 128 + +//----------------------------------------------------------------------------- +// Returns 0 to COMMAND_COMPLETION_MAXITEMS worth of completion strings +//----------------------------------------------------------------------------- +typedef int (*FnCommandCompletionCallback)(const char* partial, char commands[COMMAND_COMPLETION_MAXITEMS][COMMAND_COMPLETION_ITEM_LENGTH]); + +// From r5reloaded +class ConCommandBase +{ +public: + bool HasFlags(int nFlags); + void AddFlags(int nFlags); + void RemoveFlags(int nFlags); + + bool IsCommand(void) const; + bool IsRegistered(void) const; + bool IsFlagSet(int nFlags) const; + static bool IsFlagSet(ConCommandBase* pCommandBase, int nFlags); // For hooking to engine's implementation. + + int GetFlags(void) const; + ConCommandBase* GetNext(void) const; + const char* GetHelpText(void) const; + + char* CopyString(const char* szFrom) const; + + void* m_pConCommandBaseVTable; // 0x0000 + ConCommandBase* m_pNext; // 0x0008 + bool m_bRegistered; // 0x0010 + char pad_0011[7]; // 0x0011 <- 3 bytes padding + unk int32. + const char* m_pszName; // 0x0018 + const char* m_pszHelpString; // 0x0020 + int m_nFlags; // 0x0028 + ConCommandBase* s_pConCommandBases; // 0x002C + IConCommandBaseAccessor* s_pAccessor; // 0x0034 +}; // Size: 0x0040 + +// taken from ttf2sdk +class ConCommand : public ConCommandBase +{ + friend class CCVar; + +public: + ConCommand(void) {}; // !TODO: Rebuild engine constructor in SDK instead. + ConCommand(const char* szName, const char* szHelpString, int nFlags, void* pCallback, void* pCommandCompletionCallback); + void Init(void); + bool IsCommand(void) const; + + FnCommandCallback_t m_pCommandCallback {}; // 0x0040 <- starts from 0x40 since we inherit ConCommandBase. + FnCommandCompletionCallback m_pCompletionCallback {}; // 0x0048 <- defaults to sub_180417410 ('xor eax, eax'). + int m_nCallbackFlags {}; // 0x0050 + char pad_0054[4]; // 0x0054 + int unk0; // 0x0058 + int unk1; // 0x005C +}; // Size: 0x0060 + +void RegisterConCommand(const char* name, void (*callback)(const CCommand&), const char* helpString, int flags); +void RegisterConCommand( + const char* name, void (*callback)(const CCommand&), const char* helpString, int flags, FnCommandCompletionCallback completionCallback); diff --git a/primedev/core/convar/convar.cpp b/primedev/core/convar/convar.cpp new file mode 100644 index 00000000..e77ae1fd --- /dev/null +++ b/primedev/core/convar/convar.cpp @@ -0,0 +1,534 @@ +#include "bits.h" +#include "cvar.h" +#include "convar.h" +#include "core/sourceinterface.h" + +#include "plugins/pluginbackend.h" +#include "plugins/plugin_abi.h" + +#include <float.h> + +typedef void (*ConVarRegisterType)( + ConVar* pConVar, + const char* pszName, + const char* pszDefaultValue, + int nFlags, + const char* pszHelpString, + bool bMin, + float fMin, + bool bMax, + float fMax, + void* pCallback); +ConVarRegisterType conVarRegister; + +typedef void (*ConVarMallocType)(void* pConVarMaloc, int a2, int a3); +ConVarMallocType conVarMalloc; + +void* g_pConVar_Vtable = nullptr; +void* g_pIConVar_Vtable = nullptr; + +//----------------------------------------------------------------------------- +// Purpose: ConVar interface initialization +//----------------------------------------------------------------------------- +ON_DLL_LOAD("engine.dll", ConVar, (CModule module)) +{ + conVarMalloc = module.Offset(0x415C20).RCast<ConVarMallocType>(); + conVarRegister = module.Offset(0x417230).RCast<ConVarRegisterType>(); + + g_pConVar_Vtable = module.Offset(0x67FD28); + g_pIConVar_Vtable = module.Offset(0x67FDC8); + + g_pCVarInterface = new SourceInterface<CCvar>("vstdlib.dll", "VEngineCvar007"); + g_pCVar = *g_pCVarInterface; + + g_pPluginCommunicationhandler->m_sEngineData.conVarMalloc = reinterpret_cast<PluginConVarMallocType>(conVarMalloc); + g_pPluginCommunicationhandler->m_sEngineData.conVarRegister = reinterpret_cast<PluginConVarRegisterType>(conVarRegister); + g_pPluginCommunicationhandler->m_sEngineData.ConVar_Vtable = reinterpret_cast<void*>(g_pConVar_Vtable); + g_pPluginCommunicationhandler->m_sEngineData.IConVar_Vtable = reinterpret_cast<void*>(g_pIConVar_Vtable); + g_pPluginCommunicationhandler->m_sEngineData.g_pCVar = reinterpret_cast<void*>(g_pCVar); +} + +//----------------------------------------------------------------------------- +// Purpose: constructor +//----------------------------------------------------------------------------- +ConVar::ConVar(const char* pszName, const char* pszDefaultValue, int nFlags, const char* pszHelpString) +{ + spdlog::info("Registering Convar {}", pszName); + + this->m_ConCommandBase.m_pConCommandBaseVTable = g_pConVar_Vtable; + this->m_ConCommandBase.s_pConCommandBases = (ConCommandBase*)g_pIConVar_Vtable; + + conVarMalloc(&this->m_pMalloc, 0, 0); // Allocate new memory for ConVar. + conVarRegister(this, pszName, pszDefaultValue, nFlags, pszHelpString, 0, 0, 0, 0, 0); +} + +//----------------------------------------------------------------------------- +// Purpose: constructor +//----------------------------------------------------------------------------- +ConVar::ConVar( + const char* pszName, + const char* pszDefaultValue, + int nFlags, + const char* pszHelpString, + bool bMin, + float fMin, + bool bMax, + float fMax, + FnChangeCallback_t pCallback) +{ + spdlog::info("Registering Convar {}", pszName); + + this->m_ConCommandBase.m_pConCommandBaseVTable = g_pConVar_Vtable; + this->m_ConCommandBase.s_pConCommandBases = (ConCommandBase*)g_pIConVar_Vtable; + + conVarMalloc(&this->m_pMalloc, 0, 0); // Allocate new memory for ConVar. + conVarRegister(this, pszName, pszDefaultValue, nFlags, pszHelpString, bMin, fMin, bMax, fMax, (void*)pCallback); +} + +//----------------------------------------------------------------------------- +// Purpose: destructor +//----------------------------------------------------------------------------- +ConVar::~ConVar(void) +{ + if (m_Value.m_pszString) + delete[] m_Value.m_pszString; +} + +//----------------------------------------------------------------------------- +// Purpose: Returns the base ConVar name. +// Output : const char* +//----------------------------------------------------------------------------- +const char* ConVar::GetBaseName(void) const +{ + return m_ConCommandBase.m_pszName; +} + +//----------------------------------------------------------------------------- +// Purpose: Returns the ConVar help text. +// Output : const char* +//----------------------------------------------------------------------------- +const char* ConVar::GetHelpText(void) const +{ + return m_ConCommandBase.m_pszHelpString; +} + +//----------------------------------------------------------------------------- +// Purpose: Add's flags to ConVar. +// Input : nFlags - +//----------------------------------------------------------------------------- +void ConVar::AddFlags(int nFlags) +{ + m_ConCommandBase.m_nFlags |= nFlags; +} + +//----------------------------------------------------------------------------- +// Purpose: Removes flags from ConVar. +// Input : nFlags - +//----------------------------------------------------------------------------- +void ConVar::RemoveFlags(int nFlags) +{ + m_ConCommandBase.m_nFlags &= ~nFlags; +} + +//----------------------------------------------------------------------------- +// Purpose: Return ConVar value as a boolean. +// Output : bool +//----------------------------------------------------------------------------- +bool ConVar::GetBool(void) const +{ + return !!GetInt(); +} + +//----------------------------------------------------------------------------- +// Purpose: Return ConVar value as a float. +// Output : float +//----------------------------------------------------------------------------- +float ConVar::GetFloat(void) const +{ + return m_Value.m_fValue; +} + +//----------------------------------------------------------------------------- +// Purpose: Return ConVar value as an integer. +// Output : int +//----------------------------------------------------------------------------- +int ConVar::GetInt(void) const +{ + return m_Value.m_nValue; +} + +//----------------------------------------------------------------------------- +// Purpose: Return ConVar value as a color. +// Output : Color +//----------------------------------------------------------------------------- +Color ConVar::GetColor(void) const +{ + unsigned char* pColorElement = ((unsigned char*)&m_Value.m_nValue); + return Color(pColorElement[0], pColorElement[1], pColorElement[2], pColorElement[3]); +} + +//----------------------------------------------------------------------------- +// Purpose: Return ConVar value as a string. +// Output : const char * +//----------------------------------------------------------------------------- +const char* ConVar::GetString(void) const +{ + if (m_ConCommandBase.m_nFlags & FCVAR_NEVER_AS_STRING) + { + return "FCVAR_NEVER_AS_STRING"; + } + + char const* str = m_Value.m_pszString; + return str ? str : ""; +} + +//----------------------------------------------------------------------------- +// Purpose: +// Input : flMinVal - +// Output : true if there is a min set. +//----------------------------------------------------------------------------- +bool ConVar::GetMin(float& flMinVal) const +{ + flMinVal = m_fMinVal; + return m_bHasMin; +} + +//----------------------------------------------------------------------------- +// Purpose: +// Input : flMaxVal - +// Output : true if there is a max set. +//----------------------------------------------------------------------------- +bool ConVar::GetMax(float& flMaxVal) const +{ + flMaxVal = m_fMaxVal; + return m_bHasMax; +} + +//----------------------------------------------------------------------------- +// Purpose: returns the min value. +// Output : float +//----------------------------------------------------------------------------- +float ConVar::GetMinValue(void) const +{ + return m_fMinVal; +} + +//----------------------------------------------------------------------------- +// Purpose: returns the max value. +// Output : float +//----------------------------------------------------------------------------- +float ConVar::GetMaxValue(void) const +{ + return m_fMaxVal; + ; +} + +//----------------------------------------------------------------------------- +// Purpose: checks if ConVar has min value. +// Output : bool +//----------------------------------------------------------------------------- +bool ConVar::HasMin(void) const +{ + return m_bHasMin; +} + +//----------------------------------------------------------------------------- +// Purpose: checks if ConVar has max value. +// Output : bool +//----------------------------------------------------------------------------- +bool ConVar::HasMax(void) const +{ + return m_bHasMax; +} + +//----------------------------------------------------------------------------- +// Purpose: sets the ConVar int value. +// Input : nValue - +//----------------------------------------------------------------------------- +void ConVar::SetValue(int nValue) +{ + if (nValue == m_Value.m_nValue) + { + return; + } + + float flValue = (float)nValue; + + // Check bounds. + if (ClampValue(flValue)) + { + nValue = (int)(flValue); + } + + // Redetermine value. + float flOldValue = m_Value.m_fValue; + m_Value.m_fValue = flValue; + m_Value.m_nValue = nValue; + + if (!(m_ConCommandBase.m_nFlags & FCVAR_NEVER_AS_STRING)) + { + char szTempValue[32]; + snprintf(szTempValue, sizeof(szTempValue), "%d", m_Value.m_nValue); + ChangeStringValue(szTempValue, flOldValue); + } +} + +//----------------------------------------------------------------------------- +// Purpose: sets the ConVar float value. +// Input : flValue - +//----------------------------------------------------------------------------- +void ConVar::SetValue(float flValue) +{ + if (flValue == m_Value.m_fValue) + { + return; + } + + // Check bounds. + ClampValue(flValue); + + // Redetermine value. + float flOldValue = m_Value.m_fValue; + m_Value.m_fValue = flValue; + m_Value.m_nValue = (int)m_Value.m_fValue; + + if (!(m_ConCommandBase.m_nFlags & FCVAR_NEVER_AS_STRING)) + { + char szTempValue[32]; + snprintf(szTempValue, sizeof(szTempValue), "%f", m_Value.m_fValue); + ChangeStringValue(szTempValue, flOldValue); + } +} + +//----------------------------------------------------------------------------- +// Purpose: sets the ConVar string value. +// Input : *szValue - +//----------------------------------------------------------------------------- +void ConVar::SetValue(const char* pszValue) +{ + if (strcmp(this->m_Value.m_pszString, pszValue) == 0) + return; + + char szTempValue[32] {}; + const char* pszNewValue {}; + + float flOldValue = m_Value.m_fValue; + pszNewValue = (char*)pszValue; + if (!pszNewValue) + { + pszNewValue = ""; + } + + if (!SetColorFromString(pszValue)) + { + // Not a color, do the standard thing + float flNewValue = (float)atof(pszValue); + if (!std::isfinite(flNewValue)) + { + spdlog::warn("Warning: ConVar '{}' = '{}' is infinite, clamping value.\n", GetBaseName(), pszValue); + flNewValue = FLT_MAX; + } + + if (ClampValue(flNewValue)) + { + snprintf(szTempValue, sizeof(szTempValue), "%f", flNewValue); + pszNewValue = szTempValue; + } + + // Redetermine value + m_Value.m_fValue = flNewValue; + m_Value.m_nValue = (int)(m_Value.m_fValue); + } + + if (!(m_ConCommandBase.m_nFlags & FCVAR_NEVER_AS_STRING)) + { + ChangeStringValue(pszNewValue, flOldValue); + } +} + +//----------------------------------------------------------------------------- +// Purpose: sets the ConVar color value. +// Input : clValue - +//----------------------------------------------------------------------------- +void ConVar::SetValue(Color clValue) +{ + std::string svResult = ""; + + for (int i = 0; i < 4; i++) + { + if (!(clValue.GetValue(i) == 0 && svResult.size() == 0)) + { + svResult += std::to_string(clValue.GetValue(i)); + svResult.append(" "); + } + } + + this->m_Value.m_pszString = svResult.c_str(); +} + +//----------------------------------------------------------------------------- +// Purpose: changes the ConVar string value. +// Input : *pszTempVal - flOldValue +//----------------------------------------------------------------------------- +void ConVar::ChangeStringValue(const char* pszTempVal, float flOldValue) +{ + assert(!(m_ConCommandBase.m_nFlags & FCVAR_NEVER_AS_STRING)); + + char* pszOldValue = (char*)_malloca(m_Value.m_iStringLength); + if (pszOldValue != NULL) + { + memcpy(pszOldValue, m_Value.m_pszString, m_Value.m_iStringLength); + } + + if (pszTempVal) + { + int len = strlen(pszTempVal) + 1; + + if (len > m_Value.m_iStringLength) + { + if (m_Value.m_pszString) + delete[] m_Value.m_pszString; + + m_Value.m_pszString = new char[len]; + m_Value.m_iStringLength = len; + } + + memcpy((char*)m_Value.m_pszString, pszTempVal, len); + } + else + { + m_Value.m_pszString = NULL; + } + + pszOldValue = 0; +} + +//----------------------------------------------------------------------------- +// Purpose: sets the ConVar color value from string. +// Input : *pszValue - +//----------------------------------------------------------------------------- +bool ConVar::SetColorFromString(const char* pszValue) +{ + bool bColor = false; + + // Try pulling RGBA color values out of the string. + int nRGBA[4] {}; + int nParamsRead = sscanf_s(pszValue, "%i %i %i %i", &(nRGBA[0]), &(nRGBA[1]), &(nRGBA[2]), &(nRGBA[3])); + + if (nParamsRead >= 3) + { + // This is probably a color! + if (nParamsRead == 3) + { + // Assume they wanted full alpha. + nRGBA[3] = 255; + } + + if (nRGBA[0] >= 0 && nRGBA[0] <= 255 && nRGBA[1] >= 0 && nRGBA[1] <= 255 && nRGBA[2] >= 0 && nRGBA[2] <= 255 && nRGBA[3] >= 0 && + nRGBA[3] <= 255) + { + // printf("*** WOW! Found a color!! ***\n"); + + // This is definitely a color! + bColor = true; + + // Stuff all the values into each byte of our int. + unsigned char* pColorElement = ((unsigned char*)&m_Value.m_nValue); + pColorElement[0] = nRGBA[0]; + pColorElement[1] = nRGBA[1]; + pColorElement[2] = nRGBA[2]; + pColorElement[3] = nRGBA[3]; + + // Copy that value into our float. + m_Value.m_fValue = (float)(m_Value.m_nValue); + } + } + + return bColor; +} + +//----------------------------------------------------------------------------- +// Purpose: Checks if ConVar is registered. +// Output : bool +//----------------------------------------------------------------------------- +bool ConVar::IsRegistered(void) const +{ + return m_ConCommandBase.m_bRegistered; +} + +//----------------------------------------------------------------------------- +// Purpose: Returns true if this is a command +// Output : bool +//----------------------------------------------------------------------------- +bool ConVar::IsCommand(void) const +{ + return false; +} + +//----------------------------------------------------------------------------- +// Purpose: Test each ConVar query before setting the value. +// Input : nFlags +// Output : False if change is permitted, true if not. +//----------------------------------------------------------------------------- +bool ConVar::IsFlagSet(int nFlags) const +{ + return m_ConCommandBase.m_nFlags & nFlags; +} + +//----------------------------------------------------------------------------- +// Purpose: Check whether to clamp and then perform clamp. +// Input : flValue - +// Output : Returns true if value changed. +//----------------------------------------------------------------------------- +bool ConVar::ClampValue(float& flValue) +{ + if (m_bHasMin && (flValue < m_fMinVal)) + { + flValue = m_fMinVal; + return true; + } + + if (m_bHasMax && (flValue > m_fMaxVal)) + { + flValue = m_fMaxVal; + return true; + } + + return false; +} + +int ParseConVarFlagsString(std::string modName, std::string sFlags) +{ + int iFlags = 0; + std::stringstream stFlags(sFlags); + std::string sFlag; + + while (std::getline(stFlags, sFlag, '|')) + { + // trim the flag + sFlag.erase(sFlag.find_last_not_of(" \t\n\f\v\r") + 1); + sFlag.erase(0, sFlag.find_first_not_of(" \t\n\f\v\r")); + + // skip if empty + if (sFlag.empty()) + continue; + + // find the matching flag value + bool ok = false; + for (auto const& flagPair : g_PrintCommandFlags) + { + if (sFlag == flagPair.second) + { + iFlags |= flagPair.first; + ok = true; + break; + } + } + if (!ok) + { + spdlog::warn("Mod ConCommand {} has unknown flag {}", modName, sFlag); + } + } + + return iFlags; +} diff --git a/primedev/core/convar/convar.h b/primedev/core/convar/convar.h new file mode 100644 index 00000000..f0366b46 --- /dev/null +++ b/primedev/core/convar/convar.h @@ -0,0 +1,194 @@ +#pragma once +#include "core/sourceinterface.h" +#include "core/math/color.h" +#include "cvar.h" +#include "concommand.h" + +// taken directly from iconvar.h + +// The default, no flags at all +#define FCVAR_NONE 0 + +// Command to ConVars and ConCommands +// ConVar Systems +#define FCVAR_UNREGISTERED (1 << 0) // If this is set, don't add to linked list, etc. +#define FCVAR_DEVELOPMENTONLY (1 << 1) // Hidden in released products. Flag is removed automatically if ALLOW_DEVELOPMENT_CVARS is defined. +#define FCVAR_GAMEDLL (1 << 2) // defined by the game DLL +#define FCVAR_CLIENTDLL (1 << 3) // defined by the client DLL +#define FCVAR_HIDDEN (1 << 4) // Hidden. Doesn't appear in find or auto complete. Like DEVELOPMENTONLY, but can't be compiled out. + +// ConVar only +#define FCVAR_PROTECTED \ + (1 << 5) // It's a server cvar, but we don't send the data since it's a password, etc. Sends 1 if it's not bland/zero, 0 otherwise as + // value. +#define FCVAR_SPONLY (1 << 6) // This cvar cannot be changed by clients connected to a multiplayer server. +#define FCVAR_ARCHIVE (1 << 7) // set to cause it to be saved to vars.rc +#define FCVAR_NOTIFY (1 << 8) // notifies players when changed +#define FCVAR_USERINFO (1 << 9) // changes the client's info string + +#define FCVAR_PRINTABLEONLY (1 << 10) // This cvar's string cannot contain unprintable characters ( e.g., used for player name etc ). +#define FCVAR_GAMEDLL_FOR_REMOTE_CLIENTS \ + (1 << 10) // When on concommands this allows remote clients to execute this cmd on the server. + // We are changing the default behavior of concommands to disallow execution by remote clients without + // this flag due to the number existing concommands that can lag or crash the server when clients abuse them. + +#define FCVAR_UNLOGGED (1 << 11) // If this is a FCVAR_SERVER, don't log changes to the log file / console if we are creating a log +#define FCVAR_NEVER_AS_STRING (1 << 12) // never try to print that cvar + +// It's a ConVar that's shared between the client and the server. +// At signon, the values of all such ConVars are sent from the server to the client (skipped for local client, of course ) +// If a change is requested it must come from the console (i.e., no remote client changes) +// If a value is changed while a server is active, it's replicated to all connected clients +#define FCVAR_REPLICATED (1 << 13) // server setting enforced on clients, TODO rename to FCAR_SERVER at some time +#define FCVAR_CHEAT (1 << 14) // Only useable in singleplayer / debug / multiplayer & sv_cheats +#define FCVAR_SS (1 << 15) // causes varnameN where N == 2 through max splitscreen slots for mod to be autogenerated +#define FCVAR_DEMO (1 << 16) // record this cvar when starting a demo file +#define FCVAR_DONTRECORD (1 << 17) // don't record these command in demofiles +#define FCVAR_SS_ADDED (1 << 18) // This is one of the "added" FCVAR_SS variables for the splitscreen players +#define FCVAR_RELEASE (1 << 19) // Cvars tagged with this are the only cvars avaliable to customers +#define FCVAR_RELOAD_MATERIALS (1 << 20) // If this cvar changes, it forces a material reload +#define FCVAR_RELOAD_TEXTURES (1 << 21) // If this cvar changes, if forces a texture reload + +#define FCVAR_NOT_CONNECTED (1 << 22) // cvar cannot be changed by a client that is connected to a server +#define FCVAR_MATERIAL_SYSTEM_THREAD (1 << 23) // Indicates this cvar is read from the material system thread +#define FCVAR_ARCHIVE_PLAYERPROFILE (1 << 24) // respawn-defined flag, same as FCVAR_ARCHIVE but writes to profile.cfg + +#define FCVAR_SERVER_CAN_EXECUTE \ + (1 << 28) // the server is allowed to execute this command on clients via + // ClientCommand/NET_StringCmd/CBaseClientState::ProcessStringCmd. +#define FCVAR_SERVER_CANNOT_QUERY \ + (1 << 29) // If this is set, then the server is not allowed to query this cvar's value (via IServerPluginHelpers::StartQueryCvarValue). + +// !!!NOTE!!! : this is likely incorrect, there are multiple concommands that the vanilla game registers with this flag that 100% should not +// be remotely executable i.e. multiple commands that only exist on client (screenshot, joystick_initialize) we now use +// FCVAR_GAMEDLL_FOR_REMOTE_CLIENTS in all places this flag was previously used +#define FCVAR_CLIENTCMD_CAN_EXECUTE \ + (1 << 30) // IVEngineClient::ClientCmd is allowed to execute this command. + // Note: IVEngineClient::ClientCmd_Unrestricted can run any client command. + +#define FCVAR_ACCESSIBLE_FROM_THREADS (1 << 25) // used as a debugging tool necessary to check material system thread convars + +// TODO: could be cool to repurpose these for northstar use somehow? +// #define FCVAR_AVAILABLE (1<<26) +// #define FCVAR_AVAILABLE (1<<27) +// #define FCVAR_AVAILABLE (1<<31) + +// flag => string stuff +const std::multimap<int, const char*> g_PrintCommandFlags = { + {FCVAR_UNREGISTERED, "UNREGISTERED"}, + {FCVAR_DEVELOPMENTONLY, "DEVELOPMENTONLY"}, + {FCVAR_GAMEDLL, "GAMEDLL"}, + {FCVAR_CLIENTDLL, "CLIENTDLL"}, + {FCVAR_HIDDEN, "HIDDEN"}, + {FCVAR_PROTECTED, "PROTECTED"}, + {FCVAR_SPONLY, "SPONLY"}, + {FCVAR_ARCHIVE, "ARCHIVE"}, + {FCVAR_NOTIFY, "NOTIFY"}, + {FCVAR_USERINFO, "USERINFO"}, + + // TODO: PRINTABLEONLY and GAMEDLL_FOR_REMOTE_CLIENTS are both 1<<10, one is for vars and one is for commands + // this fucking sucks i think + {FCVAR_PRINTABLEONLY, "PRINTABLEONLY"}, + {FCVAR_GAMEDLL_FOR_REMOTE_CLIENTS, "GAMEDLL_FOR_REMOTE_CLIENTS"}, + + {FCVAR_UNLOGGED, "UNLOGGED"}, + {FCVAR_NEVER_AS_STRING, "NEVER_AS_STRING"}, + {FCVAR_REPLICATED, "REPLICATED"}, + {FCVAR_CHEAT, "CHEAT"}, + {FCVAR_SS, "SS"}, + {FCVAR_DEMO, "DEMO"}, + {FCVAR_DONTRECORD, "DONTRECORD"}, + {FCVAR_SS_ADDED, "SS_ADDED"}, + {FCVAR_RELEASE, "RELEASE"}, + {FCVAR_RELOAD_MATERIALS, "RELOAD_MATERIALS"}, + {FCVAR_RELOAD_TEXTURES, "RELOAD_TEXTURES"}, + {FCVAR_NOT_CONNECTED, "NOT_CONNECTED"}, + {FCVAR_MATERIAL_SYSTEM_THREAD, "MATERIAL_SYSTEM_THREAD"}, + {FCVAR_ARCHIVE_PLAYERPROFILE, "ARCHIVE_PLAYERPROFILE"}, + {FCVAR_SERVER_CAN_EXECUTE, "SERVER_CAN_EXECUTE"}, + {FCVAR_SERVER_CANNOT_QUERY, "SERVER_CANNOT_QUERY"}, + {FCVAR_CLIENTCMD_CAN_EXECUTE, "UNKNOWN"}, + {FCVAR_ACCESSIBLE_FROM_THREADS, "ACCESSIBLE_FROM_THREADS"}}; + +//----------------------------------------------------------------------------- +// Forward declarations +//----------------------------------------------------------------------------- +class ConCommandBase; +class ConCommand; +class ConVar; + +typedef void (*FnChangeCallback_t)(ConVar* var, const char* pOldValue, float flOldValue); + +//----------------------------------------------------------------------------- +// Purpose: A console variable +//----------------------------------------------------------------------------- +class ConVar +{ +public: + ConVar(void) {}; + ConVar(const char* pszName, const char* pszDefaultValue, int nFlags, const char* pszHelpString); + ConVar( + const char* pszName, + const char* pszDefaultValue, + int nFlags, + const char* pszHelpString, + bool bMin, + float fMin, + bool bMax, + float fMax, + FnChangeCallback_t pCallback); + ~ConVar(void); + + const char* GetBaseName(void) const; + const char* GetHelpText(void) const; + + void AddFlags(int nFlags); + void RemoveFlags(int nFlags); + + bool GetBool(void) const; + float GetFloat(void) const; + int GetInt(void) const; + Color GetColor(void) const; + const char* GetString(void) const; + + bool GetMin(float& flMinValue) const; + bool GetMax(float& flMaxValue) const; + float GetMinValue(void) const; + float GetMaxValue(void) const; + + bool HasMin(void) const; + bool HasMax(void) const; + + void SetValue(int nValue); + void SetValue(float flValue); + void SetValue(const char* pszValue); + void SetValue(Color clValue); + + void ChangeStringValue(const char* pszTempValue, float flOldValue); + bool SetColorFromString(const char* pszValue); + bool ClampValue(float& value); + + bool IsRegistered(void) const; + bool IsCommand(void) const; + bool IsFlagSet(int nFlags) const; + + struct CVValue_t + { + const char* m_pszString; + int64_t m_iStringLength; + float m_fValue; + int m_nValue; + }; + + ConCommandBase m_ConCommandBase {}; // 0x0000 + const char* m_pszDefaultValue {}; // 0x0040 + CVValue_t m_Value {}; // 0x0048 + bool m_bHasMin {}; // 0x005C + float m_fMinVal {}; // 0x0060 + bool m_bHasMax {}; // 0x0064 + float m_fMaxVal {}; // 0x0068 + void* m_pMalloc {}; // 0x0070 + char m_pPad80[10] {}; // 0x0080 +}; // Size: 0x0080 + +int ParseConVarFlagsString(std::string modName, std::string flags); diff --git a/primedev/core/convar/cvar.cpp b/primedev/core/convar/cvar.cpp new file mode 100644 index 00000000..aa5f0365 --- /dev/null +++ b/primedev/core/convar/cvar.cpp @@ -0,0 +1,26 @@ +#include "cvar.h" +#include "convar.h" +#include "concommand.h" + +//----------------------------------------------------------------------------- +// Purpose: returns all ConVars +//----------------------------------------------------------------------------- +std::unordered_map<std::string, ConCommandBase*> CCvar::DumpToMap() +{ + std::stringstream ss; + CCVarIteratorInternal* itint = FactoryInternalIterator(); // Allocate new InternalIterator. + + std::unordered_map<std::string, ConCommandBase*> allConVars; + + for (itint->SetFirst(); itint->IsValid(); itint->Next()) // Loop through all instances. + { + ConCommandBase* pCommand = itint->Get(); + const char* pszCommandName = pCommand->m_pszName; + allConVars[pszCommandName] = pCommand; + } + + return allConVars; +} + +SourceInterface<CCvar>* g_pCVarInterface; +CCvar* g_pCVar; diff --git a/primedev/core/convar/cvar.h b/primedev/core/convar/cvar.h new file mode 100644 index 00000000..beaa84f4 --- /dev/null +++ b/primedev/core/convar/cvar.h @@ -0,0 +1,38 @@ +#pragma once +#include "convar.h" + +//----------------------------------------------------------------------------- +// Forward declarations +//----------------------------------------------------------------------------- +class ConCommandBase; +class ConCommand; +class ConVar; + +//----------------------------------------------------------------------------- +// Internals for ICVarIterator +//----------------------------------------------------------------------------- +class CCVarIteratorInternal // Fully reversed table, just look at the virtual function table and rename the function. +{ +public: + virtual void SetFirst(void) = 0; // 0 + virtual void Next(void) = 0; // 1 + virtual bool IsValid(void) = 0; // 2 + virtual ConCommandBase* Get(void) = 0; // 3 +}; + +//----------------------------------------------------------------------------- +// Default implementation +//----------------------------------------------------------------------------- +class CCvar +{ +public: + M_VMETHOD(ConCommandBase*, FindCommandBase, 14, (const char* pszCommandName), (this, pszCommandName)); + M_VMETHOD(ConVar*, FindVar, 16, (const char* pszVarName), (this, pszVarName)); + M_VMETHOD(ConCommand*, FindCommand, 18, (const char* pszCommandName), (this, pszCommandName)); + M_VMETHOD(CCVarIteratorInternal*, FactoryInternalIterator, 41, (), (this)); + + std::unordered_map<std::string, ConCommandBase*> DumpToMap(); +}; + +extern SourceInterface<CCvar>* g_pCVarInterface; +extern CCvar* g_pCVar; |