From 278d9aa7eb084bd6f93e6ac1fd84033dc316ab5e Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Thu, 15 Feb 2024 14:15:27 +0100 Subject: Fix handling of non-consecutive emote IDs Previous code was assuming there would be no gaps in the emote IDs. Also cleaned up some confusion where the "emote ID" being passed around in the code was often offset by 1. Now it is only offset in communication with tmwAthena and when saving the shortcuts. --- src/resources/emotedb.cpp | 71 +++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 39 deletions(-) (limited to 'src/resources/emotedb.cpp') diff --git a/src/resources/emotedb.cpp b/src/resources/emotedb.cpp index 752f7b4f..e1dd8f4a 100644 --- a/src/resources/emotedb.cpp +++ b/src/resources/emotedb.cpp @@ -27,12 +27,14 @@ #include "resources/resourcemanager.h" #include "resources/imageset.h" +#include +#include + namespace { - Emotes mEmotes; + std::vector mEmotes; Emote mUnknown; bool mLoaded = false; - int mLastEmote = 0; } void EmoteDB::init() @@ -41,33 +43,30 @@ void EmoteDB::init() unload(); mUnknown.name = "unknown"; - mUnknown.effect = -1; + mUnknown.effectId = -1; mUnknown.sprite = new ImageSprite( ResourceManager::getInstance()->getImage("graphics/sprites/error.png")); - - mLastEmote = 0; - } void EmoteDB::readEmoteNode(xmlNodePtr node, const std::string &filename) { - int id = XML::getProperty(node, "id", -1); + const int id = XML::getProperty(node, "id", -1); if (id == -1) { logger->log("Emote Database: Emote with missing ID in %s!", filename.c_str()); return; } - auto *currentEmote = new Emote; + Emote currentEmote; - currentEmote->name = XML::getProperty(node, "name", "unknown"); - currentEmote->effect = XML::getProperty(node, "effectid", -1); + currentEmote.id = id; + currentEmote.name = XML::getProperty(node, "name", "unknown"); + currentEmote.effectId = XML::getProperty(node, "effectid", -1); - if (currentEmote->effect == -1) + if (currentEmote.effectId == -1) { logger->log("Emote Database: Warning: Emote %s has no attached effect in %s!", - currentEmote->name.c_str(), filename.c_str()); - delete currentEmote; + currentEmote.name.c_str(), filename.c_str()); return; } @@ -78,31 +77,25 @@ void EmoteDB::readEmoteNode(xmlNodePtr node, const std::string &filename) if (imageName.empty() || width <= 0 || height <= 0) { logger->log("Emote Database: Warning: Emote %s has bad imageset values in %s", - currentEmote->name.c_str(), filename.c_str()); - delete currentEmote; + currentEmote.name.c_str(), filename.c_str()); return; } ImageSet *is = ResourceManager::getInstance()->getImageSet(imageName, width, height); - if (!is || !(is->size() > 0)) + if (!is || is->size() == 0) { logger->log("Emote Database: Error loading imageset for emote %s in %s", - currentEmote->name.c_str(), filename.c_str()); + currentEmote.name.c_str(), filename.c_str()); delete is; - delete currentEmote; return; } - else - { - // For now we just use the first image in the animation - currentEmote->sprite = new ImageSprite(is->get(0)); - } - mEmotes[id] = currentEmote; - if (id > mLastEmote) - mLastEmote = id; + // For now we just use the first image in the animation + currentEmote.sprite = new ImageSprite(is->get(0)); + + mEmotes.push_back(std::move(currentEmote)); } void EmoteDB::checkStatus() @@ -112,31 +105,31 @@ void EmoteDB::checkStatus() void EmoteDB::unload() { - Emotes::iterator i; - for (i = mEmotes.begin(); i != mEmotes.end(); i++) - { - delete i->second; - } - + // todo: don't we need to delete the sprites? mEmotes.clear(); - mLoaded = false; } -const Emote *EmoteDB::get(int id) +const Emote &EmoteDB::get(int id) { - Emotes::const_iterator i = mEmotes.find(id); + auto i = std::find_if(mEmotes.begin(), mEmotes.end(), + [id](const Emote &e) { return e.id == id; }); if (i == mEmotes.end()) { logger->log("EmoteDB: Warning, unknown emote ID %d requested", id); - return &mUnknown; + return mUnknown; } - return i->second; + return *i; +} + +const Emote &EmoteDB::getByIndex(int index) +{ + return mEmotes.at(index); } -int EmoteDB::getLast() +int EmoteDB::getEmoteCount() { - return mLastEmote; + return mEmotes.size(); } -- cgit v1.2.3-70-g09d2