[Events]: Cleanup (#1016)

This commit is contained in:
Edo 2023-05-07 15:26:06 +01:00 committed by GitHub
parent 9526c7aa74
commit 54f6162a7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 37 deletions

View File

@ -697,7 +697,7 @@ namespace Components
mg_mgr_init(&Mgr);
Network::OnStart([]
Events::OnNetworkInit([]() -> void
{
const auto* nc = mg_http_listen(&Mgr, Utils::String::VA(":%hu", Network::GetPort()), &EventHandler, &Mgr);
if (!nc)
@ -709,7 +709,7 @@ namespace Components
ServerRunning = true;
Terminate = false;
ServerThread = Utils::Thread::CreateNamedThread("Mongoose", []
ServerThread = Utils::Thread::CreateNamedThread("Mongoose", []() -> void
{
Com_InitThreadData();
@ -722,7 +722,7 @@ namespace Components
}
else
{
Events::OnDvarInit([]
Events::OnDvarInit([]() -> void
{
UIDlTimeLeft = Dvar::Register<const char*>("ui_dl_timeLeft", "", Game::DVAR_NONE, "");
UIDlProgress = Dvar::Register<const char*>("ui_dl_progress", "", Game::DVAR_NONE, "");

View File

@ -9,6 +9,7 @@ namespace Components
Utils::Signal<Events::Callback> Events::ClientInitSignal;
Utils::Signal<Events::Callback> Events::ServerInitSignal;
Utils::Signal<Events::Callback> Events::DvarInitSignal;
Utils::Signal<Events::Callback> Events::NetworkInitSignal;
void Events::OnClientDisconnect(const Utils::Slot<ClientCallback>& callback)
{
@ -45,6 +46,11 @@ namespace Components
DvarInitSignal.connect(callback);
}
void Events::OnNetworkInit(const Utils::Slot<Callback>& callback)
{
NetworkInitSignal.connect(callback);
}
/*
* Should be called when a client drops from the server
* but not "between levels" (Quake-III-Arena)
@ -101,6 +107,22 @@ namespace Components
Utils::Hook::Call<void()>(0x60AD10)(); // Com_InitDvars
}
void Events::NetworkStart()
{
NetworkInitSignal();
NetworkInitSignal.clear();
}
__declspec(naked) void Events::NET_OpenSocks_Hk()
{
__asm
{
mov eax, 64D900h
call eax
jmp NetworkStart
}
}
Events::Events()
{
Utils::Hook(0x625235, ClientDisconnect_Hk, HOOK_CALL).install()->quick(); // SV_FreeClient
@ -117,5 +139,7 @@ namespace Components
Utils::Hook(0x60BB3A, Com_InitDvars_Hk, HOOK_CALL).install()->quick(); // Com_Init_Try_Block_Function
Utils::Hook(0x4D3665, SV_Init_Hk, HOOK_CALL).install()->quick(); // SV_Init
Utils::Hook(0x4FD4D4, NET_OpenSocks_Hk, HOOK_JUMP).install()->quick(); // NET_OpenIP
}
}

View File

@ -30,6 +30,8 @@ namespace Components
// Client & Server (triggered once)
static void OnDvarInit(const Utils::Slot<Callback>& callback);
static void OnNetworkInit(const Utils::Slot<Callback>& callback);
private:
static Utils::Signal<ClientCallback> ClientDisconnectSignal;
static Utils::Signal<ClientConnectCallback> ClientConnectSignal;
@ -38,6 +40,7 @@ namespace Components
static Utils::Signal<Callback> ClientInitSignal;
static Utils::Signal<Callback> ServerInitSignal;
static Utils::Signal<Callback> DvarInitSignal;
static Utils::Signal<Callback> NetworkInitSignal;
static void ClientDisconnect_Hk(int clientNum);
static void SV_UserinfoChanged_Hk(Game::client_s* cl);
@ -46,5 +49,8 @@ namespace Components
static void CL_InitOnceForAllClients_HK();
static void SV_Init_Hk();
static void Com_InitDvars_Hk();
static void NetworkStart();
static void NET_OpenSocks_Hk();
};
}

View File

@ -2,7 +2,6 @@
namespace Components
{
Utils::Signal<Network::CallbackRaw> Network::StartupSignal;
// Packet interception
std::unordered_map<std::string, Network::networkCallback> Network::CL_Callbacks;
std::unordered_map<std::string, Network::networkRawCallback> Network::CL_RawCallbacks;
@ -152,11 +151,6 @@ namespace Components
return (this->getType() != Game::NA_BAD && this->getType() >= Game::NA_BOT && this->getType() <= Game::NA_IP);
}
void Network::OnStart(const Utils::Slot<CallbackRaw>& callback)
{
StartupSignal.connect(callback);
}
void Network::Send(Game::netsrc_t type, const Address& target, const std::string& data)
{
// Do not use NET_OutOfBandPrint. It only supports non-binary data!
@ -228,12 +222,6 @@ namespace Components
BroadcastRange(100, 65536, data);
}
void Network::NetworkStart()
{
StartupSignal();
StartupSignal.clear();
}
std::uint16_t Network::GetPort()
{
assert((*Game::port));
@ -241,16 +229,6 @@ namespace Components
return static_cast<std::uint16_t>((*Game::port)->current.unsignedInt);
}
__declspec(naked) void Network::NetworkStartStub()
{
__asm
{
mov eax, 64D900h
call eax
jmp NetworkStart
}
}
__declspec(naked) void Network::PacketErrorCheck()
{
__asm
@ -392,9 +370,6 @@ namespace Components
// Parse port as short in Net_AddrToString
Utils::Hook::Set<const char*>(0x4698E3, "%u.%u.%u.%u:%hu");
// Install startup handler
Utils::Hook(0x4FD4D4, NetworkStartStub, HOOK_JUMP).install()->quick();
// Prevent recvfrom error spam
Utils::Hook(0x46531A, PacketErrorCheck, HOOK_JUMP).install()->quick();

View File

@ -46,8 +46,6 @@ namespace Components
Game::netadr_t address;
};
typedef void(CallbackRaw)();
using networkCallback = std::function<void(Address&, const std::string&)>;
using networkRawCallback = std::function<void(Game::netadr_t*, Game::msg_t* msg)>;
@ -55,8 +53,6 @@ namespace Components
static std::uint16_t GetPort();
static void OnStart(const Utils::Slot<CallbackRaw>& callback);
// Send quake-styled binary data
static void Send(const Address& target, const std::string& data);
static void Send(Game::netsrc_t type, const Address& target, const std::string& data);
@ -77,13 +73,9 @@ namespace Components
static void OnClientPacketRaw(const std::string& command, const networkRawCallback& callback);
private:
static Utils::Signal<CallbackRaw> StartupSignal;
static std::unordered_map<std::string, networkCallback> CL_Callbacks;
static std::unordered_map<std::string, networkRawCallback> CL_RawCallbacks;
static void NetworkStart();
static void NetworkStartStub();
static void PacketErrorCheck();
static bool CL_HandleCommand(Game::netadr_t* address, const char* command, Game::msg_t* message);

View File

@ -22,7 +22,7 @@ namespace Game
assert((size >= 0));
#pragma warning(push)
#pragma warning(disable: 6250)
[[maybe_unused]] const auto result = VirtualFree(ptr, size, MEM_DECOMMIT);
VirtualFree(ptr, size, MEM_DECOMMIT);
#pragma warning(pop)
}