BlockStateDictionary: extract a more generic helper method for decoding block palette files

This commit is contained in:
Dylan K. Taylor 2022-07-06 14:37:46 +01:00
parent dc8f65c0dd
commit a8728a02f6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 19 additions and 20 deletions

View File

@ -29,11 +29,9 @@ use pocketmine\data\bedrock\block\BlockStateStringValues;
use pocketmine\data\bedrock\block\BlockTypeNames; use pocketmine\data\bedrock\block\BlockTypeNames;
use pocketmine\errorhandler\ErrorToExceptionHandler; use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\NbtException; use pocketmine\nbt\NbtException;
use pocketmine\nbt\TreeRoot; use pocketmine\network\mcpe\convert\BlockStateDictionary;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use function array_map;
use function array_values; use function array_values;
use function asort; use function asort;
use function count; use function count;
@ -183,10 +181,7 @@ if($paletteRaw === false){
} }
try{ try{
$states = array_map( $states = BlockStateDictionary::loadPaletteFromString($paletteRaw);
fn(TreeRoot $root) => BlockStateData::fromNbt($root->mustGetCompoundTag()),
(new NetworkNbtSerializer())->readMultiple($paletteRaw)
);
}catch(NbtException){ }catch(NbtException){
fwrite(STDERR, "Invalid block palette file $argv[1]\n"); fwrite(STDERR, "Invalid block palette file $argv[1]\n");
exit(1); exit(1);

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\convert; namespace pocketmine\network\mcpe\convert;
use pocketmine\data\bedrock\block\BlockStateData; use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\TreeRoot; use pocketmine\nbt\TreeRoot;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer; use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use function array_map; use function array_map;
@ -107,6 +108,19 @@ final class BlockStateDictionary{
*/ */
public function getStates() : array{ return $this->states; } public function getStates() : array{ return $this->states; }
/**
* @return BlockStateData[]
* @phpstan-return list<BlockStateData>
*
* @throws NbtDataException
*/
public static function loadPaletteFromString(string $blockPaletteContents) : array{
return array_map(
fn(TreeRoot $root) => BlockStateData::fromNbt($root->mustGetCompoundTag()),
(new NetworkNbtSerializer())->readMultiple($blockPaletteContents)
);
}
public static function loadFromString(string $blockPaletteContents, string $metaMapContents) : self{ public static function loadFromString(string $blockPaletteContents, string $metaMapContents) : self{
$metaMap = json_decode($metaMapContents, flags: JSON_THROW_ON_ERROR); $metaMap = json_decode($metaMapContents, flags: JSON_THROW_ON_ERROR);
if(!is_array($metaMap)){ if(!is_array($metaMap)){
@ -114,12 +128,8 @@ final class BlockStateDictionary{
} }
$entries = []; $entries = [];
$states = array_map(
fn(TreeRoot $root) => BlockStateData::fromNbt($root->mustGetCompoundTag()),
(new NetworkNbtSerializer())->readMultiple($blockPaletteContents)
);
foreach($states as $i => $state){ foreach(self::loadPaletteFromString($blockPaletteContents) as $i => $state){
$meta = $metaMap[$i] ?? null; $meta = $metaMap[$i] ?? null;
if($meta === null){ if($meta === null){
throw new \InvalidArgumentException("Missing associated meta value for state $i (" . $state->toNbt() . ")"); throw new \InvalidArgumentException("Missing associated meta value for state $i (" . $state->toNbt() . ")");

View File

@ -23,17 +23,14 @@ declare(strict_types=1);
namespace pocketmine\tools\generate_block_palette_spec; namespace pocketmine\tools\generate_block_palette_spec;
use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\errorhandler\ErrorToExceptionHandler; use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\nbt\NbtException; use pocketmine\nbt\NbtException;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\TreeRoot; use pocketmine\network\mcpe\convert\BlockStateDictionary;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use function array_map;
use function array_values; use function array_values;
use function count; use function count;
use function dirname; use function dirname;
@ -54,10 +51,7 @@ if(count($argv) !== 3){
[, $inputFile, $outputFile] = $argv; [, $inputFile, $outputFile] = $argv;
try{ try{
$states = array_map( $states = BlockStateDictionary::loadPaletteFromString(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($inputFile)));
fn(TreeRoot $root) => BlockStateData::fromNbt($root->mustGetCompoundTag()),
(new NetworkNbtSerializer())->readMultiple(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents($inputFile)))
);
}catch(NbtException){ }catch(NbtException){
fwrite(STDERR, "Invalid block palette file $argv[1]\n"); fwrite(STDERR, "Invalid block palette file $argv[1]\n");
exit(1); exit(1);