minecraft-pi-reborn/libreborn/include/libreborn/patch.h

84 lines
2.9 KiB
C
Raw Permalink Normal View History

#pragma once
#ifdef __cplusplus
extern "C" {
#endif
// Patching Functions
2022-03-14 23:09:25 +00:00
#ifdef REBORN_HAS_PATCH_CODE
2024-02-12 05:44:38 +00:00
void reborn_init_patch();
2024-05-05 00:46:15 +00:00
// Replace Call Located At start With A Call To target
void _overwrite_call(const char *file, int line, void *start, void *target);
2024-04-03 07:19:12 +00:00
#define overwrite_call(start, target) _overwrite_call(__FILE__, __LINE__, start, target)
2024-05-05 00:46:15 +00:00
#define _check_if_method_is_new(name) \
2024-04-03 07:19:12 +00:00
{ \
2024-05-05 00:46:15 +00:00
if (!__is_new_method_##name()) { \
ERR("Method Is Not \"New\""); \
2024-04-03 07:19:12 +00:00
} \
}
2024-05-05 00:46:15 +00:00
#define _setup_fancy_overwrite(start, name, target) \
_check_if_method_is_new(name); \
static name##_t _original_for_##target = start; \
static name##_t _helper_for_##target = __overwrite_helper_for_##name(target, _original_for_##target)
// Replace All Calls To Method start With target
void _overwrite_calls(const char *file, int line, void *start, void *target);
2024-04-03 07:19:12 +00:00
#define overwrite_calls_manual(start, target) _overwrite_calls(__FILE__, __LINE__, start, target)
#define overwrite_calls(start, target) \
{ \
_setup_fancy_overwrite(start, start, target); \
overwrite_calls_manual((void *) start, (void *) _helper_for_##target); \
2024-05-05 00:46:15 +00:00
start = _helper_for_##target; \
2024-04-03 07:19:12 +00:00
}
2024-05-05 00:46:15 +00:00
// Replace All Calls To Virtual Method start With target
2024-04-03 07:19:12 +00:00
#define overwrite_virtual_calls(start, target) \
{ \
_setup_fancy_overwrite(*start##_vtable_addr, start, target); \
overwrite_calls_manual((void *) *start##_vtable_addr, (void *) _helper_for_##target); \
}
2024-05-05 00:46:15 +00:00
// Replace All Calls To start With target Within [to, from)
2023-12-25 22:29:30 +00:00
void _overwrite_calls_within(const char *file, int line, void *from, void *to, void *start, void *target);
2024-04-03 07:19:12 +00:00
#define overwrite_calls_within(from, to, start, target) _overwrite_calls_within(__FILE__, __LINE__, from, to, start, target)
2023-12-25 22:29:30 +00:00
2024-05-05 00:46:15 +00:00
// Get Target Address From BL Instruction
2022-06-25 21:30:08 +00:00
void *extract_from_bl_instruction(unsigned char *from);
2024-05-05 00:46:15 +00:00
// Replace Method start With target
void _overwrite(const char *file, int line, void *start, void *target);
2024-05-05 00:46:15 +00:00
#define overwrite_manual(start, target) _overwrite(__FILE__, __LINE__, (void *) start, (void *) target)
#define overwrite(start, target) \
{ \
_check_if_method_is_new(start); \
start##_t type_check = target; \
overwrite_manual(start, (void *) type_check); \
}
2024-05-05 00:46:15 +00:00
// Patch Instruction
void _patch(const char *file, int line, void *start, unsigned char patch[4]);
2024-04-03 07:19:12 +00:00
#define patch(start, patch) _patch(__FILE__, __LINE__, start, patch)
2024-05-05 00:46:15 +00:00
// Patch 4 Bytes Of Data
void _patch_address(const char *file, int line, void *start, void *target);
2024-04-03 07:19:12 +00:00
#define patch_address(start, target) _patch_address(__FILE__, __LINE__, (void *) start, (void *) target)
2024-05-05 00:46:15 +00:00
// Patch VTable Entry
// This does not affect sub-classes.
#define patch_vtable(start, target) \
{ \
start##_t type_check = target; \
patch_address(start##_vtable_addr, (void *) type_check); \
}
2022-03-14 23:09:25 +00:00
#endif
#ifdef __cplusplus
}
#endif