mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +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:
		| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user