diff options
Diffstat (limited to 'src/tool.cpp')
-rw-r--r-- | src/tool.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/tool.cpp b/src/tool.cpp index 839b1e387..2d2f9dee5 100644 --- a/src/tool.cpp +++ b/src/tool.cpp @@ -25,6 +25,34 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/serialize.h" #include "util/numeric.h" +void ToolGroupCap::toJson(Json::Value &object) const +{ + object["maxlevel"] = maxlevel; + object["uses"] = uses; + + Json::Value times_object; + for (auto time : times) + times_object[time.first] = time.second; + object["times"] = times_object; +} + +void ToolGroupCap::fromJson(const Json::Value &json) +{ + if (json.isObject()) { + if (json["maxlevel"].isInt()) + maxlevel = json["maxlevel"].asInt(); + if (json["uses"].isInt()) + uses = json["uses"].asInt(); + const Json::Value ×_object = json["times"]; + if (times_object.isArray()) { + Json::ArrayIndex size = times_object.size(); + for (Json::ArrayIndex i = 0; i < size; ++i) + if (times_object[i].isDouble()) + times[i] = times_object[i].asFloat(); + } + } +} + void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const { writeU8(os, 3); // protocol_version >= 36 @@ -84,6 +112,63 @@ void ToolCapabilities::deSerialize(std::istream &is) } } +void ToolCapabilities::serializeJson(std::ostream &os) const +{ + Json::Value root; + root["full_punch_interval"] = full_punch_interval; + root["max_drop_level"] = max_drop_level; + + Json::Value groupcaps_object; + for (auto groupcap : groupcaps) { + groupcap.second.toJson(groupcaps_object[groupcap.first]); + } + root["groupcaps"] = groupcaps_object; + + Json::Value damage_groups_object; + DamageGroup::const_iterator dgiter; + for (dgiter = damageGroups.begin(); dgiter != damageGroups.end(); ++dgiter) { + damage_groups_object[dgiter->first] = dgiter->second; + } + root["damage_groups"] = damage_groups_object; + + os << root; +} + +void ToolCapabilities::deserializeJson(std::istream &is) +{ + Json::Value root; + is >> root; + if (root.isObject()) { + if (root["full_punch_interval"].isDouble()) + full_punch_interval = root["full_punch_interval"].asFloat(); + if (root["max_drop_level"].isInt()) + max_drop_level = root["max_drop_level"].asInt(); + + Json::Value &groupcaps_object = root["groupcaps"]; + if (groupcaps_object.isObject()) { + Json::ValueIterator gciter; + for (gciter = groupcaps_object.begin(); + gciter != groupcaps_object.end(); ++gciter) { + ToolGroupCap groupcap; + groupcap.fromJson(*gciter); + groupcaps[gciter.key().asString()] = groupcap; + } + } + + Json::Value &damage_groups_object = root["damage_groups"]; + if (damage_groups_object.isObject()) { + Json::ValueIterator dgiter; + for (dgiter = damage_groups_object.begin(); + dgiter != damage_groups_object.end(); ++dgiter) { + Json::Value &value = *dgiter; + if (value.isInt()) + damageGroups[dgiter.key().asString()] = + value.asInt(); + } + } + } +} + DigParams getDigParams(const ItemGroupList &groups, const ToolCapabilities *tp, float time_from_last_punch) { |