Generate traits for runtime enum serialization instead of helper classes

This commit is contained in:
Dylan K. Taylor 2022-07-18 15:13:40 +01:00
parent 172bd9a129
commit 8660dfe576
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
17 changed files with 81 additions and 87 deletions

View File

@ -59,8 +59,8 @@ function buildWriterFunc(string $virtualTypeName, string $nativeTypeName, array
$lines = [];
$functionName = "write$virtualTypeName";
$lines[] = "public static function $functionName(RuntimeDataWriter \$w, \\$nativeTypeName \$value) : void{";
$lines[] = "\t\$w->writeInt($bits, match(\$value){";
$lines[] = "public function $functionName(\\$nativeTypeName \$value) : void{";
$lines[] = "\t\$this->writeInt($bits, match(\$value){";
foreach($memberNames as $key => $memberName){
$lines[] = "\t\t$memberName => $key,";
@ -84,8 +84,8 @@ function buildReaderFunc(string $virtualTypeName, string $nativeTypeName, array
$lines = [];
$functionName = "read$virtualTypeName";
$lines[] = "public static function $functionName(RuntimeDataReader \$r) : \\$nativeTypeName{";
$lines[] = "\treturn match(\$r->readInt($bits)){";
$lines[] = "public function $functionName() : \\$nativeTypeName{";
$lines[] = "\treturn match(\$this->readInt($bits)){";
foreach($memberNames as $key => $memberName){
$lines[] = "\t\t$key => $memberName,";
@ -165,8 +165,16 @@ $enumsUsed = [
PotionType::getAll()
];
$readerFuncs = [];
$writerFuncs = [];
$readerFuncs = [
"" => [
"abstract public function readInt(int \$bits) : int;"
]
];
$writerFuncs = [
"" => [
"abstract public function writeInt(int \$bits, int \$value) : void;"
]
];
$functionName = "";
foreach($enumsUsed as $enumMembers){
@ -220,14 +228,14 @@ namespace pocketmine\data\runtime;
HEADER;
echo "final class $className{\n\n";
echo "trait $className{\n\n";
echo implode("\n\n", array_map(fn(array $functionLines) => "\t" . implode("\n\t", $functionLines), $functions));
echo "\n\n}\n";
file_put_contents(dirname(__DIR__) . '/src/data/runtime/' . $className . '.php', ob_get_clean());
}
printFunctions($writerFuncs, "RuntimeEnumSerializer");
printFunctions($readerFuncs, "RuntimeEnumDeserializer");
printFunctions($writerFuncs, "RuntimeEnumSerializerTrait");
printFunctions($readerFuncs, "RuntimeEnumDeserializerTrait");
echo "Done. Don't forget to run CS fixup after generating code.\n";

View File

@ -29,8 +29,6 @@ use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumDeserializer;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -52,12 +50,12 @@ final class Bell extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{
$this->attachmentType = RuntimeEnumDeserializer::readBellAttachmentType($r);
$this->attachmentType = $r->readBellAttachmentType();
$this->facing = $r->readHorizontalFacing();
}
protected function encodeState(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeBellAttachmentType($w, $this->attachmentType);
$w->writeBellAttachmentType($this->attachmentType);
$w->writeHorizontalFacing($this->facing);
}

View File

@ -26,8 +26,6 @@ namespace pocketmine\block;
use pocketmine\block\utils\LeverFacing;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumDeserializer;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\item\Item;
use pocketmine\math\Axis;
use pocketmine\math\Facing;
@ -50,12 +48,12 @@ class Lever extends Flowable{
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{
$this->facing = RuntimeEnumDeserializer::readLeverFacing($r);
$this->facing = $r->readLeverFacing();
$this->activated = $r->readBool();
}
protected function encodeState(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeLeverFacing($w, $this->facing);
$w->writeLeverFacing($this->facing);
$w->writeBool($this->activated);
}

View File

@ -26,8 +26,6 @@ namespace pocketmine\block;
use pocketmine\block\utils\MushroomBlockType;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumDeserializer;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\item\Item;
use function mt_rand;
@ -42,11 +40,11 @@ class RedMushroomBlock extends Opaque{
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{
$this->mushroomBlockType = RuntimeEnumDeserializer::readMushroomBlockType($r);
$this->mushroomBlockType = $r->readMushroomBlockType();
}
protected function encodeState(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeMushroomBlockType($w, $this->mushroomBlockType);
$w->writeMushroomBlockType($this->mushroomBlockType);
}
public function getMushroomBlockType() : MushroomBlockType{ return $this->mushroomBlockType; }

View File

@ -28,8 +28,6 @@ use pocketmine\block\utils\SkullType;
use pocketmine\data\runtime\InvalidSerializedRuntimeDataException;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumDeserializer;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -56,11 +54,11 @@ class Skull extends Flowable{
public function getRequiredTypeDataBits() : int{ return 3; }
protected function decodeType(RuntimeDataReader $r) : void{
$this->skullType = RuntimeEnumDeserializer::readSkullType($r);
$this->skullType = $r->readSkullType();
}
protected function encodeType(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeSkullType($w, $this->skullType);
$w->writeSkullType($this->skullType);
}
public function getRequiredStateDataBits() : int{ return 3; }

View File

@ -27,8 +27,6 @@ use pocketmine\block\utils\SlabType;
use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumDeserializer;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -47,11 +45,11 @@ class Slab extends Transparent{
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(RuntimeDataReader $r) : void{
$this->slabType = RuntimeEnumDeserializer::readSlabType($r);
$this->slabType = $r->readSlabType();
}
protected function encodeState(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeSlabType($w, $this->slabType);
$w->writeSlabType($this->slabType);
}
public function isTransparent() : bool{

View File

@ -26,8 +26,6 @@ namespace pocketmine\block\utils;
use pocketmine\block\Block;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumDeserializer;
use pocketmine\data\runtime\RuntimeEnumSerializer;
trait ColoredTrait{
/** @var DyeColor */
@ -37,12 +35,12 @@ trait ColoredTrait{
/** @see Block::decodeType() */
protected function decodeType(RuntimeDataReader $r) : void{
$this->color = RuntimeEnumDeserializer::readDyeColor($r);
$this->color = $r->readDyeColor();
}
/** @see Block::encodeType() */
protected function encodeType(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeDyeColor($w, $this->color);
$w->writeDyeColor($this->color);
}
public function getColor() : DyeColor{ return $this->color; }

View File

@ -25,8 +25,6 @@ namespace pocketmine\block\utils;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumDeserializer;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\item\Axe;
use pocketmine\item\Item;
use pocketmine\item\ItemTypeIds;
@ -43,12 +41,12 @@ trait CopperTrait{
public function getRequiredTypeDataBits() : int{ return 3; }
protected function decodeType(RuntimeDataReader $r) : void{
$this->oxidation = RuntimeEnumDeserializer::readCopperOxidation($r);
$this->oxidation = $r->readCopperOxidation();
$this->waxed = $r->readBool();
}
protected function encodeType(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeCopperOxidation($w, $this->oxidation);
$w->writeCopperOxidation($this->oxidation);
$w->writeBool($this->waxed);
}

View File

@ -26,8 +26,6 @@ namespace pocketmine\block\utils;
use pocketmine\block\Block;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumDeserializer;
use pocketmine\data\runtime\RuntimeEnumSerializer;
trait CoralTypeTrait{
protected CoralType $coralType;
@ -37,13 +35,13 @@ trait CoralTypeTrait{
/** @see Block::decodeType() */
protected function decodeType(RuntimeDataReader $r) : void{
$this->coralType = RuntimeEnumDeserializer::readCoralType($r);
$this->coralType = $r->readCoralType();
$this->dead = $r->readBool();
}
/** @see Block::encodeType() */
protected function encodeType(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeCoralType($w, $this->coralType);
$w->writeCoralType($this->coralType);
$w->writeBool($this->dead);
}

View File

@ -29,6 +29,7 @@ use pocketmine\math\Facing;
use pocketmine\utils\AssumptionFailedError;
final class RuntimeDataReader{
use RuntimeEnumDeserializerTrait;
private int $offset = 0;

View File

@ -29,6 +29,7 @@ use pocketmine\math\Facing;
use pocketmine\utils\AssumptionFailedError;
final class RuntimeDataWriter{
use RuntimeEnumSerializerTrait;
private int $value = 0;
private int $offset = 0;

View File

@ -27,10 +27,12 @@ namespace pocketmine\data\runtime;
* This class is auto-generated. Do not edit it manually.
* @see build/generate-runtime-enum-serializers.php
*/
final class RuntimeEnumDeserializer{
trait RuntimeEnumDeserializerTrait{
public static function readBellAttachmentType(RuntimeDataReader $r) : \pocketmine\block\utils\BellAttachmentType{
return match($r->readInt(2)){
abstract public function readInt(int $bits) : int;
public function readBellAttachmentType() : \pocketmine\block\utils\BellAttachmentType{
return match($this->readInt(2)){
0 => \pocketmine\block\utils\BellAttachmentType::CEILING(),
1 => \pocketmine\block\utils\BellAttachmentType::FLOOR(),
2 => \pocketmine\block\utils\BellAttachmentType::ONE_WALL(),
@ -39,8 +41,8 @@ final class RuntimeEnumDeserializer{
};
}
public static function readCopperOxidation(RuntimeDataReader $r) : \pocketmine\block\utils\CopperOxidation{
return match($r->readInt(2)){
public function readCopperOxidation() : \pocketmine\block\utils\CopperOxidation{
return match($this->readInt(2)){
0 => \pocketmine\block\utils\CopperOxidation::EXPOSED(),
1 => \pocketmine\block\utils\CopperOxidation::NONE(),
2 => \pocketmine\block\utils\CopperOxidation::OXIDIZED(),
@ -49,8 +51,8 @@ final class RuntimeEnumDeserializer{
};
}
public static function readCoralType(RuntimeDataReader $r) : \pocketmine\block\utils\CoralType{
return match($r->readInt(3)){
public function readCoralType() : \pocketmine\block\utils\CoralType{
return match($this->readInt(3)){
0 => \pocketmine\block\utils\CoralType::BRAIN(),
1 => \pocketmine\block\utils\CoralType::BUBBLE(),
2 => \pocketmine\block\utils\CoralType::FIRE(),
@ -60,8 +62,8 @@ final class RuntimeEnumDeserializer{
};
}
public static function readDyeColor(RuntimeDataReader $r) : \pocketmine\block\utils\DyeColor{
return match($r->readInt(4)){
public function readDyeColor() : \pocketmine\block\utils\DyeColor{
return match($this->readInt(4)){
0 => \pocketmine\block\utils\DyeColor::BLACK(),
1 => \pocketmine\block\utils\DyeColor::BLUE(),
2 => \pocketmine\block\utils\DyeColor::BROWN(),
@ -82,8 +84,8 @@ final class RuntimeEnumDeserializer{
};
}
public static function readLeverFacing(RuntimeDataReader $r) : \pocketmine\block\utils\LeverFacing{
return match($r->readInt(3)){
public function readLeverFacing() : \pocketmine\block\utils\LeverFacing{
return match($this->readInt(3)){
0 => \pocketmine\block\utils\LeverFacing::DOWN_AXIS_X(),
1 => \pocketmine\block\utils\LeverFacing::DOWN_AXIS_Z(),
2 => \pocketmine\block\utils\LeverFacing::EAST(),
@ -96,8 +98,8 @@ final class RuntimeEnumDeserializer{
};
}
public static function readMushroomBlockType(RuntimeDataReader $r) : \pocketmine\block\utils\MushroomBlockType{
return match($r->readInt(4)){
public function readMushroomBlockType() : \pocketmine\block\utils\MushroomBlockType{
return match($this->readInt(4)){
0 => \pocketmine\block\utils\MushroomBlockType::ALL_CAP(),
1 => \pocketmine\block\utils\MushroomBlockType::CAP_EAST(),
2 => \pocketmine\block\utils\MushroomBlockType::CAP_MIDDLE(),
@ -113,8 +115,8 @@ final class RuntimeEnumDeserializer{
};
}
public static function readPotionType(RuntimeDataReader $r) : \pocketmine\item\PotionType{
return match($r->readInt(6)){
public function readPotionType() : \pocketmine\item\PotionType{
return match($this->readInt(6)){
0 => \pocketmine\item\PotionType::AWKWARD(),
1 => \pocketmine\item\PotionType::FIRE_RESISTANCE(),
2 => \pocketmine\item\PotionType::HARMING(),
@ -161,8 +163,8 @@ final class RuntimeEnumDeserializer{
};
}
public static function readSkullType(RuntimeDataReader $r) : \pocketmine\block\utils\SkullType{
return match($r->readInt(3)){
public function readSkullType() : \pocketmine\block\utils\SkullType{
return match($this->readInt(3)){
0 => \pocketmine\block\utils\SkullType::CREEPER(),
1 => \pocketmine\block\utils\SkullType::DRAGON(),
2 => \pocketmine\block\utils\SkullType::PLAYER(),
@ -173,8 +175,8 @@ final class RuntimeEnumDeserializer{
};
}
public static function readSlabType(RuntimeDataReader $r) : \pocketmine\block\utils\SlabType{
return match($r->readInt(2)){
public function readSlabType() : \pocketmine\block\utils\SlabType{
return match($this->readInt(2)){
0 => \pocketmine\block\utils\SlabType::BOTTOM(),
1 => \pocketmine\block\utils\SlabType::DOUBLE(),
2 => \pocketmine\block\utils\SlabType::TOP(),

View File

@ -27,10 +27,12 @@ namespace pocketmine\data\runtime;
* This class is auto-generated. Do not edit it manually.
* @see build/generate-runtime-enum-serializers.php
*/
final class RuntimeEnumSerializer{
trait RuntimeEnumSerializerTrait{
public static function writeBellAttachmentType(RuntimeDataWriter $w, \pocketmine\block\utils\BellAttachmentType $value) : void{
$w->writeInt(2, match($value){
abstract public function writeInt(int $bits, int $value) : void;
public function writeBellAttachmentType(\pocketmine\block\utils\BellAttachmentType $value) : void{
$this->writeInt(2, match($value){
\pocketmine\block\utils\BellAttachmentType::CEILING() => 0,
\pocketmine\block\utils\BellAttachmentType::FLOOR() => 1,
\pocketmine\block\utils\BellAttachmentType::ONE_WALL() => 2,
@ -39,8 +41,8 @@ final class RuntimeEnumSerializer{
});
}
public static function writeCopperOxidation(RuntimeDataWriter $w, \pocketmine\block\utils\CopperOxidation $value) : void{
$w->writeInt(2, match($value){
public function writeCopperOxidation(\pocketmine\block\utils\CopperOxidation $value) : void{
$this->writeInt(2, match($value){
\pocketmine\block\utils\CopperOxidation::EXPOSED() => 0,
\pocketmine\block\utils\CopperOxidation::NONE() => 1,
\pocketmine\block\utils\CopperOxidation::OXIDIZED() => 2,
@ -49,8 +51,8 @@ final class RuntimeEnumSerializer{
});
}
public static function writeCoralType(RuntimeDataWriter $w, \pocketmine\block\utils\CoralType $value) : void{
$w->writeInt(3, match($value){
public function writeCoralType(\pocketmine\block\utils\CoralType $value) : void{
$this->writeInt(3, match($value){
\pocketmine\block\utils\CoralType::BRAIN() => 0,
\pocketmine\block\utils\CoralType::BUBBLE() => 1,
\pocketmine\block\utils\CoralType::FIRE() => 2,
@ -60,8 +62,8 @@ final class RuntimeEnumSerializer{
});
}
public static function writeDyeColor(RuntimeDataWriter $w, \pocketmine\block\utils\DyeColor $value) : void{
$w->writeInt(4, match($value){
public function writeDyeColor(\pocketmine\block\utils\DyeColor $value) : void{
$this->writeInt(4, match($value){
\pocketmine\block\utils\DyeColor::BLACK() => 0,
\pocketmine\block\utils\DyeColor::BLUE() => 1,
\pocketmine\block\utils\DyeColor::BROWN() => 2,
@ -82,8 +84,8 @@ final class RuntimeEnumSerializer{
});
}
public static function writeLeverFacing(RuntimeDataWriter $w, \pocketmine\block\utils\LeverFacing $value) : void{
$w->writeInt(3, match($value){
public function writeLeverFacing(\pocketmine\block\utils\LeverFacing $value) : void{
$this->writeInt(3, match($value){
\pocketmine\block\utils\LeverFacing::DOWN_AXIS_X() => 0,
\pocketmine\block\utils\LeverFacing::DOWN_AXIS_Z() => 1,
\pocketmine\block\utils\LeverFacing::EAST() => 2,
@ -96,8 +98,8 @@ final class RuntimeEnumSerializer{
});
}
public static function writeMushroomBlockType(RuntimeDataWriter $w, \pocketmine\block\utils\MushroomBlockType $value) : void{
$w->writeInt(4, match($value){
public function writeMushroomBlockType(\pocketmine\block\utils\MushroomBlockType $value) : void{
$this->writeInt(4, match($value){
\pocketmine\block\utils\MushroomBlockType::ALL_CAP() => 0,
\pocketmine\block\utils\MushroomBlockType::CAP_EAST() => 1,
\pocketmine\block\utils\MushroomBlockType::CAP_MIDDLE() => 2,
@ -113,8 +115,8 @@ final class RuntimeEnumSerializer{
});
}
public static function writePotionType(RuntimeDataWriter $w, \pocketmine\item\PotionType $value) : void{
$w->writeInt(6, match($value){
public function writePotionType(\pocketmine\item\PotionType $value) : void{
$this->writeInt(6, match($value){
\pocketmine\item\PotionType::AWKWARD() => 0,
\pocketmine\item\PotionType::FIRE_RESISTANCE() => 1,
\pocketmine\item\PotionType::HARMING() => 2,
@ -161,8 +163,8 @@ final class RuntimeEnumSerializer{
});
}
public static function writeSkullType(RuntimeDataWriter $w, \pocketmine\block\utils\SkullType $value) : void{
$w->writeInt(3, match($value){
public function writeSkullType(\pocketmine\block\utils\SkullType $value) : void{
$this->writeInt(3, match($value){
\pocketmine\block\utils\SkullType::CREEPER() => 0,
\pocketmine\block\utils\SkullType::DRAGON() => 1,
\pocketmine\block\utils\SkullType::PLAYER() => 2,
@ -173,8 +175,8 @@ final class RuntimeEnumSerializer{
});
}
public static function writeSlabType(RuntimeDataWriter $w, \pocketmine\block\utils\SlabType $value) : void{
$w->writeInt(2, match($value){
public function writeSlabType(\pocketmine\block\utils\SlabType $value) : void{
$this->writeInt(2, match($value){
\pocketmine\block\utils\SlabType::BOTTOM() => 0,
\pocketmine\block\utils\SlabType::DOUBLE() => 1,
\pocketmine\block\utils\SlabType::TOP() => 2,

View File

@ -30,7 +30,6 @@ use pocketmine\block\utils\DyeColor;
use pocketmine\data\bedrock\BannerPatternTypeIdMap;
use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
@ -65,7 +64,7 @@ class Banner extends ItemBlockWallOrFloor{
}
protected function encodeType(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeDyeColor($w, $this->color);
$w->writeDyeColor($this->color);
}
/**

View File

@ -25,7 +25,6 @@ namespace pocketmine\item;
use pocketmine\block\utils\DyeColor;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumSerializer;
class Dye extends Item{
private DyeColor $color;
@ -36,7 +35,7 @@ class Dye extends Item{
}
protected function encodeType(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writeDyeColor($w, $this->color);
$w->writeDyeColor($this->color);
}
public function getColor() : DyeColor{

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\entity\Living;
use pocketmine\player\Player;
@ -38,7 +37,7 @@ class Potion extends Item implements ConsumableItem{
}
protected function encodeType(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writePotionType($w, $this->potionType);
$w->writePotionType($this->potionType);
}
public function getType() : PotionType{ return $this->potionType; }

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\data\runtime\RuntimeEnumSerializer;
use pocketmine\entity\Location;
use pocketmine\entity\projectile\SplashPotion as SplashPotionEntity;
use pocketmine\entity\projectile\Throwable;
@ -40,7 +39,7 @@ class SplashPotion extends ProjectileItem{
}
protected function encodeType(RuntimeDataWriter $w) : void{
RuntimeEnumSerializer::writePotionType($w, $this->potionType);
$w->writePotionType($this->potionType);
}
public function getType() : PotionType{ return $this->potionType; }