diff --git a/dll/dll/settings.h b/dll/dll/settings.h index d0450bd2..559c0fa1 100644 --- a/dll/dll/settings.h +++ b/dll/dll/settings.h @@ -208,6 +208,8 @@ private: std::map leaderboards{}; std::map stats{}; + std::map images{}; + //supported languages std::set supported_languages_set{}; std::string supported_languages{}; @@ -265,8 +267,6 @@ public: // enable owning Steam Applications IDs (mostly builtin apps + dedicated servers) bool enable_builtin_preowned_ids = false; - std::map images{}; - //subscribed lobby/group ids std::set subscribed_groups{}; std::vector subscribed_groups_clans{}; @@ -383,6 +383,7 @@ public: //images int add_image(const std::string &data, uint32 width, uint32 height); + Image_Data* get_image(int handle); // overlay auto accept stuff void acceptAnyOverlayInvites(bool value); diff --git a/dll/local_storage.cpp b/dll/local_storage.cpp index 09ed210b..62dc0fb5 100644 --- a/dll/local_storage.cpp +++ b/dll/local_storage.cpp @@ -859,9 +859,9 @@ std::vector Local_Storage::load_image(std::string const& image_pa std::vector res{}; int width{}, height{}; image_pixel_t* img = (image_pixel_t*)stbi_load(image_path.c_str(), &width, &height, nullptr, 4); - if (img != nullptr) - { - res.resize(width*height); + PRINT_DEBUG("stbi_load('%s') -> %s", image_path.c_str(), (img ? "loaded" : stbi_failure_reason())); + if (img) { + res.resize(width * height); std::copy(img, img + width * height, res.begin()); stbi_image_free(img); @@ -875,19 +875,18 @@ std::string Local_Storage::load_image_resized(std::string const& image_path, std { std::string resized_image{}; const size_t resized_img_size = resolution * resolution * 4; - - if (image_path.length() > 0) { + if (image_path.size()) { int width = 0; int height = 0; unsigned char *img = stbi_load(image_path.c_str(), &width, &height, nullptr, 4); - PRINT_DEBUG("stbi_load('%s') -> %s", image_path.c_str(), (img == nullptr ? stbi_failure_reason() : "loaded")); - if (img != nullptr) { + PRINT_DEBUG("stbi_load('%s') -> %s", image_path.c_str(), (img ? "loaded" : stbi_failure_reason())); + if (img) { std::vector out_resized(resized_img_size); stbir_resize_uint8(img, width, height, 0, (unsigned char*)&out_resized[0], resolution, resolution, 0, 4); resized_image = std::string((char*)&out_resized[0], out_resized.size()); stbi_image_free(img); } - } else if (image_data.length() > 0) { + } else if (image_data.size()) { std::vector out_resized(resized_img_size); stbir_resize_uint8((unsigned char*)image_data.c_str(), 184, 184, 0, (unsigned char*)&out_resized[0], resolution, resolution, 0, 4); resized_image = std::string((char*)&out_resized[0], out_resized.size()); diff --git a/dll/settings.cpp b/dll/settings.cpp index 2093c0d5..531fbd0f 100644 --- a/dll/settings.cpp +++ b/dll/settings.cpp @@ -353,13 +353,33 @@ std::map::const_iterator Settings::setStatDefiniton(co int Settings::add_image(const std::string &data, uint32 width, uint32 height) { - int last = static_cast(images.size()) + 1; - struct Image_Data dt; + auto previous_it = std::find_if(images.begin(), images.end(), [&](const std::pair &item) { + return item.second.data == data + && item.second.height == height + && item.second.width == width; + }); + if (images.end() != previous_it) { + return static_cast(previous_it->first); + } + + struct Image_Data dt{}; dt.width = width; dt.height = height; dt.data = data; - images[last] = dt; - return last; + + auto new_handle = images.size() + 1; // never return 0, it is a bad handle for most ISteamUserStats APIs + images[new_handle] = dt; + + return static_cast(new_handle); +} + +Image_Data* Settings::get_image(int handle) +{ + auto image_it = images.find(handle); + if (images.end() == image_it) { + return nullptr; + } + return &image_it->second; } diff --git a/dll/steam_friends.cpp b/dll/steam_friends.cpp index a7a3070c..0393b2ae 100644 --- a/dll/steam_friends.cpp +++ b/dll/steam_friends.cpp @@ -1280,9 +1280,15 @@ void Steam_Friends::Callback(Common_Message *msg) f->set_name(settings->get_local_name()); f->set_appid(settings->get_local_game_id().AppID()); f->set_lobby_id(settings->get_lobby().ConvertToUint64()); + int avatar_number = GetLargeFriendAvatar(settings->get_local_steam_id()); - if (settings->images[avatar_number].data.length() > 0) f->set_avatar(settings->images[avatar_number].data); - else f->set_avatar(""); + auto avatar_info = settings->get_image(avatar_number); + if (avatar_info && avatar_info->data.size()) { + f->set_avatar(avatar_info->data); + } else { + f->set_avatar(""); + } + msg_.set_allocated_friend_(f); network->sendTo(&msg_, true); } diff --git a/dll/steam_utils.cpp b/dll/steam_utils.cpp index afa4eb39..f4395135 100644 --- a/dll/steam_utils.cpp +++ b/dll/steam_utils.cpp @@ -81,11 +81,11 @@ bool Steam_Utils::GetImageSize( int iImage, uint32 *pnWidth, uint32 *pnHeight ) if (!iImage || !pnWidth || !pnHeight) return false; - auto image = settings->images.find(iImage); - if (settings->images.end() == image) return false; + auto image_info = settings->get_image(iImage); + if (!image_info) return false; - *pnWidth = image->second.width; - *pnHeight = image->second.height; + *pnWidth = image_info->width; + *pnHeight = image_info->height; return true; } @@ -99,10 +99,10 @@ bool Steam_Utils::GetImageRGBA( int iImage, uint8 *pubDest, int nDestBufferSize if (!iImage || !pubDest || nDestBufferSize <= 0) return false; - auto image = settings->images.find(iImage); - if (settings->images.end() == image) return false; + auto image_info = settings->get_image(iImage); + if (!image_info) return false; - image->second.data.copy((char *)pubDest, nDestBufferSize); + image_info->data.copy((char *)pubDest, nDestBufferSize); return true; }