diff options
author | Mattes D <github@xoft.cz> | 2019-12-02 16:45:55 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2019-12-28 22:43:35 +0100 |
commit | 7453a9fbe120f345625a24bbea18c35cd3c26a6a (patch) | |
tree | e97cc9c90fedcad6e44f4011f0fdc4d2f6a284bf /src/BlockTypePalette.cpp | |
parent | BlockTypePalette: Refactored for usage in both directions. (diff) | |
download | cuberite-7453a9fbe120f345625a24bbea18c35cd3c26a6a.tar cuberite-7453a9fbe120f345625a24bbea18c35cd3c26a6a.tar.gz cuberite-7453a9fbe120f345625a24bbea18c35cd3c26a6a.tar.bz2 cuberite-7453a9fbe120f345625a24bbea18c35cd3c26a6a.tar.lz cuberite-7453a9fbe120f345625a24bbea18c35cd3c26a6a.tar.xz cuberite-7453a9fbe120f345625a24bbea18c35cd3c26a6a.tar.zst cuberite-7453a9fbe120f345625a24bbea18c35cd3c26a6a.zip |
Diffstat (limited to 'src/BlockTypePalette.cpp')
-rw-r--r-- | src/BlockTypePalette.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/BlockTypePalette.cpp b/src/BlockTypePalette.cpp index f0f54b7c1..92269db9d 100644 --- a/src/BlockTypePalette.cpp +++ b/src/BlockTypePalette.cpp @@ -1,5 +1,8 @@ #include "Globals.h" #include "BlockTypePalette.h" +#include "json/value.h" +#include "json/reader.h" + @@ -113,3 +116,82 @@ std::map<UInt32, UInt32> BlockTypePalette::createTransformMapWithFallback(const } return res; } + + + + + +void BlockTypePalette::loadFromString(const AString & aString) +{ + // TODO: Detect format (Json vs Lua) + return loadFromJsonString(aString); +} + + + + + +void BlockTypePalette::loadFromJsonString(const AString & aJsonPalette) +{ + // Parse the string into JSON object: + Json::Value root; + Json::CharReaderBuilder builder; + std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); + std::string errs; + if (!reader->parse(aJsonPalette.data(), aJsonPalette.data() + aJsonPalette.size(), &root, &errs)) + { + throw LoadFailedException(errs); + } + + // Check the JSON's metadata + version: + if (!root.isObject() || + !root.isMember("Metadata") || + !root["Metadata"].isMember("ProtocolBlockTypePaletteVersion") || + !root.isMember("Palette") || + !root["Palette"].isArray()) + { + throw LoadFailedException("Incorrect palette format, wrong or missing metadata."); + } + auto version = root["Metadata"]["ProtocolBlockTypePaletteVersion"].asUInt(); + if (version != 1) + { + throw(Printf("Palette format version %d not supported.", version)); + } + + // Load the palette: + auto len = root["Palette"].size(); + for (decltype(len) i = 0; i < len; ++i) + { + const auto & record = root["Palette"][i]; + if (!record.isObject()) + { + throw LoadFailedException(Printf("Palette record #%u is not a JSON object.", i)); + } + + auto blockTypeName = record["name"].asString(); + auto id = static_cast<UInt32>(std::stoul(record["id"].asString())); + std::map<AString, AString> state; + + if (record.isMember("props")) + { + const auto & props = record["props"]; + if (!props.isObject()) + { + throw LoadFailedException(Printf("Palette record #%u: \"props\" value is not a JSON object.", i)); + } + for (const auto & key: props.getMemberNames()) + { + state[key] = props[key].asString(); + } + } + BlockState blockState(state); + + // Insert / update in the maps: + mNumberToBlock[id] = {blockTypeName, blockState}; + mBlockToNumber[blockTypeName][blockState] = id; + if (id > mMaxIndex) + { + mMaxIndex = id; + } + } // for i - Palette[] +} |