3 #include "json/value.h" 4 #include "json/reader.h" 21 auto idx =
maybeIndex(aBlockTypeName, aBlockState);
45 auto itr2 = itr1->second.find(aBlockState);
46 if (itr2 == itr1->second.end())
50 return {itr2->second,
true};
82 std::map<UInt32, UInt32> res;
85 auto fromIndex = fromEntry.first;
86 const auto & blockTypeName = fromEntry.second.first;
87 const auto & blockState = fromEntry.second.second;
88 res[fromIndex] =
index(blockTypeName, blockState);
99 std::map<UInt32, UInt32> res;
102 auto fromIndex = fromEntry.first;
103 const auto & blockTypeName = fromEntry.second.first;
104 const auto & blockState = fromEntry.second.second;
105 auto thisIndex =
maybeIndex(blockTypeName, blockState);
106 if (thisIndex.second)
109 res[fromIndex] = thisIndex.first;
114 res[fromIndex] = aFallbackIndex;
126 static const AString hdrTsvRegular =
"BlockTypePalette";
127 static const AString hdrTsvUpgrade =
"UpgradeBlockTypePalette";
130 if (aString.substr(0, hdrTsvRegular.length()) == hdrTsvRegular)
134 else if (aString.substr(0, hdrTsvUpgrade.length()) == hdrTsvUpgrade)
149 Json::CharReaderBuilder builder;
150 std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
152 if (!reader->parse(aJsonPalette.data(), aJsonPalette.data() + aJsonPalette.size(), &root, &errs))
158 if (!root.isObject())
164 for (
auto itr = root.begin(), end = root.end(); itr != end; ++itr)
166 const auto & blockTypeName = itr.name();
167 const auto & states = (*itr)[
"states"];
168 if (states == Json::Value())
172 for (
const auto & state: states)
174 auto id =
static_cast<UInt32>(std::stoul(state[
"id"].asString()));
175 std::map<AString, AString> props;
176 if (state.isMember(
"properties"))
178 const auto & properties = state[
"properties"];
179 if (!properties.isObject())
181 throw LoadFailedException(
Printf(
"Member \"properties\" is not a JSON object (block type \"%s\", id %u).", blockTypeName,
id));
183 for (
const auto & key: properties.getMemberNames())
185 props[key] = properties[key].asString();
204 auto numLines = lines.size();
205 for (
size_t idx = 1; idx < numLines; ++idx)
207 const auto & line = lines[idx];
211 lines.erase(lines.begin(), lines.begin() +
static_cast<AStringVector::difference_type
>(idx) + 1);
219 if (s[0] ==
"FileVersion")
223 fileVersion = std::stoi(s[1]);
225 catch (
const std::exception & exc)
230 else if (s[0] ==
"CommonPrefix")
235 if (fileVersion != 1)
241 size_t minSplit = aIsUpgrade ? 3 : 2;
242 for (
const auto & line: lines)
245 auto numSplit = s.size();
246 if (numSplit < minSplit)
253 id =
static_cast<UInt32>(std::stoi(s[0]));
255 catch (
const std::exception & exc)
265 id =
id +
static_cast<UInt32>(
Clamp(std::stoi(s[1]), 0, 15));
267 catch (
const std::exception & exc)
273 const auto & blockTypeName = s[idx];
275 std::map<AString, AString> state;
276 while (idx + 1 < numSplit)
278 state[s[idx]] = s[idx + 1];
281 addMapping(
id, commonPrefix + blockTypeName, state);
void loadFromJsonString(const AString &aJsonPalette)
Loads the palette from the JSON representation, https://wiki.vg/Data_Generators Throws a LoadFailedEx...
std::pair< UInt32, bool > maybeIndex(const AString &aBlockTypeName, const BlockState &aBlockState) const
Returns the <index, true> of the specified block type name and state, if it exists.
const std::pair< AString, BlockState > & entry(UInt32 aIndex) const
Returns the blockspec represented by the specified palette index.
void addMapping(UInt32 aID, const AString &aBlockTypeName, const BlockState &aBlockState)
Adds a mapping between the numeric and stringular representation into both maps, updates the mMaxInde...
UInt32 index(const AString &aBlockTypeName, const BlockState &aBlockState)
Returns the index of the specified block type name and state.
Exception that is thrown if requiesting an index not present in the palette.
std::map< UInt32, UInt32 > createTransformMapAddMissing(const BlockTypePalette &aFrom)
Returns an index-transform map from aFrom to this (this.entry(idx) == aFrom.entry(res[idx])).
Holds a palette that maps between block type + state and numbers.
UInt32 mMaxIndex
The maximum index ever used in the maps.
AStringVector StringSplitAndTrim(const AString &str, const AString &delim)
Split the string at any of the listed delimiters and trim each value.
BlockTypePalette()
Create a new empty instance.
void loadFromString(const AString &aString)
Loads the palette from the string representation.
std::map< UInt32, std::pair< AString, BlockState > > mNumberToBlock
The mapping from numeric to stringular representation.
std::map< UInt32, UInt32 > createTransformMapWithFallback(const BlockTypePalette &aFrom, UInt32 aFallbackIndex) const
Returns an index-transform map from aFrom to this (this.entry(idx) == aFrom.entry(res[idx])).
AString & Printf(AString &str, const char *format, fmt::ArgList args)
Output the formatted text into the string.
std::unordered_map< AString, std::map< BlockState, UInt32 > > mBlockToNumber
The mapping from stringular to numeric representation.
T Clamp(T a_Value, T a_Min, T a_Max)
Clamp X to the specified range.
AStringVector StringSplit(const AString &str, const AString &delim)
Split the string at any of the listed delimiters.
void loadFromTsv(const AString &aTsvPalette, bool aIsUpgrade)
Loads the palette from the regular or upgrade TSV representation.
UInt32 count() const
Returns the total number of entries in the palette.
Exception that is thrown when loading the palette fails hard (bad format).
Represents the state of a single block (previously known as "block meta").