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"
|
|
|
|
#include "json/reader.h"
|
|
|
|
|
2019-08-28 02:29:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// 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[]
|
|
|
|
}
|