[ServerCommands] Optimize code and adapt to our style

This commit is contained in:
momo5502 2017-05-31 10:42:43 +02:00
parent 8cf9c0d93f
commit 1628afb4c3
3 changed files with 45 additions and 45 deletions

View File

@ -529,8 +529,8 @@ namespace Components
} }
// Intercept server commands // Intercept server commands
ServerCommands::OnCommand(20, [](Command::Params* params) { ServerCommands::OnCommand(20, [](Command::Params* params)
{
for (int client = 0; client < 18; client++) for (int client = 0; client < 18; client++)
{ {
Dedicated::PlayerGuids[client][0].bits = strtoull(params->get(2 * client + 1), nullptr, 16); Dedicated::PlayerGuids[client][0].bits = strtoull(params->get(2 * client + 1), nullptr, 16);
@ -543,9 +543,7 @@ namespace Components
} }
return true; return true;
}); });
} }
QuickPatch::OnFrame([]() QuickPatch::OnFrame([]()

View File

@ -2,25 +2,25 @@
namespace Components namespace Components
{ {
std::unordered_map < std::int32_t, std::function < bool(Command::Params*) > > ServerCommands::Commands; std::unordered_map<std::int32_t, std::function<bool(Command::Params*)>> ServerCommands::Commands;
std::uint32_t ServerCommands::lastServerCommand; std::uint32_t ServerCommands::LastServerCommand;
void ServerCommands::OnCommand(std::int32_t cmd, std::function < bool(Command::Params*) > cb) void ServerCommands::OnCommand(std::int32_t cmd, std::function<bool(Command::Params*)> cb)
{ {
Commands[cmd] = cb; ServerCommands::Commands[cmd] = cb;
} }
bool ServerCommands::OnServerCommand() bool ServerCommands::OnServerCommand()
{ {
Command::ClientParams params(*Game::cmd_id); Command::ClientParams params(*Game::cmd_id);
for (auto &ServerCommandCB : Commands) for (auto &serverCommandCB : ServerCommands::Commands)
{ {
if (params.length() >= 1) if (params.length() >= 1)
{ {
if (params.get(0)[0] == ServerCommandCB.first) if (params.get(0)[0] == serverCommandCB.first)
{ {
return ServerCommandCB.second(&params); return serverCommandCB.second(&params);
} }
} }
} }
@ -28,78 +28,79 @@ namespace Components
return false; return false;
} }
void __declspec(naked) ServerCommands::OnServerCommandStub() __declspec(naked) void ServerCommands::OnServerCommandStub()
{ {
__asm __asm
{ {
call OnServerCommand; push eax
test al, al; pushad
jnz jumpback; call ServerCommands::OnServerCommand
mov [esp + 20h], eax
popad
pop eax
push 5944AEh; test al, al
retn; jnz jumpback
push 5944AEh
retn
jumpback: jumpback:
push 594536h; push 594536h
retn; retn
} }
} }
void __declspec(naked) ServerCommands::OnServerCommandPreFailStub() __declspec(naked) void ServerCommands::OnServerCommandPreFailStub()
{ {
__asm __asm
{ {
mov lastServerCommand, ecx; mov ServerCommands::LastServerCommand, ecx
cmp ecx, 79h; cmp ecx, 79h
jl above; jl above
push 59449Fh; push 59449Fh
retn; retn
above: above:
push 593C28h; push 593C28h
retn; retn
} }
} }
void ServerCommands::OnServerCommandFailPrint(int type, const char *, ...) void ServerCommands::OnServerCommandFailPrint(int type, const char *, ...)
{ {
Command::ClientParams params(*Game::cmd_id); Command::ClientParams params(*Game::cmd_id);
const char *cmd = ""; Game::Com_Printf(type, "Unknown client game command: %i %s\n", LastServerCommand, params.join(1));
for (std::size_t i = 1; i < params.length(); i++)
cmd = Utils::String::VA("%s %s", cmd, params.get(i));
Game::Com_Printf(type, "Unknown client game command: %i %s\n", lastServerCommand, cmd);
} }
void __declspec(naked) ServerCommands::OnServerCommandFailPrintStub() __declspec(naked) void ServerCommands::OnServerCommandFailPrintStub()
{ {
__asm __asm
{ {
pushad; pushad
call OnServerCommandFailPrint; call ServerCommands::OnServerCommandFailPrint
popad; popad
push 5944C0h; push 5944C0h
retn; retn
} }
} }
ServerCommands::ServerCommands() ServerCommands::ServerCommands()
{ {
// Server command receive hook // Server command receive hook
Utils::Hook(0x59449F, OnServerCommandStub).install()->quick(); Utils::Hook(0x59449F, ServerCommands::OnServerCommandStub).install()->quick();
// Server command fail hooks // Server command fail hooks
Utils::Hook(0x593C1F, OnServerCommandPreFailStub).install()->quick(); Utils::Hook(0x593C1F, ServerCommands::OnServerCommandPreFailStub).install()->quick();
Utils::Hook(0x5944BB, OnServerCommandFailPrintStub).install()->quick(); Utils::Hook(0x5944BB, ServerCommands::OnServerCommandFailPrintStub).install()->quick();
Utils::Hook::Set<std::uint8_t>(0x5944D3, 0xEB); Utils::Hook::Set<std::uint8_t>(0x5944D3, 0xEB);
} }
ServerCommands::~ServerCommands() ServerCommands::~ServerCommands()
{ {
Commands.clear(); ServerCommands::Commands.clear();
} }
} }

View File

@ -11,12 +11,13 @@ namespace Components
static void OnCommand(std::int32_t cmd, std::function<bool(Command::Params*)> cb); static void OnCommand(std::int32_t cmd, std::function<bool(Command::Params*)> cb);
private: private:
static std::unordered_map < std::int32_t, std::function < bool(Command::Params*) > > Commands; static std::unordered_map<std::int32_t, std::function<bool(Command::Params*)>> Commands;
static std::uint32_t LastServerCommand;
static bool OnServerCommand(); static bool OnServerCommand();
static void OnServerCommandStub(); static void OnServerCommandStub();
static void OnServerCommandPreFailStub(); static void OnServerCommandPreFailStub();
static void OnServerCommandFailPrint(int type, const char * trash, ...); static void OnServerCommandFailPrint(int type, const char * trash, ...);
static void OnServerCommandFailPrintStub(); static void OnServerCommandFailPrintStub();
static std::uint32_t lastServerCommand;
}; };
} }