Skip to content

Commit

Permalink
More organization
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTheMan827 committed Nov 22, 2024
1 parent 752dc28 commit 3c2fc51
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
36 changes: 17 additions & 19 deletions include/autohooks.hpp → include/AutoHooks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@
#include "beatsaber-hook/shared/utils/hooking.hpp"
#include <concepts>

class AutoHooks {
private:
inline static std::vector<void (*)()> installFuncs;
namespace AutoHooks {
inline std::vector<void (*)()> installFuncs;

public:
static void AddInstallFunc(void (*installFunc)()) {
inline void AddInstallFunc(void (*installFunc)()) {
installFuncs.push_back(installFunc);
}

static inline void InstallHooks() {
inline void InstallHooks() {
for (auto& func : installFuncs) func();
}
};

template<auto mPtr>
concept has_metadata = requires() {
{ ::il2cpp_utils::il2cpp_type_check::MetadataGetter<mPtr>::methodInfo() } -> std::same_as<MethodInfo const*>;
};
template<auto mPtr>
concept has_metadata = requires() {
{ ::il2cpp_utils::il2cpp_type_check::MetadataGetter<mPtr>::methodInfo() } -> std::same_as<MethodInfo const*>;
};

template<auto mPtr>
requires(has_metadata<mPtr>)
using Metadata = ::il2cpp_utils::il2cpp_type_check::MetadataGetter<mPtr>;
template<auto mPtr>
requires(has_metadata<mPtr>)
using Metadata = ::il2cpp_utils::il2cpp_type_check::MetadataGetter<mPtr>;

/// @brief checks whether the function is match hookable, which requires the function to be at least 5 (5 * 4 = 20 bytes) instructions and not have an address of 0 (abstract/virtual funcs)
template<auto mPtr>
concept match_hookable = has_metadata<mPtr> && Metadata<mPtr>::size >= (0x5 * sizeof(int32_t)) && Metadata<mPtr>::addrs != 0xffffffff;
/// @brief checks whether the function is match hookable, which requires the function to be at least 5 (5 * 4 = 20 bytes) instructions and not have an address of 0 (abstract/virtual funcs)
template<auto mPtr>
concept match_hookable = has_metadata<mPtr> && Metadata<mPtr>::size >= (0x5 * sizeof(int32_t)) && Metadata<mPtr>::addrs != 0xffffffff;
}

#define HOOK_AUTO_INSTALL_ORIG(name_) \
struct Auto_Hook_##name_ { \
Expand All @@ -53,7 +51,7 @@ concept match_hookable = has_metadata<mPtr> && Metadata<mPtr>::size >= (0x5 * si
#define MAKE_AUTO_HOOK_MATCH(name_, mPtr, retval, ...) \
struct Hook_##name_ { \
using funcType = retval (*)(__VA_ARGS__); \
static_assert(match_hookable<mPtr>); \
static_assert(AutoHooks::match_hookable<mPtr>); \
static_assert(std::is_same_v<funcType, ::Hooking::InternalMethodCheck<decltype(mPtr)>::funcType>, "Hook method signature does not match!"); \
constexpr static const char* name() { return #name_; } \
static const MethodInfo* getInfo() { return ::il2cpp_utils::il2cpp_type_check::MetadataGetter<mPtr>::methodInfo(); } \
Expand All @@ -68,7 +66,7 @@ concept match_hookable = has_metadata<mPtr> && Metadata<mPtr>::size >= (0x5 * si
#define MAKE_AUTO_HOOK_ORIG_MATCH(name_, mPtr, retval, ...) \
struct Hook_##name_ { \
using funcType = retval (*)(__VA_ARGS__); \
static_assert(match_hookable<mPtr>); \
static_assert(AutoHooks::match_hookable<mPtr>); \
static_assert(std::is_same_v<funcType, ::Hooking::InternalMethodCheck<decltype(mPtr)>::funcType>, "Hook method signature does not match!"); \
constexpr static const char* name() { return #name_; } \
static const MethodInfo* getInfo() { return ::il2cpp_utils::il2cpp_type_check::MetadataGetter<mPtr>::methodInfo(); } \
Expand Down
7 changes: 7 additions & 0 deletions include/Logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "beatsaber-hook/shared/utils/logging.hpp"

/// @brief A logger, useful for printing debug messages
/// @return
inline constexpr auto Logger = Paper::ConstLoggerContext(MOD_ID "_" VERSION);
4 changes: 0 additions & 4 deletions include/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include "beatsaber-hook/shared/utils/logging.hpp"
#include "beatsaber-hook/shared/utils/typedefs.h"

/// @brief A logger, useful for printing debug messages
/// @return
static constexpr auto Logger = Paper::ConstLoggerContext(MOD_ID "_" VERSION);

#define MOD_EXPORT __attribute__((visibility("default")))
#ifdef __cplusplus
#define MOD_EXPORT_FUNC extern "C" MOD_EXPORT
Expand Down
6 changes: 6 additions & 0 deletions include/modInfo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "scotland2/shared/loader.hpp"

/// @brief Stores the ID and version of our mod, and is sent to the modloader upon startup
inline const modloader::ModInfo modInfo{MOD_ID, VERSION, 0};
19 changes: 11 additions & 8 deletions src/Hooks/MainViewController.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#include "main.hpp"
#include "autohooks.hpp"
#include "AutoHooks.hpp"
#include "Logger.hpp"

// GlobalNamespace
#include "GlobalNamespace/MainMenuViewController.hpp"

// UnityEngine
#include "UnityEngine/GameObject.hpp"

using namespace GlobalNamespace;
namespace Hooks {
// Usings
using namespace GlobalNamespace;

// Hooks the MainMenuViewController to hide the musicPackPromoBanner object.
MAKE_AUTO_HOOK_MATCH(MainMenuViewController_DidActivate, &MainMenuViewController::DidActivate, void, MainMenuViewController* self, bool firstActivation, bool addedToHierarchy, bool screenSystemEnabling) {
Logger.debug("MainMenuViewController_DidActivate");
self->____musicPackPromoBanner->get_gameObject()->SetActive(false);
MainMenuViewController_DidActivate(self, firstActivation, addedToHierarchy, screenSystemEnabling);
// Hooks the MainMenuViewController to hide the musicPackPromoBanner object.
MAKE_AUTO_HOOK_MATCH(MainMenuViewController_DidActivate, &MainMenuViewController::DidActivate, void, MainMenuViewController* self, bool firstActivation, bool addedToHierarchy, bool screenSystemEnabling) {
Logger.debug("MainMenuViewController_DidActivate");
self->____musicPackPromoBanner->get_gameObject()->SetActive(false);
MainMenuViewController_DidActivate(self, firstActivation, addedToHierarchy, screenSystemEnabling);
}
}
7 changes: 4 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "main.hpp"
#include "autohooks.hpp"
#include "AutoHooks.hpp"
#include "Logger.hpp"
#include "modInfo.hpp"

/// @brief Called at the early stages of game loading
/// @param info
/// @return
MOD_EXPORT_FUNC void setup(CModInfo& info) {
info.id = MOD_ID;
info.version = VERSION;
info = modInfo.to_c();

Logger.info("Completed setup!");
}
Expand Down

0 comments on commit 3c2fc51

Please sign in to comment.