Fix binding related crash

This commit is contained in:
Federico Cecchetto 2022-06-12 16:45:13 +02:00
parent 8bfa958fe7
commit ed52857402
2 changed files with 42 additions and 9 deletions

View File

@ -95,22 +95,49 @@ namespace binding
return get_num_keys() + get_binding_for_custom_command(command);
}
void cl_execute_key_stub(const int local_client_num, int key, const int down, const int time)
std::optional<std::string> get_custom_binding_for_key(int key)
{
key -= get_num_keys();
if (static_cast<size_t>(key) < custom_binds.size() && !custom_binds[key].empty())
{
return {custom_binds[key]};
}
return {};
}
void cl_execute_key_stub(const int local_client_num, int key, const int down, const unsigned int time)
{
if (key >= get_num_keys())
{
key -= get_num_keys();
if (static_cast<size_t>(key) < custom_binds.size() && !custom_binds[key].empty())
const auto bind = get_custom_binding_for_key(key);
if (!bind.has_value())
{
game::Cbuf_AddText(local_client_num, 0, utils::string::va("%s\n", custom_binds[key].data()));
return;
}
return;
return game::Cbuf_AddText(local_client_num, 0, utils::string::va("%s\n", bind.value().data()));
}
cl_execute_key_hook.invoke<void>(local_client_num, key, down, time);
}
const char* cmd_get_binding_for_key_stub(unsigned int key)
{
if (key >= static_cast<unsigned int>(get_num_keys()))
{
const auto bind = get_custom_binding_for_key(key);
if (!bind.has_value())
{
return "";
}
return utils::string::va("%s", bind.value().data());
}
return game::command_whitelist[key];
}
}
class component final : public component_interface
@ -131,6 +158,12 @@ namespace binding
// execute custom binds
cl_execute_key_hook.create(SELECT_VALUE(0x1A8350_b, 0x130610_b), &cl_execute_key_stub);
if (game::environment::is_sp())
{
// called from getcommandfromkey in gsc
utils::hook::jump(0x3772E0_b, cmd_get_binding_for_key_stub);
}
}
};
}

View File

@ -109,11 +109,11 @@ namespace colors
if (index == '8')
{
*color = *reinterpret_cast<DWORD*>(SELECT_VALUE(0x0, 0xEA749B4_b)); // 1.15
*color = *reinterpret_cast<DWORD*>(SELECT_VALUE(0xF79D288_b, 0xEA749B4_b));
}
else if (index == '9')
{
*color = *reinterpret_cast<DWORD*>(SELECT_VALUE(0x0, 0xEA749B8_b)); // 1.15
*color = *reinterpret_cast<DWORD*>(SELECT_VALUE(0xF79D28C_b, 0xEA749B8_b));
}
else if (index == ':')
{
@ -121,7 +121,7 @@ namespace colors
}
else if (index == ';')
{
*color = *reinterpret_cast<DWORD*>(SELECT_VALUE(0x0, 0xEA749C0_b)); // 1.15
*color = *reinterpret_cast<DWORD*>(SELECT_VALUE(0xF79D294_b, 0xEA749C0_b));
}
else if (index == '<')
{