Browse Source

Merge pull request #308 from vcmi/mapServiceTweaks

Map service tweaks
Alexander Shishkin 8 years ago
parent
commit
8c9d6c1c1d

+ 11 - 8
client/CPreGame.cpp

@@ -131,10 +131,10 @@ void setPlayer(PlayerSettings &pset, ui8 player, const std::map<ui8, std::string
 		playerColor = pset.color;
 }
 
-void updateStartInfo(std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader, const std::map<ui8, std::string> &playerNames)
+void updateStartInfo(std::string filename, StartInfo & sInfo, const std::unique_ptr<CMapHeader> & mapHeader, const std::map<ui8, std::string> &playerNames)
 {
 	sInfo.playerInfos.clear();
-	if(!mapHeader)
+	if(!mapHeader.get())
 	{
 		return;
 	}
@@ -799,7 +799,12 @@ void CSelectionScreen::changeSelection(const CMapInfo * to)
 	   SEL->sInfo.difficulty = to->scenarioOpts->difficulty;
 	if(screenType != CMenuScreen::campaignList)
 	{
-		updateStartInfo(to ? to->fileURI : "", sInfo, to ? to->mapHeader.get() : nullptr);
+		std::unique_ptr<CMapHeader> emptyHeader;
+        if(to)
+			updateStartInfo(to->fileURI, sInfo, to->mapHeader);
+		else
+			updateStartInfo("", sInfo, emptyHeader);
+
 		if(screenType == CMenuScreen::newGame)
 		{
 			if(to && to->isRandomMap)
@@ -3219,7 +3224,7 @@ void CHotSeatPlayers::enterSelectionScreen()
 void CBonusSelection::init()
 {
 	highlightedRegion = nullptr;
-	ourHeader = nullptr;
+	ourHeader.reset();
 	diffLb = nullptr;
 	diffRb = nullptr;
 	bonuses = nullptr;
@@ -3342,7 +3347,6 @@ CBonusSelection::CBonusSelection(const std::string & campaignFName)
 CBonusSelection::~CBonusSelection()
 {
 	SDL_FreeSurface(background);
-	delete ourHeader;
 	sFlags->unload();
 }
 
@@ -3423,10 +3427,9 @@ void CBonusSelection::selectMap(int mapNr, bool initialSelect)
 		scenarioName += ':' + boost::lexical_cast<std::string>(selectedMap);
 
 		//get header
-		delete ourHeader;
 		std::string & headerStr = ourCampaign->camp->mapPieces.find(mapNr)->second;
 		auto buffer = reinterpret_cast<const ui8 *>(headerStr.data());
-		ourHeader = CMapService::loadMapHeader(buffer, headerStr.size(), scenarioName).release();
+		ourHeader = CMapService::loadMapHeader(buffer, headerStr.size(), scenarioName);
 
 		std::map<ui8, std::string> names;
 		names[1] = settings["general"]["playerName"].String();
@@ -3922,7 +3925,7 @@ ISelectionScreenInfo::~ISelectionScreenInfo()
 	SEL = nullptr;
 }
 
-void ISelectionScreenInfo::updateStartInfo(std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader)
+void ISelectionScreenInfo::updateStartInfo(std::string filename, StartInfo & sInfo, const std::unique_ptr<CMapHeader> & mapHeader)
 {
 	::updateStartInfo(filename, sInfo, mapHeader, playerNames);
 }

+ 2 - 2
client/CPreGame.h

@@ -341,7 +341,7 @@ public:
 	virtual void postChatMessage(const std::string &txt){};
 
 	void setPlayer(PlayerSettings &pset, ui8 player);
-	void updateStartInfo( std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader );
+	void updateStartInfo(std::string filename, StartInfo & sInfo, const std::unique_ptr<CMapHeader> & mapHeader);
 
 	ui8 getIdOfFirstUnallocatedPlayer(); //returns 0 if none
 	bool isGuest() const;
@@ -539,7 +539,7 @@ private:
 	int selectedMap;
 	boost::optional<int> selectedBonus;
 	StartInfo startInfo;
-	CMapHeader * ourHeader;
+	std::unique_ptr<CMapHeader> ourHeader;
 };
 
 /// Campaign selection screen

+ 2 - 2
lib/CGameState.cpp

@@ -16,7 +16,6 @@
 #include "StartInfo.h"
 #include "NetPacks.h"
 #include "registerTypes/RegisterTypes.h"
-#include "mapping/CMapInfo.h"
 #include "BattleInfo.h"
 #include "JsonNode.h"
 #include "filesystem/Filesystem.h"
@@ -840,7 +839,8 @@ void CGameState::initNewGame(bool allowSavingRandomMap)
 	else
 	{
 		logGlobal->infoStream() << "Open map file: " << scenarioOps->mapname;
-		map = CMapService::loadMap(scenarioOps->mapname).release();
+		const ResourceID mapURI(scenarioOps->mapname, EResType::MAP);
+		map = CMapService::loadMap(mapURI).release();
 	}
 }
 

+ 3 - 1
lib/mapping/CMapInfo.cpp

@@ -1,6 +1,7 @@
 #include "StdInc.h"
 #include "CMapInfo.h"
 
+#include "../filesystem/ResourceID.h"
 #include "../StartInfo.h"
 #include "../GameConstants.h"
 #include "CMapService.h"
@@ -58,7 +59,7 @@ CMapInfo::~CMapInfo()
 void CMapInfo::mapInit(const std::string & fname)
 {
 	fileURI = fname;
-	mapHeader = CMapService::loadMapHeader(fname);
+	mapHeader = CMapService::loadMapHeader(ResourceID(fname, EResType::MAP));
 	countPlayers();
 }
 
@@ -81,3 +82,4 @@ CMapInfo & CMapInfo::operator=(CMapInfo &&tmp)
 	return *this;
 }
 
+#undef STEAL

+ 9 - 14
lib/mapping/CMapService.cpp

@@ -13,24 +13,16 @@
 #include "MapFormatJson.h"
 
 
-std::unique_ptr<CMap> CMapService::loadMap(const std::string & name)
+std::unique_ptr<CMap> CMapService::loadMap(const ResourceID & name)
 {
 	auto stream = getStreamFromFS(name);
-	std::unique_ptr<CMap> map(getMapLoader(stream)->loadMap());
-	std::unique_ptr<CMapHeader> header(map.get());
-
-	getMapPatcher(name)->patchMapHeader(header);
-	header.release();
-
-	return map;
+	return getMapLoader(stream)->loadMap();
 }
 
-std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const std::string & name)
+std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ResourceID & name)
 {
 	auto stream = getStreamFromFS(name);
-	std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader();
-	getMapPatcher(name)->patchMapHeader(header);
-	return header;
+	return getMapLoader(stream)->loadMapHeader();
 }
 
 std::unique_ptr<CMap> CMapService::loadMap(const ui8 * buffer, int size, const std::string & name)
@@ -39,6 +31,7 @@ std::unique_ptr<CMap> CMapService::loadMap(const ui8 * buffer, int size, const s
 	std::unique_ptr<CMap> map(getMapLoader(stream)->loadMap());
 	std::unique_ptr<CMapHeader> header(map.get());
 
+	//might be original campaign and require patch
 	getMapPatcher(name)->patchMapHeader(header);
 	header.release();
 
@@ -49,6 +42,8 @@ std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ui8 * buffer, int s
 {
 	auto stream = getStreamFromMem(buffer, size);
 	std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader();
+
+	//might be original campaign and require patch
 	getMapPatcher(name)->patchMapHeader(header);
 	return header;
 }
@@ -70,9 +65,9 @@ void CMapService::saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::
 	}
 }
 
-std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const std::string & name)
+std::unique_ptr<CInputStream> CMapService::getStreamFromFS(const ResourceID & name)
 {
-	return CResourceHandler::get()->load(ResourceID(name, EResType::MAP));
+	return CResourceHandler::get()->load(name);
 }
 
 std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const ui8 * buffer, int size)

+ 5 - 3
lib/mapping/CMapService.h

@@ -11,6 +11,8 @@
 
 #pragma once
 
+class ResourceID;
+
 class CMap;
 class CMapHeader;
 class CInputStream;
@@ -31,7 +33,7 @@ public:
 	 * @param name the name of the map
 	 * @return a unique ptr to the loaded map class
 	 */
-	static std::unique_ptr<CMap> loadMap(const std::string & name);
+	static std::unique_ptr<CMap> loadMap(const ResourceID & name);
 
 	/**
 	 * Loads the VCMI/H3 map header specified by the name.
@@ -39,7 +41,7 @@ public:
 	 * @param name the name of the map
 	 * @return a unique ptr to the loaded map header class
 	 */
-	static std::unique_ptr<CMapHeader> loadMapHeader(const std::string & name);
+	static std::unique_ptr<CMapHeader> loadMapHeader(const ResourceID & name);
 
 	/**
 	 * Loads the VCMI/H3 map file from a buffer. This method is temporarily
@@ -77,7 +79,7 @@ private:
 	 * @param name the name of the map
 	 * @return a unique ptr to the input stream class
 	 */
-	static std::unique_ptr<CInputStream> getStreamFromFS(const std::string & name);
+	static std::unique_ptr<CInputStream> getStreamFromFS(const ResourceID & name);
 
 	/**
 	 * Gets a map input stream from a buffer.

+ 0 - 1
lib/registerTypes/TypesClientPacks1.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 #include "RegisterTypes.h"
 
-#include "../mapping/CMapInfo.h"
 #include "../StartInfo.h"
 #include "../CGameState.h"
 #include "../mapping/CMap.h"

+ 0 - 1
lib/registerTypes/TypesClientPacks2.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 #include "RegisterTypes.h"
 
-#include "../mapping/CMapInfo.h"
 #include "../StartInfo.h"
 #include "../CStack.h"
 #include "../BattleInfo.h"

+ 0 - 1
lib/registerTypes/TypesMapObjects1.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 #include "RegisterTypes.h"
 
-#include "../mapping/CMapInfo.h"
 #include "../StartInfo.h"
 #include "../CGameState.h"
 #include "../mapping/CMap.h"

+ 0 - 1
lib/registerTypes/TypesMapObjects2.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 #include "RegisterTypes.h"
 
-#include "../mapping/CMapInfo.h"
 #include "../StartInfo.h"
 #include "../CStack.h"
 #include "../BattleInfo.h"

+ 0 - 1
lib/registerTypes/TypesMapObjects3.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 #include "RegisterTypes.h"
 
-#include "../mapping/CMapInfo.h"
 #include "../StartInfo.h"
 #include "../CGameState.h"
 #include "../mapping/CMap.h"

+ 0 - 1
lib/registerTypes/TypesServerPacks.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 #include "RegisterTypes.h"
 
-#include "../mapping/CMapInfo.h"
 #include "../StartInfo.h"
 #include "../CGameState.h"
 #include "../mapping/CMap.h"

+ 0 - 1
server/CVCMIServer.h

@@ -74,7 +74,6 @@ public:
 	std::list<CPackForSelectionScreen*> toAnnounce;
 	boost::recursive_mutex mx;
 
-	//std::vector<CMapInfo> maps;
 	TAcceptor *acceptor;
 	TSocket *upcomingConnection;
 

+ 3 - 2
test/CMapEditManagerTest.cpp

@@ -115,9 +115,10 @@ BOOST_AUTO_TEST_CASE(CMapEditManager_DrawTerrain_View)
 	logGlobal->info("CMapEditManager_DrawTerrain_View start");
 	try
 	{
+		const ResourceID testMap("test/TerrainViewTest", EResType::MAP);
 		// Load maps and json config
-		const auto originalMap = CMapService::loadMap("test/TerrainViewTest");
-		auto map = CMapService::loadMap("test/TerrainViewTest");
+		const auto originalMap = CMapService::loadMap(testMap);
+		auto map = CMapService::loadMap(testMap);
 		logGlobal->info("Loaded test map successfully.");
 
 		// Validate edit manager