Skip to content

Commit

Permalink
refactor: reuse function pointer hook with overload
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jan 31, 2024
1 parent 0b92722 commit a36e58e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
15 changes: 8 additions & 7 deletions src/ll/api/dimension/CustomDimensionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ LL_TYPE_STATIC_HOOK(
VanillaDimensionsFromSerializedIntHook,
HookPriority::Normal,
VanillaDimensions,
"?fromSerializedInt@VanillaDimensions@@SA?AV?$Result@V?$AutomaticID@VDimension@@H@@Verror_code@std@@@Bedrock@@$$"
"QEAV?$Result@HVerror_code@std@@@3@@Z",
VanillaDimensions::fromSerializedInt,
Bedrock::Result<DimensionType>,
Bedrock::Result<int>& dim
Bedrock::Result<int>&& dim
) {
if (!dim || *dim <= 2) {
return origin(dim);
return origin(std::move(dim));
}
return *dim;
};
Expand All @@ -60,7 +59,7 @@ LL_TYPE_STATIC_HOOK(
VanillaDimensionsFromSerializedIntHookI,
HookPriority::Normal,
VanillaDimensions,
"?fromSerializedInt@VanillaDimensions@@SA?AV?$AutomaticID@VDimension@@H@@H@Z",
VanillaDimensions::fromSerializedInt,
DimensionType,
int dimId
) {
Expand Down Expand Up @@ -130,7 +129,8 @@ CustomDimensionManager::CustomDimensionManager() : impl(std::make_unique<Impl>()
name,
Impl::DimensionInfo{
info.dimId,
*CompoundTag::fromBinaryNbt(string_utils::decompress(base64_utils::decode(info.base64Nbt)))}
*CompoundTag::fromBinaryNbt(string_utils::decompress(base64_utils::decode(info.base64Nbt)))
}
);
}
impl->mNewDimensionId += static_cast<int>(impl->customDimensionMap.size());
Expand Down Expand Up @@ -206,7 +206,8 @@ DimensionType CustomDimensionManager::addDimension(
dimName,
CustomDimensionConfig::Config::Info{
info.id,
base64_utils::encode(string_utils::compress(info.nbt.toBinaryNbt()))}
base64_utils::encode(string_utils::compress(info.nbt.toBinaryNbt()))
}
);
CustomDimensionConfig::saveConfigFile();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/event/player/PlayerChatEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LL_TYPE_INSTANCE_HOOK(
PlayerSendMessageEventHook,
HookPriority::Normal,
ServerNetworkHandler,
"?handle@ServerNetworkHandler@@UEAAXAEBVNetworkIdentifier@@AEBVTextPacket@@@Z",
&ServerNetworkHandler::handle,
void,
NetworkIdentifier const& identifier,
TextPacket const& packet
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/event/player/PlayerSneakAndSprintEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL_TYPE_INSTANCE_HOOK(
PlayerActionEventHook,
HookPriority::Normal,
ServerNetworkHandler,
"?handle@ServerNetworkHandler@@UEAAXAEBVNetworkIdentifier@@AEBVPlayerActionPacket@@@Z",
&ServerNetworkHandler::handle,
void,
NetworkIdentifier const& id,
PlayerActionPacket const& packet
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/memory/Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ struct __declspec(empty_bases) Hook {};
if constexpr (::ll::memory::virtualDetector<_OriginFuncType, IDENTIFIER>()) { \
static_assert( \
::ll::concepts::always_false<T>, \
#IDENTIFIER " is a virtual function, for now you can't use function pointer to hook it." \
#IDENTIFIER " is a virtual function, you need use prefix $ workaround to hook it." \
); \
} \
} \
Expand Down
78 changes: 39 additions & 39 deletions src/ll/api/reflection/Reflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,49 @@ 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 <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>;
// 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!");
// 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
}
};
// // 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 <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) {
Expand Down
9 changes: 4 additions & 5 deletions src/ll/core/dimension/FakeDimensionId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ LL_TYPE_INSTANCE_HOOK(
ServerNetworkHandlerPlayerActionPacketHandler,
HookPriority::Normal,
ServerNetworkHandler,
"?handle@ServerNetworkHandler@@UEAAXAEBVNetworkIdentifier@@AEBVPlayerActionPacket@@@Z",
&ServerNetworkHandler::handle,
void,
NetworkIdentifier const& netId,
PlayerActionPacket& packet
NetworkIdentifier const& netId,
PlayerActionPacket const& packet
) {
auto player = getServerPlayer(netId, packet.mClientSubId);
auto uuid = player->getUuid();
Expand Down Expand Up @@ -352,8 +352,7 @@ LL_TYPE_INSTANCE_HOOK(
LevelrequestPlayerChangeDimensionHandler,
HookPriority::Normal,
Level,
"?requestPlayerChangeDimension@Level@@UEAAXAEAVPlayer@@V?$unique_ptr@VChangeDimensionRequest@@U?$default_delete@"
"VChangeDimensionRequest@@@std@@@std@@@Z",
&Level::requestPlayerChangeDimension,
void,
Player& player,
std::unique_ptr<ChangeDimensionRequest> changeRequest
Expand Down
2 changes: 1 addition & 1 deletion src/ll/test/ConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ LL_AUTO_TYPE_INSTANCE_HOOK(ConfigTest, HookPriority::Normal, ServerInstance, &Se
ll::logger.debug("789\xDB\xFE");
ll::logger.debug("789\xDB\xFE");

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

#if !(defined(__INTELLISENSE__) || defined(__clangd__) || defined(__clang__))
myTypeList1::push_back<int>();
Expand Down

0 comments on commit a36e58e

Please sign in to comment.