Started work on a real avatars implementation.

Fix issue where since the image ids changed for
every get avatar call mudrunner would keep allocating
memory indefinitely.
merge-requests/21/head
Mr_Goldberg 2019-09-07 09:39:41 -04:00
parent 734ff89291
commit 58083e0863
No known key found for this signature in database
GPG Key ID: 8597D87419DEF278
4 changed files with 80 additions and 30 deletions

View File

@ -202,4 +202,15 @@ void Settings::setLeaderboard(std::string leaderboard, enum ELeaderboardSortMeth
leader.display_type = display_type; leader.display_type = display_type;
leaderboards[leaderboard] = leader; leaderboards[leaderboard] = leader;
} }
int Settings::add_image(std::string data, uint32 width, uint32 height)
{
int last = images.size() + 1;
struct Image_Data dt;
dt.width = width;
dt.height = height;
dt.data = data;
images[last] = dt;
return last;
}

View File

@ -52,6 +52,12 @@ struct Stat_config {
}; };
}; };
struct Image_Data {
uint32 width;
uint32 height;
std::string data;
};
class Settings { class Settings {
CSteamID steam_id; CSteamID steam_id;
CGameID game_id; CGameID game_id;
@ -116,6 +122,10 @@ public:
//stats //stats
std::map<std::string, Stat_config> getStats() { return stats; } std::map<std::string, Stat_config> getStats() { return stats; }
void setStatDefiniton(std::string name, struct Stat_config stat_config) {stats[name] = stat_config; } void setStatDefiniton(std::string name, struct Stat_config stat_config) {stats[name] = stat_config; }
//images
std::map<int, struct Image_Data> images;
int add_image(std::string data, uint32 width, uint32 height);
}; };
#endif #endif

View File

@ -19,6 +19,12 @@
#define SEND_FRIEND_RATE 4.0 #define SEND_FRIEND_RATE 4.0
struct Avatar_Numbers {
int smallest;
int medium;
int large;
};
class Steam_Friends : class Steam_Friends :
public ISteamFriends004, public ISteamFriends004,
public ISteamFriends005, public ISteamFriends005,
@ -45,7 +51,7 @@ public ISteamFriends
bool modified; bool modified;
std::vector<Friend> friends; std::vector<Friend> friends;
unsigned img_count; std::map<uint64, struct Avatar_Numbers> avatars;
CSteamID lobby_id; CSteamID lobby_id;
std::chrono::high_resolution_clock::time_point last_sent_friends; std::chrono::high_resolution_clock::time_point last_sent_friends;
@ -82,6 +88,29 @@ bool isAppIdCompatible(Friend *f)
return settings->get_local_game_id().AppID() == f->appid(); return settings->get_local_game_id().AppID() == f->appid();
} }
struct Avatar_Numbers add_friend_avatars(CSteamID id)
{
uint64 steam_id = id.ConvertToUint64();
auto avatar_ids = avatars.find(steam_id);
if (avatar_ids != avatars.end()) {
return avatar_ids->second;
}
//TODO: get real image data from self/other peers
struct Avatar_Numbers avatar_numbers;
char zero_array[184 * 184 * 4] = {};
std::string small_avatar(zero_array, 32 * 32 * 4);
std::string medium_avatar(zero_array, 64 * 64 * 4);
std::string large_avatar(zero_array, 184 * 184 * 4);
avatar_numbers.smallest = settings->add_image(small_avatar, 32, 32);
avatar_numbers.medium = settings->add_image(medium_avatar, 64, 64);
avatar_numbers.large = settings->add_image(large_avatar, 184, 184);
avatars[steam_id] = avatar_numbers;
return avatar_numbers;
}
public: public:
static void steam_friends_callback(void *object, Common_Message *msg) static void steam_friends_callback(void *object, Common_Message *msg)
{ {
@ -552,8 +581,10 @@ void ActivateGameOverlayInviteDialog( CSteamID steamIDLobby )
int GetSmallFriendAvatar( CSteamID steamIDFriend ) int GetSmallFriendAvatar( CSteamID steamIDFriend )
{ {
PRINT_DEBUG("Steam_Friends::GetSmallFriendAvatar\n"); PRINT_DEBUG("Steam_Friends::GetSmallFriendAvatar\n");
++img_count; //IMPORTANT NOTE: don't change friend avatar numbers for the same friend or else some games endlessly allocate stuff.
return (img_count * 3) + 1; std::lock_guard<std::recursive_mutex> lock(global_mutex);
struct Avatar_Numbers numbers = add_friend_avatars(steamIDFriend);
return numbers.smallest;
} }
@ -561,8 +592,9 @@ int GetSmallFriendAvatar( CSteamID steamIDFriend )
int GetMediumFriendAvatar( CSteamID steamIDFriend ) int GetMediumFriendAvatar( CSteamID steamIDFriend )
{ {
PRINT_DEBUG("Steam_Friends::GetMediumFriendAvatar\n"); PRINT_DEBUG("Steam_Friends::GetMediumFriendAvatar\n");
++img_count; std::lock_guard<std::recursive_mutex> lock(global_mutex);
return (img_count * 3) + 2; struct Avatar_Numbers numbers = add_friend_avatars(steamIDFriend);
return numbers.medium;
} }
@ -571,8 +603,9 @@ int GetMediumFriendAvatar( CSteamID steamIDFriend )
int GetLargeFriendAvatar( CSteamID steamIDFriend ) int GetLargeFriendAvatar( CSteamID steamIDFriend )
{ {
PRINT_DEBUG("Steam_Friends::GetLargeFriendAvatar\n"); PRINT_DEBUG("Steam_Friends::GetLargeFriendAvatar\n");
++img_count; std::lock_guard<std::recursive_mutex> lock(global_mutex);
return (img_count * 3) + 0; struct Avatar_Numbers numbers = add_friend_avatars(steamIDFriend);
return numbers.large;
} }
int GetFriendAvatar( CSteamID steamIDFriend, int eAvatarSize ) int GetFriendAvatar( CSteamID steamIDFriend, int eAvatarSize )
@ -671,19 +704,19 @@ bool SetRichPresence( const char *pchKey, const char *pchValue )
PRINT_DEBUG("Steam_Friends::SetRichPresence %s %s\n", pchKey, pchValue ? pchValue : "NULL"); PRINT_DEBUG("Steam_Friends::SetRichPresence %s %s\n", pchKey, pchValue ? pchValue : "NULL");
std::lock_guard<std::recursive_mutex> lock(global_mutex); std::lock_guard<std::recursive_mutex> lock(global_mutex);
if (pchValue) { if (pchValue) {
#ifdef SHOW_DIALOG_RICH_CONNECT auto prev_value = (*us.mutable_rich_presence()).find(pchKey);
if (std::string(pchKey) == std::string("connect")) if (prev_value == (*us.mutable_rich_presence()).end() || prev_value->second != pchValue) {
MessageBox(0, pchValue, pchKey, MB_OK); (*us.mutable_rich_presence())[pchKey] = pchValue;
#endif modified = true;
(*us.mutable_rich_presence())[pchKey] = pchValue; }
} else { } else {
auto to_remove = us.mutable_rich_presence()->find(pchKey); auto to_remove = us.mutable_rich_presence()->find(pchKey);
if (to_remove != us.mutable_rich_presence()->end()) { if (to_remove != us.mutable_rich_presence()->end()) {
us.mutable_rich_presence()->erase(to_remove); us.mutable_rich_presence()->erase(to_remove);
modified = true;
} }
} }
modified = true;
return true; return true;
} }

View File

@ -82,39 +82,35 @@ const char *GetIPCountry()
return "US"; return "US";
} }
static uint32 width_image(int iImage)
{
if ((iImage % 3) == 1) return 32;
if ((iImage % 3) == 2) return 64;
return 184;
}
// returns true if the image exists, and valid sizes were filled out // returns true if the image exists, and valid sizes were filled out
bool GetImageSize( int iImage, uint32 *pnWidth, uint32 *pnHeight ) bool GetImageSize( int iImage, uint32 *pnWidth, uint32 *pnHeight )
{ {
PRINT_DEBUG("GetImageSize\n"); PRINT_DEBUG("GetImageSize %i\n", iImage);
if (!iImage || !pnWidth || !pnHeight) return false; if (!iImage || !pnWidth || !pnHeight) return false;
auto image = settings->images.find(iImage);
if (image == settings->images.end()) return false;
*pnWidth = width_image(iImage); *pnWidth = image->second.width;
*pnHeight = width_image(iImage);; *pnHeight = image->second.height;
return true; return true;
} }
// returns true if the image exists, and the buffer was successfully filled out // returns true if the image exists, and the buffer was successfully filled out
// results are returned in RGBA format // results are returned in RGBA format
// the destination buffer size should be 4 * height * width * sizeof(char) // the destination buffer size should be 4 * height * width * sizeof(char)
bool GetImageRGBA( int iImage, uint8 *pubDest, int nDestBufferSize ) bool GetImageRGBA( int iImage, uint8 *pubDest, int nDestBufferSize )
{ {
PRINT_DEBUG("GetImageRGBA\n"); PRINT_DEBUG("GetImageRGBA %i\n", iImage);
if (!iImage || !pubDest || !nDestBufferSize) return false; if (!iImage || !pubDest || !nDestBufferSize) return false;
unsigned size = width_image(iImage) * width_image(iImage) * 4; auto image = settings->images.find(iImage);
if (image == settings->images.end()) return false;
unsigned size = image->second.data.size();
if (nDestBufferSize < size) size = nDestBufferSize; if (nDestBufferSize < size) size = nDestBufferSize;
memset(pubDest, 0xFF, size); image->second.data.copy((char *)pubDest, nDestBufferSize);
return true; return true;
} }
// returns the IP of the reporting server for valve - currently only used in Source engine games // returns the IP of the reporting server for valve - currently only used in Source engine games
bool GetCSERIPPort( uint32 *unIP, uint16 *usPort ) bool GetCSERIPPort( uint32 *unIP, uint16 *usPort )
{ {