Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/DLLmain.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define _D2VARS_H

#include "DLLmain.h"
#include <windows.h>
#include "D2Patch.h"

/****************************************************************************
Expand Down Expand Up @@ -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++)
Expand All @@ -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++)
Expand Down Expand Up @@ -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);
}
25 changes: 24 additions & 1 deletion src/DLLmain.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#pragma once

#ifndef _DLLMAIN_H
#define _DLLMAIN_H

/****************************************************************************
* *
* DLLmain.h *
Expand All @@ -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 <windows.h>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
char* __fastcall GetModuleExt(char* ModuleName);

#endif // _DLLMAIN_H