forked from PeterTh/dsfix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPS.cpp
More file actions
314 lines (268 loc) · 7.03 KB
/
FPS.cpp
File metadata and controls
314 lines (268 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Dark Souls FPS fix by Clement Barnier (Nwks)
///////////////////////////////////////////////
#include "FPS.h"
#include <windows.h>
#include "Settings.h"
#include "main.h"
#include "Detouring.h"
void enableGFWLCompatibility(void);
#define JMP32_SZ 5
#define CALL32_SZ 5
#define NOPOP 0x90
#define JMPOP 0xE9
#define CALLOP 0xE8
// Globals
static DWORD originalBase = NULL;
static DWORD imageBase = NULL;
// Hook Globals
static DWORD HighGraphics;
static DWORD TaskFuncPtr;
static LARGE_INTEGER timerFreq;
static LARGE_INTEGER counterAtStart;
//----------------------------------------------------------------------------------------
//Functions
//----------------------------------------------------------------------------------------
// Misc
//------------------------------------
DWORD getAbsoluteAddress(DWORD offset) {
if (imageBase)
return imageBase + offset;
else
return NULL;
}
DWORD convertAddress(DWORD Address) {
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);
if (FPS < minFPS)
stepTime = minFPS;
else if (stepTime > maxFPS)
FPS = maxFPS;
float cappedStep = 1/(float)FPS;
DWORD data = *(DWORD*)&cappedStep;
writeToAddress(&data, convertAddress(0x012497F0), sizeof(data));
}
// Timer
float getElapsedTime(void) {
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return (float)( (c.QuadPart - counterAtStart.QuadPart) * 1000.0f / (float)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, int type) {
BYTE OP, SZ;
if (type == 0) {
OP = JMPOP;
SZ = JMP32_SZ;
}
else if(type == 1) {
OP = CALLOP;
SZ = CALL32_SZ;
}
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);
}
//----------------------------------------------------------------------------------------
//Game hooks
//----------------------------------------------------------------------------------------
// Render Loop
//----------------------------------------------------------------------------------------
void renderLoop(void) {
bool run = true;
DWORD threadID = GetCurrentThreadId();
int taskFunc;
DWORD task;
//Pointers conversion
DWORD pGetTaskF = convertAddress(0x00577330);
DWORD pCleanTaskF = convertAddress(0x00577450);
QueryPerformanceFrequency(&timerFreq);
QueryPerformanceCounter(&counterAtStart);
float lastTime = 0.0f;
// Render loop
do {
// Get Job & state
_asm {
PUSH 1
MOV ECX, HighGraphics
CALL pGetTaskF //Get task pointer
MOV task, EAX //Store task pointer (EAX)
MOV ECX, [EAX+0x0C] //Get task function
MOV taskFunc, ECX //Store function
}
if (task == (HighGraphics+0x08)) //No task
break;
switch (taskFunc) {
// Exit loop
case 0:
_asm {
MOV EDI, TaskFuncPtr
MOV EAX, [EDI]
MOV EDX, [EAX+0x0C]
MOV ECX, EDI
CALL EDX
}
run = false;
break;
// Start Watch-dog Thread
case 1:
_asm {
MOV ESI, task
MOV EDI, TaskFuncPtr
MOV ECX, [ESI+0x2C]
MOV EDX, [ESI+0x28]
MOV EAX, [EDI]
PUSH ECX
MOV ECX, [ESI+0x24]
PUSH EDX
MOV EDX, [EAX+0x08]
PUSH ECX
MOV ECX, EDI
CALL EDX
}
break;
// Update and/or Render
case 2:
_asm {
MOV ESI, task
MOV EDI, TaskFuncPtr
MOV ECX, [ESI+0x24]
MOV EAX, [EDI]
MOV EDX, [EAX+0x10]
PUSH ECX
MOV ECX, EDI
CALL EDX
}
break;
// Do nothing (was debug log)
case 3:
_asm {
MOV EDI, TaskFuncPtr
MOV EAX, [EDI]
MOV EDX, [EAX+0x14]
MOV ECX, TaskFuncPtr
CALL EDX
}
break;
// Do nothing (was debug log)
case 4:
_asm {
MOV EDI, TaskFuncPtr
MOV EAX, [EDI]
MOV EDX, [EAX+0x18]
MOV ECX, TaskFuncPtr
CALL EDX
}
break;
// Force Render (caused by watchdog timeout)
case 5:
_asm {
MOV EDI, TaskFuncPtr
MOV EAX, [EDI]
MOV EDX, [EAX+0x1C]
MOV ECX, TaskFuncPtr
CALL EDX
}
break;
default:
break;
}
// If rendering was performed, update animation step-time
if((taskFunc == 2) || (taskFunc == 5)) {
// FPS regulation
float maxFPS = (float)Settings::get().getCurrentFPSLimit();
float minFPS = 10.0f;
float currentTime = getElapsedTime();
float deltaTime = currentTime - lastTime;
// Update step-time
updateAnimationStepTime((float)deltaTime, minFPS, maxFPS);
lastTime = currentTime;
}
// Task cleanup
_asm {
MOV ESI, task
PUSH ESI
MOV ECX, HighGraphics
CALL pCleanTaskF
}
} while (run);
}
// Render Entry
//----------------------------------------------------------------------------------------
__declspec(naked) void renderLoopEntry(void) {
#define LOCALOFFSET __LOCAL_SIZE
// Prologue
_asm {
// Create Stack frame
PUSH EBX
PUSH EBP
MOV EBP, ESP
SUB ESP, LOCALOFFSET
// Retrieve stack parameters
MOV EBX, [EBP+0x0C]
PUSH ESI
PUSH EDI
// Store parameters to globals
MOV HighGraphics, EBX //HighGraphics: EBP
MOV TaskFuncPtr, ECX //Pointer to functions structure: EDI
}
// Start Recorder Process
renderLoop();
// Epilogue
__asm {
POP EDI
POP ESI
MOV ESP, EBP
POP EBP
POP EBX
RETN 4
}
}
void applyFPSPatch() {
enableGFWLCompatibility();
// Get imageBase
HANDLE exeHandle = NULL;
originalBase = 0x0400000;
exeHandle = GetModuleHandle(NULL);
if(exeHandle != NULL)
imageBase = (DWORD)exeHandle;
// Patches
//--------------------------------------------------------------
DWORD address;
DWORD data;
// Override D3D Presentation Interval
address = convertAddress(0x010275AE);
data = 5; //Set to immediate
writeToAddress(&data, address, sizeof(data));
// Detour Render loop entry
address = convertAddress(0x00BD6000);
DetourApply((BYTE*)address, (BYTE*)renderLoopEntry, 6, 0);
SDLOG(0, "FPS rate unlocked\n");
}