diff --git a/src/pocketmine/network/mcpe/handler/SimpleSessionHandler.php b/src/pocketmine/network/mcpe/handler/SimpleSessionHandler.php index 7376e61040..c04c7fabd5 100644 --- a/src/pocketmine/network/mcpe/handler/SimpleSessionHandler.php +++ b/src/pocketmine/network/mcpe/handler/SimpleSessionHandler.php @@ -391,7 +391,37 @@ class SimpleSessionHandler extends SessionHandler{ } public function handleModalFormResponse(ModalFormResponsePacket $packet) : bool{ - return $this->player->onFormSubmit($packet->formId, json_decode($packet->formData, true)); + return $this->player->onFormSubmit($packet->formId, self::stupid_json_decode($packet->formData, true)); + } + + /** + * Hack to work around a stupid bug in Minecraft W10 which causes empty strings to be sent unquoted in form responses. + * + * @param string $json + * @param bool $assoc + * + * @return mixed + */ + private static function stupid_json_decode(string $json, bool $assoc = false){ + if(preg_match('/^\[(.+)\]$/s', $json, $matches) > 0){ + $parts = preg_split('/(?:"(?:\\"|[^"])*"|)\K(,)/', $matches[1]); //Splits on commas not inside quotes, ignoring escaped quotes + foreach($parts as $k => $part){ + $part = trim($part); + if($part === ""){ + $part = "\"\""; + } + $parts[$k] = $part; + } + + $fixed = "[" . implode(",", $parts) . "]"; + if(($ret = json_decode($fixed, $assoc)) === null){ + throw new \InvalidArgumentException("Failed to fix JSON: " . json_last_error_msg() . "(original: $json, modified: $fixed)"); + } + + return $ret; + } + + return json_decode($json, $assoc); } public function handleServerSettingsRequest(ServerSettingsRequestPacket $packet) : bool{ diff --git a/tests/phpunit/network/mcpe/handler/StupidJsonDecodeTest.php b/tests/phpunit/network/mcpe/handler/StupidJsonDecodeTest.php new file mode 100644 index 0000000000..4082353a97 --- /dev/null +++ b/tests/phpunit/network/mcpe/handler/StupidJsonDecodeTest.php @@ -0,0 +1,56 @@ +setAccessible(true); + + $decoded = $func->invoke(null, $brokenJson, true); + self::assertEquals($expect, $decoded); + } +}