From 3baa5ab71214f96e6e7ab12cb9beef08118473b5 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 6 Jan 2023 00:41:57 +0000 Subject: [PATCH] InGamePacketHandler: removed obsolete workaround --- .../mcpe/handler/InGamePacketHandler.php | 57 ++-------------- .../mcpe/handler/StupidJsonDecodeTest.php | 68 ------------------- 2 files changed, 5 insertions(+), 120 deletions(-) delete mode 100644 tests/phpunit/network/mcpe/handler/StupidJsonDecodeTest.php diff --git a/src/network/mcpe/handler/InGamePacketHandler.php b/src/network/mcpe/handler/InGamePacketHandler.php index 7b0fbb88560..2c566fd51db 100644 --- a/src/network/mcpe/handler/InGamePacketHandler.php +++ b/src/network/mcpe/handler/InGamePacketHandler.php @@ -112,7 +112,6 @@ use function base64_encode; use function count; use function fmod; -use function implode; use function in_array; use function is_bool; use function is_infinite; @@ -122,12 +121,9 @@ use function max; use function mb_strlen; use function microtime; -use function preg_match; use function sprintf; use function strlen; use function strpos; -use function substr; -use function trim; use const JSON_THROW_ON_ERROR; /** @@ -875,57 +871,14 @@ public function handleModalFormResponse(ModalFormResponsePacket $packet) : bool{ //TODO: make APIs for this to allow plugins to use this information return $this->player->onFormSubmit($packet->formId, null); }elseif($packet->formData !== null){ - return $this->player->onFormSubmit($packet->formId, self::stupid_json_decode($packet->formData, true)); - }else{ - throw new PacketHandlingException("Expected either formData or cancelReason to be set in ModalFormResponsePacket"); - } - } - - /** - * Hack to work around a stupid bug in Minecraft W10 which causes empty strings to be sent unquoted in form responses. - * - * @return mixed - * @throws PacketHandlingException - */ - private static function stupid_json_decode(string $json, bool $assoc = false){ - if(preg_match('/^\[(.+)\]$/s', $json, $matches) > 0){ - $raw = $matches[1]; - $lastComma = -1; - $newParts = []; - $inQuotes = false; - for($i = 0, $len = strlen($raw); $i <= $len; ++$i){ - if($i === $len || ($raw[$i] === "," && !$inQuotes)){ - $part = substr($raw, $lastComma + 1, $i - ($lastComma + 1)); - if(trim($part) === ""){ //regular parts will have quotes or something else that makes them non-empty - $part = '""'; - } - $newParts[] = $part; - $lastComma = $i; - }elseif($raw[$i] === '"'){ - if(!$inQuotes){ - $inQuotes = true; - }else{ - $backslashes = 0; - for(; $backslashes < $i && $raw[$i - $backslashes - 1] === "\\"; ++$backslashes){} - if(($backslashes % 2) === 0){ //unescaped quote - $inQuotes = false; - } - } - } - } - - $fixed = "[" . implode(",", $newParts) . "]"; try{ - return json_decode($fixed, $assoc, self::MAX_FORM_RESPONSE_DEPTH, JSON_THROW_ON_ERROR); + $responseData = json_decode($packet->formData, true, self::MAX_FORM_RESPONSE_DEPTH, JSON_THROW_ON_ERROR); }catch(\JsonException $e){ - throw PacketHandlingException::wrap($e, "Failed to fix JSON (original: $json, modified: $fixed)"); + throw PacketHandlingException::wrap($e, "Failed to decode form response data"); } - } - - try{ - return json_decode($json, $assoc, self::MAX_FORM_RESPONSE_DEPTH, JSON_THROW_ON_ERROR); - }catch(\JsonException $e){ - throw PacketHandlingException::wrap($e); + return $this->player->onFormSubmit($packet->formId, $responseData); + }else{ + throw new PacketHandlingException("Expected either formData or cancelReason to be set in ModalFormResponsePacket"); } } diff --git a/tests/phpunit/network/mcpe/handler/StupidJsonDecodeTest.php b/tests/phpunit/network/mcpe/handler/StupidJsonDecodeTest.php deleted file mode 100644 index 6a48bd1badc..00000000000 --- a/tests/phpunit/network/mcpe/handler/StupidJsonDecodeTest.php +++ /dev/null @@ -1,68 +0,0 @@ -stupidJsonDecodeFunc = (new \ReflectionMethod(InGamePacketHandler::class, 'stupid_json_decode'))->getClosure(); - } - - /** - * @return mixed[][] - * @phpstan-return list - */ - public function stupidJsonDecodeProvider() : array{ - return [ - ["[\n \"a\",\"b,c,d,e\\\" \",,0,1,2, false, 0.001]", ['a', 'b,c,d,e" ', '', 0, 1, 2, false, 0.001]], - ["0", 0], - ["false", false], - ["null", null], - ['["\",,\"word","a\",,\"word2",]', ['",,"word', 'a",,"word2', '']], - ['["\",,\"word","a\",,\"word2",""]', ['",,"word', 'a",,"word2', '']], - ['["Hello,, PocketMine"]', ['Hello,, PocketMine']], - ['[,]', ['', '']], - ['[]', []] - ]; - } - - /** - * @dataProvider stupidJsonDecodeProvider - * - * @param mixed $expect - * - * @throws \ReflectionException - */ - public function testStupidJsonDecode(string $brokenJson, $expect) : void{ - $decoded = ($this->stupidJsonDecodeFunc)($brokenJson, true); - self::assertEquals($expect, $decoded); - } -}