[Menus] Safer menu merging

This commit is contained in:
momo5502 2017-07-09 01:56:12 +02:00
parent 32789b29cf
commit 3f06d6c990

View File

@ -273,17 +273,17 @@ namespace Components
void Menus::SafeMergeMenus(std::vector<std::pair<bool, Game::menuDef_t*>>* menus, std::vector<std::pair<bool, Game::menuDef_t*>> newMenus) void Menus::SafeMergeMenus(std::vector<std::pair<bool, Game::menuDef_t*>>* menus, std::vector<std::pair<bool, Game::menuDef_t*>> newMenus)
{ {
// Check if we overwrote a menu // Check if we overwrote a menu
for (unsigned int i = 0; i < menus->size(); ++i) for (auto i = menus->begin(); i != menus->end();)
{ {
// Try to find the native menu // Try to find the native menu
bool found = !menus->at(i).first; // Only if custom menu, try to find it bool found = !i->first; // Only if custom menu, try to find it
// If there is none, try to find a custom menu // If there is none, try to find a custom menu
if (!found) if (!found)
{ {
for (auto& entry : Menus::MenuList) for (auto& entry : Menus::MenuList)
{ {
if (menus->at(i).second == entry.second) if (i->second == entry.second)
{ {
found = true; found = true;
break; break;
@ -294,23 +294,26 @@ namespace Components
// Remove the menu if it has been deallocated (not found) // Remove the menu if it has been deallocated (not found)
if (!found) if (!found)
{ {
menus->erase(menus->begin() + i); i = menus->erase(i);
--i;
continue; continue;
} }
bool increment = true;
// Remove the menu if it has been loaded twice // Remove the menu if it has been loaded twice
for (auto& newMenu : newMenus) for (auto& newMenu : newMenus)
{ {
if (menus->at(i).second->window.name == std::string(newMenu.second->window.name)) if (i->second->window.name == std::string(newMenu.second->window.name))
{ {
Menus::RemoveMenu(menus->at(i).second); Menus::RemoveMenu(i->second);
menus->erase(menus->begin() + i); i = menus->erase(i);
--i; increment = false;
break; break;
} }
} }
if(increment) ++i;
} }
Utils::Merge(menus, newMenus); Utils::Merge(menus, newMenus);