[Friends] Correctly transmit presence data

This commit is contained in:
momo5502 2017-01-29 00:31:11 +01:00
parent 4271e3b108
commit 4e587e91a0
4 changed files with 72 additions and 22 deletions

View File

@ -8,11 +8,8 @@ namespace Components
void Friends::UpdateUserInfo(SteamID user) void Friends::UpdateUserInfo(SteamID user)
{ {
if (!Steam::Proxy::SteamFriends) return;
std::lock_guard<std::recursive_mutex> _(Friends::Mutex); std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
Friends::Friend userInfo;
auto i = std::find_if(Friends::FriendsList.begin(), Friends::FriendsList.end(), [user] (Friends::Friend entry) auto i = std::find_if(Friends::FriendsList.begin(), Friends::FriendsList.end(), [user] (Friends::Friend entry)
{ {
return (entry.userId.Bits == user.Bits); return (entry.userId.Bits == user.Bits);
@ -20,28 +17,30 @@ namespace Components
if(i != Friends::FriendsList.end()) if(i != Friends::FriendsList.end())
{ {
userInfo = *i; Proto::IPC::Function function;
function.set_name("getPresence");
*function.add_params() = Utils::String::VA("%llx", user.Bits);
std::string* key = function.add_params();
*key = "iw4x_status";
IPCHandler::SendWorker("friends", function.SerializeAsString());
*key = "iw4x_rank";
IPCHandler::SendWorker("friends", function.SerializeAsString());
*key = "iw4x_server";
IPCHandler::SendWorker("friends", function.SerializeAsString());
} }
userInfo.userId = user; /*userInfo.online = Steam::Proxy::SteamFriends->GetFriendPersonaState(user) != 0;
userInfo.online = Steam::Proxy::SteamFriends->GetFriendPersonaState(user) != 0;
userInfo.name = Steam::Proxy::SteamFriends->GetFriendPersonaName(user); userInfo.name = Steam::Proxy::SteamFriends->GetFriendPersonaName(user);
userInfo.statusName = Steam::Proxy::SteamFriends->GetFriendRichPresence(user, "iw4x_status"); userInfo.statusName = Steam::Proxy::SteamFriends->GetFriendRichPresence(user, "iw4x_status");
userInfo.prestige = Utils::Cryptography::Rand::GenerateInt() % (10 + 1); userInfo.prestige = Utils::Cryptography::Rand::GenerateInt() % (10 + 1);
userInfo.experience = Utils::Cryptography::Rand::GenerateInt() % (2516000 + 1); userInfo.experience = Utils::Cryptography::Rand::GenerateInt() % (2516000 + 1);*/
//if (!userInfo.online) return; /* qsort(Friends::FriendsList.data(), Friends::FriendsList.size(), sizeof(Friends::Friend), [](const void* first, const void* second)
if (i != Friends::FriendsList.end())
{
*i = userInfo;
}
else
{
Friends::FriendsList.push_back(userInfo);
}
qsort(Friends::FriendsList.data(), Friends::FriendsList.size(), sizeof(Friends::Friend), [](const void* first, const void* second)
{ {
const Friends::Friend* friend1 = static_cast<const Friends::Friend*>(first); const Friends::Friend* friend1 = static_cast<const Friends::Friend*>(first);
const Friends::Friend* friend2 = static_cast<const Friends::Friend*>(second); const Friends::Friend* friend2 = static_cast<const Friends::Friend*>(second);
@ -50,7 +49,7 @@ namespace Components
std::string name2 = Utils::String::ToLower(Colors::Strip(friend2->name)); std::string name2 = Utils::String::ToLower(Colors::Strip(friend2->name));
return name1.compare(name2); return name1.compare(name2);
}); });*/
} }
void Friends::UpdateFriends() void Friends::UpdateFriends()
@ -143,6 +142,41 @@ namespace Components
} }
} }
void Friends::PresenceResponse(std::vector<std::string> params)
{
if (params.size() >= 3)
{
std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
SteamID id;
id.Bits = strtoull(params[0].data(), nullptr, 16);
std::string key = params[1];
std::string value = params[2];
auto entry = std::find_if(Friends::FriendsList.begin(), Friends::FriendsList.end(), [id](Friends::Friend entry)
{
return (entry.userId.Bits == id.Bits);
});
if (entry == Friends::FriendsList.end()) return;
if(key == "iw4x_status")
{
entry->statusName = value;
}
else if(key == "iw4x_rank")
{
if(value.size() == 4)
{
int data = *reinterpret_cast<int*>(const_cast<char*>(value.data()));
entry->experience = data & 0xFFFFFF;
entry->prestige = (data >> 24) & 0xFF;
}
}
}
}
void Friends::FriendsResponse(std::vector<std::string> params) void Friends::FriendsResponse(std::vector<std::string> params)
{ {
std::lock_guard<std::recursive_mutex> _(Friends::Mutex); std::lock_guard<std::recursive_mutex> _(Friends::Mutex);
@ -195,7 +229,7 @@ namespace Components
auto fInterface = IPCHandler::NewInterface("steamCallbacks"); auto fInterface = IPCHandler::NewInterface("steamCallbacks");
// Callback to update user information // Callback to update user information
fInterface->map("304", [](std::vector<std::string> params) fInterface->map("336", [](std::vector<std::string> params)
{ {
if (params.size() >= 1 && params[0].size() == sizeof(Friends::FriendRichPresenceUpdate)) if (params.size() >= 1 && params[0].size() == sizeof(Friends::FriendRichPresenceUpdate))
{ {
@ -228,6 +262,7 @@ namespace Components
fInterface = IPCHandler::NewInterface("friends"); fInterface = IPCHandler::NewInterface("friends");
fInterface->map("friendsResponse", Friends::FriendsResponse); fInterface->map("friendsResponse", Friends::FriendsResponse);
fInterface->map("nameResponse", Friends::NameResponse); fInterface->map("nameResponse", Friends::NameResponse);
fInterface->map("presenceResponse", Friends::PresenceResponse);
} }
Friends::~Friends() Friends::~Friends()

View File

@ -15,11 +15,13 @@ namespace Components
static void UpdateFriends(); static void UpdateFriends();
private: private:
#pragma pack(push, 4)
struct FriendRichPresenceUpdate struct FriendRichPresenceUpdate
{ {
SteamID m_steamIDFriend; // friend who's rich presence has changed SteamID m_steamIDFriend; // friend who's rich presence has changed
int32_t m_nAppID; // the appID of the game (should always be the current game) int32_t m_nAppID; // the appID of the game (should always be the current game)
}; };
#pragma pack(pop)
struct PersonaStateChange struct PersonaStateChange
{ {
@ -51,5 +53,6 @@ namespace Components
static void FriendsResponse(std::vector<std::string> params); static void FriendsResponse(std::vector<std::string> params);
static void NameResponse(std::vector<std::string> params); static void NameResponse(std::vector<std::string> params);
static void PresenceResponse(std::vector<std::string> params);
}; };
} }

View File

@ -93,7 +93,18 @@ namespace Handlers
response.set_name("presenceResponse"); response.set_name("presenceResponse");
*response.add_params() = Utils::String::VA("%llX", id.Bits); *response.add_params() = Utils::String::VA("%llX", id.Bits);
*response.add_params() = params[1].data(); *response.add_params() = params[1].data();
*response.add_params() = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, params[1].data());
std::string* value = response.add_params();
*value = Steam::Proxy::SteamFriends->GetFriendRichPresence(id, params[1].data());
if (params[1] == "iw4x_rank")
{
int experience = Utils::Cryptography::Rand::GenerateInt() % (2516000 + 1);
int prestige = Utils::Cryptography::Rand::GenerateInt() % (10 + 1);
int data = (experience & 0xFFFFFF) | ((prestige << 24) & 0xFF);
*value = std::string(reinterpret_cast<char*>(&data), 4);
}
endpoint.send(this->getCommand(), response.SerializeAsString()); endpoint.send(this->getCommand(), response.SerializeAsString());
} }

View File

@ -8,6 +8,7 @@ namespace Worker
{ {
Runner runner(Worker::ProcessId); Runner runner(Worker::ProcessId);
runner.attachHandler(new Handlers::Friends()); runner.attachHandler(new Handlers::Friends());
runner.attachHandler(new Handlers::SteamCallbacks());
runner.run(); runner.run();
return 0; return 0;
} }