Improve HOOK()

This commit is contained in:
TheBrokenRail 2024-10-07 10:18:16 -04:00
parent 6388663b6c
commit 0c92551e8a
9 changed files with 24 additions and 42 deletions

View File

@ -20,16 +20,14 @@ HOOK(chat_handle_packet_send, void, (const Minecraft *minecraft, ChatPacket *pac
gui->addMessage(out);
} else {
// Call Original Method
ensure_chat_handle_packet_send();
real_chat_handle_packet_send(minecraft, packet);
real_chat_handle_packet_send()(minecraft, packet);
}
}
// Add Server Command
HOOK(server_get_commands, std::vector<ServerCommand> *, (Minecraft *minecraft, ServerSideNetworkHandler *server_side_network_handler)) {
// Call Original Method
ensure_server_get_commands();
std::vector<ServerCommand> *commands = real_server_get_commands(minecraft, server_side_network_handler);
std::vector<ServerCommand> *commands = real_server_get_commands()(minecraft, server_side_network_handler);
// Add Command
commands->push_back({
.name = "greet",

View File

@ -34,19 +34,18 @@
#define hooked_function_setup
#endif
#define HOOK(name, return_type, args) \
typedef return_type (*name##_t)args; \
static name##_t real_##name = NULL; \
\
__attribute__((__unused__)) static void ensure_##name() { \
if (!real_##name) { \
typedef return_type (*real_##name##_t)args; \
__attribute__((__unused__)) static real_##name##_t real_##name() { \
static real_##name##_t func = NULL; \
if (!func) { \
dlerror(); \
real_##name = (name##_t) dlsym(RTLD_NEXT, #name); \
if (!real_##name) { \
func = (real_##name##_t) dlsym(RTLD_NEXT, #name); \
if (!func) { \
ERR("Error Resolving Symbol: " #name ": %s", dlerror()); \
} \
} \
return func; \
} \
\
hooked_function_setup __attribute__((__used__)) return_type name args
#ifdef __cplusplus

View File

@ -7,8 +7,7 @@
// Load GL Function
#define GL_FUNC(name, return_type, args) \
typedef return_type (*real_##name##_t)args; \
\
real_##name##_t real_##name() { \
static real_##name##_t real_##name() { \
static real_##name##_t func = nullptr; \
if (!func) { \
func = (real_##name##_t) glfwGetProcAddress(#name); \

View File

@ -17,15 +17,13 @@
// Custom Title
HOOK(SDL_WM_SetCaption, void, (__attribute__((unused)) const char *title, const char *icon)) {
ensure_SDL_WM_SetCaption();
real_SDL_WM_SetCaption(MCPI_APP_TITLE, icon);
real_SDL_WM_SetCaption()(MCPI_APP_TITLE, icon);
}
// Mouse Cursor Is Always Invisible In Vanilla MCPI
// Because In Vanilla MCPI, The GPU Overlay Covered The Normal Mouse Cursor
HOOK(SDL_ShowCursor, int, (int toggle)) {
ensure_SDL_ShowCursor();
return real_SDL_ShowCursor(toggle == SDL_QUERY ? SDL_QUERY : SDL_DISABLE);
return real_SDL_ShowCursor()(toggle == SDL_QUERY ? SDL_QUERY : SDL_DISABLE);
}
// Intercept SDL Events
@ -40,8 +38,7 @@ HOOK(SDL_PollEvent, int, (SDL_Event *event)) {
}
// Poll Events
ensure_SDL_PollEvent();
int ret = real_SDL_PollEvent(event);
int ret = real_SDL_PollEvent()(event);
// Handle Events
if (ret == 1 && event != nullptr) {

View File

@ -43,8 +43,7 @@ static void Inventory_setupDefault_FillingContainer_addItem_call_injection(Filli
SETUP_CALLBACK(swap_buffers);
HOOK(media_swap_buffers, void, ()) {
get_misc_swap_buffers_functions().run();
ensure_media_swap_buffers();
real_media_swap_buffers();
real_media_swap_buffers()();
}
// API

View File

@ -145,8 +145,7 @@ HOOK(bind, int, (int sockfd, const struct sockaddr *addr, socklen_t addrlen)) {
}
new_addr = (const sockaddr *) &in_addr;
}
ensure_bind();
return real_bind(sockfd, new_addr, addrlen);
return real_bind()(sockfd, new_addr, addrlen);
}
// Generate Caves

View File

@ -26,8 +26,7 @@ HOOK(glDeleteBuffers, void, (GLsizei n, const GLuint *buffers)) {
if (buffers[0] >= MULTIDRAW_BASE) {
delete storage;
} else {
ensure_glDeleteBuffers();
real_glDeleteBuffers(n, buffers);
real_glDeleteBuffers()(n, buffers);
}
}
@ -40,15 +39,13 @@ HOOK(glBindBuffer, void, (const GLenum target, GLuint buffer)) {
} else {
current_chunk = -1;
}
ensure_glBindBuffer();
real_glBindBuffer(target, buffer);
real_glBindBuffer()(target, buffer);
}
HOOK(glBufferData, void, (GLenum target, GLsizeiptr size, const void *data, GLenum usage)) {
if (target == GL_ARRAY_BUFFER && current_chunk >= 0 && storage != nullptr) {
storage->upload(current_chunk, size, data);
} else {
ensure_glBufferData();
real_glBufferData(target, size, data, usage);
real_glBufferData()(target, size, data, usage);
}
}

View File

@ -16,8 +16,7 @@
HOOK(access, int, (const char *pathname, int mode)) {
char *new_path = override_get_path(pathname);
// Open File
ensure_access();
const int ret = real_access(new_path != nullptr ? new_path : pathname, mode);
const int ret = real_access()(new_path != nullptr ? new_path : pathname, mode);
// Free Data
if (new_path != nullptr) {
free(new_path);
@ -61,8 +60,7 @@ char *override_get_path(const char *filename) {
// Test Asset Folders
for (int i = 0; !asset_folders[i].empty(); i++) {
new_path = asset_folders[i] + '/' + &filename[data_prefix_length];
ensure_access();
if (real_access(new_path.c_str(), F_OK) == -1) {
if (real_access()(new_path.c_str(), F_OK) == -1) {
// Not Found In Asset Folder
new_path = "";
continue;
@ -87,8 +85,7 @@ char *override_get_path(const char *filename) {
HOOK(fopen, FILE *, (const char *filename, const char *mode)) {
char *new_path = override_get_path(filename);
// Open File
ensure_fopen();
FILE *file = real_fopen(new_path != nullptr ? new_path : filename, mode);
FILE *file = real_fopen()(new_path != nullptr ? new_path : filename, mode);
// Free Data
if (new_path != nullptr) {
free(new_path);
@ -101,8 +98,7 @@ HOOK(fopen, FILE *, (const char *filename, const char *mode)) {
HOOK(fopen64, FILE *, (const char *filename, const char *mode)) {
char *new_path = override_get_path(filename);
// Open File
ensure_fopen64();
FILE *file = real_fopen64(new_path != nullptr ? new_path : filename, mode);
FILE *file = real_fopen64()(new_path != nullptr ? new_path : filename, mode);
// Free Data
if (new_path != nullptr) {
free(new_path);

View File

@ -43,8 +43,7 @@ HOOK(glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsi
get_texture_data().push_back(data);
// Call Original Method
ensure_glTexImage2D();
real_glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);
real_glTexImage2D()(target, level, internalformat, width, height, border, format, type, pixels);
}
HOOK(glDeleteTextures, void, (GLsizei n, const GLuint *textures)) {
// Remove Old Data
@ -62,8 +61,7 @@ HOOK(glDeleteTextures, void, (GLsizei n, const GLuint *textures)) {
}
// Call Original Method
ensure_glDeleteTextures();
real_glDeleteTextures(n, textures);
real_glDeleteTextures()(n, textures);
}
static void get_texture_size(const GLint id, GLsizei *width, GLsizei *height) {
// Iterate