forked from marcussacana/FAR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nier.cpp
2453 lines (1888 loc) · 74.4 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
#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.5.6.5"
#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;
#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::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;
#include <unordered_set>
static std::unordered_set <ID3D11Texture2D *> far_title_textures;
static std::unordered_set <ID3D11ShaderResourceView *> far_title_views;
// (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;
std::unordered_set <ID3D11Texture2D *> textures;
std::unordered_set <ID3D11ShaderResourceView *> views;
} 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;
std::unordered_set <ID3D11Texture2D *> textures;
std::unordered_set <ID3D11ShaderResourceView *> views;
} 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
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 );
// 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;
}
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 )
{
// Global Illumination (DrDaxxy)
if ( pDesc != nullptr && pDesc->StructureByteStride == 96 &&
pDesc->ByteWidth == 96 * 128 )
{
D3D11_BUFFER_DESC new_desc = *pDesc;
new_desc.ByteWidth = 96 * __FAR_GlobalIllumWorkGroupSize;
return D3D11Dev_CreateBuffer_Original (This, &new_desc, pInitialData, ppBuffer);
}
return D3D11Dev_CreateBuffer_Original (This, pDesc, pInitialData, ppBuffer);
}
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 (__uuidof (ID3D11Buffer), (void **)&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);
if (SUCCEEDED (hr) && pDesc != nullptr && ppSRView != nullptr)
{
// Bloom
if (far_bloom.textures.count ((ID3D11Texture2D *)pResource))
far_bloom.views.emplace (*ppSRView);
// AO
else if (far_ao.textures.count ((ID3D11Texture2D *)pResource))
far_ao.views.emplace (*ppSRView);
// Title Screen
else if (far_title_textures.count ((ID3D11Texture2D *)pResource))
far_title_views.emplace (*ppSRView);
}
return hr;
}
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);
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))
{
#if 0
#define VEC3_NORM(v) sqrt( (v)[0]*(v)[0] + (v)[1]*(v)[1] + (v)[2]*(v)[2] )
#endif
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
);
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;
}
}
else if (uiMaskedKeyCode == uiLockCenterMask)
{
far_cam.toggleCenterLock ();
}
else if (uiMaskedKeyCode == uiLockFocusMask)
{
far_cam.toggleFocusLock ();
}
else if (uiMaskedKeyCode == uiToggleFreelookMask)
{
__FAR_Freelook = (! __FAR_Freelook);
}
} break;
}
SK_PluginKeyPress_Original (Control, Shift, Alt, vkCode);
}
HRESULT
STDMETHODCALLTYPE
SK_FAR_PresentFirstFrame (IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
{
// Wait for the mod to init, it may be held up during version check
while (! InterlockedAdd (&__FAR_init, 0)) Sleep (16);
{
game_state.enforce_cap = (! far_uncap_fps->get_value ());
bool busy_wait = far_limiter_busy->get_value ();
game_state.patchable =
SK_FAR_SetLimiterWait ( busy_wait ? SK_FAR_WaitBehavior::Busy :
SK_FAR_WaitBehavior::Sleep );
//
// Hook keyboard input, only necessary for the FPS cap toggle right now
//
extern void WINAPI SK_PluginKeyPress (BOOL,BOOL,BOOL,BYTE);
SK_CreateFuncHook ( L"SK_PluginKeyPress",
SK_PluginKeyPress,
SK_FAR_PluginKeyPress,
(LPVOID *)&SK_PluginKeyPress_Original );
SK_EnableHook (SK_PluginKeyPress);
}
if (GetModuleHandle (L"RTSSHooks64.dll"))
{
bool warned = far_rtss_warned->get_value ();
if (! warned)
{
warned = true;
SK_MessageBox ( L"RivaTuner Statistics Server Detected\r\n\r\n\t"
L"If FAR does not work correctly, this is probably why.",
L"Incompatible Third-Party Software", MB_OK | MB_ICONWARNING );
far_rtss_warned->set_value (true);
far_rtss_warned->store ();
far_prefs->write (far_prefs_file);
}
}
// Since people don't read guides, nag them to death...
if (far_osd_disclaimer->get_value ())
{
CreateThread ( nullptr, 0,
SK_FAR_OSD_Disclaimer, nullptr,
0x00, nullptr );
}
return S_OK;
}
typedef HRESULT (WINAPI *D3D11Dev_CreateTexture2D_pfn)(
_In_ ID3D11Device *This,
_In_ const D3D11_TEXTURE2D_DESC *pDesc,
_In_opt_ const D3D11_SUBRESOURCE_DATA *pInitialData,
_Out_opt_ ID3D11Texture2D **ppTexture2D
);
typedef void (WINAPI *D3D11_DrawIndexed_pfn)(
_In_ ID3D11DeviceContext *This,
_In_ UINT IndexCount,
_In_ UINT StartIndexLocation,
_In_ INT BaseVertexLocation
);
typedef void (WINAPI *D3D11_Draw_pfn)(
_In_ ID3D11DeviceContext *This,
_In_ UINT VertexCount,
_In_ UINT StartVertexLocation
);
typedef void (WINAPI *D3D11_DrawIndexedInstanced_pfn)(
_In_ ID3D11DeviceContext *This,
_In_ UINT IndexCountPerInstance,
_In_ UINT InstanceCount,
_In_ UINT StartIndexLocation,
_In_ INT BaseVertexLocation,
_In_ UINT StartInstanceLocation
);
typedef void (WINAPI *D3D11_DrawIndexedInstancedIndirect_pfn)(
_In_ ID3D11DeviceContext *This,
_In_ ID3D11Buffer *pBufferForArgs,
_In_ UINT AlignedByteOffsetForArgs
);
typedef void (WINAPI *D3D11_DrawInstanced_pfn)(
_In_ ID3D11DeviceContext *This,
_In_ UINT VertexCountPerInstance,
_In_ UINT InstanceCount,
_In_ UINT StartVertexLocation,
_In_ UINT StartInstanceLocation
);
typedef void (WINAPI *D3D11_DrawInstancedIndirect_pfn)(
_In_ ID3D11DeviceContext *This,
_In_ ID3D11Buffer *pBufferForArgs,
_In_ UINT AlignedByteOffsetForArgs
);
typedef void (WINAPI *D3D11_PSSetShaderResources_pfn)(
_In_ ID3D11DeviceContext *This,
_In_ UINT StartSlot,
_In_ UINT NumViews,
_In_opt_ ID3D11ShaderResourceView* const *ppShaderResourceViews
);
static D3D11Dev_CreateTexture2D_pfn D3D11Dev_CreateTexture2D_Original = nullptr;
static D3D11_DrawIndexed_pfn D3D11_DrawIndexed_Original = nullptr;
static D3D11_Draw_pfn D3D11_Draw_Original = nullptr;
static D3D11_DrawIndexedInstanced_pfn D3D11_DrawIndexedInstanced_Original = nullptr;
static D3D11_DrawIndexedInstancedIndirect_pfn D3D11_DrawIndexedInstancedIndirect_Original = nullptr;
static D3D11_DrawInstanced_pfn D3D11_DrawInstanced_Original = nullptr;
static D3D11_DrawInstancedIndirect_pfn D3D11_DrawInstancedIndirect_Original = nullptr;
static D3D11_PSSetShaderResources_pfn D3D11_PSSetShaderResources_Original = nullptr;
extern HRESULT
WINAPI
D3D11Dev_CreateTexture2D_Override (
_In_ ID3D11Device *This,
_In_ const D3D11_TEXTURE2D_DESC *pDesc,
_In_opt_ const D3D11_SUBRESOURCE_DATA *pInitialData,
_Out_opt_ ID3D11Texture2D **ppTexture2D );
extern void
WINAPI
D3D11_DrawIndexed_Override (
_In_ ID3D11DeviceContext *This,
_In_ UINT IndexCount,
_In_ UINT StartIndexLocation,
_In_ INT BaseVertexLocation );
extern void
WINAPI
D3D11_Draw_Override (
_In_ ID3D11DeviceContext *This,
_In_ UINT VertexCount,
_In_ UINT StartVertexLocation );
extern void
WINAPI
D3D11_PSSetShaderResources_Override (
_In_ ID3D11DeviceContext *This,
_In_ UINT StartSlot,
_In_ UINT NumViews,
_In_opt_ ID3D11ShaderResourceView* const *ppShaderResourceViews );
// Overview (Durante):
//
// The bloom pyramid in Nier:A is built up of 5 buffers, which are sized
// 800x450, 400x225, 200x112, 100x56 and 50x28, regardless of resolution
// the mismatch between the largest buffer size and the screen resolution (in e.g. 2560x1440 or even 1920x1080)
// leads to some really ugly artifacts.