1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Draft on classes hierarchy.

* patcher and loader uses diferent input (JsonNode and binary stream of zipped map archive), dicided to split them
This commit is contained in:
AlexVinS 2015-08-07 22:33:44 +03:00 committed by AlexVinS
parent 36f4d86844
commit adc271d7b3
4 changed files with 136 additions and 102 deletions

View File

@ -103,5 +103,5 @@ std::unique_ptr<IMapPatcher> CMapService::getMapPatcher(std::string scenarioName
boost::to_lower(scenarioName);
logGlobal->debugStream() << "Request to patch map " << scenarioName;
return std::unique_ptr<IMapPatcher>(new CMapLoaderJson(node[scenarioName]));
return std::unique_ptr<IMapPatcher>(new CMapPatcher(node[scenarioName]));
}

View File

@ -126,7 +126,7 @@ public:
virtual std::unique_ptr<CMapHeader> loadMapHeader() = 0;
};
class DLL_LINKAGE IMapPatcher : public IMapLoader
class DLL_LINKAGE IMapPatcher
{
public:
/**

View File

@ -23,12 +23,88 @@ static const std::string conditionNames[] = {
static const std::string typeNames[] = { "victory", "defeat" };
CMapLoaderJson::CMapLoaderJson(JsonNode stream):
static EventCondition JsonToCondition(const JsonNode & node)
{
EventCondition event;
event.condition = EventCondition::EWinLoseType(vstd::find_pos(conditionNames, node.Vector()[0].String()));
if (node.Vector().size() > 1)
{
const JsonNode & data = node.Vector()[1];
if (data["type"].getType() == JsonNode::DATA_STRING)
event.objectType = VLC->modh->identifiers.getIdentifier(data["type"]).get();
if (data["type"].getType() == JsonNode::DATA_FLOAT)
event.objectType = data["type"].Float();
if (!data["value"].isNull())
event.value = data["value"].Float();
if (!data["position"].isNull())
{
event.position.x = data["position"].Vector()[0].Float();
event.position.y = data["position"].Vector()[1].Float();
event.position.z = data["position"].Vector()[2].Float();
}
}
return event;
}
///CMapFormatJson
void CMapFormatJson::readTriggeredEvents(const JsonNode & input)
{
mapHeader->victoryMessage = input["victoryString"].String();
mapHeader->victoryIconIndex = input["victoryIconIndex"].Float();
mapHeader->defeatMessage = input["defeatString"].String();
mapHeader->defeatIconIndex = input["defeatIconIndex"].Float();
mapHeader->triggeredEvents.clear();
for (auto & entry : input["triggeredEvents"].Struct())
{
TriggeredEvent event;
event.identifier = entry.first;
readTriggeredEvent(event, entry.second);
mapHeader->triggeredEvents.push_back(event);
}
}
void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode & source)
{
event.onFulfill = source["message"].String();
event.description = source["description"].String();
event.effect.type = vstd::find_pos(typeNames, source["effect"]["type"].String());
event.effect.toOtherMessage = source["effect"]["messageToSend"].String();
event.trigger = EventExpression(source["condition"], JsonToCondition); // logical expression
}
///CMapPatcher
CMapPatcher::CMapPatcher(JsonNode stream):
input(stream)
{
}
void CMapPatcher::patchMapHeader(std::unique_ptr<CMapHeader> & header)
{
header.swap(mapHeader);
if (!input.isNull())
readPatchData();
header.swap(mapHeader);
}
void CMapPatcher::readPatchData()
{
readTriggeredEvents(input);
}
///CMapLoaderJson
CMapLoaderJson::CMapLoaderJson(CInputStream * stream):
input(stream)
{
}
std::unique_ptr<CMap> CMapLoaderJson::loadMap()
{
map = new CMap();
@ -85,13 +161,6 @@ JsonNode eventToJson(const EventCondition & cond)
return ret;
}
*/
void CMapLoaderJson::patchMapHeader(std::unique_ptr<CMapHeader> & header)
{
header.swap(mapHeader);
if (!input.isNull())
readPatchData();
header.swap(mapHeader);
}
void CMapLoaderJson::readMap()
{
@ -102,69 +171,11 @@ void CMapLoaderJson::readMap()
void CMapLoaderJson::readHeader()
{
//TODO: read such data like map name & size
readPatchData();
// readTriggeredEvents();
readPlayerInfo();
assert(0); // Not implemented
}
void CMapLoaderJson::readPatchData()
{
mapHeader->victoryMessage = input["victoryString"].String();
mapHeader->victoryIconIndex = input["victoryIconIndex"].Float();
mapHeader->defeatMessage = input["defeatString"].String();
mapHeader->defeatIconIndex = input["defeatIconIndex"].Float();
readTriggeredEvents();
}
void CMapLoaderJson::readTriggeredEvents()
{
mapHeader->triggeredEvents.clear();
for (auto & entry : input["triggeredEvents"].Struct())
{
TriggeredEvent event;
event.identifier = entry.first;
readTriggeredEvent(event, entry.second);
mapHeader->triggeredEvents.push_back(event);
}
}
static EventCondition JsonToCondition(const JsonNode & node)
{
EventCondition event;
event.condition = EventCondition::EWinLoseType(vstd::find_pos(conditionNames, node.Vector()[0].String()));
if (node.Vector().size() > 1)
{
const JsonNode & data = node.Vector()[1];
if (data["type"].getType() == JsonNode::DATA_STRING)
event.objectType = VLC->modh->identifiers.getIdentifier(data["type"]).get();
if (data["type"].getType() == JsonNode::DATA_FLOAT)
event.objectType = data["type"].Float();
if (!data["value"].isNull())
event.value = data["value"].Float();
if (!data["position"].isNull())
{
event.position.x = data["position"].Vector()[0].Float();
event.position.y = data["position"].Vector()[1].Float();
event.position.z = data["position"].Vector()[2].Float();
}
}
return event;
}
void CMapLoaderJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode & source)
{
event.onFulfill = source["message"].String();
event.description = source["description"].String();
event.effect.type = vstd::find_pos(typeNames, source["effect"]["type"].String());
event.effect.toOtherMessage = source["effect"]["messageToSend"].String();
event.trigger = EventExpression(source["condition"], JsonToCondition); // logical expression
}
void CMapLoaderJson::readPlayerInfo()
{
assert(0); // Not implemented

View File

@ -15,8 +15,61 @@
#include "../JsonNode.h"
class TriggeredEvent;
class CInputStream;
class DLL_LINKAGE CMapLoaderJson : public IMapPatcher
class DLL_LINKAGE CMapFormatJson
{
protected:
/** ptr to the map object which gets filled by data from the buffer or written to buffer */
CMap * map;
/**
* ptr to the map header object which gets filled by data from the buffer or written to buffer.
* (when loading map and mapHeader point to the same object)
*/
std::unique_ptr<CMapHeader> mapHeader;
/**
* Reads triggered events, including victory/loss conditions
*/
void readTriggeredEvents(const JsonNode & input);
/**
* Reads one of triggered events
*/
void readTriggeredEvent(TriggeredEvent & event, const JsonNode & source);
};
class DLL_LINKAGE CMapPatcher : public CMapFormatJson, public IMapPatcher
{
public:
/**
* Default constructor.
*
* @param stream. A stream containing the map data.
*/
CMapPatcher(JsonNode stream);
public: //IMapPatcher
/**
* Modifies supplied map header using Json data
*
*/
void patchMapHeader(std::unique_ptr<CMapHeader> & header) override;
private:
/**
* Reads subset of header that can be replaced by patching.
*/
void readPatchData();
const JsonNode input;
};
class DLL_LINKAGE CMapLoaderJson : public CMapFormatJson, public IMapLoader
{
public:
/**
@ -24,7 +77,7 @@ public:
*
* @param stream a stream containing the map data
*/
CMapLoaderJson(JsonNode stream);
CMapLoaderJson(CInputStream * stream);
/**
* Loads the VCMI/Json map file.
@ -40,12 +93,6 @@ public:
*/
std::unique_ptr<CMapHeader> loadMapHeader() override;
/**
* Modifies supplied map header using Json data
*
*/
void patchMapHeader(std::unique_ptr<CMapHeader> & header) override;
private:
/**
* Reads complete map.
@ -57,35 +104,11 @@ private:
*/
void readHeader();
/**
* Reads subset of header that can be replaced by patching.
*/
void readPatchData();
/**
* Reads player information.
*/
void readPlayerInfo();
/**
* Reads triggered events, including victory/loss conditions
*/
void readTriggeredEvents();
/**
* Reads one of triggered events
*/
void readTriggeredEvent(TriggeredEvent & event, const JsonNode & source);
/** ptr to the map object which gets filled by data from the buffer */
CMap * map;
/**
* ptr to the map header object which gets filled by data from the buffer.
* (when loading map and mapHeader point to the same object)
*/
std::unique_ptr<CMapHeader> mapHeader;
const JsonNode input;
CInputStream * input;
};