Skip to content

Commit 8d920dd

Browse files
author
kwolekr
committedFeb 16, 2013
Merge pull request luanti-org#465 from doserj/mod_selection_empty_modname_fix
Remove use of operator[] on a std::map, so no spurious elements get inse...
2 parents c88d89d + 9ebf1fd commit 8d920dd

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed
 

‎src/guiConfigureWorld.cpp

+51-32
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,9 @@ bool GUIConfigureWorld::OnEvent(const SEvent& event)
414414
else if(selected_node != NULL && selected_node->getText() != NULL)
415415
{
416416
std::string modname = wide_to_narrow(selected_node->getText());
417-
ModSpec mod = m_addonmods[modname];
418-
enableAllMods(mod.modpack_content,true);
417+
std::map<std::string, ModSpec>::iterator mod_it = m_addonmods.find(modname);
418+
if(mod_it != m_addonmods.end())
419+
enableAllMods(mod_it->second.modpack_content,true);
419420
}
420421
return true;
421422
}
@@ -427,8 +428,9 @@ bool GUIConfigureWorld::OnEvent(const SEvent& event)
427428
if(selected_node != NULL && selected_node->getText() != NULL)
428429
{
429430
std::string modname = wide_to_narrow(selected_node->getText());
430-
ModSpec mod = m_addonmods[modname];
431-
enableAllMods(mod.modpack_content,false);
431+
std::map<std::string, ModSpec>::iterator mod_it = m_addonmods.find(modname);
432+
if(mod_it != m_addonmods.end())
433+
enableAllMods(mod_it->second.modpack_content,false);
432434
}
433435
return true;
434436
}
@@ -446,7 +448,7 @@ bool GUIConfigureWorld::OnEvent(const SEvent& event)
446448
return true;
447449
}
448450
if(event.GUIEvent.EventType==gui::EGET_TREEVIEW_NODE_SELECT &&
449-
event.GUIEvent.Caller->getID() == GUI_ID_MOD_TREEVIEW)
451+
event.GUIEvent.Caller->getID() == GUI_ID_MOD_TREEVIEW)
450452
{
451453
selecting_dep = -1;
452454
selecting_rdep = -1;
@@ -560,42 +562,49 @@ void GUIConfigureWorld::adjustSidebar()
560562
modname_w = L"N/A";
561563
std::string modname = wide_to_narrow(modname_w);
562564

563-
// if no mods installed, don't show buttons or checkbox on the sidebar
565+
ModSpec mspec;
566+
std::map<std::string, ModSpec>::iterator it = m_addonmods.find(modname);
567+
if(it != m_addonmods.end())
568+
mspec = it->second;
569+
570+
m_dependencies_listbox->clear();
571+
m_rdependencies_listbox->clear();
572+
573+
// if no mods installed, there is nothing to enable/disable, so we
574+
// don't show buttons or checkbox on the sidebar
564575
if(node->getParent() == m_treeview->getRoot() && !node->hasChilds())
565576
{
566577
m_disableall->setVisible(false);
567578
m_enableall->setVisible(false);
568579
m_enabled_checkbox->setVisible(false);
580+
return;
569581
}
570-
else
582+
583+
// a modpack is not enabled/disabled by itself, only its cotnents
584+
// are. so we show show enable/disable all buttons, but hide the
585+
// checkbox
586+
if(node->getParent() == m_treeview->getRoot() ||
587+
mspec.is_modpack)
571588
{
572-
// if modpack, show enable/disable all buttons. otherwise, show
573-
// enabled checkbox
574-
if(node->getParent() == m_treeview->getRoot() ||
575-
m_addonmods[modname].is_modpack)
576-
{
577-
m_enabled_checkbox->setVisible(false);
578-
m_disableall->setVisible(true);
579-
m_enableall->setVisible(true);
580-
m_modname_text->setText((L"Modpack: "+modname_w).c_str());
581-
}
582-
else
583-
{
584-
m_disableall->setVisible(false);
585-
m_enableall->setVisible(false);
586-
m_enabled_checkbox->setVisible(true);
587-
m_modname_text->setText((L"Mod: "+modname_w).c_str());
588-
}
589-
}
589+
m_enabled_checkbox->setVisible(false);
590+
m_disableall->setVisible(true);
591+
m_enableall->setVisible(true);
592+
m_modname_text->setText((L"Modpack: "+modname_w).c_str());
593+
return;
594+
}
595+
596+
// for a normal mod, we hide the enable/disable all buttons, but show the checkbox.
597+
m_disableall->setVisible(false);
598+
m_enableall->setVisible(false);
599+
m_enabled_checkbox->setVisible(true);
600+
m_modname_text->setText((L"Mod: "+modname_w).c_str());
601+
590602
// the mod is enabled unless it is disabled in the world.mt settings.
591603
bool mod_enabled = true;
592604
if(m_settings.exists("load_mod_"+modname))
593605
mod_enabled = m_settings.getBool("load_mod_"+modname);
594606
m_enabled_checkbox->setChecked(mod_enabled);
595607

596-
// dependencies of this mod:
597-
m_dependencies_listbox->clear();
598-
ModSpec mspec = m_addonmods[modname];
599608
for(std::set<std::string>::iterator it=mspec.depends.begin();
600609
it != mspec.depends.end(); ++it)
601610
{
@@ -611,9 +620,7 @@ void GUIConfigureWorld::adjustSidebar()
611620
m_dependencies_listbox->addItem(narrow_to_wide(dependency).c_str());
612621
}
613622

614-
615623
// reverse dependencies of this mod:
616-
m_rdependencies_listbox->clear();
617624
std::pair< std::multimap<std::string, std::string>::iterator,
618625
std::multimap<std::string, std::string>::iterator > rdep =
619626
m_reverse_depends.equal_range(modname);
@@ -639,19 +646,25 @@ void GUIConfigureWorld::enableAllMods(std::map<std::string, ModSpec> mods,bool e
639646
enableAllMods(mod.modpack_content,enable);
640647
else // not a modpack
641648
setEnabled(mod.name, enable);
649+
642650
}
643651
}
644652

645653
void GUIConfigureWorld::enableMod(std::string modname)
646654
{
655+
std::map<std::string, ModSpec>::iterator mod_it = m_addonmods.find(modname);
656+
if(mod_it == m_addonmods.end()){
657+
errorstream << "enableMod() called with invalid mod name \"" << modname << "\"" << std::endl;
658+
return;
659+
}
660+
ModSpec mspec = mod_it->second;
647661
m_settings.setBool("load_mod_"+modname,true);
648662
std::map<std::string,gui::IGUITreeViewNode*>::iterator it =
649663
m_nodes.find(modname);
650-
if(it != m_nodes.end())
664+
if(it != m_nodes.end())
651665
(*it).second->setIcon(CHECKMARK_STR);
652666
m_new_mod_names.erase(modname);
653667
//also enable all dependencies
654-
ModSpec mspec = m_addonmods[modname];
655668
for(std::set<std::string>::iterator it=mspec.depends.begin();
656669
it != mspec.depends.end(); ++it)
657670
{
@@ -664,6 +677,12 @@ void GUIConfigureWorld::enableMod(std::string modname)
664677

665678
void GUIConfigureWorld::disableMod(std::string modname)
666679
{
680+
std::map<std::string, ModSpec>::iterator mod_it = m_addonmods.find(modname);
681+
if(mod_it == m_addonmods.end()){
682+
errorstream << "disableMod() called with invalid mod name \"" << modname << "\"" << std::endl;
683+
return;
684+
}
685+
667686
m_settings.setBool("load_mod_"+modname,false);
668687
std::map<std::string,gui::IGUITreeViewNode*>::iterator it =
669688
m_nodes.find(modname);

0 commit comments

Comments
 (0)
Please sign in to comment.