From 36f26e7b346a39ab2c9fe3f2db3b4c1bff318745 Mon Sep 17 00:00:00 2001 From: nullifiedcat Date: Sat, 28 Jul 2018 20:14:10 +0300 Subject: [PATCH] start removing catvars --- include/CMakeLists.txt | 1 + include/core/cvwrapper.hpp | 157 ------------------- include/hacks/Spam.hpp | 3 +- include/settings/Bool.hpp | 82 ++++++++++ include/settings/CMakeLists.txt | 17 +++ include/settings/Float.hpp | 55 +++++++ include/settings/Int.hpp | 48 ++++++ include/settings/Key.hpp | 143 +++++++++++++++++ include/settings/Manager.hpp | 49 ++++++ include/settings/Registered.hpp | 45 ++++++ include/settings/Rgba.hpp | 88 +++++++++++ include/settings/Settings.hpp | 146 ++++++++++++++++++ include/settings/SettingsIO.hpp | 55 +++++++ include/settings/String.hpp | 54 +++++++ src/backpacktf.cpp | 13 +- src/chatlog.cpp | 18 +-- src/core/cvwrapper.cpp | 263 +------------------------------- src/crits.cpp | 18 +-- src/entitycache.cpp | 12 +- src/entityhitboxcache.cpp | 7 +- src/hacks/Spam.cpp | 45 +++--- 21 files changed, 836 insertions(+), 483 deletions(-) create mode 100644 include/settings/Bool.hpp create mode 100644 include/settings/CMakeLists.txt create mode 100644 include/settings/Float.hpp create mode 100644 include/settings/Int.hpp create mode 100644 include/settings/Key.hpp create mode 100644 include/settings/Manager.hpp create mode 100644 include/settings/Registered.hpp create mode 100644 include/settings/Rgba.hpp create mode 100644 include/settings/Settings.hpp create mode 100644 include/settings/SettingsIO.hpp create mode 100644 include/settings/String.hpp diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 588f81d7..2f2991a6 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -52,6 +52,7 @@ add_subdirectory(hooks) add_subdirectory(reclasses) add_subdirectory(sdk) add_subdirectory(online) +add_subdirectory(settings) if(EnableVisuals) add_subdirectory(visual) diff --git a/include/core/cvwrapper.hpp b/include/core/cvwrapper.hpp index b8171f82..44623b87 100644 --- a/include/core/cvwrapper.hpp +++ b/include/core/cvwrapper.hpp @@ -16,36 +16,6 @@ class ConVar; #include #include -// Catvar types -enum CatVar_t -{ - CV_SWITCH, - CV_INT, - CV_FLOAT, - CV_STRING, - CV_ENUM, - CV_KEY -}; - -// TODO reverse -// Enum Something -class CatEnum -{ -public: - CatEnum(std::vector values, int min = 0); - std::string Name(int value); - -public: - const std::vector value_names; - int min_value; - int max_value; - int size; -}; -namespace hacks::tf2::global -{ -void runcfg(); -} -// TODO reverse, no idea how catcommands are handled class CatCommand { public: @@ -66,133 +36,6 @@ public: ConCommand *cmd{ nullptr }; }; -class CatVar -{ -public: // TODo, unknown reverse - CatVar(CatVar_t type, std::string name, std::string defaults, - std::string desc_short, std::string desc_long = "no description"); - 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(CatEnum &cat_enum, std::string name, std::string defaults, - std::string desc_short, std::string desc_long); - - inline operator bool() const - { - if (this && convar_parent && convar_parent->IsRegistered() && - this->registered) - return !!convar_parent->m_nValue; - else - return false; - } - inline operator int() const - { - return convar_parent->m_nValue; - } - inline 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) 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 RegisterCallbackFn; - void OnRegister(RegisterCallbackFn fn); - void InstallChangeCallback(FnChangeCallback_t callback); - - [[deprecated]] inline bool GetBool() const - { - return this->operator bool(); - } - [[deprecated]] inline int GetInt() const - { - return this->operator int(); - } - [[deprecated]] inline float GetFloat() const - { - return this->operator float(); - }; - inline const char *GetString() const - { - return convar_parent->GetString(); - } - [[deprecated]] 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) - { - this->operator=(value); - } - - inline bool KeyDown() - { - return g_IInputSystem->IsButtonDown( - static_cast(static_cast(*this))); - } - - inline const std::string &GetBase() const - { - return current_base; - } - - // Storage for the catvar -public: - const CatVar_t type; - const std::string name; - const std::string defaults{ "0" }; - const std::string desc_short{ "" }; - const std::string desc_long{ "" }; - CatEnum *enum_type{ nullptr }; - - std::string current_base{ "0" }; - - bool restricted{ false }; - float min{ 0.0f }; - float max{ 0.0f }; - - std::vector callbacks{}; - bool registered{ false }; - - ConVar *convar{ nullptr }; - ConVar *convar_parent{ nullptr }; - - int id{ 0 }; - static int last_id; -}; - -// TODO, find out what this formatting does "std::vector& -// ArrayName()?" - -// needed for registration -std::vector ®istrationArray(); std::vector &commandRegistrationArray(); -// List to store catvars -std::vector &CatVarList(); -// Setup commands probs needed at int -void RegisterCatCommands(); void RegisterCatVars(); -int GetRebasedCatVarCount(); diff --git a/include/hacks/Spam.hpp b/include/hacks/Spam.hpp index c4e88c05..165333ae 100755 --- a/include/hacks/Spam.hpp +++ b/include/hacks/Spam.hpp @@ -21,8 +21,7 @@ extern const std::vector builtin_nonecore; extern const std::vector builtin_lmaobox; extern const std::vector builtin_lithium; -extern CatVar spam_source; - +bool isActive(); void Init(); void CreateMove(); void Reload(); diff --git a/include/settings/Bool.hpp b/include/settings/Bool.hpp new file mode 100644 index 00000000..ef9f1fd5 --- /dev/null +++ b/include/settings/Bool.hpp @@ -0,0 +1,82 @@ +/* + Created on 01.07.18. +*/ + +#pragma once + +#include "Settings.hpp" +#include "Manager.hpp" + +namespace settings +{ + +template<> +class Variable: public VariableBase +{ +public: + ~Variable() override = default; + + Variable() = default; + + explicit Variable(bool v) + { + setInternal(v); + } + + VariableType getType() override + { + return VariableType::BOOL; + } + + void fromString(const std::string& string) override + { + if (string == "0" || string == "false") + setInternal(false); + else if (string == "1" || string == "true") + setInternal(true); + } + + Variable& operator=(const std::string& string) + { + fromString(string); + } + + const std::string &toString() override + { + return string; + } + + explicit operator bool() const + { + return value; + } + Variable& operator=(bool next) + { + setInternal(next); + } + + const bool &operator*() override + { + return value; + } + + void flip() + { + setInternal(!value); + } + +protected: + void setInternal(bool next) + { + if (next == value) + return; + fireCallbacks(next); + value = next; + string = value ? "true" : "false"; + } + + bool value{ false }; + std::string string{ "false" }; +}; + +} \ No newline at end of file diff --git a/include/settings/CMakeLists.txt b/include/settings/CMakeLists.txt new file mode 100644 index 00000000..2eb3b932 --- /dev/null +++ b/include/settings/CMakeLists.txt @@ -0,0 +1,17 @@ +target_sources(cathook PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/Bool.hpp" + "${CMAKE_CURRENT_LIST_DIR}/Float.hpp" + "${CMAKE_CURRENT_LIST_DIR}/Int.hpp" + "${CMAKE_CURRENT_LIST_DIR}/Key.hpp" + "${CMAKE_CURRENT_LIST_DIR}/Manager.hpp" + "${CMAKE_CURRENT_LIST_DIR}/Registered.hpp" + "${CMAKE_CURRENT_LIST_DIR}/Settings.hpp" + "${CMAKE_CURRENT_LIST_DIR}/SettingsIO.hpp" + "${CMAKE_CURRENT_LIST_DIR}/String.hpp") + +if (EnableVisuals) + target_sources(cathook PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/Rgba.hpp") +endif() + +target_include_directories(cathook PRIVATE "${CMAKE_CURRENT_LIST_DIR}") \ No newline at end of file diff --git a/include/settings/Float.hpp b/include/settings/Float.hpp new file mode 100644 index 00000000..66564ff1 --- /dev/null +++ b/include/settings/Float.hpp @@ -0,0 +1,55 @@ +/* + Created on 01.07.18. +*/ + +#pragma once + +#include "Settings.hpp" + +namespace settings +{ + +template<> +class Variable: public ArithmeticVariable +{ +public: + ~Variable() override = default; + + VariableType getType() override + { + return VariableType::FLOAT; + } + + void fromString(const std::string &string) override + { + errno = 0; + auto next = std::strtof(string.c_str(), nullptr); + if (next == 0.0f && errno) + return; + set(next); + } + + Variable& operator=(const std::string& string) + { + fromString(string); + } + + Variable& operator=(const float& next) + { + set(next); + } + + explicit operator bool() const + { + return value != 0.0f; + } +protected: + void updateString() override + { + char str[32]; + snprintf(str, 31, "%.2f", value); + string = str; + } +}; + +} \ No newline at end of file diff --git a/include/settings/Int.hpp b/include/settings/Int.hpp new file mode 100644 index 00000000..535bd035 --- /dev/null +++ b/include/settings/Int.hpp @@ -0,0 +1,48 @@ +/* + Created on 01.07.18. +*/ + +#pragma once + +#include "Settings.hpp" + +namespace settings +{ + +template<> +class Variable: public ArithmeticVariable +{ +public: + ~Variable() override = default; + + VariableType getType() override + { + return VariableType::INT; + } + + void fromString(const std::string& string) override + { + errno = 0; + auto result = std::strtol(string.c_str(), nullptr, 10); + if (result == 0 && errno) + return; + set(result); + } + + Variable& operator=(const std::string& string) + { + fromString(string); + } + + Variable& operator=(const int& next) + { + set(next); + } + + explicit operator bool() const + { + return value != 0; + } +}; + +} \ No newline at end of file diff --git a/include/settings/Key.hpp b/include/settings/Key.hpp new file mode 100644 index 00000000..5bf79f16 --- /dev/null +++ b/include/settings/Key.hpp @@ -0,0 +1,143 @@ +/* + Created on 01.07.18. +*/ + +#pragma once + +#include +#include +#include +#include "Settings.hpp" + +namespace settings +{ + +struct Key +{ + int mouse{ 0 }; + SDL_Scancode scan{ static_cast(0) }; +}; + +template<> +class Variable: public VariableBase +{ +public: + ~Variable() override = default; + + VariableType getType() override + { + return VariableType::KEY; + } + + // Valid inputs: "Mouse1", "Mouse5", "Key 6", "Key 10", "Key 2", "Space". + void fromString(const std::string& string) override + { + if (string == "") + { + reset(); + return; + } + + Key key{}; + if (string.find("Mouse") != std::string::npos) + { + key.mouse = std::strtol(string.c_str() + 5, nullptr, 10); + } + else if (string.find("Key ") != std::string::npos) + { + key.scan = static_cast(std::strtol(string.c_str() + 4, + nullptr, 10)); + } + else + { + auto code = SDL_GetKeyFromName(string.c_str()); + if (code != SDLK_UNKNOWN) + key.scan = SDL_GetScancodeFromKey(code); + } + + setInternal(key); + } + + Variable& operator=(const std::string& string) + { + fromString(string); + } + + Variable& reset() + { + setInternal(Key{}); + } + + Variable& key(SDL_Scancode key) + { + Key k{}; + k.scan = key; + setInternal(k); + } + + Variable& mouse(int mouse_key) + { + Key k{}; + k.mouse = mouse_key; + setInternal(k); + } + + const Key &operator*() override + { + return value; + } + + const std::string &toString() override + { + return string; + } + + explicit operator bool() const + { + return value.mouse || value.scan; + } + + bool isKeyDown() const + { + if (value.mouse) + { + auto flag = SDL_GetMouseState(nullptr, nullptr); + if (flag & SDL_BUTTON(value.mouse)) + return true; + } + else + { + auto keys = SDL_GetKeyboardState(nullptr); + if (keys[value.scan]) + return true; + } + return false; + } + +protected: + void setInternal(Key next) + { + if (next.mouse) + string = "Mouse" + std::to_string(next.mouse); + else + { + if (next.scan == static_cast(0)) + string = ""; + else + { + const char *s = SDL_GetKeyName(SDL_GetKeyFromScancode(next.scan)); + if (!s || *s == 0) + string = "Key " + std::to_string(next.scan); + else + string = s; + } + } + value = next; + fireCallbacks(next); + } + + std::string string{}; + Key value{}; +}; + +} \ No newline at end of file diff --git a/include/settings/Manager.hpp b/include/settings/Manager.hpp new file mode 100644 index 00000000..64616820 --- /dev/null +++ b/include/settings/Manager.hpp @@ -0,0 +1,49 @@ +/* + Created on 02.07.18. +*/ + +#pragma once + +#include +#include +#include "Settings.hpp" + +/* + * Default value = value when variable was registered + * + * Manager needs to be able to: + * - Save config + * - Load config + * - Get value by name + * - Set value by name + * - Reset variable to defaults + * - Reset everything to defaults + * - Rebase config (hmmm) + */ + +namespace settings +{ + +class Manager +{ +public: + struct VariableDescriptor + { + explicit VariableDescriptor(IVariable& variable); + + bool isChanged(); + + IVariable& variable; + std::string defaults{}; + VariableType type{}; + }; + + static Manager& instance(); +public: + void add(IVariable& me, std::string name); + IVariable *lookup(const std::string& string); + + std::unordered_map registered{}; +}; + +} \ No newline at end of file diff --git a/include/settings/Registered.hpp b/include/settings/Registered.hpp new file mode 100644 index 00000000..483897da --- /dev/null +++ b/include/settings/Registered.hpp @@ -0,0 +1,45 @@ +/* + Created on 02.07.18. +*/ + +#pragma once + +#include "Settings.hpp" + +namespace settings +{ + +void registerVariable(IVariable& variable, std::string name); + +template +class RegisteredVariableProxy: public Variable +{ +public: + using Variable::operator=; + + explicit RegisteredVariableProxy(std::string name) + { + registerVariable(*this, name); + } + + RegisteredVariableProxy(std::string name, std::string value) + { + this->fromString(value); + registerVariable(*this, name); + } +}; + +template +using RVariable = RegisteredVariableProxy; + +using Bool = RegisteredVariableProxy; +using Int = RegisteredVariableProxy; +using Float = RegisteredVariableProxy; +using String = RegisteredVariableProxy; +using Button = RegisteredVariableProxy; + +#if ENABLE_VISUALS +using Rgba = RegisteredVariableProxy; +#endif + +} \ No newline at end of file diff --git a/include/settings/Rgba.hpp b/include/settings/Rgba.hpp new file mode 100644 index 00000000..b449654b --- /dev/null +++ b/include/settings/Rgba.hpp @@ -0,0 +1,88 @@ +/* + Created on 01.07.18. +*/ + +#pragma once + +#include "Settings.hpp" + +constexpr uint8_t hexToInt(char c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + else if (c >= 'a' && c <= 'f') + return 10 + c - 'a'; + else if (c >= 'A' && c <= 'F') + return 10 + c - 'A'; + return 0; +} + +namespace settings +{ + +template<> +class Variable: public VariableBase +{ +public: + ~Variable() override = default; + + VariableType getType() override + { + return VariableType::COLOR; + } + + void fromString(const std::string& string) override + { + uint8_t rgba[4] = { 0, 0, 0, 255 }; + + for (int i = 0; i < string.length() && i < 8; ++i) + { + if (!(i % 2)) + rgba[i / 2] = 0; + rgba[i / 2] |= (hexToInt(string[i]) << ((i % 2) ? 0 : 4)); + } + + glez::rgba next{}; + next.r = float(rgba[0]) / 255.f; + next.g = float(rgba[1]) / 255.f; + next.b = float(rgba[2]) / 255.f; + next.a = float(rgba[3]) / 255.f; + setInternal(next); + } + + Variable& operator=(const glez::rgba& rgba) + { + setInternal(rgba); + } + + Variable& operator=(const std::string& string) + { + fromString(string); + } + + const std::string &toString() override + { + return string; + } + const glez::rgba &operator*() override + { + return value; + } +protected: + void setInternal(glez::rgba next) + { + fireCallbacks(next); + value = next; + uint8_t rgba_int[4] = { uint8_t(next.r * 255.f), uint8_t(next.g * 255.f), uint8_t(next.b * 255.f), uint8_t(next.a * 255.f) }; + for (int i = 0; i < 4; ++i) + { + string[i * 2] = "0123456789abcdef"[(rgba_int[i] >> 4u) & 0xF]; + string[i * 2 + 1] = "0123456789abcdef"[rgba_int[i] & 0xF]; + } + } + + glez::rgba value{}; + std::string string{ "00000000" }; +}; + +} \ No newline at end of file diff --git a/include/settings/Settings.hpp b/include/settings/Settings.hpp new file mode 100644 index 00000000..a2132dfb --- /dev/null +++ b/include/settings/Settings.hpp @@ -0,0 +1,146 @@ +/* + Created on 25.06.18. +*/ + +#pragma once + +#include +#include +#include + +#include + +/* + + Value types: + - bool + - int + - float + - string + - rgba + + Interface: + from/to String + get, set + on change callback + + increment / decrement + + set max string length + + set default value + reset to default + + */ + +namespace settings +{ + +enum class VariableType +{ + BOOL, + INT, + FLOAT, + STRING, + COLOR, + KEY +}; + +class IVariable +{ +public: + virtual ~IVariable() = default; + + // Faster than checking with dynamic_cast + virtual VariableType getType() = 0; + virtual void fromString(const std::string& string) = 0; + // Const reference because every variable will cache the string value instead of generating it every call + virtual const std::string& toString() = 0; +}; + +template +class VariableBase: public IVariable +{ +public: + using type = T; + + ~VariableBase() override = default; + + virtual const T& operator*() = 0; + + void installChangeCallback(std::function& var, T after)> callback) + { + callbacks.push_back(callback); + } +protected: + void fireCallbacks(T next) + { + for (auto& c: callbacks) + c(*this, next); + } + + std::vector&, T)>> callbacks{}; + T default_value{}; +}; + +template +class Variable +{ +}; + +template +class ArithmeticVariable: public VariableBase +{ +public: + ~ArithmeticVariable() override = default; + + explicit operator T() const + { + return value; + } + const std::string &toString() override + { + return string; + } + const T &operator*() override + { + return value; + } + + //T min{ std::numeric_limits::min() }; + //T max{ std::numeric_limits::max() }; + + virtual void set(T next) + { + //if (next < min) next = min; + //if (next > max) next = max; + if (next != value) + { + this->fireCallbacks(next); + value = next; + this->updateString(); + } + } + +protected: + T value{}; + std::string string{}; + + virtual void updateString() + { + string = std::to_string(value); + } +}; + +} + +#include "Bool.hpp" +#include "Float.hpp" +#include "Int.hpp" +#include "String.hpp" +#include "Key.hpp" +#include "Registered.hpp" + +#if ENABLE_VISUALS +#include "Rgba.hpp" +#endif \ No newline at end of file diff --git a/include/settings/SettingsIO.hpp b/include/settings/SettingsIO.hpp new file mode 100644 index 00000000..e36c115e --- /dev/null +++ b/include/settings/SettingsIO.hpp @@ -0,0 +1,55 @@ +/* + Created on 27.07.18. +*/ + +#pragma once + +#include +#include +#include +#include "Manager.hpp" + +namespace settings +{ + +class SettingsReader +{ +public: + explicit SettingsReader(Manager& manager); + + bool loadFrom(std::string path); +protected: + void pushChar(char c); + void finishString(bool complete); + void onReadKeyValue(std::string key, std::string value); + bool reading_key{ true }; + + bool escape{ false }; + bool quote{ false }; + bool comment{ false }; + bool was_non_space{ false }; + + std::string temporary_spaces{}; + std::string oss{}; + std::string stored_key{}; + std::ifstream stream{}; + Manager& manager; +}; + +class SettingsWriter +{ +public: + explicit SettingsWriter(Manager& manager); + + bool saveTo(std::string path, bool only_changed); + +protected: + void write(std::string name, IVariable *variable); + void writeEscaped(std::string str); + + bool only_changed{ false }; + std::ofstream stream{}; + Manager& manager; +}; + +} \ No newline at end of file diff --git a/include/settings/String.hpp b/include/settings/String.hpp new file mode 100644 index 00000000..37d29522 --- /dev/null +++ b/include/settings/String.hpp @@ -0,0 +1,54 @@ +/* + Created on 01.07.18. +*/ + +#pragma once + +#include "Settings.hpp" + +namespace settings +{ + +template<> +class Variable: public VariableBase +{ +public: + ~Variable() override = default; + + VariableType getType() override + { + return VariableType::STRING; + } + + void fromString(const std::string& string) override + { + fireCallbacks(string); + value = string; + } + + const std::string &toString() override + { + return value; + } + + Variable& operator=(const std::string& string) + { + fireCallbacks(std::string(string)); + value = string; + } + + const std::string &operator*() override + { + return value; + } + + explicit operator bool() const + { + return !value.empty(); + } + +protected: + std::string value{}; +}; + +} \ No newline at end of file diff --git a/src/backpacktf.cpp b/src/backpacktf.cpp index 337a3385..49294970 100644 --- a/src/backpacktf.cpp +++ b/src/backpacktf.cpp @@ -13,6 +13,9 @@ #include #include +#include + +static settings::Bool bptf_enable{ "backpack-tf.enable", "false" }; namespace backpacktf { @@ -21,14 +24,11 @@ std::unordered_map cache{}; std::queue pending_queue{}; std::mutex queue_mutex{}; std::mutex cache_mutex{}; +bool thread_running{ true }; std::string api_key_s = ""; bool valid_api_key = false; -static CatVar - enable_bptf(CV_SWITCH, "bptf_enable", "0", "Enable backpack.tf", - "Enable backpack.tf integration\nYou have to set your API " - "key in cat_bptf_key"); CatCommand api_key("bptf_key", "Set API Key", [](const CCommand &args) { api_key_s = args.ArgS(); logging::Info("API key changed!"); @@ -49,7 +49,7 @@ void store_data(unsigned id, float value, bool no_value, bool outdated_value); void processing_thread() { logging::Info("[bp.tf] Starting the thread"); - while (true) + while (thread_running) { if (enabled()) { @@ -188,7 +188,7 @@ void request_data(unsigned id) bool enabled() { - return enable_bptf && valid_api_key; + return bptf_enable && valid_api_key; } backpack_data_s &access_data(unsigned id) @@ -235,5 +235,6 @@ std::thread &GetBackpackTFThread() void init() { GetBackpackTFThread(); + } } diff --git a/src/chatlog.cpp b/src/chatlog.cpp index 3d45b0b4..04ed51f5 100644 --- a/src/chatlog.cpp +++ b/src/chatlog.cpp @@ -11,17 +11,15 @@ #include #include #include +#include + +static settings::Bool enable{ "chat-log.enable", "false" }; +static settings::Bool no_spam{ "chat-log.no-spam", "true" }; +static settings::Bool no_ipc{ "chat-log.no-ipc","true" }; namespace chatlog { -static CatVar enabled(CV_SWITCH, "chat_log", "0", "Chat log", - "Log chat to file"); -static CatVar dont_log_spam(CV_SWITCH, "chat_log_nospam", "1", "No Spam", - "Don't log your messages if spam is active"); -static CatVar dont_log_ipc(CV_SWITCH, "chat_log_noipc", "1", "No IPC", - "Don't log messages sent by bots"); - class csv_stream { public: @@ -107,17 +105,17 @@ csv_stream &logger() void LogMessage(int eid, std::string message) { - if (!enabled) + if (!enable) { return; } - if (dont_log_spam && hacks::shared::spam::spam_source and + if (no_spam && hacks::shared::spam::spam_source and eid == g_IEngine->GetLocalPlayer()) return; player_info_s info; if (not g_IEngine->GetPlayerInfo(eid, &info)) return; - if (dont_log_ipc && + if (no_ipc && playerlist::AccessData(info.friendsID).state == playerlist::k_EState::IPC) return; diff --git a/src/core/cvwrapper.cpp b/src/core/cvwrapper.cpp index 03907141..d6fc24ae 100644 --- a/src/core/cvwrapper.cpp +++ b/src/core/cvwrapper.cpp @@ -5,160 +5,9 @@ * Author: nullifiedcat */ -#include "common.hpp" - -int CatVar::last_id{ 0 }; - -int rebased_count{ 0 }; - -int GetRebasedCatVarCount() -{ - return rebased_count; -} -enum torun -{ - rebase = 0, - resetbase, - save, - save_complete -}; -int torun = -1; -std::string Args[3]; -namespace hacks::tf2::global -{ -void runcfg() -{ - if (torun == -1) - return; - if (torun == rebase) - { - for (auto &cv : CatVarList()) - { - std::string value(cv->GetString()); - if (value != cv->defaults) - { - cv->current_base = value; - rebased_count++; - } - } - logging::Info("Successfully rebased %d variables", rebased_count); - } - else if (torun == resetbase) - { - for (auto &cv : CatVarList()) - { - cv->current_base = cv->defaults; - } - rebased_count = 0; - } - else if (torun == save) - { - std::string filename("lastcfg"); - if (Args[0] != "") - { - filename = Args[0]; - Args[0] = ""; - } - std::string path = format("tf/cfg/cat_", filename, ".cfg"); - logging::Info("Saving settings to %s", path.c_str()); - if (GetRebasedCatVarCount()) - { - logging::Info("[Warning] %d CatVars are rebased!", - GetRebasedCatVarCount()); - } - std::ofstream file(path, std::ios::out); - if (file.bad()) - { - logging::Info("Couldn't open the file!"); - torun = -1; - return; - } - for (const auto &i : CatVarList()) - { - if (!i) - continue; - if (!i->GetString()) - continue; - if (i->GetBase() != std::string(i->GetString())) - { - file << CON_PREFIX << i->name << " \"" << i->GetString() - << "\"\n"; - } - } - file.close(); - } - else if (torun == save_complete) - { - std::string filename("lastcfg"); - if (Args[0] != "") - { - filename = Args[0]; - Args[0] = ""; - } - std::string path = format("tf/cfg/cat_", filename, ".cfg"); - logging::Info("Saving settings to %s", path.c_str()); - if (GetRebasedCatVarCount()) - { - logging::Info("[Warning] %d CatVars are rebased!", - GetRebasedCatVarCount()); - } - std::ofstream file(path, std::ios::out); - if (file.bad()) - { - logging::Info("Couldn't open the file!"); - return; - } - file << "exec cat_defaults\n"; - for (const auto &i : CatVarList()) - { - if (!i) - continue; - if (!i->GetString()) - continue; - if (i->GetBase() != std::string(i->GetString())) - { - file << CON_PREFIX << i->name << " \"" << i->GetString() - << "\"\n"; - } - } - file.close(); - } - torun = -1; -} -} - -static CatCommand cfg_rebase("cfg_setbase", "Rebase config", - []() { torun = rebase; }); - -static CatCommand cfg_resetbase("cfg_resetbase", "Reset config base", - []() { torun = resetbase; }); - -static CatCommand save_settings("save", "Save settings (optional filename)", - [](const CCommand &args) { - if (args.ArgC() > 1) - { - Args[0] = std::string(args.Arg(1)); - } - torun = save; - }); - -static CatCommand - save_settings_complete("save_complete", - "Save all settings (optional filename)", - [](const CCommand &args) { - if (args.ArgC() > 1) - { - Args[0] = std::string(args.Arg(1)); - } - torun = save_complete; - }); - -// Prevent initialization errors. -std::vector ®istrationArray() -{ - static std::vector vector; - return vector; -} +#include +#include +#include std::vector &commandRegistrationArray() { @@ -212,108 +61,4 @@ void RegisterCatCommands() cmd->Register(); commandRegistrationArray().pop_back(); } -} - -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) -{ - registrationArray().push_back(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), restricted(true) -{ - max = max_val; - registrationArray().push_back(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), restricted(true) -{ - min = min_val; - max = max_val; - registrationArray().push_back(this); -} - -CatVar::CatVar(CatEnum &cat_enum, std::string _name, std::string _defaults, - std::string _desc_short, std::string _desc_long) - : type(CV_ENUM), name(_name), defaults(_defaults), desc_short(_desc_short), - desc_long(_desc_long), enum_type(&cat_enum), restricted(true) -{ - min = cat_enum.min_value; - max = cat_enum.max_value; - registrationArray().push_back(this); -} - -void CatVar::OnRegister(CatVar::RegisterCallbackFn fn) -{ - if (registered) - fn(this); - else - callbacks.push_back(fn); -} - -void CatVar::Register() -{ - CatVarList().push_back(this); - id = last_id++; - convar = CreateConVar(CON_PREFIX + name, defaults, desc_short); - convar_parent = convar->m_pParent; - current_base = defaults; - while (!callbacks.empty()) - { - callbacks.back()(this); - callbacks.pop_back(); - } - registered = true; -} - -// TODO, reverse -void CatVar::InstallChangeCallback(FnChangeCallback_t callback) -{ - OnRegister([callback](CatVar *var) { - var->convar_parent->InstallChangeCallback(callback); - }); -} - -// Used during int to setup catvars -void RegisterCatVars() -{ - while (registrationArray().size()) - { - CatVar *var = registrationArray().back(); - var->Register(); - registrationArray().pop_back(); - } -} - -// Use when creating a CatEnum for use in a CatVar -CatEnum::CatEnum(std::vector values, int min) : value_names(values) -{ - min_value = min; - max_value = min + int(values.size()) - 1; - size = int(values.size()); -} - -// use to get a name from a CatEnum -std::string CatEnum::Name(int value) -{ - if (value >= min_value && value < max_value) - { - return value_names.at(unsigned(value) - unsigned(min_value)); - } - return "unknown"; -} - -std::vector &CatVarList() -{ - static std::vector object{}; - return object; -} +} \ No newline at end of file diff --git a/src/crits.cpp b/src/crits.cpp index d37aef46..088bfe00 100644 --- a/src/crits.cpp +++ b/src/crits.cpp @@ -5,16 +5,14 @@ * Author: nullifiedcat */ +#include #include "common.hpp" -static CatVar crit_info(CV_SWITCH, "crit_info", "0", "Show crit info"); -static CatVar crit_key(CV_KEY, "crit_key", "0", "Crit Key"); -static CatVar crit_melee(CV_SWITCH, "crit_melee", "0", "Melee crits"); -static CatVar crit_legiter( - CV_SWITCH, "crit_force_gameplay", "0", "Don't hinder gameplay", - "Attempt to crit when possible but do not hinder normal gameplay"); -static CatVar crit_experimental(CV_SWITCH, "crit_experimental", "0", - "Experimental crithack"); +static settings::Bool crit_info{ "crit.info", "false" }; +static settings::Button crit_key{ "crit.key", "" }; +static settings::Bool crit_melee{ "crit.melee", "false" }; +static settings::Bool crit_legiter{ "crit.force-gameplay", "false" }; +static settings::Bool crit_experimental{ "crit.experimental", "false" }; std::unordered_map command_number_mod{}; @@ -172,8 +170,8 @@ void create_move() if (!re::C_TFWeaponBase::AreRandomCritsEnabled(weapon)) return; unfuck_bucket(weapon); - if ((g_pUserCmd->buttons & IN_ATTACK) && crit_key.KeyDown() && - g_pUserCmd->command_number && crit_key) + if ((g_pUserCmd->buttons & IN_ATTACK) && crit_key && crit_key.isKeyDown() && + g_pUserCmd->command_number) { force_crit(weapon); } diff --git a/src/entitycache.cpp b/src/entitycache.cpp index 4383085a..bbcf4351 100644 --- a/src/entitycache.cpp +++ b/src/entitycache.cpp @@ -8,6 +8,7 @@ #include "common.hpp" #include +#include bool IsProjectileACrit(CachedEntity *ent) { @@ -44,10 +45,9 @@ CachedEntity::~CachedEntity() { } -static CatVar ve_window(CV_FLOAT, "debug_ve_window", "0", "VE Window"); -static CatVar ve_smooth(CV_SWITCH, "debug_ve_smooth", "1", "VE Smoothing"); -static CatVar ve_averager_size(CV_INT, "debug_ve_averaging", "8", - "VE Averaging"); +static settings::Float ve_window{ "debug.ve.window", "0" }; +static settings::Bool ve_smooth{ "debug.ve.smooth", "true" }; +static settings::Int ve_averager_size{ "debug.ve.averaging", "0" }; void CachedEntity::Update() { @@ -76,8 +76,8 @@ void CachedEntity::Update() g_IEngine->GetPlayerInfo(m_IDX, &player_info); } -static CatVar fast_vischeck(CV_SWITCH, "fast_vischeck", "0", "Fast VisCheck", - "VisCheck only certain player hitboxes"); +// FIXME maybe disable this by default +static settings::Bool fast_vischeck{ "debug.fast-vischeck", "true" }; bool CachedEntity::IsVisible() { diff --git a/src/entityhitboxcache.cpp b/src/entityhitboxcache.cpp index c0324ad4..4d47fcbf 100644 --- a/src/entityhitboxcache.cpp +++ b/src/entityhitboxcache.cpp @@ -5,6 +5,7 @@ * Author: nullifiedcat */ +#include #include "common.hpp" namespace hitbox_cache @@ -105,11 +106,7 @@ bool EntityHitboxCache::VisibilityCheck(int id) return m_VisCheck[id]; } -static CatEnum setupbones_time_enum({ "ZERO", "CURTIME", "LP SERVERTIME", - "SIMTIME" }); -static CatVar setupbones_time( - setupbones_time_enum, "setupbones_time", "3", "Setupbones", - "Defines setupbones 4th argument, change it if your aimbot misses, idk!!"); +static settings::Int setupbones_time{ "source.setupbones-time", "3" }; std::mutex setupbones_mutex; diff --git a/src/hacks/Spam.cpp b/src/hacks/Spam.cpp index 4e6308a8..4535c74d 100644 --- a/src/hacks/Spam.cpp +++ b/src/hacks/Spam.cpp @@ -6,34 +6,21 @@ */ #include +#include #include "common.hpp" #include "MiscTemporary.hpp" +static settings::Int spam_source{ "spam.source", "0" }; +static settings::Bool random_order{ "spam.random", "0" }; +static settings::String filename{ "spam.filename", "spam.txt" }; +static settings::Int spam_delay{ "spam.delay", "800" }; +static settings::Int voicecommand_spam{ "spam.voicecommand", "0" }; +static settings::Bool teamname_spam{ "spam.teamname","0" }; + namespace hacks::shared::spam { -static CatEnum spam_enum({ "DISABLED", "CUSTOM", "DEFAULT", "LENNYFACES", - "BLANKS", "NULLCORE", "LMAOBOX", "LITHIUM" }); -CatVar spam_source(spam_enum, "spam", "0", "Chat Spam", - "Defines source of spam lines. CUSTOM spam file must be set " - "in cat_spam_file and loaded with cat_spam_reload (Use " - "console!)"); -static CatVar random_order(CV_SWITCH, "spam_random", "0", "Random Order"); -static CatVar - filename(CV_STRING, "spam_file", "spam.txt", "Spam file", - "Spam file name. Each line should be no longer than 100 " - "characters, file must be located in cathook data folder"); -CatCommand reload("spam_reload", "Reload spam file", Reload); -static CatVar spam_delay(CV_INT, "spam_delay", "800", "Spam delay", - "Delay between spam messages (in ms)", 0.0f, 8000.0f); -static CatEnum voicecommand_enum({ "DISABLED", "RANDOM", "MEDIC", "THANKS", - "NICE SHOT", "CHEERS", "JEERS" }); -static CatVar voicecommand_spam(voicecommand_enum, "spam_voicecommand", "0", - "Voice Command Spam", - "Spams tf voice commands"); - -static CatVar teamname_spam(CV_SWITCH, "spam_teamname", "0", "Teamname Spam", - "Spam changes the tournament name"); +static CatCommand reload("spam_reload", "Reload spam file", Reload); static int last_index; @@ -226,10 +213,9 @@ int QueryPlayer(Query query) void Init() { - filename.InstallChangeCallback( - [](IConVar *var, const char *pszOV, float flOV) { - file.TryLoad(std::string(filename.GetString())); - }); + filename.installChangeCallback([](settings::VariableBase& var, std::string after) { + file.TryLoad(after); + }); } bool SubstituteQueries(std::string &input) @@ -268,8 +254,6 @@ bool FormatSpamMessage(std::string &message) void CreateMove() { - if (!DelayTimer.check((int) delay * 1000)) - return; IF_GAME(IsTF2()) { // Spam changes the tournament name in casual and compeditive gamemodes @@ -439,4 +423,9 @@ const std::vector builtin_lithium = { "SAVE YOUR MONEY AND GET LITHIUMCHEAT! IT IS FREE!", "GOT ROLLED BY LITHIUM? HEY, THAT MEANS IT'S TIME TO GET LITHIUMCHEAT!!" }; + +bool isActive() +{ + return bool(spam_source); +} }