Skip to content

Commit

Permalink
IMPRPOVED CatVar class
Browse files Browse the repository at this point in the history
  • Loading branch information
nullifiedcat committed Mar 2, 2017
1 parent 8eca92b commit 04868f1
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 57 deletions.
73 changes: 59 additions & 14 deletions src/cvwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,63 @@
#include "common.h"
#include "sdk.h"

CatVar::CatVar(CatVar_t type, std::string name, std::string value, std::string help, CatEnum* enum_type, std::string long_description, bool hasminmax, float maxv, float minv) {
m_Type = type;
m_pConVar = CreateConVar(CON_PREFIX + name, value, help);
m_EnumType = enum_type;
m_flMinValue = minv;
m_flMaxValue = maxv;
m_bHasMinmax = hasminmax;
SetDescription(long_description);
}

CatEnum::CatEnum(std::vector<std::string> values, int min) {
m_values = values;
CatVar::CatVar(CatVar_t type, std::string name, std::string value, std::string help, CatEnum* enum_type, std::string long_description, bool hasminmax, float maxv, float minv)
: type(type), name(name), defaults(value), desc_short(help), desc_long(long_description), enum_type(enum_type) {
min = minv;
max = maxv;
restricted = hasminmax;
g_UnregisteredCatVars.push(this);
}

CatVar::CatVar(CatVar_t type, std::string name, std::string defaults, std::string desc_short, std::string desc_long)
: type(type), name(name), defaults(defaults), desc_short(desc_short), desc_long(desc_long), enum_type(nullptr), restricted(false) {
// For some reason, adding min(0.0f), max(0.0f) gives a compilation error.
min = 0.0f;
max = 0.0f;
g_UnregisteredCatVars.push(this);
}

CatVar::CatVar(CatVar_t type, std::string name, std::string defaults, std::string desc_short, std::string desc_long, float max_val)
: type(type), name(name), defaults(defaults), desc_short(desc_short), desc_long(desc_long), enum_type(nullptr), restricted(true) {
min = 0.0f;
max = max_val;
g_UnregisteredCatVars.push(this);
}

CatVar::CatVar(CatVar_t type, std::string name, std::string defaults, std::string desc_short, std::string desc_long, float min_val, float max_val)
: type(type), name(name), defaults(defaults), desc_short(desc_short), desc_long(desc_long), enum_type(nullptr), restricted(true) {
min = min_val;
max = max_val;
g_UnregisteredCatVars.push(this);
}

CatVar::CatVar(CatVar_t type, std::string name, std::string defaults, std::string desc_short, std::string desc_long, CatEnum& cat_enum)
: type(type), name(name), defaults(defaults), desc_short(desc_short), desc_long(desc_long), enum_type(&cat_enum), restricted(true) {
min = cat_enum.Minimum();
max = cat_enum.Maximum();
g_UnregisteredCatVars.push(this);
}

void CatVar::Register() {
convar = CreateConVar(CON_PREFIX + name, defaults, desc_short);
convar_parent = convar->m_pParent;
while (!callbacks.empty()) {
callbacks.top()(this);
callbacks.pop();
}
registered = true;
}

std::stack<CatVar*> g_UnregisteredCatVars;
void RegisterCatVars() {
while (g_UnregisteredCatVars.size()) {
CatVar* var = g_UnregisteredCatVars.top();
var->Register();
g_UnregisteredCatVars.pop();
}
}

CatEnum::CatEnum(std::vector<std::string> values, int min) : m_values(values) {
m_iMin = min;
m_iMax = min + values.size() - 1;
m_iLength = values.size();
Expand All @@ -34,11 +79,11 @@ std::string CatEnum::Name(int value) {
return "unknown";
}

int CatEnum::Maximum() {
int CatEnum::Maximum() const {
return m_iMax;
}

int CatEnum::Minimum() {
int CatEnum::Minimum() const {
return m_iMin;
}

62 changes: 38 additions & 24 deletions src/cvwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ConVar;
#include "beforecheaders.h"
#include <string>
#include <vector>
#include <functional>
#include <stack>
#include "aftercheaders.h"

Expand All @@ -37,52 +38,65 @@ class CatEnum {
public:
CatEnum(std::vector<std::string> values, int min = 0);
std::string Name(int value);
int Maximum();
int Minimum();
std::vector<std::string> m_values;
int Maximum() const ;
int Minimum() const ;
const std::vector<std::string> m_values;
int m_iMin;
int m_iMax;
int m_iLength;
};

class CatVar {
public:
[[deprecated]]
CatVar(CatVar_t type, std::string name, std::string value, std::string help, CatEnum* enum_type = 0, std::string long_description = "no description", bool hasminmax = false, float max = 1.0f, float min = 0.0f);
inline CatVar_t GetType() { return m_Type; }
inline CatEnum* GetEnum() { return m_EnumType; }
inline ConVar* GetConVar() { return m_pConVar; }
inline void SetDescription(std::string description) { m_strDescription = description; }
inline std::string Description() { return m_strDescription; }

CatVar(CatVar_t type, std::string name, std::string defaults, std::string desc_short, std::string desc_long);
CatVar(CatVar_t type, std::string name, std::string defaults, std::string desc_short, std::string desc_long, float max_val);
CatVar(CatVar_t type, std::string name, std::string defaults, std::string desc_short, std::string desc_long, float min_val, float max_val);
CatVar(CatVar_t type, std::string name, std::string defaults, std::string desc_short, std::string desc_long, CatEnum& cat_enum);

inline explicit operator bool() const { return !!convar_parent->m_nValue; }
inline explicit operator int() const { return convar_parent->m_nValue; }
inline explicit operator float() const { return convar_parent->m_fValue; }
inline void operator =(const int& value) { convar_parent->InternalSetIntValue(value); }
inline void operator =(const float& value) { convar_parent->InternalSetFloatValue(value); }
inline bool operator ==(const int& value) { return convar_parent->m_nValue == value; }
inline bool operator ==(const float& value) { return convar_parent->m_fValue == value; }
inline bool operator ==(const int& value) const { return convar_parent->m_nValue == value; }
inline bool operator ==(const float& value) const { return convar_parent->m_fValue == value; }

void Register();
typedef std::function<void(CatVar*)> RegisterCallbackFn;
std::stack<RegisterCallbackFn> callbacks;
inline void OnRegister(RegisterCallbackFn fn) {
if (registered) fn(this);
else callbacks.push(fn);
}

[[deprecated]]
inline bool GetBool() const { return m_pConVar->GetBool(); }
inline bool GetBool() const { return this->operator bool(); }
[[deprecated]]
inline int GetInt() const { return m_pConVar->GetInt(); }
inline int GetInt() const { return this->operator int(); }
[[deprecated]]
inline float GetFloat() const { return m_pConVar->GetFloat(); };
inline const char* GetString() const { return m_pConVar->GetString(); }
inline float GetFloat() const { return this->operator float(); };
inline const char* GetString() const { return convar_parent->GetString(); }
[[deprecated]]
inline void SetValue(float value) { m_pConVar->SetValue(value); }
inline void SetValue(std::string value) { m_pConVar->SetValue(value.c_str()); }
inline void SetValue(float value) { this->operator =(value); }
inline void SetValue(std::string value) { convar_parent->SetValue(value.c_str()); }
[[deprecated]]
inline void SetValue(int value) { m_pConVar->SetValue(value); }
inline void SetValue(int value) { this->operator =(value); }

bool m_bHasMinmax;
float m_flMaxValue;
float m_flMinValue;
bool restricted;
float max;
float min;
bool registered;

std::string m_strDescription;
CatEnum* m_EnumType;
CatVar_t m_Type;
ConVar* m_pConVar;
const CatVar_t type;
const std::string name;
const std::string defaults;
const std::string desc_short;
const std::string desc_long;
CatEnum* enum_type;
ConVar* convar;
ConVar* convar_parent;
};

Expand Down
4 changes: 3 additions & 1 deletion src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ void GlobalSettings::Init() {
this->bNoVisuals = new CatVar(CV_SWITCH, "novisuals", "0", "Disable visuals", NULL, "Disable all visuals");
this->bCleanScreenshots = new CatVar(CV_SWITCH, "clean_screenshot", "1", "Clean screenshots", NULL, "Clean screenshots");
this->bDebugLog = new CatVar(CV_SWITCH, "log", "1", "Debug Log", NULL, "Disable this if you don't need cathook messages in your console");
this->bThirdperson->m_pConVar->InstallChangeCallback(ThirdpersonCallback);
this->bThirdperson->OnRegister([](CatVar* var) {
var->convar->InstallChangeCallback(ThirdpersonCallback);
});
this->bFastOutline = new CatVar(CV_SWITCH, "fastoutline", "0", "Low quality outline", NULL, "Might increase performance when there is a lot of ESP text to draw");
this->kRollSpeedhack = new CatVar(CV_KEY, "rollspeedhack", "0", "Roll Speedhack", NULL, "Roll speedhack key");
bInvalid = true;
Expand Down
28 changes: 14 additions & 14 deletions src/gui/CCVarContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
#include "../common.h"
#include "../sdk.h"

CCVarContainer::CCVarContainer(IWidget* parent, CatVar* var) : CBaseContainer(("cvc_" + std::string(var->m_pConVar->GetName())), parent) {
CCVarContainer::CCVarContainer(IWidget* parent, CatVar* var) : CBaseContainer("cvc_" + var->name, parent) {
m_pVar = var;
m_pInput = 0;
m_pLabel = new CTextLabel(GetName() + "_desc", this, std::string(var->m_pConVar->GetHelpText()));
m_pLabel = new CTextLabel(GetName() + "_desc", this, var->desc_short);
m_pControl = 0;
if (var->Description().length()) {
Props()->SetString("tooltip", var->Description().c_str());
if (var->desc_short.length()) {
Props()->SetString("tooltip", var->desc_short.c_str());
}
bool needsinput = false;
switch (var->GetType()) {
switch (var->type) {
case CatVar_t::CV_SWITCH: {// Label, Checkbox
CCheckbox* cb = new CCheckbox(GetName() + "_control", this, var->GetBool());
cb->SetCallback([this](CCheckbox*, bool value) {
Expand All @@ -37,19 +37,19 @@ CCVarContainer::CCVarContainer(IWidget* parent, CatVar* var) : CBaseContainer(("
} break;
case CatVar_t::CV_ENUM: { // Most difficult thing, dropdown menu
CDropdown* dd = new CDropdown(GetName() + "_control", this);
for (int i = var->GetEnum()->Minimum(); i <= var->GetEnum()->Maximum(); i++) {
dd->AddValue(var->GetEnum()->Name(i));
for (int i = var->enum_type->Minimum(); i <= var->enum_type->Maximum(); i++) {
dd->AddValue(var->enum_type->Name(i));
}
dd->SetCallback([this](CDropdown*, int value) {
m_pVar->SetValue(value);
});
dd->Props()->SetInt("offset", var->GetEnum()->Minimum());
dd->Props()->SetInt("offset", var->enum_type->Minimum());
m_pControl = dd;
} break;
case CatVar_t::CV_FLOAT: {
if (var->m_bHasMinmax) {
if (var->restricted) {
CSlider* sl = new CSlider(GetName() + "_control", this);
sl->Setup(var->m_flMinValue, var->m_flMaxValue);
sl->Setup(var->min, var->max);
sl->SetValue(var->GetFloat());
m_pControl = sl;
sl->SetCallback([this](CSlider*, float oldv, float newv) {
Expand All @@ -59,9 +59,9 @@ CCVarContainer::CCVarContainer(IWidget* parent, CatVar* var) : CBaseContainer(("
needsinput = true;
} break;
case CatVar_t::CV_INT: {
if (var->m_bHasMinmax) {
if (var->restricted) {
CSlider* sl = new CSlider(GetName() + "_control", this);
sl->Setup(var->m_flMinValue, var->m_flMaxValue);
sl->Setup(var->min, var->max);
sl->SetStep(1.0f);
sl->SetValue(var->GetInt());
sl->SetCallback([this](CSlider*, float oldv, float newv) {
Expand All @@ -88,7 +88,7 @@ CCVarContainer::CCVarContainer(IWidget* parent, CatVar* var) : CBaseContainer(("
m_pInput = new CTextInput(GetName() + "_input", this);
m_pInput->SetValue(std::string(var->GetString()));
m_pInput->SetCallback([this](CTextInput*, std::string old, std::string newv) {
if (m_pVar->GetType() == CV_STRING) {
if (m_pVar->type == CV_STRING) {
m_pVar->SetValue(newv);
} else {
try {
Expand Down Expand Up @@ -148,7 +148,7 @@ void CCVarContainer::Update() {
if (!m_pInput->IsFocused()) m_pInput->SetValue(m_pVar->GetString());
}
if (m_pControl && !m_pControl->IsFocused()) {
switch (m_pVar->GetType()) {
switch (m_pVar->type) {
case CatVar_t::CV_ENUM: {
dynamic_cast<CDropdown*>(m_pControl)->SetValue(m_pVar->GetInt());
} break;
Expand Down
4 changes: 3 additions & 1 deletion src/gui/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ CatGUI::~CatGUI() {
void CatGUI::Setup() {
m_pRootWindow = new RootWindow();
m_pRootWindow->Setup();
v_bGUIVisible->m_pConVar->InstallChangeCallback(GUIVisibleCallback);
v_bGUIVisible->OnRegister([](CatVar* var) {
var->convar->InstallChangeCallback(GUIVisibleCallback);
});
}

void CatGUI::ShowTooltip(std::string text) {
Expand Down
2 changes: 1 addition & 1 deletion src/hack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ void hack::Initialize() {
hack::InitHacks();
g_Settings.Init();
g_pGUI = new CatGUI();
g_pGUI->Setup();
EndConVars();
g_pGUI->Setup();
gNetvars.init();
InitNetVars();
g_pLocalPlayer = new LocalPlayer();
Expand Down
2 changes: 1 addition & 1 deletion src/hacks/KillSay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ KillSay::~KillSay() {
}

void KillSay::Reload() {
m_TextFile->LoadFile(v_sFileName->m_pConVar->GetString());
m_TextFile->LoadFile(v_sFileName->GetString());
}

void CC_KillSay_ReloadFile(const CCommand& args) {
Expand Down
1 change: 1 addition & 0 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void BeginConVars() {
}

void EndConVars() {
RegisterCatVars();
if (hConVarsFile) {
fprintf(hConVarsFile, "\nexec cat_autoexec\n");
fprintf(hConVarsFile, "cat_killsay_reload\ncat_spam_reload\n");
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/others.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void Shutdown_hook(void* thisptr, const char* reason) {
SEGV_BEGIN;
if (g_Settings.bHackEnabled->GetBool()) {
const char* new_reason = reason;
if (g_Settings.sDisconnectMsg->m_pConVar->m_StringLength > 3) {
if (g_Settings.sDisconnectMsg->convar->m_StringLength > 3) {
new_reason = g_Settings.sDisconnectMsg->GetString();
}
((Shutdown_t*)hooks::hkNetChannel->GetMethod(hooks::offShutdown))(thisptr, new_reason);
Expand Down

0 comments on commit 04868f1

Please sign in to comment.