PocketMine-MP 5.39.3 git-21ae710729750cd637333d673bbbbbc598fc659e
Loading...
Searching...
No Matches
PacketHandlerInspector.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\handler;
25
30use function assert;
31use function implode;
32use function is_a;
33use function sort;
34use const SORT_STRING;
35
42
47 private static array $cache = [];
48
53 public static function getHandlerActions(PacketHandler $handler) : array{
54 if(isset(self::$cache[$handler::class])){
55 return self::$cache[$handler::class];
56 }
57
58 $whitelist = [];
59 $interface = new \ReflectionClass(PacketHandlerInterface::class);
60 $handlerReflect = new \ReflectionClass($handler);
61
62 foreach($interface->getMethods(\ReflectionMethod::IS_PUBLIC) as $handlerMethod){
63 try{
64 $implementation = $handlerReflect->getMethod($handlerMethod->getName());
65 }catch(\ReflectionException $e){
66 throw new AssumptionFailedError("PacketHandler implements PacketHandlerInterface, this should be impossible");
67 }
68
69 $packetArg = $implementation->getParameters()[0] ?? throw new AssumptionFailedError("PacketHandlerInterface method should always have a packet as the first argument");
70 $packetArgType = $packetArg->getType();
71 if(!$packetArgType instanceof \ReflectionNamedType){
72 continue;
73 }
74 $packetClass = $packetArgType->getName();
75 assert(is_a($packetClass, DataPacket::class, true));
76
77 $implementor = $implementation->getDeclaringClass()->getName();
78 $whitelist[$packetClass] = $implementor !== PacketHandler::class ? PacketHandlerAction::HANDLED : PacketHandlerAction::DISCARD_WITH_DEBUG;
79 }
80
81 foreach($handlerReflect->getAttributes(SilentDiscard::class) as $attribute){
82 $info = $attribute->newInstance();
83 $packetClass = $info->packetClass;
84 if(isset($whitelist[$packetClass]) && $whitelist[$packetClass] !== PacketHandlerAction::DISCARD_WITH_DEBUG){
85 $shortName = (new \ReflectionClass($packetClass))->getShortName();
86 \GlobalLogger::get()->warning("#[SilentDiscard($shortName)] has no effect on " . $handler::class . ", as the handler for $shortName is implemented");
87 continue;
88 }
89 $whitelist[$packetClass] = PacketHandlerAction::DISCARD_SILENT;
90 }
91
92 $allowedPackets = [];
93 foreach($whitelist as $packetClass => $action){
94 if($action === PacketHandlerAction::HANDLED){
95 $allowedPackets[] = (new \ReflectionClass($packetClass))->getShortName();
96 }
97 }
98 sort($allowedPackets, SORT_STRING);
99 \GlobalLogger::get()->debug("Packets handled by " . $handler::class . ": " . implode(', ', $allowedPackets));
100
101 return self::$cache[$handler::class] = $whitelist;
102 }
103}