Skip to content

Commit

Permalink
fix: fix entt traits
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Dec 5, 2023
1 parent cd00b80 commit 5357005
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/ll/api/base/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ template <std::floating_point T>
inline T rand(T max) {
return rand<T>() * max;
}
template <std::floating_point T>
template <class T>
inline T rand(T min, T max) {
return min + rand<T>(max - min);
}
Expand Down
24 changes: 18 additions & 6 deletions src/ll/test/EventTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,12 +184,20 @@ LL_AUTO_TYPED_INSTANCE_HOOK(
bus.emplaceListener<PlayerSwingEvent>([](PlayerSwingEvent& ev) {
ll::logger.debug("Player {} left click", ev.player.getRealName());
});
bus.emplaceListener<PlayerStartSprintEvent>([](PlayerStartSprintEvent& ev) {
ll::logger.debug("Player {} start sprint", ev.player.getRealName());
});
bus.emplaceListener<PlayerStopSprintEvent>([](PlayerStopSprintEvent& ev) {
ll::logger.debug("Player {} stop sprint", ev.player.getRealName());
auto listenersp = Listener<PlayerSprintEvent>::create([](PlayerSprintEvent& ev) {
switch (do_hash(typeid(ev).name())) {
case do_hash(ll::reflection::type_raw_name_v<PlayerStartSprintEvent>): {
ll::logger.debug("Player {} start sprint", ev.player.getRealName());
} break;
case do_hash(ll::reflection::type_raw_name_v<PlayerStopSprintEvent>): {
ll::logger.debug("Player {} stop sprint", ev.player.getRealName());
} break;
default:
break;
}
});
bus.addListener<PlayerStartSprintEvent>(listenersp);
bus.addListener<PlayerStopSprintEvent>(listenersp);
bus.emplaceListener<PlayerStartSneakEvent>([](PlayerStartSneakEvent& ev) {
ll::logger.debug("Player {} start sneak", ev.player.getRealName());
});
Expand Down
38 changes: 21 additions & 17 deletions src/ll/test/TestDynamicCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ static void setupCrashCommand() {
#include <ranges>

static void setupTimingCommand() {
constexpr static size_t counttick = 100;

auto command = DynamicCommand::createCommand("timing", "timing", CommandPermissionLevel::GameDirectors);
command->addOverload();
command->setCallback([](DynamicCommand const&,
Expand All @@ -339,10 +341,12 @@ static void setupTimingCommand() {
std::unordered_map<uint, DefaultEntitySystemsCollection::ECSTiming> timings{};
using namespace ll::chrono;
TickSyncSleep<GameTimeClock> 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)];
Expand All @@ -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<TimingData> 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<double>(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
);
}
Expand All @@ -392,7 +398,6 @@ static void setupTimingCommand() {
}


#include "mc/network/ServerNetworkHandler.h"
#include "mc/server/SimulatedPlayer.h"
#include "mc/world/actor/Actor.h"

Expand Down Expand Up @@ -456,9 +461,8 @@ static void kickAllSimulatePlayerCommand() {
CommandOutput& output,
std::unordered_map<std::string, DynamicCommand::Result>&) {
ll::Global<Level>->forEachPlayer([&](Player& player) {
if (*(void**)&player == LL_RESOLVE_SYMBOL("??_7SimulatedPlayer@@6B@")) {
player.remove();
player.disconnect("");
if (player.isSimulatedPlayer()) {
((SimulatedPlayer&)player).simulateDisconnect();
}
return true;
});
Expand Down
8 changes: 7 additions & 1 deletion src/mc/entity/EntityId.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
#include "mc/_HeaderOutputPredefine.h"
#include "mc/entity/EntityIdTraits.h"

class EntityId : public entt::basic_entt_traits<EntityIdTraits> {
template <>
class entt::entt_traits<EntityId> : public entt::basic_entt_traits<EntityIdTraits> {
public:
static constexpr entity_type page_size = 2048;
};

class EntityId : public entt::entt_traits<EntityId> {
public:
entity_type mRawId{};

Expand Down
1 change: 0 additions & 1 deletion src/mc/entity/EntityIdTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
54 changes: 24 additions & 30 deletions src/mc/nbt/CompoundTagVariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,41 @@ class CompoundTagVariant {

[[nodiscard]] inline CompoundTagVariant(Variant tag) : mTagStorage(std::move(tag)) {}

[[nodiscard]] CompoundTagVariant(std::unique_ptr<Tag> const& tag) {
[[nodiscard]] CompoundTagVariant(std::unique_ptr<Tag>&& 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<Tag> const& tag) : CompoundTagVariant(std::move(tag->copy())) {}
template <std::derived_from<Tag> T>
[[nodiscard]] constexpr CompoundTagVariant(T tag) : mTagStorage(std::forward<T>(tag)) {}
[[nodiscard]] constexpr CompoundTagVariant(T tag) : mTagStorage(std::move(tag)) {}
template <std::integral T>
[[nodiscard]] constexpr CompoundTagVariant(T integer) { // NOLINT
constexpr size_t size = sizeof(T);
Expand All @@ -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 <std::derived_from<Tag> T>
[[nodiscard]] bool hold() const {
return std::holds_alternative<T>(mTagStorage);
Expand All @@ -111,6 +113,14 @@ class CompoundTagVariant {
return std::get<T>(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<ListTag>()) {
return get<ListTag>()[index];
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/mc/nbt/SnbtDumpImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/mc/nbt/SnbtParseImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ std::optional<CompoundTagVariant> 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());
Expand Down
6 changes: 4 additions & 2 deletions src/mc/server/SimulatedPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ optional_ref<SimulatedPlayer> SimulatedPlayer::create(
if (!ll::Global<ServerNetworkHandler>) {
return nullptr;
}
OwnerPtrT<EntityRefTraits> ownerPtr =
ll::Global<ServerNetworkHandler>->createSimulatedPlayer(name, std::to_string(INT32_MAX + random::rand<uint>()));
OwnerPtrT<EntityRefTraits> ownerPtr = ll::Global<ServerNetworkHandler>->createSimulatedPlayer(
name,
std::to_string(random::rand<int64>(INT64_MIN, -1))
);
auto player = ownerPtr.tryUnwrap<SimulatedPlayer>();

if (player == nullptr) {
Expand Down
10 changes: 6 additions & 4 deletions src/mc/world/actor/player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ std::optional<NetworkPeer::NetworkStatus> 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 {
Expand Down

0 comments on commit 5357005

Please sign in to comment.