Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pmmp/BedrockProtocol into fix/mov…
Browse files Browse the repository at this point in the history
…ement-correction-packet
  • Loading branch information
dktapps committed Aug 13, 2024
2 parents dc14d94 + 5db8d10 commit 5b1be13
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 49 deletions.
101 changes: 74 additions & 27 deletions src/PlayerAuthInputPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,54 @@ class PlayerAuthInputPacket extends DataPacket implements ServerboundPacket{
private float $analogMoveVecX;
private float $analogMoveVecZ;

/**
* @generate-create-func
* @param PlayerBlockAction[] $blockActions
*/
private static function internalCreate(
Vector3 $position,
float $pitch,
float $yaw,
float $headYaw,
float $moveVecX,
float $moveVecZ,
int $inputFlags,
int $inputMode,
int $playMode,
int $interactionMode,
?Vector3 $vrGazeDirection,
int $tick,
Vector3 $delta,
?ItemInteractionData $itemInteractionData,
?ItemStackRequest $itemStackRequest,
?array $blockActions,
?PlayerAuthInputVehicleInfo $vehicleInfo,
float $analogMoveVecX,
float $analogMoveVecZ,
) : self{
$result = new self;
$result->position = $position;
$result->pitch = $pitch;
$result->yaw = $yaw;
$result->headYaw = $headYaw;
$result->moveVecX = $moveVecX;
$result->moveVecZ = $moveVecZ;
$result->inputFlags = $inputFlags;
$result->inputMode = $inputMode;
$result->playMode = $playMode;
$result->interactionMode = $interactionMode;
$result->vrGazeDirection = $vrGazeDirection;
$result->tick = $tick;
$result->delta = $delta;
$result->itemInteractionData = $itemInteractionData;
$result->itemStackRequest = $itemStackRequest;
$result->blockActions = $blockActions;
$result->vehicleInfo = $vehicleInfo;
$result->analogMoveVecX = $analogMoveVecX;
$result->analogMoveVecZ = $analogMoveVecZ;
return $result;
}

/**
* @param int $inputFlags @see PlayerAuthInputFlags
* @param int $inputMode @see InputMode
Expand Down Expand Up @@ -86,43 +134,42 @@ public static function create(
//yuck, can we get a properly written packet just once? ...
throw new \InvalidArgumentException("Gaze direction must be provided for VR play mode");
}
$result = new self;
$result->position = $position->asVector3();
$result->pitch = $pitch;
$result->yaw = $yaw;
$result->headYaw = $headYaw;
$result->moveVecX = $moveVecX;
$result->moveVecZ = $moveVecZ;

$result->inputFlags = $inputFlags & ~((1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST) | (1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION) | (1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS));
$realInputFlags = $inputFlags & ~((1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST) | (1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION) | (1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS));
if($itemStackRequest !== null){
$result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST;
$realInputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_STACK_REQUEST;
}
if($itemInteractionData !== null){
$result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION;
$realInputFlags |= 1 << PlayerAuthInputFlags::PERFORM_ITEM_INTERACTION;
}
if($blockActions !== null){
$result->inputFlags |= 1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS;
$realInputFlags |= 1 << PlayerAuthInputFlags::PERFORM_BLOCK_ACTIONS;
}
if($vehicleInfo !== null){
$result->inputFlags |= 1 << PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE;
$realInputFlags |= 1 << PlayerAuthInputFlags::IN_CLIENT_PREDICTED_VEHICLE;
}

$result->inputMode = $inputMode;
$result->playMode = $playMode;
$result->interactionMode = $interactionMode;
if($vrGazeDirection !== null){
$result->vrGazeDirection = $vrGazeDirection->asVector3();
}
$result->tick = $tick;
$result->delta = $delta;
$result->itemInteractionData = $itemInteractionData;
$result->itemStackRequest = $itemStackRequest;
$result->blockActions = $blockActions;
$result->vehicleInfo = $vehicleInfo;
$result->analogMoveVecX = $analogMoveVecX;
$result->analogMoveVecZ = $analogMoveVecZ;
return $result;
return self::internalCreate(
$position,
$pitch,
$yaw,
$headYaw,
$moveVecX,
$moveVecZ,
$realInputFlags,
$inputMode,
$playMode,
$interactionMode,
$vrGazeDirection?->asVector3(),
$tick,
$delta,
$itemInteractionData,
$itemStackRequest,
$blockActions,
$vehicleInfo,
$analogMoveVecX,
$analogMoveVecZ
);
}

public function getPosition() : Vector3{
Expand Down
51 changes: 29 additions & 22 deletions tools/generate-create-static-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

require dirname(__DIR__) . '/vendor/autoload.php';

function generateCreateFunction(\ReflectionClass $reflect, int $indentLevel, int $modifiers) : array{
function generateCreateFunction(\ReflectionClass $reflect, int $indentLevel, int $modifiers, string $methodName) : array{
$properties = [];
$paramTags = [];
$longestParamType = 0;
Expand Down Expand Up @@ -73,7 +73,7 @@ function generateCreateFunction(\ReflectionClass $reflect, int $indentLevel, int
($modifiers & \ReflectionMethod::IS_PROTECTED) !== 0 => "protected",
default => throw new \InvalidArgumentException("Visibility must be a ReflectionMethod visibility constant")
};
$funcStart = "$visibilityStr static function create(";
$funcStart = "$visibilityStr static function $methodName(";
$funcEnd = ") : self{";
$params = [];
foreach($properties as $name => $reflectProperty){
Expand Down Expand Up @@ -130,27 +130,34 @@ function generateCreateFunction(\ReflectionClass $reflect, int $indentLevel, int
continue;
}
$reflect = new \ReflectionClass($className);
echo "Found class " . $reflect->getName() . ": ";
if(!$reflect->hasMethod("create")){
echo "skipped: no create() found\n";
continue;
}
$createReflect = $reflect->getMethod("create");
if($createReflect->getDeclaringClass()->getName() !== $reflect->getName()){
echo "skipped: create() declared by parent class\n";
continue;
}
$newContents = $contents;
$modified = [];
foreach($reflect->getMethods(\ReflectionMethod::IS_STATIC) as $method){
if($method->getDeclaringClass()->getName() !== $reflect->getName() || $method->isAbstract()){
continue;
}

$docComment = $createReflect->getDocComment();
if($docComment === false || preg_match('/@generate-create-func\s/', $docComment) !== 1){
echo "skipped: @generate-create-func not found in create() doc comment\n";
continue;
$docComment = $method->getDocComment();
if($docComment === false || preg_match('/@generate-create-func\s/', $docComment) !== 1){
continue;
}

$lines = preg_split("/(*ANYCRLF)\n/", $newContents);
$docCommentLines = preg_split("/(*ANYCRLF)\n/", $docComment);
$beforeLines = array_slice($lines, 0, $method->getStartLine() - 1 - count($docCommentLines));
$afterLines = array_slice($lines, $method->getEndLine());
$newContents = implode("\n", $beforeLines) . "\n" . implode("\n", generateCreateFunction($reflect, 1, $method->getModifiers(), $method->getName())) . "\n" . implode("\n", $afterLines);

$modified[] = $method->getName();
}

$lines = preg_split("/(*ANYCRLF)\n/", $contents);
$docCommentLines = preg_split("/(*ANYCRLF)\n/", $docComment);
$beforeLines = array_slice($lines, 0, $createReflect->getStartLine() - 1 - count($docCommentLines));
$afterLines = array_slice($lines, $createReflect->getEndLine());
file_put_contents($file, implode("\n", $beforeLines) . "\n" . implode("\n", generateCreateFunction($reflect, 1, $createReflect->getModifiers())) . "\n" . implode("\n", $afterLines));
echo "successfully patched class\n";
$shortName = substr($reflect->getName(), strlen("pocketmine\\network\\mcpe\\protocol\\"));
if($newContents !== $contents){
file_put_contents($file, $newContents);
echo "Successfully patched class $shortName: " . implode(", ", $modified) . "\n";
}elseif(count($modified) > 0){
echo "Already up to date class $shortName: " . implode(", ", $modified) . "\n";
}else{
echo "No functions found with @generate-create-func tag in class $shortName\n";
}
}

0 comments on commit 5b1be13

Please sign in to comment.