Add eggy hooks (linux only for now)

develop
Petr Mrázek 2012-02-28 03:37:56 +01:00
parent f8721c88b5
commit 731472a478
11 changed files with 266 additions and 62 deletions

@ -40,9 +40,11 @@ add_definitions(-DDFHACK_VERSION="${DFHACK_VERSION}")
IF(UNIX) IF(UNIX)
# put the lib into DF/hack # put the lib into DF/hack
SET(DFHACK_LIBRARY_DESTINATION hack) SET(DFHACK_LIBRARY_DESTINATION hack)
SET(DFHACK_EGGY_DESTINATION libs)
ELSE() ELSE()
# windows is crap, therefore we can't do nice things with it. leave the libs on a nasty pile... # windows is crap, therefore we can't do nice things with it. leave the libs on a nasty pile...
SET(DFHACK_LIBRARY_DESTINATION .) SET(DFHACK_LIBRARY_DESTINATION .)
SET(DFHACK_EGGY_DESTINATION .)
ENDIF() ENDIF()
# external tools will be installed here: # external tools will be installed here:
SET(DFHACK_BINARY_DESTINATION .) SET(DFHACK_BINARY_DESTINATION .)

@ -103,9 +103,6 @@ modules/World.cpp
modules/Graphic.cpp modules/Graphic.cpp
) )
SET(PROJECT_HDRS_LINUX
)
SET(PROJECT_HDRS_WINDOWS SET(PROJECT_HDRS_WINDOWS
include/wdirent.h include/wdirent.h
) )
@ -113,18 +110,31 @@ include/wdirent.h
SET(PROJECT_SRCS_LINUX SET(PROJECT_SRCS_LINUX
Console-linux.cpp Console-linux.cpp
Hooks-linux.cpp Hooks-linux.cpp
PlugLoad-linux.cpp
Process-linux.cpp Process-linux.cpp
) )
SET(PROJECT_SRCS_WINDOWS SET(PROJECT_SRCS_WINDOWS
Console-windows.cpp Console-windows.cpp
Hooks-windows.cpp Hooks-windows.cpp
PlugLoad-windows.cpp
Process-windows.cpp Process-windows.cpp
) )
SET(PROJECT_SRCS_LINUX_EGG
Console-linux.cpp
Hooks-egg.cpp
PlugLoad-linux.cpp
Process-linux.cpp
)
IF(UNIX) IF(UNIX)
LIST(APPEND PROJECT_HDRS ${PROJECT_HDRS_LINUX}) OPTION(BUILD_EGGY "Make DFHack strangely egg-shaped." OFF)
IF(BUILD_EGGY)
LIST(APPEND PROJECT_SRCS ${PROJECT_SRCS_LINUX_EGG})
ELSE()
LIST(APPEND PROJECT_SRCS ${PROJECT_SRCS_LINUX}) LIST(APPEND PROJECT_SRCS ${PROJECT_SRCS_LINUX})
ENDIF()
ELSE() ELSE()
LIST(APPEND PROJECT_HDRS ${PROJECT_HDRS_WINDOWS}) LIST(APPEND PROJECT_HDRS ${PROJECT_HDRS_WINDOWS})
LIST(APPEND PROJECT_SRCS ${PROJECT_SRCS_WINDOWS}) LIST(APPEND PROJECT_SRCS ${PROJECT_SRCS_WINDOWS})
@ -174,9 +184,13 @@ ENDIF()
ADD_LIBRARY(dfhack SHARED ${PROJECT_SRCS}) ADD_LIBRARY(dfhack SHARED ${PROJECT_SRCS})
ADD_DEPENDENCIES(dfhack generate_headers) ADD_DEPENDENCIES(dfhack generate_headers)
IF(WIN32) IF(BUILD_EGGY)
SET_TARGET_PROPERTIES(dfhack PROPERTIES OUTPUT_NAME "egg" )
else()
IF(WIN32)
SET_TARGET_PROPERTIES(dfhack PROPERTIES OUTPUT_NAME "SDL" ) SET_TARGET_PROPERTIES(dfhack PROPERTIES OUTPUT_NAME "SDL" )
ENDIF() ENDIF()
endif()
#effectively disables debug builds... #effectively disables debug builds...
SET_TARGET_PROPERTIES(dfhack PROPERTIES DEBUG_POSTFIX "-debug" ) SET_TARGET_PROPERTIES(dfhack PROPERTIES DEBUG_POSTFIX "-debug" )
@ -188,15 +202,23 @@ IF(UNIX)
install(PROGRAMS ${dfhack_SOURCE_DIR}/package/linux/dfhack install(PROGRAMS ${dfhack_SOURCE_DIR}/package/linux/dfhack
DESTINATION .) DESTINATION .)
ELSE() ELSE()
if(NOT BUILD_EGGY)
# On windows, copy the renamed SDL so DF can still run. # On windows, copy the renamed SDL so DF can still run.
install(PROGRAMS ${dfhack_SOURCE_DIR}/package/windows/SDLreal.dll install(PROGRAMS ${dfhack_SOURCE_DIR}/package/windows/SDLreal.dll
DESTINATION ${DFHACK_LIBRARY_DESTINATION}) DESTINATION ${DFHACK_LIBRARY_DESTINATION})
endif()
ENDIF() ENDIF()
#install the main lib #install the main lib
install(TARGETS dfhack if(NOT BUILD_EGGY)
install(TARGETS dfhack
LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION} LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION}
RUNTIME DESTINATION ${DFHACK_LIBRARY_DESTINATION}) RUNTIME DESTINATION ${DFHACK_LIBRARY_DESTINATION})
else()
install(TARGETS dfhack
LIBRARY DESTINATION ${DFHACK_EGGY_DESTINATION}
RUNTIME DESTINATION ${DFHACK_EGGY_DESTINATION})
endif()
#install the offset file #install the offset file
install(FILES xml/symbols.xml install(FILES xml/symbols.xml
DESTINATION ${DFHACK_DATA_DESTINATION}) #linux: share/dfhack DESTINATION ${DFHACK_DATA_DESTINATION}) #linux: share/dfhack
@ -204,6 +226,7 @@ install(FILES xml/symbols.xml
install(FILES ../dfhack.init-example install(FILES ../dfhack.init-example
DESTINATION ${DFHACK_BINARY_DESTINATION}) DESTINATION ${DFHACK_BINARY_DESTINATION})
# Unused for so long that it's not even relevant now...
if(BUILD_DEVEL) if(BUILD_DEVEL)
if(WIN32) if(WIN32)
install(TARGETS dfhack install(TARGETS dfhack

@ -851,6 +851,7 @@ int Core::Shutdown ( void )
return -1; return -1;
} }
// FIXME: this is HORRIBLY broken
// from ncurses // from ncurses
#define KEY_F0 0410 /* Function keys. Space for 64 */ #define KEY_F0 0410 /* Function keys. Space for 64 */
#define KEY_F(n) (KEY_F0+(n)) /* Value of function key n */ #define KEY_F(n) (KEY_F0+(n)) /* Value of function key n */
@ -887,12 +888,12 @@ bool Core::ncurses_wgetch(int in, int & out)
return true; return true;
} }
int Core::SDL_Event(SDL::Event* ev, int orig_return) int Core::SDL_Event(SDL::Event* ev)
{ {
// do NOT process events before we are ready. // do NOT process events before we are ready.
if(!started) return orig_return; if(!started) return true;
if(!ev) if(!ev)
return orig_return; return true;
if(ev && ev->type == SDL::ET_KEYDOWN || ev->type == SDL::ET_KEYUP) if(ev && ev->type == SDL::ET_KEYDOWN || ev->type == SDL::ET_KEYUP)
{ {
SDL::KeyboardEvent * ke = (SDL::KeyboardEvent *)ev; SDL::KeyboardEvent * ke = (SDL::KeyboardEvent *)ev;
@ -913,7 +914,7 @@ int Core::SDL_Event(SDL::Event* ev, int orig_return)
hotkey_states[ke->ksym.sym] = false; hotkey_states[ke->ksym.sym] = false;
} }
} }
return orig_return; return true;
// do stuff with the events... // do stuff with the events...
} }

@ -0,0 +1,91 @@
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <string>
#include <map>
#include "Core.h"
#include "Hooks.h"
#include <iostream>
// hook - called before rendering
DFhackCExport int egg_init(void)
{
// reroute stderr
freopen("stderr.log", "w", stderr);
// we don't reroute stdout until we figure out if this should be done at all
// See: Console-linux.cpp
fprintf(stderr,"dfhack: hooking successful\n");
return true;
}
// hook - called before rendering
DFhackCExport int egg_shutdown(void)
{
DFHack::Core & c = DFHack::Core::getInstance();
return c.Shutdown();
}
// hook - called for each game tick (or more often)
DFhackCExport int egg_tick(void)
{
DFHack::Core & c = DFHack::Core::getInstance();
return c.Update();
}
// hook - called before rendering
DFhackCExport int egg_prerender(void)
{
return true;
}
// hook - called for each SDL event, returns 0 when the event has been consumed. 1 otherwise
DFhackCExport int egg_sdl_event(SDL::Event* event)
{
// if the event is valid, intercept
if( event != 0 )
{
DFHack::Core & c = DFHack::Core::getInstance();
return c.SDL_Event(event);
}
return true;
}
// return this if you want to kill the event.
const int curses_error = -1;
// hook - ncurses event, -1 signifies error.
DFhackCExport int egg_curses_event(int orig_return)
{
/*
if(orig_return != -1)
{
DFHack::Core & c = DFHack::Core::getInstance();
int out;
return c.ncurses_wgetch(orig_return,);
}*/
return orig_return;
}

@ -43,31 +43,6 @@ distribution.
#include "Hooks.h" #include "Hooks.h"
#include <iostream> #include <iostream>
/*
* Plugin loading functions
*/
namespace DFHack
{
DFLibrary * OpenPlugin (const char * filename)
{
dlerror();
DFLibrary * ret = (DFLibrary *) dlopen(filename, RTLD_NOW);
if(!ret)
{
std::cerr << dlerror() << std::endl;
}
return ret;
}
void * LookupPlugin (DFLibrary * plugin ,const char * function)
{
return (DFLibrary *) dlsym((void *)plugin, function);
}
void ClosePlugin (DFLibrary * plugin)
{
dlclose((void *) plugin);
}
}
/******************************************************************************* /*******************************************************************************
* SDL part starts here * * SDL part starts here *
*******************************************************************************/ *******************************************************************************/

@ -35,24 +35,6 @@ distribution.
#include "tinythread.h" #include "tinythread.h"
#include "modules/Graphic.h" #include "modules/Graphic.h"
/*
* Plugin loading functions
*/
namespace DFHack
{
DFLibrary * OpenPlugin (const char * filename)
{
return (DFLibrary *) LoadLibrary(filename);
}
void * LookupPlugin (DFLibrary * plugin ,const char * function)
{
return (void *) GetProcAddress((HMODULE)plugin, function);
}
void ClosePlugin (DFLibrary * plugin)
{
FreeLibrary((HMODULE) plugin);
}
}
/*************************************************************************/ /*************************************************************************/
// extremely boring wrappers beyond this point. Only fix when broken // extremely boring wrappers beyond this point. Only fix when broken

@ -0,0 +1,44 @@
#include <stdio.h>
#include <dlfcn.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#include <string>
#include <map>
#include "DFHack.h"
#include "Core.h"
#include "Hooks.h"
#include <iostream>
/*
* Plugin loading functions
*/
namespace DFHack
{
DFLibrary * OpenPlugin (const char * filename)
{
dlerror();
DFLibrary * ret = (DFLibrary *) dlopen(filename, RTLD_NOW);
if(!ret)
{
std::cerr << dlerror() << std::endl;
}
return ret;
}
void * LookupPlugin (DFLibrary * plugin ,const char * function)
{
return (DFLibrary *) dlsym((void *)plugin, function);
}
void ClosePlugin (DFLibrary * plugin)
{
dlclose((void *) plugin);
}
}

@ -0,0 +1,55 @@
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define DFhackCExport extern "C" __declspec(dllexport)
#include <windows.h>
#include <stdint.h>
#include <vector>
#include <string>
#include "Core.h"
#include "Hooks.h"
#include <stdio.h>
#include "tinythread.h"
#include "modules/Graphic.h"
/*
* Plugin loading functions
*/
namespace DFHack
{
DFLibrary * OpenPlugin (const char * filename)
{
return (DFLibrary *) LoadLibrary(filename);
}
void * LookupPlugin (DFLibrary * plugin ,const char * function)
{
return (void *) GetProcAddress((HMODULE)plugin, function);
}
void ClosePlugin (DFLibrary * plugin)
{
FreeLibrary((HMODULE) plugin);
}
}

@ -77,6 +77,12 @@ namespace DFHack
friend int ::SDL_PollEvent(SDL::Event *); friend int ::SDL_PollEvent(SDL::Event *);
friend int ::SDL_Init(uint32_t flags); friend int ::SDL_Init(uint32_t flags);
friend int ::wgetch(WINDOW * w); friend int ::wgetch(WINDOW * w);
friend int ::egg_init(void);
friend int ::egg_shutdown(void);
friend int ::egg_tick(void);
friend int ::egg_prerender(void);
friend int ::egg_sdl_event(SDL::Event* event);
friend int ::egg_curses_event(int orig_return);
public: public:
/// Get the single Core instance or make one. /// Get the single Core instance or make one.
static Core& getInstance() static Core& getInstance()
@ -127,7 +133,7 @@ namespace DFHack
bool Init(); bool Init();
int Update (void); int Update (void);
int Shutdown (void); int Shutdown (void);
int SDL_Event(SDL::Event* event, int orig_return); int SDL_Event(SDL::Event* event);
bool ncurses_wgetch(int in, int & out); bool ncurses_wgetch(int in, int & out);
Core(Core const&); // Don't Implement Core(Core const&); // Don't Implement
void operator=(Core const&); // Don't implement void operator=(Core const&); // Don't implement

@ -50,3 +50,21 @@ DFhackCExport int SDL_PollEvent(SDL::Event* event);
DFhackCExport int SDL_Init(uint32_t flags); DFhackCExport int SDL_Init(uint32_t flags);
DFhackCExport int wgetch(WINDOW * win); DFhackCExport int wgetch(WINDOW * win);
// hook - called early from DF's main()
DFhackCExport int egg_init(void);
// hook - called before rendering
DFhackCExport int egg_shutdown(void);
// hook - called for each game tick (or more often)
DFhackCExport int egg_tick(void);
// hook - called before rendering
DFhackCExport int egg_prerender(void);
// hook - called for each SDL event, can filter both the event and the return value
DFhackCExport int egg_sdl_event(SDL::Event* event);
// hook - ncurses event. return -1 to consume
DFhackCExport int egg_curses_event(int orig_return);

@ -0,0 +1,7 @@
#!/bin/sh
DF_DIR=$(dirname "$0")
cd "${DF_DIR}"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"./stonesense/deplibs":"./hack/deplibs"
export SDL_DISABLE_LOCK_KEYS=1 # Work around for bug in Debian/Ubuntu SDL patch.
#export SDL_VIDEO_CENTERED=1 # Centre the screen. Messes up resizing.
./libs/Dwarf_Fortress $* # Go, go, go! :)