瀏覽代碼

Pass loaded game over network too

nordsoft 3 年之前
父節點
當前提交
8a8716ce8a
共有 3 個文件被更改,包括 41 次插入32 次删除
  1. 1 1
      client/CServerHandler.cpp
  2. 39 30
      client/Client.cpp
  3. 1 1
      client/Client.h

+ 1 - 1
client/CServerHandler.cpp

@@ -541,7 +541,7 @@ void CServerHandler::startGameplay(CGameState * gameState)
 		client->newGame(gameState);
 		client->newGame(gameState);
 		break;
 		break;
 	case StartInfo::LOAD_GAME:
 	case StartInfo::LOAD_GAME:
-		client->loadGame();
+		client->loadGame(gameState);
 		break;
 		break;
 	default:
 	default:
 		throw std::runtime_error("Invalid mode");
 		throw std::runtime_error("Invalid mode");

+ 39 - 30
client/Client.cpp

@@ -197,54 +197,63 @@ void CClient::newGame(CGameState * gameState)
 	initPlayerInterfaces();
 	initPlayerInterfaces();
 }
 }
 
 
-void CClient::loadGame()
+void CClient::loadGame(CGameState * gameState)
 {
 {
 	logNetwork->info("Loading procedure started!");
 	logNetwork->info("Loading procedure started!");
 
 
-	std::unique_ptr<CLoadFile> loader;
-	try
+	if(gameState)
 	{
 	{
-		boost::filesystem::path clientSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(CSH->si->mapname, EResType::CLIENT_SAVEGAME));
-		boost::filesystem::path controlServerSaveName;
-
-		if(CResourceHandler::get("local")->existsResource(ResourceID(CSH->si->mapname, EResType::SERVER_SAVEGAME)))
-		{
-			controlServerSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(CSH->si->mapname, EResType::SERVER_SAVEGAME));
-		}
-		else // create entry for server savegame. Triggered if save was made after launch and not yet present in res handler
+		logNetwork->info("Game state was transferred over network, loading.");
+		gs = gameState;
+	}
+	else
+	{
+		std::unique_ptr<CLoadFile> loader;
+		try
 		{
 		{
-			controlServerSaveName = boost::filesystem::path(clientSaveName).replace_extension(".vsgm1");
-			CResourceHandler::get("local")->createResource(controlServerSaveName.string(), true);
-		}
+			boost::filesystem::path clientSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(CSH->si->mapname, EResType::CLIENT_SAVEGAME));
+			boost::filesystem::path controlServerSaveName;
+
+			if(CResourceHandler::get("local")->existsResource(ResourceID(CSH->si->mapname, EResType::SERVER_SAVEGAME)))
+			{
+				controlServerSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(CSH->si->mapname, EResType::SERVER_SAVEGAME));
+			}
+			else // create entry for server savegame. Triggered if save was made after launch and not yet present in res handler
+			{
+				controlServerSaveName = boost::filesystem::path(clientSaveName).replace_extension(".vsgm1");
+				CResourceHandler::get("local")->createResource(controlServerSaveName.string(), true);
+			}
 
 
-		if(clientSaveName.empty())
-			throw std::runtime_error("Cannot open client part of " + CSH->si->mapname);
-		if(controlServerSaveName.empty() || !boost::filesystem::exists(controlServerSaveName))
-			throw std::runtime_error("Cannot open server part of " + CSH->si->mapname);
+			if(clientSaveName.empty())
+				throw std::runtime_error("Cannot open client part of " + CSH->si->mapname);
+			if(controlServerSaveName.empty() || !boost::filesystem::exists(controlServerSaveName))
+				throw std::runtime_error("Cannot open server part of " + CSH->si->mapname);
 
 
+			{
+				CLoadIntegrityValidator checkingLoader(clientSaveName, controlServerSaveName, MINIMAL_SERIALIZATION_VERSION);
+				loadCommonState(checkingLoader);
+				loader = checkingLoader.decay();
+			}
+			
+			serialize(loader->serializer, loader->serializer.fileVersion);
+		}
+		catch(std::exception & e)
 		{
 		{
-			CLoadIntegrityValidator checkingLoader(clientSaveName, controlServerSaveName, MINIMAL_SERIALIZATION_VERSION);
-			loadCommonState(checkingLoader);
-			loader = checkingLoader.decay();
+			logGlobal->error("Cannot load game %s. Error: %s", CSH->si->mapname, e.what());
+			throw; //obviously we cannot continue here
 		}
 		}
-
+		logNetwork->trace("Loaded common part of save %d ms", CSH->th->getDiff());
 	}
 	}
-	catch(std::exception & e)
-	{
-		logGlobal->error("Cannot load game %s. Error: %s", CSH->si->mapname, e.what());
-		throw; //obviously we cannot continue here
-	}
-	logNetwork->trace("Loaded common part of save %d ms", CSH->th->getDiff());
 	gs->preInit(VLC);
 	gs->preInit(VLC);
 	gs->updateOnLoad(CSH->si.get());
 	gs->updateOnLoad(CSH->si.get());
+	logNetwork->info("Game loaded, initialize interfaces.");
+	
 	initMapHandler();
 	initMapHandler();
 
 
 	reinitScripting();
 	reinitScripting();
 
 
 	initPlayerEnvironments();
 	initPlayerEnvironments();
 
 
-	serialize(loader->serializer, loader->serializer.fileVersion);
-
 	initPlayerInterfaces();
 	initPlayerInterfaces();
 }
 }
 
 

+ 1 - 1
client/Client.h

@@ -151,7 +151,7 @@ public:
 	events::EventBus * eventBus() const override;
 	events::EventBus * eventBus() const override;
 
 
 	void newGame(CGameState * gameState);
 	void newGame(CGameState * gameState);
-	void loadGame();
+	void loadGame(CGameState * gameState);
 	void serialize(BinarySerializer & h, const int version);
 	void serialize(BinarySerializer & h, const int version);
 	void serialize(BinaryDeserializer & h, const int version);
 	void serialize(BinaryDeserializer & h, const int version);