Add bindings module

This commit is contained in:
Federico Cecchetto 2022-01-05 17:11:40 +01:00
parent 019fed2baa
commit de2ef7eaef
2 changed files with 143 additions and 1 deletions

View File

@ -0,0 +1,130 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
#include <utils/hook.hpp>
#include <utils/string.hpp>
namespace binding
{
namespace
{
std::vector<std::string> custom_binds = {};
utils::hook::detour cl_execute_key_hook;
utils::hook::detour key_write_bindings_to_buffer_hook;
int key_write_bindings_to_buffer_stub(int /*localClientNum*/, char* buffer, const int buffer_size)
{
auto bytes_used = 0;
const auto buffer_size_align = static_cast<std::int32_t>(buffer_size) - 4;
for (auto key_index = 0; key_index < 256; ++key_index)
{
const auto* const key_button = game::Key_KeynumToString(key_index, 0, 1);
auto value = game::playerKeys->keys[key_index].binding;
if (value && value < 100)
{
const auto len = sprintf_s(&buffer[bytes_used], (buffer_size_align - bytes_used),
"bind %s \"%s\"\n", key_button, game::command_whitelist[value]);
if (len < 0)
{
return bytes_used;
}
bytes_used += len;
}
else if (value >= 100)
{
value -= 100;
if (static_cast<size_t>(value) < custom_binds.size() && !custom_binds[value].empty())
{
const auto len = sprintf_s(&buffer[bytes_used], (buffer_size_align - bytes_used),
"bind %s \"%s\"\n", key_button, custom_binds[value].data());
if (len < 0)
{
return bytes_used;
}
bytes_used += len;
}
}
}
buffer[bytes_used] = 0;
return bytes_used;
}
int get_binding_for_custom_command(const char* command)
{
auto index = 0;
for (auto& bind : custom_binds)
{
if (bind == command)
{
return index;
}
index++;
}
custom_binds.emplace_back(command);
index = static_cast<unsigned int>(custom_binds.size()) - 1;
return index;
}
int key_get_binding_for_cmd_stub(const char* command)
{
// original binds
for (auto i = 0; i <= 100; i++)
{
if (game::command_whitelist[i] && !strcmp(command, game::command_whitelist[i]))
{
return i;
}
}
// custom binds
return 100 + get_binding_for_custom_command(command);
}
void cl_execute_key_stub(const int local_client_num, int key, const int down, const unsigned int time)
{
if (key >= 100)
{
key -= 100;
if (static_cast<size_t>(key) < custom_binds.size() && !custom_binds[key].empty())
{
game::Cbuf_AddText(local_client_num, utils::string::va("%s\n", custom_binds[key].data()));
}
return;
}
cl_execute_key_hook.invoke<void>(local_client_num, key, down, time);
}
}
class component final : public component_interface
{
public:
void post_unpack() override
{
// write all bindings to config file
key_write_bindings_to_buffer_hook.create(0x3D3840_b, key_write_bindings_to_buffer_stub);
// links a custom command to an index
utils::hook::jump(0x59AE30_b, key_get_binding_for_cmd_stub, true);
// execute custom binds
cl_execute_key_hook.create(0x3CF1E0_b, &cl_execute_key_stub);
}
};
}
REGISTER_COMPONENT(binding::component)

View File

@ -72,17 +72,25 @@ namespace game
G_GivePlayerWeapon{0x51B660};
WEAK symbol<void(void* ps, const unsigned int weapon, int hadWeapon)> G_InitializeAmmo{0x4C4110};
WEAK symbol<void(int clientNum, const unsigned int weapon)> G_SelectWeapon{0x51C0D0};
WEAK symbol<bool(int localClientNum, ScreenPlacement* screenPlacement, const float* worldDir, float* outScreenPos)> WorldPosToScreenPos{0x36F310};
WEAK symbol<void(int* hitNum, const float* start, const float* end, int passEntityNum,
int passEntityNum1, int contentmask)> G_SightTrace{0x4CBCA0};
WEAK symbol<char*(char* string)> I_CleanStr{0x620660};
WEAK symbol<char* (GfxImage* image, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipCount,
uint32_t imageFlags, DXGI_FORMAT imageFormat, int a8, const char* name, const void* initData)> Image_Setup{0x74B2A0};
WEAK symbol<const char* (int, int, int)> Key_KeynumToString{0x3D32D0};
WEAK symbol<void(int clientNum, const char* menu, int a3, int a4, unsigned int a5)> LUI_OpenMenu{0x5F0EE0};
WEAK symbol<bool(int clientNum, const char* menu)> Menu_IsMenuOpenAndVisible{0x5EE1A0};
WEAK symbol<Material*(const char* material)> Material_RegisterHandle{0x759BA0};
WEAK symbol<void(pathnode_t*, float* out)> PathNode_WorldifyPosFromParent{0x525830};
WEAK symbol<const float* (const float* v)> Scr_AllocVector{0x5C3220};
WEAK symbol<void()> Scr_ClearOutParams{0x5C6E50};
WEAK symbol<scr_entref_t(unsigned int entId)> Scr_GetEntityIdRef{0x5C56C0};
@ -112,7 +120,8 @@ namespace game
WEAK symbol<void(const void* obj, void* pose, unsigned int entnum, unsigned int renderFxFlags, float* lightingOrigin,
float materialTime, __int64 a7, __int64 a8)> R_AddDObjToScene{0x775C40};
WEAK symbol<ScreenPlacement* ()> ScrPlace_GetViewPlacement{0x3E16A0};
WEAK symbol<ScreenPlacement*()> ScrPlace_GetViewPlacement{0x3E16A0};
WEAK symbol<ScreenPlacement*()> ScrPlace_GetView{0x3E1660};
WEAK symbol<const char*(scr_string_t stringValue)> SL_ConvertToString{0x5BFBB0};
WEAK symbol<scr_string_t(const char* str, unsigned int user)> SL_GetString{0x5C0170};
@ -138,6 +147,7 @@ namespace game
WEAK symbol<cmd_function_s*> cmd_functions{0xAD17BB8};
WEAK symbol<CmdArgs> cmd_args{0xAD17A60};
WEAK symbol<const char*> command_whitelist{0xBF84E0};
WEAK symbol<HWND> hWnd{0xCCF81C0};
@ -145,10 +155,12 @@ namespace game
WEAK symbol<int> g_poolSize{0xBF2E40};
WEAK symbol<gentity_s> g_entities{0x52DDDA0};
WEAK symbol<PathData> pathData{0x52CCDA0};
WEAK symbol<DWORD> threadIds{0xB11DC80};
WEAK symbol<GfxDrawMethod_s> gfxDrawMethod{0xEDF9E00};
WEAK symbol<refdef_t> refdef{0x1BC2500};
WEAK symbol<int> keyCatchers{0x203F3C0};