1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Small redesign

This commit is contained in:
AlexVinS 2016-02-13 15:22:26 +03:00
parent 22b93106c8
commit 5a92e0de2b
3 changed files with 43 additions and 32 deletions

View File

@ -179,8 +179,9 @@ const int CMapFormatJson::VERSION_MINOR = 0;
const std::string CMapFormatJson::HEADER_FILE_NAME = "header.json"; const std::string CMapFormatJson::HEADER_FILE_NAME = "header.json";
const std::string CMapFormatJson::OBJECTS_FILE_NAME = "objects.json"; const std::string CMapFormatJson::OBJECTS_FILE_NAME = "objects.json";
void CMapFormatJson::readTriggeredEvents(const JsonNode & input) void CMapFormatJson::readTriggeredEvents(JsonDeserializer & handler)
{ {
const JsonNode & input = handler.getCurrent();
mapHeader->victoryMessage = input["victoryString"].String(); mapHeader->victoryMessage = input["victoryString"].String();
mapHeader->victoryIconIndex = input["victoryIconIndex"].Float(); mapHeader->victoryIconIndex = input["victoryIconIndex"].Float();
@ -211,15 +212,15 @@ void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode &
void CMapFormatJson::writeTriggeredEvents(JsonNode& output) void CMapFormatJson::writeTriggeredEvents(JsonNode& output)
{ {
output["victoryString"].String() = map->victoryMessage; output["victoryString"].String() = mapHeader->victoryMessage;
output["victoryIconIndex"].Float() = map->victoryIconIndex; output["victoryIconIndex"].Float() = mapHeader->victoryIconIndex;
output["defeatString"].String() = map->defeatMessage; output["defeatString"].String() = mapHeader->defeatMessage;
output["defeatIconIndex"].Float() = map->defeatIconIndex; output["defeatIconIndex"].Float() = mapHeader->defeatIconIndex;
JsonMap & triggeredEvents = output["triggeredEvents"].Struct(); JsonMap & triggeredEvents = output["triggeredEvents"].Struct();
for(auto event : map->triggeredEvents) for(auto event : mapHeader->triggeredEvents)
writeTriggeredEvent(event, triggeredEvents[event.identifier]); writeTriggeredEvent(event, triggeredEvents[event.identifier]);
} }
@ -246,15 +247,16 @@ CMapPatcher::CMapPatcher(JsonNode stream):
void CMapPatcher::patchMapHeader(std::unique_ptr<CMapHeader> & header) void CMapPatcher::patchMapHeader(std::unique_ptr<CMapHeader> & header)
{ {
header.swap(mapHeader); map = nullptr;
mapHeader = header.get();
if (!input.isNull()) if (!input.isNull())
readPatchData(); readPatchData();
header.swap(mapHeader);
} }
void CMapPatcher::readPatchData() void CMapPatcher::readPatchData()
{ {
readTriggeredEvents(input); JsonDeserializer handler(input);
readTriggeredEvents(handler);
} }
@ -281,18 +283,21 @@ si32 CMapLoaderJson::getIdentifier(const std::string& type, const std::string& n
std::unique_ptr<CMap> CMapLoaderJson::loadMap() std::unique_ptr<CMap> CMapLoaderJson::loadMap()
{ {
LOG_TRACE(logGlobal); LOG_TRACE(logGlobal);
map = new CMap(); std::unique_ptr<CMap> result = std::unique_ptr<CMap>(new CMap());
mapHeader = std::unique_ptr<CMapHeader>(dynamic_cast<CMapHeader *>(map)); map = result.get();
mapHeader = map;
readMap(); readMap();
return std::unique_ptr<CMap>(dynamic_cast<CMap *>(mapHeader.release())); return std::move(result);
} }
std::unique_ptr<CMapHeader> CMapLoaderJson::loadMapHeader() std::unique_ptr<CMapHeader> CMapLoaderJson::loadMapHeader()
{ {
LOG_TRACE(logGlobal); LOG_TRACE(logGlobal);
mapHeader.reset(new CMapHeader); map = nullptr;
std::unique_ptr<CMapHeader> result = std::unique_ptr<CMapHeader>(new CMapHeader());
mapHeader = result.get();
readHeader(); readHeader();
return std::move(mapHeader); return std::move(result);
} }
const JsonNode CMapLoaderJson::getFromArchive(const std::string & archiveFilename) const JsonNode CMapLoaderJson::getFromArchive(const std::string & archiveFilename)
@ -361,7 +366,7 @@ void CMapLoaderJson::readHeader()
// std::vector<bool> allowedHeroes; // std::vector<bool> allowedHeroes;
// std::vector<ui16> placeholdedHeroes; // std::vector<ui16> placeholdedHeroes;
readTriggeredEvents(header); readTriggeredEvents(handler);
readPlayerInfo(handler); readPlayerInfo(handler);
@ -714,6 +719,7 @@ void CMapSaverJson::addToArchive(const JsonNode& data, const std::string& filena
void CMapSaverJson::saveMap(const std::unique_ptr<CMap>& map) void CMapSaverJson::saveMap(const std::unique_ptr<CMap>& map)
{ {
this->map = map.get(); this->map = map.get();
this->mapHeader = this->map;
writeHeader(); writeHeader();
writeTerrain(); writeTerrain();
writeObjects(); writeObjects();
@ -729,25 +735,25 @@ void CMapSaverJson::writeHeader()
//todo: multilevel map save support //todo: multilevel map save support
JsonNode & levels = header["mapLevels"]; JsonNode & levels = header["mapLevels"];
levels["surface"]["height"].Float() = map->height; levels["surface"]["height"].Float() = mapHeader->height;
levels["surface"]["width"].Float() = map->width; levels["surface"]["width"].Float() = mapHeader->width;
levels["surface"]["index"].Float() = 0; levels["surface"]["index"].Float() = 0;
if(map->twoLevel) if(mapHeader->twoLevel)
{ {
levels["underground"]["height"].Float() = map->height; levels["underground"]["height"].Float() = mapHeader->height;
levels["underground"]["width"].Float() = map->width; levels["underground"]["width"].Float() = mapHeader->width;
levels["underground"]["index"].Float() = 1; levels["underground"]["index"].Float() = 1;
} }
header["name"].String() = map->name; header["name"].String() = mapHeader->name;
header["description"].String() = map->description; header["description"].String() = mapHeader->description;
//todo: support arbitrary percentage //todo: support arbitrary percentage
header["difficulty"].String() = HeaderDetail::difficultyForwardMap.at(map->difficulty); header["difficulty"].String() = HeaderDetail::difficultyForwardMap.at(mapHeader->difficulty);
header["heroLevelLimit"].Float() = map->levelLimit; header["heroLevelLimit"].Float() = mapHeader->levelLimit;
writeTriggeredEvents(header); writeTriggeredEvents(header);
@ -768,7 +774,7 @@ void CMapSaverJson::writePlayerInfo(JsonNode & output)
for(int player = 0; player < PlayerColor::PLAYER_LIMIT_I; player++) for(int player = 0; player < PlayerColor::PLAYER_LIMIT_I; player++)
{ {
const PlayerInfo & info = map->players[player]; const PlayerInfo & info = mapHeader->players[player];
if(info.canAnyonePlay()) if(info.canAnyonePlay())
writePlayerInfo(info, dest[GameConstants::PLAYER_COLOR_NAMES[player]]); writePlayerInfo(info, dest[GameConstants::PLAYER_COLOR_NAMES[player]]);
@ -795,14 +801,14 @@ void CMapSaverJson::writeTeams(JsonNode& output)
JsonNode & dest = output["teams"]; JsonNode & dest = output["teams"];
std::vector<std::set<PlayerColor>> teamsData; std::vector<std::set<PlayerColor>> teamsData;
teamsData.resize(map->howManyTeams); teamsData.resize(mapHeader->howManyTeams);
//get raw data //get raw data
for(int idx = 0; idx < map->players.size(); idx++) for(int idx = 0; idx < mapHeader->players.size(); idx++)
{ {
const PlayerInfo & player = map->players.at(idx); const PlayerInfo & player = mapHeader->players.at(idx);
int team = player.team.getNum(); int team = player.team.getNum();
if(vstd::iswithin(team, 0, map->howManyTeams-1) && player.canAnyonePlay()) if(vstd::iswithin(team, 0, mapHeader->howManyTeams-1) && player.canAnyonePlay())
teamsData.at(team).insert(PlayerColor(idx)); teamsData.at(team).insert(PlayerColor(idx));
} }

View File

@ -42,12 +42,12 @@ protected:
* ptr to the map header object which gets filled by data from the buffer. * ptr to the map header object which gets filled by data from the buffer.
* (when loading map and mapHeader point to the same object) * (when loading map and mapHeader point to the same object)
*/ */
std::unique_ptr<CMapHeader> mapHeader; CMapHeader * mapHeader;
/** /**
* Reads triggered events, including victory/loss conditions * Reads triggered events, including victory/loss conditions
*/ */
void readTriggeredEvents(const JsonNode & input); void readTriggeredEvents(JsonDeserializer & handler);
/** /**
* Writes triggered events, including victory/loss conditions * Writes triggered events, including victory/loss conditions
@ -89,7 +89,7 @@ private:
void readPatchData(); void readPatchData();
const JsonNode input; JsonNode input;
}; };
class DLL_LINKAGE CMapLoaderJson : public CMapFormatJson, public IMapLoader class DLL_LINKAGE CMapLoaderJson : public CMapFormatJson, public IMapLoader

View File

@ -47,6 +47,11 @@ public:
return *root; return *root;
}; };
JsonNode & getCurrent()
{
return *current;
};
JsonStructSerializer enterStruct(const std::string & fieldName); JsonStructSerializer enterStruct(const std::string & fieldName);
protected: protected: