2019-08-28 02:29:02 -04:00
|
|
|
#include "Globals.h"
|
|
|
|
#include "BlockTypePalette.h"
|
2019-12-02 10:45:55 -05:00
|
|
|
#include "json/value.h"
|
2020-05-09 10:51:15 -04:00
|
|
|
#include "JsonUtils.h"
|
2019-12-02 10:45:55 -05:00
|
|
|
|
2019-08-28 02:29:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-13 16:38:33 -05:00
|
|
|
/** Returns the index into aString >= aStartIdx at which the next separator occurs.
|
|
|
|
Separator is one of \t, \n or \r.
|
|
|
|
Returns AString::npos if no such separator. */
|
|
|
|
static size_t findNextSeparator(const AString & aString, size_t aStartIdx = 0)
|
|
|
|
{
|
|
|
|
for (size_t i = aStartIdx, len = aString.length(); i < len; ++i)
|
|
|
|
{
|
|
|
|
switch (aString[i])
|
|
|
|
{
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AString::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-01 08:41:46 -05:00
|
|
|
BlockTypePalette::BlockTypePalette():
|
|
|
|
mMaxIndex(0)
|
2019-08-28 02:29:02 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UInt32 BlockTypePalette::index(const AString & aBlockTypeName, const BlockState & aBlockState)
|
|
|
|
{
|
|
|
|
auto idx = maybeIndex(aBlockTypeName, aBlockState);
|
|
|
|
if (idx.second)
|
|
|
|
{
|
|
|
|
return idx.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found, append:
|
2019-12-01 08:41:46 -05:00
|
|
|
auto index = mMaxIndex++;
|
|
|
|
mBlockToNumber[aBlockTypeName][aBlockState] = index;
|
|
|
|
mNumberToBlock[index] = {aBlockTypeName, aBlockState};
|
|
|
|
return index;
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<UInt32, bool> BlockTypePalette::maybeIndex(const AString & aBlockTypeName, const BlockState & aBlockState) const
|
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
auto itr1 = mBlockToNumber.find(aBlockTypeName);
|
|
|
|
if (itr1 == mBlockToNumber.end())
|
2019-08-28 02:29:02 -04:00
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
return {0, false};
|
|
|
|
}
|
|
|
|
auto itr2 = itr1->second.find(aBlockState);
|
|
|
|
if (itr2 == itr1->second.end())
|
|
|
|
{
|
|
|
|
return {0, false};
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
2019-12-01 08:41:46 -05:00
|
|
|
return {itr2->second, true};
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UInt32 BlockTypePalette::count() const
|
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
return static_cast<UInt32>(mNumberToBlock.size());
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const std::pair<AString, BlockState> & BlockTypePalette::entry(UInt32 aIndex) const
|
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
auto itr = mNumberToBlock.find(aIndex);
|
|
|
|
if (itr == mNumberToBlock.end())
|
|
|
|
{
|
|
|
|
throw NoSuchIndexException(aIndex);
|
|
|
|
}
|
|
|
|
return itr->second;
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-01 08:41:46 -05:00
|
|
|
std::map<UInt32, UInt32> BlockTypePalette::createTransformMapAddMissing(const BlockTypePalette & aFrom)
|
2019-08-28 02:29:02 -04:00
|
|
|
{
|
|
|
|
std::map<UInt32, UInt32> res;
|
2019-12-01 08:41:46 -05:00
|
|
|
for (const auto & fromEntry: aFrom.mNumberToBlock)
|
2019-08-28 02:29:02 -04:00
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
auto fromIndex = fromEntry.first;
|
|
|
|
const auto & blockTypeName = fromEntry.second.first;
|
|
|
|
const auto & blockState = fromEntry.second.second;
|
|
|
|
res[fromIndex] = index(blockTypeName, blockState);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::map<UInt32, UInt32> BlockTypePalette::createTransformMapWithFallback(const BlockTypePalette & aFrom, UInt32 aFallbackIndex) const
|
|
|
|
{
|
|
|
|
std::map<UInt32, UInt32> res;
|
|
|
|
for (const auto & fromEntry: aFrom.mNumberToBlock)
|
|
|
|
{
|
|
|
|
auto fromIndex = fromEntry.first;
|
|
|
|
const auto & blockTypeName = fromEntry.second.first;
|
|
|
|
const auto & blockState = fromEntry.second.second;
|
|
|
|
auto thisIndex = maybeIndex(blockTypeName, blockState);
|
|
|
|
if (thisIndex.second)
|
|
|
|
{
|
|
|
|
// The entry was found in this
|
|
|
|
res[fromIndex] = thisIndex.first;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The entry was NOT found in this, replace with fallback:
|
|
|
|
res[fromIndex] = aFallbackIndex;
|
|
|
|
}
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2019-12-02 10:45:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BlockTypePalette::loadFromString(const AString & aString)
|
|
|
|
{
|
2019-12-20 16:10:24 -05:00
|
|
|
static const AString hdrTsvRegular = "BlockTypePalette";
|
|
|
|
static const AString hdrTsvUpgrade = "UpgradeBlockTypePalette";
|
|
|
|
|
|
|
|
// Detect format by checking the header line (none -> JSON):
|
|
|
|
if (aString.substr(0, hdrTsvRegular.length()) == hdrTsvRegular)
|
|
|
|
{
|
|
|
|
return loadFromTsv(aString, false);
|
|
|
|
}
|
|
|
|
else if (aString.substr(0, hdrTsvUpgrade.length()) == hdrTsvUpgrade)
|
|
|
|
{
|
|
|
|
return loadFromTsv(aString, true);
|
|
|
|
}
|
2019-12-02 10:45:55 -05:00
|
|
|
return loadFromJsonString(aString);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BlockTypePalette::loadFromJsonString(const AString & aJsonPalette)
|
|
|
|
{
|
|
|
|
// Parse the string into JSON object:
|
|
|
|
Json::Value root;
|
|
|
|
std::string errs;
|
2020-05-09 10:51:15 -04:00
|
|
|
if (!JsonUtils::ParseString(aJsonPalette, root, &errs))
|
2019-12-02 10:45:55 -05:00
|
|
|
{
|
|
|
|
throw LoadFailedException(errs);
|
|
|
|
}
|
|
|
|
|
2019-12-20 16:10:24 -05:00
|
|
|
// Sanity-check the JSON's structure:
|
|
|
|
if (!root.isObject())
|
2019-12-02 10:45:55 -05:00
|
|
|
{
|
2019-12-20 16:10:24 -05:00
|
|
|
throw LoadFailedException("Incorrect palette format, expected an object at root.");
|
2019-12-02 10:45:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the palette:
|
2019-12-20 16:10:24 -05:00
|
|
|
for (auto itr = root.begin(), end = root.end(); itr != end; ++itr)
|
2019-12-02 10:45:55 -05:00
|
|
|
{
|
2019-12-20 16:10:24 -05:00
|
|
|
const auto & blockTypeName = itr.name();
|
|
|
|
const auto & states = (*itr)["states"];
|
|
|
|
if (states == Json::Value())
|
2019-12-02 10:45:55 -05:00
|
|
|
{
|
2019-12-20 16:10:24 -05:00
|
|
|
throw LoadFailedException(Printf("Missing \"states\" for block type \"%s\"", blockTypeName));
|
2019-12-02 10:45:55 -05:00
|
|
|
}
|
2019-12-20 16:10:24 -05:00
|
|
|
for (const auto & state: states)
|
|
|
|
{
|
|
|
|
auto id = static_cast<UInt32>(std::stoul(state["id"].asString()));
|
|
|
|
std::map<AString, AString> props;
|
|
|
|
if (state.isMember("properties"))
|
|
|
|
{
|
|
|
|
const auto & properties = state["properties"];
|
|
|
|
if (!properties.isObject())
|
|
|
|
{
|
|
|
|
throw LoadFailedException(Printf("Member \"properties\" is not a JSON object (block type \"%s\", id %u).", blockTypeName, id));
|
|
|
|
}
|
|
|
|
for (const auto & key: properties.getMemberNames())
|
|
|
|
{
|
|
|
|
props[key] = properties[key].asString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addMapping(id, blockTypeName, props);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 10:45:55 -05:00
|
|
|
|
|
|
|
|
2019-12-20 16:10:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BlockTypePalette::loadFromTsv(const AString & aTsvPalette, bool aIsUpgrade)
|
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
static const AString hdrTsvRegular = "BlockTypePalette";
|
|
|
|
static const AString hdrTsvUpgrade = "UpgradeBlockTypePalette";
|
|
|
|
|
|
|
|
// Check the file signature:
|
|
|
|
auto idx = findNextSeparator(aTsvPalette);
|
|
|
|
if ((idx == AString::npos) || (aTsvPalette[idx] == '\t'))
|
|
|
|
{
|
|
|
|
throw LoadFailedException("Invalid signature");
|
|
|
|
}
|
|
|
|
auto signature = aTsvPalette.substr(0, idx);
|
|
|
|
bool isUpgrade = (signature == hdrTsvUpgrade);
|
|
|
|
if (!isUpgrade && (signature != hdrTsvRegular))
|
|
|
|
{
|
|
|
|
throw LoadFailedException("Unknown signature");
|
|
|
|
}
|
|
|
|
if (aTsvPalette[idx] == '\r') // CR of the CRLF pair, skip the LF:
|
|
|
|
{
|
|
|
|
idx += 1;
|
|
|
|
}
|
2019-12-20 16:10:24 -05:00
|
|
|
|
|
|
|
// Parse the header:
|
2020-01-13 16:38:33 -05:00
|
|
|
bool hasHadVersion = false;
|
2019-12-20 16:10:24 -05:00
|
|
|
AString commonPrefix;
|
2020-01-13 16:38:33 -05:00
|
|
|
int line = 2;
|
|
|
|
auto len = aTsvPalette.length();
|
|
|
|
while (true)
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
auto keyStart = idx + 1;
|
|
|
|
auto keyEnd = findNextSeparator(aTsvPalette, idx + 1);
|
|
|
|
if (keyEnd == AString::npos)
|
|
|
|
{
|
|
|
|
throw LoadFailedException(Printf("Invalid header key format on line %u", line));
|
|
|
|
}
|
|
|
|
if (keyEnd == idx + 1) // Empty line, end of headers
|
2019-12-02 10:45:55 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
if (aTsvPalette[keyEnd] == '\r') // CR of the CRLF pair, skip the LF:
|
|
|
|
{
|
|
|
|
++keyEnd;
|
|
|
|
}
|
|
|
|
idx = keyEnd;
|
|
|
|
++line;
|
2019-12-20 16:10:24 -05:00
|
|
|
break;
|
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
auto valueEnd = findNextSeparator(aTsvPalette, keyEnd + 1);
|
|
|
|
if ((valueEnd == AString::npos) || (aTsvPalette[valueEnd] == '\t'))
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
throw LoadFailedException(Printf("Invalid header value format on line %u", line));
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
auto key = aTsvPalette.substr(keyStart, keyEnd - keyStart);
|
|
|
|
if (key == "FileVersion")
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
unsigned version = 0;
|
|
|
|
auto value = aTsvPalette.substr(keyEnd + 1, valueEnd - keyEnd - 1);
|
|
|
|
if (!StringToInteger(value, version))
|
2019-12-02 10:45:55 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
throw LoadFailedException("Invalid FileVersion value");
|
2019-12-02 10:45:55 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
else if (version != 1)
|
2019-12-02 10:45:55 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
throw LoadFailedException(Printf("Unknown FileVersion: %u. Only version 1 is supported.", version));
|
2019-12-02 10:45:55 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
hasHadVersion = true;
|
|
|
|
}
|
|
|
|
else if (key == "CommonPrefix")
|
|
|
|
{
|
|
|
|
commonPrefix = aTsvPalette.substr(keyEnd + 1, valueEnd - keyEnd - 1);
|
2019-12-02 10:45:55 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
idx = valueEnd;
|
|
|
|
if (aTsvPalette[idx] == '\r') // CR of the CRLF pair, skip the LF:
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
++idx;
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
++line;
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
if (!hasHadVersion)
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
throw LoadFailedException("No FileVersion value");
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2019-12-02 10:45:55 -05:00
|
|
|
|
2019-12-20 16:10:24 -05:00
|
|
|
// Parse the data:
|
2020-01-13 16:38:33 -05:00
|
|
|
while (idx + 1 < len)
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
auto lineStart = idx + 1;
|
|
|
|
auto idEnd = findNextSeparator(aTsvPalette, lineStart);
|
|
|
|
if ((idEnd == AString::npos) || (aTsvPalette[idEnd] != '\t'))
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
throw LoadFailedException(Printf("Incomplete data on line %u (id)", line));
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
|
|
|
UInt32 id;
|
2020-01-13 16:38:33 -05:00
|
|
|
if (!StringToInteger(aTsvPalette.substr(lineStart, idEnd - lineStart), id))
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
throw LoadFailedException(Printf("Failed to parse id on line %u", line));
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
size_t metaEnd = idEnd;
|
|
|
|
if (isUpgrade)
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
metaEnd = findNextSeparator(aTsvPalette, idEnd + 1);
|
|
|
|
if ((metaEnd == AString::npos) || (aTsvPalette[metaEnd] != '\t'))
|
|
|
|
{
|
|
|
|
throw LoadFailedException(Printf("Incomplete data on line %u (meta)", line));
|
|
|
|
}
|
|
|
|
UInt32 meta = 0;
|
|
|
|
if (!StringToInteger(aTsvPalette.substr(idEnd + 1, metaEnd - idEnd - 1), meta))
|
|
|
|
{
|
|
|
|
throw LoadFailedException(Printf("Failed to parse meta on line %u", line));
|
|
|
|
}
|
|
|
|
if (meta > 15)
|
|
|
|
{
|
|
|
|
throw LoadFailedException(Printf("Invalid meta value on line %u: %u", line, meta));
|
|
|
|
}
|
|
|
|
id = (id * 16) | meta;
|
|
|
|
}
|
|
|
|
auto blockTypeEnd = findNextSeparator(aTsvPalette, metaEnd + 1);
|
|
|
|
if (blockTypeEnd == AString::npos)
|
|
|
|
{
|
|
|
|
throw LoadFailedException(Printf("Incomplete data on line %u (blockTypeName)", line));
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
auto blockTypeName = aTsvPalette.substr(metaEnd + 1, blockTypeEnd - metaEnd - 1);
|
|
|
|
auto blockStateEnd = blockTypeEnd;
|
|
|
|
AStringMap blockState;
|
|
|
|
while (aTsvPalette[blockStateEnd] == '\t')
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
auto keyEnd = findNextSeparator(aTsvPalette, blockStateEnd + 1);
|
|
|
|
if ((keyEnd == AString::npos) || (aTsvPalette[keyEnd] != '\t'))
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
throw LoadFailedException(Printf("Incomplete data on line %u (blockState key)", line));
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
auto valueEnd = findNextSeparator(aTsvPalette, keyEnd + 1);
|
|
|
|
if (valueEnd == AString::npos)
|
2019-12-20 16:10:24 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
throw LoadFailedException(Printf("Incomplete data on line %u (blockState value)", line));
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
auto key = aTsvPalette.substr(blockStateEnd + 1, keyEnd - blockStateEnd - 1);
|
|
|
|
auto value = aTsvPalette.substr(keyEnd + 1, valueEnd - keyEnd - 1);
|
|
|
|
blockState[key] = value;
|
|
|
|
blockStateEnd = valueEnd;
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
addMapping(id, commonPrefix + blockTypeName, std::move(blockState));
|
|
|
|
++line;
|
|
|
|
if (aTsvPalette[blockStateEnd] == '\r') // CR of the CRLF pair, skip the LF:
|
2019-12-02 10:45:55 -05:00
|
|
|
{
|
2020-01-13 16:38:33 -05:00
|
|
|
++blockStateEnd;
|
2019-12-02 10:45:55 -05:00
|
|
|
}
|
2020-01-13 16:38:33 -05:00
|
|
|
idx = blockStateEnd;
|
2019-12-20 16:10:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BlockTypePalette::addMapping(UInt32 aID, const AString & aBlockTypeName, const BlockState & aBlockState)
|
|
|
|
{
|
|
|
|
mNumberToBlock[aID] = {aBlockTypeName, aBlockState};
|
|
|
|
mBlockToNumber[aBlockTypeName][aBlockState] = aID;
|
|
|
|
if (aID > mMaxIndex)
|
|
|
|
{
|
|
|
|
mMaxIndex = aID;
|
|
|
|
}
|
2019-12-02 10:45:55 -05:00
|
|
|
}
|