Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.53 KB | None | 0 0
  1. //BinCompile.cpp
  2. #include "stdafx.h"
  3. #include "D2Hook.h"
  4.  
  5. externs;
  6. extern void ErrorLogWrite(LPSTR szMessage);
  7.  
  8. void binImageDebug(void){
  9.     ErrorLogWrite("Bin images loaded");
  10. };
  11. static void __declspec(naked) _b_binImageDebug(void) {
  12.     _asm{
  13.         pushad;
  14.         call binImageDebug;
  15.         popad;
  16.         push EBX;
  17.         push ESI;
  18.         mov eax,0x6FD504B8
  19.         jmp eax;
  20.     }
  21. };
  22. PatchData_t *h_binImageDebug(void){
  23.     PatchData_t *returnValue;
  24.     returnValue = PlacePatch(PATCH_JUMP, 0x6FD504B6, (unsigned int)&_b_binImageDebug);
  25.     return returnValue;
  26. }
  27.  
  28. //D2Hook.h
  29. extern CHAR szAppPath[MAX_PATH]; //The current working directory
  30.  
  31. typedef struct {
  32.     int addr;
  33.     int size;
  34.     char origbytes[24];
  35. } PatchData_t;
  36.  
  37. typedef enum {
  38.     PATCH_JUMP,
  39.     PATCH_CALL,
  40. } PatchType_e;
  41.  
  42. typedef enum{
  43.     UERR_ERROR_OK,
  44.     UERR_ERROR_LOAD_LIBRARY,
  45.     UERR_ERROR_OUT_OF_MEMORY,
  46.     UERR_ERROR_PROTECT_MODE,
  47.     UERR_ERROR_MAX
  48. }UERR_ERROR;
  49. #define UERR_ERROR_COUNT    UERR_ERROR_MAX+1
  50.  
  51. typedef BOOL (* UERR_ERROR_HANDLER)(UERR_ERROR ueCode);
  52.  
  53. //Name: UErrThrowError
  54. //Function: Throws an error to be handled by the error handler
  55. void ThrowError(LPSTR szFile, LPSTR szFunc, LPSTR szDesc, UERR_ERROR ueCode);
  56.  
  57. //Name: UErrAddHandler
  58. //Function: Adds an error handler for the specified code
  59. void UErrAddHandler(UERR_ERROR_HANDLER lpfn, UERR_ERROR ueCode);
  60.  
  61. //Name: UErrLogInit
  62. //Function: Initializes an error/debugging log
  63. void UErrLogInit(LPSTR szFile);
  64.  
  65. //Name: UErrLogWrite
  66. //Function: Writes a line to the error/debugging log
  67. void ErrorLogWrite(LPSTR szMessage);
  68.  
  69. //Name: UErrLogWritef
  70. //Function: Writes a line to the error/debugging log, printf format
  71. void __cdecl UErrLogWritef(LPSTR szFormat, ...);
  72.  
  73. #define externs extern PatchData_t *PlacePatch( int type, unsigned int address, unsigned int destination );
  74.  
  75. //D2Hook.cpp
  76. #include "stdafx.h"
  77. #include "D2Hook.h"
  78.  
  79. HANDLE hDLLModule = NULL;
  80. CHAR szAppPath[MAX_PATH] = ""; //The current working directory
  81.  
  82. static CHAR szErrorDesc[1024] = ""; //Provides storage for error description
  83. static CHAR szErrorFunc[256] = ""; //Provides storage for function causing error
  84. static CHAR szErrorFile[256] = ""; //Provides storage for file causing error
  85. static UERR_ERROR ueErrCode = UERR_ERROR_OK; //Current error code
  86.  
  87. static CHAR szLogFile[MAX_PATH] = "";
  88. static BOOL bLogInit = FALSE;
  89.  
  90. static UERR_ERROR_HANDLER ueHandlers[UERR_ERROR_COUNT]; //Array of error handlers
  91.  
  92. static LPSTR szFile = "root\\Main\\UError.cpp";
  93.  
  94. extern PatchData_t *h_binImageDebug(void);
  95.  
  96. static void MainGetAppPath(){
  97.  
  98.     _getcwd(szAppPath, MAX_PATH);
  99.  
  100.     if(szAppPath[strlen(szAppPath)] != '\\'){
  101.         szAppPath[strlen(szAppPath) + 1] = 0;
  102.         szAppPath[strlen(szAppPath)] = '\\';
  103.     }
  104.  
  105. }
  106.  
  107. static int UnlockMemory(int address, int size) {
  108.     int ret;
  109.     int dummy;
  110.     ret = VirtualProtect((LPVOID)address, size, PAGE_EXECUTE_READWRITE, (PDWORD)&dummy);
  111.     return (ret != 0);
  112. }
  113.  
  114. // ==================================================
  115. // LockMemory (WIN32 & Linux compatible)
  116. // --------------------------------------------------
  117. // Makes the memory at address read-only for at least
  118. // size bytes.
  119. // Returns 1 if successful, returns 0 on failure.
  120. // ==================================================
  121. static int LockMemory(int address, int size) {
  122.     int ret;
  123.     ret = VirtualProtect((LPVOID)address, size, PAGE_EXECUTE_READ, NULL);
  124.     return (ret != 0);
  125. }
  126.  
  127. PatchData_t *PlacePatch( int type, unsigned int address, unsigned int destination ) {
  128.     PatchData_t *patch = new PatchData_t;
  129.    
  130.     // Disassembler code removed
  131.     int sz = 5;
  132.  
  133.     patch->addr = address;
  134.     patch->size = sz;
  135.     memcpy(patch->origbytes, (const void *)address, sz);
  136.     UnlockMemory(address, sz); // Make the memory writable
  137.     *(unsigned char *)address = type == PATCH_JUMP ? 0xE9 : 0xE8;
  138.     *(unsigned int *)(address+1) = destination - (address + 5);
  139.     memset((void *)(address+5),0x90,sz-5);  // Nop the rest
  140.     LockMemory(address, sz);
  141.     return patch;
  142. }
  143.  
  144. static void RemovePatch(PatchData_t **patch) {
  145.     if (!*patch)
  146.         return;
  147.     UnlockMemory((*patch)->addr, (*patch)->size);
  148.     memcpy((void *)(*patch)->addr, (*patch)->origbytes, (*patch)->size);
  149.     LockMemory((*patch)->addr, (*patch)->size);
  150.     delete *patch;
  151.     *patch = 0;
  152. }
  153.  
  154. // ==================================================
  155. // PatchByte (WIN32 & Linux compatible)
  156. // --------------------------------------------------
  157. // Modifies a single byte
  158. // Returns 1 if successful, returns 0 on failure.
  159. // ==================================================
  160. static int PatchByte(int address, unsigned char newbyte) {
  161.     if (!UnlockMemory(address, 1)) return 0;
  162.     *(unsigned char *)address = newbyte;
  163.     LockMemory(address, 1);
  164.     return 1;
  165. }
  166.  
  167.  
  168.  
  169. // ========================================================================
  170. //                      ERROR HANDLING
  171. // ========================================================================
  172.  
  173. //===================================================
  174. // ErrorExit
  175. // --------------------------------------------------
  176. // Exits with an error
  177. static void ErrorExit(UERR_ERROR ueCode){
  178.  
  179.     //Log this exit
  180.     ErrorLogWrite("Performing emergency shutdown under error conditions.");
  181.  
  182.     exit(ueCode);
  183.  
  184. }
  185.  
  186. //===================================================
  187. // ThrowError
  188. // --------------------------------------------------
  189. // Throws an error that will be handled by error handler
  190.  
  191. void ThrowError(LPSTR szFile, LPSTR szFunc, LPSTR szDesc, UERR_ERROR ueCode){
  192.     strcpy(szErrorFile, szFile);
  193.     strcpy(szErrorFunc, szFunc);
  194.     strcpy(szErrorDesc, szDesc);
  195.     ueErrCode = ueCode;
  196.  
  197.     //Log the error
  198.     ErrorLogWrite("==============================================================");
  199.     UErrLogWritef("\tError Code: %u", ueCode);
  200.     UErrLogWritef("\tFile: %s", szErrorFile);
  201.     UErrLogWritef("\tFunction: %s", szErrorFunc);
  202.     UErrLogWritef("\tDescription: %s", szErrorDesc);
  203.     ErrorLogWrite("==============================================================");
  204.  
  205.     //Try and pass this to an error handler
  206.     BOOL bHandled = FALSE;
  207.     if(IsBadCodePtr((FARPROC)ueHandlers[ueErrCode]) == 0){
  208.         bHandled = ueHandlers[ueErrCode](ueErrCode);
  209.     }
  210.  
  211.     if(!bHandled){
  212.         ErrorExit(ueErrCode);
  213.     }
  214.  
  215. }
  216.  
  217. //Name: UErrAddHandler
  218. //Function: Adds an error handler for the specified code
  219. void UErrAddHandler(UERR_ERROR_HANDLER lpfn, UERR_ERROR ueCode){
  220.  
  221.     ueHandlers[ueCode] = lpfn;
  222.  
  223. }
  224.  
  225. //Name: UErrLogInit
  226. //Function: Initializes an error/debugging log
  227. void UErrLogInit(LPSTR szFile){
  228.  
  229.     strcpy(szLogFile, szFile);
  230.  
  231.     FILE *fLog = fopen(szLogFile, "w");
  232.     if(fLog == NULL){
  233.         return;
  234.     }
  235.     fclose(fLog);
  236.  
  237.     bLogInit = TRUE;
  238.  
  239. }
  240.  
  241. //Name: UErrLogWrite
  242. //Function: Writes a line to the error/debugging log
  243. void ErrorLogWrite(LPSTR szMessage){
  244.     time_t timeStamp;
  245.     tm * ptm;
  246.  
  247.     time( &timeStamp );
  248.  
  249.     ptm = gmtime ( &timeStamp );
  250.  
  251.     if(!bLogInit){
  252.         return;
  253.     }
  254.  
  255.     FILE *fDebug = fopen(szLogFile, "a+");
  256.     if(fDebug == NULL){
  257.         bLogInit = FALSE;
  258.         return;
  259.     }
  260.     fprintf(fDebug, "[%2d:%02d:%02d] ", (ptm->tm_hour)%24, ptm->tm_min, ptm->tm_sec);
  261.     fprintf(fDebug, szMessage);
  262.     fprintf(fDebug, "\n");
  263.     fclose(fDebug);
  264.  
  265. }
  266.  
  267. //Name: UErrLogWritef
  268. //Function: Writes a line to the error/debugging log, printf format
  269. void __cdecl UErrLogWritef(LPSTR szFormat, ...){
  270.  
  271.     if(!bLogInit){
  272.         bLogInit = FALSE;
  273.         return;
  274.     }
  275.  
  276.     va_list vaArgs;
  277.     va_start(vaArgs, szFormat);
  278.  
  279.     FILE *fDebug = fopen(szLogFile, "a+");
  280.     if(fDebug == NULL){
  281.         return;
  282.     }
  283.     vfprintf(fDebug, szFormat, vaArgs);
  284.     fprintf(fDebug, "\n");
  285.     fclose(fDebug);
  286.  
  287.     va_end(vaArgs);
  288.  
  289. }
  290.  
  291. //Name: UErrGetFile
  292. //Function: Gets the file of the current/last thrown error
  293. LPSTR UErrGetFile(){
  294.  
  295.     return szErrorFile;
  296.  
  297. }
  298.  
  299. //Name: UErrGetFunction
  300. //Function: Gets the function of the current/last thrown error
  301. LPSTR UErrGetFunction(){
  302.  
  303.     return szErrorFunc;
  304.  
  305. }
  306.  
  307. //Name: UErrGetDescription
  308. //Function: Gets the description of the current/last thrown error
  309. LPSTR UErrGetDescription(){
  310.  
  311.     return szErrorDesc;
  312.  
  313. }
  314.  
  315. //Name: UErrGetCode
  316. //Function: Gets the code of the current/last thrown error
  317. UERR_ERROR UErrGetCode(){
  318.  
  319.     return ueErrCode;
  320.  
  321. }
  322.  
  323. extern PatchData_t *h_levelUpEffect(void);
  324. void performNecessaryHooks(void){
  325.     //PatchData_t *patch;
  326.     h_binImageDebug();
  327. }
  328. void cleanupAllHooks(void){
  329. }
  330. void hookInit(void){
  331.     LPCWSTR library = TEXT("D2Launch.dll");
  332.  
  333.     MainGetAppPath();  
  334.  
  335.     //Initialize logging
  336.     CHAR szLogFile[1024] = "";
  337.     sprintf(szLogFile, "%sDebugLog.txt", szAppPath);
  338.     UErrLogInit(szLogFile);
  339.     ErrorLogWrite("Starting Diablo II...");
  340.  
  341.     //Load necessary DLLs
  342.     if(!LoadLibrary(library)){
  343.         ThrowError(szFile, "MainInit", "Failed to load library D2Launch.dll", UERR_ERROR_LOAD_LIBRARY);
  344.     }
  345.  
  346.     library = TEXT("D2Common.dll");
  347.     if(!LoadLibrary(library)){
  348.         ThrowError(szFile, "MainInit", "Failed to load library D2Common.dll", UERR_ERROR_LOAD_LIBRARY);
  349.     }
  350.  
  351.     library = TEXT("D2Game.dll");
  352.     if(!LoadLibrary(library)){
  353.         ThrowError(szFile, "MainInit", "Failed to load library D2Game.dll", UERR_ERROR_LOAD_LIBRARY);
  354.     }
  355.  
  356.     library = TEXT("D2Client.dll");
  357.     if(!LoadLibrary(library)){
  358.         ThrowError(szFile, "MainInit", "Failed to load library D2Client.dll", UERR_ERROR_LOAD_LIBRARY);
  359.     }
  360.  
  361.     library = TEXT("D2Extra.dll");
  362.     if(!LoadLibrary(library)){
  363.         ThrowError(szFile, "MainInit", "Failed to load library D2Extra.dll", UERR_ERROR_LOAD_LIBRARY);
  364.     }
  365.  
  366.     library = TEXT("D2Win.dll");
  367.     if(!LoadLibrary(library)){
  368.         ThrowError(szFile, "MainInit", "Failed to load library D2Win.dll", UERR_ERROR_LOAD_LIBRARY);
  369.     }
  370.  
  371.     library = TEXT("Fog.dll");
  372.     if(!LoadLibrary(library)){
  373.         ThrowError(szFile, "MainInit", "Failed to load library Fog.dll", UERR_ERROR_LOAD_LIBRARY);
  374.     }
  375.  
  376.     performNecessaryHooks();
  377. }
  378. void hookCleanup(void){
  379.    
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement