mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-13 19:54:17 +02:00
MetaString can be serialized in json
This commit is contained in:
@@ -300,4 +300,84 @@ void MetaString::replaceCreatureName(const CStackBasicDescriptor & stack)
|
|||||||
replaceCreatureName(stack.type->getId(), stack.count);
|
replaceCreatureName(stack.type->getId(), stack.count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MetaString::jsonSerialize(JsonNode & dest) const
|
||||||
|
{
|
||||||
|
JsonNode jsonMessage;
|
||||||
|
JsonNode jsonLocalStrings;
|
||||||
|
JsonNode jsonExactStrings;
|
||||||
|
JsonNode jsonStringsTextID;
|
||||||
|
JsonNode jsonNumbers;
|
||||||
|
|
||||||
|
for (const auto & entry : message )
|
||||||
|
{
|
||||||
|
JsonNode value;
|
||||||
|
value.Float() = static_cast<int>(entry);
|
||||||
|
jsonMessage.Vector().push_back(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto & entry : localStrings )
|
||||||
|
{
|
||||||
|
JsonNode value;
|
||||||
|
value.Float() = static_cast<int>(entry.first) * 10000 + entry.second;
|
||||||
|
jsonLocalStrings.Vector().push_back(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto & entry : exactStrings )
|
||||||
|
{
|
||||||
|
JsonNode value;
|
||||||
|
value.String() = entry;
|
||||||
|
jsonExactStrings.Vector().push_back(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto & entry : stringsTextID )
|
||||||
|
{
|
||||||
|
JsonNode value;
|
||||||
|
value.String() = entry;
|
||||||
|
jsonStringsTextID.Vector().push_back(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto & entry : numbers )
|
||||||
|
{
|
||||||
|
JsonNode value;
|
||||||
|
value.Float() = entry;
|
||||||
|
jsonNumbers.Vector().push_back(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
dest["message"] = jsonMessage;
|
||||||
|
dest["localStrings"] = jsonLocalStrings;
|
||||||
|
dest["exactStrings"] = jsonExactStrings;
|
||||||
|
dest["stringsTextID"] = jsonStringsTextID;
|
||||||
|
dest["numbers"] = jsonNumbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MetaString::jsonDeserialize(const JsonNode & source)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
|
||||||
|
if (source.isString())
|
||||||
|
{
|
||||||
|
// compatibility with fields that were converted from string to MetaString
|
||||||
|
if(boost::starts_with(source.String(), "core.") || boost::starts_with(source.String(), "vcmi."))
|
||||||
|
appendTextID(source.String());
|
||||||
|
else
|
||||||
|
appendRawString(source.String());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto & entry : source["message"].Vector() )
|
||||||
|
message.push_back(static_cast<EMessage>(entry.Integer()));
|
||||||
|
|
||||||
|
for (const auto & entry : source["localStrings"].Vector() )
|
||||||
|
localStrings.push_back({ static_cast<EMetaText>(entry.Integer() / 10000), entry.Integer() % 10000 });
|
||||||
|
|
||||||
|
for (const auto & entry : source["exactStrings"].Vector() )
|
||||||
|
exactStrings.push_back(entry.String());
|
||||||
|
|
||||||
|
for (const auto & entry : source["stringsTextID"].Vector() )
|
||||||
|
stringsTextID.push_back(entry.String());
|
||||||
|
|
||||||
|
for (const auto & entry : source["numbers"].Vector() )
|
||||||
|
numbers.push_back(entry.Integer());
|
||||||
|
}
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_END
|
VCMI_LIB_NAMESPACE_END
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
class JsonNode;
|
||||||
class CreatureID;
|
class CreatureID;
|
||||||
class CStackBasicDescriptor;
|
class CStackBasicDescriptor;
|
||||||
using TQuantity = si32;
|
using TQuantity = si32;
|
||||||
@@ -108,6 +109,9 @@ public:
|
|||||||
/// Returns true if current string is empty
|
/// Returns true if current string is empty
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
|
|
||||||
|
void jsonSerialize(JsonNode & dest) const;
|
||||||
|
void jsonDeserialize(const JsonNode & dest);
|
||||||
|
|
||||||
template <typename Handler> void serialize(Handler & h, const int version)
|
template <typename Handler> void serialize(Handler & h, const int version)
|
||||||
{
|
{
|
||||||
h & exactStrings;
|
h & exactStrings;
|
||||||
|
@@ -683,10 +683,10 @@ void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode &
|
|||||||
{
|
{
|
||||||
using namespace TriggeredEventsDetail;
|
using namespace TriggeredEventsDetail;
|
||||||
|
|
||||||
// event.onFulfill = source["message"].String();
|
event.onFulfill.jsonDeserialize(source["message"]);
|
||||||
// event.description = source["description"].String();
|
event.description.jsonDeserialize(source["description"]);
|
||||||
event.effect.type = vstd::find_pos(typeNames, source["effect"]["type"].String());
|
event.effect.type = vstd::find_pos(typeNames, source["effect"]["type"].String());
|
||||||
// event.effect.toOtherMessage = source["effect"]["messageToSend"].String();
|
event.effect.toOtherMessage.jsonDeserialize(source["effect"]["messageToSend"]);
|
||||||
event.trigger = EventExpression(source["condition"], JsonToCondition); // logical expression
|
event.trigger = EventExpression(source["condition"], JsonToCondition); // logical expression
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -704,16 +704,16 @@ void CMapFormatJson::writeTriggeredEvent(const TriggeredEvent & event, JsonNode
|
|||||||
{
|
{
|
||||||
using namespace TriggeredEventsDetail;
|
using namespace TriggeredEventsDetail;
|
||||||
|
|
||||||
// if(!event.onFulfill.empty())
|
if(!event.onFulfill.empty())
|
||||||
// dest["message"].String() = event.onFulfill;
|
event.onFulfill.jsonSerialize(dest["message"]);
|
||||||
|
|
||||||
// if(!event.description.empty())
|
if(!event.description.empty())
|
||||||
// dest["description"].String() = event.description;
|
event.description.jsonSerialize(dest["description"]);
|
||||||
|
|
||||||
dest["effect"]["type"].String() = typeNames.at(static_cast<size_t>(event.effect.type));
|
dest["effect"]["type"].String() = typeNames.at(static_cast<size_t>(event.effect.type));
|
||||||
|
|
||||||
// if(!event.effect.toOtherMessage.empty())
|
if(!event.effect.toOtherMessage.empty())
|
||||||
// dest["effect"]["messageToSend"].String() = event.effect.toOtherMessage;
|
event.description.jsonSerialize(dest["effect"]["messageToSend"]);
|
||||||
|
|
||||||
dest["condition"] = event.trigger.toJson(ConditionToJson);
|
dest["condition"] = event.trigger.toJson(ConditionToJson);
|
||||||
}
|
}
|
||||||
|
@@ -5790,7 +5790,7 @@ void CGameHandler::checkVictoryLossConditionsForPlayer(PlayerColor player)
|
|||||||
void CGameHandler::getVictoryLossMessage(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult, InfoWindow & out) const
|
void CGameHandler::getVictoryLossMessage(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult, InfoWindow & out) const
|
||||||
{
|
{
|
||||||
out.player = player;
|
out.player = player;
|
||||||
out.text = victoryLossCheckResult.messageToOthers;
|
out.text = victoryLossCheckResult.messageToSelf;
|
||||||
out.text.replaceLocalString(EMetaText::COLOR, player.getNum());
|
out.text.replaceLocalString(EMetaText::COLOR, player.getNum());
|
||||||
out.components.emplace_back(Component::EComponentType::FLAG, player.getNum(), 0, 0);
|
out.components.emplace_back(Component::EComponentType::FLAG, player.getNum(), 0, 0);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user