From d127bbe89dfcfd00000dbfd8a36eaf4b555bdef9 Mon Sep 17 00:00:00 2001 From: Edo Date: Fri, 6 Jan 2023 22:06:10 +0000 Subject: [PATCH] [Loader]: Establish a priority list for some modules (#706) --- src/Components/Loader.cpp | 9 ++-- src/Components/Modules/ConnectProtocol.cpp | 10 +++-- src/Components/Modules/Flags.hpp | 2 - src/Components/Modules/IPCPipe.cpp | 50 ++++++++++------------ src/Components/Modules/IPCPipe.hpp | 6 +-- src/Components/Modules/Singleton.cpp | 4 +- 6 files changed, 38 insertions(+), 43 deletions(-) diff --git a/src/Components/Loader.cpp b/src/Components/Loader.cpp index 0e544032..f876fa49 100644 --- a/src/Components/Loader.cpp +++ b/src/Components/Loader.cpp @@ -82,14 +82,16 @@ namespace Components Uninitializing = false; Utils::Memory::GetAllocator()->clear(); + // High priority + Register(new Singleton()); + Register(new Auth()); Register(new Command()); Register(new Dvar()); - Register(new Exception()); // Install our exception handler as early as posssible to get better debug dumps from startup crashes - Register(new Flags()); + Register(new Exception()); // Install our exception handler as early as possible to get better debug dumps from startup crashes + Register(new IPCPipe()); Register(new Network()); Register(new Logger()); - Register(new Singleton()); Register(new UIScript()); Register(new ZoneBuilder()); @@ -118,7 +120,6 @@ namespace Components Register(new FileSystem()); Register(new Friends()); Register(new Gamepad()); - Register(new IPCPipe()); Register(new Lean()); Register(new Localization()); Register(new MapDump()); diff --git a/src/Components/Modules/ConnectProtocol.cpp b/src/Components/Modules/ConnectProtocol.cpp index 1542b28e..4a44ad34 100644 --- a/src/Components/Modules/ConnectProtocol.cpp +++ b/src/Components/Modules/ConnectProtocol.cpp @@ -200,7 +200,8 @@ namespace Components { if (Used()) { - Command::Execute(std::format("connect {}", ConnectString), false); + const auto* cmd = Utils::String::Format("connect {}", ConnectString); + Command::Execute(cmd, false); } } @@ -211,7 +212,8 @@ namespace Components // IPC handler IPCPipe::On("connect", [](const std::string& data) { - Command::Execute(std::format("connect {}", data), false); + const auto* cmd = Utils::String::Format("connect {}", data); + Command::Execute(cmd, false); }); // Invocation handler @@ -227,7 +229,7 @@ namespace Components if (!Singleton::IsFirstInstance()) { IPCPipe::Write("connect", ConnectString); - ExitProcess(0); + ExitProcess(EXIT_SUCCESS); } else { @@ -237,7 +239,7 @@ namespace Components Scheduler::Once([] { Command::Execute("openmenu popup_reconnectingtoparty", false); - }, Scheduler::Pipeline::CLIENT, 8s); + }, Scheduler::Pipeline::MAIN, 8s); } } } diff --git a/src/Components/Modules/Flags.hpp b/src/Components/Modules/Flags.hpp index b8ee410d..5d97f740 100644 --- a/src/Components/Modules/Flags.hpp +++ b/src/Components/Modules/Flags.hpp @@ -5,8 +5,6 @@ namespace Components class Flags : public Component { public: - Flags() = default; - static bool HasFlag(const std::string& flag); private: diff --git a/src/Components/Modules/IPCPipe.cpp b/src/Components/Modules/IPCPipe.cpp index a2712ac2..a7e79a40 100644 --- a/src/Components/Modules/IPCPipe.cpp +++ b/src/Components/Modules/IPCPipe.cpp @@ -8,7 +8,7 @@ namespace Components #pragma region Pipe - Pipe::Pipe() : connectCallback(0), pipe(INVALID_HANDLE_VALUE), threadAttached(false), type(IPCTYPE_NONE), reconnectAttempt(0) + Pipe::Pipe() : connectCallback(nullptr), pipe(INVALID_HANDLE_VALUE), threadAttached(false), type(IPCTYPE_NONE), reconnectAttempt(0) { this->destroy(); } @@ -67,7 +67,7 @@ namespace Components if (!Loader::IsPerformingUnitTests()) { this->threadAttached = true; - this->thread = std::thread(Pipe::ReceiveThread, this); + this->thread = std::thread(ReceiveThread, this); } Logger::Print("Pipe successfully created\n"); @@ -93,7 +93,7 @@ namespace Components { if (this->type != IPCTYPE_CLIENT || this->pipe == INVALID_HANDLE_VALUE) return false; - Pipe::Packet _packet; + Packet _packet; strcpy_s(_packet.command, command.data()); strcpy_s(_packet.buffer, data.data()); @@ -103,15 +103,9 @@ namespace Components void Pipe::destroy() { - //this->Type = IPCTYPE_NONE; - - //*this->PipeFile = 0; - //*this->PipeName = 0; - if (this->pipe && INVALID_HANDLE_VALUE != this->pipe) { CancelIoEx(this->pipe, nullptr); - //DeleteFileA(this->pipeFile); if (this->type == IPCTYPE_SERVER) DisconnectNamedPipe(this->pipe); @@ -134,11 +128,11 @@ namespace Components void Pipe::setName(const std::string& name) { - memset(this->pipeName, 0, sizeof(this->pipeName)); - memset(this->pipeFile, 0, sizeof(this->pipeFile)); + ZeroMemory(this->pipeName, sizeof(this->pipeName)); + ZeroMemory(this->pipeFile, sizeof(this->pipeFile)); strncpy_s(this->pipeName, name.data(), sizeof(this->pipeName)); - sprintf_s(this->pipeFile, sizeof(this->pipeFile), "\\\\.\\Pipe\\%s", this->pipeName); + sprintf_s(this->pipeFile, "\\\\.\\Pipe\\%s", this->pipeName); } void Pipe::ReceiveThread(Pipe* pipe) @@ -158,7 +152,7 @@ namespace Components while (pipe->threadAttached && pipe->pipe && pipe->pipe != INVALID_HANDLE_VALUE) { - BOOL bResult = ReadFile(pipe->pipe, &pipe->packet, sizeof(pipe->packet), &cbBytes, nullptr); + auto bResult = ReadFile(pipe->pipe, &pipe->packet, sizeof(pipe->packet), &cbBytes, nullptr); if (bResult && cbBytes) { @@ -169,7 +163,7 @@ namespace Components } else if (pipe->threadAttached && pipe->pipe != INVALID_HANDLE_VALUE) { - Logger::Print("Failed to read from client through pipe\n"); + Logger::PrintError(Game::CON_CHANNEL_ERROR, "Failed to read from client through pipe\n"); DisconnectNamedPipe(pipe->pipe); ConnectNamedPipe(pipe->pipe, nullptr); @@ -187,20 +181,20 @@ namespace Components { if (Singleton::IsFirstInstance()) { - IPCPipe::ClientPipe.connect(IPC_PIPE_NAME_CLIENT); + ClientPipe.connect(IPC_PIPE_NAME_CLIENT); } } // Writes to the process on the other end of the pipe bool IPCPipe::Write(const std::string& command, const std::string& data) { - return IPCPipe::ClientPipe.write(command, data); + return ClientPipe.write(command, data); } // Installs a callback for receiving commands from the process on the other end of the pipe - void IPCPipe::On(const std::string& command, Utils::Slot callback) + void IPCPipe::On(const std::string& command, const Utils::Slot& callback) { - IPCPipe::ServerPipe.setCallback(command, callback); + ServerPipe.setCallback(command, callback); } IPCPipe::IPCPipe() @@ -208,37 +202,37 @@ namespace Components if (Dedicated::IsEnabled() || Loader::IsPerformingUnitTests() || ZoneBuilder::IsEnabled()) return; // Server pipe - IPCPipe::ServerPipe.onConnect(IPCPipe::ConnectClient); - IPCPipe::ServerPipe.create((Singleton::IsFirstInstance() ? IPC_PIPE_NAME_SERVER : IPC_PIPE_NAME_CLIENT)); + ServerPipe.onConnect(ConnectClient); + ServerPipe.create((Singleton::IsFirstInstance() ? IPC_PIPE_NAME_SERVER : IPC_PIPE_NAME_CLIENT)); // Connect second instance's client pipe to first instance's server pipe if (!Singleton::IsFirstInstance()) { - IPCPipe::ClientPipe.connect(IPC_PIPE_NAME_SERVER); + ClientPipe.connect(IPC_PIPE_NAME_SERVER); } - IPCPipe::On("ping", [](const std::string& data) + On("ping", [](const std::string& data) { Logger::Print("Received ping form pipe, sending pong!\n"); - IPCPipe::Write("pong", data); + Write("pong", data); }); - IPCPipe::On("pong", [](const std::string& /*data*/) + On("pong", []([[maybe_unused]] const std::string& data) { Logger::Print("Received pong form pipe!\n"); }); // Test pipe functionality by sending pings - Command::Add("ipcping", [](Command::Params*) + Command::Add("ipcping", []([[maybe_unused]] Command::Params* params) { Logger::Print("Sending ping to pipe!\n"); - IPCPipe::Write("ping", ""); + Write("ping", ""); }); } void IPCPipe::preDestroy() { - IPCPipe::ServerPipe.destroy(); - IPCPipe::ClientPipe.destroy(); + ServerPipe.destroy(); + ClientPipe.destroy(); } } diff --git a/src/Components/Modules/IPCPipe.hpp b/src/Components/Modules/IPCPipe.hpp index 14218cdd..1a9d0d08 100644 --- a/src/Components/Modules/IPCPipe.hpp +++ b/src/Components/Modules/IPCPipe.hpp @@ -51,8 +51,8 @@ namespace Components Type type; Packet packet; - char pipeName[MAX_PATH]; - char pipeFile[MAX_PATH]; + char pipeName[MAX_PATH]{}; + char pipeFile[MAX_PATH]{}; unsigned int reconnectAttempt; void setName(const std::string& name); @@ -68,7 +68,7 @@ namespace Components void preDestroy() override; static bool Write(const std::string& command, const std::string& data); - static void On(const std::string& command, Utils::Slot callback); + static void On(const std::string& command, const Utils::Slot& callback); private: static Pipe ServerPipe; diff --git a/src/Components/Modules/Singleton.cpp b/src/Components/Modules/Singleton.cpp index 3986a930..218e6825 100644 --- a/src/Components/Modules/Singleton.cpp +++ b/src/Components/Modules/Singleton.cpp @@ -19,7 +19,7 @@ namespace Components { printf("%s", "IW4x " VERSION " (built " __DATE__ " " __TIME__ ")\n"); printf("%d\n", REVISION); - std::exit(0); + ExitProcess(EXIT_SUCCESS); } Console::FreeNativeConsole(); @@ -30,7 +30,7 @@ namespace Components if (!FirstInstance && !ConnectProtocol::Used() && MessageBoxA(nullptr, "Do you want to start another instance?\nNot all features will be available!", "Game already running", MB_ICONEXCLAMATION | MB_YESNO) == IDNO) { - std::exit(0); + ExitProcess(EXIT_SUCCESS); } } }