forked from Kaldaien/FAR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nier.cpp
2723 lines (2120 loc) · 86.2 KB
/
nier.cpp
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2017 Andon "Kaldaien" Coleman,
// Niklas "DrDaxxy" Kielblock,
// Peter "Durante" Thoman
//
// Francesco149, Idk31, Smithfield, and GitHub contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#define _CRT_SECURE_NO_WARNINGS
#include <SpecialK/dxgi_backend.h>
#include <SpecialK/config.h>
#include <SpecialK/command.h>
#include <SpecialK/framerate.h>
#include <SpecialK/ini.h>
#include <SpecialK/parameter.h>
#include <SpecialK/utility.h>
#include <SpecialK/log.h>
#include <SpecialK/steam_api.h>
#include <SpecialK/input/input.h>
#include <SpecialK/input/xinput.h>
#include <SpecialK/hooks.h>
#include <SpecialK/core.h>
#include <process.h>
#include <imgui/imgui.h>
#include <imgui/backends/imgui_d3d11.h>
#include <atlbase.h>
#define FAR_VERSION_NUM L"0.6.2.4"
#define FAR_VERSION_STR L"FAR v " FAR_VERSION_NUM
// Block until update finishes, otherwise the update dialog
// will be dismissed as the game crashes when it tries to
// draw the first frame.
volatile LONG __FAR_init = FALSE;
float __FAR_MINIMUM_EXT = 0.0f;
#define WORKING_FPS_UNCAP
#define WORKING_CAMERA_CONTROLS
#define WORKING_GAMESTATES
#define WORKING_FPS_SLEEP_FIX
struct far_game_state_s {
// Game state addresses courtesy of Francesco149
DWORD* pMenu = (DWORD *)0x14190D494;//0x1418F39C4;
DWORD* pLoading = (DWORD *)0x14198F0A0;//0x141975520;
DWORD* pHacking = (DWORD *)0x1410FA090;//0x1410E0AB4;
DWORD* pShortcuts = (DWORD *)0x141415AC4;//0x1413FC35C;
float* pHUDOpacity = (float *)0x1419861BC;//0x14196C63C;
bool capped = true; // Actual state of limiter
bool enforce_cap = true; // User's current preference
bool patchable = false; // True only if the memory addresses can be validated
bool needFPSCap (void) {
return enforce_cap|| ( *pMenu != 0) || (*pLoading != 0) ||
(*pHacking != 0) || (*pShortcuts != 0);
}
void capFPS (void);
void uncapFPS (void);
} static game_state;
sk::ParameterFactory far_factory;
iSK_INI* far_prefs = nullptr;
wchar_t far_prefs_file [MAX_PATH] = { L'\0' };
sk::ParameterInt* far_gi_workgroups = nullptr;
sk::ParameterFloat* far_gi_min_light_extent = nullptr;
sk::ParameterInt* far_bloom_width = nullptr;
sk::ParameterBool* far_bloom_disable = nullptr;
sk::ParameterBool* far_fix_motion_blur = nullptr;
sk::ParameterInt* far_bloom_skip = nullptr;
sk::ParameterInt* far_ao_width = nullptr;
sk::ParameterInt* far_ao_height = nullptr;
sk::ParameterBool* far_ao_disable = nullptr;
sk::ParameterBool* far_limiter_busy = nullptr;
sk::ParameterBool* far_uncap_fps = nullptr;
sk::ParameterBool* far_slow_state_cache = nullptr;
sk::ParameterBool* far_rtss_warned = nullptr;
sk::ParameterBool* far_osd_disclaimer = nullptr;
sk::ParameterBool* far_accepted_license = nullptr;
sk::ParameterStringW* far_hudless_binding = nullptr;
sk::ParameterStringW* far_center_lock = nullptr;
sk::ParameterStringW* far_focus_lock = nullptr;
sk::ParameterStringW* far_free_look = nullptr;
struct {
bool enqueue = false; // Trigger a Steam screenshot
int clear = 4; // Reset enqueue after 3 frames
float opacity = 1.0f; // Original HUD opacity
SK_Keybind keybind = {
"HUD Free Screenshot", L"Num -",
false, false, false, VK_OEM_MINUS
};
} static __FAR_HUDLESS;
typedef float vec3_t [3]; // X,Z,Y
struct far_cam_state_s {
SK_Keybind freelook_binding = {
"Toggle Camera Freelook Mode", L"Num 5",
false, false, false, VK_NUMPAD5
};
// Memory addresses courtesy of Idk31 and Smithfield
// Ptr at { F3 44 0F 11 88 74 02 00 00 89 88 84 02 00 00 } + 4
vec3_t* pCamera = (vec3_t *)0x141605400;//0x1415EB950;
vec3_t* pLook = (vec3_t *)0x141605410;//0x1415EB960;
float* pRoll = (float *) 0x141415B90;//1415EB990;
vec3_t fwd, right, up;
bool center_lock = false,
focus_lock = false;
SK_Keybind center_binding {
"Camera Center Lock Toggle", L"Num /",
true, true, false, VK_DIVIDE
};
SK_Keybind focus_binding {
"Camera Focus Lock Toggle", L"Ctrl+Shift+F11",
true, true, false, VK_F11
};
bool toggleCenterLock (void) {
// { 0x0F, 0xC6, 0xC0, 0x00, 0x0F, 0x5C, 0xF1, 0x0F, 0x59, 0xF0, 0x0F, 0x58, 0xCE } -0x7 = Center Lock
if (center_lock) SK_GetCommandProcessor ()->ProcessCommandLine ("mem l 4d5729 0F0112FCD00D290F");
else SK_GetCommandProcessor ()->ProcessCommandLine ("mem l 4d5729 0F90909090909090");
//4cdc89
return (center_lock = (! center_lock));
}
bool toggleFocusLock (void) {
// Center Lock - C1
if (focus_lock) SK_GetCommandProcessor ()->ProcessCommandLine ("mem l 4D5668 850112FDA10D290F");
else SK_GetCommandProcessor ()->ProcessCommandLine ("mem l 4D5668 8590909090909090");
return (focus_lock = (! focus_lock));
}
} static far_cam;
bool __FAR_Freelook = false;
typedef void (__stdcall *SK_PlugIn_ControlPanelWidget_pfn)(void);
void __stdcall SK_FAR_ControlPanel (void);
static SK_PlugIn_ControlPanelWidget_pfn SK_PlugIn_ControlPanelWidget_Original = nullptr;
// (Presumable) Size of compute shader workgroup
int __FAR_GlobalIllumWorkGroupSize = 128;
bool __FAR_GlobalIllumCompatMode = true;
struct {
int width = -1; // Set at startup from user prefs, never changed
bool disable = false;
int skip = 0;
bool active = false;
} far_bloom;
struct {
int width = -1; // Set at startup from user prefs, never changed
int height = -1; // Set at startup from user prefs, never changed
bool active = false;
bool disable = false;
bool fix_motion_blur = true;
} far_ao;
double __FAR_TargetFPS = 59.94;
extern void
__stdcall
SK_SetPluginName (std::wstring name);
typedef HRESULT (WINAPI *D3D11Dev_CreateBuffer_pfn)(
_In_ ID3D11Device *This,
_In_ const D3D11_BUFFER_DESC *pDesc,
_In_opt_ const D3D11_SUBRESOURCE_DATA *pInitialData,
_Out_opt_ ID3D11Buffer **ppBuffer
);
typedef HRESULT (WINAPI *D3D11Dev_CreateShaderResourceView_pfn)(
_In_ ID3D11Device *This,
_In_ ID3D11Resource *pResource,
_In_opt_ const D3D11_SHADER_RESOURCE_VIEW_DESC *pDesc,
_Out_opt_ ID3D11ShaderResourceView **ppSRView
);
static D3D11Dev_CreateBuffer_pfn D3D11Dev_CreateBuffer_Original;
static D3D11Dev_CreateShaderResourceView_pfn D3D11Dev_CreateShaderResourceView_Original;
extern
HRESULT
WINAPI
D3D11Dev_CreateBuffer_Override (
_In_ ID3D11Device *This,
_In_ const D3D11_BUFFER_DESC *pDesc,
_In_opt_ const D3D11_SUBRESOURCE_DATA *pInitialData,
_Out_opt_ ID3D11Buffer **ppBuffer );
extern
HRESULT
WINAPI
D3D11Dev_CreateShaderResourceView_Override (
_In_ ID3D11Device *This,
_In_ ID3D11Resource *pResource,
_In_opt_ const D3D11_SHADER_RESOURCE_VIEW_DESC *pDesc,
_Out_opt_ ID3D11ShaderResourceView **ppSRView );
extern
void
STDMETHODCALLTYPE
D3D11_PSSetConstantBuffers_Override (
_In_ ID3D11DeviceContext* This,
_In_ UINT StartSlot,
_In_ UINT NumBuffers,
_In_opt_ ID3D11Buffer *const *ppConstantBuffers );
extern
void
WINAPI
D3D11_DrawIndexedInstanced_Override (
_In_ ID3D11DeviceContext *This,
_In_ UINT IndexCountPerInstance,
_In_ UINT InstanceCount,
_In_ UINT StartIndexLocation,
_In_ INT BaseVertexLocation,
_In_ UINT StartInstanceLocation );
extern
void
WINAPI
D3D11_DrawIndexedInstancedIndirect_Override (
_In_ ID3D11DeviceContext *This,
_In_ ID3D11Buffer *pBufferForArgs,
_In_ UINT AlignedByteOffsetForArgs );
extern
void
WINAPI
D3D11_DrawInstanced_Override (
_In_ ID3D11DeviceContext *This,
_In_ UINT VertexCountPerInstance,
_In_ UINT InstanceCount,
_In_ UINT StartVertexLocation,
_In_ UINT StartInstanceLocation );
extern
void
WINAPI
D3D11_DrawInstancedIndirect_Override (
_In_ ID3D11DeviceContext *This,
_In_ ID3D11Buffer *pBufferForArgs,
_In_ UINT AlignedByteOffsetForArgs );
extern
void
STDMETHODCALLTYPE
D3D11_UpdateSubresource_Override (
_In_ ID3D11DeviceContext* This,
_In_ ID3D11Resource *pDstResource,
_In_ UINT DstSubresource,
_In_opt_ const D3D11_BOX *pDstBox,
_In_ const void *pSrcData,
_In_ UINT SrcRowPitch,
_In_ UINT SrcDepthPitch);
// Was threaded originally, but it is important to block until
// the update check completes.
unsigned int
__stdcall
SK_FAR_CheckVersion (LPVOID user)
{
UNREFERENCED_PARAMETER (user);
extern bool
__stdcall
SK_FetchVersionInfo (const wchar_t* wszProduct);
extern HRESULT
__stdcall
SK_UpdateSoftware (const wchar_t* wszProduct);
if (SK_FetchVersionInfo (L"FAR"))
SK_UpdateSoftware (L"FAR");
return 0;
}
#include <../depends/include/glm/glm.hpp>
struct far_scatter_param_s
{
union
{
struct
{
float Near_Far_Stepnum [4][4];
float LightColor [4][4];
float LightDirView_Range15 [4];
float ScatterParam_Range7 [4];
};
float data [40];
};
far_scatter_param_s (LPCVOID pData)
{
if (pData != nullptr)
memcpy (&data [0], pData, sizeof far_scatter_param_s);
else
memset (&data [0], 0, sizeof far_scatter_param_s);
}
};
std::map <ID3D11Buffer*, far_scatter_param_s> scatter_buffers;
std::map <ID3D11Buffer*, far_scatter_param_s> scatter_buffers_ovr;
HRESULT
WINAPI
SK_FAR_CreateBuffer (
_In_ ID3D11Device *This,
_In_ const D3D11_BUFFER_DESC *pDesc,
_In_opt_ const D3D11_SUBRESOURCE_DATA *pInitialData,
_Out_opt_ ID3D11Buffer **ppBuffer )
{
struct far_light_volume_s {
float world_pos [ 4];
float world_to_vol [16];
float half_extents [ 4];
};
// Global Illumination (DrDaxxy)
if ( pDesc != nullptr && pDesc->StructureByteStride == sizeof (far_light_volume_s) &&
pDesc->ByteWidth == sizeof (far_light_volume_s) * 128 &&
pDesc->BindFlags & D3D11_BIND_SHADER_RESOURCE )
{
D3D11_BUFFER_DESC new_desc = *pDesc;
new_desc.ByteWidth = sizeof (far_light_volume_s) * __FAR_GlobalIllumWorkGroupSize;
// New Stuff for 0.6.0
// -------------------
//
// >> Project small lights to infinity and leave large lights lit <<
//
if (pInitialData != nullptr && pInitialData->pSysMem != nullptr)
{
far_light_volume_s* lights =
(far_light_volume_s *)pInitialData->pSysMem;
static far_light_volume_s new_lights [128];
memcpy ( new_lights,
lights,
sizeof (far_light_volume_s) * 128 );
// This code is bloody ugly, but it works ;)
for (int i = 0; i < __FAR_GlobalIllumWorkGroupSize; i++)
{
float light_pos [4] = { lights [i].world_pos [0], lights [i].world_pos [1],
lights [i].world_pos [2], lights [i].world_pos [3] };
glm::vec4 cam_pos_world ( light_pos [0] - ((float *)far_cam.pCamera) [0],
light_pos [1] - ((float *)far_cam.pCamera) [1],
light_pos [2] - ((float *)far_cam.pCamera) [2],
1.0f );
glm::mat4x4 world_mat ( lights [i].world_to_vol [ 0], lights [i].world_to_vol [ 1], lights [i].world_to_vol [ 2], lights [i].world_to_vol [ 3],
lights [i].world_to_vol [ 4], lights [i].world_to_vol [ 5], lights [i].world_to_vol [ 6], lights [i].world_to_vol [ 7],
lights [i].world_to_vol [ 8], lights [i].world_to_vol [ 9], lights [i].world_to_vol [10], lights [i].world_to_vol [11],
lights [i].world_to_vol [12], lights [i].world_to_vol [13], lights [i].world_to_vol [14], lights [i].world_to_vol [15] );
glm::vec4 test = world_mat * cam_pos_world;
if ( ( fabs (lights [i].half_extents [0]) <= fabs (test.x) * __FAR_MINIMUM_EXT ||
fabs (lights [i].half_extents [1]) <= fabs (test.y) * __FAR_MINIMUM_EXT ||
fabs (lights [i].half_extents [2]) <= fabs (test.z) * __FAR_MINIMUM_EXT ) /* && ( fabs (lights [i].half_extents [0]) > 0.0001f ||
fabs (lights [i].half_extents [1]) > 0.0001f ||
fabs (lights [i].half_extents [2]) > 0.0001f ) */ )
{
// Degenerate light volume
new_lights [i].half_extents [0] = 0.0f;
new_lights [i].half_extents [1] = 0.0f;
new_lights [i].half_extents [2] = 0.0f;
// Project to infinity (but not beyond, because that makes no sense)
new_lights [i].world_pos [0] = 0.0f; new_lights [i].world_pos [1] = 0.0f;
new_lights [i].world_pos [2] = 0.0f; new_lights [i].world_pos [3] = 0.0f;
}
}
D3D11_SUBRESOURCE_DATA new_data = *pInitialData;
new_data.pSysMem = (void *)new_lights;
return D3D11Dev_CreateBuffer_Original (This, &new_desc, &new_data, ppBuffer);
}
return D3D11Dev_CreateBuffer_Original (This, &new_desc, pInitialData, ppBuffer);
}
HRESULT hr =
D3D11Dev_CreateBuffer_Original (This, pDesc, pInitialData, ppBuffer);
if (SUCCEEDED (hr) && ppBuffer != nullptr)
{
if ( pDesc != nullptr && pDesc->ByteWidth == sizeof (far_scatter_param_s) &&
pDesc->BindFlags & D3D11_BIND_CONSTANT_BUFFER &&
pInitialData )
{
scatter_buffers.emplace (std::make_pair (*ppBuffer, far_scatter_param_s (pInitialData->pSysMem)));
scatter_buffers_ovr.emplace (std::make_pair (*ppBuffer, far_scatter_param_s (pInitialData->pSysMem)));
}
}
return hr;
}
HRESULT
WINAPI
SK_FAR_CreateShaderResourceView (
_In_ ID3D11Device *This,
_In_ ID3D11Resource *pResource,
_In_opt_ const D3D11_SHADER_RESOURCE_VIEW_DESC *pDesc,
_Out_opt_ ID3D11ShaderResourceView **ppSRView )
{
// Global Illumination (DrDaxxy)
if ( pDesc != nullptr && pDesc->ViewDimension == D3D_SRV_DIMENSION_BUFFEREX &&
pDesc->BufferEx.NumElements == 128 )
{
if (! __FAR_GlobalIllumCompatMode)
return D3D11Dev_CreateShaderResourceView_Original (This, pResource, pDesc, ppSRView);
CComPtr <ID3D11Buffer> pBuf;
if ( SUCCEEDED (
pResource->QueryInterface <ID3D11Buffer> (&pBuf)
)
)
{
D3D11_SHADER_RESOURCE_VIEW_DESC new_desc = *pDesc;
D3D11_BUFFER_DESC buf_desc;
pBuf->GetDesc (&buf_desc);
if (buf_desc.ByteWidth == 96 * __FAR_GlobalIllumWorkGroupSize)
new_desc.BufferEx.NumElements = __FAR_GlobalIllumWorkGroupSize;
return D3D11Dev_CreateShaderResourceView_Original (This, pResource, &new_desc, ppSRView);
}
}
HRESULT hr =
D3D11Dev_CreateShaderResourceView_Original (This, pResource, pDesc, ppSRView);
return hr;
}
void
STDMETHODCALLTYPE
SK_FAR_PSSetConstantBuffers (
_In_ ID3D11DeviceContext *This,
_In_ UINT StartSlot,
_In_ UINT NumBuffers,
_In_opt_ ID3D11Buffer *const *ppConstantBuffers )
{
if (ppConstantBuffers)
{
for (UINT i = 0; i < NumBuffers; i++)
{
if (StartSlot + i == 8)
{
if (scatter_buffers.count (ppConstantBuffers [i]))
{
dll_log.Log (L"NEAT");
}
}
}
}
D3D11_PSSetConstantBuffers_Original (This, StartSlot, NumBuffers, ppConstantBuffers );
}
enum class SK_FAR_WaitBehavior
{
Sleep = 0x1,
Busy = 0x2
};
SK_FAR_WaitBehavior wait_behavior (SK_FAR_WaitBehavior::Sleep);
extern LPVOID __SK_base_img_addr;
extern LPVOID __SK_end_img_addr;
extern void* __stdcall SK_Scan (const uint8_t* pattern, size_t len, const uint8_t* mask);
typedef _Return_type_success_(return >= 0) LONG NTSTATUS;
typedef NTSTATUS (NTAPI *NtQueryTimerResolution_pfn)
(
OUT PULONG MinimumResolution,
OUT PULONG MaximumResolution,
OUT PULONG CurrentResolution
);
typedef NTSTATUS (NTAPI *NtSetTimerResolution_pfn)
(
IN ULONG DesiredResolution,
IN BOOLEAN SetResolution,
OUT PULONG CurrentResolution
);
HMODULE NtDll = 0;
NtQueryTimerResolution_pfn NtQueryTimerResolution = nullptr;
NtSetTimerResolution_pfn NtSetTimerResolution = nullptr;
void
SK_FAR_SetFramerateCap (bool enable)
{
if (enable)
{
game_state.enforce_cap = false;
far_uncap_fps->set_value (true);
} else {
far_uncap_fps->set_value (false);
game_state.enforce_cap = true;
}
}
// Altimor's FPS cap removal
//
uint8_t* psleep = (uint8_t *)0x14092E887; // Original pre-patch
uint8_t* pspinlock = (uint8_t *)0x14092E8CF; // +0x48
uint8_t* pmin_tstep = (uint8_t *)0x140805DEC; // Original pre-patch
uint8_t* pmax_tstep = (uint8_t *)0x140805E18; // +0x2c
bool
SK_FAR_SetLimiterWait (SK_FAR_WaitBehavior behavior)
{
static uint8_t sleep_wait [] = { 0x7e, 0x08, 0x8b, 0xca, 0xff, 0x15 };
static uint8_t busy_wait [] = { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 };
static bool init = false;
// Square-Enix rarely patches the games they publish, so just search for this pattern and
// don't bother to adjust memory addresses... if it's not found using the hard-coded address,
// bail-out.
if (! init)
{
init = true;
if ( (psleep = (uint8_t *)SK_Scan ( sleep_wait, 6, nullptr )) == nullptr )
{
dll_log.Log (L"[ FARLimit ] Could not locate Framerate Limiter Sleep Addr.");
}
else {
psleep += 4;
dll_log.Log (L"[ FARLimit ] Scanned Framerate Limiter Sleep Addr.: 0x%p", psleep);
memcpy (sleep_wait, psleep, 6);
pspinlock = psleep + 0x48;
uint8_t tstep0 [] = { 0x73, 0x1C, 0xC7, 0x05, 0x00, 0x00 };
uint8_t tstep0_mask [] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00 };
pmin_tstep = (uint8_t *)SK_Scan ( tstep0, sizeof tstep0, tstep0_mask );
pmax_tstep = pmin_tstep + 0x2c;
dll_log.Log (L"[ FARLimit ] Scanned Framerate Limiter TStepMin Addr.: 0x%p", pmin_tstep);
//{ 0xF3, 0x0F, 0x11, 0x44, 0x24, 0x20, 0xF3, 0x0F, 0x11, 0x4C, 0x24, 0x24, 0xF3, 0x0F, 0x11, 0x54, 0x24, 0x28, 0xF3, 0x0F, 0x11, 0x5C, 0x24, 0x2C } (-4) = HUD Opacity
}
}
if (psleep == nullptr)
return false;
wait_behavior = behavior;
DWORD dwProtect;
VirtualProtect (psleep, 6, PAGE_EXECUTE_READWRITE, &dwProtect);
// Hard coded for now; safe to do this without pausing threads and flushing caches
// because the config UI runs from the only thread that the game framerate limits.
switch (behavior)
{
case SK_FAR_WaitBehavior::Busy:
memcpy (psleep, busy_wait, 6);
break;
case SK_FAR_WaitBehavior::Sleep:
memcpy (psleep, sleep_wait, 6);
break;
}
VirtualProtect (psleep, 6, dwProtect, &dwProtect);
return true;
}
extern void
STDMETHODCALLTYPE
SK_BeginBufferSwap (void);
extern BOOL
__stdcall
SK_DrawExternalOSD (std::string app_name, std::string text);
typedef void (STDMETHODCALLTYPE *SK_EndFrame_pfn)(void);
static SK_EndFrame_pfn SK_EndFrame_Original = nullptr;
void
STDMETHODCALLTYPE
SK_FAR_EndFrame (void)
{
static LONGLONG frames_drawn = 0;
SK_EndFrame_Original ();
if (far_osd_disclaimer->get_value ())
SK_DrawExternalOSD ( "FAR", " Press Ctrl + Shift + O to toggle In-Game OSD\n"
" Press Ctrl + Shift + Backspace to access In-Game Config Menu\n"
" ( Select + Start on Gamepads )\n\n"
" * This message will go away the first time you actually read it and successfully toggle the OSD.\n" );
else if (config.system.log_level == 1)
{
std::string validation = "";
if (game_state.needFPSCap () && frames_drawn >= 0)
{
validation += "FRAME: ";
static char szFrameNum [32] = { '\0' };
snprintf (szFrameNum, 31, "%lli (%c) ", frames_drawn, 'A' + (int)(frames_drawn++ % 26LL) );
validation += szFrameNum;
}
else //if ((! game_state.needFPSCap ()) || frames_drawn < 0)
{
// First offense is last offense
frames_drawn = -1;
validation += "*** CHEATER ***";
}
SK_DrawExternalOSD ( "FAR", validation );
}
else if (config.system.log_level > 1)
{
std::string state = "";
if (game_state.needFPSCap ()) {
state += "< Needs Cap :";
std::string reasons = "";
if (*game_state.pLoading) reasons += " loading ";
if (*game_state.pMenu) reasons += " menu ";
if (*game_state.pHacking) reasons += " hacking ";
if (*game_state.pShortcuts) reasons += " shortcuts ";
state += reasons;
state += ">";
}
if (game_state.capped)
state += " { Capped }";
else
state += " { Uncapped }";
SK_DrawExternalOSD ( "FAR", state);
if (frames_drawn > 0)
frames_drawn = -1;
}
else {
SK_DrawExternalOSD ( "FAR", "" );
if (frames_drawn > 0)
frames_drawn = -1;
}
// Prevent patching an altered executable
if (game_state.patchable)
{
if (game_state.needFPSCap () && (! game_state.capped))
{
game_state.capFPS ();
game_state.capped = true;
}
if ((! game_state.needFPSCap ()) && game_state.capped)
{
game_state.uncapFPS ();
game_state.capped = false;
}
if (__FAR_HUDLESS.enqueue)
{
if (__FAR_HUDLESS.clear == 4-1)
{
// In all truth, I should capture the screenshot myself, but I don't
// want to bother with that right now ;)
SK_SteamAPI_TakeScreenshot ();
__FAR_HUDLESS.clear--;
}
else if (__FAR_HUDLESS.clear <= 0)
{
(*game_state.pHUDOpacity) =
__FAR_HUDLESS.opacity;
__FAR_HUDLESS.clear = 4;
__FAR_HUDLESS.enqueue = false;
}
else
__FAR_HUDLESS.clear--;
}
}
XINPUT_STATE state = { };
if (__FAR_Freelook && SK_XInput_PollController (0, &state))
{
float LX = state.Gamepad.sThumbLX;
float LY = state.Gamepad.sThumbLY;
float norm = sqrt (LX*LX + LY*LY);
float nLX = LX / norm;
float nLY = LY / norm;
float unit = 1.0f;
if (norm > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
{
norm = std::min (norm, 32767.0f) - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
unit = norm / (32767.0f - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
}
else
{
norm = 0.0f;
unit = 0.0f;
}
float uLX = (LX / 32767.0f) * unit;
float uLY = (LY / 32767.0f) * unit;
float RX = state.Gamepad.sThumbRX;
float RY = state.Gamepad.sThumbRY;
norm = sqrt (RX*RX + RY*RY);
float nRX = RX / norm;
float nRY = RY / norm;
unit = 1.0f;
if (norm > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
{
norm = std::min (norm, 32767.0f) - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
unit = norm / (32767.0f - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE);
}
else
{
norm = 0.0f;
unit = 0.0f;
}
float uRX = (RX / 32767.0f) * unit;
float uRY = (RY / 32767.0f) * unit;
float ddX = uLX;
float ddY = uLY;
float ddA = uRX;
float ddB = uRY;
vec3_t pos; pos [0] = (*far_cam.pCamera) [0]; pos [1] = (*far_cam.pCamera) [1]; pos [2] = (*far_cam.pCamera) [2];
vec3_t target; target [0] = (*far_cam.pLook) [0]; target [1] = (*far_cam.pLook) [1]; target [2] = (*far_cam.pLook) [2];
vec3_t diff; diff [0] = target [0] - pos [0];
diff [1] = target [1] - pos [1];
diff [2] = target [2] - pos [2];
float hypXY = sqrtf (diff [0] * diff [0] + diff [2] * diff [2]);
float dX, dY, dZ;
dX = ddX*diff [2]/hypXY;
dY = ddX*diff [0]/hypXY;
#if 0
if (ddZ != 0.0f)
{
float roll = far_cam.roll;
float cosRoll = cosf (roll);
dX = dX*cosRoll
dY = dY*cosRoll
dZ = ddX*sinf(roll)
(*far_cam.pLook)[1] = target [1] - dZ;
(*far_cam.pCamera)[1] = pos [1] - dZ;
}
#endif
(*far_cam.pLook) [0] = target [0] - dX;
(*far_cam.pLook) [2] = target [2] + dY;
(*far_cam.pCamera) [0] = pos [0] - dX;
(*far_cam.pCamera) [2] = pos [2] + dY;
pos [0] = (*far_cam.pCamera) [0]; pos [1] = (*far_cam.pCamera) [1]; pos [2] = (*far_cam.pCamera) [2];
target [0] = (*far_cam.pLook) [0]; target [1] = (*far_cam.pLook) [1]; target [2] = (*far_cam.pLook) [2];
diff [0] = target [0] - pos [0];
diff [1] = target [1] - pos [1];
diff [2] = target [2] - pos [2];
hypXY = sqrtf (diff [0] * diff [0] + diff [2] * diff [2]);
float hypZ = sqrtf (diff [1] * diff [1] + hypXY * hypXY);
#if 0
if useZ then
dX = flySpeed*Xdiff/hypZ
dY = flySpeed*Ydiff/hypZ
dZ = flySpeed*Zdiff/hypZ
if moveDir == "backward" then dZ = -dZ end
writeFloat(tarZAD, tarZ+dZ)
writeFloat(camZAD, camZ+dZ)
else
#endif
dX = ddY * diff [0] / hypXY;
dY = ddY * diff [2] / hypXY;
dZ = ddY * diff [1] / hypXY;
(*far_cam.pLook) [0] = target [0] + dX;
(*far_cam.pLook) [2] = target [2] + dY;
(*far_cam.pCamera) [0] = pos [0] + dX;
(*far_cam.pCamera) [2] = pos [2] + dY;
}
}
// Sit and spin until the user figures out what an OSD is
//
DWORD
WINAPI
SK_FAR_OSD_Disclaimer (LPVOID user)
{
while ((volatile bool&)config.osd.show)
Sleep (66);
far_osd_disclaimer->set_value (false);
far_osd_disclaimer->store ();
far_prefs->write (far_prefs_file);
CloseHandle (GetCurrentThread ());
return 0;
}
typedef void (CALLBACK *SK_PluginKeyPress_pfn)(
BOOL Control, BOOL Shift, BOOL Alt,
BYTE vkCode
);
static SK_PluginKeyPress_pfn SK_PluginKeyPress_Original;
#define SK_MakeKeyMask(vKey,ctrl,shift,alt) \
(UINT)((vKey) | (((ctrl) != 0) << 9) | \
(((shift)!= 0) << 10) | \
(((alt) != 0) << 11))
#define SK_ControlShiftKey(vKey) SK_MakeKeyMask ((vKey), true, true, false)
void
CALLBACK
SK_FAR_PluginKeyPress (BOOL Control, BOOL Shift, BOOL Alt, BYTE vkCode)
{
UINT uiMaskedKeyCode =
SK_MakeKeyMask (vkCode, Control, Shift, Alt);
const UINT uiHudlessMask =
SK_MakeKeyMask ( __FAR_HUDLESS.keybind.vKey, __FAR_HUDLESS.keybind.ctrl,
__FAR_HUDLESS.keybind.shift, __FAR_HUDLESS.keybind.alt );
const UINT uiLockCenterMask =
SK_MakeKeyMask ( far_cam.center_binding.vKey, far_cam.center_binding.ctrl,
far_cam.center_binding.shift, far_cam.center_binding.alt );
const UINT uiLockFocusMask =
SK_MakeKeyMask ( far_cam.focus_binding.vKey, far_cam.focus_binding.ctrl,
far_cam.focus_binding.shift, far_cam.focus_binding.alt );
const UINT uiToggleFreelookMask =
SK_MakeKeyMask ( far_cam.freelook_binding.vKey, far_cam.freelook_binding.ctrl,
far_cam.freelook_binding.shift, far_cam.freelook_binding.alt );
switch (uiMaskedKeyCode)
{
#ifdef WORKING_FPS_UNCAP
case SK_ControlShiftKey (VK_OEM_PERIOD):
SK_FAR_SetFramerateCap (game_state.enforce_cap);
break;
#endif
case SK_ControlShiftKey (VK_OEM_6): // ']'
{
if (__FAR_GlobalIllumWorkGroupSize < 8)
__FAR_GlobalIllumWorkGroupSize = 8;
__FAR_GlobalIllumWorkGroupSize <<= 1ULL;
if (__FAR_GlobalIllumWorkGroupSize > 128)
__FAR_GlobalIllumWorkGroupSize = 128;
} break;
case SK_ControlShiftKey (VK_OEM_4): // '['
{
if (__FAR_GlobalIllumWorkGroupSize > 128)
__FAR_GlobalIllumWorkGroupSize = 128;
__FAR_GlobalIllumWorkGroupSize >>= 1UL;
if (__FAR_GlobalIllumWorkGroupSize < 16)
__FAR_GlobalIllumWorkGroupSize = 0;
} break;
default:
{
if (uiMaskedKeyCode == uiHudlessMask)
{
if (__FAR_HUDLESS.enqueue == false)
{
__FAR_HUDLESS.clear = 4;
__FAR_HUDLESS.enqueue = true;
__FAR_HUDLESS.opacity = (*game_state.pHUDOpacity);
*game_state.pHUDOpacity = 0.0f;
}