Skip to content

Commit

Permalink
feat: add new obj serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 3, 2025
1 parent 608d6f9 commit f948a8d
Show file tree
Hide file tree
Showing 24 changed files with 128 additions and 130 deletions.
5 changes: 3 additions & 2 deletions src-server/ll/api/event/player/PlayerConnectEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ll/api/event/player/PlayerConnectEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/EventRefObjSerializer.h"
#include "ll/api/memory/Hook.h"

#include "mc/network/ServerNetworkHandler.h"
Expand All @@ -10,8 +11,8 @@ namespace ll::event::inline player {

void PlayerConnectEvent::serialize(CompoundTag& nbt) const {
Cancellable::serialize(nbt);
nbt["networkIdentifier"] = (uintptr_t)&networkIdentifier();
nbt["connectionRequest"] = (uintptr_t)&connectionRequest();
nbt["networkIdentifier"] = serializeRefObj(networkIdentifier());
nbt["connectionRequest"] = serializeRefObj(connectionRequest());
}

NetworkIdentifier const& PlayerConnectEvent::networkIdentifier() const { return mNetworkIdentifier; }
Expand Down
3 changes: 2 additions & 1 deletion src-server/ll/api/event/server/ServerStartedEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ll/api/event/server/ServerStartedEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/EventRefObjSerializer.h"
#include "ll/api/memory/Hook.h"

#include "mc/world/events/ServerInstanceEventCoordinator.h"
Expand All @@ -10,7 +11,7 @@ namespace ll::event::inline server {

void ServerStartedEvent::serialize(CompoundTag& nbt) const {
Event::serialize(nbt);
nbt["server"] = (uintptr_t)&server();
nbt["server"] = serializeRefObj(server());
}

ServerInstance& ServerStartedEvent::server() const { return mInstance; }
Expand Down
3 changes: 2 additions & 1 deletion src-server/ll/api/event/server/ServerStoppingEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ll/api/event/server/ServerStoppingEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/EventRefObjSerializer.h"
#include "ll/api/memory/Hook.h"

#include "mc/nbt/CompoundTag.h"
Expand All @@ -8,7 +9,7 @@ namespace ll::event::inline server {

void ServerStoppingEvent::serialize(CompoundTag& nbt) const {
Event::serialize(nbt);
nbt["server"] = (uintptr_t)&server();
nbt["server"] = serializeRefObj(server());
}

ServerInstance& ServerStoppingEvent::server() const { return mServer; }
Expand Down
136 changes: 33 additions & 103 deletions src-test/server/EventTest.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
#include "ll/api/event/Cancellable.h"
#include "ll/api/event/EventBus.h"
#include "ll/api/io/PatternFormatter.h"
#include "ll/api/io/Sink.h"
#include "ll/api/memory/Hook.h"
#include "ll/api/thread/ServerThreadExecutor.h"
#include "ll/api/utils/ErrorUtils.h"
#include "ll/api/utils/StringUtils.h"
#include "ll/core/LeviLamina.h"
#include "mc/network/packet/TextPacket.h"
#include "mc/server/ServerInstance.h"
#include "mc/world/events/ServerInstanceEventCoordinator.h"

#include "ll/api/event/DynamicListener.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/MultiListener.h"
#include "ll/api/event/command/ExecuteCommandEvent.h"
#include "ll/api/event/entity/ActorHurtEvent.h"
#include "ll/api/event/player/PlayerAddExperienceEvent.h"
#include "ll/api/event/player/PlayerAttackEvent.h"
#include "ll/api/event/player/PlayerConnectEvent.h"
#include "ll/api/event/player/PlayerDestroyBlockEvent.h"
#include "ll/api/event/player/PlayerDieEvent.h"
#include "ll/api/event/player/PlayerDisconnectEvent.h"
#include "ll/api/event/player/PlayerInteractBlockEvent.h"
#include "ll/api/event/player/PlayerJoinEvent.h"
#include "ll/api/event/player/PlayerJumpEvent.h"
#include "ll/api/event/player/PlayerPickUpItemEvent.h"
#include "ll/api/event/player/PlayerPlaceBlockEvent.h"
#include "ll/api/event/player/PlayerRespawnEvent.h"
#include "ll/api/event/player/PlayerSneakEvent.h"
#include "ll/api/event/player/PlayerSprintEvent.h"
#include "ll/api/event/player/PlayerSwingEvent.h"
#include "ll/api/event/player/PlayerUseItemEvent.h"
#include "ll/api/event/world/BlockChangedEvent.h"
#include "ll/api/event/world/SpawnMobEvent.h"
#include "ll/api/io/FileUtils.h"
#include "mc/deps/core/utility/MCRESULT.h"
#include "mc/nbt/CompoundTag.h"
Expand Down Expand Up @@ -132,88 +117,33 @@ LL_AUTO_TYPE_INSTANCE_HOOK(

using namespace ll::event;

bus.emplaceListener<ExecutingCommandEvent>([](ExecutingCommandEvent& ev) {
ll::getLogger().debug("ExecutingCommandEvent: {}", ev.commandContext().mCommand);
ll::getLogger().debug("origin: {}", ev.commandContext().mOrigin->serialize().toSnbt());
});
bus.emplaceListener<ExecutedCommandEvent>([](ExecutedCommandEvent& ev) {
ll::getLogger().debug("ExecutedCommandEvent: {}", ev.commandContext().mCommand);
ll::getLogger().debug("result: {}", ev.result().getFullCode());
});
bus.emplaceListener<PlayerConnectEvent>([](PlayerConnectEvent& ev) {
ll::getLogger().debug("Player connect: {} {}", ev.self().getRealName(), ev.self().getIPAndPort());
});
bus.emplaceListener<PlayerJoinEvent>([](PlayerJoinEvent& ev) {
ll::getLogger().debug("Player join: {} {}", ev.self().getRealName(), ev.self().getLocaleCode());
});
bus.emplaceListener<PlayerDisconnectEvent>([](PlayerDisconnectEvent& ev) {
ll::getLogger().debug("Player leave: {}", ev.self().getRealName());
});
bus.emplaceListener<PlayerAttackEvent>([](PlayerAttackEvent& ev) {
ll::getLogger().debug(
"Player {} attack {} cause {}",
ev.self().getRealName(),
ev.target().getTypeName(),
magic_enum::enum_name(ev.cause())
);
});
bus.emplaceListener<PlayerDieEvent>([](PlayerDieEvent& ev) {
ll::getLogger()
.debug("Player {} died source {}", ev.self().getRealName(), magic_enum::enum_name(ev.source().getCause()));
});
bus.emplaceListener<PlayerRespawnEvent>([](PlayerRespawnEvent& ev) {
ll::getLogger().debug("Player {} respawned", ev.self().getRealName());
});
bus.emplaceListener<PlayerJumpEvent>([](PlayerJumpEvent& ev) {
ll::getLogger().debug("Player {} jumped", ev.self().getRealName());
});
bus.emplaceListener<PlayerAddExperienceEvent>([](PlayerAddExperienceEvent& ev) {
ll::getLogger().debug("Player {} add experience {}", ev.self().getRealName(), ev.experience());
if (ev.experience() == 114514) {
ev.cancel();
}
});
bus.emplaceListener<PlayerPickUpItemEvent>([](PlayerPickUpItemEvent& ev) {
ll::getLogger().debug("Player {} take {}", ev.self().getRealName(), ev.itemActor().item().getTypeName());
});
bus.emplaceListener<PlayerSwingEvent>([](PlayerSwingEvent& ev) {
ll::getLogger().debug("Player {} left click", ev.self().getRealName());
});
auto listenersp = Listener<PlayerSprintEvent>::create([](PlayerSprintEvent& ev) {
switch (ev.getId().hash) {
case ll::event::getEventId<PlayerSprintingEvent>.hash: {
ll::getLogger().debug("Player {} start sprint", ev.self().getRealName());
} break;
case ll::event::getEventId<PlayerSprintedEvent>.hash: {
ll::getLogger().debug("Player {} stop sprint", ev.self().getRealName());
} break;
default:
break;
std::vector<std::pair<std::string, EventId>> ids;
for (auto [modName, id] : bus.events()) {
ids.emplace_back(std::string{modName}, id);
}
for (auto [modName, id] : ids) {
if (!id.name.ends_with("::LevelTickEvent"))
bus.addListener(
DynamicListener::create([modName](CompoundTag& nbt) {
ll::getLogger().debug("event from {}, {}", modName, nbt.toSnbt(SnbtFormat::PrettyChatPrint));
}),
id
);
}

class BroadcastSink : public ll::io::Sink {
public:
BroadcastSink() : Sink(ll::makePolymorphic<ll::io::PatternFormatter>("<{tit}|{lvl}> [{tm:%T}] {msg}")) {}

void append(ll::io::LogMessageView const& view) override {
std::string buffer;
formatter->format(view, buffer);
ll::thread::ServerThreadExecutor::getDefault().execute(
[pkt = TextPacket::createRawMessage(ll::string_utils::replaceAnsiToMcCode(buffer))] {
pkt.sendToClients();
}
);
}
});
bus.addListener<PlayerSprintingEvent>(listenersp);
bus.addListener<PlayerSprintedEvent>(listenersp);
auto mul = MultiListener<PlayerSneakingEvent, PlayerSneakedEvent>::create([](auto&& ev) {
ll::getLogger()
.debug("Player {} MultiListener of {}", ev.self().getRealName(), ll::reflection::type_raw_name_v<decltype(ev)>);
});
bus.addListener(mul);

auto dl = DynamicListener::create([](CompoundTag& nbt) {
// nbt["cancelled"] = true;
ll::getLogger().debug("{}", nbt.toSnbt(SnbtFormat::PrettyConsolePrint));
});


bus.addListener(dl, getEventId<PlayerUseItemEvent>);
bus.addListener(dl, getEventId<PlayerInteractBlockEvent>);
bus.addListener(dl, getEventId<ActorHurtEvent>);
bus.addListener(dl, getEventId<PlayerDestroyBlockEvent>);
bus.addListener(dl, getEventId<PlayerPlacingBlockEvent>);
bus.addListener(dl, getEventId<SpawnedMobEvent>);

bus.emplaceListener<BlockChangedEvent>([](BlockChangedEvent& ev) {
ll::getLogger()
.debug("Block Changed Pos: {} Dimension: {}", ev.pos().toString(), ev.blockSource().getDimensionId().id);
});
};
ll::getLogger().addSink(std::make_shared<BroadcastSink>());
}
2 changes: 1 addition & 1 deletion src/ll/api/event/Cancellable.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Cancellable : public T {
Event::serializeWithCancel(nbt);
}
void deserialize(CompoundTag const& nbt) override {
T::deserialize(nbt);
Event::deserializeWithCancel(nbt);
T::deserialize(nbt);
}

[[nodiscard]] constexpr bool isCancelled() { return Event::mCancelled; }
Expand Down
4 changes: 2 additions & 2 deletions src/ll/api/event/EventId.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "ll/api/reflection/Reflection.h"
#include "ll/api/reflection/TypeName.h"
#include "ll/api/utils/HashUtils.h"

namespace ll::event {
Expand Down Expand Up @@ -34,7 +34,7 @@ constexpr EventIdView getEventId = []() -> EventIdView {
return self::CustomEventId;
} else {
static_assert(std::is_final_v<self>, "Only final classes can use getEventId");
return EventIdView{ll::reflection::type_unprefix_name_v<self>};
return EventIdView{reflection::type_unprefix_name_v<self>};
}
}();
} // namespace ll::event
31 changes: 31 additions & 0 deletions src/ll/api/event/EventRefObjSerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "ll/api/reflection/TypeName.h"
#include "mc/nbt/CompoundTag.h"

namespace ll::event {
template <class T>
[[nodiscard]] inline ::CompoundTag serializeRefObj(T& obj) {
return ::CompoundTag{
{"_nullable_", false},
{ "_pointer_", (uintptr_t)std::addressof(obj)},
{ "_type_", reflection::type_unprefix_name_v<std::remove_cvref_t<T>>},
{ "_isconst_", std::is_const_v<T>},
};
}
template <class T>
[[nodiscard]] inline ::CompoundTag serializePtrObj(T* ptr) {
return ::CompoundTag{
{"_nullable_", true},
{ "_pointer_", (uintptr_t)ptr},
{ "_type_", reflection::type_unprefix_name_v<std::remove_cvref_t<T>>},
{ "_isconst_", std::is_const_v<T>},
};
}
[[nodiscard]] inline bool isEventSerializedObj(::CompoundTag const& tag) {
return tag.contains("_type_") && tag.contains("_pointer_");
}
[[nodiscard]] inline bool isEventSerializedObj(::CompoundTagVariant const& tag) {
return tag.is_object() ? isEventSerializedObj(tag.get<::CompoundTag>()) : false;
}
} // namespace ll::event
10 changes: 7 additions & 3 deletions src/ll/api/event/command/ExecuteCommandEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ll/api/event/command/ExecuteCommandEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/EventRefObjSerializer.h"
#include "ll/api/memory/Hook.h"

#include "mc/common/Globals.h"
Expand All @@ -9,8 +10,8 @@ namespace ll::event::inline command {

void ExecuteCommandEvent::serialize(CompoundTag& nbt) const {
Event::serialize(nbt);
nbt["minecraftCommands"] = (uintptr_t)&minecraftCommands();
nbt["commandContext"] = (uintptr_t)&commandContext();
nbt["minecraftCommands"] = serializeRefObj(minecraftCommands());
nbt["commandContext"] = serializeRefObj(commandContext());
nbt["suppressOutput"] = suppressOutput();
}
void ExecutingCommandEvent::deserialize(CompoundTag const& nbt) {
Expand All @@ -19,7 +20,10 @@ void ExecutingCommandEvent::deserialize(CompoundTag const& nbt) {
}
void ExecutedCommandEvent::serialize(CompoundTag& nbt) const {
ExecuteCommandEvent::serialize(nbt);
nbt["result"] = (uintptr_t)&result();
nbt["result"] = CompoundTag{
{"success", result().mSuccess},
{ "code", result().mCode},
};
}

MinecraftCommands& ExecuteCommandEvent::minecraftCommands() const { return mMinecraftCommands; }
Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/event/entity/ActorEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "ll/api/event/entity/ActorEvent.h"
#include "ll/api/event/EventRefObjSerializer.h"

#include "mc/nbt/CompoundTag.h"

namespace ll::event::inline entity {

void ActorEvent::serialize(CompoundTag& nbt) const {
Event::serialize(nbt);
nbt["self"] = (uintptr_t)&self();
nbt["self"] = serializeRefObj(self());
}

Actor& ActorEvent::self() const { return mSelf; }
Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/event/entity/ActorHurtEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ll/api/event/entity/ActorHurtEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/EventRefObjSerializer.h"
#include "ll/api/memory/Hook.h"

#include "mc/nbt/CompoundTag.h"
Expand All @@ -8,7 +9,7 @@ namespace ll::event::inline entity {

void ActorHurtEvent::serialize(CompoundTag& nbt) const {
Cancellable::serialize(nbt);
nbt["source"] = (uintptr_t)&source();
nbt["source"] = serializeRefObj(source());
nbt["damage"] = damage();
nbt["knock"] = knock();
nbt["ignite"] = ignite();
Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/event/entity/MobDieEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ll/api/event/entity/MobDieEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/EventRefObjSerializer.h"
#include "ll/api/memory/Hook.h"

#include "mc/nbt/CompoundTag.h"
Expand All @@ -8,7 +9,7 @@ namespace ll::event::inline entity {

void MobDieEvent::serialize(CompoundTag& nbt) const {
MobEvent::serialize(nbt);
nbt["source"] = (uintptr_t)&source();
nbt["source"] = serializeRefObj(source());
}

ActorDamageSource const& MobDieEvent::source() const { return mSource; }
Expand Down
8 changes: 8 additions & 0 deletions src/ll/api/event/entity/MobEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#include "ll/api/event/entity/MobEvent.h"
#include "ll/api/event/EventRefObjSerializer.h"

#include "mc/nbt/CompoundTag.h"

namespace ll::event::inline entity {

void MobEvent::serialize(CompoundTag& nbt) const {
ActorEvent::serialize(nbt);
nbt["self"] = serializeRefObj(self());
}

Mob& MobEvent::self() const { return static_cast<Mob&>(ActorEvent::self()); }

} // namespace ll::event::inline entity
2 changes: 2 additions & 0 deletions src/ll/api/event/entity/MobEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class MobEvent : public ActorEvent {
constexpr explicit MobEvent(Mob& mob) : ActorEvent(mob) {}

public:
LLAPI void serialize(CompoundTag&) const override;

LLNDAPI Mob& self() const;
};
} // namespace ll::event::inline entity
3 changes: 2 additions & 1 deletion src/ll/api/event/player/PlayerAttackEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ll/api/event/player/PlayerAttackEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/EventRefObjSerializer.h"
#include "ll/api/memory/Hook.h"

#include "mc/nbt/CompoundTag.h"
Expand All @@ -8,7 +9,7 @@ namespace ll::event::inline player {

void PlayerAttackEvent::serialize(CompoundTag& nbt) const {
Cancellable::serialize(nbt);
nbt["target"] = (uintptr_t)&target();
nbt["target"] = serializeRefObj(target());
nbt["cause"] = magic_enum::enum_name(cause());
}

Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/event/player/PlayerDieEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ll/api/event/player/PlayerDieEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/event/EventRefObjSerializer.h"
#include "ll/api/memory/Hook.h"

#include "mc/nbt/CompoundTag.h"
Expand All @@ -8,7 +9,7 @@ namespace ll::event::inline player {

void PlayerDieEvent::serialize(CompoundTag& nbt) const {
PlayerEvent::serialize(nbt);
nbt["source"] = (uintptr_t)&source();
nbt["source"] = serializeRefObj(source());
}

ActorDamageSource const& PlayerDieEvent::source() const { return mSource; }
Expand Down
Loading

0 comments on commit f948a8d

Please sign in to comment.