Cuberite
A lightweight, fast and extensible game server for Minecraft
|
Holds a palette that maps between block type + state and numbers. More...
#include <BlockTypePalette.h>
Classes | |
class | LoadFailedException |
Exception that is thrown when loading the palette fails hard (bad format). More... | |
class | NoSuchIndexException |
Exception that is thrown if requiesting an index not present in the palette. More... | |
Public Member Functions | |
BlockTypePalette () | |
Create a new empty instance. More... | |
UInt32 | count () const |
Returns the total number of entries in the palette. More... | |
std::map< UInt32, UInt32 > | createTransformMapAddMissing (const BlockTypePalette &aFrom) |
Returns an index-transform map from aFrom to this (this.entry(idx) == aFrom.entry(res[idx])). More... | |
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])). More... | |
const std::pair< AString, BlockState > & | entry (UInt32 aIndex) const |
Returns the blockspec represented by the specified palette index. More... | |
UInt32 | index (const AString &aBlockTypeName, const BlockState &aBlockState) |
Returns the index of the specified block type name and state. More... | |
void | loadFromString (const AString &aString) |
Loads the palette from the string representation. More... | |
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. More... | |
Protected Member Functions | |
void | addMapping (UInt32 aID, const AString &aBlockTypeName, const BlockState &aBlockState) |
Adds a mapping between the numeric and stringular representation into both maps, updates the mMaxIndex, if appropriate. More... | |
void | loadFromJsonString (const AString &aJsonPalette) |
Loads the palette from the JSON representation, https://wiki.vg/Data_Generators Throws a LoadFailedException if the loading fails hard (bad string format); but still a part of the data may already be loaded at that point. More... | |
void | loadFromTsv (const AString &aTsvPalette, bool aIsUpgrade) |
Loads the palette from the regular or upgrade TSV representation. More... | |
Protected Attributes | |
std::unordered_map< AString, std::map< BlockState, UInt32 > > | mBlockToNumber |
The mapping from stringular to numeric representation. More... | |
UInt32 | mMaxIndex |
The maximum index ever used in the maps. More... | |
std::map< UInt32, std::pair< AString, BlockState > > | mNumberToBlock |
The mapping from numeric to stringular representation. More... | |
Holds a palette that maps between block type + state and numbers.
Used primarily by PalettedBlockArea to map from stringular block representation to numeric, and by protocols to map from stringular block representation to protocol-numeric. The object itself provides no thread safety, users of this class need to handle locking, if required. Note that the palette itself doesn't support erasing; to erase, create a new instance and re-add only the wanted items.
Internally, the object uses two synced maps, one for each translation direction.
The palette can be loaded from a string (file). The loader supports either the blocks.json file exported by the vanilla server itself (https://wiki.vg/Data_Generators), or a processed text file generated by our tool $/Tools/BlockTypePaletteGenerator/, or a hand-written text file describing the upgrade from 1.12 BlockType + BlockMeta to 1.13 string representations. The text file is a TSV (tab-separated values), which basically means the data is generally structured as <value1><tab><value2><tab><value3><tab>...<valueN><eol>, where eol is the platform's CR / CRLF / LF lineend. The file starts with a single value on the first line, "BlockTypePalette" or "UpgradeBlockTypePalette", which is used to detect the file format. The following lines are "headers", simple <key><tab>
<eol> entries that contain the metadata about the file. "FileVersion" is a compulsory key, "CommonPrefix" is supported, others are ignored. The headers are followed by an empty line (that signalizes the end of headers) and then the actual data. For regular BlockTypePalette TSV file of version 1, the data is in the format: <index><tab><blockTypeName><tab><state1Name><tab><state1Value><tab><state2Name> ... <eol> For the UpgradeBlockTypePalette TSV file of version 1, the data is in the format: <blockType><tab><blockMeta><tab><blockTypeName><tab><state1Name><tab><state1Value><tab><state2Name> ... <eol> If a CommonPrefix header is present, its value is pre-pended to each blockTypeName loaded (thus allowing the file to be overall smaller).
Definition at line 36 of file BlockTypePalette.h.
BlockTypePalette::BlockTypePalette | ( | ) |
Create a new empty instance.
Definition at line 34 of file BlockTypePalette.cpp.
|
protected |
Adds a mapping between the numeric and stringular representation into both maps, updates the mMaxIndex, if appropriate.
Silently overwrites any previous mapping for the ID, if present, but keeps the old string->id mapping.
Definition at line 372 of file BlockTypePalette.cpp.
UInt32 BlockTypePalette::count | ( | ) | const |
Returns the total number of entries in the palette.
Definition at line 81 of file BlockTypePalette.cpp.
std::map< UInt32, UInt32 > BlockTypePalette::createTransformMapAddMissing | ( | const BlockTypePalette & | aFrom | ) |
Returns an index-transform map from aFrom to this (this.entry(idx) == aFrom.entry(res[idx])).
Entries from aFrom that are not present in this are added. Used when pasting two areas, to transform the src palette to dst palette.
Definition at line 104 of file BlockTypePalette.cpp.
std::map< UInt32, UInt32 > BlockTypePalette::createTransformMapWithFallback | ( | const BlockTypePalette & | aFrom, |
UInt32 | aFallbackIndex | ||
) | const |
Returns an index-transform map from aFrom to this (this.entry(idx) == aFrom.entry(res[idx])).
Entries from aFrom that are not present in this are assigned the fallback index. Used for protocol block type mapping.
Definition at line 121 of file BlockTypePalette.cpp.
const std::pair< AString, BlockState > & BlockTypePalette::entry | ( | UInt32 | aIndex | ) | const |
Returns the blockspec represented by the specified palette index.
If the index is not valid, throws a NoSuchIndexException.
Definition at line 90 of file BlockTypePalette.cpp.
UInt32 BlockTypePalette::index | ( | const AString & | aBlockTypeName, |
const BlockState & | aBlockState | ||
) |
Returns the index of the specified block type name and state.
If the combination is not found, it is added to the palette and the new index is returned.
Definition at line 43 of file BlockTypePalette.cpp.
|
protected |
Loads the palette from the JSON representation, https://wiki.vg/Data_Generators Throws a LoadFailedException if the loading fails hard (bad string format); but still a part of the data may already be loaded at that point.
See also: loadFromString().
Definition at line 169 of file BlockTypePalette.cpp.
void BlockTypePalette::loadFromString | ( | const AString & | aString | ) |
Loads the palette from the string representation.
Throws a LoadFailedException if the loading fails hard (bad string format); but still a part of the data may already be loaded at that point. 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". Auto-detects the string format (json / tsv, normal / upgrade palette) and calls the appropriate load function.
Definition at line 148 of file BlockTypePalette.cpp.
|
protected |
Loads the palette from the regular or upgrade TSV representation.
aIsUpgrade specifies whether the format is an upgrade TSV (true) or a regular one (false) Throws a LoadFailedException if the loading fails hard (bad string format); but still a part of the data may already be loaded at that point. See also: loadFromString().
Definition at line 219 of file BlockTypePalette.cpp.
std::pair< UInt32, bool > BlockTypePalette::maybeIndex | ( | const AString & | aBlockTypeName, |
const BlockState & | aBlockState | ||
) | const |
Returns the <index, true> of the specified block type name and state, if it exists.
If the combination is not found, returns <undefined, false>.
Definition at line 62 of file BlockTypePalette.cpp.
|
protected |
The mapping from stringular to numeric representation.
mStringToNumber["blockTypeName"][blockState] = index.
Definition at line 114 of file BlockTypePalette.h.
|
protected |
The maximum index ever used in the maps.
Used when adding new entries through the index() call.
Definition at line 118 of file BlockTypePalette.h.
|
protected |
The mapping from numeric to stringular representation.
mNumberToBlock[index] = {"blockTypeName", blockState}.
Definition at line 110 of file BlockTypePalette.h.