From 4271e3b10859d7d15c0c548d3dab17667138c45e Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 28 Jan 2017 22:01:49 +0100 Subject: [PATCH] [SteamCallbacks] Transmit callbacks from the worker to the client --- src/Components/Modules/Friends.cpp | 29 +++++++++----- src/Steam/Proxy.cpp | 11 ++++-- src/Steam/Proxy.hpp | 2 +- src/Worker/Handlers/Friends.cpp | 1 + src/Worker/Handlers/SteamCallbacks.cpp | 55 ++++++++++++++++++++++++++ src/Worker/Handlers/SteamCallbacks.hpp | 22 +++++++++++ src/Worker/Runner.cpp | 8 +++- src/Worker/Runner.hpp | 3 ++ 8 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 src/Worker/Handlers/SteamCallbacks.cpp create mode 100644 src/Worker/Handlers/SteamCallbacks.hpp diff --git a/src/Components/Modules/Friends.cpp b/src/Components/Modules/Friends.cpp index 69907d59..d4845512 100644 --- a/src/Components/Modules/Friends.cpp +++ b/src/Components/Modules/Friends.cpp @@ -192,18 +192,30 @@ namespace Components { Friends::UpdateFriends(); + auto fInterface = IPCHandler::NewInterface("steamCallbacks"); + // Callback to update user information - Steam::Proxy::RegisterCallback(336, [](void* data) + fInterface->map("304", [](std::vector params) { - Friends::FriendRichPresenceUpdate* update = static_cast(data); - Friends::UpdateUserInfo(update->m_steamIDFriend); + if (params.size() >= 1 && params[0].size() == sizeof(Friends::FriendRichPresenceUpdate)) + { + const Friends::FriendRichPresenceUpdate* update = reinterpret_cast(params[0].data()); + Friends::UpdateUserInfo(update->m_steamIDFriend); + } }); // Persona state has changed - Steam::Proxy::RegisterCallback(304, [](void* data) + fInterface->map("304", [](std::vector params) { - Friends::PersonaStateChange* state = static_cast(data); - if(Steam::Proxy::SteamFriends) Steam::Proxy::SteamFriends->RequestFriendRichPresence(state->m_ulSteamID); + if(params.size() >= 1 && params[0].size() == sizeof(Friends::PersonaStateChange)) + { + const Friends::PersonaStateChange* state = reinterpret_cast(params[0].data()); + + Proto::IPC::Function function; + function.set_name("requestPresence"); + *function.add_params() = Utils::String::VA("%llx", state->m_ulSteamID.Bits); + IPCHandler::SendWorker("friends", function.SerializeAsString()); + } }); UIScript::Add("LoadFriends", [](UIScript::Token) @@ -213,16 +225,13 @@ namespace Components UIFeeder::Add(6.0f, Friends::GetFriendCount, Friends::GetFriendText, Friends::SelectFriend); - auto fInterface = IPCHandler::NewInterface("friends"); + fInterface = IPCHandler::NewInterface("friends"); fInterface->map("friendsResponse", Friends::FriendsResponse); fInterface->map("nameResponse", Friends::NameResponse); } Friends::~Friends() { - Steam::Proxy::UnregisterCallback(304); - Steam::Proxy::UnregisterCallback(336); - { std::lock_guard _(Friends::Mutex); Friends::FriendsList.clear(); diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index e59b00ed..e3b409a8 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -82,7 +82,7 @@ namespace Steam Proxy::Callbacks.erase(callId); } - void Proxy::RunCallback(int32_t callId, void* data) + void Proxy::RunCallback(int32_t callId, void* data, size_t size) { std::lock_guard _(Proxy::CallMutex); @@ -91,6 +91,11 @@ namespace Steam { ::Utils::Hook::Call(callback->second)(data); } + + if(Worker::IsWorker()) + { + Handlers::SteamCallbacks::HandleCallback(callId, data, size); + } } void Proxy::RunFrame() @@ -110,7 +115,7 @@ namespace Steam #endif //Steam::Callbacks::RunCallback(message.m_iCallback, message.m_pubParam); - Proxy::RunCallback(message.m_iCallback, message.m_pubParam); + Proxy::RunCallback(message.m_iCallback, message.m_pubParam, message.m_cubParam); Proxy::SteamFreeLastCallback(Proxy::SteamPipe); } @@ -151,7 +156,7 @@ namespace Steam continue; } - Proxy::RunCallback(call.callId, buffer); + Proxy::RunCallback(call.callId, buffer, call.dataSize); } } } diff --git a/src/Steam/Proxy.hpp b/src/Steam/Proxy.hpp index fe974175..6945f97b 100644 --- a/src/Steam/Proxy.hpp +++ b/src/Steam/Proxy.hpp @@ -396,7 +396,7 @@ namespace Steam static std::function SteamFreeLastCallback; static std::function SteamGetAPICallResult; - static void RunCallback(int32_t callId, void* data); + static void RunCallback(int32_t callId, void* data, size_t size); static void UnregisterCalls(); diff --git a/src/Worker/Handlers/Friends.cpp b/src/Worker/Handlers/Friends.cpp index 96728fdb..fe7ef36e 100644 --- a/src/Worker/Handlers/Friends.cpp +++ b/src/Worker/Handlers/Friends.cpp @@ -92,6 +92,7 @@ namespace Handlers Proto::IPC::Function response; response.set_name("presenceResponse"); *response.add_params() = Utils::String::VA("%llX", id.Bits); + *response.add_params() = params[1].data(); *response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, params[1].data()); endpoint.send(this->getCommand(), response.SerializeAsString()); diff --git a/src/Worker/Handlers/SteamCallbacks.cpp b/src/Worker/Handlers/SteamCallbacks.cpp new file mode 100644 index 00000000..e2ed0ac1 --- /dev/null +++ b/src/Worker/Handlers/SteamCallbacks.cpp @@ -0,0 +1,55 @@ +#include "STDInclude.hpp" + +namespace Handlers +{ + void SteamCallbacks::handle(Worker::Endpoint endpoint, std::string data) + { + Proto::IPC::Function function; + if (function.ParseFromString(data)) + { + auto handler = this->functions.find(function.name()); + if (handler != this->functions.end()) + { + printf("Handing function %s\n", function.name().data()); + + 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 SteamCallbacks::addFunction(std::string function, Friends::Callback callback) + { + this->functions[function] = callback; + } + + void SteamCallbacks::HandleCallback(int32_t callId, void* data, size_t size) + { + if(Worker::Runner::Channel) + { + Proto::IPC::Function response; + response.set_name(Utils::String::VA("%d", callId)); + response.add_params()->append(static_cast(data), size); + + Proto::IPC::Command command; + command.set_name(SteamCallbacks().getCommand()); + command.set_data(response.SerializeAsString()); + + Worker::Runner::Channel->send(command.SerializeAsString()); + } + } + + SteamCallbacks::SteamCallbacks() + { + + } + + SteamCallbacks::~SteamCallbacks() + { + + } +} diff --git a/src/Worker/Handlers/SteamCallbacks.hpp b/src/Worker/Handlers/SteamCallbacks.hpp new file mode 100644 index 00000000..968510ea --- /dev/null +++ b/src/Worker/Handlers/SteamCallbacks.hpp @@ -0,0 +1,22 @@ +#pragma once + +namespace Handlers +{ + class SteamCallbacks : public Worker::Runner::Handler + { + public: + typedef std::function)> Callback; + + SteamCallbacks(); + ~SteamCallbacks(); + + std::string getCommand() override { return "steamCallbacks"; }; + void handle(Worker::Endpoint endpoint, std::string data) override; + + static void HandleCallback(int32_t callId, void* data, size_t size); + + private: + std::unordered_map functions; + void addFunction(std::string function, Callback callback); + }; +} diff --git a/src/Worker/Runner.cpp b/src/Worker/Runner.cpp index 0b06b5b5..afd27a15 100644 --- a/src/Worker/Runner.cpp +++ b/src/Worker/Runner.cpp @@ -2,14 +2,16 @@ namespace Worker { + Utils::IPC::BidirectionalChannel* Runner::Channel; + Runner::Runner(int pid) : processId(pid), terminate(false) { - + Runner::Channel = nullptr; } Runner::~Runner() { - + Runner::Channel = nullptr; } void Runner::run() @@ -46,6 +48,7 @@ namespace Worker { printf("Worker started\n"); Utils::IPC::BidirectionalChannel channel("IW4x-Worker-Channel", !Worker::IsWorker()); + Runner::Channel = &channel; while (!this->terminate) { @@ -74,5 +77,6 @@ namespace Worker } printf("Terminating worker\n"); + Runner::Channel = nullptr; } } diff --git a/src/Worker/Runner.hpp b/src/Worker/Runner.hpp index 328aa529..31ad6731 100644 --- a/src/Worker/Runner.hpp +++ b/src/Worker/Runner.hpp @@ -43,6 +43,8 @@ namespace Worker void attachHandler(Runner::Handler* handler); + static Utils::IPC::BidirectionalChannel* Channel; + private: void worker(); @@ -53,3 +55,4 @@ namespace Worker } #include "Handlers/Friends.hpp" +#include "Handlers/SteamCallbacks.hpp"