Skip to content

Commit

Permalink
Fix #5232
Browse files Browse the repository at this point in the history
- Don't clear the favorites data of the kart properties manager when leaving the kart selection screen - this was the cause of the bug.
- Remove a block of code that was never accessed because the condition was always false
- Use getIdent() behind the scenes for more robustness
- Prevent a crash when uninstalling an addon that's registered as a favorite kart
  • Loading branch information
Alayan-stk-2 committed Dec 29, 2024
1 parent 2fac72b commit 4ea1a28
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/config/favorite_status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ FavoriteStatus::FavoriteStatus(const XMLNode* node, std::string parse_type)
m_favorite[temp_group_string].insert(temp_string);
}
}
} // FavoriteStatu
} // FavoriteStatus

//------------------------------------------------------------------------------
/** Adds a new favorite track to this player profile and to the group
Expand Down
4 changes: 2 additions & 2 deletions src/karts/kart_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ bool KartProperties::operator<(const KartProperties &other) const
PlayerProfile *p = PlayerManager::getCurrentPlayer();
bool this_is_locked = p->isLocked(getIdent());
bool other_is_locked = p->isLocked(other.getIdent());
bool this_is_favorite = p->isFavoriteKart(getNonTranslatedName());
bool other_is_favorite = p->isFavoriteKart(other.getNonTranslatedName());
bool this_is_favorite = p->isFavoriteKart(getIdent());
bool other_is_favorite = p->isFavoriteKart(other.getIdent());

if (this_is_locked != other_is_locked)
{
Expand Down
39 changes: 11 additions & 28 deletions src/karts/kart_properties_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void KartPropertiesManager::removeKart(const std::string &ident)
for (auto it = m_current_favorite_status->getAllFavorites().begin();
it != m_current_favorite_status->getAllFavorites().end(); it++)
{ // User-defined groups
if (it->second.find(kp->getNonTranslatedName()) != it->second.end())
if (it->second.find(kp->getIdent()) != it->second.end())
{
groups.push_back(it->first);
}
Expand All @@ -129,13 +129,14 @@ void KartPropertiesManager::removeKart(const std::string &ident)
it = std::find(m_groups_2_indices_no_custom[groups[i]].begin(),
m_groups_2_indices_no_custom[groups[i]].end(), index);
// Handle no custom group first
assert(it!=m_groups_2_indices_no_custom[groups[i]].end());

m_groups_2_indices_no_custom[groups[i]].erase(it);

if(m_groups_2_indices_no_custom[groups[i]].size()==0)
if(it!=m_groups_2_indices_no_custom[groups[i]].end())
{
m_groups_2_indices_no_custom.erase(groups[i]);
m_groups_2_indices_no_custom[groups[i]].erase(it);

if(m_groups_2_indices_no_custom[groups[i]].size()==0)
{
m_groups_2_indices_no_custom.erase(groups[i]);
}
}

it = std::find(m_groups_2_indices[groups[i]].begin(),
Expand Down Expand Up @@ -317,17 +318,6 @@ bool KartPropertiesManager::loadKart(const std::string &dir)
m_kart_available.push_back(true);

std::vector<std::string> groups=kart_properties->getGroups();
if (m_current_favorite_status)
{
for (auto it = m_current_favorite_status->getAllFavorites().begin();
it != m_current_favorite_status->getAllFavorites().end(); it++)
{ // User-defined groups
if (it->second.find(kart_properties->getNonTranslatedName()) != it->second.end())
{
groups.push_back(it->first);
}
}
}

for(unsigned int g=0; g<groups.size(); g++)
{
Expand Down Expand Up @@ -518,7 +508,7 @@ void KartPropertiesManager::setFavoriteKartStatus(FavoriteStatus *status)
{
for (unsigned int i=0; i<m_karts_properties.size(); i++)
{
if (m_karts_properties[i].getNonTranslatedName() == *it_name)
if (m_karts_properties[i].getIdent() == *it_name)
{
m_groups_2_indices[it->first].push_back(i);
break;
Expand All @@ -544,13 +534,7 @@ void KartPropertiesManager::setFavoriteKartStatus(FavoriteStatus *status)
int x = g2i.find(a)->second[0], y = g2i.find(b)->second[0];
return x == y ? a < b : x < y;
});
} // addFavorite

//-----------------------------------------------------------------------------
void KartPropertiesManager::clearFavoriteKartStatus()
{
setFavoriteKartStatus(NULL);
} // addFavorite
} // setFavoriteKartStatus

//-----------------------------------------------------------------------------
/** Returns true if a kart is available to be selected. A kart is available to
Expand Down Expand Up @@ -588,8 +572,7 @@ void KartPropertiesManager::selectKartName(const std::string &kart_name)
* determined
* \return A vector of indices with the karts in the given group.
*/
const std::vector<int> KartPropertiesManager::getKartsInGroup(
const std::string& g)
const std::vector<int> KartPropertiesManager::getKartsInGroup(const std::string& g)
{
if (g == ALL_KART_GROUPS_ID)
{
Expand Down
1 change: 0 additions & 1 deletion src/karts/kart_properties_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class KartPropertiesManager: public NoCopy
* We need to treat it specially, because the list of tracks in this group
* depends on the player-profile, not on the track data. */
void setFavoriteKartStatus(FavoriteStatus *status);
void clearFavoriteKartStatus();
bool kartAvailable(int kartid);
std::vector<std::string> getAllAvailableKarts() const;
void setUnavailableKarts(std::vector<std::string>);
Expand Down
10 changes: 4 additions & 6 deletions src/states_screens/kart_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,6 @@ void KartSelectionScreen::init()

void KartSelectionScreen::tearDown()
{
kart_properties_manager->clearFavoriteKartStatus();

#ifndef SERVER_ONLY
GE::getGEConfig()->m_enable_draw_call_cache = false;
GE::GEVulkanDriver* gevk = GE::getVKDriver();
Expand Down Expand Up @@ -1220,13 +1218,13 @@ void KartSelectionScreen::eventCallback(Widget* widget,
{
const KartProperties *kp = kart_properties_manager->getKart(selection);

if (PlayerManager::getCurrentPlayer()->isFavoriteKart(kp->getNonTranslatedName()))
if (PlayerManager::getCurrentPlayer()->isFavoriteKart(kp->getIdent()))
{
PlayerManager::getCurrentPlayer()->removeFavoriteKart(kp->getNonTranslatedName());
PlayerManager::getCurrentPlayer()->removeFavoriteKart(kp->getIdent());
}
else
{
PlayerManager::getCurrentPlayer()->addFavoriteKart(kp->getNonTranslatedName());
PlayerManager::getCurrentPlayer()->addFavoriteKart(kp->getIdent());
}
setKartsFromCurrentGroup();
}
Expand Down Expand Up @@ -1674,7 +1672,7 @@ void KartSelectionScreen::setKartsFromCurrentGroup()
prop->getAbsoluteIconFile(), LOCKED_BADGE,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
}
else if (PlayerManager::getCurrentPlayer()->isFavoriteKart(prop->getNonTranslatedName()))
else if (PlayerManager::getCurrentPlayer()->isFavoriteKart(prop->getIdent()))
{
w->addItem(prop->getName(),
prop->getIdent(),
Expand Down

0 comments on commit 4ea1a28

Please sign in to comment.