فهرست منبع

Simplify library initialization

Ivan Savenko 7 ماه پیش
والد
کامیت
222b73bbcd
8فایلهای تغییر یافته به همراه65 افزوده شده و 83 حذف شده
  1. 5 5
      clientapp/EntryPoint.cpp
  2. 13 27
      lib/GameLibrary.cpp
  3. 31 33
      lib/GameLibrary.h
  4. 1 1
      lib/modding/CModHandler.cpp
  5. 1 1
      lib/modding/CModHandler.h
  6. 8 12
      mapeditor/mainwindow.cpp
  7. 3 2
      serverapp/EntryPoint.cpp
  8. 3 2
      test/CVcmiTestConfig.cpp

+ 5 - 5
clientapp/EntryPoint.cpp

@@ -78,10 +78,11 @@ static CBasicLogConfigurator *logConfig;
 
 static void init()
 {
-	CStopWatch tmh;
 	try
 	{
-		loadDLLClasses();
+		CStopWatch tmh;
+		LIBRARY->initializeLibrary();
+		logGlobal->info("Initializing VCMI_Lib: %d ms", tmh.getDiff());
 	}
 	catch (const DataLoadingException & e)
 	{
@@ -89,8 +90,6 @@ static void init()
 		return;
 	}
 
-	logGlobal->info("Initializing VCMI_Lib: %d ms", tmh.getDiff());
-
 	// Debug code to load all maps on start
 	//ClientCommandManager commandController;
 	//commandController.processCommand("translate maps", false);
@@ -241,7 +240,8 @@ int main(int argc, char * argv[])
 	// Init filesystem and settings
 	try
 	{
-		preinitDLL(false);
+		LIBRARY = new GameLibrary;
+		LIBRARY->initializeFilesystem(false);
 	}
 	catch (const DataLoadingException & e)
 	{

+ 13 - 27
lib/GameLibrary.cpp

@@ -46,20 +46,7 @@ VCMI_LIB_NAMESPACE_BEGIN
 
 GameLibrary * LIBRARY = nullptr;
 
-DLL_LINKAGE void preinitDLL(bool extractArchives)
-{
-	LIBRARY = new GameLibrary();
-	LIBRARY->loadFilesystem(extractArchives);
-	settings.init("config/settings.json", "vcmi:settings");
-	persistentStorage.init("config/persistentStorage.json", "");
-	LIBRARY->loadModFilesystem();
 
-}
-
-DLL_LINKAGE void loadDLLClasses(bool onlyEssential)
-{
-	LIBRARY->init(onlyEssential);
-}
 
 const ArtifactService * GameLibrary::artifacts() const
 {
@@ -160,12 +147,21 @@ void GameLibrary::loadModFilesystem()
 	logGlobal->info("\tMod filesystems: %d ms", loadTime.getDiff());
 }
 
-template <class Handler> void createHandler(std::shared_ptr<Handler> & handler)
+template <class Handler>
+void createHandler(std::unique_ptr<Handler> & handler)
+{
+	handler = std::make_unique<Handler>();
+}
+
+void GameLibrary::initializeFilesystem(bool extractArchives)
 {
-	handler = std::make_shared<Handler>();
+	loadFilesystem(extractArchives);
+	settings.init("config/settings.json", "vcmi:settings");
+	persistentStorage.init("config/persistentStorage.json", "");
+	loadModFilesystem();
 }
 
-void GameLibrary::init(bool onlyEssential)
+void GameLibrary::initializeLibrary()
 {
 	createHandler(settingsHandler);
 	modh->initializeConfig();
@@ -194,7 +190,7 @@ void GameLibrary::init(bool onlyEssential)
 	createHandler(obstacleHandler);
 
 	modh->load();
-	modh->afterLoad(onlyEssential);
+	modh->afterLoad();
 }
 
 #if SCRIPTING_ENABLED
@@ -207,14 +203,4 @@ void GameLibrary::scriptsLoaded()
 GameLibrary::GameLibrary() = default;
 GameLibrary::~GameLibrary() = default;
 
-std::shared_ptr<CContentHandler> GameLibrary::getContent() const
-{
-	return modh->content;
-}
-
-void GameLibrary::setContent(std::shared_ptr<CContentHandler> content)
-{
-	modh->content = std::move(content);
-}
-
 VCMI_LIB_NAMESPACE_END

+ 31 - 33
lib/GameLibrary.h

@@ -51,10 +51,6 @@ namespace scripting
 /// Loads and constructs several handlers
 class DLL_LINKAGE GameLibrary final : public Services
 {
-
-	std::shared_ptr<CContentHandler> getContent() const;
-	void setContent(std::shared_ptr<CContentHandler> content);
-
 public:
 	const ArtifactService * artifacts() const override;
 	const CreatureService * creatures() const override;
@@ -76,38 +72,44 @@ public:
 	const IBonusTypeHandler * getBth() const; //deprecated
 	const CIdentifierStorage * identifiers() const;
 
-	std::shared_ptr<CArtHandler> arth;
-	std::shared_ptr<CBonusTypeHandler> bth;
-	std::shared_ptr<CHeroHandler> heroh;
-	std::shared_ptr<CHeroClassHandler> heroclassesh;
-	std::shared_ptr<CCreatureHandler> creh;
-	std::shared_ptr<CSpellHandler> spellh;
-	std::shared_ptr<CSkillHandler> skillh;
+	std::unique_ptr<CArtHandler> arth;
+	std::unique_ptr<CBonusTypeHandler> bth;
+	std::unique_ptr<CHeroHandler> heroh;
+	std::unique_ptr<CHeroClassHandler> heroclassesh;
+	std::unique_ptr<CCreatureHandler> creh;
+	std::unique_ptr<CSpellHandler> spellh;
+	std::unique_ptr<CSkillHandler> skillh;
 	// TODO: Remove ObjectHandler altogether?
-	std::shared_ptr<CObjectHandler> objh;
-	std::shared_ptr<CObjectClassesHandler> objtypeh;
-	std::shared_ptr<CTownHandler> townh;
-	std::shared_ptr<CGeneralTextHandler> generaltexth;
-	std::shared_ptr<CModHandler> modh;
-	std::shared_ptr<TerrainTypeHandler> terrainTypeHandler;
-	std::shared_ptr<RoadTypeHandler> roadTypeHandler;
-	std::shared_ptr<RiverTypeHandler> riverTypeHandler;
-	std::shared_ptr<CIdentifierStorage> identifiersHandler;
-	std::shared_ptr<CTerrainViewPatternConfig> terviewh;
-	std::shared_ptr<CRmgTemplateStorage> tplh;
-	std::shared_ptr<BattleFieldHandler> battlefieldsHandler;
-	std::shared_ptr<ObstacleHandler> obstacleHandler;
-	std::shared_ptr<GameSettings> settingsHandler;
-	std::shared_ptr<ObstacleSetHandler> biomeHandler;
+	std::unique_ptr<CObjectHandler> objh;
+	std::unique_ptr<CObjectClassesHandler> objtypeh;
+	std::unique_ptr<CTownHandler> townh;
+	std::unique_ptr<CGeneralTextHandler> generaltexth;
+	std::unique_ptr<CModHandler> modh;
+	std::unique_ptr<TerrainTypeHandler> terrainTypeHandler;
+	std::unique_ptr<RoadTypeHandler> roadTypeHandler;
+	std::unique_ptr<RiverTypeHandler> riverTypeHandler;
+	std::unique_ptr<CIdentifierStorage> identifiersHandler;
+	std::unique_ptr<CTerrainViewPatternConfig> terviewh;
+	std::unique_ptr<CRmgTemplateStorage> tplh;
+	std::unique_ptr<BattleFieldHandler> battlefieldsHandler;
+	std::unique_ptr<ObstacleHandler> obstacleHandler;
+	std::unique_ptr<GameSettings> settingsHandler;
+	std::unique_ptr<ObstacleSetHandler> biomeHandler;
 
 #if SCRIPTING_ENABLED
-	std::shared_ptr<scripting::ScriptHandler> scriptHandler;
+	std::unique_ptr<scripting::ScriptHandler> scriptHandler;
 #endif
 
-	GameLibrary(); //c-tor, loads .lods and NULLs handlers
+	GameLibrary();
 	~GameLibrary();
-	void init(bool onlyEssential); //uses standard config file
 
+	/// initializes settings and filesystem
+	void initializeFilesystem(bool extractArchives);
+
+	/// Loads all game entities
+	void initializeLibrary();
+
+private:
 	// basic initialization. should be called before init(). Can also extract original H3 archives
 	void loadFilesystem(bool extractArchives);
 	void loadModFilesystem();
@@ -119,8 +121,4 @@ public:
 
 extern DLL_LINKAGE GameLibrary * LIBRARY;
 
-DLL_LINKAGE void preinitDLL(bool extractArchives);
-DLL_LINKAGE void loadDLLClasses(bool onlyEssential = false);
-
-
 VCMI_LIB_NAMESPACE_END

+ 1 - 1
lib/modding/CModHandler.cpp

@@ -309,7 +309,7 @@ void CModHandler::load()
 	logMod->info("\tAll game content loaded");
 }
 
-void CModHandler::afterLoad(bool onlyEssential)
+void CModHandler::afterLoad()
 {
 	JsonNode modSettings;
 	for (const auto & modEntry : getActiveMods())

+ 1 - 1
lib/modding/CModHandler.h

@@ -62,7 +62,7 @@ public:
 
 	/// load content from all available mods
 	void load();
-	void afterLoad(bool onlyEssential);
+	void afterLoad();
 
 	CModHandler();
 	~CModHandler();

+ 8 - 12
mapeditor/mainwindow.cpp

@@ -69,16 +69,6 @@ QPixmap pixmapFromJson(const QJsonValue &val)
   return p;
 }
 
-void init()
-{
-	loadDLLClasses();
-
-	Settings config = settings.write["session"]["editor"];
-	config->Bool() = true;
-
-	logGlobal->info("Initializing VCMI_Lib");
-}
-
 void MainWindow::loadUserSettings()
 {
 	//load window settings
@@ -190,7 +180,8 @@ MainWindow::MainWindow(QWidget* parent) :
 	logGlobal->info("The log file will be saved to %s", logPath);
 
 	//init
-	preinitDLL(extractionOptions.extractArchives);
+	LIBRARY = new GameLibrary();
+	LIBRARY->initializeFilesystem(extractionOptions.extractArchives);
 
 	// Initialize logging based on settings
 	logConfig->configure();
@@ -250,7 +241,12 @@ MainWindow::MainWindow(QWidget* parent) :
 	loadUserSettings(); //For example window size
 	setTitle();
 
-	init();
+	LIBRARY->initializeLibrary();
+
+	Settings config = settings.write["session"]["editor"];
+	config->Bool() = true;
+
+	logGlobal->info("Initializing VCMI_Lib");
 
 	graphics = new Graphics(); // should be before curh->init()
 	graphics->load();//must be after Content loading but should be in main thread

+ 3 - 2
serverapp/EntryPoint.cpp

@@ -78,10 +78,11 @@ int main(int argc, const char * argv[])
 
 	boost::program_options::variables_map opts;
 	handleCommandOptions(argc, argv, opts);
-	preinitDLL(false);
+	LIBRARY = new GameLibrary;
+	LIBRARY->initializeFilesystem(false);
 	logConfigurator.configure();
 
-	loadDLLClasses();
+	LIBRARY->initializeLibrary();
 	std::srand(static_cast<uint32_t>(time(nullptr)));
 
 	{

+ 3 - 2
test/CVcmiTestConfig.cpp

@@ -22,8 +22,9 @@
 
 void CVcmiTestConfig::SetUp()
 {
-	preinitDLL(true);
-	loadDLLClasses(true);
+	LIBRARY = new GameLibrary;
+	LIBRARY->initializeFilesystem(false);
+	LIBRARY->initializeLibrary();
 
 	/* TEST_DATA_DIR may be wrong, if yes below test don't run,
 	find your test data folder in your build and change TEST_DATA_DIR for it*/