From 46d3045d6fcbb30df2f7f3bcdd78bef8a7037a1b Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 28 Jan 2017 15:51:50 +0100 Subject: [PATCH] [Friends] Experimental ipc function handler --- src/Components/Modules/Friends.cpp | 28 ++++++++++ src/Components/Modules/IPCHandler.cpp | 8 +-- src/Proto/ipc.proto | 8 ++- src/Worker/Handlers/Friends.cpp | 80 ++++++++++++--------------- src/Worker/Handlers/Friends.hpp | 20 +++++-- src/Worker/Runner.cpp | 10 ++-- src/Worker/Runner.hpp | 31 ++++++++++- src/Worker/Worker.cpp | 1 + 8 files changed, 122 insertions(+), 64 deletions(-) diff --git a/src/Components/Modules/Friends.cpp b/src/Components/Modules/Friends.cpp index 48cbfa8b..5b02a8ac 100644 --- a/src/Components/Modules/Friends.cpp +++ b/src/Components/Modules/Friends.cpp @@ -167,6 +167,34 @@ namespace Components }); UIFeeder::Add(6.0f, Friends::GetFriendCount, Friends::GetFriendText, Friends::SelectFriend); + + IPCHandler::OnWorker("friends", [](std::string data) + { + Proto::IPC::Function function; + if(function.ParseFromString(data)) + { + if(function.name() == "friendsResponse") + { + auto params = function.params(); + + Logger::Print("Received friendslist: %d\n", params.size()); + + for(auto param : params) + { + Logger::Print("%s\n", param.data()); + } + } + } + }); + + Command::Add("getFriends", [](Command::Params*) + { + Proto::IPC::Function function; + function.set_name("getFriends"); + function.add_params()->append(Utils::String::VA("%d", 4)); + + IPCHandler::SendWorker("friends", function.SerializeAsString()); + }); } Friends::~Friends() diff --git a/src/Components/Modules/IPCHandler.cpp b/src/Components/Modules/IPCHandler.cpp index 952035e4..f0f519fe 100644 --- a/src/Components/Modules/IPCHandler.cpp +++ b/src/Components/Modules/IPCHandler.cpp @@ -13,7 +13,7 @@ namespace Components IPCHandler::InitChannels(); Proto::IPC::Command command; - command.set_command(message); + command.set_name(message); command.set_data(data); IPCHandler::WorkerChannel->send(command.SerializeAsString()); @@ -24,7 +24,7 @@ namespace Components IPCHandler::InitChannels(); Proto::IPC::Command command; - command.set_command(message); + command.set_name(message); command.set_data(data); IPCHandler::ClientChannel->send(command.SerializeAsString()); @@ -78,7 +78,7 @@ namespace Components Proto::IPC::Command command; if(command.ParseFromString(packet)) { - auto callback = IPCHandler::ClientCallbacks.find(command.command()); + auto callback = IPCHandler::ClientCallbacks.find(command.name()); if (callback != IPCHandler::ClientCallbacks.end()) { callback->second(command.data()); @@ -97,7 +97,7 @@ namespace Components Proto::IPC::Command command; if (command.ParseFromString(packet)) { - auto callback = IPCHandler::WorkerCallbacks.find(command.command()); + auto callback = IPCHandler::WorkerCallbacks.find(command.name()); if (callback != IPCHandler::WorkerCallbacks.end()) { callback->second(command.data()); diff --git a/src/Proto/ipc.proto b/src/Proto/ipc.proto index f2e71514..2a5471d1 100644 --- a/src/Proto/ipc.proto +++ b/src/Proto/ipc.proto @@ -4,6 +4,12 @@ package Proto.IPC; message Command { - bytes command = 1; + bytes name = 1; bytes data = 2; } + +message Function +{ + bytes name = 1; + repeated bytes params = 2; +} diff --git a/src/Worker/Handlers/Friends.cpp b/src/Worker/Handlers/Friends.cpp index 8fe8d635..c659839e 100644 --- a/src/Worker/Handlers/Friends.cpp +++ b/src/Worker/Handlers/Friends.cpp @@ -1,65 +1,55 @@ #include "STDInclude.hpp" -namespace Worker +namespace Handlers { - int ProcessId; - - int __stdcall EntryPoint(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, char* /*lpCmdLine*/, int /*nCmdShow*/) + void Friends::handle(Worker::Endpoint endpoint, std::string data) { - Runner runner(Worker::ProcessId); - runner.run(); - return 0; - } - - void Initialize() - { - if(!Steam::Proxy::Inititalize()) + Proto::IPC::Function function; + if (function.ParseFromString(data)) { - printf("Failed to initialize worker!\n"); - ExitProcess(1); - } - else - { -#ifdef DEBUG - SetConsoleTitleA("IW4x: Worker"); -#else - FreeConsole(); -#endif + auto handler = this->functions.find(function.name()); + if (handler != this->functions.end()) + { + printf("Handing function %s\n", function.name().data()); - Utils::Hook(0x6BABA1, Worker::EntryPoint, HOOK_CALL).install()->quick(); + auto params = function.params(); + handler->second(endpoint, std::vector(params.begin(), params.end())); + } + else + { + printf("No handler for function %s\n", function.name().data()); + } } } - void Uninitialize() + void Friends::addFunction(std::string function, Friends::Callback callback) { - Steam::Proxy::Uninititalize(); + this->functions[function] = callback; } - bool ParseWorkerFlag() + Friends::Friends() { - char* command = "-parent "; - char* parentProc = strstr(GetCommandLineA(), command); - - if (parentProc) + this->addFunction("getFriends", [&](Worker::Endpoint endpoint, std::vector params) { - parentProc += strlen(command); - Worker::ProcessId = atoi(parentProc); + if (params.size() >= 1 && Steam::Proxy::SteamFriends) + { + int flag = atoi(params[0].data()); + int count = Steam::Proxy::SteamFriends->GetFriendCount(flag); - return true; - } + Proto::IPC::Function response; + response.set_name("friendsResponse"); - return false; - } + for (int i = 0; i < count; ++i) + { + std::string* param = response.add_params(); + SteamID id = Steam::Proxy::SteamFriends->GetFriendByIndex(i, flag); - bool IsWorker() - { - static Utils::Value flag; + param->clear(); + param->append(Utils::String::VA("%llX", id.Bits)); + } - if (!flag.isValid()) - { - flag.set(Worker::ParseWorkerFlag()); - } - - return flag.get(); + endpoint.send(this->getCommand(), response.SerializeAsString()); + } + }); } } diff --git a/src/Worker/Handlers/Friends.hpp b/src/Worker/Handlers/Friends.hpp index 4f700b23..357e5667 100644 --- a/src/Worker/Handlers/Friends.hpp +++ b/src/Worker/Handlers/Friends.hpp @@ -1,11 +1,19 @@ #pragma once -namespace Worker +namespace Handlers { - void Initialize(); - void Uninitialize(); + class Friends : public Worker::Runner::Handler + { + public: + typedef std::function)> Callback; - bool IsWorker(); + Friends(); + + std::string getCommand() override { return "friends"; }; + void handle(Worker::Endpoint endpoint, std::string data) override; + + private: + void addFunction(std::string function, Callback callback); + std::unordered_map functions; + }; } - -#include "Runner.hpp" diff --git a/src/Worker/Runner.cpp b/src/Worker/Runner.cpp index a2b0c38a..5d2b9e11 100644 --- a/src/Worker/Runner.cpp +++ b/src/Worker/Runner.cpp @@ -37,9 +37,9 @@ namespace Worker } } - void Runner::attachHandler(std::shared_ptr handler) + void Runner::attachHandler(Runner::Handler* handler) { - this->handlers[handler->getCommand()] = handler; + this->handlers[handler->getCommand()] = std::shared_ptr(handler); } void Runner::worker() @@ -57,15 +57,15 @@ namespace Worker Proto::IPC::Command command; if(command.ParseFromString(buffer)) { - auto handler = this->handlers.find(command.command()); + auto handler = this->handlers.find(command.name()); if (handler != this->handlers.end()) { - printf("Handling command %s\n", command.command().data()); + printf("Dispathcing command %s to handler\n", command.name().data()); handler->second->handle(&channel, command.data()); } else { - printf("No handler found for command %s\n", command.command().data()); + printf("No handler found for command %s\n", command.name().data()); } } } diff --git a/src/Worker/Runner.hpp b/src/Worker/Runner.hpp index f882acb2..328aa529 100644 --- a/src/Worker/Runner.hpp +++ b/src/Worker/Runner.hpp @@ -2,6 +2,29 @@ namespace Worker { + class Endpoint + { + public: + Endpoint() : Endpoint(nullptr) {} + Endpoint(Utils::IPC::BidirectionalChannel* _channel) : channel(_channel) {} + Endpoint(const Endpoint& obj) : Endpoint(obj.channel) {} + + void send(std::string message, std::string data) + { + if (this->channel) + { + Proto::IPC::Command command; + command.set_name(message); + command.set_data(data); + + this->channel->send(command.SerializeAsString()); + } + } + + private: + Utils::IPC::BidirectionalChannel* channel; + }; + class Runner { public: @@ -10,7 +33,7 @@ namespace Worker public: virtual ~Handler() {}; virtual std::string getCommand() = 0; - virtual void handle(Utils::IPC::BidirectionalChannel* channel, std::string data) = 0; + virtual void handle(Endpoint endpoint, std::string data) = 0; }; Runner(int pid); @@ -18,13 +41,15 @@ namespace Worker void run(); - void attachHandler(std::shared_ptr handler); + void attachHandler(Runner::Handler* handler); private: void worker(); int processId; bool terminate; - std::map> handlers; + std::unordered_map> handlers; }; } + +#include "Handlers/Friends.hpp" diff --git a/src/Worker/Worker.cpp b/src/Worker/Worker.cpp index 8fe8d635..fbf9792e 100644 --- a/src/Worker/Worker.cpp +++ b/src/Worker/Worker.cpp @@ -7,6 +7,7 @@ namespace Worker int __stdcall EntryPoint(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, char* /*lpCmdLine*/, int /*nCmdShow*/) { Runner runner(Worker::ProcessId); + runner.attachHandler(new Handlers::Friends()); runner.run(); return 0; }