From 9f6c82189aa5a7dbf4955c7aaeaa75dc3b3e05a7 Mon Sep 17 00:00:00 2001 From: korenkonder Date: Sat, 3 Jun 2023 23:58:27 +0300 Subject: [PATCH] Update various stuff --- extern/src/Helpers.h | 28 ++++++++++ extern/src/SigScan.h | 66 +++++++++++++++-------- src/OpenLyricLimit/OpenLyricLimit.vcxproj | 9 +--- src/PotatoMM/PotatoMM.vcxproj | 9 +--- 4 files changed, 74 insertions(+), 38 deletions(-) diff --git a/extern/src/Helpers.h b/extern/src/Helpers.h index 1f6494a..70bc49a 100644 --- a/extern/src/Helpers.h +++ b/extern/src/Helpers.h @@ -1,5 +1,7 @@ #pragma once +#include "detours.h" + #define _CONCAT2(x, y) x##y #define CONCAT2(x, y) _CONCAT(x, y) #define INSERT_PADDING(length) \ @@ -21,6 +23,9 @@ const HMODULE MODULE_HANDLE = GetModuleHandle(nullptr); #define FUNCTION_PTR(returnType, callingConvention, function, location, ...) \ returnType (callingConvention *function)(__VA_ARGS__) = (returnType(callingConvention*)(__VA_ARGS__))(location) +#define FUNCTION_PTR_H(returnType, callingConvention, function, ...) \ + extern returnType (callingConvention *function) (__VA_ARGS__) + #define PROC_ADDRESS(libraryName, procName) \ GetProcAddress(LoadLibrary(TEXT(libraryName)), procName) @@ -55,6 +60,9 @@ const HMODULE MODULE_HANDLE = GetModuleHandle(nullptr); } \ } +#define READ_MEMORY(location, type) \ + *(type *)location + #define WRITE_MEMORY(location, type, ...) \ { \ const type data[] = { __VA_ARGS__ }; \ @@ -64,6 +72,14 @@ const HMODULE MODULE_HANDLE = GetModuleHandle(nullptr); VirtualProtect((void*)(location), sizeof(data), oldProtect, &oldProtect); \ } +#define WRITE_MEMORY_STRING(location, data, length) \ + { \ + DWORD oldProtect; \ + VirtualProtect ((void *)(location), length, PAGE_EXECUTE_READWRITE, &oldProtect); \ + memcpy ((void *)(location), data, length); \ + VirtualProtect ((void *)(location), length, oldProtect, &oldProtect); \ + } + #define WRITE_JUMP(location, function) \ { \ WRITE_MEMORY(location, uint8_t, 0xE9); \ @@ -84,3 +100,15 @@ const HMODULE MODULE_HANDLE = GetModuleHandle(nullptr); *((uint8_t*)(location) + i) = 0x90; \ VirtualProtect((void*)(location), (size_t)(count), oldProtect, &oldProtect); \ } + +#define WRITE_NULL(location, count) \ + { \ + DWORD oldProtect; \ + VirtualProtect((void*)(location), (size_t)(count), PAGE_EXECUTE_READWRITE, &oldProtect); \ + for (size_t i = 0; i < (size_t)(count); i++) \ + *((uint8_t*)(location) + i) = 0x00; \ + VirtualProtect((void*)(location), (size_t)(count), oldProtect, &oldProtect); \ + } + +#define COUNTOFARR(arr) \ + sizeof(arr) / sizeof(arr[0]) diff --git a/extern/src/SigScan.h b/extern/src/SigScan.h index cc2c7e7..f5ed89d 100644 --- a/extern/src/SigScan.h +++ b/extern/src/SigScan.h @@ -2,8 +2,7 @@ #include -FORCEINLINE const MODULEINFO& getModuleInfo() -{ +FORCEINLINE const MODULEINFO& getModuleInfo() { static MODULEINFO moduleInfo; if (moduleInfo.SizeOfImage) @@ -16,18 +15,15 @@ FORCEINLINE const MODULEINFO& getModuleInfo() } // Signature scan in specified memory region -FORCEINLINE void* sigScan(const char* signature, const char* mask, size_t sigSize, void* memory, const size_t memorySize) -{ +FORCEINLINE void* sigScan(const char* signature, const char* mask, size_t sigSize, void* memory, const size_t memorySize) { if (sigSize == 0) sigSize = strlen(mask); - for (size_t i = 0; i < memorySize; i++) - { + for (size_t i = 0; i < memorySize; i++) { char* currMemory = (char*)memory + i; size_t j; - for (j = 0; j < sigSize; j++) - { + for (j = 0; j < sigSize; j++) { if (mask[j] != '?' && signature[j] != currMemory[j]) break; } @@ -40,14 +36,12 @@ FORCEINLINE void* sigScan(const char* signature, const char* mask, size_t sigSiz } // Signature scan in current process -FORCEINLINE void* sigScan(const char* signature, const char* mask, void* hint) -{ +FORCEINLINE void* sigScan(const char* signature, const char* mask, void* hint) { const MODULEINFO& info = getModuleInfo(); const size_t sigSize = strlen(mask); // Ensure hint address is within the process memory region so there are no crashes. - if ((hint >= info.lpBaseOfDll) && ((char*)hint + sigSize <= (char*)info.lpBaseOfDll + info.SizeOfImage)) - { + if ((hint >= info.lpBaseOfDll) && ((char*)hint + sigSize <= (char*)info.lpBaseOfDll + info.SizeOfImage)) { void* result = sigScan(signature, mask, sigSize, hint, sigSize); if (result) @@ -57,33 +51,61 @@ FORCEINLINE void* sigScan(const char* signature, const char* mask, void* hint) return sigScan(signature, mask, sigSize, info.lpBaseOfDll, info.SizeOfImage); } +// Signature scan string in specified memory region +FORCEINLINE void* sigScanString(const char* signature, void* memory, const size_t memorySize) { + const size_t sigSize = strlen(signature); + + for (size_t i = 0; i < memorySize; i++) { + char* currMemory = (char*)memory + i; + + if (!memcmp(signature, currMemory, sigSize)) + return currMemory; + } + + return nullptr; +} + +// Signature scan string in current process +FORCEINLINE void* sigScanString(const char* signature) { + const MODULEINFO& info = getModuleInfo(); + return sigScanString(signature, info.lpBaseOfDll, info.SizeOfImage); +} + // Automatically scanned signature #define SIG_SCAN(x, y, ...) \ FORCEINLINE void* x(); \ inline bool x##Valid = true; \ inline void* x##Addr = x(); \ - FORCEINLINE void* x() \ - { \ + FORCEINLINE void* x() { \ constexpr const char* x##Data[] = { __VA_ARGS__ }; \ constexpr size_t x##Size = _countof(x##Data); \ - if (!x##Addr) \ - { \ - if constexpr (x##Size == 2) \ - { \ + if (!x##Addr) { \ + if constexpr (x##Size == 2) { \ x##Addr = sigScan(x##Data[0], x##Data[1], (void*)(y)); \ if (x##Addr) \ return x##Addr; \ } \ else \ - { \ - for (int i = 0; i < x##Size; i += 2) \ - { \ + for (int i = 0; i < x##Size; i += 2) { \ x##Addr = sigScan(x##Data[i], x##Data[i + 1], (void*)(y)); \ if (x##Addr) \ return x##Addr; \ } \ - } \ x##Valid = false; \ } \ return x##Addr; \ } + +#define SIG_SCAN_STRING(x, y) \ + FORCEINLINE void* x(); \ + inline bool x##Valid = true; \ + inline void* x##Addr = x(); \ + FORCEINLINE void* x() { \ + if (!x##Addr) { \ + x##Addr = sigScanString(y); \ + if (x##Addr) \ + return x##Addr; \ + x##Valid = false; \ + } \ + return x##Addr; \ + } diff --git a/src/OpenLyricLimit/OpenLyricLimit.vcxproj b/src/OpenLyricLimit/OpenLyricLimit.vcxproj index 22d0188..eb54786 100644 --- a/src/OpenLyricLimit/OpenLyricLimit.vcxproj +++ b/src/OpenLyricLimit/OpenLyricLimit.vcxproj @@ -84,7 +84,7 @@ NotUsing Level3 - MinSpace + MaxSpeed WIN32;DINPUT8_DLL;%(PreprocessorDefinitions) true false @@ -93,16 +93,11 @@ true FastCall CompileAsCpp - - $(IntDir)/%(RelativeDir) Speed - - false MultiThreadedDLL 26812 - true $(SolutionDir)extern\src stdcpp17 @@ -111,11 +106,9 @@ true Windows detours.lib;syelog.lib;%(AdditionalDependencies) - true /ignore:4098 /ignore:4099 /ignore:4286 %(AdditionalOptions) true false - UseLinkTimeCodeGeneration $(SolutionDir)extern\lib\ diff --git a/src/PotatoMM/PotatoMM.vcxproj b/src/PotatoMM/PotatoMM.vcxproj index 9afb5f0..d1353f0 100644 --- a/src/PotatoMM/PotatoMM.vcxproj +++ b/src/PotatoMM/PotatoMM.vcxproj @@ -84,7 +84,7 @@ NotUsing Level3 - MinSpace + MaxSpeed WIN32;DINPUT8_DLL;%(PreprocessorDefinitions) true false @@ -93,16 +93,11 @@ true FastCall CompileAsCpp - - $(IntDir)/%(RelativeDir) Speed - - false MultiThreadedDLL 26812 - true $(SolutionDir)extern\src stdcpp17 @@ -111,11 +106,9 @@ true Windows detours.lib;syelog.lib;%(AdditionalDependencies) - true /ignore:4098 /ignore:4099 /ignore:4286 %(AdditionalOptions) true false - UseLinkTimeCodeGeneration $(SolutionDir)extern\lib\