1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-06 09:09:40 +02:00

Merge pull request #308 from vcmi/mapServiceTweaks

Map service tweaks
This commit is contained in:
Alexander Shishkin
2017-06-05 18:30:52 +03:00
committed by GitHub
14 changed files with 35 additions and 39 deletions

View File

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

View File

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

View File

@@ -16,7 +16,6 @@
#include "StartInfo.h" #include "StartInfo.h"
#include "NetPacks.h" #include "NetPacks.h"
#include "registerTypes/RegisterTypes.h" #include "registerTypes/RegisterTypes.h"
#include "mapping/CMapInfo.h"
#include "BattleInfo.h" #include "BattleInfo.h"
#include "JsonNode.h" #include "JsonNode.h"
#include "filesystem/Filesystem.h" #include "filesystem/Filesystem.h"
@@ -840,7 +839,8 @@ void CGameState::initNewGame(bool allowSavingRandomMap)
else else
{ {
logGlobal->infoStream() << "Open map file: " << scenarioOps->mapname; 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();
} }
} }

View File

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

View File

@@ -13,24 +13,16 @@
#include "MapFormatJson.h" #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); auto stream = getStreamFromFS(name);
std::unique_ptr<CMap> map(getMapLoader(stream)->loadMap()); return getMapLoader(stream)->loadMap();
std::unique_ptr<CMapHeader> header(map.get());
getMapPatcher(name)->patchMapHeader(header);
header.release();
return map;
} }
std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const std::string & name) std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ResourceID & name)
{ {
auto stream = getStreamFromFS(name); auto stream = getStreamFromFS(name);
std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader(); return getMapLoader(stream)->loadMapHeader();
getMapPatcher(name)->patchMapHeader(header);
return header;
} }
std::unique_ptr<CMap> CMapService::loadMap(const ui8 * buffer, int size, const std::string & name) 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<CMap> map(getMapLoader(stream)->loadMap());
std::unique_ptr<CMapHeader> header(map.get()); std::unique_ptr<CMapHeader> header(map.get());
//might be original campaign and require patch
getMapPatcher(name)->patchMapHeader(header); getMapPatcher(name)->patchMapHeader(header);
header.release(); header.release();
@@ -49,6 +42,8 @@ std::unique_ptr<CMapHeader> CMapService::loadMapHeader(const ui8 * buffer, int s
{ {
auto stream = getStreamFromMem(buffer, size); auto stream = getStreamFromMem(buffer, size);
std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader(); std::unique_ptr<CMapHeader> header = getMapLoader(stream)->loadMapHeader();
//might be original campaign and require patch
getMapPatcher(name)->patchMapHeader(header); getMapPatcher(name)->patchMapHeader(header);
return 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) std::unique_ptr<CInputStream> CMapService::getStreamFromMem(const ui8 * buffer, int size)

View File

@@ -11,6 +11,8 @@
#pragma once #pragma once
class ResourceID;
class CMap; class CMap;
class CMapHeader; class CMapHeader;
class CInputStream; class CInputStream;
@@ -31,7 +33,7 @@ public:
* @param name the name of the map * @param name the name of the map
* @return a unique ptr to the loaded map class * @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. * Loads the VCMI/H3 map header specified by the name.
@@ -39,7 +41,7 @@ public:
* @param name the name of the map * @param name the name of the map
* @return a unique ptr to the loaded map header class * @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 * 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 * @param name the name of the map
* @return a unique ptr to the input stream class * @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. * Gets a map input stream from a buffer.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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