From 0c92551e8aa88cdeac4f9a578196a151af7a88bd Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Mon, 7 Oct 2024 10:18:16 -0400 Subject: [PATCH] Improve HOOK() --- example-mods/chat-commands/chat-commands.cpp | 6 ++---- libreborn/include/libreborn/util.h | 15 +++++++-------- media-layer/gles/src/passthrough.cpp | 3 +-- mods/src/compat/compat.cpp | 9 +++------ mods/src/misc/api.cpp | 3 +-- mods/src/misc/misc.cpp | 3 +-- mods/src/multidraw/glue.cpp | 9 +++------ mods/src/override/override.cpp | 12 ++++-------- mods/src/textures/textures.cpp | 6 ++---- 9 files changed, 24 insertions(+), 42 deletions(-) diff --git a/example-mods/chat-commands/chat-commands.cpp b/example-mods/chat-commands/chat-commands.cpp index de20cfef6e..4803c2766b 100644 --- a/example-mods/chat-commands/chat-commands.cpp +++ b/example-mods/chat-commands/chat-commands.cpp @@ -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 *, (Minecraft *minecraft, ServerSideNetworkHandler *server_side_network_handler)) { // Call Original Method - ensure_server_get_commands(); - std::vector *commands = real_server_get_commands(minecraft, server_side_network_handler); + std::vector *commands = real_server_get_commands()(minecraft, server_side_network_handler); // Add Command commands->push_back({ .name = "greet", diff --git a/libreborn/include/libreborn/util.h b/libreborn/include/libreborn/util.h index 3c2ac4c515..315edc18a6 100644 --- a/libreborn/include/libreborn/util.h +++ b/libreborn/include/libreborn/util.h @@ -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 diff --git a/media-layer/gles/src/passthrough.cpp b/media-layer/gles/src/passthrough.cpp index 5a130ad8aa..a4362744e4 100644 --- a/media-layer/gles/src/passthrough.cpp +++ b/media-layer/gles/src/passthrough.cpp @@ -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); \ diff --git a/mods/src/compat/compat.cpp b/mods/src/compat/compat.cpp index 7f57c9dd88..ac372078c8 100644 --- a/mods/src/compat/compat.cpp +++ b/mods/src/compat/compat.cpp @@ -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) { diff --git a/mods/src/misc/api.cpp b/mods/src/misc/api.cpp index 48ee22fc3a..ebf82c8683 100644 --- a/mods/src/misc/api.cpp +++ b/mods/src/misc/api.cpp @@ -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 diff --git a/mods/src/misc/misc.cpp b/mods/src/misc/misc.cpp index 8ae6952ccb..516027ea89 100644 --- a/mods/src/misc/misc.cpp +++ b/mods/src/misc/misc.cpp @@ -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 diff --git a/mods/src/multidraw/glue.cpp b/mods/src/multidraw/glue.cpp index 6f94eed7e4..d56693bda0 100644 --- a/mods/src/multidraw/glue.cpp +++ b/mods/src/multidraw/glue.cpp @@ -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); } } diff --git a/mods/src/override/override.cpp b/mods/src/override/override.cpp index 1d2a9d2d6f..ab0bbee159 100644 --- a/mods/src/override/override.cpp +++ b/mods/src/override/override.cpp @@ -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); diff --git a/mods/src/textures/textures.cpp b/mods/src/textures/textures.cpp index 49c4e2dd21..8b7fc18788 100644 --- a/mods/src/textures/textures.cpp +++ b/mods/src/textures/textures.cpp @@ -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