From bc14660e55e4726b7b6bfb7e4d2ffea8d2618b55 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 3 Feb 2021 23:37:41 +0000 Subject: [PATCH 1/4] Added missing ItemStackRequest protocol changes --- .../CraftRecipeOptionalStackRequestAction.php | 59 +++++++++++++++++++ .../stackrequest/ItemStackRequest.php | 21 ++++++- .../ItemStackRequestActionType.php | 5 +- 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/CraftRecipeOptionalStackRequestAction.php diff --git a/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/CraftRecipeOptionalStackRequestAction.php b/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/CraftRecipeOptionalStackRequestAction.php new file mode 100644 index 000000000..9047947c8 --- /dev/null +++ b/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/CraftRecipeOptionalStackRequestAction.php @@ -0,0 +1,59 @@ +recipeId = $type; + $this->filterStringIndex = $filterStringIndex; + } + + public function getRecipeId() : int{ return $this->recipeId; } + + public function getFilterStringIndex() : int{ return $this->filterStringIndex; } + + public static function getTypeId() : int{ return ItemStackRequestActionType::CRAFTING_RECIPE_OPTIONAL; } + + public static function read(NetworkBinaryStream $in) : self{ + $recipeId = $in->readGenericTypeNetworkId(); + $filterStringIndex = $in->getLInt(); + return new self($recipeId, $filterStringIndex); + } + + public function write(NetworkBinaryStream $out) : void{ + $out->writeGenericTypeNetworkId($this->recipeId); + $out->putLInt($this->filterStringIndex); + } +} diff --git a/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequest.php b/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequest.php index 648eb295a..688b8fa94 100644 --- a/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequest.php +++ b/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequest.php @@ -32,13 +32,21 @@ final class ItemStackRequest{ private $requestId; /** @var ItemStackRequestAction[] */ private $actions; + /** + * @var string[] + * @phpstan-var list + */ + private $filterStrings; /** * @param ItemStackRequestAction[] $actions + * @param string[] $filterStrings + * @phpstan-param list $filterStrings */ - public function __construct(int $requestId, array $actions){ + public function __construct(int $requestId, array $actions, array $filterStrings){ $this->requestId = $requestId; $this->actions = $actions; + $this->filterStrings = $filterStrings; } public function getRequestId() : int{ return $this->requestId; } @@ -60,6 +68,7 @@ final class ItemStackRequest{ case CraftRecipeStackRequestAction::getTypeId(): return CraftRecipeStackRequestAction::read($in); case CraftRecipeAutoStackRequestAction::getTypeId(): return CraftRecipeAutoStackRequestAction::read($in); case CreativeCreateStackRequestAction::getTypeId(): return CreativeCreateStackRequestAction::read($in); + case CraftRecipeOptionalStackRequestAction::getTypeId(): return CraftRecipeOptionalStackRequestAction::read($in); case DeprecatedCraftingNonImplementedStackRequestAction::getTypeId(): return DeprecatedCraftingNonImplementedStackRequestAction::read($in); case DeprecatedCraftingResultsStackRequestAction::getTypeId(): return DeprecatedCraftingResultsStackRequestAction::read($in); } @@ -73,7 +82,11 @@ final class ItemStackRequest{ $typeId = $in->getByte(); $actions[] = self::readAction($in, $typeId); } - return new self($requestId, $actions); + $filterStrings = []; + for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ + $filterStrings[] = $in->getString(); + } + return new self($requestId, $actions, $filterStrings); } public function write(NetworkBinaryStream $out) : void{ @@ -83,5 +96,9 @@ final class ItemStackRequest{ $out->putByte($action::getTypeId()); $action->write($out); } + $out->putUnsignedVarInt(count($this->filterStrings)); + foreach($this->filterStrings as $string){ + $out->putString($string); + } } } diff --git a/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequestActionType.php b/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequestActionType.php index 08816c70b..7dea5c210 100644 --- a/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequestActionType.php +++ b/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequestActionType.php @@ -41,6 +41,7 @@ final class ItemStackRequestActionType{ public const CRAFTING_RECIPE = 9; public const CRAFTING_RECIPE_AUTO = 10; //recipe book? public const CREATIVE_CREATE = 11; - public const CRAFTING_NON_IMPLEMENTED_DEPRECATED_ASK_TY_LAING = 12; //anvils aren't fully implemented yet - public const CRAFTING_RESULTS_DEPRECATED_ASK_TY_LAING = 13; //no idea what this is for + public const CRAFTING_RECIPE_OPTIONAL = 12; //anvil/cartography table rename + public const CRAFTING_NON_IMPLEMENTED_DEPRECATED_ASK_TY_LAING = 13; + public const CRAFTING_RESULTS_DEPRECATED_ASK_TY_LAING = 14; //no idea what this is for } From 317a48d9b09f4df0da45f82581ae716a77b0bdd8 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 3 Feb 2021 23:40:34 +0000 Subject: [PATCH 2/4] ItemStackRequest: expose filterStrings --- .../types/inventory/stackrequest/ItemStackRequest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequest.php b/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequest.php index 688b8fa94..16e3f779f 100644 --- a/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequest.php +++ b/src/pocketmine/network/mcpe/protocol/types/inventory/stackrequest/ItemStackRequest.php @@ -54,6 +54,12 @@ final class ItemStackRequest{ /** @return ItemStackRequestAction[] */ public function getActions() : array{ return $this->actions; } + /** + * @return string[] + * @phpstan-return list + */ + public function getFilterStrings() : array{ return $this->filterStrings; } + private static function readAction(NetworkBinaryStream $in, int $typeId) : ItemStackRequestAction{ switch($typeId){ case TakeStackRequestAction::getTypeId(): return TakeStackRequestAction::read($in); From eaf3a869817d328cbff880843ced6b9b71ae0484 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 4 Feb 2021 15:03:14 +0000 Subject: [PATCH 3/4] MainLogger: fixed UB in writeLogStream() notify() has to be used inside a synchronized block. --- src/pocketmine/utils/MainLogger.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index 5014c20e2..adf4c61ff 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -347,10 +347,12 @@ class MainLogger extends \AttachableThreadedLogger{ fwrite($logResource, $chunk); } - if($this->syncFlush){ - $this->syncFlush = false; - $this->notify(); //if this was due to a sync flush, tell the caller to stop waiting - } + $this->synchronized(function() : void{ + if($this->syncFlush){ + $this->syncFlush = false; + $this->notify(); //if this was due to a sync flush, tell the caller to stop waiting + } + }); } /** From 8ef1e54e200a2d9184bf98f114ed11463f0e47c8 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 4 Feb 2021 15:07:40 +0000 Subject: [PATCH 4/4] MainLogger: fixing CPU waste on logger thread this doesn't need to keep spinning every 25ms; it can just wake up when there's actually log messages to write into the buffer. --- src/pocketmine/utils/MainLogger.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index adf4c61ff..ce97ac373 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -321,6 +321,7 @@ class MainLogger extends \AttachableThreadedLogger{ } $this->logStream[] = $time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL; + $this->notify(); }); } @@ -367,7 +368,7 @@ class MainLogger extends \AttachableThreadedLogger{ while(!$this->shutdown){ $this->writeLogStream($logResource); $this->synchronized(function() : void{ - $this->wait(25000); + $this->wait(); }); }