-
-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathUObjectHook.cpp
More file actions
3560 lines (2802 loc) · 126 KB
/
Copy pathUObjectHook.cpp
File metadata and controls
3560 lines (2802 loc) · 126 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 <fstream>
#include <utility/Logging.hpp>
#include <utility/String.hpp>
#include <utility/ScopeGuard.hpp>
#include <sdk/UObjectBase.hpp>
#include <sdk/UObjectArray.hpp>
#include <sdk/UClass.hpp>
#include <sdk/FField.hpp>
#include <sdk/FProperty.hpp>
#include <sdk/UFunction.hpp>
#include <sdk/AActor.hpp>
#include <sdk/threading/GameThreadWorker.hpp>
#include <sdk/FMalloc.hpp>
#include <sdk/FStructProperty.hpp>
#include <sdk/USceneComponent.hpp>
#include <sdk/UGameplayStatics.hpp>
#include <sdk/APlayerController.hpp>
#include <sdk/APawn.hpp>
#include <sdk/APlayerCameraManager.hpp>
#include <sdk/ScriptVector.hpp>
#include <sdk/FBoolProperty.hpp>
#include <sdk/FObjectProperty.hpp>
#include <sdk/FArrayProperty.hpp>
#include <sdk/UMotionControllerComponent.hpp>
#include "uobjecthook/SDKDumper.hpp"
#include "VR.hpp"
#include "UObjectHook.hpp"
//#define VERBOSE_UOBJECTHOOK
std::shared_ptr<UObjectHook>& UObjectHook::get() {
static std::shared_ptr<UObjectHook> instance = std::make_shared<UObjectHook>();
return instance;
}
UObjectHook::MotionControllerState::~MotionControllerState() {
if (this->adjustment_visualizer != nullptr) {
GameThreadWorker::get().enqueue([vis = this->adjustment_visualizer]() {
SPDLOG_INFO("[UObjectHook::MotionControllerState] Destroying adjustment visualizer for component {:x}", (uintptr_t)vis);
if (!UObjectHook::get()->exists(vis)) {
return;
}
vis->destroy_actor();
SPDLOG_INFO("[UObjectHook::MotionControllerState] Destroyed adjustment visualizer for component {:x}", (uintptr_t)vis);
});
}
}
nlohmann::json UObjectHook::MotionControllerStateBase::to_json() const {
return {
{"rotation_offset", utility::math::to_json(rotation_offset)},
{"location_offset", utility::math::to_json(location_offset)},
{"hand", hand},
{"permanent", permanent}
};
}
void UObjectHook::MotionControllerStateBase::from_json(const nlohmann::json& data) {
if (data.contains("rotation_offset")) {
rotation_offset = utility::math::from_json_quat(data["rotation_offset"]);
}
if (data.contains("location_offset")) {
location_offset = utility::math::from_json_vec3(data["location_offset"]);
}
if (data.contains("hand")) {
hand = data["hand"].get<uint8_t>();
hand = hand % 2;
}
if (data.contains("permanent") && data["permanent"].is_boolean()) {
permanent = data["permanent"].get<bool>();
}
}
void UObjectHook::activate() {
if (m_hooked) {
return;
}
if (GameThreadWorker::get().is_same_thread()) {
hook();
return;
}
m_wants_activate = true;
}
void UObjectHook::hook() {
if (m_hooked) {
return;
}
SPDLOG_INFO("[UObjectHook] Hooking UObjectBase");
m_hooked = true;
m_wants_activate = false;
auto destructor_fn = sdk::UObjectBase::get_destructor();
if (!destructor_fn) {
SPDLOG_ERROR("[UObjectHook] Failed to find UObjectBase::destructor, cannot hook UObjectBase");
return;
}
auto add_object_fn = sdk::UObjectBase::get_add_object();
if (!add_object_fn) {
SPDLOG_ERROR("[UObjectHook] Failed to find UObjectBase::AddObject, cannot hook UObjectBase");
return;
}
m_destructor_hook = safetyhook::create_inline((void**)destructor_fn.value(), &destructor);
if (!m_destructor_hook) {
SPDLOG_ERROR("[UObjectHook] Failed to hook UObjectBase::destructor, cannot hook UObjectBase");
return;
}
m_add_object_hook = safetyhook::create_inline((void**)add_object_fn.value(), &add_object);
if (!m_add_object_hook) {
SPDLOG_ERROR("[UObjectHook] Failed to hook UObjectBase::AddObject, cannot hook UObjectBase");
return;
}
SPDLOG_INFO("[UObjectHook] Hooked UObjectBase");
// Add all the objects that already exist
auto uobjectarray = sdk::FUObjectArray::get();
for (auto i = 0; i < uobjectarray->get_object_count(); ++i) {
auto object = uobjectarray->get_object(i);
if (object == nullptr || object->object == nullptr) {
continue;
}
add_new_object(object->object);
}
SPDLOG_INFO("[UObjectHook] Added {} existing objects", m_objects.size());
SPDLOG_INFO("[UObjectHook] Deserializing persistent states");
reload_persistent_states();
SPDLOG_INFO("[UObjectHook] Deserialized {} persistent states", m_persistent_states.size());
m_fully_hooked = true;
}
void UObjectHook::add_new_object(sdk::UObjectBase* object) {
std::unique_lock _{m_mutex};
std::unique_ptr<MetaObject> meta_object{};
/*static const auto prim_comp_t = sdk::find_uobject<sdk::UClass>(L"Class /Script/Engine.PrimitiveComponent");
if (prim_comp_t != nullptr && object->get_class()->is_a(prim_comp_t)) {
static const auto bGenerateOverlapEvents = (sdk::FBoolProperty*)prim_comp_t->find_property(L"bGenerateOverlapEvents");
if (bGenerateOverlapEvents != nullptr) {
bGenerateOverlapEvents->set_value_in_object(object, true);
}
}*/
const auto c = object->get_class();
if (c == nullptr) {
return;
}
if (!m_reusable_meta_objects.empty()) {
meta_object = std::move(m_reusable_meta_objects.back());
m_reusable_meta_objects.pop_back();
} else {
meta_object = std::make_unique<MetaObject>();
}
m_objects.insert(object);
meta_object->full_name = object->get_full_name();
meta_object->uclass = object->get_class();
m_meta_objects[object] = std::move(meta_object);
m_most_recent_objects.push_front((sdk::UObject*)object);
if (m_most_recent_objects.size() > 50) {
m_most_recent_objects.pop_back();
}
for (auto super = (sdk::UStruct*)object->get_class(); super != nullptr; super = super->get_super_struct()) {
m_objects_by_class[(sdk::UClass*)super].insert(object);
if (auto it = m_on_creation_add_component_jobs.find((sdk::UClass*)super); it != m_on_creation_add_component_jobs.end()) {
GameThreadWorker::get().enqueue([object, this]() {
if (!this->exists(object)) {
return;
}
for (auto super = (sdk::UStruct*)object->get_class(); super != nullptr; super = super->get_super_struct()) {
std::function<void(sdk::UObject*)> job{};
{
std::shared_lock _{m_mutex};
if (auto it = this->m_on_creation_add_component_jobs.find((sdk::UClass*)super); it != this->m_on_creation_add_component_jobs.end()) {
job = it->second;
}
}
if (job) {
job((sdk::UObject*)object);
}
}
});
}
}
#ifdef VERBOSE_UOBJECTHOOK
SPDLOG_INFO("Adding object {:x} {:s}", (uintptr_t)object, utility::narrow(m_meta_objects[object]->full_name));
#endif
}
void UObjectHook::on_config_load(const utility::Config& cfg, bool set_defaults) {
ZoneScopedN(__FUNCTION__);
for (IModValue& option : m_options) {
option.config_load(cfg, set_defaults);
}
if (!set_defaults && m_enabled_at_startup->value()) {
m_wants_activate = true;
}
}
void UObjectHook::on_config_save(utility::Config& cfg) {
ZoneScopedN(__FUNCTION__);
for (IModValue& option : m_options) {
option.config_save(cfg);
}
}
void UObjectHook::on_pre_engine_tick(sdk::UGameEngine* engine, float delta) {
m_last_delta_time = delta;
if (m_wants_activate) {
hook();
}
if (m_fully_hooked) {
{
std::shared_lock _{m_mutex};
const auto ui_active = g_framework->is_drawing_ui();
for (auto& state : m_motion_controller_attached_components) {
if (m_overlap_detection_actor == nullptr && state.second->adjusting && ui_active) {
state.second->adjusting = false;
}
}
}
update_persistent_states();
}
}
const auto quat_converter = glm::quat{Matrix4x4f {
0, 0, -1, 0,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 1
}};
// TODO: split this into some functions because its getting a bit massive
void UObjectHook::on_pre_calculate_stereo_view_offset(void* stereo_device, const int32_t view_index, Rotator<float>* view_rotation,
const float world_to_meters, Vector3f* view_location, bool is_double)
{
if (!m_fully_hooked) {
return;
}
auto& vr = VR::get();
if (!vr->is_hmd_active()) {
return;
}
if (m_uobject_hook_disabled) {
return;
}
auto view_d = (Vector3d*)view_location;
auto rot_d = (Rotator<double>*)view_rotation;
if (is_double) {
m_last_camera_location = glm::vec3{*view_d};
} else {
m_last_camera_location = *view_location;
}
if (m_camera_attach.object != nullptr) {
if (m_camera_attach.object->is_a(sdk::AActor::static_class())) {
const auto actor = (sdk::AActor*)m_camera_attach.object;
const auto location = actor->get_actor_location();
const auto rotation = actor->get_actor_rotation();
// Conv rotation to glm quat
const auto rotation_glm_mat = glm::yawPitchRoll(
glm::radians(-rotation.y),
glm::radians(rotation.x),
glm::radians(-rotation.z));
const auto rotation_glm_quat = glm::quat{rotation_glm_mat};
const auto adjusted_loc = location - (quat_converter * (rotation_glm_quat * utility::math::ue4_to_glm(m_camera_attach.offset)));
if (is_double) {
*view_d = glm::vec<3, double>{adjusted_loc};
} else {
*view_location = adjusted_loc;
}
} else if (m_camera_attach.object->is_a(sdk::USceneComponent::static_class())) {
const auto comp = (sdk::USceneComponent*)m_camera_attach.object;
const auto location = comp->get_world_location();
const auto rotation = comp->get_world_rotation();
// Conv rotation to glm quat
const auto rotation_glm_mat = glm::yawPitchRoll(
glm::radians(-rotation.y),
glm::radians(rotation.x),
glm::radians(-rotation.z));
const auto rotation_glm_quat = glm::quat{rotation_glm_mat};
const auto adjusted_loc = location - (quat_converter * (rotation_glm_quat * utility::math::ue4_to_glm(m_camera_attach.offset)));
if (is_double) {
*view_d = glm::vec<3, double>{adjusted_loc};
} else {
*view_location = adjusted_loc;
}
} // else todo?
}
if ((view_index + 1) % 2 == 0) {
tick_attachments(view_rotation, world_to_meters, view_location, is_double);
}
}
void UObjectHook::on_post_calculate_stereo_view_offset(void* stereo_device, const int32_t view_index, Rotator<float>* view_rotation,
const float world_to_meters, Vector3f* view_location, bool is_double)
{
if (!m_fully_hooked) {
return;
}
if (!VR::get()->is_hmd_active()) {
return;
}
if (m_uobject_hook_disabled) {
return;
}
std::shared_lock _{m_mutex};
bool any_adjusting = false;
for (auto& it : m_motion_controller_attached_components) {
if (it.second->adjusting) {
any_adjusting = true;
break;
}
}
VR::get()->set_aim_allowed(!any_adjusting);
}
void UObjectHook::tick_attachments(Rotator<float>* view_rotation, const float world_to_meters, Vector3f* view_location, bool is_double) {
if (m_uobject_hook_disabled) {
return;
}
auto& vr = VR::get();
auto view_d = (Vector3d*)view_location;
auto rot_d = (Rotator<double>*)view_rotation;
const auto view_mat_inverse = !is_double ?
glm::yawPitchRoll(
glm::radians(-view_rotation->yaw),
glm::radians(view_rotation->pitch),
glm::radians(-view_rotation->roll)) :
glm::yawPitchRoll(
glm::radians(-(float)rot_d->yaw),
glm::radians((float)rot_d->pitch),
glm::radians(-(float)rot_d->roll));
const auto view_quat_inverse = glm::quat {
view_mat_inverse
};
auto vqi_norm = glm::normalize(view_quat_inverse);
// Decoupled Pitch
if (vr->is_decoupled_pitch_enabled()) {
vqi_norm = utility::math::flatten(vqi_norm);
}
const auto rotation_offset = vr->get_rotation_offset();
const auto hmd_origin = glm::vec3{vr->get_transform(0)[3]};
const auto pos = glm::vec3{rotation_offset * (hmd_origin - glm::vec3{vr->get_standing_origin()})};
const auto adjusted_world_to_meters = world_to_meters * vr->get_world_scale();
const auto view_quat_inverse_flat = utility::math::flatten(view_quat_inverse);
const auto offset1 = quat_converter * (glm::normalize(view_quat_inverse_flat) * (pos * adjusted_world_to_meters));
glm::vec3 final_position{};
if (is_double) {
final_position = glm::vec3{*view_d} - offset1;
} else {
final_position = *view_location - offset1;
}
if (!vr->is_using_controllers()) {
return;
}
glm::vec3 right_hand_position = vr->get_controller_position_with_offset(VRRuntime::Hand::RIGHT);
glm::quat right_hand_rotation = vr->get_controller_rotation_with_offset(VRRuntime::Hand::RIGHT);// vr->get_aim_rotation(vr->get_right_controller_index());
const float lerp_speed = m_attach_lerp_speed->value() * m_last_delta_time;
if (m_attach_lerp_enabled->value()) {
auto spherical_distance_right = glm::dot(right_hand_rotation, m_last_right_aim_rotation);
if (spherical_distance_right < 0.0f) {
spherical_distance_right = -spherical_distance_right;
}
const auto lenr = glm::max(1.0f, glm::length(right_hand_position - m_last_right_grip_location));
m_last_right_grip_location = glm::lerp(m_last_right_grip_location, right_hand_position, glm::min(1.0f, lerp_speed * lenr));
m_last_right_aim_rotation = glm::slerp(m_last_right_aim_rotation, right_hand_rotation, lerp_speed * spherical_distance_right);
right_hand_position = m_last_right_grip_location;
right_hand_rotation = m_last_right_aim_rotation;
}
const auto original_right_hand_rotation = right_hand_rotation;
const auto original_right_hand_position = right_hand_position - hmd_origin;
glm::vec3 left_hand_position = vr->get_controller_position_with_offset(VRRuntime::Hand::LEFT);
glm::quat left_hand_rotation = vr->get_controller_rotation_with_offset(VRRuntime::Hand::LEFT); // vr->get_aim_rotation(vr->get_left_controller_index());
if (m_attach_lerp_enabled->value()) {
auto spherical_distance_left = glm::dot(left_hand_rotation, m_last_left_aim_rotation);
if (spherical_distance_left < 0.0f) {
spherical_distance_left = -spherical_distance_left;
}
const auto lenl = glm::max(1.0f, glm::length(left_hand_position - m_last_left_grip_location));
m_last_left_grip_location = glm::lerp(m_last_left_grip_location, left_hand_position, glm::min(1.0f, lerp_speed * lenl));
m_last_left_aim_rotation = glm::slerp(m_last_left_aim_rotation, left_hand_rotation, lerp_speed * spherical_distance_left);
left_hand_position = m_last_left_grip_location;
left_hand_rotation = m_last_left_aim_rotation;
}
right_hand_position = glm::vec3{rotation_offset * (right_hand_position - hmd_origin)};
left_hand_position = glm::vec3{rotation_offset * (left_hand_position - hmd_origin)};
right_hand_position = quat_converter * (glm::normalize(view_quat_inverse_flat) * (right_hand_position * adjusted_world_to_meters));
left_hand_position = quat_converter * (glm::normalize(view_quat_inverse_flat) * (left_hand_position * adjusted_world_to_meters));
right_hand_position = final_position - right_hand_position;
left_hand_position = final_position - left_hand_position;
right_hand_rotation = rotation_offset * right_hand_rotation;
right_hand_rotation = (glm::normalize(view_quat_inverse_flat) * right_hand_rotation);
left_hand_rotation = rotation_offset * left_hand_rotation;
left_hand_rotation = (glm::normalize(view_quat_inverse_flat) * left_hand_rotation);
//right_hand_rotation = glm::normalize(right_hand_rotation * right_hand_offset_q);
auto right_hand_euler = glm::degrees(utility::math::euler_angles_from_steamvr(right_hand_rotation));
//left_hand_rotation = glm::normalize(left_hand_rotation * left_hand_offset_q);
auto left_hand_euler = glm::degrees(utility::math::euler_angles_from_steamvr(left_hand_rotation));
auto with_mutex = [this](auto fn) {
std::shared_lock _{m_mutex};
auto result = fn();
return result;
};
update_motion_controller_components(left_hand_position, left_hand_euler, right_hand_position, right_hand_euler);
auto comps = with_mutex([this]() { return m_motion_controller_attached_components; });
sdk::TArray<sdk::UPrimitiveComponent*> overlapped_components{};
sdk::TArray<sdk::UPrimitiveComponent*> overlapped_components_left{};
// Update overlapped components and overlap actor transform
if (m_overlap_detection_actor != nullptr && this->exists(m_overlap_detection_actor)) {
m_overlap_detection_actor->set_actor_location(right_hand_position, false, false);
m_overlap_detection_actor->set_actor_rotation(right_hand_euler, false);
if (!g_framework->is_drawing_ui()) {
overlapped_components = std::move(m_overlap_detection_actor->get_overlapping_components());
}
}
// Update overlapped components and overlap actor transform (left)
if (m_overlap_detection_actor_left != nullptr && this->exists(m_overlap_detection_actor_left)) {
m_overlap_detection_actor_left->set_actor_location(left_hand_position, false, false);
m_overlap_detection_actor_left->set_actor_rotation(left_hand_euler, false);
if (!g_framework->is_drawing_ui()) {
overlapped_components_left = std::move(m_overlap_detection_actor_left->get_overlapping_components());
}
}
// Check intuitive attachment for overlapped components
if (!g_framework->is_drawing_ui() && (!overlapped_components.empty() || !overlapped_components_left.empty())) {
static bool prev_right_a_pressed = false;
static bool prev_left_a_pressed = false;
const auto is_a_down_raw_right = vr->is_action_active_any_joystick(vr->get_action_handle(VR::s_action_a_button_right));
const auto was_a_pressed_right = !prev_right_a_pressed && is_a_down_raw_right;
const auto is_a_down_raw_left = vr->is_action_active_any_joystick(vr->get_action_handle(VR::s_action_a_button_left));
const auto was_a_pressed_left = !prev_left_a_pressed && is_a_down_raw_left;
prev_right_a_pressed = is_a_down_raw_right;
prev_left_a_pressed = is_a_down_raw_left;
// Update existing attached components before moving onto overlapped ones.
for (auto& it : comps) {
auto& state = *it.second;
if (state.hand == 0) {
if (is_a_down_raw_left) {
state.adjusting = true;
} else if (!is_a_down_raw_left) {
state.adjusting = false;
}
} else if (state.hand == 1) {
if (is_a_down_raw_right) {
state.adjusting = true;
} else if (!is_a_down_raw_right) {
state.adjusting = false;
}
}
}
auto update_overlaps = [&](int32_t hand, const sdk::TArray<sdk::UPrimitiveComponent*>& components) {
const auto was_pressed = hand == 0 ? was_a_pressed_left : was_a_pressed_right;
const auto is_pressed = hand == 0 ? is_a_down_raw_left : is_a_down_raw_right;
for (auto overlap : components) {
static const auto capsule_component_t = sdk::find_uobject<sdk::UClass>(L"Class /Script/Engine.CapsuleComponent");
if (overlap->get_class()->is_a(capsule_component_t)) {
continue;
}
{
std::shared_lock _{m_mutex};
if (m_spawned_spheres_to_components.contains(overlap)) {
overlap = (sdk::UPrimitiveComponent*)m_spawned_spheres_to_components[overlap];
}
}
const auto owner = overlap->get_owner();
bool owner_is_adjustment_vis = false;
if (owner == m_overlap_detection_actor || owner == m_overlap_detection_actor_left) {
continue;
}
// Make sure we don't try to attach to the adjustment visualizer
{
std::shared_lock _{m_mutex};
auto it = std::find_if(m_motion_controller_attached_components.begin(), m_motion_controller_attached_components.end(),
[&](auto& it) {
if (it.second->adjustment_visualizer != nullptr && this->exists_unsafe(it.second->adjustment_visualizer)) {
if (it.second->adjustment_visualizer == owner) {
owner_is_adjustment_vis = true;
return true;
}
}
return false;
});
if (owner_is_adjustment_vis) {
continue;
}
}
if (was_pressed) {
auto state = get_or_add_motion_controller_state(overlap);
state->adjusting = true;
state->hand = hand;
} /*else if (!is_pressed) {
auto state = get_motion_controller_state(overlap);
if (state && (*state)->hand == hand) {
(*state)->adjusting = false;
}
}*/
}
};
update_overlaps(0, overlapped_components_left);
update_overlaps(1, overlapped_components);
}
for (auto& it : comps) {
auto comp = it.first;
if (!this->exists(comp) || it.second == nullptr) {
continue;
}
auto& state = *it.second;
const auto orig_position = comp->get_world_location();
const auto orig_rotation = comp->get_world_rotation();
// Convert orig_rotation to quat
const auto orig_rotation_mat = glm::yawPitchRoll(
glm::radians(-orig_rotation.y),
glm::radians(orig_rotation.x),
glm::radians(-orig_rotation.z));
const auto orig_rotation_quat = glm::quat{orig_rotation_mat};
const auto& hand_rotation = state.hand == 1 ? right_hand_rotation : left_hand_rotation;
const auto& hand_position = state.hand == 1 ? right_hand_position : left_hand_position;
const auto& hand_euler = state.hand == 1 ? right_hand_euler : left_hand_euler;
const auto adjusted_rotation = hand_rotation * glm::inverse(state.rotation_offset);
const auto adjusted_euler = glm::degrees(utility::math::euler_angles_from_steamvr(adjusted_rotation));
const auto adjusted_location = hand_position + (quat_converter * (adjusted_rotation * state.location_offset));
if (state.adjusting) {
// Create a temporary actor that visualizes how we're adjusting the component
if (state.adjustment_visualizer == nullptr) {
auto ugs = sdk::UGameplayStatics::get();
auto visualizer = ugs->spawn_actor(sdk::UGameEngine::get()->get_world(), sdk::AActor::static_class(), orig_position);
if (visualizer != nullptr) {
auto add_comp = [&](sdk::UClass* c, std::function<void(sdk::UActorComponent*)> fn) -> sdk::UActorComponent* {
if (c == nullptr) {
SPDLOG_ERROR("[UObjectHook] Cannot add component of null class");
return nullptr;
}
auto new_comp = visualizer->add_component_by_class(c);
if (new_comp != nullptr) {
fn(new_comp);
if (new_comp->is_a(sdk::USceneComponent::static_class())) {
auto scene_comp = (sdk::USceneComponent*)new_comp;
scene_comp->set_hidden_in_game(false);
}
visualizer->finish_add_component(new_comp);
} else {
SPDLOG_ERROR("[UObjectHook] Failed to add component {} to adjustment visualizer", utility::narrow(c->get_full_name()));
}
return new_comp;
};
add_comp(sdk::find_uobject<sdk::UClass>(L"Class /Script/Engine.SphereComponent"), [](sdk::UActorComponent* new_comp) {
struct SphereRadiusParams {
float radius{};
bool update_overlaps{false};
};
auto params = SphereRadiusParams{};
params.radius = 10.f;
const auto fn = new_comp->get_class()->find_function(L"SetSphereRadius");
if (fn != nullptr) {
new_comp->process_event(fn, ¶ms);
}
});
/*add_comp(sdk::find_uobject<sdk::UClass>(L"Class /Script/Engine.ArrowComponent"), [](sdk::UActorComponent* new_comp) {
struct {
float color[4]{1.0f, 0.0f, 0.0f, 1.0f};
} params{};
const auto fn = new_comp->get_class()->find_function(L"SetArrowColor");
if (fn != nullptr) {
new_comp->process_event(fn, ¶ms);
}
});*/
// Ghetto way of making a "mesh" out of the box components
for (auto j = 0; j < 3; ++j) {
for (auto i = 1; i < 25; ++i) {
add_comp(sdk::find_uobject<sdk::UClass>(L"Class /Script/Engine.BoxComponent"), [i, j](sdk::UActorComponent* new_comp) {
auto color = (uint8_t*)new_comp->get_property_data(L"ShapeColor");
if (color != nullptr) {
color[0] = 255;
color[1] = 255;
color[2] = 0;
color[3] = 0;
}
auto extent = (void*)new_comp->get_property_data(L"BoxExtent");
if (extent != nullptr) {
static const bool is_ue5 = sdk::ScriptVector::static_struct()->get_struct_size() == sizeof(glm::vec<3, double>);
const auto ratio = (float)i / 100.0f;
const auto x = j == 0 ? ratio * 25.0f : 25.0f;
const auto y = j == 1 ? ratio * 5.0f : 5.0f;
const auto z = j == 2 ? ratio * 5.0f : 5.0f;
glm::vec3 wanted_ext = glm::vec3{x, y, z};
if (is_ue5) {
*((glm::vec<3, double>*)extent) = wanted_ext;
} else {
*((glm::vec3*)extent) = wanted_ext;
}
}
});
}
}
//ugs->finish_spawning_actor(visualizer, orig_position);
std::unique_lock _{m_mutex};
state.adjustment_visualizer = visualizer;
} else {
SPDLOG_ERROR("[UObjectHook] Failed to spawn actor for adjustment visualizer");
}
} else {
state.adjustment_visualizer->set_actor_location(hand_position, false, false);
state.adjustment_visualizer->set_actor_rotation(hand_euler, false);
}
std::unique_lock _{m_mutex};
const auto mat_inverse =
glm::yawPitchRoll(
glm::radians(-orig_rotation.y),
glm::radians(orig_rotation.x),
glm::radians(-orig_rotation.z));
const auto mq = glm::quat{mat_inverse};
const auto mqi = glm::inverse(mq);
state.rotation_offset = mqi * hand_rotation;
state.location_offset = mqi * utility::math::ue4_to_glm(hand_position - orig_position);
} else {
if (state.adjustment_visualizer != nullptr) {
state.adjustment_visualizer->destroy_actor();
std::unique_lock _{m_mutex};
state.adjustment_visualizer = nullptr;
}
comp->set_world_location(adjusted_location, false, false);
comp->set_world_rotation(adjusted_euler, false, false);
}
if (!state.permanent) {
GameThreadWorker::get().enqueue([this, comp, orig_position, orig_rotation]() {
if (!this->exists(comp)) {
return;
}
comp->set_world_location(orig_position, false, false);
comp->set_world_rotation(orig_rotation, false, false);
});
}
}
}
void UObjectHook::spawn_overlapper(uint32_t hand) {
GameThreadWorker::get().enqueue([this, hand]() {
auto ugs = sdk::UGameplayStatics::get();
auto world = sdk::UGameEngine::get()->get_world();
if (ugs == nullptr || world == nullptr) {
return;
}
auto overlapper = ugs->spawn_actor(world, sdk::AActor::static_class(), glm::vec3{0, 0, 0});
if (hand == 1) {
if (m_overlap_detection_actor != nullptr && this->exists(m_overlap_detection_actor)) {
m_overlap_detection_actor->destroy_actor();
}
m_overlap_detection_actor = overlapper;
} else {
if (m_overlap_detection_actor_left != nullptr && this->exists(m_overlap_detection_actor_left)) {
m_overlap_detection_actor_left->destroy_actor();
}
m_overlap_detection_actor_left = overlapper;
}
if (overlapper != nullptr) {
auto add_comp = [&](sdk::AActor* target, sdk::UClass* c, std::function<void(sdk::UActorComponent*)> fn) -> sdk::UActorComponent* {
if (c == nullptr) {
SPDLOG_ERROR("[UObjectHook] Cannot add component of null class");
return nullptr;
}
auto new_comp = target->add_component_by_class(c);
if (new_comp != nullptr) {
fn(new_comp);
if (new_comp->is_a(sdk::USceneComponent::static_class())) {
auto scene_comp = (sdk::USceneComponent*)new_comp;
scene_comp->set_hidden_in_game(false);
}
target->finish_add_component(new_comp);
} else {
SPDLOG_ERROR("[UObjectHook] Failed to add component {} to target", utility::narrow(c->get_full_name()));
}
return new_comp;
};
const auto sphere_t = sdk::find_uobject<sdk::UClass>(L"Class /Script/Engine.SphereComponent");
add_comp(overlapper, sphere_t, [](sdk::UActorComponent* new_comp) {
struct SphereRadiusParams {
float radius{};
bool update_overlaps{true};
} params{};
params.radius = 10.0f;
const auto fn = new_comp->get_class()->find_function(L"SetSphereRadius");
if (fn != nullptr) {
new_comp->process_event(fn, ¶ms);
}
});
const auto skeletal_mesh_t = sdk::find_uobject<sdk::UClass>(L"Class /Script/Engine.SkeletalMeshComponent");
const auto meshes = get_objects_by_class(skeletal_mesh_t);
for (auto obj : meshes) {
if (!this->exists(obj)) {
continue;
}
// Dont add the same one twice
if (m_components_with_spheres.contains((sdk::USceneComponent*)obj)) {
continue;
}
const auto default_obj = obj->get_class()->get_class_default_object();
if (obj == default_obj) {
continue;
}
SPDLOG_INFO("[UObjectHook] Spawning sphere for skeletal mesh {}", utility::narrow(obj->get_full_name()));
auto mesh = (sdk::USceneComponent*)obj;
auto owner = mesh->get_owner();
if (owner == nullptr) {
continue;
}
const auto owner_default = owner->get_class()->get_class_default_object();
if (owner == owner_default) {
continue;
}
SPDLOG_INFO("[UObjectHook] Owner of skeletal mesh is {}", utility::narrow(owner->get_full_name()));
auto new_sphere = (sdk::USceneComponent*)add_comp(owner, sphere_t, [](sdk::UActorComponent* new_comp) {
struct SphereRadiusParams {
float radius{};
bool update_overlaps{true};
} params{};
params.radius = 10.0f;
const auto fn = new_comp->get_class()->find_function(L"SetSphereRadius");
if (fn != nullptr) {
new_comp->process_event(fn, ¶ms);
}
});
if (new_sphere != nullptr) {
new_sphere->attach_to(mesh, L"None", 0, true);
new_sphere->set_local_transform(glm::vec3{}, glm::vec4{0, 0, 0, 1}, glm::vec3{1, 1, 1});
std::unique_lock _{m_mutex};
m_spawned_spheres.insert(new_sphere);
m_spawned_spheres_to_components[new_sphere] = mesh;
m_components_with_spheres.insert(mesh);
}
}
} else {
SPDLOG_ERROR("[UObjectHook] Failed to spawn actor for overlapper");
}
});
}
void UObjectHook::destroy_overlapper() {
GameThreadWorker::get().enqueue([this]() {
// Destroy all spawned spheres
auto spheres = get_spawned_spheres();
for (auto sphere : spheres) {
if (!this->exists(sphere)) {
continue;
}
sphere->destroy_component();
}
{
std::unique_lock _{m_mutex};
m_spawned_spheres.clear();
m_spawned_spheres_to_components.clear();
m_components_with_spheres.clear();
}
if (m_overlap_detection_actor != nullptr && this->exists(m_overlap_detection_actor)) {
m_overlap_detection_actor->destroy_actor();
m_overlap_detection_actor = nullptr;
}
if (m_overlap_detection_actor_left != nullptr && this->exists(m_overlap_detection_actor_left)) {
m_overlap_detection_actor_left->destroy_actor();
m_overlap_detection_actor_left = nullptr;
}
});
}
std::filesystem::path UObjectHook::get_persistent_dir() {
const auto base_dir = Framework::get_persistent_dir();
const auto uobjecthook_dir = base_dir / "uobjecthook";
try {
if (!std::filesystem::exists(uobjecthook_dir)) {
std::filesystem::create_directories(uobjecthook_dir);
}
} catch (const std::exception& e) {
SPDLOG_ERROR("[UObjectHook] Failed to create persistent directory: {}", e.what());
} catch (...) {
SPDLOG_ERROR("[UObjectHook] Failed to create persistent directory");
}
return uobjecthook_dir;
}
nlohmann::json UObjectHook::serialize_mc_state(const std::vector<std::string>& path, const std::shared_ptr<MotionControllerState>& state) {
nlohmann::json result{};
result["path"] = path;
result["state"] = state->to_json();
result["type"] = "motion_controller";
return result;
}
nlohmann::json UObjectHook::serialize_camera(const std::vector<std::string>& path) {
nlohmann::json result{};
result["path"] = path;
result["offset"] = utility::math::to_json(m_camera_attach.offset);
result["type"] = "camera";
// todo: adjustments/offsets, etc...? all it needs is the camera object which is fine
return result;
}
void UObjectHook::save_camera_state(const std::vector<std::string>& path) {
auto json = serialize_camera(path);
const auto wanted_dir = UObjectHook::get_persistent_dir() / "camera_state.json";
// Create dir if necessary