Fix binding related crash

This commit is contained in:
Federico Cecchetto 2022-05-12 23:15:39 +02:00
parent 18f6a6fa57
commit 33062ec55f

View File

@ -93,23 +93,49 @@ namespace binding
// custom binds // custom binds
return ORIGINAL_BIND_COUNT + get_binding_for_custom_command(command); return ORIGINAL_BIND_COUNT + get_binding_for_custom_command(command);
} }
std::optional<std::string> get_custom_binding_for_key(int key)
{
key -= ORIGINAL_BIND_COUNT;
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) void cl_execute_key_stub(const int local_client_num, int key, const int down, const unsigned int time)
{ {
if (key >= ORIGINAL_BIND_COUNT) if (key >= ORIGINAL_BIND_COUNT)
{ {
key -= ORIGINAL_BIND_COUNT; const auto bind = get_custom_binding_for_key(key);
if (!bind.has_value())
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;
} }
return; return game::Cbuf_AddText(local_client_num, utils::string::va("%s\n", bind.value().data()));
} }
cl_execute_key_hook.invoke<void>(local_client_num, key, down, time); 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 >= ORIGINAL_BIND_COUNT)
{
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 class component final : public component_interface
@ -125,6 +151,8 @@ namespace binding
// execute custom binds // execute custom binds
cl_execute_key_hook.create(0x1403CF1E0, &cl_execute_key_stub); cl_execute_key_hook.create(0x1403CF1E0, &cl_execute_key_stub);
utils::hook::jump(0x14059AE90, cmd_get_binding_for_key_stub);
} }
}; };
} }