From 040f8f7b7a3251ed263e6f60cdb5713313a4f378 Mon Sep 17 00:00:00 2001 From: belal Date: Sun, 12 Sep 2010 21:36:31 -0400 Subject: [PATCH 1/7] Add function to get the current working directory of the DF process, as well as offsets for the relative paths of the current tileset and color file, also created a simple program to dump those three things out --- data/Memory-ng.xml | 6 ++++++ library/DFProcess-linux-wine.cpp | 2 +- library/DFProcess-windows-SHM.cpp | 10 +++++++++ library/DFProcess-windows.cpp | 9 ++++++++ library/include/dfhack/DFProcess.h | 5 +++++ library/include/dfhack/modules/Gui.h | 4 ++++ library/modules/Gui.cpp | 28 ++++++++++++++++++++++++ tools/playground/CMakeLists.txt | 7 ++++++ tools/playground/paths.cpp | 32 ++++++++++++++++++++++++++++ 9 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 tools/playground/paths.cpp diff --git a/data/Memory-ng.xml b/data/Memory-ng.xml index f331c10d2..127266adb 100644 --- a/data/Memory-ng.xml +++ b/data/Memory-ng.xml @@ -694,6 +694,8 @@
+
+
@@ -1452,6 +1454,10 @@
+ +
+
+ diff --git a/library/DFProcess-linux-wine.cpp b/library/DFProcess-linux-wine.cpp index 8e8e3c1f5..20b33a9db 100644 --- a/library/DFProcess-linux-wine.cpp +++ b/library/DFProcess-linux-wine.cpp @@ -637,4 +637,4 @@ string WineProcess::readClassName (uint32_t vptr) string raw = readCString(typeinfo + 0xC); // skips the .?AV raw.resize(raw.length() - 2);// trim @@ from end return raw; -} +} \ No newline at end of file diff --git a/library/DFProcess-windows-SHM.cpp b/library/DFProcess-windows-SHM.cpp index 9742ceeca..7941f8c73 100644 --- a/library/DFProcess-windows-SHM.cpp +++ b/library/DFProcess-windows-SHM.cpp @@ -929,6 +929,16 @@ string SHMProcess::readClassName (uint32_t vptr) return raw; } +string SHMProcess::getPath() +{ + HMODULE hmod; + DWORD junk; + char String[255]; + HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, d->process_ID ); //get the handle from the process ID + EnumProcessModules(hProcess, &hmod, 1 * sizeof(HMODULE), &junk); //get the module from the handle + GetModuleFileNameEx(hProcess,hmod,String,sizeof(String)); //get the filename from the module + return(string(String)); +} // get module index by name and version. bool 0 = error bool SHMProcess::getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT) { diff --git a/library/DFProcess-windows.cpp b/library/DFProcess-windows.cpp index a658b49a7..697558c5b 100644 --- a/library/DFProcess-windows.cpp +++ b/library/DFProcess-windows.cpp @@ -505,3 +505,12 @@ string NormalProcess::readClassName (uint32_t vptr) raw.resize(raw.length() - 2);// trim @@ from end return raw; } +string NormalProcess::getPath() +{ + HMODULE hmod; + DWORD junk; + char String[255]; + EnumProcessModules(d->my_handle, &hmod, 1 * sizeof(HMODULE), &junk); //get the module from the handle + GetModuleFileNameEx(d->my_handle,hmod,String,sizeof(String)); //get the filename from the module + return(string(String)); +} \ No newline at end of file diff --git a/library/include/dfhack/DFProcess.h b/library/include/dfhack/DFProcess.h index 282e1b95f..8918c1584 100644 --- a/library/include/dfhack/DFProcess.h +++ b/library/include/dfhack/DFProcess.h @@ -163,6 +163,8 @@ namespace DFHack virtual VersionInfo *getDescriptor() = 0; /// get the DF Process ID virtual int getPID() = 0; + /// get the DF Process FilePath + virtual std::string getPath() = 0; /// get module index by name and version. bool 1 = error virtual bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT) = 0; /// get the SHM start if available @@ -230,6 +232,7 @@ namespace DFHack void getMemRanges(std::vector & ranges ); VersionInfo *getDescriptor(); int getPID(); + std::string getPath(); // get module index by name and version. bool 1 = error bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT) { OUTPUT=0; return false;}; // get the SHM start if available @@ -295,6 +298,7 @@ namespace DFHack void getMemRanges(std::vector & ranges ); VersionInfo *getDescriptor(); int getPID(); + std::string getPath(); // get module index by name and version. bool 1 = error bool getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT); // get the SHM start if available @@ -364,6 +368,7 @@ namespace DFHack // get the SHM start if available char * getSHMStart (void){return 0;}; bool SetAndWait (uint32_t state){return false;}; + std::string getPath() {return std::string()}; //FIXME should be pretty trival }; #endif } diff --git a/library/include/dfhack/modules/Gui.h b/library/include/dfhack/modules/Gui.h index 6ac91f35c..64cf06d97 100644 --- a/library/include/dfhack/modules/Gui.h +++ b/library/include/dfhack/modules/Gui.h @@ -28,6 +28,10 @@ namespace DFHack bool ReadViewScreen(t_viewscreen &); /// read the DF menu state (designation menu ect) uint32_t ReadMenuState(); + /// read the current tileset file used in DF + string ReadCurrentTileSetFilename(); + /// read the current color file used in DF + string ReadCurrentColorsFilename(); private: struct Private; diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 177a00467..15b5a0959 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -44,6 +44,10 @@ struct Gui::Private bool ViewScreeInited; uint32_t current_menu_state_offset; bool MenuStateInited; + uint32_t tileset_filename_offset; + bool TilesetFilenameInited; + uint32_t colors_filename_offset; + bool ColorsFilenameInited; DFContextShared *d; Process * owner; }; @@ -73,6 +77,18 @@ Gui::Gui(DFContextShared * _d) d->ViewScreeInited = true; } catch(exception &){}; + try + { + d->tileset_filename_offset = OG_Gui->getAddress ("tileset_filename"); + d->TilesetFilenameInited = true; + } + catch(exception &){}; + try + { + d->colors_filename_offset = OG_Gui->getAddress ("colors_filename"); + d->ColorsFilenameInited = true; + } + catch(exception &){}; d->Started = true; } @@ -129,3 +145,15 @@ bool Gui::ReadViewScreen (t_viewscreen &screen) } return d->d->offset_descriptor->resolveObjectToClassID (last, screen.type); } +string Gui::ReadCurrentTileSetFilename() +{ + if(d->TilesetFilenameInited) + return(d->owner->readCString(d->tileset_filename_offset)); + return ""; +} +string Gui::ReadCurrentColorsFilename() +{ + if(d->ColorsFilenameInited) + return(d->owner->readCString(d->colors_filename_offset)); + return ""; +} \ No newline at end of file diff --git a/tools/playground/CMakeLists.txt b/tools/playground/CMakeLists.txt index ab21e3c2d..f21d9170c 100644 --- a/tools/playground/CMakeLists.txt +++ b/tools/playground/CMakeLists.txt @@ -60,6 +60,13 @@ TARGET_LINK_LIBRARIES(dfcatsplosion dfhack) ADD_EXECUTABLE(dfcopypaste copypaste.cpp) TARGET_LINK_LIBRARIES(dfcopypaste dfhack) +# paths +# Author: belal +# dumps the current path to the DF exe, as well as the relative paths to the +# current tileset and color files +ADD_EXECUTABLE(dfpaths paths.cpp) +TARGET_LINK_LIBRARIES(dfpaths dfhack) + # this needs the C bindings IF(BUILD_DFHACK_C_BINDINGS) # for trying out some 'stuff' diff --git a/tools/playground/paths.cpp b/tools/playground/paths.cpp new file mode 100644 index 000000000..5ae3cd37a --- /dev/null +++ b/tools/playground/paths.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +#include +using namespace DFHack; + +int main () +{ + DFHack::ContextManager DFMgr("Memory.xml"); + DFHack::Context *DF = DFMgr.getSingleContext(); + try + { + DF->Attach(); + } + catch (exception& e) + { + cerr << e.what() << endl; + #ifndef LINUX_BUILD + cin.ignore(); + #endif + return 1; + } + DFHack::Process * Process = DF->getProcess(); + DFHack::Gui * gui = DF->getGui(); + cout << Process->getPath() << endl; + cout << gui->ReadCurrentTileSetFilename() << "\n" << gui->ReadCurrentColorsFilename() << endl; + #ifndef LINUX_BUILD + cout << "Done. Press any key to continue" << endl; + cin.ignore(); + #endif + return 0; +} \ No newline at end of file From d2db7524a1a6744854f0fec94a8f42d65f664600 Mon Sep 17 00:00:00 2001 From: belal Date: Mon, 13 Sep 2010 09:45:53 -0400 Subject: [PATCH 2/7] Keeping the current working directory addition, but reverting the current tileset and color stuff, as the tileset was just an initializer, not the actual current one, just going to have to read the init.txt file myself Revert "Add function to get the current working directory of the DF process, as well as offsets for the relative paths of the current tileset and color file, also created a simple program to dump those three things out" This reverts commit 040f8f7b7a3251ed263e6f60cdb5713313a4f378. --- data/Memory-ng.xml | 6 ------ library/include/dfhack/modules/Gui.h | 4 ---- library/modules/Gui.cpp | 28 ---------------------------- tools/playground/paths.cpp | 1 - 4 files changed, 39 deletions(-) diff --git a/data/Memory-ng.xml b/data/Memory-ng.xml index 127266adb..f331c10d2 100644 --- a/data/Memory-ng.xml +++ b/data/Memory-ng.xml @@ -694,8 +694,6 @@
-
-
@@ -1454,10 +1452,6 @@
- -
-
- diff --git a/library/include/dfhack/modules/Gui.h b/library/include/dfhack/modules/Gui.h index 64cf06d97..6ac91f35c 100644 --- a/library/include/dfhack/modules/Gui.h +++ b/library/include/dfhack/modules/Gui.h @@ -28,10 +28,6 @@ namespace DFHack bool ReadViewScreen(t_viewscreen &); /// read the DF menu state (designation menu ect) uint32_t ReadMenuState(); - /// read the current tileset file used in DF - string ReadCurrentTileSetFilename(); - /// read the current color file used in DF - string ReadCurrentColorsFilename(); private: struct Private; diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index 15b5a0959..177a00467 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -44,10 +44,6 @@ struct Gui::Private bool ViewScreeInited; uint32_t current_menu_state_offset; bool MenuStateInited; - uint32_t tileset_filename_offset; - bool TilesetFilenameInited; - uint32_t colors_filename_offset; - bool ColorsFilenameInited; DFContextShared *d; Process * owner; }; @@ -77,18 +73,6 @@ Gui::Gui(DFContextShared * _d) d->ViewScreeInited = true; } catch(exception &){}; - try - { - d->tileset_filename_offset = OG_Gui->getAddress ("tileset_filename"); - d->TilesetFilenameInited = true; - } - catch(exception &){}; - try - { - d->colors_filename_offset = OG_Gui->getAddress ("colors_filename"); - d->ColorsFilenameInited = true; - } - catch(exception &){}; d->Started = true; } @@ -145,15 +129,3 @@ bool Gui::ReadViewScreen (t_viewscreen &screen) } return d->d->offset_descriptor->resolveObjectToClassID (last, screen.type); } -string Gui::ReadCurrentTileSetFilename() -{ - if(d->TilesetFilenameInited) - return(d->owner->readCString(d->tileset_filename_offset)); - return ""; -} -string Gui::ReadCurrentColorsFilename() -{ - if(d->ColorsFilenameInited) - return(d->owner->readCString(d->colors_filename_offset)); - return ""; -} \ No newline at end of file diff --git a/tools/playground/paths.cpp b/tools/playground/paths.cpp index 5ae3cd37a..cc00f79a3 100644 --- a/tools/playground/paths.cpp +++ b/tools/playground/paths.cpp @@ -23,7 +23,6 @@ int main () DFHack::Process * Process = DF->getProcess(); DFHack::Gui * gui = DF->getGui(); cout << Process->getPath() << endl; - cout << gui->ReadCurrentTileSetFilename() << "\n" << gui->ReadCurrentColorsFilename() << endl; #ifndef LINUX_BUILD cout << "Done. Press any key to continue" << endl; cin.ignore(); From 6661d7e1f766e169a94dc21d29718e70bbf3659e Mon Sep 17 00:00:00 2001 From: belal Date: Thu, 16 Sep 2010 18:21:41 -0700 Subject: [PATCH 3/7] added getPath() for all linux versions as well, should work fine --- library/DFProcess-linux-SHM.cpp | 11 +++++++++++ library/DFProcess-linux-wine.cpp | 13 ++++++++++++- library/DFProcess-linux.cpp | 11 +++++++++++ library/include/dfhack/DFProcess.h | 2 +- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/library/DFProcess-linux-SHM.cpp b/library/DFProcess-linux-SHM.cpp index ace2dacf8..1432dc2c1 100644 --- a/library/DFProcess-linux-SHM.cpp +++ b/library/DFProcess-linux-SHM.cpp @@ -920,3 +920,14 @@ bool SHMProcess::Private::Aux_Core_Attach(bool & versionOK, pid_t & PID) #endif return true; } +string SHMProcess::getPath() +{ + char cwd_name[256]; + char target_name[1024]; + int target_result; + + sprintf(cwd_name,"/proc/%d/cwd", getPID()); + // resolve /proc/PID/exe link + target_result = readlink(cwd_name, target_name, sizeof(target_name)); + return(string(target_name)); +} diff --git a/library/DFProcess-linux-wine.cpp b/library/DFProcess-linux-wine.cpp index 20b33a9db..489118d5b 100644 --- a/library/DFProcess-linux-wine.cpp +++ b/library/DFProcess-linux-wine.cpp @@ -637,4 +637,15 @@ string WineProcess::readClassName (uint32_t vptr) string raw = readCString(typeinfo + 0xC); // skips the .?AV raw.resize(raw.length() - 2);// trim @@ from end return raw; -} \ No newline at end of file +} +string WineProcess::getPath() +{ + char cwd_name[256]; + char target_name[1024]; + int target_result; + + sprintf(cwd_name,"/proc/%d/cwd", getPID()); + // resolve /proc/PID/exe link + target_result = readlink(cwd_name, target_name, sizeof(target_name)); + return(string(target_name)); +} diff --git a/library/DFProcess-linux.cpp b/library/DFProcess-linux.cpp index 164834543..932847f2e 100644 --- a/library/DFProcess-linux.cpp +++ b/library/DFProcess-linux.cpp @@ -586,3 +586,14 @@ string NormalProcess::readClassName (uint32_t vptr) size_t end = raw.length(); return raw.substr(start,end-start); } +string NormalProcess::getPath() +{ + char cwd_name[256]; + char target_name[1024]; + int target_result; + + sprintf(cwd_name,"/proc/%d/cwd", getPID()); + // resolve /proc/PID/exe link + target_result = readlink(cwd_name, target_name, sizeof(target_name)); + return(string(target_name)); +} diff --git a/library/include/dfhack/DFProcess.h b/library/include/dfhack/DFProcess.h index 8918c1584..3743cf4b7 100644 --- a/library/include/dfhack/DFProcess.h +++ b/library/include/dfhack/DFProcess.h @@ -368,7 +368,7 @@ namespace DFHack // get the SHM start if available char * getSHMStart (void){return 0;}; bool SetAndWait (uint32_t state){return false;}; - std::string getPath() {return std::string()}; //FIXME should be pretty trival + std::string getPath(); }; #endif } From 3dfbc5fb211507df69043b01dd20286231ad1a5d Mon Sep 17 00:00:00 2001 From: belal Date: Thu, 16 Sep 2010 21:44:38 -0400 Subject: [PATCH 4/7] update widows getPath to only output the path, not the path and exe filename, to correspond with the linux versions --- library/DFProcess-windows-SHM.cpp | 3 ++- library/DFProcess-windows.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/library/DFProcess-windows-SHM.cpp b/library/DFProcess-windows-SHM.cpp index 7941f8c73..aa9d50dfe 100644 --- a/library/DFProcess-windows-SHM.cpp +++ b/library/DFProcess-windows-SHM.cpp @@ -937,7 +937,8 @@ string SHMProcess::getPath() HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, d->process_ID ); //get the handle from the process ID EnumProcessModules(hProcess, &hmod, 1 * sizeof(HMODULE), &junk); //get the module from the handle GetModuleFileNameEx(hProcess,hmod,String,sizeof(String)); //get the filename from the module - return(string(String)); + string out(String); + return(out.substr(0,out.find_last_of("\\"))); } // get module index by name and version. bool 0 = error bool SHMProcess::getModuleIndex (const char * name, const uint32_t version, uint32_t & OUTPUT) diff --git a/library/DFProcess-windows.cpp b/library/DFProcess-windows.cpp index 697558c5b..9471116aa 100644 --- a/library/DFProcess-windows.cpp +++ b/library/DFProcess-windows.cpp @@ -512,5 +512,6 @@ string NormalProcess::getPath() char String[255]; EnumProcessModules(d->my_handle, &hmod, 1 * sizeof(HMODULE), &junk); //get the module from the handle GetModuleFileNameEx(d->my_handle,hmod,String,sizeof(String)); //get the filename from the module - return(string(String)); + string out(String); + return(out.substr(0,out.find_last_of("\\"))); } \ No newline at end of file From 918de0271ba1167b86257a56094b5d371ee07740 Mon Sep 17 00:00:00 2001 From: belal Date: Fri, 17 Sep 2010 09:20:15 -0400 Subject: [PATCH 5/7] fix to get the windowIO.h to compile from a submodule --- library/include/dfhack/modules/WindowIO.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/include/dfhack/modules/WindowIO.h b/library/include/dfhack/modules/WindowIO.h index 6e1af9fcb..5244d2a1c 100644 --- a/library/include/dfhack/modules/WindowIO.h +++ b/library/include/dfhack/modules/WindowIO.h @@ -25,9 +25,9 @@ distribution. #ifndef KEYS_H_INCLUDED #define KEYS_H_INCLUDED -#include "dfhack/DFPragma.h" -#include "dfhack/DFExport.h" -#include "dfhack/DFModule.h" +#include "../DFPragma.h" +#include "../DFExport.h" +#include "../DFModule.h" namespace DFHack { From 0f7a27d611db33c5f0208a177cb8002014c9b2a5 Mon Sep 17 00:00:00 2001 From: belal Date: Sun, 19 Sep 2010 19:49:45 -0400 Subject: [PATCH 6/7] fix the WindowIO-windows.cpp module to work correctly with the new DF, also made it a little simpler. --- library/modules/WindowIO-windows.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/library/modules/WindowIO-windows.cpp b/library/modules/WindowIO-windows.cpp index 37c5eb08c..1a432ade4 100644 --- a/library/modules/WindowIO-windows.cpp +++ b/library/modules/WindowIO-windows.cpp @@ -130,20 +130,19 @@ WindowIO::~WindowIO () void WindowIO::TypeStr (const char *input, int delay, bool useShift) { //sendmessage needs a window handle HWND, so have to get that from the process HANDLE - HWND currentWindow = GetForegroundWindow(); window myWindow; myWindow.pid = d->p->getPID(); EnumWindows (EnumWindowsProc, (LPARAM) &myWindow); - char cChar; - DWORD dfProccess = GetWindowThreadProcessId(myWindow.windowHandle,NULL); - DWORD currentProccess = GetWindowThreadProcessId(currentWindow,NULL); - AttachThreadInput(currentProccess,dfProccess,TRUE); //The two threads have to have attached input in order to change the keyboard state, which is needed to set the shift state + DWORD dfProcess = GetWindowThreadProcessId(myWindow.windowHandle,NULL); + DWORD currentProcess = GetCurrentThreadId(); + AttachThreadInput(currentProcess,dfProcess,TRUE); //The two threads have to have attached input in order to change the keyboard state, which is needed to set the shift state while ( (cChar = *input++)) // loops through chars { short vk = VkKeyScan (cChar); // keycode of char if (useShift || (vk >> 8) &1) // char is capital, so need to hold down shift { + vk = vk & 0xFF; // remove the shift state from the virtual key code BYTE keybstate[256] = {0}; BYTE keybstateOrig[256] = {0}; GetKeyboardState((LPBYTE)&keybstateOrig); @@ -160,7 +159,7 @@ void WindowIO::TypeStr (const char *input, int delay, bool useShift) SendMessage(myWindow.windowHandle,WM_KEYUP,vk,0); } } - AttachThreadInput(currentProccess,dfProccess,FALSE); //detach the threads + AttachThreadInput(currentProcess,dfProcess,FALSE); //detach the threads Sleep (delay); } From 583f997e2fe0c3935e7ffa7e282c848d6663e3df Mon Sep 17 00:00:00 2001 From: belal Date: Thu, 23 Sep 2010 08:33:45 -0400 Subject: [PATCH 7/7] fix linux getPaths to append a null terminator on the strings --- library/DFProcess-linux-SHM.cpp | 1 + library/DFProcess-linux-wine.cpp | 1 + library/DFProcess-linux.cpp | 1 + 3 files changed, 3 insertions(+) diff --git a/library/DFProcess-linux-SHM.cpp b/library/DFProcess-linux-SHM.cpp index 1432dc2c1..6f6bedb34 100644 --- a/library/DFProcess-linux-SHM.cpp +++ b/library/DFProcess-linux-SHM.cpp @@ -929,5 +929,6 @@ string SHMProcess::getPath() sprintf(cwd_name,"/proc/%d/cwd", getPID()); // resolve /proc/PID/exe link target_result = readlink(cwd_name, target_name, sizeof(target_name)); + target_name[target_result] = '\0'; return(string(target_name)); } diff --git a/library/DFProcess-linux-wine.cpp b/library/DFProcess-linux-wine.cpp index f92d4b5cd..8ab9bc689 100644 --- a/library/DFProcess-linux-wine.cpp +++ b/library/DFProcess-linux-wine.cpp @@ -633,5 +633,6 @@ string WineProcess::getPath() sprintf(cwd_name,"/proc/%d/cwd", getPID()); // resolve /proc/PID/exe link target_result = readlink(cwd_name, target_name, sizeof(target_name)); + target_name[target_result] = '\0'; return(string(target_name)); } diff --git a/library/DFProcess-linux.cpp b/library/DFProcess-linux.cpp index 932847f2e..24bff365a 100644 --- a/library/DFProcess-linux.cpp +++ b/library/DFProcess-linux.cpp @@ -595,5 +595,6 @@ string NormalProcess::getPath() sprintf(cwd_name,"/proc/%d/cwd", getPID()); // resolve /proc/PID/exe link target_result = readlink(cwd_name, target_name, sizeof(target_name)); + target_name[target_result] = '\0'; return(string(target_name)); }