Skip to content

Commit

Permalink
feat: add reflection offset getter
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 30, 2024
1 parent 37d932b commit 94f17f8
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 17 deletions.
1 change: 1 addition & 0 deletions .clangd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CompileFlags:
Add:
- "-ferror-limit=0"
- '-D__FUNCTION__="dummy"'
- '-D__clangd__'
- "-Yusrc/ll/api/Global.h"
- "-FIsrc/ll/api/Global.h" # clangd bug can't find pch file
Remove:
Expand Down
4 changes: 2 additions & 2 deletions src/ll/api/base/Meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ template <class Group, class T, auto Id = int64{}>
return Id;
}
}

#if 0
// TODO: fix this
#if !(defined(__INTELLISENSE__) || defined(__clangd__) || defined(__clang__))
template <class Group>
struct DynamicTypeList {
template <size_t N>
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/base/MsvcPredefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// MSVC has customized some functions and classes inside the compiler, but they are not included in IntelliSense. This
// header file is only used for IntelliSense.
#if defined(__INTELLISENSE__) || defined(__clang__)
#if defined(__INTELLISENSE__) || defined(__clang__) || defined(__clangd__)
// NOLINTBEGIN
#pragma pack(push, ehdata, 4)

Expand Down
8 changes: 8 additions & 0 deletions src/ll/api/command/CommandRegistrar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "ll/api/command/CommandRegistrar.h"

namespace ll::command {

CommandRegistrar::CommandRegistrar(CommandRegistry& registry) : registry(&registry) {}
CommandRegistrar::~CommandRegistrar() = default;

} // namespace ll::command
12 changes: 11 additions & 1 deletion src/ll/api/command/CommandRegistrar.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@

#include "mc/server/commands/CommandRegistry.h"

namespace ll::command {}
namespace ll::command {
class CommandRegistrar {
CommandRegistry* registry;

public:
LLNDAPI CommandRegistrar(CommandRegistry&);
LLAPI ~CommandRegistrar();

CommandRegistry* operator->() const { return registry; }
};
} // namespace ll::command
51 changes: 49 additions & 2 deletions src/ll/api/reflection/Reflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#endif

#include "boost/pfr.hpp"
#include "boost/pfr/detail/offset_based_getter.hpp"

#include "magic_enum.hpp"

Expand All @@ -26,11 +27,57 @@ inline constexpr bool is_reflectable_v =
template <class T>
concept Reflectable = is_reflectable_v<T>;

template <class T>
inline constexpr auto const name_array_v = boost::pfr::names_as_array<std::remove_cvref_t<T>>();

template <class T, std::size_t... I>
constexpr auto makeSequenceTupleFromTieTuple(const T& t, std::index_sequence<I...>) noexcept {
return boost::pfr::detail::sequence_tuple::make_sequence_tuple(boost::pfr::detail::sequence_tuple::get<I>(t)...);
}

template <
typename U,
typename S = decltype(makeSequenceTupleFromTieTuple(
boost::pfr::detail::tie_as_tuple(boost::pfr::detail::fake_object<U>),
boost::pfr::detail::make_index_sequence<boost::pfr::tuple_size_v<U>>()
))>
struct OffsetGetter {
using this_t = OffsetGetter<U, S>;

static_assert(
sizeof(U) == sizeof(S),
"Member sequence does not indicate correct size for struct type! Maybe the user-provided type is not a "
"SimpleAggregate?"
);
static_assert(alignof(U) == alignof(S), " Member sequence does not indicate correct alignment for struct type!");

// Get offset of idx'th member
// Idea: Layout object has the same offsets as instance of S, so if S and U are layout compatible, then these offset
// calculations are correct.
template <std::size_t idx>
static consteval std::ptrdiff_t offset() noexcept {
#if defined(__INTELLISENSE__) || defined(__clangd__) || defined(__clang__)
return 0; // TODO: fix this
#else
constexpr boost::pfr::detail::tuple_of_aligned_storage_t<S> layout{};
return (&boost::pfr::detail::sequence_tuple::get<idx>(layout).storage_)
- (&boost::pfr::detail::sequence_tuple::get<0>(layout).storage_);
#endif
}
};

template <class T, std::size_t... I>
consteval auto makeOffsetArrayImpl(std::index_sequence<I...>) {
return boost::pfr::detail::make_stdarray((OffsetGetter<T>::template offset<I>())...);
}
template <class T>
inline constexpr auto const offset_array_v =
makeOffsetArrayImpl<T>(std::make_index_sequence<boost::pfr::tuple_size_v<T>>());

template <Reflectable T, class F>
constexpr void forEachMember(T&& value, F&& func) {
static constexpr auto const namearray = boost::pfr::names_as_array<std::remove_cvref_t<T>>();
boost::pfr::for_each_field(std::forward<T>(value), [func = std::forward<F>(func)](auto&& field, std::size_t idx) {
func(namearray[idx], std::forward<decltype(field)>(field));
func(name_array_v<T>[idx], std::forward<decltype(field)>(field));
});
}
} // namespace ll::reflection
12 changes: 10 additions & 2 deletions src/ll/test/ConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@

#include "mc/world/actor/DataItem.h"

// [0, 8, 16, 96, 97, 98, 104, 136, 144, 160, 176, 184, 196, 208, 232, 248, 304, 328, 360, 392, 408]

// ["structure", "version", "ver", "someFlag", "eeeeFlag", "ssbbFlag", "str", "plain", "amap", "bmap", "vec2", "vec3",
// "pos", "box", "tuple", "pair", "array", "vector", "nullvector", "mulset", "hi"]

template <class T>
class TestClass {
public:
Expand Down Expand Up @@ -86,7 +91,7 @@ class TestClass {
// LL_AUTO_TYPE_INSTANCE_HOOK(Virtual, HookPriority::Normal, FillCommand, &FillCommand::execute, void, CommandOrigin
// const&, CommandOutput&) {
// }
#if 0
#if !(defined(__INTELLISENSE__) || defined(__clangd__) || defined(__clang__))
struct myTypeList1 : ll::meta::DynamicTypeList<myTypeList1> {};
struct myTypeList2 : ll::meta::DynamicTypeList<myTypeList2> {};
struct myTypeList3 : ll::meta::TypeList<bool, int> {};
Expand Down Expand Up @@ -154,7 +159,10 @@ LL_AUTO_TYPE_INSTANCE_HOOK(ConfigTest, HookPriority::Normal, ServerInstance, &Se

ll::logger.debug("789\xDB\xFE");
ll::logger.debug("789\xDB\xFE");
#if 0

ll::logger.debug("{}", ll::reflection::offset_array_v<TestClass<int>>);

#if !(defined(__INTELLISENSE__) || defined(__clangd__) || defined(__clang__))
myTypeList1::push_back<int>();
myTypeList1::push_back<float>();
ll::logger.debug("{}", ll::reflection::type_raw_name_v<decltype(myTypeList1::value())>);
Expand Down
4 changes: 2 additions & 2 deletions src/ll/test/EventTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

#include "ll/api/utils/HashUtils.h"

#if 0
#if !(defined(__INTELLISENSE__) || defined(__clangd__) || defined(__clang__))
struct myTypeList2 : ll::meta::DynamicTypeList<myTypeList2> {};
#endif

Expand Down Expand Up @@ -213,7 +213,7 @@ LL_AUTO_TYPE_INSTANCE_HOOK(
.debug("Player {} MultiListener of {}", ev.self().getRealName(), ll::reflection::type_raw_name_v<decltype(ev)>);
});
bus.addListener(mul);
#if 0
#if !(defined(__INTELLISENSE__) || defined(__clangd__) || defined(__clang__))
myTypeList2::push_back<float>();
ll::logger.debug("{}", typeid(myTypeList2::value()).name());
#endif
Expand Down
2 changes: 0 additions & 2 deletions src/ll/test/PtrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ LL_AUTO_TYPE_INSTANCE_HOOK(
&BlockDefinitionGroup::registerBlocks,
void
) {
std::cout << "type_hash " << entt::type_hash<FlagComponent<ExitFromPassengerFlag>>::value() << std::endl;

ll::error_utils::printException(ll::logger, ll::error_utils::getWinLastError());

try {
Expand Down
6 changes: 3 additions & 3 deletions src/mc/server/commands/CommandRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CommandRegistry::Signature::Signature(
: name(name),
desc(desc),
perm(perm),
main_symbol(symbol),
mainSymbol(symbol),
flag(flag) {}

void CommandRegistry::registerOverload(
Expand Down Expand Up @@ -102,11 +102,11 @@ bool CommandRegistry::unregisterCommand(std::string const& name) {

auto sig = mSignatures.find(command);
std::erase_if(mCommandSymbols, [&](Symbol const& r) {
return r == sig->second.main_symbol || r == sig->second.alt_symbol;
return r == sig->second.mainSymbol || r == sig->second.altSymbol;
});

std::erase_if(mFactorizations, [&](Factorization const& r) {
return r.commandSymbol == sig->second.main_symbol || r.commandSymbol == sig->second.alt_symbol;
return r.commandSymbol == sig->second.mainSymbol || r.commandSymbol == sig->second.altSymbol;
});

mSignatures.erase(sig);
Expand Down
4 changes: 2 additions & 2 deletions src/mc/server/commands/CommandRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ class CommandRegistry {
std::vector<Overload> overloads; // this+0x40
std::vector<void*> unk88; // this+0x58
CommandPermissionLevel perm; // this+0x70
Symbol main_symbol; // this+0x74
Symbol alt_symbol; // this+0x78
Symbol mainSymbol; // this+0x74
Symbol altSymbol; // this+0x78
CommandFlag flag; // this+0x7C
int firstRule{}; // this+0x80
int firstFactorization{}; // this+0x84
Expand Down

0 comments on commit 94f17f8

Please sign in to comment.