Browse Source

Fix: client crashes while browsing saves, if some factions are removed

Dmitry Orlov 4 years ago
parent
commit
9cf953157a
1 changed files with 26 additions and 14 deletions
  1. 26 14
      client/lobby/OptionsTab.cpp

+ 26 - 14
client/lobby/OptionsTab.cpp

@@ -83,20 +83,20 @@ size_t OptionsTab::CPlayerSettingsHelper::getImageIndex()
 		TOWN_RANDOM = 38,  TOWN_NONE = 39, // Special frames in ITPA
 		HERO_RANDOM = 163, HERO_NONE = 164 // Special frames in PortraitsSmall
 	};
+	auto factionIndex = settings.castle >= CGI->townh->factions.size() ? 0 : settings.castle;
 
 	switch(type)
 	{
 	case TOWN:
-		switch(settings.castle)
+		switch (settings.castle)
 		{
 		case PlayerSettings::NONE:
 			return TOWN_NONE;
 		case PlayerSettings::RANDOM:
 			return TOWN_RANDOM;
 		default:
-			return CGI->townh->factions[settings.castle]->town->clientInfo.icons[true][false] + 2;
+			return CGI->townh->factions[factionIndex]->town->clientInfo.icons[true][false] + 2;
 		}
-
 	case HERO:
 		switch(settings.hero)
 		{
@@ -108,7 +108,8 @@ size_t OptionsTab::CPlayerSettingsHelper::getImageIndex()
 		{
 			if(settings.heroPortrait >= 0)
 				return settings.heroPortrait;
-			return CGI->heroh->heroes[settings.hero]->imageIndex;
+			auto index = settings.hero >= CGI->heroh->heroes.size() ? 0 : settings.hero;
+			return CGI->heroh->heroes[index]->imageIndex;
 		}
 		}
 
@@ -124,7 +125,7 @@ size_t OptionsTab::CPlayerSettingsHelper::getImageIndex()
 			return GOLD;
 		case PlayerSettings::RESOURCE:
 		{
-			switch(CGI->townh->factions[settings.castle]->town->primaryRes)
+			switch(CGI->townh->factions[factionIndex]->town->primaryRes)
 			{
 			case Res::WOOD_AND_ORE:
 				return WOOD_ORE;
@@ -179,7 +180,10 @@ std::string OptionsTab::CPlayerSettingsHelper::getName()
 		case PlayerSettings::RANDOM:
 			return CGI->generaltexth->allTexts[522];
 		default:
-			return CGI->townh->factions[settings.castle]->name;
+		{
+			auto factionIndex = settings.castle >= CGI->townh->factions.size() ? 0 : settings.castle;
+			return CGI->townh->factions[factionIndex]->name;
+		}
 		}
 	}
 	case HERO:
@@ -194,7 +198,8 @@ std::string OptionsTab::CPlayerSettingsHelper::getName()
 		{
 			if(!settings.heroName.empty())
 				return settings.heroName;
-			return CGI->heroh->heroes[settings.hero]->name;
+			auto index = settings.hero >= CGI->heroh->heroes.size() ? 0 : settings.hero;
+			return CGI->heroh->heroes[index]->name;
 		}
 		}
 	}
@@ -240,6 +245,9 @@ std::string OptionsTab::CPlayerSettingsHelper::getTitle()
 }
 std::string OptionsTab::CPlayerSettingsHelper::getSubtitle()
 {
+	auto factionIndex = settings.castle >= CGI->townh->factions.size() ? 0 : settings.castle;
+	auto heroIndex = settings.hero >= CGI->heroh->heroes.size() ? 0 : settings.hero;
+
 	switch(type)
 	{
 	case TOWN:
@@ -247,7 +255,7 @@ std::string OptionsTab::CPlayerSettingsHelper::getSubtitle()
 	case HERO:
 	{
 		if(settings.hero >= 0)
-			return getName() + " - " + CGI->heroh->heroes[settings.hero]->heroClass->name;
+			return getName() + " - " + CGI->heroh->heroes[heroIndex]->heroClass->name;
 		return getName();
 	}
 
@@ -259,7 +267,7 @@ std::string OptionsTab::CPlayerSettingsHelper::getSubtitle()
 			return CGI->generaltexth->allTexts[87]; //500-1000
 		case PlayerSettings::RESOURCE:
 		{
-			switch(CGI->townh->factions[settings.castle]->town->primaryRes)
+			switch(CGI->townh->factions[factionIndex]->town->primaryRes)
 			{
 			case Res::MERCURY:
 				return CGI->generaltexth->allTexts[694];
@@ -281,6 +289,8 @@ std::string OptionsTab::CPlayerSettingsHelper::getSubtitle()
 
 std::string OptionsTab::CPlayerSettingsHelper::getDescription()
 {
+	auto factionIndex = settings.castle >= CGI->townh->factions.size() ? 0 : settings.castle;
+
 	switch(type)
 	{
 	case TOWN:
@@ -299,7 +309,7 @@ std::string OptionsTab::CPlayerSettingsHelper::getDescription()
 			return CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
 		case PlayerSettings::RESOURCE:
 		{
-			switch(CGI->townh->factions[settings.castle]->town->primaryRes)
+			switch(CGI->townh->factions[factionIndex]->town->primaryRes)
 			{
 			case Res::MERCURY:
 				return CGI->generaltexth->allTexts[690];
@@ -366,9 +376,10 @@ void OptionsTab::CPlayerOptionTooltipBox::genTownWindow()
 	pos = Rect(0, 0, 228, 290);
 	genHeader();
 	labelAssociatedCreatures = std::make_shared<CLabel>(pos.w / 2 + 8, 122, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[79]);
-
+	auto factionIndex = settings.castle >= CGI->townh->factions.size() ? 0 : settings.castle;
 	std::vector<std::shared_ptr<CComponent>> components;
-	const CTown * town = CGI->townh->factions[settings.castle]->town;
+	const CTown * town = CGI->townh->factions[factionIndex]->town;
+
 	for(auto & elem : town->creatures)
 	{
 		if(!elem.empty())
@@ -382,9 +393,10 @@ void OptionsTab::CPlayerOptionTooltipBox::genHeroWindow()
 	pos = Rect(0, 0, 292, 226);
 	genHeader();
 	labelHeroSpeciality = std::make_shared<CLabel>(pos.w / 2 + 4, 117, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[78]);
+	auto heroIndex = settings.hero >= CGI->heroh->heroes.size() ? 0 : settings.hero;
 
-	imageSpeciality = std::make_shared<CAnimImage>("UN44", CGI->heroh->heroes[settings.hero]->imageIndex, 0, pos.w / 2 - 22, 134);
-	labelSpecialityName = std::make_shared<CLabel>(pos.w / 2, 188, FONT_SMALL, CENTER, Colors::WHITE, CGI->heroh->heroes[settings.hero]->specName);
+	imageSpeciality = std::make_shared<CAnimImage>("UN44", CGI->heroh->heroes[heroIndex]->imageIndex, 0, pos.w / 2 - 22, 134);
+	labelSpecialityName = std::make_shared<CLabel>(pos.w / 2, 188, FONT_SMALL, CENTER, Colors::WHITE, CGI->heroh->heroes[heroIndex]->specName);
 }
 
 void OptionsTab::CPlayerOptionTooltipBox::genBonusWindow()