From 6f433ff58ff264a608436a1bc4c8a3ef2f4995b5 Mon Sep 17 00:00:00 2001 From: Timothy Collett Date: Fri, 29 Jun 2012 10:15:48 -0400 Subject: [PATCH] Move back to using interposition to be more portable --- library/Hooks-darwin.cpp | 26 +++++++++++++++++--------- library/include/Core.h | 7 +++++++ library/include/Hooks.h | 6 ++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/library/Hooks-darwin.cpp b/library/Hooks-darwin.cpp index ef89105d4..190616b86 100644 --- a/library/Hooks-darwin.cpp +++ b/library/Hooks-darwin.cpp @@ -38,11 +38,11 @@ distribution. #include #include -/*typedef struct interpose_s +typedef struct interpose_s { void *new_func; void *orig_func; -} interpose_t;*/ +} interpose_t; #include "DFHack.h" #include "Core.h" @@ -58,11 +58,18 @@ distribution. };*/ +#define DYLD_INTERPOSE(_replacment,_replacee) __attribute__((used)) static struct{ const void* replacment; const void* replacee; } _interpose_##_replacee __attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacment, (const void*)(unsigned long)&_replacee }; + +DYLD_INTERPOSE(DFH_SDL_Init,SDL_Init); +DYLD_INTERPOSE(DFH_SDL_PollEvent,SDL_PollEvent); +DYLD_INTERPOSE(DFH_SDL_Quit,SDL_Quit); +DYLD_INTERPOSE(DFH_SDL_NumJoysticks,SDL_NumJoysticks); + /******************************************************************************* * SDL part starts here * *******************************************************************************/ // hook - called for each game tick (or more often) -DFhackCExport int SDL_NumJoysticks(void) +DFhackCExport int DFH_SDL_NumJoysticks(void) { DFHack::Core & c = DFHack::Core::getInstance(); return c.Update(); @@ -70,7 +77,7 @@ DFhackCExport int SDL_NumJoysticks(void) // hook - called at program exit static void (*_SDL_Quit)(void) = 0; -DFhackCExport void SDL_Quit(void) +DFhackCExport void DFH_SDL_Quit(void) { DFHack::Core & c = DFHack::Core::getInstance(); c.Shutdown(); @@ -79,16 +86,16 @@ DFhackCExport void SDL_Quit(void) _SDL_Quit(); }*/ - _SDL_Quit(); + SDL_Quit(); } // called by DF to check input events static int (*_SDL_PollEvent)(SDL::Event* event) = 0; -DFhackCExport int SDL_PollEvent(SDL::Event* event) +DFhackCExport int DFH_SDL_PollEvent(SDL::Event* event) { pollevent_again: // if SDL returns 0 here, it means there are no more events. return 0 - int orig_return = _SDL_PollEvent(event); + int orig_return = SDL_PollEvent(event); if(!orig_return) return 0; // otherwise we have an event to filter @@ -128,7 +135,7 @@ DFhackCExport int wgetch(WINDOW *win) // hook - called at program start, initialize some stuffs we'll use later static int (*_SDL_Init)(uint32_t flags) = 0; -DFhackCExport int SDL_Init(uint32_t flags) +DFhackCExport int DFH_SDL_Init(uint32_t flags) { // reroute stderr fprintf(stderr,"dfhack: attempting to hook in\n"); @@ -158,6 +165,7 @@ DFhackCExport int SDL_Init(uint32_t flags) DFHack::Core & c = DFHack::Core::getInstance(); //c.Init(); - int ret = _SDL_Init(flags); + //int ret = _SDL_Init(flags); + int ret = SDL_Init(flags); return ret; } \ No newline at end of file diff --git a/library/include/Core.h b/library/include/Core.h index e4d1080d6..653298d8f 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -83,10 +83,17 @@ namespace DFHack // Better than tracking some weird variables all over the place. class DFHACK_EXPORT Core { +#ifdef _DARWIN + friend int ::DFH_SDL_NumJoysticks(void); + friend void ::DFH_SDL_Quit(void); + friend int ::DFH_SDL_PollEvent(SDL::Event *); + friend int ::DFH_SDL_Init(uint32_t flags); +#else friend int ::SDL_NumJoysticks(void); friend void ::SDL_Quit(void); friend int ::SDL_PollEvent(SDL::Event *); friend int ::SDL_Init(uint32_t flags); +#endif friend int ::wgetch(WINDOW * w); friend int ::egg_init(void); friend int ::egg_shutdown(void); diff --git a/library/include/Hooks.h b/library/include/Hooks.h index 418a5ce3b..325af43eb 100644 --- a/library/include/Hooks.h +++ b/library/include/Hooks.h @@ -44,6 +44,12 @@ namespace SDL // these functions are here because they call into DFHack::Core and therefore need to // be declared as friend functions/known +#ifdef _DARWIN +DFhackCExport int DFH_SDL_NumJoysticks(void); +DFhackCExport void DFH_SDL_Quit(void); +DFhackCExport int DFH_SDL_PollEvent(SDL::Event* event); +DFhackCExport int DFH_SDL_Init(uint32_t flags); +#endif DFhackCExport int SDL_NumJoysticks(void); DFhackCExport void SDL_Quit(void); DFhackCExport int SDL_PollEvent(SDL::Event* event);