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

MetaString can be serialized in json

This commit is contained in:
Ivan Savenko 2023-06-18 18:59:04 +03:00
parent d51fe62804
commit a84666edf6
4 changed files with 94 additions and 10 deletions

View File

@ -300,4 +300,84 @@ void MetaString::replaceCreatureName(const CStackBasicDescriptor & stack)
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

View File

@ -11,6 +11,7 @@
VCMI_LIB_NAMESPACE_BEGIN
class JsonNode;
class CreatureID;
class CStackBasicDescriptor;
using TQuantity = si32;
@ -108,6 +109,9 @@ public:
/// Returns true if current string is empty
bool empty() const;
void jsonSerialize(JsonNode & dest) const;
void jsonDeserialize(const JsonNode & dest);
template <typename Handler> void serialize(Handler & h, const int version)
{
h & exactStrings;

View File

@ -683,10 +683,10 @@ void CMapFormatJson::readTriggeredEvent(TriggeredEvent & event, const JsonNode &
{
using namespace TriggeredEventsDetail;
// event.onFulfill = source["message"].String();
// event.description = source["description"].String();
event.onFulfill.jsonDeserialize(source["message"]);
event.description.jsonDeserialize(source["description"]);
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
}
@ -704,16 +704,16 @@ void CMapFormatJson::writeTriggeredEvent(const TriggeredEvent & event, JsonNode
{
using namespace TriggeredEventsDetail;
// if(!event.onFulfill.empty())
// dest["message"].String() = event.onFulfill;
if(!event.onFulfill.empty())
event.onFulfill.jsonSerialize(dest["message"]);
// if(!event.description.empty())
// dest["description"].String() = event.description;
if(!event.description.empty())
event.description.jsonSerialize(dest["description"]);
dest["effect"]["type"].String() = typeNames.at(static_cast<size_t>(event.effect.type));
// if(!event.effect.toOtherMessage.empty())
// dest["effect"]["messageToSend"].String() = event.effect.toOtherMessage;
if(!event.effect.toOtherMessage.empty())
event.description.jsonSerialize(dest["effect"]["messageToSend"]);
dest["condition"] = event.trigger.toJson(ConditionToJson);
}

View File

@ -5790,7 +5790,7 @@ void CGameHandler::checkVictoryLossConditionsForPlayer(PlayerColor player)
void CGameHandler::getVictoryLossMessage(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult, InfoWindow & out) const
{
out.player = player;
out.text = victoryLossCheckResult.messageToOthers;
out.text = victoryLossCheckResult.messageToSelf;
out.text.replaceLocalString(EMetaText::COLOR, player.getNum());
out.components.emplace_back(Component::EComponentType::FLAG, player.getNum(), 0, 0);
}