Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////
- //
- // FPS limit removal patch for GothicMod.exe 1.08k_mod
- //
- // UNSUPPORTED - USAGE AT YOUR OWN RISK
- //
- //////////////////////////////////////////////////////////////////////////////
- #include <windows.h>
- //////////////////////////////////////////////////////////////////////////////
- // constants for the patch
- const LPTSTR PATCH_CAPTION = TEXT("FPS patch");
- const unsigned long PATCH_OFFSET = 0x001CE592;
- const unsigned long PATCH_LENGTH = 0x00000007;
- const BYTE PATCH_O[PATCH_LENGTH] = {0x51, 0xFF, 0x15, 0xF0, 0x00, 0x7D, 0x00};
- const BYTE PATCH_N[PATCH_LENGTH] = {0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90};
- //////////////////////////////////////////////////////////////////////////////
- // show error/success messages
- void ShowErrorMessage(LPTSTR Func, DWORD Code)
- {
- TCHAR Message[1024];
- LPTSTR ErrorMsg;
- LPTSTR Caption;
- DWORD Flags;
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, Code, 0, LPTSTR(&ErrorMsg), 0, NULL);
- wsprintf(Message, TEXT("%s: %s"), Func, ErrorMsg);
- if (Code == ERROR_SUCCESS)
- {
- Caption = PATCH_CAPTION;
- Flags = MB_OK | MB_ICONINFORMATION;
- }
- else
- {
- Caption = NULL;
- Flags = MB_OK | MB_ICONERROR;
- }
- MessageBox(NULL, Message, Caption, Flags);
- if (ErrorMsg) LocalFree((HLOCAL)ErrorMsg);
- }
- // open GUI dialog box to let the user choose the file
- BOOL SelectFileToPatch(LPTSTR Filename, DWORD MaxFilename)
- {
- OPENFILENAME ofn;
- ZeroMemory(&ofn, sizeof(OPENFILENAME));
- ofn.lStructSize = sizeof(OPENFILENAME);
- ofn.lpstrFilter = TEXT("GothicMod.exe\0GothicMod.exe\0");
- ofn.nFilterIndex = 1;
- ofn.lpstrFile = Filename;
- ofn.nMaxFile = MaxFilename;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST |
- OFN_NOREADONLYRETURN | OFN_NOTESTFILECREATE |
- OFN_HIDEREADONLY | OFN_ENABLESIZING;
- return GetOpenFileName(&ofn);
- }
- // get file handle with read/write access
- BOOL OpenFileToPatch(LPTSTR Filename, HANDLE &Filehandle)
- {
- Filehandle = CreateFile(Filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
- if ( Filehandle == INVALID_HANDLE_VALUE )
- {
- ShowErrorMessage(TEXT("Open"), GetLastError());
- return FALSE;
- }
- return TRUE;
- }
- // close file handle
- BOOL CloseFileToPatch(HANDLE &Filehandle)
- {
- if ( (Filehandle != NULL) && (Filehandle != INVALID_HANDLE_VALUE) )
- CloseHandle(Filehandle);
- Filehandle = INVALID_HANDLE_VALUE;
- return TRUE;
- }
- // seek to the file offset where the patch starts
- BOOL SeekFileToPatch(HANDLE Filehandle)
- {
- if ( SetFilePointer(Filehandle, PATCH_OFFSET, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER )
- {
- ShowErrorMessage(TEXT("Seek"), GetLastError());
- return FALSE;
- }
- return TRUE;
- }
- // verify if the source pattern matches the original which we want to patch
- BOOL VerifyFileToPatch(HANDLE Filehandle)
- {
- BYTE Buffer[PATCH_LENGTH];
- DWORD BytesRead;
- int Index;
- if ( !ReadFile(Filehandle, &Buffer, PATCH_LENGTH, &BytesRead, NULL) || (BytesRead != PATCH_LENGTH) )
- {
- ShowErrorMessage(TEXT("Read"), GetLastError());
- return FALSE;
- }
- else
- {
- for (Index = 0; Index < PATCH_LENGTH; Index++)
- if ( Buffer[Index] != PATCH_O[Index] )
- {
- MessageBox(NULL, TEXT("Wrong version or already patched!"), PATCH_CAPTION, MB_OK | MB_ICONERROR);
- return FALSE;
- }
- return TRUE;
- }
- }
- // try to make an backup (filename + '.org')
- BOOL BackupFileToPatch(LPTSTR Filename)
- {
- TCHAR NewFilename[MAX_PATH+4];
- lstrcpyn(NewFilename, Filename, MAX_PATH);
- lstrcat(NewFilename, TEXT(".org"));
- if ( !CopyFile(Filename, NewFilename, FALSE) )
- {
- ShowErrorMessage(TEXT("Backup"), GetLastError());
- return FALSE;
- }
- return TRUE;
- }
- // finally patch the file
- BOOL WriteFileToPatch(HANDLE Filehandle)
- {
- DWORD BytesWritten;
- if ( !WriteFile(Filehandle, &PATCH_N, PATCH_LENGTH, &BytesWritten, NULL) || (BytesWritten != PATCH_LENGTH) )
- {
- ShowErrorMessage(TEXT("Write"), GetLastError());
- return FALSE;
- }
- return TRUE;
- }
- //////////////////////////////////////////////////////////////////////////////
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hInstance);
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- UNREFERENCED_PARAMETER(nCmdShow);
- TCHAR Filename[MAX_PATH] = {0};
- HANDLE Filehandle;
- if ( SelectFileToPatch(Filename, sizeof(Filename) / sizeof(TCHAR)) )
- if ( OpenFileToPatch(Filename, Filehandle) )
- {
- if ( SeekFileToPatch(Filehandle) )
- if ( VerifyFileToPatch(Filehandle) )
- {
- CloseFileToPatch(Filehandle);
- if ( BackupFileToPatch(Filename) )
- if ( OpenFileToPatch(Filename, Filehandle) )
- if ( SeekFileToPatch(Filehandle) )
- if ( WriteFileToPatch(Filehandle) )
- {
- ShowErrorMessage(PATCH_CAPTION, ERROR_SUCCESS);
- }
- }
- CloseFileToPatch(Filehandle);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement