Browse Source

Fixed #1338 and #1341. Introduced post-load step to ease initialization of bonus source ids.

Michał W. Urbańczyk 12 years ago
parent
commit
0eb496fb57
5 changed files with 40 additions and 3 deletions
  1. 15 2
      lib/CArtHandler.cpp
  2. 1 0
      lib/CArtHandler.h
  3. 17 1
      lib/CModHandler.cpp
  4. 4 0
      lib/CModHandler.h
  5. 3 0
      lib/IHandlerBase.h

+ 15 - 2
lib/CArtHandler.cpp

@@ -261,8 +261,7 @@ CArtifact * CArtHandler::loadFromJson(const JsonNode & node)
 	for (auto b : node["bonuses"].Vector())
 	for (auto b : node["bonuses"].Vector())
 	{
 	{
 		auto bonus = JsonUtils::parseBonus (b);
 		auto bonus = JsonUtils::parseBonus (b);
-		bonus->sid = art->id;
-		art->addNewBonus (bonus);
+		art->addNewBonus(bonus);
 	}
 	}
 	return art;
 	return art;
 }
 }
@@ -637,6 +636,20 @@ void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::
 	}
 	}
 }
 }
 
 
+void CArtHandler::afterLoadFinalization()
+{
+	//All artifacts have their id, so we can properly update their bonuses' source ids.
+	for(auto &art : artifacts)
+	{
+		for(auto &bonus : art->getExportedBonusList())
+		{
+			assert(art == artifacts[art->id]);
+			assert(bonus->source == Bonus::ARTIFACT);
+			bonus->sid = art->id;
+		}
+	}
+}
+
 CArtifactInstance::CArtifactInstance()
 CArtifactInstance::CArtifactInstance()
 {
 {
 	init();
 	init();

+ 1 - 0
lib/CArtHandler.h

@@ -235,6 +235,7 @@ public:
 
 
 	void loadObject(std::string scope, std::string name, const JsonNode & data) override;
 	void loadObject(std::string scope, std::string name, const JsonNode & data) override;
 	void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
 	void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
+	void afterLoadFinalization() override;
 
 
 	std::vector<bool> getDefaultAllowed() const override;
 	std::vector<bool> getDefaultAllowed() const override;
 
 

+ 17 - 1
lib/CModHandler.cpp

@@ -264,6 +264,11 @@ void CContentHandler::ContentTypeHandler::loadMod(std::string modName)
 	}
 	}
 }
 }
 
 
+void CContentHandler::ContentTypeHandler::afterLoadFinalization()
+{
+	handler->afterLoadFinalization();
+}
+
 CContentHandler::CContentHandler()
 CContentHandler::CContentHandler()
 {
 {
 	handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(&VLC->heroh->classes, "heroClass")));
 	handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(&VLC->heroh->classes, "heroClass")));
@@ -291,6 +296,14 @@ void CContentHandler::loadMod(std::string modName)
 	}
 	}
 }
 }
 
 
+void CContentHandler::afterLoadFinalization()
+{
+	for(auto & handler : handlers)
+	{
+		handler.second.afterLoadFinalization();
+	}
+}
+
 CModHandler::CModHandler()
 CModHandler::CModHandler()
 {
 {
 	for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
 	for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
@@ -558,8 +571,11 @@ void CModHandler::loadGameContent()
 	VLC->creh->loadCrExpBon();
 	VLC->creh->loadCrExpBon();
 	VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
 	VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
 	identifiers.finalize();
 	identifiers.finalize();
-
 	logGlobal->infoStream() << "\tResolving identifiers: " << timer.getDiff() << " ms";
 	logGlobal->infoStream() << "\tResolving identifiers: " << timer.getDiff() << " ms";
+
+	content.afterLoadFinalization();
+	logGlobal->infoStream() << "\tHandlers post-load finalization: " << timer.getDiff() << " ms";
+
 	logGlobal->infoStream() << "\tAll game content loaded in " << totalTime.getDiff() << " ms";
 	logGlobal->infoStream() << "\tAll game content loaded in " << totalTime.getDiff() << " ms";
 }
 }
 
 

+ 4 - 0
lib/CModHandler.h

@@ -93,6 +93,7 @@ class CContentHandler
 		/// local version of methods in ContentHandler
 		/// local version of methods in ContentHandler
 		void preloadModData(std::string modName, std::vector<std::string> fileList);
 		void preloadModData(std::string modName, std::vector<std::string> fileList);
 		void loadMod(std::string modName);
 		void loadMod(std::string modName);
+		void afterLoadFinalization();
 	};
 	};
 
 
 	std::map<std::string, ContentTypeHandler> handlers;
 	std::map<std::string, ContentTypeHandler> handlers;
@@ -105,6 +106,9 @@ public:
 
 
 	/// actually loads data in mod
 	/// actually loads data in mod
 	void loadMod(std::string modName);
 	void loadMod(std::string modName);
+
+	/// all data was loaded, time for final validation / integration
+	void afterLoadFinalization();
 };
 };
 
 
 typedef std::string TModID;
 typedef std::string TModID;

+ 3 - 0
lib/IHandlerBase.h

@@ -29,6 +29,9 @@ public:
 	virtual void loadObject(std::string scope, std::string name, const JsonNode & data) = 0;
 	virtual void loadObject(std::string scope, std::string name, const JsonNode & data) = 0;
 	virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) = 0;
 	virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) = 0;
 
 
+	/// allows handler to do post-loading step for validation or integration of loaded data
+	virtual void afterLoadFinalization(){};
+
 	/**
 	/**
 	 * Gets a list of objects that are allowed by default on maps
 	 * Gets a list of objects that are allowed by default on maps
 	 *
 	 *