From 4e3ca72886cc0fd4811fd9d21e9c91ae68350c04 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Thu, 26 Dec 2024 11:22:58 -0600 Subject: [PATCH 1/2] reorganize/tidy `Process-*.cpp` source units Rearrange all three `Process-*.cpp` files so that the contents are in parallel as much as possible remove `using namespace std` code smell reorganize header includes change minimum supported windows version to 6.0 (Vista) --- library/Process-darwin.cpp | 44 ++++++++++---------- library/Process-linux.cpp | 21 ++++++---- library/Process-windows.cpp | 80 +++++++++++++++++++++---------------- 3 files changed, 82 insertions(+), 63 deletions(-) diff --git a/library/Process-darwin.cpp b/library/Process-darwin.cpp index 1ac1d4ddf2..8e4753cee4 100644 --- a/library/Process-darwin.cpp +++ b/library/Process-darwin.cpp @@ -21,33 +21,41 @@ must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ +#include +#include +#include +#include +#include +#include -#include "Internal.h" #include #include -#include #include #include +#include +#include #include +#include +#include +#include +#include +#include -#include -#include -#include -#include -#include -#include -using namespace std; -#include +#include "Error.h" +#include "Internal.h" #include "MemAccess.h" #include "Memory.h" -#include "VersionInfoFactory.h" #include "VersionInfo.h" -#include "Error.h" -#include +#include "VersionInfoFactory.h" +#include "md5wrapper.h" using namespace DFHack; +using std::string; +using std::map; +using std::vector; + Process::Process(const VersionInfoFactory& known_versions) : identified(false), my_pe(0) { char path[1024]; @@ -67,8 +75,8 @@ Process::Process(const VersionInfoFactory& known_versions) : identified(false), auto vinfo = known_versions.getVersionInfoByMD5(my_md5); if(vinfo) { - my_descriptor = std::make_shared(*vinfo); identified = true; + my_descriptor = std::make_shared(*vinfo); } else { @@ -115,17 +123,11 @@ string Process::doReadClassName (void * vptr) char * typeinfo = Process::readPtr(((char *)vptr - sizeof(void*))); char * typestring = Process::readPtr(typeinfo + sizeof(void*)); string raw = readCString(typestring); - size_t start = raw.find_first_of("abcdefghijklmnopqrstuvwxyz");// trim numbers + size_t start = raw.find_first_of("abcdefghijklmnopqrstuvwxyz");// trim numbers size_t end = raw.length(); return raw.substr(start,end-start); } -#include -#include -#include -#include -#include - const char * inheritance_strings[] = { "SHARE", "COPY", "NONE", "DONATE_COPY", diff --git a/library/Process-linux.cpp b/library/Process-linux.cpp index d64e4445b0..23b6487133 100644 --- a/library/Process-linux.cpp +++ b/library/Process-linux.cpp @@ -22,30 +22,35 @@ must not be misrepresented as being the original software. distribution. */ -#include #include -#include -#include +#include +#include #include #include #include +#include + +#include +#include #include #include #include -#include #include "Error.h" #include "Internal.h" -#include "md5wrapper.h" #include "MemAccess.h" #include "Memory.h" -#include "modules/Filesystem.h" #include "VersionInfo.h" #include "VersionInfoFactory.h" +#include "modules/Filesystem.h" +#include "md5wrapper.h" -using namespace std; using namespace DFHack; +using std::string; +using std::map; +using std::vector; + Process::Process(const VersionInfoFactory& known_versions) : identified(false), my_pe(0) { const char * exe_link_name = "/proc/self/exe"; @@ -69,8 +74,8 @@ Process::Process(const VersionInfoFactory& known_versions) : identified(false), auto vinfo = known_versions.getVersionInfoByMD5(my_md5); if(vinfo) { - my_descriptor = std::make_shared(*vinfo); identified = true; + my_descriptor = std::make_shared(*vinfo); } else { diff --git a/library/Process-windows.cpp b/library/Process-windows.cpp index ea244fe146..a452b0b7d5 100644 --- a/library/Process-windows.cpp +++ b/library/Process-windows.cpp @@ -22,29 +22,34 @@ must not be misrepresented as being the original software. distribution. */ -#include "Internal.h" - -#define _WIN32_WINNT 0x0501 -#define WINVER 0x0501 - -#define WIN32_LEAN_AND_MEAN -#include -#include - #include #include #include -#include -#include #include -using namespace std; +#include +#include +#include -#include "VersionInfo.h" -#include "VersionInfoFactory.h" #include "Error.h" +#include "Internal.h" #include "MemAccess.h" #include "Memory.h" +#include "VersionInfo.h" +#include "VersionInfoFactory.h" + +#define _WIN32_WINNT 0x0600 +#define WINVER 0x0600 + +#define WIN32_LEAN_AND_MEAN +#include +#include + using namespace DFHack; + +using std::string; +using std::map; +using std::vector; + namespace DFHack { class PlatformSpecific @@ -62,6 +67,7 @@ namespace DFHack char * base; }; } + Process::Process(const VersionInfoFactory& factory) : identified(false) { HMODULE hmod = NULL; @@ -90,7 +96,7 @@ Process::Process(const VersionInfoFactory& factory) : identified(false) d->sections = (IMAGE_SECTION_HEADER *) malloc(sectionsSize); read(d->base + pe_offset + sizeof(d->pe_header), sectionsSize, (uint8_t *)(d->sections)); } - catch (exception &) + catch (std::exception &) { return; } @@ -117,6 +123,25 @@ Process::~Process() free(d->sections); } +string Process::doReadClassName (void * vptr) +{ + char * rtti = readPtr((char *)vptr - sizeof(void*)); +#ifdef DFHACK64 + void *base; + if (!RtlPcToFileHeader(rtti, &base)) + return "dummy"; + char * typeinfo = (char *)base + readDWord(rtti + 0xC); + string raw = readCString(typeinfo + 0x10+4); // skips the .?AV +#else + char * typeinfo = readPtr(rtti + 0xC); + string raw = readCString(typeinfo + 0xC); // skips the .?AV +#endif + if (!raw.length()) + return "dummy"; + raw.resize(raw.length() - 2);// trim @@ from end + return raw; +} + /* typedef struct _MEMORY_BASIC_INFORMATION { @@ -341,25 +366,6 @@ int Process::adjustOffset(int offset, bool to_file) return -1; } -string Process::doReadClassName (void * vptr) -{ - char * rtti = readPtr((char *)vptr - sizeof(void*)); -#ifdef DFHACK64 - void *base; - if (!RtlPcToFileHeader(rtti, &base)) - return "dummy"; - char * typeinfo = (char *)base + readDWord(rtti + 0xC); - string raw = readCString(typeinfo + 0x10+4); // skips the .?AV -#else - char * typeinfo = readPtr(rtti + 0xC); - string raw = readCString(typeinfo + 0xC); // skips the .?AV -#endif - if (!raw.length()) - return "dummy"; - raw.resize(raw.length() - 2);// trim @@ from end - return raw; -} - uint32_t Process::getTickCount() { return GetTickCount(); @@ -376,6 +382,12 @@ string Process::getPath() return(out.substr(0,out.find_last_of("\\"))); } +int Process::getPID() +{ + return (int) GetCurrentProcessId(); +} + + bool Process::setPermisions(const t_memrange & range,const t_memrange &trgrange) { DWORD newprotect=0; From 13867a58220760638fa7c6054395ec2c07b95651 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Thu, 26 Dec 2024 11:42:19 -0600 Subject: [PATCH 2/2] add `std::cerr` and `std::endl` missed these on the first pass, mb --- library/Process-darwin.cpp | 3 +++ library/Process-linux.cpp | 2 ++ library/Process-windows.cpp | 3 +++ 3 files changed, 8 insertions(+) diff --git a/library/Process-darwin.cpp b/library/Process-darwin.cpp index 8e4753cee4..63114e77ce 100644 --- a/library/Process-darwin.cpp +++ b/library/Process-darwin.cpp @@ -55,6 +55,9 @@ using namespace DFHack; using std::string; using std::map; using std::vector; +using std::endl; +using std::cerr; + Process::Process(const VersionInfoFactory& known_versions) : identified(false), my_pe(0) { diff --git a/library/Process-linux.cpp b/library/Process-linux.cpp index 23b6487133..ea54516034 100644 --- a/library/Process-linux.cpp +++ b/library/Process-linux.cpp @@ -50,6 +50,8 @@ using namespace DFHack; using std::string; using std::map; using std::vector; +using std::endl; +using std::cerr; Process::Process(const VersionInfoFactory& known_versions) : identified(false), my_pe(0) { diff --git a/library/Process-windows.cpp b/library/Process-windows.cpp index a452b0b7d5..7d9469b455 100644 --- a/library/Process-windows.cpp +++ b/library/Process-windows.cpp @@ -49,6 +49,9 @@ using namespace DFHack; using std::string; using std::map; using std::vector; +using std::endl; +using std::cerr; + namespace DFHack {