Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BinCompile.cpp
- #include "stdafx.h"
- #include "D2Hook.h"
- externs;
- extern void ErrorLogWrite(LPSTR szMessage);
- void binImageDebug(void){
- ErrorLogWrite("Bin images loaded");
- };
- static void __declspec(naked) _b_binImageDebug(void) {
- _asm{
- pushad;
- call binImageDebug;
- popad;
- push EBX;
- push ESI;
- mov eax,0x6FD504B8
- jmp eax;
- }
- };
- PatchData_t *h_binImageDebug(void){
- PatchData_t *returnValue;
- returnValue = PlacePatch(PATCH_JUMP, 0x6FD504B6, (unsigned int)&_b_binImageDebug);
- return returnValue;
- }
- //D2Hook.h
- extern CHAR szAppPath[MAX_PATH]; //The current working directory
- typedef struct {
- int addr;
- int size;
- char origbytes[24];
- } PatchData_t;
- typedef enum {
- PATCH_JUMP,
- PATCH_CALL,
- } PatchType_e;
- typedef enum{
- UERR_ERROR_OK,
- UERR_ERROR_LOAD_LIBRARY,
- UERR_ERROR_OUT_OF_MEMORY,
- UERR_ERROR_PROTECT_MODE,
- UERR_ERROR_MAX
- }UERR_ERROR;
- #define UERR_ERROR_COUNT UERR_ERROR_MAX+1
- typedef BOOL (* UERR_ERROR_HANDLER)(UERR_ERROR ueCode);
- //Name: UErrThrowError
- //Function: Throws an error to be handled by the error handler
- void ThrowError(LPSTR szFile, LPSTR szFunc, LPSTR szDesc, UERR_ERROR ueCode);
- //Name: UErrAddHandler
- //Function: Adds an error handler for the specified code
- void UErrAddHandler(UERR_ERROR_HANDLER lpfn, UERR_ERROR ueCode);
- //Name: UErrLogInit
- //Function: Initializes an error/debugging log
- void UErrLogInit(LPSTR szFile);
- //Name: UErrLogWrite
- //Function: Writes a line to the error/debugging log
- void ErrorLogWrite(LPSTR szMessage);
- //Name: UErrLogWritef
- //Function: Writes a line to the error/debugging log, printf format
- void __cdecl UErrLogWritef(LPSTR szFormat, ...);
- #define externs extern PatchData_t *PlacePatch( int type, unsigned int address, unsigned int destination );
- //D2Hook.cpp
- #include "stdafx.h"
- #include "D2Hook.h"
- HANDLE hDLLModule = NULL;
- CHAR szAppPath[MAX_PATH] = ""; //The current working directory
- static CHAR szErrorDesc[1024] = ""; //Provides storage for error description
- static CHAR szErrorFunc[256] = ""; //Provides storage for function causing error
- static CHAR szErrorFile[256] = ""; //Provides storage for file causing error
- static UERR_ERROR ueErrCode = UERR_ERROR_OK; //Current error code
- static CHAR szLogFile[MAX_PATH] = "";
- static BOOL bLogInit = FALSE;
- static UERR_ERROR_HANDLER ueHandlers[UERR_ERROR_COUNT]; //Array of error handlers
- static LPSTR szFile = "root\\Main\\UError.cpp";
- extern PatchData_t *h_binImageDebug(void);
- static void MainGetAppPath(){
- _getcwd(szAppPath, MAX_PATH);
- if(szAppPath[strlen(szAppPath)] != '\\'){
- szAppPath[strlen(szAppPath) + 1] = 0;
- szAppPath[strlen(szAppPath)] = '\\';
- }
- }
- static int UnlockMemory(int address, int size) {
- int ret;
- int dummy;
- ret = VirtualProtect((LPVOID)address, size, PAGE_EXECUTE_READWRITE, (PDWORD)&dummy);
- return (ret != 0);
- }
- // ==================================================
- // LockMemory (WIN32 & Linux compatible)
- // --------------------------------------------------
- // Makes the memory at address read-only for at least
- // size bytes.
- // Returns 1 if successful, returns 0 on failure.
- // ==================================================
- static int LockMemory(int address, int size) {
- int ret;
- ret = VirtualProtect((LPVOID)address, size, PAGE_EXECUTE_READ, NULL);
- return (ret != 0);
- }
- PatchData_t *PlacePatch( int type, unsigned int address, unsigned int destination ) {
- PatchData_t *patch = new PatchData_t;
- // Disassembler code removed
- int sz = 5;
- patch->addr = address;
- patch->size = sz;
- memcpy(patch->origbytes, (const void *)address, sz);
- UnlockMemory(address, sz); // Make the memory writable
- *(unsigned char *)address = type == PATCH_JUMP ? 0xE9 : 0xE8;
- *(unsigned int *)(address+1) = destination - (address + 5);
- memset((void *)(address+5),0x90,sz-5); // Nop the rest
- LockMemory(address, sz);
- return patch;
- }
- static void RemovePatch(PatchData_t **patch) {
- if (!*patch)
- return;
- UnlockMemory((*patch)->addr, (*patch)->size);
- memcpy((void *)(*patch)->addr, (*patch)->origbytes, (*patch)->size);
- LockMemory((*patch)->addr, (*patch)->size);
- delete *patch;
- *patch = 0;
- }
- // ==================================================
- // PatchByte (WIN32 & Linux compatible)
- // --------------------------------------------------
- // Modifies a single byte
- // Returns 1 if successful, returns 0 on failure.
- // ==================================================
- static int PatchByte(int address, unsigned char newbyte) {
- if (!UnlockMemory(address, 1)) return 0;
- *(unsigned char *)address = newbyte;
- LockMemory(address, 1);
- return 1;
- }
- // ========================================================================
- // ERROR HANDLING
- // ========================================================================
- //===================================================
- // ErrorExit
- // --------------------------------------------------
- // Exits with an error
- static void ErrorExit(UERR_ERROR ueCode){
- //Log this exit
- ErrorLogWrite("Performing emergency shutdown under error conditions.");
- exit(ueCode);
- }
- //===================================================
- // ThrowError
- // --------------------------------------------------
- // Throws an error that will be handled by error handler
- void ThrowError(LPSTR szFile, LPSTR szFunc, LPSTR szDesc, UERR_ERROR ueCode){
- strcpy(szErrorFile, szFile);
- strcpy(szErrorFunc, szFunc);
- strcpy(szErrorDesc, szDesc);
- ueErrCode = ueCode;
- //Log the error
- ErrorLogWrite("==============================================================");
- UErrLogWritef("\tError Code: %u", ueCode);
- UErrLogWritef("\tFile: %s", szErrorFile);
- UErrLogWritef("\tFunction: %s", szErrorFunc);
- UErrLogWritef("\tDescription: %s", szErrorDesc);
- ErrorLogWrite("==============================================================");
- //Try and pass this to an error handler
- BOOL bHandled = FALSE;
- if(IsBadCodePtr((FARPROC)ueHandlers[ueErrCode]) == 0){
- bHandled = ueHandlers[ueErrCode](ueErrCode);
- }
- if(!bHandled){
- ErrorExit(ueErrCode);
- }
- }
- //Name: UErrAddHandler
- //Function: Adds an error handler for the specified code
- void UErrAddHandler(UERR_ERROR_HANDLER lpfn, UERR_ERROR ueCode){
- ueHandlers[ueCode] = lpfn;
- }
- //Name: UErrLogInit
- //Function: Initializes an error/debugging log
- void UErrLogInit(LPSTR szFile){
- strcpy(szLogFile, szFile);
- FILE *fLog = fopen(szLogFile, "w");
- if(fLog == NULL){
- return;
- }
- fclose(fLog);
- bLogInit = TRUE;
- }
- //Name: UErrLogWrite
- //Function: Writes a line to the error/debugging log
- void ErrorLogWrite(LPSTR szMessage){
- time_t timeStamp;
- tm * ptm;
- time( &timeStamp );
- ptm = gmtime ( &timeStamp );
- if(!bLogInit){
- return;
- }
- FILE *fDebug = fopen(szLogFile, "a+");
- if(fDebug == NULL){
- bLogInit = FALSE;
- return;
- }
- fprintf(fDebug, "[%2d:%02d:%02d] ", (ptm->tm_hour)%24, ptm->tm_min, ptm->tm_sec);
- fprintf(fDebug, szMessage);
- fprintf(fDebug, "\n");
- fclose(fDebug);
- }
- //Name: UErrLogWritef
- //Function: Writes a line to the error/debugging log, printf format
- void __cdecl UErrLogWritef(LPSTR szFormat, ...){
- if(!bLogInit){
- bLogInit = FALSE;
- return;
- }
- va_list vaArgs;
- va_start(vaArgs, szFormat);
- FILE *fDebug = fopen(szLogFile, "a+");
- if(fDebug == NULL){
- return;
- }
- vfprintf(fDebug, szFormat, vaArgs);
- fprintf(fDebug, "\n");
- fclose(fDebug);
- va_end(vaArgs);
- }
- //Name: UErrGetFile
- //Function: Gets the file of the current/last thrown error
- LPSTR UErrGetFile(){
- return szErrorFile;
- }
- //Name: UErrGetFunction
- //Function: Gets the function of the current/last thrown error
- LPSTR UErrGetFunction(){
- return szErrorFunc;
- }
- //Name: UErrGetDescription
- //Function: Gets the description of the current/last thrown error
- LPSTR UErrGetDescription(){
- return szErrorDesc;
- }
- //Name: UErrGetCode
- //Function: Gets the code of the current/last thrown error
- UERR_ERROR UErrGetCode(){
- return ueErrCode;
- }
- extern PatchData_t *h_levelUpEffect(void);
- void performNecessaryHooks(void){
- //PatchData_t *patch;
- h_binImageDebug();
- }
- void cleanupAllHooks(void){
- }
- void hookInit(void){
- LPCWSTR library = TEXT("D2Launch.dll");
- MainGetAppPath();
- //Initialize logging
- CHAR szLogFile[1024] = "";
- sprintf(szLogFile, "%sDebugLog.txt", szAppPath);
- UErrLogInit(szLogFile);
- ErrorLogWrite("Starting Diablo II...");
- //Load necessary DLLs
- if(!LoadLibrary(library)){
- ThrowError(szFile, "MainInit", "Failed to load library D2Launch.dll", UERR_ERROR_LOAD_LIBRARY);
- }
- library = TEXT("D2Common.dll");
- if(!LoadLibrary(library)){
- ThrowError(szFile, "MainInit", "Failed to load library D2Common.dll", UERR_ERROR_LOAD_LIBRARY);
- }
- library = TEXT("D2Game.dll");
- if(!LoadLibrary(library)){
- ThrowError(szFile, "MainInit", "Failed to load library D2Game.dll", UERR_ERROR_LOAD_LIBRARY);
- }
- library = TEXT("D2Client.dll");
- if(!LoadLibrary(library)){
- ThrowError(szFile, "MainInit", "Failed to load library D2Client.dll", UERR_ERROR_LOAD_LIBRARY);
- }
- library = TEXT("D2Extra.dll");
- if(!LoadLibrary(library)){
- ThrowError(szFile, "MainInit", "Failed to load library D2Extra.dll", UERR_ERROR_LOAD_LIBRARY);
- }
- library = TEXT("D2Win.dll");
- if(!LoadLibrary(library)){
- ThrowError(szFile, "MainInit", "Failed to load library D2Win.dll", UERR_ERROR_LOAD_LIBRARY);
- }
- library = TEXT("Fog.dll");
- if(!LoadLibrary(library)){
- ThrowError(szFile, "MainInit", "Failed to load library Fog.dll", UERR_ERROR_LOAD_LIBRARY);
- }
- performNecessaryHooks();
- }
- void hookCleanup(void){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement