6#include "ll/api/base/Macro.h"
7#include "ll/api/base/Meta.h"
8#include "ll/api/network/packet/PacketRegistrar.h"
9#include "ll/api/reflection/TypeName.h"
10#include "ll/api/utils/HashUtils.h"
11#include "mc/common/SubClientId.h"
12#include "mc/deps/raknet/PacketPriority.h"
13#include "mc/network/Compressibility.h"
14#include "mc/network/NetworkPeer.h"
15#include "mc/platform/Result.h"
29namespace ll::network {
36 PacketPriority mPriority;
37 NetworkPeer::Reliability mReliability;
38 SubClientId mSenderSubId;
39 Compressibility mCompressible;
41 static constexpr std::string_view CustomPacketId{};
43 static constexpr uint PacketVersion{0};
45 static constexpr PacketSide Side{PacketSide::Both};
47 static constexpr bool Optional{
false};
49 [[nodiscard]]
constexpr explicit Packet(
50 PacketPriority priority = PacketPriority::MediumPriority,
51 NetworkPeer::Reliability reliability = NetworkPeer::Reliability::ReliableOrdered,
52 SubClientId clientSubId = SubClientId::PrimaryClient,
55 : mPriority(priority),
56 mReliability(reliability),
57 mSenderSubId(clientSubId),
58 mCompressible(compress ? Compressibility::Compressible : Compressibility::Incompressible) {}
60 virtual ~Packet() =
default;
64 [[nodiscard]] virtual ::std::string_view getName()
const = 0;
66 [[nodiscard]] LLNDAPI
virtual PacketRuntimeId getRuntimeId()
const;
68 LLAPI
void sendTo(
Player const& player)
const;
72 LLAPI
void sendToClient(
NetworkIdentifier const& identifier, ::SubClientId clientId)
const;
75 LLAPI
void sendToClients()
const;
76 LLAPI
void sendToClients(
NetworkIdentifier const& exceptId, ::SubClientId exceptSubid)
const;
78 LLAPI
void sendToServer()
const;
80 LLNDAPI
bool canSendTo(
Player const& player)
const;
84 LLNDAPI
bool canSendToServer()
const;
86 LLNDAPI
static bool canSendTo(PacketRuntimeId runtimeId,
Player const& player);
88 LLNDAPI
static bool canSendTo(PacketRuntimeId runtimeId,
NetworkIdentifier const& identifier);
90 LLNDAPI
static bool canSendToServer(PacketRuntimeId runtimeId);
105constexpr std::string_view getPacketId = []() -> std::string_view {
106 using self = std::remove_cvref_t<T>;
107 if constexpr (!self::CustomPacketId.empty()) {
108 return self::CustomPacketId;
110 return reflection::type_unprefix_name_v<self>;
114[[nodiscard]]
constexpr PacketRuntimeId makePacketRuntimeId(std::string_view
id, uint version = 0) {
115 auto hash = hash_utils::doHash(
id);
116 return version == 0 ? hash : hash_utils::HashCombiner{hash}.add(version).hash();
120constexpr PacketRuntimeId getPacketRuntimeId =
121 makePacketRuntimeId(getPacketId<T>, std::remove_cvref_t<T>::PacketVersion);
124template <
class Derived>
125inline bool packetRegistration = [] {
126 PacketRegistrar::getInstance().registerPacket(
127 getPacketId<Derived>,
128 getPacketRuntimeId<Derived>,
129 []() -> std::unique_ptr<Packet> {
return std::make_unique<Derived>(); },
136template <
class Derived,
class PacketType, PacketS
ide S
ide>
137inline bool packetHandlerRegistration = [] {
138 static Derived instance;
139 PacketRegistrar::getInstance()
140 .registerHandler(getPacketId<PacketType>, getPacketRuntimeId<PacketType>, instance, Side);
145template <
class Derived>
147 static bool ensureRegistered() {
return detail::packetRegistration<Derived>; }
150 using Packet::Packet;
153 [[nodiscard]] ::std::string_view getName()
const override {
return getPacketId<Derived>; }
154 [[nodiscard]] PacketRuntimeId getRuntimeId()
const override {
return getPacketRuntimeId<Derived>; }
156 [[nodiscard]]
static bool canSendTo(
Player const& player) {
157 return Packet::canSendTo(getPacketRuntimeId<Derived>, player);
161 return Packet::canSendTo(getPacketRuntimeId<Derived>, identifier);
164 [[nodiscard]]
static bool canSendToServer() {
return Packet::canSendToServer(getPacketRuntimeId<Derived>); }
166 inline static bool sRegistered = ensureRegistered();
169template <
class Derived,
class PacketType, PacketS
ide S
ide = PacketType::S
ide>
173 static bool ensureRegistered() {
return detail::packetHandlerRegistration<Derived, PacketType, Side>; }
175 inline static bool sRegistered = ensureRegistered();
184 static_cast<Derived const*
>(
this)->handlePacket(netId, callback, side,
static_cast<PacketType const&
>(packet));
Definition BinaryStream.h:10
Definition IPacketHandlerDispatcher.h:12
Definition NetEventCallback.h:273
Definition NetworkIdentifier.h:10
Definition ReadOnlyBinaryStream.h:8
Definition UserEntityIdentifierComponent.h:17
Definition PacketRegistrar.h:29
Definition optional_ref.h:10
Definition DimensionType.h:5
Definition NetworkIdentifierWithSubId.h:10