浏览代码

Changes related to #1444 — fixing affected savegames, assert to detect when things go wrong.

Michał W. Urbańczyk 12 年之前
父节点
当前提交
0e5a59e2a5
共有 5 个文件被更改,包括 18 次插入2 次删除
  1. 4 0
      lib/CGameState.cpp
  2. 0 1
      lib/CObjectHandler.cpp
  3. 11 0
      lib/CObjectHandler.h
  4. 1 1
      lib/CTownHandler.h
  5. 2 0
      lib/NetPacksLib.cpp

+ 4 - 0
lib/CGameState.cpp

@@ -1369,6 +1369,10 @@ void CGameState::init(StartInfo * si)
 				}
 			}
 
+		//Early check for #1444-like problems
+		for(auto building : vti->builtBuildings)
+			assert(vti->town->buildings[building]);
+
 		//town events
 		for(CCastleEvent &ev : vti->events)
 		{

+ 0 - 1
lib/CObjectHandler.cpp

@@ -2685,7 +2685,6 @@ void CGTownInstance::battleFinished(const CGHeroInstance *hero, const BattleResu
 	}
 }
 
-
 bool CGVisitableOPH::wasVisited (const CGHeroInstance * h) const
 {
 	return vstd::contains(visitors, h->id);

+ 11 - 0
lib/CObjectHandler.h

@@ -617,6 +617,17 @@ public:
 
 		h & town & townAndVis;
 		BONUS_TREE_DESERIALIZATION_FIX
+
+		vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
+		{
+			if(!town->buildings.count(building) ||  !town->buildings.at(building))
+			{
+				logGlobal->errorStream() << boost::format("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s")
+					% name % pos % building;
+				return true;
+			}
+			return false;
+		});
 	}
 	//////////////////////////////////////////////////////////////////////////
 

+ 1 - 1
lib/CTownHandler.h

@@ -205,7 +205,7 @@ public:
 		//Fix #1444 corrupted save
 		while(auto badElem = vstd::tryFindIf(buildings, findNull))
 		{
-			std::cout << "#1444-like bug encountered, fixing buildings list by removing bogus entry " << badElem->first << " from " << faction->name << std::endl;
+			logGlobal->errorStream() << "#1444-like bug encountered in CTown::serialize, fixing buildings list by removing bogus entry " << badElem->first << " from " << faction->name;
 			buildings.erase(badElem->first);
 		}
 	}

+ 2 - 0
lib/NetPacksLib.cpp

@@ -13,6 +13,7 @@
 #include "CCreatureHandler.h"
 #include "CGameState.h"
 #include "BattleState.h"
+#include "CTownHandler.h"
 
 #undef min
 #undef max
@@ -448,6 +449,7 @@ DLL_LINKAGE void NewStructures::applyGs( CGameState *gs )
 	CGTownInstance *t = gs->getTown(tid);
 	for(const auto & id : bid)
 	{
+		assert(t->town->buildings[id]);
 		t->builtBuildings.insert(id);
 	}
 	t->builded = builded;