Skip to content

Commit

Permalink
Moved ProtocolBlockTypePalette functionality into BlockTypePalette.
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft committed Dec 28, 2019
1 parent 2de6b75 commit 7453a9f
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 404 deletions.
82 changes: 82 additions & 0 deletions src/BlockTypePalette.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "Globals.h"
#include "BlockTypePalette.h"
#include "json/value.h"
#include "json/reader.h"




Expand Down Expand Up @@ -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[]
}
28 changes: 28 additions & 0 deletions src/BlockTypePalette.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ class BlockTypePalette
};


/** Exception that is thrown when loading the palette fails hard (bad format). */
class LoadFailedException:
public std::runtime_error
{
using Super = std::runtime_error;

public:
LoadFailedException(const AString & aReason):
Super(aReason)
{
}
};



/** Create a new empty instance. */
BlockTypePalette();
Expand Down Expand Up @@ -63,6 +77,13 @@ class BlockTypePalette
Used for protocol block type mapping. */
std::map<UInt32, UInt32> createTransformMapWithFallback(const BlockTypePalette & aFrom, UInt32 aFallbackIndex) const;

/** Loads the palette from the string representation.
Throws a LoadFailedException if the loading fails hard (bad string format).
If the string specifies duplicate entries (either to already existing entries, or to itself),
the duplicates replace the current values silently (this allows us to chain multiple files as "overrides".
Currently handles only JSON representation, expected to handle also Lua representation in the future. */
void loadFromString(const AString & aString);


protected:

Expand All @@ -77,4 +98,11 @@ class BlockTypePalette
/** The maximum index ever used in the maps.
Used when adding new entries through the index() call. */
UInt32 mMaxIndex;


/** Loads the palette from the JSON representation.
Throws a LoadFailedException if the loading fails hard (bad string format).
If the string specifies duplicate entries (either to already existing entries, or to itself),
the duplicates replace the current values silently (this allows us to chain multiple files as "overrides". */
void loadFromJsonString(const AString & aJsonPalette);
};
2 changes: 0 additions & 2 deletions src/Protocol/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ SET (SRCS
Protocol_1_12.cpp
Protocol_1_13.cpp
ProtocolRecognizer.cpp
ProtocolBlockTypePalette.cpp
)

SET (HDRS
Expand All @@ -30,7 +29,6 @@ SET (HDRS
Protocol_1_12.h
Protocol_1_13.h
ProtocolRecognizer.h
ProtocolBlockTypePalette.h
)

if (NOT MSVC)
Expand Down
144 changes: 0 additions & 144 deletions src/Protocol/ProtocolBlockTypePalette.cpp

This file was deleted.

40 changes: 0 additions & 40 deletions src/Protocol/ProtocolBlockTypePalette.h

This file was deleted.

Loading

0 comments on commit 7453a9f

Please sign in to comment.