Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FPS limit removal patch for GothicMod.exe 1.08k_mod
  4. //
  5. //  UNSUPPORTED - USAGE AT YOUR OWN RISK
  6. //
  7. //////////////////////////////////////////////////////////////////////////////
  8.  
  9. #include <windows.h>
  10.  
  11. //////////////////////////////////////////////////////////////////////////////
  12.  
  13. // constants for the patch
  14. const LPTSTR PATCH_CAPTION = TEXT("FPS patch");
  15. const unsigned long PATCH_OFFSET = 0x001CE592;
  16. const unsigned long PATCH_LENGTH = 0x00000007;
  17. const BYTE PATCH_O[PATCH_LENGTH] = {0x51, 0xFF, 0x15, 0xF0, 0x00, 0x7D, 0x00};
  18. const BYTE PATCH_N[PATCH_LENGTH] = {0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90};
  19.  
  20. //////////////////////////////////////////////////////////////////////////////
  21.  
  22. // show error/success messages
  23. void ShowErrorMessage(LPTSTR Func, DWORD Code)
  24. {
  25.     TCHAR Message[1024];
  26.     LPTSTR ErrorMsg;
  27.     LPTSTR Caption;
  28.     DWORD Flags;
  29.  
  30.     FormatMessage(
  31.         FORMAT_MESSAGE_ALLOCATE_BUFFER |
  32.         FORMAT_MESSAGE_FROM_SYSTEM |
  33.         FORMAT_MESSAGE_IGNORE_INSERTS,
  34.         NULL, Code, 0, LPTSTR(&ErrorMsg), 0, NULL);
  35.     wsprintf(Message, TEXT("%s: %s"), Func, ErrorMsg);
  36.     if (Code == ERROR_SUCCESS)
  37.     {
  38.         Caption = PATCH_CAPTION;
  39.         Flags = MB_OK | MB_ICONINFORMATION;
  40.     }
  41.     else
  42.     {
  43.         Caption = NULL;
  44.         Flags = MB_OK | MB_ICONERROR;
  45.     }
  46.     MessageBox(NULL, Message, Caption, Flags);
  47.     if (ErrorMsg) LocalFree((HLOCAL)ErrorMsg);
  48. }
  49.  
  50. // open GUI dialog box to let the user choose the file
  51. BOOL SelectFileToPatch(LPTSTR Filename, DWORD MaxFilename)
  52. {
  53.     OPENFILENAME ofn;
  54.  
  55.     ZeroMemory(&ofn, sizeof(OPENFILENAME));
  56.     ofn.lStructSize = sizeof(OPENFILENAME);
  57.     ofn.lpstrFilter = TEXT("GothicMod.exe\0GothicMod.exe\0");
  58.     ofn.nFilterIndex = 1;
  59.     ofn.lpstrFile = Filename;
  60.     ofn.nMaxFile = MaxFilename;
  61.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST |
  62.                 OFN_NOREADONLYRETURN | OFN_NOTESTFILECREATE |
  63.                 OFN_HIDEREADONLY | OFN_ENABLESIZING;
  64.     return GetOpenFileName(&ofn);
  65. }
  66.  
  67. // get file handle with read/write access
  68. BOOL OpenFileToPatch(LPTSTR Filename, HANDLE &Filehandle)
  69. {
  70.     Filehandle = CreateFile(Filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  71.     if ( Filehandle == INVALID_HANDLE_VALUE )
  72.     {
  73.         ShowErrorMessage(TEXT("Open"), GetLastError());
  74.         return FALSE;
  75.     }
  76.     return TRUE;
  77. }
  78.  
  79. // close file handle
  80. BOOL CloseFileToPatch(HANDLE &Filehandle)
  81. {
  82.     if ( (Filehandle != NULL) && (Filehandle != INVALID_HANDLE_VALUE) )
  83.         CloseHandle(Filehandle);
  84.     Filehandle = INVALID_HANDLE_VALUE;
  85.     return TRUE;
  86. }
  87.  
  88. // seek to the file offset where the patch starts
  89. BOOL SeekFileToPatch(HANDLE Filehandle)
  90. {
  91.     if ( SetFilePointer(Filehandle, PATCH_OFFSET, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER )
  92.     {
  93.         ShowErrorMessage(TEXT("Seek"), GetLastError());
  94.         return FALSE;
  95.     }
  96.     return TRUE;
  97. }
  98.  
  99. // verify if the source pattern matches the original which we want to patch
  100. BOOL VerifyFileToPatch(HANDLE Filehandle)
  101. {
  102.     BYTE Buffer[PATCH_LENGTH];
  103.     DWORD BytesRead;
  104.     int Index;
  105.  
  106.     if ( !ReadFile(Filehandle, &Buffer, PATCH_LENGTH, &BytesRead, NULL) || (BytesRead != PATCH_LENGTH) )
  107.     {
  108.         ShowErrorMessage(TEXT("Read"), GetLastError());
  109.         return FALSE;
  110.     }
  111.     else
  112.     {
  113.         for (Index = 0; Index < PATCH_LENGTH; Index++)
  114.         if ( Buffer[Index] != PATCH_O[Index] )
  115.         {
  116.             MessageBox(NULL, TEXT("Wrong version or already patched!"), PATCH_CAPTION, MB_OK | MB_ICONERROR);
  117.             return FALSE;
  118.         }
  119.         return TRUE;
  120.     }
  121. }
  122.  
  123. // try to make an backup (filename + '.org')
  124. BOOL BackupFileToPatch(LPTSTR Filename)
  125. {
  126.     TCHAR NewFilename[MAX_PATH+4];
  127.  
  128.     lstrcpyn(NewFilename, Filename, MAX_PATH);
  129.     lstrcat(NewFilename, TEXT(".org"));
  130.     if ( !CopyFile(Filename, NewFilename, FALSE) )
  131.     {
  132.         ShowErrorMessage(TEXT("Backup"), GetLastError());
  133.         return FALSE;
  134.     }
  135.     return TRUE;
  136. }
  137.  
  138. // finally patch the file
  139. BOOL WriteFileToPatch(HANDLE Filehandle)
  140. {
  141.     DWORD BytesWritten;
  142.  
  143.     if ( !WriteFile(Filehandle, &PATCH_N, PATCH_LENGTH, &BytesWritten, NULL) || (BytesWritten != PATCH_LENGTH) )
  144.     {
  145.         ShowErrorMessage(TEXT("Write"), GetLastError());
  146.         return FALSE;
  147.     }
  148.     return TRUE;
  149. }
  150.  
  151. //////////////////////////////////////////////////////////////////////////////
  152.  
  153. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  154. {
  155.     UNREFERENCED_PARAMETER(hInstance);
  156.     UNREFERENCED_PARAMETER(hPrevInstance);
  157.     UNREFERENCED_PARAMETER(lpCmdLine);
  158.     UNREFERENCED_PARAMETER(nCmdShow);
  159.  
  160.     TCHAR Filename[MAX_PATH] = {0};
  161.     HANDLE Filehandle;
  162.  
  163.     if ( SelectFileToPatch(Filename, sizeof(Filename) / sizeof(TCHAR)) )
  164.     if ( OpenFileToPatch(Filename, Filehandle) )
  165.     {
  166.         if ( SeekFileToPatch(Filehandle) )
  167.         if ( VerifyFileToPatch(Filehandle) )
  168.         {
  169.             CloseFileToPatch(Filehandle);
  170.             if ( BackupFileToPatch(Filename) )
  171.             if ( OpenFileToPatch(Filename, Filehandle) )
  172.             if ( SeekFileToPatch(Filehandle) )
  173.             if ( WriteFileToPatch(Filehandle) )
  174.             {
  175.                 ShowErrorMessage(PATCH_CAPTION, ERROR_SUCCESS);
  176.             }
  177.         }
  178.         CloseFileToPatch(Filehandle);
  179.     }
  180.  
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement