Skip to content

Commit

Permalink
library: add proper verbosity setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamcake committed Dec 11, 2024
1 parent 6c05d9d commit 9e7d172
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ target_compile_definitions(${BOLT_PLUGIN_LIB_NAME} PUBLIC SPNG_STATIC=1 SPNG_USE
if(MSVC)
target_compile_definitions(${BOLT_PLUGIN_LIB_NAME} PUBLIC _USE_MATH_DEFINES=1)
endif()
if(BOLT_LIBRARY_VERBOSE)
target_compile_definitions(${BOLT_PLUGIN_LIB_NAME} PUBLIC VERBOSE)
endif()
8 changes: 4 additions & 4 deletions src/library/dll/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ static int game_height = 0;

static HCURSOR cursor_default;

//#define VERBOSE

// -D BOLT_LIBRARY_VERBOSE=1
#if defined(VERBOSE)
static LPCWSTR logfilename = L"bolt-log.txt";
static FILE* logfile = 0;
#define LOG(...) if(fprintf(logfile, __VA_ARGS__))fflush(logfile)
#else
Expand Down Expand Up @@ -263,7 +263,7 @@ static void* bolt_GetProcAddress(const char* proc) {
static HGLRC WINAPI hook_wglCreateContext(HDC hdc) {
EnterCriticalSection(&wgl_lock);
#if defined(VERBOSE)
if (!logfile) _wfopen_s(&logfile, L"bolt-log.txt", L"wb");
if (!logfile) { _wfopen_s(&logfile, logfilename, L"wb"); _bolt_gl_set_logfile(logfile); }
#endif
HGLRC ret = real_wglCreateContext(hdc);
if (ret) _bolt_gl_onCreateContext(ret, NULL, &libgl, bolt_GetProcAddress, false);
Expand Down Expand Up @@ -316,7 +316,7 @@ static BOOL WINAPI hook_SwapBuffers(HDC hdc) {

static HWND WINAPI hook_CreateWindowExA(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) {
#if defined(VERBOSE)
if (!logfile) _wfopen_s(&logfile, L"bolt-log.txt", L"wb");
if (!logfile) { _wfopen_s(&logfile, logfilename, L"wb"); _bolt_gl_set_logfile(logfile); }
#endif
LOG("CreateWindowExA(class=\"%s\", name=\"%s\", x=%i, y=%i, w=%i, h=%i)\n", lpClassName, lpWindowName, X, Y, nWidth, nHeight);
HWND ret = real_CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
Expand Down
9 changes: 4 additions & 5 deletions src/library/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
#include <stdlib.h>
#include <string.h>

// comment or uncomment this to enable verbose logging of hooks in this file
//#define VERBOSE

// don't change this part, change the line above this instead
// -D BOLT_LIBRARY_VERBOSE=1
#if defined(VERBOSE)
#define LOG(...) printf(__VA_ARGS__);fflush(stdout)
static FILE* logfile;
void _bolt_gl_set_logfile(FILE* f) { logfile = f; }
#define LOG(...) if(fprintf(logfile, __VA_ARGS__))fflush(logfile)
#else
#define LOG(...)
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/library/gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <stdbool.h>
#include <stdint.h>

#if defined(VERBOSE)
#include <stdio.h>
void _bolt_gl_set_logfile(FILE* f);
#endif

/* types from gl.h */
typedef unsigned int GLenum;
typedef unsigned char GLboolean;
Expand Down
30 changes: 18 additions & 12 deletions src/library/so/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
#include "../plugin/plugin.h"
#include "../../../modules/hashmap/hashmap.h"

// comment or uncomment this to enable verbose logging of hooks in this file
//#define VERBOSE

// don't change this part, change the line above this instead
// -D BOLT_LIBRARY_VERBOSE=1
#if defined(VERBOSE)
#define LOG(...) if(printf(__VA_ARGS__)) fflush(stdout)
#else
#define LOG(...)
#endif

#if defined(XCBVERBOSE)
#define XCBLOG(...) if(printf(__VA_ARGS__)) fflush(stdout)
#else
#define XCBLOG(...)
#endif

// note: this is currently always triggered by single-threaded dlopen calls so no locking necessary
static uint8_t inited = 0;
#define INIT() if (!inited) _bolt_init_functions();
Expand Down Expand Up @@ -310,6 +313,9 @@ static void _bolt_init_functions() {
_bolt_plugin_on_startup();
pthread_mutex_init(&egl_lock, NULL);
dl_iterate_phdr(_bolt_dl_iterate_callback, NULL);
#if defined(VERBOSE)
_bolt_gl_set_logfile(stdout);
#endif
inited = 1;
}

Expand Down Expand Up @@ -623,49 +629,49 @@ static uint8_t _bolt_handle_xcb_event(xcb_connection_t* c, xcb_generic_event_t*
}

xcb_generic_event_t* xcb_poll_for_event(xcb_connection_t* c) {
LOG("xcb_poll_for_event\n");
XCBLOG("xcb_poll_for_event\n");
xcb_generic_event_t* ret = real_xcb_poll_for_event(c);
while (true) {
if (_bolt_handle_xcb_event(c, ret)) {
LOG("xcb_poll_for_event end\n");
XCBLOG("xcb_poll_for_event end\n");
return ret;
}
ret = real_xcb_poll_for_queued_event(c);
}
}

xcb_generic_event_t* xcb_poll_for_queued_event(xcb_connection_t* c) {
LOG("xcb_poll_for_queued_event\n");
XCBLOG("xcb_poll_for_queued_event\n");
xcb_generic_event_t* ret;
while (true) {
ret = real_xcb_poll_for_queued_event(c);
if (_bolt_handle_xcb_event(c, ret)) {
LOG("xcb_poll_for_queued_event end\n");
XCBLOG("xcb_poll_for_queued_event end\n");
return ret;
}
}
}

xcb_generic_event_t* xcb_wait_for_event(xcb_connection_t* c) {
LOG("xcb_wait_for_event\n");
XCBLOG("xcb_wait_for_event\n");
xcb_generic_event_t* ret;
while (true) {
ret = real_xcb_wait_for_event(c);
if (_bolt_handle_xcb_event(c, ret)) {
LOG("xcb_wait_for_event end\n");
XCBLOG("xcb_wait_for_event end\n");
return ret;
}
}
}

xcb_get_geometry_reply_t* xcb_get_geometry_reply(xcb_connection_t* c, xcb_get_geometry_cookie_t cookie, xcb_generic_error_t** e) {
LOG("xcb_get_geometry_reply\n");
XCBLOG("xcb_get_geometry_reply\n");
// currently the game appears to call this twice per frame for the main window and never for
// any other window, so we can assume the result here applies to the main window.
xcb_get_geometry_reply_t* ret = real_xcb_get_geometry_reply(c, cookie, e);
main_window_width = ret->width;
main_window_height = ret->height;
LOG("xcb_get_geometry_reply end (returned %u %u)\n", main_window_width, main_window_height);
XCBLOG("xcb_get_geometry_reply end (returned %u %u)\n", main_window_width, main_window_height);
return ret;
}

Expand Down

0 comments on commit 9e7d172

Please sign in to comment.