summaryrefslogtreecommitdiffstats
path: root/src/BlockTypePalette.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/BlockTypePalette.cpp')
-rw-r--r--src/BlockTypePalette.cpp82
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[]
+}