Skip to content

Commit

Permalink
Merge remote-tracking branch 'jjyg/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ab9rf committed Jul 11, 2012
2 parents 08469ab + 8f8f253 commit 79ca632
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ build/library
build/tools
build/plugins
build/depends
build/install_manifest.txt

#ignore Kdevelop stuff
.kdev4
Expand All @@ -49,6 +50,10 @@ dfhack/python/PyDFHack.egg-info
dfhack/python/build
dfhack/python/dist

# Ruby binding binaries
plugins/ruby/libruby*
plugins/ruby/ruby-autogen.rb

# CPack stuff
build/CPack*Config.cmake

Expand Down
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ include_directories(depends/tinyxml)
include_directories(depends/tthread)
include_directories(${ZLIB_INCLUDE_DIRS})
include_directories(depends/clsocket/src)
if(APPLE)
include_directories(${CMAKE_INSTALL_PREFIX}/libs/SDL.framework/Headers)
endif()
add_subdirectory(depends)


Expand Down
2 changes: 1 addition & 1 deletion depends/clsocket
Submodule clsocket updated from c85e9f to d0b2d0
7 changes: 5 additions & 2 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ IF(UNIX)
OPTION(CONSOLE_NO_CATCH "Make the console not catch 'CTRL+C' events for easier debugging." OFF)
ENDIF()

include_directories (include)
include_directories (proto)
include_directories (include)

SET(PERL_EXECUTABLE "perl" CACHE FILEPATH "This is the perl executable to run in the codegen step. Tweak it if you need to run a specific one.")

Expand Down Expand Up @@ -267,8 +267,11 @@ SET_TARGET_PROPERTIES(dfhack PROPERTIES DEBUG_POSTFIX "-debug" )

IF(APPLE)
SET(SDL_LIBRARY ${CMAKE_INSTALL_PREFIX}/libs/SDL.framework)
SET(CXX_LIBRARY ${CMAKE_INSTALL_PREFIX}/libs/libstdc++.6.dylib)
SET(ZIP_LIBRARY /usr/lib/libz.dylib)
TARGET_LINK_LIBRARIES(dfhack ${SDL_LIBRARY})
# TARGET_LINK_LIBRARIES(dfhack /usr/lib/libc++.dylib)
TARGET_LINK_LIBRARIES(dfhack ${CXX_LIBRARY})
TARGET_LINK_LIBRARIES(dfhack ${ZIP_LIBRARY})
SET_TARGET_PROPERTIES(dfhack PROPERTIES VERSION 1.0.0)
SET_TARGET_PROPERTIES(dfhack PROPERTIES SOVERSION 1.0.0)
ENDIF()
Expand Down
10 changes: 10 additions & 0 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ Core::Core()
misc_data_mutex=0;
last_world_data_ptr = NULL;
last_local_map_ptr = NULL;
last_pause_state = false;
top_viewscreen = NULL;
screen_window = NULL;
server = NULL;
Expand Down Expand Up @@ -1116,6 +1117,15 @@ int Core::Update()
}
}

if (df::global::pause_state)
{
if (*df::global::pause_state != last_pause_state)
{
onStateChange(out, last_pause_state ? SC_UNPAUSED : SC_PAUSED);
last_pause_state = *df::global::pause_state;
}
}

// Execute per-frame handlers
onUpdate(out);

Expand Down
30 changes: 19 additions & 11 deletions library/Hooks-darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ distribution.
#include <string>
#include <map>

/*typedef struct interpose_s
typedef struct interpose_s
{
void *new_func;
void *orig_func;
} interpose_t;*/
} interpose_t;

#include "DFHack.h"
#include "Core.h"
Expand All @@ -58,19 +58,26 @@ 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();
}

// 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();
Expand All @@ -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)
static int (*_SDL_PollEvent)(SDL::Event* event) = 0;
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
Expand Down Expand Up @@ -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");
Expand All @@ -140,7 +147,7 @@ DFhackCExport int SDL_Init(uint32_t flags)
fprintf(stderr,"dfhack: saving real SDL functions\n");
_SDL_Init = (int (*)( uint32_t )) dlsym(RTLD_NEXT, "SDL_Init");
_SDL_Quit = (void (*)( void )) dlsym(RTLD_NEXT, "SDL_Quit");
_SDL_PollEvent = (int (*)(SDL_Event*))dlsym(RTLD_NEXT,"SDL_PollEvent");
_SDL_PollEvent = (int (*)(SDL::Event*))dlsym(RTLD_NEXT,"SDL_PollEvent");

fprintf(stderr,"dfhack: saved real SDL functions\n");
// check if we got them
Expand All @@ -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;
}
113 changes: 89 additions & 24 deletions library/Process-darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,99 @@ string Process::doReadClassName (void * vptr)
return raw.substr(start,end-start);
}

//FIXME: cross-reference with ELF segment entries?
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <mach/vm_region.h>
#include <mach/vm_statistics.h>
#include <dlfcn.h>

const char *
inheritance_strings[] = {
"SHARE", "COPY", "NONE", "DONATE_COPY",
};

const char *
behavior_strings[] = {
"DEFAULT", "RANDOM", "SEQUENTIAL", "RESQNTL", "WILLNEED", "DONTNEED",
};

void Process::getMemRanges( vector<t_memrange> & ranges )
{
char buffer[1024];
char permissions[5]; // r/-, w/-, x/-, p/s, 0

FILE *mapFile = ::fopen("/proc/self/maps", "r");
size_t start, end, offset, device1, device2, node;
kern_return_t kr;
task_t the_task;

while (fgets(buffer, 1024, mapFile))
{
t_memrange temp;
temp.name[0] = 0;
sscanf(buffer, "%zx-%zx %s %zx %2zx:%2zx %zu %[^\n]",
&start,
&end,
(char*)&permissions,
&offset, &device1, &device2, &node,
(char*)temp.name);
temp.start = (void *) start;
temp.end = (void *) end;
temp.read = permissions[0] == 'r';
temp.write = permissions[1] == 'w';
temp.execute = permissions[2] == 'x';
temp.shared = permissions[3] == 's';
temp.valid = true;
ranges.push_back(temp);
}
the_task = mach_task_self();

vm_size_t vmsize;
vm_address_t address;
vm_region_basic_info_data_t info;
mach_msg_type_number_t info_count;
vm_region_flavor_t flavor;
memory_object_name_t object;

kr = KERN_SUCCESS;
address = 0;

do {
flavor = VM_REGION_BASIC_INFO;
info_count = VM_REGION_BASIC_INFO_COUNT;
kr = vm_region(the_task, &address, &vmsize, flavor,
(vm_region_info_t)&info, &info_count, &object);
if (kr == KERN_SUCCESS) {
if (info.reserved==1) {
address += vmsize;
continue;
}
Dl_info dlinfo;
int dlcheck;
dlcheck = dladdr((const void*)address, &dlinfo);
if (dlcheck==0) {
dlinfo.dli_fname = "";
}

t_memrange temp;
strncpy( temp.name, dlinfo.dli_fname, 1023 );
temp.name[1023] = 0;
temp.start = (void *) address;
temp.end = (void *) (address+vmsize);
temp.read = (info.protection & VM_PROT_READ);
temp.write = (info.protection & VM_PROT_WRITE);
temp.execute = (info.protection & VM_PROT_EXECUTE);
temp.shared = info.shared;
temp.valid = true;
ranges.push_back(temp);

fprintf(stderr,
"%08x-%08x %8uK %c%c%c/%c%c%c %11s %6s %10s uwir=%hu sub=%u dlname: %s\n",
address, (address + vmsize), (vmsize >> 10),
(info.protection & VM_PROT_READ) ? 'r' : '-',
(info.protection & VM_PROT_WRITE) ? 'w' : '-',
(info.protection & VM_PROT_EXECUTE) ? 'x' : '-',
(info.max_protection & VM_PROT_READ) ? 'r' : '-',
(info.max_protection & VM_PROT_WRITE) ? 'w' : '-',
(info.max_protection & VM_PROT_EXECUTE) ? 'x' : '-',
inheritance_strings[info.inheritance],
(info.shared) ? "shared" : "-",
behavior_strings[info.behavior],
info.user_wired_count,
info.reserved,
dlinfo.dli_fname);

address += vmsize;
} else if (kr != KERN_INVALID_ADDRESS) {

/*if (the_task != MACH_PORT_NULL) {
mach_port_deallocate(mach_task_self(), the_task);
}*/
return;
}
} while (kr != KERN_INVALID_ADDRESS);


/* if (the_task != MACH_PORT_NULL) {
mach_port_deallocate(mach_task_self(), the_task);
}*/
}

uint32_t Process::getBase()
Expand Down
6 changes: 6 additions & 0 deletions library/VersionInfoFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ void VersionInfoFactory::ParseVersion (TiXmlElement* entry, VersionInfo* mem)
// this is wrong... I'm not going to do base image relocation on linux though.
mem->setBase(0x0);
}
else if(os == "darwin")
{
mem->setOS(OS_APPLE);
// this is wrong... I'm not going to do base image relocation on linux though.
mem->setBase(0x0);
}
else
{
return; // ignore it if it's invalid
Expand Down
12 changes: 6 additions & 6 deletions library/include/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ namespace DFHack
bool save (const char * filename)
{
std::ofstream outfile (filename);
fprintf(stderr,"Save: Initialized stream\n");
//fprintf(stderr,"Save: Initialized stream\n");
if(outfile.bad())
return false;
fprintf(stderr,"Save: Iterating...\n");
//fprintf(stderr,"Save: Iterating...\n");
for(auto iter = history.begin();iter < history.end(); iter++)
{
fprintf(stderr,"Save: Dumping %s\n",(*iter).c_str());
//fprintf(stderr,"Save: Dumping %s\n",(*iter).c_str());
outfile << *iter << std::endl;
fprintf(stderr,"Save: Flushing\n");
//fprintf(stderr,"Save: Flushing\n");
outfile.flush();
}
fprintf(stderr,"Save: Closing\n");
//fprintf(stderr,"Save: Closing\n");
outfile.close();
fprintf(stderr,"Save: Done\n");
//fprintf(stderr,"Save: Done\n");
return true;
}
/// add a command to the history
Expand Down
12 changes: 11 additions & 1 deletion library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,27 @@ namespace DFHack
SC_MAP_UNLOADED = 3,
SC_VIEWSCREEN_CHANGED = 4,
SC_CORE_INITIALIZED = 5,
SC_BEGIN_UNLOAD = 6
SC_BEGIN_UNLOAD = 6,
SC_PAUSED = 7,
SC_UNPAUSED = 8
};

// Core is a singleton. Why? Because it is closely tied to SDL calls. It tracks the global state of DF.
// There should never be more than one instance
// 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);
Expand Down Expand Up @@ -221,6 +230,7 @@ namespace DFHack
// for state change tracking
void *last_local_map_ptr;
df::viewscreen *top_viewscreen;
bool last_pause_state;
// Very important!
bool started;

Expand Down
6 changes: 6 additions & 0 deletions library/include/Hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions library/lua/dfhack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ if dfhack.is_core_context then
SC_MAP_UNLOADED = 3
SC_VIEWSCREEN_CHANGED = 4
SC_CORE_INITIALIZED = 5
SC_PAUSED = 7
SC_UNPAUSED = 8
end

-- Error handling
Expand Down
Loading

0 comments on commit 79ca632

Please sign in to comment.