2011-06-12 15:17:40 -06:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
2011-06-12 15:17:40 -06:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
#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>
|
2011-06-14 08:13:28 -06:00
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
#include "DFHack.h"
|
2011-06-14 08:13:28 -06:00
|
|
|
#include "dfhack/Core.h"
|
2011-06-16 15:53:39 -06:00
|
|
|
#include "dfhack/FakeSDL.h"
|
2011-06-12 15:17:40 -06:00
|
|
|
#include <iostream>
|
|
|
|
|
2011-06-19 20:29:38 -06:00
|
|
|
/*
|
|
|
|
* Plugin loading functions
|
|
|
|
*/
|
2011-06-22 00:14:21 -06:00
|
|
|
namespace DFHack
|
2011-06-19 20:29:38 -06:00
|
|
|
{
|
2011-06-22 00:14:21 -06:00
|
|
|
DFLibrary * OpenPlugin (const char * filename)
|
|
|
|
{
|
2011-06-24 21:35:29 -06:00
|
|
|
dlerror();
|
|
|
|
DFLibrary * ret = (DFLibrary *) dlopen(filename, RTLD_NOW);
|
|
|
|
if(!ret)
|
|
|
|
{
|
|
|
|
std::cerr << dlerror() << std::endl;
|
|
|
|
}
|
|
|
|
return ret;
|
2011-06-22 00:14:21 -06:00
|
|
|
}
|
|
|
|
void * LookupPlugin (DFLibrary * plugin ,const char * function)
|
|
|
|
{
|
|
|
|
return (DFLibrary *) dlsym((void *)plugin, function);
|
|
|
|
}
|
|
|
|
void ClosePlugin (DFLibrary * plugin)
|
|
|
|
{
|
|
|
|
dlclose((void *) plugin);
|
|
|
|
}
|
2011-06-19 20:29:38 -06:00
|
|
|
}
|
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* SDL part starts here *
|
|
|
|
*******************************************************************************/
|
2011-06-16 15:53:39 -06:00
|
|
|
bool FirstCall(void);
|
|
|
|
bool inited = false;
|
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
DFhackCExport int SDL_NumJoysticks(void)
|
|
|
|
{
|
2011-06-14 08:13:28 -06:00
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
return c.Update();
|
2011-06-12 15:17:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ptr to the real functions
|
|
|
|
//static void (*_SDL_GL_SwapBuffers)(void) = 0;
|
|
|
|
static void (*_SDL_Quit)(void) = 0;
|
|
|
|
static int (*_SDL_Init)(uint32_t flags) = 0;
|
2011-06-16 15:53:39 -06:00
|
|
|
static DFThread * (*_SDL_CreateThread)(int (*fn)(void *), void *data) = 0;
|
2011-06-12 15:17:40 -06:00
|
|
|
//static int (*_SDL_Flip)(void * some_ptr) = 0;
|
|
|
|
/*
|
|
|
|
// hook - called every tick in OpenGL mode of DF
|
|
|
|
DFhackCExport void SDL_GL_SwapBuffers(void)
|
|
|
|
{
|
|
|
|
if(_SDL_GL_SwapBuffers)
|
|
|
|
{
|
|
|
|
if(!errorstate)
|
|
|
|
{
|
|
|
|
SHM_Act();
|
|
|
|
}
|
|
|
|
counter ++;
|
|
|
|
_SDL_GL_SwapBuffers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
// hook - called every tick in the 2D mode of DF
|
|
|
|
DFhackCExport int SDL_Flip(void * some_ptr)
|
|
|
|
{
|
|
|
|
if(_SDL_Flip)
|
|
|
|
{
|
|
|
|
if(!errorstate)
|
|
|
|
{
|
|
|
|
SHM_Act();
|
|
|
|
}
|
|
|
|
counter ++;
|
|
|
|
return _SDL_Flip(some_ptr);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
static DFMutex * (*_SDL_CreateMutex)(void) = 0;
|
|
|
|
DFhackCExport DFMutex * SDL_CreateMutex(void)
|
|
|
|
{
|
|
|
|
return _SDL_CreateMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int (*_SDL_mutexP)(DFMutex * mutex) = 0;
|
|
|
|
DFhackCExport int SDL_mutexP(DFMutex * mutex)
|
|
|
|
{
|
|
|
|
return _SDL_mutexP(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int (*_SDL_mutexV)(DFMutex * mutex) = 0;
|
|
|
|
DFhackCExport int SDL_mutexV(DFMutex * mutex)
|
|
|
|
{
|
|
|
|
return _SDL_mutexV(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void (*_SDL_DestroyMutex)(DFMutex * mutex) = 0;
|
|
|
|
DFhackCExport void SDL_DestroyMutex(DFMutex * mutex)
|
|
|
|
{
|
|
|
|
_SDL_DestroyMutex(mutex);
|
|
|
|
}
|
|
|
|
|
2011-06-19 17:12:07 -06:00
|
|
|
static void * (*_SDL_LoadFunction)(DFLibrary *handle, const char *name) = 0;
|
|
|
|
DFhackCExport void * SDL_LoadFunction(DFLibrary *handle, const char *name)
|
|
|
|
{
|
|
|
|
return _SDL_LoadFunction(handle, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DFLibrary * (*_SDL_LoadObject)(const char *sofile) = 0;
|
|
|
|
DFhackCExport DFLibrary * SDL_LoadObject(const char *sofile)
|
|
|
|
{
|
|
|
|
return _SDL_LoadObject(sofile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void (*_SDL_UnloadObject)(DFLibrary * handle) = 0;
|
|
|
|
DFhackCExport void SDL_UnloadObject(DFLibrary * handle)
|
|
|
|
{
|
|
|
|
_SDL_UnloadObject(handle);
|
|
|
|
}
|
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
// hook - called at program exit
|
|
|
|
DFhackCExport void SDL_Quit(void)
|
|
|
|
{
|
2011-06-14 08:13:28 -06:00
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
c.Shutdown();
|
2011-06-12 15:17:40 -06:00
|
|
|
if(_SDL_Quit)
|
|
|
|
{
|
|
|
|
_SDL_Quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hook - called at program start, initialize some stuffs we'll use later
|
|
|
|
DFhackCExport int SDL_Init(uint32_t flags)
|
|
|
|
{
|
2011-06-22 00:14:21 -06:00
|
|
|
freopen("stdout.log", "w", stdout);
|
|
|
|
freopen("stderr.log", "w", stderr);
|
2011-06-12 15:17:40 -06:00
|
|
|
// find real functions
|
|
|
|
//_SDL_GL_SwapBuffers = (void (*)( void )) dlsym(RTLD_NEXT, "SDL_GL_SwapBuffers");
|
|
|
|
_SDL_Init = (int (*)( uint32_t )) dlsym(RTLD_NEXT, "SDL_Init");
|
|
|
|
//_SDL_Flip = (int (*)( void * )) dlsym(RTLD_NEXT, "SDL_Flip");
|
|
|
|
_SDL_Quit = (void (*)( void )) dlsym(RTLD_NEXT, "SDL_Quit");
|
2011-06-16 15:53:39 -06:00
|
|
|
_SDL_CreateThread = (DFThread* (*)(int (*fn)(void *), void *data))dlsym(RTLD_NEXT, "SDL_CreateThread");
|
|
|
|
_SDL_CreateMutex = (DFMutex*(*)())dlsym(RTLD_NEXT,"SDL_CreateMutex");
|
|
|
|
_SDL_DestroyMutex = (void (*)(DFMutex*))dlsym(RTLD_NEXT,"SDL_DestroyMutex");
|
|
|
|
_SDL_mutexP = (int (*)(DFMutex*))dlsym(RTLD_NEXT,"SDL_mutexP");
|
|
|
|
_SDL_mutexV = (int (*)(DFMutex*))dlsym(RTLD_NEXT,"SDL_mutexV");
|
2011-06-19 17:12:07 -06:00
|
|
|
_SDL_LoadFunction = (void*(*)(DFLibrary*, const char*))dlsym(RTLD_NEXT,"SDL_LoadFunction");
|
|
|
|
_SDL_LoadObject = (DFLibrary*(*)(const char*))dlsym(RTLD_NEXT,"SDL_LoadObject");
|
|
|
|
_SDL_UnloadObject = (void (*)(DFLibrary*))dlsym(RTLD_NEXT,"SDL_UnloadObject");
|
2011-06-12 15:17:40 -06:00
|
|
|
|
|
|
|
// check if we got them
|
2011-06-16 15:53:39 -06:00
|
|
|
if(_SDL_Init && _SDL_Quit && _SDL_CreateThread && _SDL_CreateMutex && _SDL_DestroyMutex && _SDL_mutexP && _SDL_mutexV)
|
2011-06-12 15:17:40 -06:00
|
|
|
{
|
|
|
|
fprintf(stderr,"dfhack: hooking successful\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// bail, this would be a disaster otherwise
|
|
|
|
fprintf(stderr,"dfhack: something went horribly wrong\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return _SDL_Init(flags);
|
|
|
|
}
|