iw4x-client/iw4/Components/Dvar.cpp

136 lines
3.3 KiB
C++
Raw Normal View History

2015-12-23 08:45:53 -05:00
#include "..\STDInclude.hpp"
namespace Components
{
Dvar::Var::Var(std::string dvarName) : Var()
{
this->dvar = Game::Dvar_FindVar(dvarName.data());
if (!this->dvar)
{
// Register the dvar?
}
}
2015-12-23 11:12:15 -05:00
template <> Game::dvar_t* Dvar::Var::Get()
{
return this->dvar;
}
2015-12-23 08:45:53 -05:00
template <> char* Dvar::Var::Get()
{
if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_STRING && this->dvar->current.string)
{
return this->dvar->current.string;
}
return "";
}
template <> const char* Dvar::Var::Get()
{
return this->Get<char*>();
}
template <> int Dvar::Var::Get()
{
if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_INT)
{
return this->dvar->current.integer;
}
return 0;
}
template <> float Dvar::Var::Get()
{
if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT)
{
return this->dvar->current.value;
}
return 0;
}
template <> float* Dvar::Var::Get()
{
static float val[4] = { 0 };
if (this->dvar && (this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_2 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_3 || this->dvar->type == Game::dvar_type::DVAR_TYPE_FLOAT_4))
{
return this->dvar->current.vec4;
}
return val;
}
template <> bool Dvar::Var::Get()
{
if (this->dvar && this->dvar->type == Game::dvar_type::DVAR_TYPE_BOOL)
{
return this->dvar->current.boolean;
}
return false;
}
void Dvar::Var::Set(char* string)
{
this->Set(reinterpret_cast<const char*>(string));
}
void Dvar::Var::Set(const char* string)
{
if (this->dvar && this->dvar->name)
{
Game::Dvar_SetCommand(this->dvar->name, string);
}
}
void Dvar::Var::Set(std::string string)
{
this->Set(string.data());
}
void Dvar::Var::Set(int integer)
{
if (this->dvar && this->dvar->name)
{
Game::Dvar_SetCommand(this->dvar->name, Utils::VA("%i", integer));
}
}
void Dvar::Var::Set(float value)
{
if (this->dvar && this->dvar->name)
{
Game::Dvar_SetCommand(this->dvar->name, Utils::VA("%f", value));
}
}
template<> static Dvar::Var Dvar::Var::Register(const char* name, bool value, Game::dvar_flag flag, const char* description)
{
return Game::Dvar_RegisterBool(name, value, flag, description);
}
template<> static Dvar::Var Dvar::Var::Register(const char* name, const char* value, Game::dvar_flag flag, const char* description)
{
return Game::Dvar_RegisterString(name, value, flag, description);
}
2015-12-23 11:12:15 -05:00
Game::dvar_t* Dvar::RegisterName(const char* name, const char* default, Game::dvar_flag flag, const char* description)
{
// TODO: Register string dvars here
return Dvar::Var::Register<const char*>(name, "Unknown Soldier", (Game::dvar_flag)(flag | Game::dvar_flag::DVAR_FLAG_SAVED), description).Get<Game::dvar_t*>();
}
2015-12-23 08:45:53 -05:00
Dvar::Dvar()
{
// set flags of cg_drawFPS to archive
2015-12-23 09:00:01 -05:00
Utils::Hook::Or<BYTE>(0x4F8F69, Game::dvar_flag::DVAR_FLAG_SAVED);
2015-12-23 08:45:53 -05:00
// un-cheat cg_fov and add archive flags
2015-12-23 09:00:01 -05:00
Utils::Hook::Xor<BYTE>(0x4F8E35, Game::dvar_flag::DVAR_FLAG_CHEAT | Game::dvar_flag::DVAR_FLAG_SAVED);
2015-12-23 08:45:53 -05:00
2015-12-23 21:26:46 -05:00
// set flags of cg_drawFPS to archive
Utils::Hook::Or<BYTE>(0x4F8F69, Game::dvar_flag::DVAR_FLAG_SAVED);
2015-12-23 08:45:53 -05:00
// set cg_fov max to 90.0
static float cgFov90 = 90.0f;
2015-12-23 09:00:01 -05:00
Utils::Hook::Set<float*>(0x4F8E28, &cgFov90);
2015-12-23 08:45:53 -05:00
2015-12-23 11:12:15 -05:00
// Hook dvar 'name' registration
Utils::Hook(0x40531C, Dvar::RegisterName, HOOK_CALL).Install()->Quick();
2015-12-23 08:45:53 -05:00
}
}