Skip to content

Commit

Permalink
Version 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterTh committed Jun 8, 2015
1 parent 1cb2594 commit 73e3981
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 79 deletions.
11 changes: 11 additions & 0 deletions DATA/VERSIONS.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
19-01-2015 -- version 2.4
=========================
- The FPS override now scans the binary to try and find the correct addresses
to perform the patching required for variable FPS support (Nwks)


15-12-2014 -- version 2.3
=========================
- Make FPS override work with the Steamworks version (all credit goes to boowoo90 for the quick fix)


28-09-2013 -- version 2.2
=========================
- Updated AA, SSAO and screenshot functionality to be compatible with latest Dark Souls version
Expand Down
Binary file modified DSfix.v11.suo
Binary file not shown.
6 changes: 4 additions & 2 deletions DSfix.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<TargetMachine>MachineX86</TargetMachine>
<AdditionalLibraryDirectories>E:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Lib\x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<ModuleDefinitionFile>DINPUT8.def</ModuleDefinitionFile>
<AdditionalDependencies>lib\GFWLCompatibility.lib;libovr.lib;winmm.lib;detours.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>winmm.lib;detours.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<IgnoreSpecificDefaultLibraries>
</IgnoreSpecificDefaultLibraries>
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
Expand All @@ -130,7 +130,7 @@
<Culture>0x0409</Culture>
</ResourceCompile>
<PostBuildEvent>
<Command>cp $(TargetPath) "F:\Program Files (x86)\Steam\steamapps\common\Dark Souls Prepare to Die Edition\DATA"</Command>
<Command>cp $(TargetPath) "E:\Steam\steamapps\common\Dark Souls Prepare to Die Edition\DATA"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down Expand Up @@ -302,6 +302,7 @@
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">WIN32;NDEBUG;_WINDOWS;_MBCS;_USRDLL</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|x64'">WIN32;NDEBUG;_WINDOWS;_MBCS;_USRDLL</PreprocessorDefinitions>
</ClCompile>
<ClCompile Include="memory.cpp" />
<ClCompile Include="RenderstateManager.cpp" />
<ClCompile Include="SaveManager.cpp" />
<ClCompile Include="Settings.cpp" />
Expand All @@ -323,6 +324,7 @@
<ClInclude Include="FPS.h" />
<ClInclude Include="Hud.h" />
<ClInclude Include="KeyActions.h" />
<ClInclude Include="memory.h" />
<ClInclude Include="SearchTex.h" />
<ClInclude Include="SMAA.h" />
<ClInclude Include="Textures.def" />
Expand Down
165 changes: 90 additions & 75 deletions FPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,40 @@
#include "main.h"
#include "Detouring.h"
#include "RenderstateManager.h"
#include "memory.h"

#ifndef WITHOUT_GFWL_LIB
void enableGFWLCompatibility(void);

#define JMP32_SZ 5
#define CALL32_SZ 5
#define NOPOP 0x90
#define JMPOP 0xE9
#define CALLOP 0xE8
#endif

// Globals
static DWORD originalBase = NULL;
static DWORD imageBase = NULL;
static DWORD OriginalBase = 0x0400000;
static DWORD ImageBase = NULL;

// Hook Globals
double lastRenderTime;
static LARGE_INTEGER timerFreq;
static LARGE_INTEGER counterAtStart;

// Hook Addresses
//------------------------------------
// Hook Parameters
//-----------------------------------
// Dark Souls executable timestamp
DWORD EXE_TIMESTAMP = 0x546FA3C5; // Steam Beta build 2.0

// Time-step value address
#define ADDR_TS 0x01245E80 // 1.0.0 was 0x012497F0, 1.0.1 was 0x012498E0
DWORD ADDR_TS = 0x011E4D70; // 1.0.0 was 0x012497F0, 1.0.1 was 0x012498E0
LPCSTR TS_PATTERN = "0080264400009444000058420000C0428988083D0000A044";
DWORD TS_OFFSET = 0x00000010;

// Presentation interval address
#define ADDR_PRESINT 0x01025E5E // 1.0.0 was 0x010275AE, 1.0.1 was 0x0102788E
DWORD ADDR_PRESINT = 0x00FFA15E; // 1.0.0 was 0x010275AE, 1.0.1 was 0x0102788E
LPCSTR PRESINT_PATTERN = "FF15xxxxxxxx83C408C78648020000020000005EC20800";
DWORD PRESINT_OFFSET = 0x0000000F;

// getDrawThreadMsgCommand address in HGCommandDispatcher loop
#define ADDR_GETCMD 0x00BD5A8D // 1.0.0 was 0x00BD601D, 1.0.1 was 0x00BD60ED
DWORD ADDR_GETCMD = 0x00BAC3DD; // 1.0.0 was 0x00BD601D, 1.0.1 was 0x00BD60ED
LPCSTR GETCMD_PATTERN = "6A018BCDE8xxxxxxxx8BF08BCEE8xxxxxxxx83F805";
DWORD GETCMD_OFFSET = 0x0000000D;

//----------------------------------------------------------------------------------------
// Support Functions
Expand All @@ -43,25 +51,18 @@ static LARGE_INTEGER counterAtStart;
// Misc
//------------------------------------
DWORD getAbsoluteAddress(DWORD offset) {
if (imageBase)
return imageBase + offset;
if (ImageBase)
return ImageBase + offset;
else
return NULL;
}

DWORD convertAddress(DWORD Address) {
return getAbsoluteAddress(Address - originalBase);
return getAbsoluteAddress(Address - OriginalBase);
}

// Memory
//------------------------------------
void writeToAddress(void* Data, DWORD Address, int Size) {
DWORD oldProtect;
VirtualProtect((LPVOID)Address, Size, PAGE_READWRITE, &oldProtect);
memcpy((void*)Address, Data, Size);
VirtualProtect((LPVOID)Address, Size, oldProtect, &oldProtect);
}

void updateAnimationStepTime(float stepTime, float minFPS, float maxFPS) {
float FPS = 1.0f/(stepTime/1000);

Expand All @@ -84,49 +85,6 @@ double getElapsedTime(void) {
return (double)( (c.QuadPart - counterAtStart.QuadPart) * 1000.0 / (double)timerFreq.QuadPart );
}

// Detour
//------------------------------------
// Make sure to adjust length according to instructions below detoured address!
// Partially overwritten instructions will mess-up disassembly and capacity to debug
void *DetourApply(BYTE *orig, BYTE *hook, int len, BYTE type)
{
BYTE OP, SZ;

if (type == JMPOP) {
OP = JMPOP;
SZ = JMP32_SZ;
}
else if (type == CALLOP) {
OP = CALLOP;
SZ = CALL32_SZ;
}
else return 0;

DWORD dwProt = 0;
BYTE *jmp = (BYTE*)malloc(len+SZ);
VirtualProtect(orig, len, PAGE_READWRITE, &dwProt);
memcpy(jmp, orig, len);

jmp += len; // increment to the end of the copied bytes
jmp[0] = OP;
*(DWORD*)(jmp+1) = (DWORD)(orig+len - jmp) - SZ;

memset(orig, NOPOP, len);

orig[0] = OP;
*(DWORD*)(orig+1) = (DWORD)(hook - orig) - SZ;
VirtualProtect(orig, len, dwProt, 0);

return (jmp-len);
}

void DetourRemove(BYTE *src, BYTE *jmp, int len) {
DWORD dwProt = 0;
VirtualProtect(src, len, PAGE_READWRITE, &dwProt);
memcpy(src, jmp, len);
VirtualProtect(src, len, dwProt, 0);
}

//----------------------------------------------------------------------------------------
// Hook functions
//----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -162,15 +120,72 @@ __declspec(naked) void getDrawThreadMsgCommand(void) {
// Game Patches
//----------------------------------------------------------------------------------------
void applyFPSPatch() {
enableGFWLCompatibility();

// Get imageBase
HANDLE exeHandle = NULL;
originalBase = 0x0400000;
exeHandle = GetModuleHandle(NULL);

if(exeHandle != NULL)
imageBase = (DWORD)exeHandle;
SDLOG(0, "Starting FPS unlock...\n");
#ifndef WITHOUT_GFWL_LIB
SDLOG(0, "Applying GFWL compatibility\n");
enableGFWLCompatibility();
#endif

// Get image info
MODULEINFO moduleInfo;
PIMAGE_DOS_HEADER dosHeader;
PIMAGE_NT_HEADERS ntHeader;
IMAGE_FILE_HEADER header;

if(GetModuleInformation(GetCurrentProcess(), GetModuleHandle(NULL), &moduleInfo, sizeof(moduleInfo)))
{
ImageBase = (DWORD)moduleInfo.lpBaseOfDll;
SDLOG(0, "ImageBase at 0x%08X\n", ImageBase);

dosHeader = (PIMAGE_DOS_HEADER)ImageBase;
ntHeader = (PIMAGE_NT_HEADERS)((DWORD)(dosHeader) + (dosHeader->e_lfanew));
header = ntHeader->FileHeader;
DWORD TimeStamp = header.TimeDateStamp;
SDLOG(0, "Executable timestamp: 0x%08X, config: 0x%08X\n", TimeStamp, EXE_TIMESTAMP);

// Perform pattern matching if timestamp differs
if (TimeStamp != EXE_TIMESTAMP) {
SDLOG(0, "Trying pattern matching...\n");

DWORD address;
address = GetMemoryAddressFromPattern(NULL, TS_PATTERN, TS_OFFSET);
if(address != NULL) {
SDLOG(0, "ADDR_TS found at 0x%08X\n", address);
ADDR_TS = address;
}
else {
SDLOG(0, "Could not match ADDR_TS pattern, FPS not unlocked\n");
return;
}
address = GetMemoryAddressFromPattern(NULL, PRESINT_PATTERN, PRESINT_OFFSET);
if(address != NULL) {
SDLOG(0, "ADDR_PRESINT found at 0x%08X\n", address);
ADDR_PRESINT = address;
}
else {
SDLOG(0, "Could not match ADDR_PRESINT pattern, FPS not unlocked\n");
return;
}
address = GetMemoryAddressFromPattern(NULL, GETCMD_PATTERN, GETCMD_OFFSET);
if(address != NULL) {
SDLOG(0, "ADDR_GETCMD found at 0x%08X\n", address);
ADDR_GETCMD = address;
}
else {
SDLOG(0, "Could not match ADDR_GETCMD pattern, FPS not unlocked\n");
return;
}
SDLOG(0, "Pattern matching successful\n");
}
else
SDLOG(0, "Using configured addresses\n");
}
else
{
SDLOG(0, "GetModuleInformation failed, FPS not unlocked\n");
return;
}

// Init counter for frame-rate calculations
lastRenderTime = 0.0f;
Expand All @@ -191,5 +206,5 @@ void applyFPSPatch() {
address = convertAddress(ADDR_GETCMD);
DetourApply((BYTE*)address, (BYTE*)getDrawThreadMsgCommand, 5, CALLOP);

SDLOG(0, "FPS rate unlocked\n");
SDLOG(0, "FPS unlocked\n");
}
2 changes: 1 addition & 1 deletion RenderstateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ HRESULT RSManager::redirectPresent(CONST RECT *pSourceRect, CONST RECT *pDestRec
}
if(timingIntroMode) {
skippedPresents++;
if(skippedPresents >= 300u && !Settings::get().getUnlockFPS()) {
if(skippedPresents >= 1200u && !Settings::get().getUnlockFPS()) {
SDLOG(1, "Intro mode ended (timeout)!\n");
timingIntroMode = false;
}
Expand Down
4 changes: 3 additions & 1 deletion main.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

#pragma once

#define VERSION "2.2"
#define VERSION "2.4"

#define RELEASE_VER

#define WITHOUT_GFWL_LIB

#ifndef RELEASE_VER
#define SDLOG(_level, _str, ...) if(Settings::get().getLogLevel() > _level) { sdlog(_str, __VA_ARGS__); }
#else
Expand Down
Loading

0 comments on commit 73e3981

Please sign in to comment.