-
-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathIXRTrackingSystemHook.cpp
More file actions
1985 lines (1544 loc) · 76.1 KB
/
Copy pathIXRTrackingSystemHook.cpp
File metadata and controls
1985 lines (1544 loc) · 76.1 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
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
#include <unordered_map>
#include <bdshemu.h>
#include <spdlog/spdlog.h>
#include <utility/Scan.hpp>
#include <utility/Module.hpp>
#include <utility/String.hpp>
#include <utility/Emulation.hpp>
#include <utility/Patch.hpp>
#include "utility/Logging.hpp"
#include <sdk/Utility.hpp>
#include <sdk/UObjectArray.hpp>
#include <sdk/UClass.hpp>
#include <sdk/FProperty.hpp>
#include <sdk/ScriptVector.hpp>
#include <sdk/UGameplayStatics.hpp>
#include <sdk/APlayerController.hpp>
#include <sdk/APlayerCameraManager.hpp>
#include "../VR.hpp"
#include <sdk/vtables/IXRTrackingSystemVTables.hpp>
#include <sdk/structures/FXRMotionControllerData.hpp>
#include <sdk/structures/FXRHMDData.hpp>
#include <sdk/UHeadMountedDisplayFunctionLibrary.hpp>
#include <sdk/UFunction.hpp>
#include "IXRTrackingSystemHook.hpp"
detail::IXRTrackingSystemVT& get_tracking_system_vtable(std::optional<std::string> version_override = std::nullopt) {
const auto str_version = version_override.has_value() ? version_override.value() : utility::narrow(sdk::search_for_version(utility::get_executable()).value_or(L"0.00"));
auto version = sdk::get_file_version_info();
if (str_version != "0.00") {
SPDLOG_INFO("Found version {} from executable", str_version);
version.dwFileVersionMS = 0;
} else {
SPDLOG_INFO("Found version {}.{} from executable (disk version)", HIWORD(version.dwFileVersionMS), LOWORD(version.dwFileVersionMS));
}
// TODO: actually dump 5.2
// >= 5.3
if (version.dwFileVersionMS == 0x50003 || str_version.starts_with("5.3")) {
return ue5_3::IXRTrackingSystemVT::get();
}
// >= 5.2
if (version.dwFileVersionMS == 0x50002 || str_version.starts_with("5.2")) {
return ue5_1::IXRTrackingSystemVT::get();
}
// >= 5.1
if (version.dwFileVersionMS == 0x50001 || str_version.starts_with("5.1")) {
return ue5_1::IXRTrackingSystemVT::get();
}
// >= 5.0
if (version.dwFileVersionMS == 0x50000 || str_version.starts_with("5.0")) {
return ue5_00::IXRTrackingSystemVT::get();
}
// 4.27
if (version.dwFileVersionMS == 0x4001B || str_version.starts_with("4.27")) {
return ue4_27::IXRTrackingSystemVT::get();
}
// 4.26
if (version.dwFileVersionMS == 0x4001A || str_version.starts_with("4.26")) {
return ue4_26::IXRTrackingSystemVT::get();
}
// 4.25
if (version.dwFileVersionMS == 0x40019 || str_version.starts_with("4.25")) {
return ue4_25::IXRTrackingSystemVT::get();
}
// 4.24
if (version.dwFileVersionMS == 0x40018 || str_version.starts_with("4.24")) {
return ue4_24::IXRTrackingSystemVT::get();
}
// 4.23
if (version.dwFileVersionMS == 0x40017 || str_version.starts_with("4.23")) {
return ue4_23::IXRTrackingSystemVT::get();
}
// 4.22
if (version.dwFileVersionMS == 0x40016 || str_version.starts_with("4.22")) {
return ue4_22::IXRTrackingSystemVT::get();
}
// 4.21
if (version.dwFileVersionMS == 0x40015 || str_version.starts_with("4.21")) {
return ue4_21::IXRTrackingSystemVT::get();
}
// 4.20
if (version.dwFileVersionMS == 0x40014 || str_version.starts_with("4.20")) {
return ue4_20::IXRTrackingSystemVT::get();
}
// 4.19
if (version.dwFileVersionMS == 0x40013 || str_version.starts_with("4.19")) {
return ue4_19::IXRTrackingSystemVT::get();
}
// 4.18
if (version.dwFileVersionMS == 0x40012 || str_version.starts_with("4.18")) {
return ue4_18::IXRTrackingSystemVT::get();
}
// versions lower than 4.18 do not have IXRTrackingSystem
return detail::IXRTrackingSystemVT::get();
}
detail::IXRCameraVT& get_camera_vtable(std::optional<std::string> version_override = std::nullopt) {
const auto str_version = version_override.has_value() ? version_override.value() : utility::narrow(sdk::search_for_version(utility::get_executable()).value_or(L"0.00"));
auto version = sdk::get_file_version_info();
if (str_version != "0.00") {
version.dwFileVersionMS = 0;
}
// TODO: actually dump 5.2
if (version.dwFileVersionMS == 0x50003 || str_version.starts_with("5.3")) {
return ue5_3::IXRCameraVT::get();
}
// >= 5.2
if (version.dwFileVersionMS == 0x50002 || str_version.starts_with("5.2")) {
return ue5_1::IXRCameraVT::get();
}
// >= 5.1
if (version.dwFileVersionMS == 0x50001 || str_version.starts_with("5.1")) {
return ue5_1::IXRCameraVT::get();
}
// >= 5.0
if (version.dwFileVersionMS == 0x50000 || str_version.starts_with("5.0")) {
return ue5_00::IXRCameraVT::get();
}
// 4.27
if (version.dwFileVersionMS == 0x4001B || str_version.starts_with("4.27")) {
return ue4_27::IXRCameraVT::get();
}
// 4.26
if (version.dwFileVersionMS == 0x4001A || str_version.starts_with("4.26")) {
return ue4_26::IXRCameraVT::get();
}
// 4.25
if (version.dwFileVersionMS == 0x40019 || str_version.starts_with("4.25")) {
return ue4_25::IXRCameraVT::get();
}
// 4.24
if (version.dwFileVersionMS == 0x40018 || str_version.starts_with("4.24")) {
return ue4_24::IXRCameraVT::get();
}
// 4.23
if (version.dwFileVersionMS == 0x40017 || str_version.starts_with("4.23")) {
return ue4_23::IXRCameraVT::get();
}
// 4.22
if (version.dwFileVersionMS == 0x40016 || str_version.starts_with("4.22")) {
return ue4_22::IXRCameraVT::get();
}
// 4.21
if (version.dwFileVersionMS == 0x40015 || str_version.starts_with("4.21")) {
return ue4_21::IXRCameraVT::get();
}
// 4.20
if (version.dwFileVersionMS == 0x40014 || str_version.starts_with("4.20")) {
return ue4_20::IXRCameraVT::get();
}
// 4.19
if (version.dwFileVersionMS == 0x40013 || str_version.starts_with("4.19")) {
return ue4_19::IXRCameraVT::get();
}
// 4.18
if (version.dwFileVersionMS == 0x40012 || str_version.starts_with("4.18")) {
return ue4_18::IXRCameraVT::get();
}
// Versions lower than 4.18 do not have IXRCamera
return detail::IXRCameraVT::get();
}
detail::IHeadMountedDisplayVT& get_hmd_vtable(std::optional<std::string> version_override = std::nullopt) {
const auto str_version = version_override.has_value() ? version_override.value() : utility::narrow(sdk::search_for_version(utility::get_executable()).value_or(L"0.00"));
auto version = sdk::get_file_version_info();
if (str_version != "0.00") {
version.dwFileVersionMS = 0;
}
// TODO: actually dump 5.2
if (version.dwFileVersionMS == 0x50003 || str_version.starts_with("5.3")) {
return ue5_3::IHeadMountedDisplayVT::get();
}
// >= 5.2
if (version.dwFileVersionMS == 0x50002 || str_version.starts_with("5.2")) {
return ue5_1::IHeadMountedDisplayVT::get();
}
// >= 5.1
if (version.dwFileVersionMS == 0x50001 || str_version.starts_with("5.1")) {
return ue5_1::IHeadMountedDisplayVT::get();
}
// >= 5.0
if (version.dwFileVersionMS == 0x50000 || str_version.starts_with("5.0")) {
return ue5_00::IHeadMountedDisplayVT::get();
}
// 4.27
if (version.dwFileVersionMS == 0x4001B || str_version.starts_with("4.27")) {
return ue4_27::IHeadMountedDisplayVT::get();
}
// 4.26
if (version.dwFileVersionMS == 0x4001A || str_version.starts_with("4.26")) {
return ue4_26::IHeadMountedDisplayVT::get();
}
// 4.25
if (version.dwFileVersionMS == 0x40019 || str_version.starts_with("4.25")) {
return ue4_25::IHeadMountedDisplayVT::get();
}
// 4.24
if (version.dwFileVersionMS == 0x40018 || str_version.starts_with("4.24")) {
return ue4_24::IHeadMountedDisplayVT::get();
}
// 4.23
if (version.dwFileVersionMS == 0x40017 || str_version.starts_with("4.23")) {
return ue4_23::IHeadMountedDisplayVT::get();
}
// 4.22
if (version.dwFileVersionMS == 0x40016 || str_version.starts_with("4.22")) {
return ue4_22::IHeadMountedDisplayVT::get();
}
// 4.21
if (version.dwFileVersionMS == 0x40015 || str_version.starts_with("4.21")) {
return ue4_21::IHeadMountedDisplayVT::get();
}
// 4.20
if (version.dwFileVersionMS == 0x40014 || str_version.starts_with("4.20")) {
return ue4_20::IHeadMountedDisplayVT::get();
}
// 4.19
if (version.dwFileVersionMS == 0x40013 || str_version.starts_with("4.19")) {
return ue4_19::IHeadMountedDisplayVT::get();
}
// 4.18
if (version.dwFileVersionMS == 0x40012 || str_version.starts_with("4.18")) {
return ue4_18::IHeadMountedDisplayVT::get();
}
// 4.17
if (version.dwFileVersionMS == 0x40011 || str_version.starts_with("4.17")) {
return ue4_17::IHeadMountedDisplayVT::get();
}
// 4.16
if (version.dwFileVersionMS == 0x40010 || str_version.starts_with("4.16")) {
return ue4_16::IHeadMountedDisplayVT::get();
}
// 4.15
if (version.dwFileVersionMS == 0x4000F || str_version.starts_with("4.15")) {
return ue4_15::IHeadMountedDisplayVT::get();
}
// 4.14
if (version.dwFileVersionMS == 0x4000E || str_version.starts_with("4.14")) {
return ue4_14::IHeadMountedDisplayVT::get();
}
// 4.13
if (version.dwFileVersionMS == 0x4000D || str_version.starts_with("4.13")) {
return ue4_13::IHeadMountedDisplayVT::get();
}
// 4.12
if (version.dwFileVersionMS == 0x4000C || str_version.starts_with("4.12")) {
return ue4_12::IHeadMountedDisplayVT::get();
}
// 4.11
if (version.dwFileVersionMS == 0x4000B || str_version.starts_with("4.11")) {
return ue4_11::IHeadMountedDisplayVT::get();
}
// 4.10
if (version.dwFileVersionMS == 0x4000A || str_version.starts_with("4.10")) {
return ue4_10::IHeadMountedDisplayVT::get();
}
return detail::IHeadMountedDisplayVT::get();
}
IXRTrackingSystemHook* g_hook = nullptr;
namespace detail {
struct FunctionInfo {
size_t functions_within{0};
bool calls_xr_camera{false};
bool calls_update_player_camera{false};
bool calls_apply_hmd_rotation{false};
bool process_view_rotation_analysis_failed{false};
};
std::mutex return_address_to_functions_mutex{};
std::unordered_map<uintptr_t, uintptr_t> return_address_to_functions{};
std::unordered_map<uintptr_t, FunctionInfo> functions{};
std::atomic<uint32_t> total_times_funcs_called{0};
}
IXRTrackingSystemHook::IXRTrackingSystemHook(FFakeStereoRenderingHook* stereo_hook, size_t offset_in_engine)
: m_stereo_hook{stereo_hook},
m_offset_in_engine{offset_in_engine}
{
SPDLOG_INFO("IXRTrackingSystemHook::IXRTrackingSystemHook");
g_hook = this;
pre_initialize();
}
void IXRTrackingSystemHook::pre_initialize() {
SPDLOG_INFO("IXRTrackingSystemHook::pre_initialize");
for (auto i = 0; i < m_xrtracking_vtable.size(); ++i) {
m_xrtracking_vtable[i] = (uintptr_t)+[](void*) {
return nullptr;
};
}
for (auto i = 0; i < m_camera_vtable.size(); ++i) {
m_camera_vtable[i] = (uintptr_t)+[](void*) {
return nullptr;
};
}
for (auto i = 0; i < m_hmd_vtable.size(); ++i) {
m_hmd_vtable[i] = (uintptr_t)+[](void*) {
return nullptr;
};
}
for (auto i = 0; i < m_stereo_rendering_vtable.size(); ++i) {
m_stereo_rendering_vtable[i] = (uintptr_t)+[](void*) {
return nullptr;
};
}
for (auto i = 0; i < m_view_extension_vtable.size(); ++i) {
m_view_extension_vtable[i] = (uintptr_t)+[](void*) {
return nullptr;
};
}
// GetSystemName
m_xrtracking_vtable[0] = (uintptr_t)+[](void* this_ptr, sdk::FName* out) -> sdk::FName* {
static sdk::FName fake_name{};
return &fake_name;
};
const auto version = sdk::get_file_version_info();
const auto str_version = utility::narrow(sdk::search_for_version(utility::get_executable()).value_or(L"0.00"));
try {
const auto first_half = std::stoi(str_version.substr(0, str_version.find('.')));
const auto second_half = std::stoi(str_version.substr(str_version.find('.') + 1, str_version.size() - 1));
if (first_half == 4 && second_half == 26) {
m_is_4_26 = true;
}
if (first_half == 4 && second_half <= 25) {
SPDLOG_INFO("IXRTrackingSystemHook::IXRTrackingSystemHook: version <= 4.25");
m_is_leq_4_25 = true;
}
if (first_half == 4 && second_half <= 17) {
SPDLOG_INFO("IXRTrackingSystemHook::IXRTrackingSystemHook: version <= 4.17");
m_is_leq_4_17 = true;
}
} catch(...) {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: failed to convert second half of version string to number");
}
if (!m_is_leq_4_25 && version.dwFileVersionMS >= 0x40000 && version.dwFileVersionMS <= 0x40019) {
SPDLOG_INFO("IXRTrackingSystemHook::IXRTrackingSystemHook: version <= 4.25");
m_is_leq_4_25 = true;
}
if (!m_is_leq_4_17 && version.dwFileVersionMS >= 0x40000 && version.dwFileVersionMS <= 0x40011) {
SPDLOG_INFO("IXRTrackingSystemHook::IXRTrackingSystemHook: version <= 4.17");
m_is_leq_4_17 = true;
}
}
void IXRTrackingSystemHook::on_draw_ui() {
}
void IXRTrackingSystemHook::on_pre_engine_tick(sdk::UGameEngine* engine, float delta) {
auto& vr = VR::get();
if (!m_initialized && (vr->is_any_aim_method_active() || vr->wants_blueprint_load())) {
if (!m_initialized) {
initialize();
}
}
if (vr->is_any_aim_method_active()) {
auto& data = m_process_view_rotation_data;
// This can happen if player logic stops running (e.g. player has died or entered a loading screen)
// so we dont want the UI off in nowhere land
if (data.was_called && std::chrono::high_resolution_clock::now() - data.last_update >= std::chrono::seconds(2)) {
data.was_called = false;
vr->recenter_view();
vr->set_pre_flattened_rotation(glm::identity<glm::quat>());
SPDLOG_INFO("IXRTrackingSystemHook: Recentering view because of timeout");
}
}
}
void IXRTrackingSystemHook::initialize() {
m_initialized = true;
SPDLOG_INFO("IXRTrackingSystemHook::initialize");
if (sdk::UGameEngine::get() == nullptr) {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: UGameEngine not found");
return;
}
if (m_offset_in_engine == 0) {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: m_offset_in_engine not set");
return;
}
const auto& camera_vt = get_camera_vtable(m_overridden_version);
const auto& trackvt = get_tracking_system_vtable(m_overridden_version);
const auto& hmdvt = get_hmd_vtable(m_overridden_version);
if (trackvt.implemented()) {
if (trackvt.GetXRCamera_index().has_value()) {
m_xrtracking_vtable[trackvt.GetXRCamera_index().value()] = (uintptr_t)&get_xr_camera;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_xr_camera_index not implemented");
}
if (trackvt.IsHeadTrackingAllowed_index().has_value()) {
m_xrtracking_vtable[trackvt.IsHeadTrackingAllowed_index().value()] = (uintptr_t)&is_head_tracking_allowed;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: is_head_tracking_allowed_index not implemented");
}
if (trackvt.IsHeadTrackingAllowedForWorld_index().has_value()) {
m_xrtracking_vtable[trackvt.IsHeadTrackingAllowedForWorld_index().value()] = (uintptr_t)&is_head_tracking_allowed_for_world;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: is_head_tracking_allowed_for_world_index not implemented");
}
if (trackvt.GetMotionControllerData_index().has_value()) {
m_xrtracking_vtable[trackvt.GetMotionControllerData_index().value()] = (uintptr_t)&get_motion_controller_data;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_motion_controller_data_index not implemented");
}
if (trackvt.GetHMDData_index().has_value()) {
m_xrtracking_vtable[trackvt.GetHMDData_index().value()] = (uintptr_t)&get_hmd_data;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_hmd_data_index not implemented");
}
if (trackvt.GetCurrentPose_index().has_value()) {
m_xrtracking_vtable[trackvt.GetCurrentPose_index().value()] = (uintptr_t)&get_current_pose;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_current_pose_index not implemented");
}
if (trackvt.GetXRSystemFlags_index().has_value()) {
m_xrtracking_vtable[trackvt.GetXRSystemFlags_index().value()] = (uintptr_t)&get_xr_system_flags;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_xr_system_flags_index not implemented");
}
// Doesn't cause a crash, but must be implemented to fix audio bugs
if (trackvt.GetAudioListenerOffset_index().has_value()) {
m_xrtracking_vtable[trackvt.GetAudioListenerOffset_index().value()] = (uintptr_t)&get_audio_listener_offset;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_audio_listener_offset_index not implemented");
}
// Some games calls this for some reason so it needs to be implemented so we dont crash
if (trackvt.GetBaseOrientation_index().has_value()) {
m_xrtracking_vtable[trackvt.GetBaseOrientation_index().value()] = (uintptr_t)&get_base_orientation;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_base_orientation_index not implemented");
}
if (trackvt.GetBasePosition_index().has_value()) {
m_xrtracking_vtable[trackvt.GetBasePosition_index().value()] = (uintptr_t)&get_base_position;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_base_position_index not implemented");
}
if (trackvt.GetBaseRotation_index().has_value()) {
m_xrtracking_vtable[trackvt.GetBaseRotation_index().value()] = (uintptr_t)&get_base_rotation;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_base_rotation_index not implemented");
}
if (trackvt.ResetOrientation_index().has_value()) {
m_xrtracking_vtable[trackvt.ResetOrientation_index().value()] = (uintptr_t)&reset_orientation;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: reset_orientation_index not implemented");
}
if (trackvt.ResetPosition_index().has_value()) {
m_xrtracking_vtable[trackvt.ResetPosition_index().value()] = (uintptr_t)&reset_position;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: reset_position_index not implemented");
}
if (trackvt.ResetOrientationAndPosition_index().has_value()) {
m_xrtracking_vtable[trackvt.ResetOrientationAndPosition_index().value()] = (uintptr_t)&reset_orientation_and_position;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: reset_orientation_and_position_index not implemented");
}
if (m_is_4_26) {
if (trackvt.GetStereoRenderingDevice_index().has_value()) {
m_xrtracking_vtable[trackvt.GetStereoRenderingDevice_index().value()] = (uintptr_t)&get_stereo_rendering_device;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_stereo_rendering_device_index not implemented");
}
} else {
if (trackvt.GetStereoRenderingDevice_index().has_value()) {
m_xrtracking_vtable[trackvt.GetStereoRenderingDevice_index().value()] = (uintptr_t)+[](void* a1, void* a2, void* a3) -> void* {
SPDLOG_INFO_ONCE("GetStereoRenderingDevice called");
return nullptr;
};
}
}
if (hmdvt.implemented() && trackvt.GetHMDDevice_index().has_value()) {
m_xrtracking_vtable[trackvt.GetHMDDevice_index().value()] = (uintptr_t)+[]() -> void* {
SPDLOG_INFO_ONCE("GetHMDDevice called");
return &g_hook->m_hmd_device;
};
if (hmdvt.IsHMDConnected_index().has_value()) {
m_hmd_vtable[hmdvt.IsHMDConnected_index().value()] = (uintptr_t)&is_hmd_connected;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: is_hmd_connected_index not implemented");
}
if (hmdvt.GetIdealDebugCanvasRenderTargetSize_index().has_value()) {
// This one is a bit tricky. In very rare cases this index can be off by one. We need to make the hook
// verify that the return address is within UGameViewportClient::Draw. We will not just hook this function, but one index ahead as well.
m_hmd_vtable[hmdvt.GetIdealDebugCanvasRenderTargetSize_index().value()] = (uintptr_t)&get_ideal_debug_canvas_render_target_size;
m_hmd_vtable[hmdvt.GetIdealDebugCanvasRenderTargetSize_index().value() + 1] = (uintptr_t)&get_ideal_debug_canvas_render_target_size;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_ideal_debug_canvas_render_target_size_index not implemented");
}
if (hmdvt.ResetOrientation_index().has_value()) {
m_hmd_vtable[hmdvt.ResetPosition_index().value()] = (uintptr_t)&reset_orientation;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: reset_orientation_index not implemented");
}
if (hmdvt.ResetPosition_index().has_value()) {
m_hmd_vtable[hmdvt.ResetPosition_index().value()] = (uintptr_t)&reset_position;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: reset_position_index not implemented");
}
if (hmdvt.ResetOrientationAndPosition_index().has_value()) {
m_hmd_vtable[hmdvt.ResetOrientationAndPosition_index().value()] = (uintptr_t)&reset_orientation_and_position;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: reset_orientation_and_position_index not implemented");
}
m_hmd_device.vtable = m_hmd_vtable.data();
m_hmd_device.stereo_rendering_vtable = m_stereo_rendering_vtable.data();
}
} else if (hmdvt.implemented()) {
SPDLOG_INFO("IXRTrackingSystemHook::IXRTrackingSystemHook: IXRTrackingSystemVT not implemented, using IHeadMountedDisplayVT");
if (hmdvt.IsHeadTrackingAllowed_index().has_value()) {
m_hmd_vtable[hmdvt.IsHeadTrackingAllowed_index().value()] = (uintptr_t)&is_head_tracking_allowed;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: is_head_tracking_allowed_index not implemented");
}
if (hmdvt.ApplyHmdRotation_index().has_value()) {
m_hmd_vtable[hmdvt.ApplyHmdRotation_index().value()] = (uintptr_t)&apply_hmd_rotation;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: apply_hmd_rotation_index not implemented");
}
if (hmdvt.IsHMDConnected_index().has_value()) {
m_hmd_vtable[hmdvt.IsHMDConnected_index().value()] = (uintptr_t)&is_hmd_connected;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: is_hmd_connected_index not implemented");
}
if (hmdvt.GetAudioListenerOffset_index().has_value()) {
m_hmd_vtable[hmdvt.GetAudioListenerOffset_index().value()] = (uintptr_t)&get_audio_listener_offset;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_audio_listener_offset_index not implemented");
}
if (hmdvt.UpdatePlayerCamera_index().has_value()) {
m_hmd_vtable[hmdvt.UpdatePlayerCamera_index().value()] = (uintptr_t)&update_player_camera;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: update_player_camera_index not implemented");
}
if (hmdvt.GetViewExtension_index().has_value()) {
m_hmd_vtable[hmdvt.GetViewExtension_index().value()] = (uintptr_t)&get_view_extension;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: get_view_extension_index not implemented");
}
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: IXRTrackingSystemVT and IXRHeadMountedDisplayVT not implemented");
}
if (camera_vt.implemented()) {
if (camera_vt.ApplyHMDRotation_index().has_value()) {
m_camera_vtable[camera_vt.ApplyHMDRotation_index().value()] = (uintptr_t)&apply_hmd_rotation;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: apply_hmd_rotation_index not implemented");
}
if (camera_vt.UpdatePlayerCamera_index().has_value()) {
m_camera_vtable[camera_vt.UpdatePlayerCamera_index().value()] = (uintptr_t)&update_player_camera;
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: update_player_camera_index not implemented");
}
} else {
SPDLOG_ERROR("IXRTrackingSystemHook::IXRTrackingSystemHook: IXRCameraVT not implemented");
}
m_view_extension.vtable = m_view_extension_vtable.data();
m_view_extension_shared.obj = &m_view_extension;
m_view_extension_shared.ref_controller = nullptr;
if (camera_vt.implemented()) {
m_xr_camera.vtable = m_camera_vtable.data();
m_xr_camera_shared.obj = &m_xr_camera;
}
if (trackvt.implemented()) { // >= 4.18
auto tracking_system = (SharedPtr*)((uintptr_t)sdk::UGameEngine::get() + m_offset_in_engine);
m_xr_tracking_system.vtable = m_xrtracking_vtable.data();
tracking_system->obj = &m_xr_tracking_system;
tracking_system->ref_controller = nullptr;
//tracking_system->ref_controller = m_ref_controller.get();
} else if (hmdvt.implemented()) { // <= 4.17
auto hmd_device = (SharedPtr*)((uintptr_t)sdk::UGameEngine::get() + m_offset_in_engine);
m_hmd_device.vtable = m_hmd_vtable.data();
m_hmd_device.stereo_rendering_vtable = m_stereo_rendering_vtable.data();
hmd_device->obj = &m_hmd_device;
hmd_device->ref_controller = nullptr;
}
SPDLOG_INFO("IXRTrackingSystemHook::IXRTrackingSystemHook done");
}
IXRTrackingSystemHook::SharedPtr* IXRTrackingSystemHook::get_stereo_rendering_device(sdk::IXRTrackingSystem* This, IXRTrackingSystemHook::SharedPtr* out, void* a3) {
// So we are not actually using this for anything right now, we are just using it to switch our vtables to 4.27 if needed.
// Not actually aware of any instance where we would legitimately need to return a valid stereo device.
if (!g_hook->m_is_4_26) {
return nullptr;
}
SPDLOG_INFO_ONCE("IXRTrackingSystemHook::get_stereo_rendering_device");
static std::mutex mtx{};
static std::unordered_set<uintptr_t> invalid_return_addresses{};
const auto return_address = (uintptr_t)_ReturnAddress();
std::scoped_lock _{mtx};
if (invalid_return_addresses.contains(return_address)) {
return nullptr;
}
try {
invalid_return_addresses.insert(return_address);
// Setup the emulator. Set RAX to magic value. If RAX changes at any point, abort.
const auto module_within = utility::get_module_within(return_address);
if (!module_within) {
SPDLOG_ERROR("IXRTrackingSystemHook::get_stereo_rendering_device: failed to get module within");
return nullptr;
}
static auto check_ix = [](const INSTRUX& ix) {
for (auto i = 0; i < ix.OperandsCount; ++i) {
const auto& op = ix.Operands[i];
if (op.Type != ND_OPERAND_TYPE::ND_OP_MEM) {
continue;
}
if (op.Info.Memory.HasBase && op.Info.Memory.Base == NDR_RAX) {
SPDLOG_INFO("Found dereference of RAX");
return true;
}
}
return false;
};
const auto retdecode = utility::decode_one((uint8_t*)return_address);
if (!retdecode) {
SPDLOG_ERROR("IXRTrackingSystemHook::get_stereo_rendering_device: failed to decode instruction at 0x{:x}", return_address);
return nullptr;
}
bool should_switch_to_4_27 = check_ix(*retdecode);
if (!should_switch_to_4_27) {
utility::ShemuContext base_context{*module_within};
base_context.ctx->Registers.RegRip = return_address;
base_context.ctx->Registers.RegRax = 0xdeadbeef;
base_context.ctx->MemThreshold = 10;
utility::emulate(*module_within, return_address, 10, base_context, [&should_switch_to_4_27](const utility::ShemuContextExtended& ctx) -> utility::ExhaustionResult {
if (should_switch_to_4_27) {
return utility::ExhaustionResult::BREAK;
}
if (check_ix(ctx.ctx->ctx->Instruction) || check_ix(ctx.next.ix)) {
should_switch_to_4_27 = true;
return utility::ExhaustionResult::BREAK;
}
if (ctx.next.ix.BranchInfo.IsBranch) {
return utility::ExhaustionResult::BREAK;
}
if (ctx.ctx->ctx->Registers.RegRax != 0xdeadbeef) {
return utility::ExhaustionResult::BREAK;
}
if (ctx.next.writes_to_memory) {
return utility::ExhaustionResult::STEP_OVER;
}
return utility::ExhaustionResult::CONTINUE;
});
}
if (should_switch_to_4_27) {
SPDLOG_INFO("IXRTrackingSystemHook::get_stereo_rendering_device: detected necessary switch to 4.27");
// pre_initialize clears out all of our already set vtables to their default values
g_hook->pre_initialize();
g_hook->m_is_4_26 = false;
g_hook->m_overridden_version = "4.27";
// now set up the vtables again
g_hook->initialize();
// This is the real function that was supposed to be called.
if (out != nullptr) {
*out = g_hook->m_xr_camera_shared;
}
return out;
}
} catch(...) {
SPDLOG_ERROR("IXRTrackingSystemHook::get_stereo_rendering_device: exception");
invalid_return_addresses.insert(return_address);
}
return nullptr;
}
void IXRTrackingSystemHook::manual_update_control_rotation() {
const auto world = sdk::UEngine::get()->get_world();
if (world == nullptr) {
return;
}
const auto controller = sdk::UGameplayStatics::get()->get_player_controller(world, 0);
if (controller == nullptr) {
return;
}
const auto pawn = controller->get_acknowledged_pawn();
if (pawn == nullptr) {
return;
}
auto control_rotation = controller->get_control_rotation();
Rotator<double> ue5_rotation {
(double)control_rotation.x,
(double)control_rotation.y,
(double)control_rotation.z
};
const auto is_ue5 = g_hook->m_stereo_hook->has_double_precision();
Rotator<float>* chosen_rotation = is_ue5 ? (Rotator<float>*)&ue5_rotation : (Rotator<float>*)&control_rotation;
update_view_rotation(controller, chosen_rotation);
// Conv back to vec3<float>
glm::vec3 final_rotation{};
if (is_ue5) {
final_rotation = glm::vec3{
(float)ue5_rotation.pitch,
(float)ue5_rotation.yaw,
(float)ue5_rotation.roll
};
} else {
final_rotation = glm::vec3{
control_rotation.x,
control_rotation.y,
control_rotation.z
};
}
controller->set_control_rotation(final_rotation);
}
bool IXRTrackingSystemHook::analyze_head_tracking_allowed(uintptr_t return_address) {
++detail::total_times_funcs_called;
std::scoped_lock _{detail::return_address_to_functions_mutex};
auto it = detail::return_address_to_functions.find(return_address);
if (it == detail::return_address_to_functions.end()) {
const auto vfunc = utility::find_virtual_function_start(return_address);
if (vfunc) {
detail::return_address_to_functions[return_address] = *vfunc;
} else {
detail::return_address_to_functions[return_address] = 0;
}
it = detail::return_address_to_functions.find(return_address);
}
if (it->second == 0) {
return false;
}
auto& func = detail::functions[it->second];
if (!g_hook->m_process_view_rotation_hook && detail::total_times_funcs_called >= 100 &&
!func.calls_xr_camera && !func.calls_update_player_camera && !func.calls_apply_hmd_rotation && !func.process_view_rotation_analysis_failed)
{
g_hook->m_attempted_hook_view_rotation = true;
SPDLOG_INFO("Possibly found ProcessViewRotation: 0x{:x}", it->second);
const auto module = utility::get_module_within(it->second);
if (!module) {
SPDLOG_ERROR("Failed to get module for ProcessViewRotation");
return false;
}
const auto func_ptr = utility::scan_ptr(*module, it->second);
if (!func_ptr) {
SPDLOG_ERROR("Failed to find ProcessViewRotation");
return false;
}
// We do not definitively hook ProcessViewRotation here, we point it towards a function analyzer
// that checks whether the arguments match up with what we expect. If they do, we hook it.
m_addr_of_process_view_rotation_ptr = *func_ptr;
//g_hook->m_process_view_rotation_hook = std::make_unique<PointerHook>((void**)*func_ptr, (void*)&process_view_rotation_analyzer);
g_hook->m_process_view_rotation_hook = safetyhook::create_inline((void*)it->second, (void*)&process_view_rotation_analyzer);
if (!g_hook->m_process_view_rotation_hook) {
SPDLOG_ERROR("Failed to hook ProcessViewRotation");
func.process_view_rotation_analysis_failed = true;
g_hook->m_attempted_hook_view_rotation = false;
return true;
}
SPDLOG_INFO("Hooked ProcessViewRotation");
}
return true;
}
void* IXRTrackingSystemHook::get_orientation_and_position_native(void* rcx, void* rdx, void* r8, void* r9) {
SPDLOG_INFO_ONCE("GetOrientationAndPosition native function {:x}", (uintptr_t)_ReturnAddress());
const auto og = g_hook->m_native_get_oap_hook->get_original<decltype(&get_orientation_and_position_native)>();
g_hook->m_within_get_oap_native = true;
const auto result = og(rcx, rdx, r8, r9);
g_hook->m_within_get_oap_native = false;
return result;
}
void* IXRTrackingSystemHook::is_head_mounted_display_enabled_native(void* rcx, void* rdx, void* r8, void* r9) {
SPDLOG_INFO_ONCE("IsHeadMountedDisplayEnabled native function {:x}", (uintptr_t)_ReturnAddress());
const auto og = g_hook->m_native_is_hmd_enabled_hook->get_original<decltype(&is_head_mounted_display_enabled_native)>();
g_hook->m_within_is_hmd_enabled_native = true;
const auto result = og(rcx, rdx, r8, r9);
g_hook->m_within_is_hmd_enabled_native = false;
return result;
}
bool IXRTrackingSystemHook::is_head_tracking_allowed(sdk::IXRTrackingSystem*) {
SPDLOG_INFO_ONCE("is_head_tracking_allowed {:x}", (uintptr_t)_ReturnAddress());
auto& vr = VR::get();
if (!vr->is_hmd_active()) {
return false;
}
if (g_hook->m_is_leq_4_17) {
return true;
}
static bool attempted_check = false;
if (vr->wants_blueprint_load() && !attempted_check) try {
attempted_check = true;
const auto uobjectarray = sdk::FUObjectArray::get();
if (uobjectarray == nullptr) {
SPDLOG_ERROR("Failed to find FUObjectArray");
return false;
}
const auto hmd_lib = sdk::UHeadMountedDisplayFunctionLibrary::static_class();
if (hmd_lib == nullptr) {
SPDLOG_ERROR("Failed to find UHeadMountedDisplayFunctionLibrary");
return false;
}
if (sdk::UFunction::get_native_function_offset() == 0) {
SPDLOG_ERROR("UFunction::get_native_function_offset is 0");
return false;
}