Skip to content

Commit

Permalink
Switch to paper logging
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBrumbler committed Mar 18, 2024
1 parent 7544b85 commit 34f82c6
Show file tree
Hide file tree
Showing 15 changed files with 486 additions and 450 deletions.
10 changes: 5 additions & 5 deletions include/capstone-helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace cs {
size_t sz = szBytes;
while (sz > 0) {
bool res = cs_disasm_iter(getHandle(), &instructions, &sz, &ptr, insn);
custom_types::_logger().debug("%p diassemb: %s (rCount: %i, nCalls: %u, sz: %zu)", (void*)ptr, insn->mnemonic, rCount, nCalls, sz);
custom_types::logger.debug("0x{:08x} diassemb: {} (rCount: {}, nCalls: {}, sz: {})", ptr, insn->mnemonic, rCount, nCalls, sz);
if (res) {
// Valid decode, so lets check to see if it is a match or we need to break.
if (insn->id == ARM64_INS_RET) {
if (rCount == 0) {
// Early termination!
cs_free(insn, 1);
custom_types::_logger().debug("Could not find: %u call at: %p within: %i rets! Found all of the rets first!", nToRetOn, addr, retCount);
custom_types::logger.debug("Could not find: {} call at: {} within: {} rets! Found all of the rets first!", nToRetOn, fmt::ptr(addr), retCount);
return {};
}
rCount--;
Expand All @@ -40,7 +40,7 @@ namespace cs {
if (nCalls == 1) {
std::string name(insn->mnemonic);
cs_free(insn, 1);
custom_types::_logger().debug("Found: %u match, at: %p within: %i rets, but the result was a %s! Cannot compute destination address!", nToRetOn, addr, retCount, name.c_str());
custom_types::logger.debug("Found: {} match, at: {} within: {} rets, but the result was a {}! Cannot compute destination address!", nToRetOn, fmt::ptr(addr), retCount, name.c_str());
return {};
} else {
nCalls--;
Expand All @@ -52,15 +52,15 @@ namespace cs {
else {
// Invalid instructions are ignored silently.
// In order to skip these properly, we must increment our instructions, ptr, and size accordingly.
custom_types::_logger().debug("FAILED PARSE: %p diassemb: 0x%x", (void*)ptr, *(uint32_t*)ptr);
custom_types::logger.debug("FAILED PARSE: 0x{:08x} diassemb: 0x{:08x}", ptr, *(uint32_t*)ptr);
sz -= 4;
ptr += 4;
instructions += 4;
}
}
// If we run out of bytes to parse, we fail
cs_free(insn, 1);
custom_types::_logger().debug("Could not find: %u call at: %p within: %i rets, within size: %zu!", nToRetOn, addr, retCount, szBytes);
custom_types::logger.debug("Could not find: {} call at: {} within: {} rets, within size: {}!", nToRetOn, fmt::ptr(addr), retCount, szBytes);
return {};
}

Expand Down
42 changes: 25 additions & 17 deletions qpm.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": "0.1.0",
"sharedDir": "shared",
"dependenciesDir": "extern",
"info": {
Expand All @@ -8,16 +9,30 @@
"url": "https://github.com/QuestPackageManager/Il2CppQuestTypePatching",
"additionalData": {
"overrideSoName": "libcustom-types.so",
"cmake": true,
"compileOptions": {
"cppFlags": [ "-Wno-invalid-offsetof" ]
}
"cppFlags": [
"-Wno-invalid-offsetof"
]
},
"cmake": true
}
},
"workspace": {
"scripts": {
"build": [
"pwsh ./build.ps1"
],
"qmod": [
"pwsh ./build.ps1 -clean",
"qpm qmod build",
"pwsh ./createqmod.ps1 CustomTypes -clean"
]
}
},
"dependencies": [
{
"id": "beatsaber-hook",
"versionRange": "^5.0.2",
"versionRange": "^5.1.1",
"additionalData": {}
},
{
Expand All @@ -26,18 +41,11 @@
"additionalData": {
"private": true
}
},
{
"id": "paper",
"versionRange": "^3.6.1",
"additionalData": {}
}
],
"workspace": {
"scripts": {
"build": [
"pwsh ./build.ps1"
],
"qmod": [
"pwsh ./build.ps1 -clean",
"qpm qmod build",
"pwsh ./createqmod.ps1 CustomTypes -clean"
]
}
}
]
}
56 changes: 50 additions & 6 deletions qpm.shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": [
{
"id": "beatsaber-hook",
"versionRange": "^5.0.2",
"versionRange": "^5.1.1",
"additionalData": {}
},
{
Expand All @@ -42,10 +42,35 @@
"additionalData": {
"private": true
}
},
{
"id": "paper",
"versionRange": "^3.6.1",
"additionalData": {}
}
]
},
"restoredDependencies": [
{
"dependency": {
"id": "paper",
"versionRange": "=3.6.1",
"additionalData": {
"soLink": "https://github.com/Fernthedev/paperlog/releases/download/v3.6.1/libpaperlog.so",
"debugSoLink": "https://github.com/Fernthedev/paperlog/releases/download/v3.6.1/debug_libpaperlog.so",
"overrideSoName": "libpaperlog.so",
"modLink": "https://github.com/Fernthedev/paperlog/releases/download/v3.6.1/paperlog.qmod",
"branchName": "version/v3_6_1",
"compileOptions": {
"systemIncludes": [
"shared/utfcpp/source"
]
},
"cmake": false
}
},
"version": "3.6.1"
},
{
"dependency": {
"id": "libil2cpp",
Expand All @@ -59,15 +84,34 @@
{
"dependency": {
"id": "beatsaber-hook",
"versionRange": "=5.0.9",
"versionRange": "=5.1.1",
"additionalData": {
"soLink": "https://github.com/QuestPackageManager/beatsaber-hook/releases/download/v5.0.9/libbeatsaber-hook_5_0_9.so",
"debugSoLink": "https://github.com/QuestPackageManager/beatsaber-hook/releases/download/v5.0.9/debug_libbeatsaber-hook_5_0_9.so",
"branchName": "version/v5_0_9",
"soLink": "https://github.com/QuestPackageManager/beatsaber-hook/releases/download/v5.1.1/libbeatsaber-hook_5_1_1.so",
"debugSoLink": "https://github.com/QuestPackageManager/beatsaber-hook/releases/download/v5.1.1/debug_libbeatsaber-hook_5_1_1.so",
"branchName": "version/v5_1_1",
"cmake": true
}
},
"version": "5.0.9"
"version": "5.1.1"
},
{
"dependency": {
"id": "fmt",
"versionRange": "=10.0.0",
"additionalData": {
"headersOnly": true,
"branchName": "version/v10_0_0",
"compileOptions": {
"systemIncludes": [
"fmt/include/"
],
"cppFlags": [
"-DFMT_HEADER_ONLY"
]
}
}
},
"version": "10.0.0"
},
{
"dependency": {
Expand Down
32 changes: 16 additions & 16 deletions shared/delegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct DelegateWrapperStatic : Il2CppObject {
struct ___TypeRegistration : ::custom_types::TypeRegistration {
___TypeRegistration() {
::custom_types::Register::AddType(this);
::custom_types::_logger().debug("Adding delegate type: %s", name());
::custom_types::logger.debug("Adding delegate type: {}", name());
instance = this;
}
std::vector<::custom_types::FieldRegistrator*> const getFields() const override {
Expand All @@ -53,10 +53,10 @@ struct DelegateWrapperStatic : Il2CppObject {
}
const char* name() const override {
if constexpr (!std::is_same_v<void, RI>) {
static std::string nm{string_format("DelegateWrapper(%zu ret, %zu args, %zu sz) (%d)", sizeof(RI), sizeof...(TArgsI), sizeof(___TargetType), custom_types::get_delegate_count())};
static std::string nm{fmt::format("DelegateWrapper({} ret, {} args, {} sz) ({})", sizeof(RI), sizeof...(TArgsI), sizeof(___TargetType), custom_types::get_delegate_count())};
return nm.c_str();
} else {
static std::string nm{string_format("DelegateWrapper(void ret, %zu args, %zu sz) (%d)", sizeof...(TArgsI), sizeof(___TargetType), custom_types::get_delegate_count())};
static std::string nm{fmt::format("DelegateWrapper(void ret, {} args, {} sz) ({})", sizeof...(TArgsI), sizeof(___TargetType), custom_types::get_delegate_count())};
return nm.c_str();
}
}
Expand Down Expand Up @@ -216,7 +216,7 @@ template<class RI, class... TArgsI>
RI __attribute__((noinline)) DelegateWrapperStatic<RI, TArgsI...>::Invoke(DelegateWrapperStatic<RI, TArgsI...>* instance, TArgsI... args) {
IL2CPP_CATCH_HANDLER(
if (!instance || !instance->wrappedFunc) {
custom_types::_logger().critical("Attempting to invoke delegate on a null or destroyed instance: %p, class: %p (%s)", instance, ___TypeRegistration::klass_ptr, ___TypeRegistration::get()->name());
custom_types::logger.critical("Attempting to invoke delegate on a null or destroyed instance: {}, class: {} ({})", fmt::ptr(instance), fmt::ptr(___TypeRegistration::klass_ptr), ___TypeRegistration::get()->name());
}
if constexpr (std::is_same_v<RI, void>) {
instance->wrappedFunc(args...);
Expand All @@ -227,7 +227,7 @@ RI __attribute__((noinline)) DelegateWrapperStatic<RI, TArgsI...>::Invoke(Delega
}
template<class RI, class... TArgsI>
void DelegateWrapperStatic<RI, TArgsI...>::dtor() {
custom_types::_logger().debug("Destroying delegate: %p, class: %p (%s)", this, ___TypeRegistration::klass_ptr, ___TypeRegistration::get()->name());
custom_types::logger.debug("Destroying delegate: {}, class: {} ({})", fmt::ptr(this), fmt::ptr(___TypeRegistration::klass_ptr), ___TypeRegistration::get()->name());
this->~DelegateWrapperStatic();
}

Expand All @@ -245,7 +245,7 @@ struct DelegateWrapperInstance : Il2CppObject {
struct ___TypeRegistration : ::custom_types::TypeRegistration {
___TypeRegistration() {
::custom_types::Register::AddType(this);
::custom_types::_logger().debug("Adding instance delegate type: %s", name());
::custom_types::logger.debug("Adding instance delegate type: {}", name());
instance = this;
}
std::vector<::custom_types::FieldRegistrator*> const getFields() const override {
Expand All @@ -266,10 +266,10 @@ struct DelegateWrapperInstance : Il2CppObject {
}
const char* name() const override {
if constexpr (!std::is_same_v<void, RI>) {
static std::string nm{string_format("DelegateWrapper(%zu ret, %zu args, %zu sz) (%d)", sizeof(RI), sizeof...(TArgsI), sizeof(___TargetType), custom_types::get_delegate_count())};
static std::string nm{fmt::format("DelegateWrapper({} ret, {} args, {} sz) ({})", sizeof(RI), sizeof...(TArgsI), sizeof(___TargetType), custom_types::get_delegate_count())};
return nm.c_str();
} else {
static std::string nm{string_format("DelegateWrapper(void ret, %zu args, %zu sz) (%d)", sizeof...(TArgsI), sizeof(___TargetType), custom_types::get_delegate_count())};
static std::string nm{fmt::format("DelegateWrapper(void ret, {} args, {} sz) ({})", sizeof...(TArgsI), sizeof(___TargetType), custom_types::get_delegate_count())};
return nm.c_str();
}
}
Expand Down Expand Up @@ -453,7 +453,7 @@ template<class RI, class TI, class... TArgsI>
RI __attribute__((noinline)) DelegateWrapperInstance<RI, TI, TArgsI...>::Invoke(DelegateWrapperInstance<RI, TI, TArgsI...>* instance, TArgsI... args) {
IL2CPP_CATCH_HANDLER(
if (!instance || !instance->wrappedFunc) {
custom_types::_logger().critical("Attempting to invoke instance delegate that is null or has been destroyed! %p, class: %p (%s)", instance, ___TypeRegistration::klass_ptr, ___TypeRegistration::get()->name());
custom_types::logger.critical("Attempting to invoke instance delegate that is null or has been destroyed! {}, class: {} ({})", fmt::ptr(instance), fmt::ptr(___TypeRegistration::klass_ptr), ___TypeRegistration::get()->name());
}
if constexpr (std::is_same_v<RI, void>) {
instance->wrappedFunc(instance->obj, args...);
Expand All @@ -464,7 +464,7 @@ RI __attribute__((noinline)) DelegateWrapperInstance<RI, TI, TArgsI...>::Invoke(
}
template<class RI, class TI, class... TArgsI>
void DelegateWrapperInstance<RI, TI, TArgsI...>::dtor() {
custom_types::_logger().debug("Destroying delegate: %p, class: %p (%s)", this, ___TypeRegistration::klass_ptr, ___TypeRegistration::get()->name());
custom_types::logger.debug("Destroying delegate: {}, class: {} ({})", fmt::ptr(this), fmt::ptr(___TypeRegistration::klass_ptr), ___TypeRegistration::get()->name());
this->~DelegateWrapperInstance();
}

Expand All @@ -480,7 +480,7 @@ T MakeDelegate(const Il2CppClass* delegateClass, DelegateWrapperStatic<R, TArgs.
// It should already have been allocated, so get will cache it for us.
// We need to ensure static initialization of both the dtor method registrator
// and the invoke method registrator:
custom_types::_logger().debug("Delegate dtor registrator: %p", DelegateWrapperStatic<R, TArgs...>::___dtor_MethodRegistrator.get());
custom_types::logger.debug("Delegate dtor registrator: {}", fmt::ptr(DelegateWrapperStatic<R, TArgs...>::___dtor_MethodRegistrator.get()));
auto* invokeMethod = CRASH_UNLESS(il2cpp_functions::class_get_method_from_name(delegateClass, "Invoke", -1));
auto* method = DelegateWrapperStatic<R, TArgs...>::___Invoke_MethodRegistrator.get();
setup_for_delegate(method);
Expand All @@ -500,14 +500,14 @@ T MakeDelegate(const Il2CppClass* delegateClass, DelegateWrapperStatic<R, TArgs.
);
CRASH_UNLESS(il2cpp_utils::RunMethodOpt<void, false>(delegate, ctor_minfo, inst, (void*)&method));

custom_types::_logger().debug("Created delegate: %p (%p), for instance: %p with MethodInfo*: %p", delegate, delegateClass, inst, method);
custom_types::logger.debug("Created delegate: {} ({}), for instance: {} with MethodInfo*: {}", fmt::ptr(delegate), fmt::ptr(delegateClass), fmt::ptr(inst), fmt::ptr(method));
log_delegate(reinterpret_cast<Il2CppDelegate*>(delegate));
return delegate;
}

template<class T = MulticastDelegate*, class R, class I, class... TArgs>
T MakeDelegate(const Il2CppClass* delegateClass, DelegateWrapperInstance<R, I, TArgs...>* inst) {
custom_types::_logger().debug("Delegate instance dtor registrator: %p", DelegateWrapperInstance<R, I, TArgs...>::___dtor_MethodRegistrator.get());
custom_types::logger.debug("Delegate instance dtor registrator: {}", fmt::ptr(DelegateWrapperInstance<R, I, TArgs...>::___dtor_MethodRegistrator.get()));
auto* invokeMethod = CRASH_UNLESS(il2cpp_functions::class_get_method_from_name(delegateClass, "Invoke", -1));
auto* method = DelegateWrapperInstance<R, I, TArgs...>::___Invoke_MethodRegistrator.get();
setup_for_delegate(method);
Expand All @@ -527,7 +527,7 @@ T MakeDelegate(const Il2CppClass* delegateClass, DelegateWrapperInstance<R, I, T
);
CRASH_UNLESS(il2cpp_utils::RunMethodOpt<void, false>(delegate, ctor_minfo, inst, (void*)&method));

custom_types::_logger().debug("Created instance delegate: %p (%p), for instance: %p with MethodInfo*: %p", delegate, delegateClass, inst, method);
custom_types::logger.debug("Created instance delegate: {} ({}), for instance: {} with MethodInfo*: {}", fmt::ptr(delegate), fmt::ptr(delegateClass), fmt::ptr(inst), fmt::ptr(method));
log_delegate(reinterpret_cast<Il2CppDelegate*>(delegate));
return delegate;
}
Expand All @@ -545,7 +545,7 @@ template<typename T = MulticastDelegate*, class R, class... TArgs>
T MakeDelegate(const Il2CppClass* delegateClass, std::function<R(TArgs...)> const& f) {
il2cpp_functions::Init();
// NOTE: This static field MUST be used in order for it to be instantiated within the generic, thus, it is important to NOT remove this log
custom_types::_logger().debug("Type registration for delegate being created (forcibly instantiated): %s", DelegateWrapperStatic<R, TArgs...>::__registration_instance_DelegateWrapperStatic.name());
custom_types::logger.debug("Type registration for delegate being created (forcibly instantiated): {}", DelegateWrapperStatic<R, TArgs...>::__registration_instance_DelegateWrapperStatic.name());
auto* wrapperInstance = reinterpret_cast<DelegateWrapperStatic<R, TArgs...>*>(il2cpp_functions::object_new(DelegateWrapperStatic<R, TArgs...>::___TypeRegistration::klass_ptr));
wrapperInstance->wrappedFunc = f;
return custom_types::MakeDelegate<T>(delegateClass, wrapperInstance);
Expand All @@ -568,7 +568,7 @@ template<typename T = MulticastDelegate*, class R, class I, class... TArgs>
// TODO: Requires that I has a classof
T MakeDelegate(const Il2CppClass* delegateClass, I obj, std::function<R (I, TArgs...)> const& f) {
il2cpp_functions::Init();
custom_types::_logger().debug("Type registration for delegate being created (forcibly instantiated): %s", DelegateWrapperInstance<R, I, TArgs...>::__registration_instance_DelegateWrapperInstance.name());
custom_types::logger.debug("Type registration for delegate being created (forcibly instantiated): {}", DelegateWrapperInstance<R, I, TArgs...>::__registration_instance_DelegateWrapperInstance.name());
auto* wrapperInstance = reinterpret_cast<DelegateWrapperInstance<R, I, TArgs...>*>(il2cpp_functions::object_new(DelegateWrapperInstance<R, I, TArgs...>::___TypeRegistration::klass_ptr));
wrapperInstance->wrappedFunc = f;
wrapperInstance->obj = obj;
Expand Down
2 changes: 1 addition & 1 deletion shared/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Il2CppRuntimeInterfaceOffsetPair;
namespace custom_types {
/// @brief Returns the logger used within custom types. Should not be called publicly.
/// @return The logger used internally
CUSTOM_TYPES_EXPORT Logger& _logger();
static constexpr auto logger = Paper::ConstLoggerContext("CustomTypes");

/// @brief Logs the provided Il2CppAssemblyName* with a provided label.
/// @param name The Il2CppAssemblyName* to log all fields on.
Expand Down
Loading

0 comments on commit 34f82c6

Please sign in to comment.