From 5357005edbc1dccb6984a32f9a9f11092cd676c8 Mon Sep 17 00:00:00 2001 From: OEOTYAN <1124718975@qq.com> Date: Wed, 6 Dec 2023 00:26:01 +0800 Subject: [PATCH] fix: fix entt traits --- src/ll/api/base/Random.h | 2 +- src/ll/test/EventTest.cpp | 24 +++++++++---- src/ll/test/TestDynamicCommand.cpp | 38 +++++++++++--------- src/mc/entity/EntityId.h | 8 ++++- src/mc/entity/EntityIdTraits.h | 1 - src/mc/nbt/CompoundTagVariant.h | 54 +++++++++++++--------------- src/mc/nbt/SnbtDumpImpl.cpp | 2 +- src/mc/nbt/SnbtParseImpl.cpp | 4 +-- src/mc/server/SimulatedPlayer.cpp | 6 ++-- src/mc/world/actor/player/Player.cpp | 10 +++--- 10 files changed, 84 insertions(+), 65 deletions(-) diff --git a/src/ll/api/base/Random.h b/src/ll/api/base/Random.h index 545ec61c91..c899767936 100644 --- a/src/ll/api/base/Random.h +++ b/src/ll/api/base/Random.h @@ -46,7 +46,7 @@ template inline T rand(T max) { return rand() * max; } -template +template inline T rand(T min, T max) { return min + rand(max - min); } diff --git a/src/ll/test/EventTest.cpp b/src/ll/test/EventTest.cpp index 6c0e4cedf2..f2b1cbfa95 100644 --- a/src/ll/test/EventTest.cpp +++ b/src/ll/test/EventTest.cpp @@ -26,9 +26,13 @@ #include "mc/world/actor/ActorDamageSource.h" #include "mc/world/item/registry/ItemStack.h" - #include "ll/api/base/FixedString.h" +#include "ll/api/base/Hash.h" + +using namespace ll::hash; +using namespace ll::hash_literals; + class TestEventB : public ll::event::Event { protected: TestEventB() = default; @@ -180,12 +184,20 @@ LL_AUTO_TYPED_INSTANCE_HOOK( bus.emplaceListener([](PlayerSwingEvent& ev) { ll::logger.debug("Player {} left click", ev.player.getRealName()); }); - bus.emplaceListener([](PlayerStartSprintEvent& ev) { - ll::logger.debug("Player {} start sprint", ev.player.getRealName()); - }); - bus.emplaceListener([](PlayerStopSprintEvent& ev) { - ll::logger.debug("Player {} stop sprint", ev.player.getRealName()); + auto listenersp = Listener::create([](PlayerSprintEvent& ev) { + switch (do_hash(typeid(ev).name())) { + case do_hash(ll::reflection::type_raw_name_v): { + ll::logger.debug("Player {} start sprint", ev.player.getRealName()); + } break; + case do_hash(ll::reflection::type_raw_name_v): { + ll::logger.debug("Player {} stop sprint", ev.player.getRealName()); + } break; + default: + break; + } }); + bus.addListener(listenersp); + bus.addListener(listenersp); bus.emplaceListener([](PlayerStartSneakEvent& ev) { ll::logger.debug("Player {} start sneak", ev.player.getRealName()); }); diff --git a/src/ll/test/TestDynamicCommand.cpp b/src/ll/test/TestDynamicCommand.cpp index 3657f6e0dd..114785706f 100644 --- a/src/ll/test/TestDynamicCommand.cpp +++ b/src/ll/test/TestDynamicCommand.cpp @@ -320,6 +320,8 @@ static void setupCrashCommand() { #include static void setupTimingCommand() { + constexpr static size_t counttick = 100; + auto command = DynamicCommand::createCommand("timing", "timing", CommandPermissionLevel::GameDirectors); command->addOverload(); command->setCallback([](DynamicCommand const&, @@ -339,10 +341,12 @@ static void setupTimingCommand() { std::unordered_map timings{}; using namespace ll::chrono; TickSyncSleep sleeper; - for (size_t i = 0; i < 100; i++) { + auto begin = std::chrono::steady_clock::now(); + for (size_t i = 0; i < counttick; i++) { + sleeper.sleepFor(1_tick); { std::lock_guard lock(collection.mTimingMutex); - for (auto& collectCategory : collection.mTickingSystemCategories) { + for (auto& collectCategory : collection.mTickingSystemCategories) { auto& tickTimings = collectCategory.mTimings; for (size_t j = 0; j < tickTimings.size(); j++) { auto& timing = timings[collectCategory.mSystems.at(j)]; @@ -351,36 +355,38 @@ static void setupTimingCommand() { } } } - sleeper.sleepFor(1_tick); } + auto end = std::chrono::steady_clock::now(); { std::lock_guard lock(collection.mTimingMutex); system.mEnableTimingCapture = false; } struct TimingData { - uint id; - DefaultEntitySystemsCollection::ECSTiming timing; + uint id; + double avg; + uint count; }; std::vector orderdTiming; orderdTiming.reserve(timings.size()); double allTime = 0.0; for (auto& [systemId, timing] : timings) { - orderdTiming.emplace_back(systemId, timing); - allTime += double(timing.mMsTime) / timing.mCount; + orderdTiming.emplace_back(systemId, double(timing.mMsTime) / counttick, timing.mCount); + allTime += double(timing.mMsTime) / counttick; } - std::ranges::sort(orderdTiming, [](TimingData const& a, TimingData const& b) { - return a.timing.mMsTime > b.timing.mMsTime; - }); + std::ranges::sort(orderdTiming, [](TimingData const& a, TimingData const& b) { return a.avg > b.avg; }); - ll::logger.warn("Total {:.5f}ms", allTime); + ll::logger.warn("TPS: {:.5f}", double(counttick) / std::chrono::duration(end - begin).count()); + ll::logger.warn("ECS cost {:.5f}ms per tick", allTime); for (int i = 0; i < orderdTiming.size() && i < 20; i++) { auto& data = orderdTiming[i]; ll::logger.warn( - " | {:.5f}ms for {}", - double(data.timing.mMsTime) / data.timing.mCount, + " | {:.5f}ms {} for {:0>3} {}", + data.avg, + data.count/counttick, + data.id, collection.mAllSystemsInfo[data.id].mName ); } @@ -392,7 +398,6 @@ static void setupTimingCommand() { } -#include "mc/network/ServerNetworkHandler.h" #include "mc/server/SimulatedPlayer.h" #include "mc/world/actor/Actor.h" @@ -456,9 +461,8 @@ static void kickAllSimulatePlayerCommand() { CommandOutput& output, std::unordered_map&) { ll::Global->forEachPlayer([&](Player& player) { - if (*(void**)&player == LL_RESOLVE_SYMBOL("??_7SimulatedPlayer@@6B@")) { - player.remove(); - player.disconnect(""); + if (player.isSimulatedPlayer()) { + ((SimulatedPlayer&)player).simulateDisconnect(); } return true; }); diff --git a/src/mc/entity/EntityId.h b/src/mc/entity/EntityId.h index b92d335872..9d40e55bee 100644 --- a/src/mc/entity/EntityId.h +++ b/src/mc/entity/EntityId.h @@ -3,7 +3,13 @@ #include "mc/_HeaderOutputPredefine.h" #include "mc/entity/EntityIdTraits.h" -class EntityId : public entt::basic_entt_traits { +template <> +class entt::entt_traits : public entt::basic_entt_traits { +public: + static constexpr entity_type page_size = 2048; +}; + +class EntityId : public entt::entt_traits { public: entity_type mRawId{}; diff --git a/src/mc/entity/EntityIdTraits.h b/src/mc/entity/EntityIdTraits.h index 12392ff312..5ae077c872 100644 --- a/src/mc/entity/EntityIdTraits.h +++ b/src/mc/entity/EntityIdTraits.h @@ -12,5 +12,4 @@ struct EntityIdTraits { static constexpr entity_type entity_mask = 0x3FFFF; static constexpr entity_type version_mask = 0x3FFF; - static constexpr entity_type page_size = 2048; }; diff --git a/src/mc/nbt/CompoundTagVariant.h b/src/mc/nbt/CompoundTagVariant.h index 4069a38d35..e1156349ee 100644 --- a/src/mc/nbt/CompoundTagVariant.h +++ b/src/mc/nbt/CompoundTagVariant.h @@ -43,40 +43,41 @@ class CompoundTagVariant { [[nodiscard]] inline CompoundTagVariant(Variant tag) : mTagStorage(std::move(tag)) {} - [[nodiscard]] CompoundTagVariant(std::unique_ptr const& tag) { + [[nodiscard]] CompoundTagVariant(std::unique_ptr&& tag) { if (!tag) { return; } switch (tag->getId()) { case Tag::Type::Byte: - mTagStorage = (ByteTag&)*tag; + mTagStorage = std::move((ByteTag&)*tag); case Tag::Type::Short: - mTagStorage = (ShortTag&)*tag; + mTagStorage = std::move((ShortTag&)*tag); case Tag::Type::Int: - mTagStorage = (IntTag&)*tag; + mTagStorage = std::move((IntTag&)*tag); case Tag::Type::Int64: - mTagStorage = (Int64Tag&)*tag; + mTagStorage = std::move((Int64Tag&)*tag); case Tag::Type::Float: - mTagStorage = (FloatTag&)*tag; + mTagStorage = std::move((FloatTag&)*tag); case Tag::Type::Double: - mTagStorage = (DoubleTag&)*tag; + mTagStorage = std::move((DoubleTag&)*tag); case Tag::Type::ByteArray: - mTagStorage = (ByteArrayTag&)*tag; + mTagStorage = std::move((ByteArrayTag&)*tag); case Tag::Type::String: - mTagStorage = (StringTag&)*tag; + mTagStorage = std::move((StringTag&)*tag); case Tag::Type::List: - mTagStorage = (ListTag&)*tag; + mTagStorage = std::move((ListTag&)*tag); case Tag::Type::Compound: - mTagStorage = (CompoundTag&)*tag; + mTagStorage = std::move((CompoundTag&)*tag); case Tag::Type::IntArray: - mTagStorage = (IntArrayTag&)*tag; + mTagStorage = std::move((IntArrayTag&)*tag); case Tag::Type::End: default: - mTagStorage = (EndTag&)*tag; + mTagStorage = std::move((EndTag&)*tag); } } + [[nodiscard]] CompoundTagVariant(std::unique_ptr const& tag) : CompoundTagVariant(std::move(tag->copy())) {} template T> - [[nodiscard]] constexpr CompoundTagVariant(T tag) : mTagStorage(std::forward(tag)) {} + [[nodiscard]] constexpr CompoundTagVariant(T tag) : mTagStorage(std::move(tag)) {} template [[nodiscard]] constexpr CompoundTagVariant(T integer) { // NOLINT constexpr size_t size = sizeof(T); @@ -96,6 +97,7 @@ class CompoundTagVariant { [[nodiscard]] inline CompoundTagVariant(std::string s) : mTagStorage(StringTag{std::move(s)}) {} // NOLINT + [[nodiscard]] Tag::Type index() const { return (Tag::Type)mTagStorage.index(); } template T> [[nodiscard]] bool hold() const { return std::holds_alternative(mTagStorage); @@ -111,6 +113,14 @@ class CompoundTagVariant { return std::get(mTagStorage); } + [[nodiscard]] Tag& get() { + return std::visit([](auto& val) -> Tag& { return (Tag&)val; }, mTagStorage); + } + + [[nodiscard]] Tag const& get() const { + return std::visit([](auto& val) -> Tag const& { return (Tag const&)val; }, mTagStorage); + } + [[nodiscard]] CompoundTagVariant operator[](size_t index) const { if (hold()) { return get()[index]; @@ -138,22 +148,6 @@ class CompoundTagVariant { mTagStorage ); } - -public: - // NOLINTBEGIN - // symbol: ?emplace@CompoundTagVariant@@QEAAAEAVTag@@$$QEAV2@@Z - MCAPI class Tag& emplace(class Tag&&); - - // symbol: ?get@CompoundTagVariant@@QEAAPEAVTag@@XZ - MCAPI class Tag* get(); - - // symbol: ?get@CompoundTagVariant@@QEBAPEBVTag@@XZ - MCAPI class Tag const* get() const; - - // symbol: ??1CompoundTagVariant@@QEAA@XZ - MCAPI ~CompoundTagVariant(); - - // NOLINTEND }; #endif // COMPOUND_TAG_VARIANT_HEADER \ No newline at end of file diff --git a/src/mc/nbt/SnbtDumpImpl.cpp b/src/mc/nbt/SnbtDumpImpl.cpp index 3e7b17b766..eb395fdf49 100644 --- a/src/mc/nbt/SnbtDumpImpl.cpp +++ b/src/mc/nbt/SnbtDumpImpl.cpp @@ -253,7 +253,7 @@ std::string TypedToSnbt(CompoundTag& self, uchar indent, SnbtFormat format) { res += ' '; } - auto key = v.get()->toSnbt(format, indent); + auto key = v.get().toSnbt(format, indent); if (isNewLine) { replaceAll(key, "\n", "\n" + indentSpace); diff --git a/src/mc/nbt/SnbtParseImpl.cpp b/src/mc/nbt/SnbtParseImpl.cpp index 1ac2d74459..1c1bf31bfd 100644 --- a/src/mc/nbt/SnbtParseImpl.cpp +++ b/src/mc/nbt/SnbtParseImpl.cpp @@ -601,8 +601,8 @@ std::optional parseList(std::string_view& s) { } if (type == Tag::Type::End) { - type = value.value().get()->getId(); - } else if (value.value().get()->getId() != type) { + type = value.value().index(); + } else if (value.value().index() != type) { return std::nullopt; } res.mList.emplace_back(value.value().toUnique()); diff --git a/src/mc/server/SimulatedPlayer.cpp b/src/mc/server/SimulatedPlayer.cpp index 27501e4497..8bc5234c19 100644 --- a/src/mc/server/SimulatedPlayer.cpp +++ b/src/mc/server/SimulatedPlayer.cpp @@ -17,8 +17,10 @@ optional_ref SimulatedPlayer::create( if (!ll::Global) { return nullptr; } - OwnerPtrT ownerPtr = - ll::Global->createSimulatedPlayer(name, std::to_string(INT32_MAX + random::rand())); + OwnerPtrT ownerPtr = ll::Global->createSimulatedPlayer( + name, + std::to_string(random::rand(INT64_MIN, -1)) + ); auto player = ownerPtr.tryUnwrap(); if (player == nullptr) { diff --git a/src/mc/world/actor/player/Player.cpp b/src/mc/world/actor/player/Player.cpp index 78c119d209..4f50accd34 100644 --- a/src/mc/world/actor/player/Player.cpp +++ b/src/mc/world/actor/player/Player.cpp @@ -45,11 +45,13 @@ std::optional Player::getNetworkStatus() const { } std::string Player::getRealName() const { - auto certificate = getCertificate(); - if (!certificate) { - return getName(); + std::string res; + auto certificate = getCertificate(); + if (certificate) { + res = ExtendedCertificate::getIdentityName(*certificate); } - return ExtendedCertificate::getIdentityName(*certificate); + if (res.empty()) res = getName(); + return res; } void Player::disconnect(std::string_view reason) const {