From 2d15a1efa19dd5ee1ab6ea7b3242bc5fb9da036a Mon Sep 17 00:00:00 2001 From: ssjkakaroto Date: Mon, 11 May 2026 16:54:37 -0300 Subject: [PATCH] Expose ApplyPatch and create PatchBytes --- src/DLLmain.cpp | 37 ++++++++++++++++++++++++++++++++++++- src/DLLmain.h | 25 ++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/DLLmain.cpp b/src/DLLmain.cpp index a15b283..c9549a2 100644 --- a/src/DLLmain.cpp +++ b/src/DLLmain.cpp @@ -1,6 +1,7 @@ #define _D2VARS_H #include "DLLmain.h" +#include #include "D2Patch.h" /**************************************************************************** @@ -57,6 +58,8 @@ BOOL __fastcall D2TEMPLATE_ApplyPatch(void* hGame, const DLLPatchStrc* hPatch) if (hPatch->nPatchSize > 0) { + if (hPatch->nPatchSize > 1024) return FALSE; + BYTE Buffer[1024]; for (size_t i = 0; i < hPatch->nPatchSize; i++) @@ -82,6 +85,38 @@ BOOL __fastcall D2TEMPLATE_ApplyPatch(void* hGame, const DLLPatchStrc* hPatch) return TRUE; } +BOOL __fastcall D2TEMPLATE_PatchBytes(void* hGame, const DLLBytesPatchStrc* hPatch) +{ + while (hPatch->nDLL != D2DLL_INVALID) + { + int nDLL = hPatch->nDLL; + if (nDLL < 0 || nDLL >= D2DLL_INVALID) return FALSE; + + DWORD dwAddress = hPatch->dwAddress; + if (!dwAddress) return FALSE; + + DWORD dwBaseAddress = gptDllFiles[nDLL].dwAddress; + if (!dwBaseAddress) return FALSE; + + dwAddress += dwBaseAddress; + + if (!hPatch->pData || hPatch->nSize == 0) return FALSE; + + void* hAddress = (void*)dwAddress; + DWORD dwOldPage; + + VirtualProtect(hAddress, hPatch->nSize, PAGE_EXECUTE_READWRITE, &dwOldPage); + int nReturn = WriteProcessMemory(hGame, hAddress, hPatch->pData, hPatch->nSize, 0); + VirtualProtect(hAddress, hPatch->nSize, dwOldPage, 0); + + if (nReturn == 0) return FALSE; + + hPatch++; + } + + return TRUE; +} + BOOL __fastcall D2TEMPLATE_LoadModules() { for (int i = 0; i < D2DLL_INVALID; i++) @@ -183,7 +218,7 @@ DWORD __fastcall GetDllOffset(char* ModuleName, DWORD BaseAddress, int Offset) char* __fastcall GetModuleExt(char* ModuleName) { char DLLExt[] = ".dll"; - char DLLName[32] = {0}; + static char DLLName[32] = {0}; strcpy(DLLName,ModuleName); return strcat(DLLName,DLLExt); } \ No newline at end of file diff --git a/src/DLLmain.h b/src/DLLmain.h index 6d4f439..29de7a1 100644 --- a/src/DLLmain.h +++ b/src/DLLmain.h @@ -1,3 +1,8 @@ +#pragma once + +#ifndef _DLLMAIN_H +#define _DLLMAIN_H + /**************************************************************************** * * * DLLmain.h * @@ -23,7 +28,10 @@ *****************************************************************************/ #define WIN32_LEAN_AND_MEAN +#ifndef _CRT_SECURE_NO_DEPRECATE #define _CRT_SECURE_NO_DEPRECATE +#endif +#undef _WIN32_WINNT #define _WIN32_WINNT 0x600 #include @@ -75,6 +83,17 @@ struct DLLPatchStrc size_t nPatchSize; }; +/* Companion struct — mirrors DLLPatchStrc but carries a raw byte array + * instead of a single DWORD value. + * Terminate patch tables with { D2DLL_INVALID, 0, nullptr, 0 }. */ +struct DLLBytesPatchStrc +{ + int nDLL; /* target DLL index, D2DLL_INVALID = end sentinel */ + DWORD dwAddress; /* offset within DLL */ + const BYTE *pData; /* pointer to byte array to write */ + size_t nSize; /* number of bytes to write */ +}; + enum D2TEMPLATE_DLL_FILES { D2DLL_BINKW32, @@ -129,5 +148,9 @@ static DLLBaseStrc gptDllFiles [] = }; void __fastcall D2TEMPLATE_FatalError(char* szMessage); +BOOL __fastcall D2TEMPLATE_ApplyPatch(void* hGame, const DLLPatchStrc* hPatch); +BOOL __fastcall D2TEMPLATE_PatchBytes(void* hGame, const DLLBytesPatchStrc* hPatch); DWORD __fastcall GetDllOffset(char* ModuleName, DWORD BaseAddress, int Offset); -char* __fastcall GetModuleExt(char* ModuleName); \ No newline at end of file +char* __fastcall GetModuleExt(char* ModuleName); + +#endif // _DLLMAIN_H \ No newline at end of file