-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnityResolve.hpp
More file actions
1881 lines (1590 loc) · 81.8 KB
/
Copy pathUnityResolve.hpp
File metadata and controls
1881 lines (1590 loc) · 81.8 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
#pragma once
#include <algorithm>
#include <bitset>
#include <chrono>
#include <codecvt>
#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <shared_mutex>
#include <span>
#include <string>
#include <type_traits>
#if defined(GLM_VERSION) || __has_include(<glm/glm.hpp>)
#include <glm/glm.hpp>
#else
#error "Please use the glm math library."
#endif
#if (defined(__i386__) || defined(__x86__)) || (defined(__arm__) && !defined(__aarch64__))
#error "32-bit is not supported, please use a 64-bit compiler."
#endif
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#define UNITY_CALLING_CONVENTION __fastcall
#define WINDOWS_MODE
#elif defined(__ANDROID__) || defined(__APPLE__) || defined(__MACH__) || defined(__linux__) || defined(__harmony__)
#include <locale>
#include <dlfcn.h>
#define UNITY_CALLING_CONVENTION
#define OTHERS_MODE
#endif
#define UTYPE(type) unity::util::TypeIdentity<type>{}
#define UNITY_ACCESS using namespace unity::access;
#define UNITY_UDL using namespace unity::udl;
namespace unity {
enum class UnityMode : std::uint8_t { IL2CPP, MONO };
class UnityType;
class UnityClass;
class UnityField;
class UnityMethod;
class UnityAssembly;
class UnityMethodArgs;
namespace access {
template<std::size_t Offset, typename T>
struct AT {
explicit(false) operator T&() {
return *ptr();
}
explicit(false) operator const T&() {
return *ptr();
}
auto value() -> T& {
return *ptr();
}
auto ptr() -> T* {
return std::bit_cast<T*>(std::bit_cast<std::uintptr_t>(this) + Offset);
}
};
template<std::size_t Offset, typename Func>
struct FN {
template<typename... Args>
auto operator()(Args&&... args) const -> decltype(auto) {
auto func = std::bit_cast<Func*>(std::bit_cast<std::uintptr_t>(this) + Offset);
return func(std::forward<Args>(args)...);
}
};
template<typename T>
struct Field {
private:
std::size_t offset_{};
public:
auto operator[](const std::size_t offset) -> void {
this->offset_ = offset;
}
[[nodiscard]] auto offset() const -> std::size_t {
return offset_;
}
};
template<typename Ret, typename... Args>
struct Method {
private:
std::function<Ret(Args...)> func;
public:
Method() = default;
explicit(false) Method(const std::uintptr_t address) {
operator[](address);
}
auto operator[](const std::uintptr_t address) -> void {
this->func = std::bit_cast<Ret(UNITY_CALLING_CONVENTION*)(Args...)>(address);
}
template<typename... U> requires (std::is_invocable_v<std::function<Ret(Args...)>, U...>)
auto operator()(U&&... args) -> Ret {
return func(std::forward<U>(args)...);
}
[[nodiscard]] auto function() const -> const std::function<Ret(Args...)>& {
return func;
}
};
template<typename T, typename C = void>
struct Property {
private:
using GetterFunc = std::conditional_t<std::is_void_v<C>, std::function<T()>, std::function<T(C*)>>;
using SetterFunc = std::conditional_t<std::is_void_v<C>, std::function<void(T)>, std::function<void(C*, T)>>;
GetterFunc get_;
SetterFunc set_;
public:
Property() = default;
Property(GetterFunc get, SetterFunc set) :
get_{std::move(get)},
set_{std::move(set)} {}
auto operator[](const std::uintptr_t get_addr, const std::uintptr_t set_addr) -> void {
if constexpr (std::is_void_v<C>) {
get_ = std::bit_cast<T(UNITY_CALLING_CONVENTION*)()>(get_addr);
set_ = std::bit_cast<void(UNITY_CALLING_CONVENTION*)(T)>(set_addr);
} else {
get_ = std::bit_cast<T(UNITY_CALLING_CONVENTION*)(C*)>(get_addr);
set_ = std::bit_cast<void(UNITY_CALLING_CONVENTION*)(C*, T)>(set_addr);
}
}
auto get() -> T requires std::is_void_v<C> {
return get_();
}
auto set(const T& value) -> void requires std::is_void_v<C> {
set_(std::move(value));
}
auto get(C* instance) -> T requires (!std::is_void_v<C>) {
return get_(instance);
}
auto set(C* instance, const T& value) -> void requires (!std::is_void_v<C>) {
set_(instance, std::move(value));
}
[[nodiscard]] auto function() const -> std::pair<const GetterFunc&, const SetterFunc&> {
return {get_, set_};
}
explicit(false) operator T&() requires std::is_void_v<C> {
return get();
}
explicit(false) operator const T&() requires std::is_void_v<C> {
return get();
}
auto operator=(const T& value) -> Property& requires std::is_void_v<C> {
set(value);
return *this;
}
};
namespace helper {
template<typename T>
struct PropertyHelper {
private:
std::function<T()> get_;
std::function<void(T)> set_;
public:
PropertyHelper(std::function<T()> get, std::function<void(T)> set) :
get_{std::move(get)},
set_{std::move(set)} {}
auto get() -> T {
return get_();
}
auto set(const T& value) -> void {
set_(value);
}
explicit(false) operator T&() {
return get();
}
auto operator=(const T& value) -> PropertyHelper& {
set(value);
return *this;
}
};
} // namespace helper
struct Class {
template<typename T>
auto operator[](const Field<T>& field) -> T& {
return *std::bit_cast<T*>(std::bit_cast<std::uintptr_t>(this) + field.offset());
}
template<typename Ret, typename... Args>
auto operator[](Method<Ret, Args...>& method) {
if constexpr (sizeof...(Args) > 0) {
using FirstArg = std::tuple_element_t<0, std::tuple<Args...>>;
if constexpr (std::is_pointer_v<FirstArg>) {
return std::bind_front(method.function(), static_cast<FirstArg>(this));
} else {
static_assert(std::is_pointer_v<FirstArg>, "Method's first argument must be a pointer type for member-like binding");
}
} else {
static_assert(sizeof...(Args) > 0, "Method requires at least one argument (the object pointer)");
}
}
template<typename T, typename C>
auto operator[](Property<T, C>& property) -> helper::PropertyHelper<T> {
auto [get_func, set_func] = property.function();
if constexpr (std::is_void_v<C>) {
return helper::PropertyHelper<T>{std::move(get_func), std::move(set_func)};
} else {
return helper::PropertyHelper<T>{std::bind_front(get_func, static_cast<C*>(this)), std::bind_front(set_func, static_cast<C*>(this))};
}
}
};
} // namespace access
namespace util {
template<typename T>
struct TypeIdentity {
using Type = T;
};
template<typename T>
auto try_find(std::shared_mutex& mutex, const std::string_view& name, const std::map<std::string_view, std::shared_ptr<T>>& container) -> std::optional<std::weak_ptr<T>> {
std::shared_lock _(mutex);
const auto it = container.find(name);
if (it == container.end()) {
return std::nullopt;
}
return it->second;
}
inline auto split_str(const std::string_view str, const std::string_view delim) -> std::vector<std::string_view> {
return str | std::views::split(delim) | std::views::transform([](auto r) -> auto {
return std::string_view(r.begin(), r.end());
}) | std::ranges::to<std::vector>();
}
} // namespace util
namespace attributes {
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Reflection/TypeAttributes.cs
enum class TypeAttributes {
VisibilityMask = 0x00000007,
NotPublic = 0x00000000,
Public = 0x00000001,
NestedPublic = 0x00000002,
NestedPrivate = 0x00000003,
NestedFamily = 0x00000004,
NestedAssembly = 0x00000005,
NestedFamANDAssem = 0x00000006,
NestedFamORAssem = 0x00000007,
LayoutMask = 0x00000018,
AutoLayout = 0x00000000,
SequentialLayout = 0x00000008,
ExplicitLayout = 0x00000010,
ExtendedLayout = 0x00000018,
ClassSemanticsMask = 0x00000020,
Class = 0x00000000,
Interface = 0x00000020,
Abstract = 0x00000080,
Sealed = 0x00000100,
SpecialName = 0x00000400,
Import = 0x00001000,
Serializable = 0x00002000,
WindowsRuntime = 0x00004000,
StringFormatMask = 0x00030000,
AnsiClass = 0x00000000,
UnicodeClass = 0x00010000,
AutoClass = 0x00020000,
CustomFormatClass = 0x00030000,
CustomFormatMask = 0x00C00000,
BeforeFieldInit = 0x00100000,
RTSpecialName = 0x00000800,
HasSecurity = 0x00040000,
ReservedMask = 0x00040800,
};
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodAttributes.cs
enum class MethodAttributes : std::uint16_t {
MemberAccessMask = 0x0007,
PrivateScope = 0x0000,
Private = 0x0001,
FamANDAssem = 0x0002,
Assembly = 0x0003,
Family = 0x0004,
FamORAssem = 0x0005,
Public = 0x0006,
Static = 0x0010,
Final = 0x0020,
Virtual = 0x0040,
HideBySig = 0x0080,
CheckAccessOnOverride = 0x0200,
VtableLayoutMask = 0x0100,
ReuseSlot = 0x0000,
NewSlot = 0x0100,
Abstract = 0x0400,
SpecialName = 0x0800,
PinvokeImpl = 0x2000,
UnmanagedExport = 0x0008,
RTSpecialName = 0x1000,
HasSecurity = 0x4000,
RequireSecObject = 0x8000,
ReservedMask = 0xd000,
};
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Reflection/FieldAttributes.cs
enum class FieldAttributes : std::uint16_t {
FieldAccessMask = 0x0007,
PrivateScope = 0x0000,
Private = 0x0001,
FamANDAssem = 0x0002,
Assembly = 0x0003,
Family = 0x0004,
FamORAssem = 0x0005,
Public = 0x0006,
Static = 0x0010,
InitOnly = 0x0020,
Literal = 0x0040,
NotSerialized = 0x0080,
SpecialName = 0x0200,
PinvokeImpl = 0x2000,
RTSpecialName = 0x0400,
HasFieldMarshal = 0x1000,
HasDefault = 0x8000,
HasFieldRVA = 0x0100,
ReservedMask = 0x9500,
};
} // namespace attributes
class UnityAssembly final {
std::uintptr_t native_ptr{};
std::string_view native_name;
mutable std::shared_mutex mutex;
std::map<std::string_view, std::shared_ptr<UnityClass>> unity_class;
public:
UnityAssembly(std::string_view name, std::uintptr_t ptr, const std::function<void(std::map<std::string_view, std::shared_ptr<UnityClass>>&)>& callback);
auto name() const noexcept -> std::string_view;
auto ptr() const noexcept -> std::uintptr_t;
auto content() const noexcept -> auto;
auto operator[](const std::string_view& class_name) const -> std::optional<std::weak_ptr<UnityClass>>;
};
namespace api::csharp {
class Type;
} // namespace api::csharp
class UnityType final {
std::uintptr_t native_ptr{};
std::uintptr_t native_object{};
std::string native_name;
std::size_t native_size{};
public:
UnityType(std::uintptr_t native_ptr, std::uintptr_t native_object, std::string native_name, std::size_t native_size);
[[nodiscard]] auto name() const noexcept -> const std::string&;
[[nodiscard]] auto ptr() const noexcept -> std::uintptr_t;
[[nodiscard]] auto size() const noexcept -> std::size_t;
[[nodiscard]] auto type() const noexcept -> api::csharp::Type*;
};
namespace process {
auto fix_class_depend() -> void;
} // namespace process
template<typename T> concept ClassMember = std::is_same_v<T, UnityField> || std::is_same_v<T, UnityMethod>;
class UnityClass final {
std::uintptr_t native_ptr{};
std::uintptr_t native_parent_ptr{};
std::weak_ptr<UnityAssembly> native_assembly;
std::weak_ptr<UnityType> native_type;
std::weak_ptr<UnityClass> native_parent;
std::string_view native_name;
std::string_view native_namespace;
std::bitset<32> native_flags;
mutable std::shared_mutex mutex;
std::map<std::string_view, std::shared_ptr<UnityField>> unity_filed;
std::multimap<std::string_view, std::shared_ptr<UnityMethod>> unity_method;
friend auto process::fix_class_depend() -> void;
public:
UnityClass(std::uintptr_t native_ptr,
const std::weak_ptr<UnityAssembly>& native_assembly,
const std::weak_ptr<UnityType>& native_type,
std::string_view native_name,
std::uintptr_t native_parent_ptr,
std::string_view native_namespace,
std::bitset<32> native_flags,
const std::function<void(std::map<std::string_view, std::shared_ptr<UnityField>>&)>& field_callback,
const std::function<void(std::multimap<std::string_view, std::shared_ptr<UnityMethod>>&)>& method_callback);
auto name() const noexcept -> std::string_view;
auto ptr() const noexcept -> std::uintptr_t;
auto parent() const noexcept -> std::weak_ptr<UnityClass>;
auto type() const noexcept -> std::weak_ptr<UnityType>;
auto flags(attributes::TypeAttributes attributes) const noexcept -> bool;
auto name_space() const noexcept -> std::string_view;
auto assembly() const noexcept -> std::weak_ptr<UnityAssembly>;
template<ClassMember T>
auto count() const -> std::size_t;
template<ClassMember T>
auto content() const -> auto;
template<ClassMember T>
auto operator[](util::TypeIdentity<T> /* unused */, std::string_view name, const std::vector<std::string_view>& args_type = {}) const -> std::optional<std::weak_ptr<T>>;
private:
template<ClassMember U>
auto get_container() const -> const auto&;
using MethodIt = decltype(unity_method)::const_iterator;
static auto find_with_args(const std::vector<std::string_view>& args, const std::ranges::subrange<MethodIt>& range) -> std::optional<std::weak_ptr<UnityMethod>>;
};
class UnityField final {
std::uintptr_t native_ptr{};
std::uintptr_t native_class_ptr{};
std::weak_ptr<UnityClass> native_class;
std::weak_ptr<UnityType> native_type;
std::string_view native_name;
std::size_t native_offset{};
std::bitset<32> native_flags;
bool native_is_static{};
friend auto process::fix_class_depend() -> void;
public:
UnityField(std::uintptr_t native_ptr, std::uintptr_t native_class_ptr, const std::weak_ptr<UnityType>& native_type, const std::string_view& native_name, std::size_t native_offset, std::bitset<32> native_flags);
[[nodiscard]] auto name() const noexcept -> std::string_view;
[[nodiscard]] auto ptr() const noexcept -> std::uintptr_t;
[[nodiscard]] auto type() const noexcept -> std::weak_ptr<UnityType>;
[[nodiscard]] auto offset() const noexcept -> std::size_t;
[[nodiscard]] auto parent() const noexcept -> std::weak_ptr<UnityClass>;
[[nodiscard]] auto flags(attributes::FieldAttributes attributes) const noexcept -> bool;
[[nodiscard]] auto is_static() const noexcept -> bool;
};
class UnityMethodArgs {
mutable std::shared_mutex mutex;
std::map<std::string_view, std::weak_ptr<UnityType>> args;
std::vector<std::pair<std::weak_ptr<UnityType>, std::string_view>> param_vec;
public:
UnityMethodArgs();
~UnityMethodArgs();
explicit UnityMethodArgs(const std::map<std::string_view, std::shared_ptr<UnityType>>& types, std::vector<std::pair<std::weak_ptr<UnityType>, std::string_view>>& params);
UnityMethodArgs(UnityMethodArgs&& other) noexcept;
UnityMethodArgs(const UnityMethodArgs& other) = delete;
auto operator=(const UnityMethodArgs& other) -> UnityMethodArgs& = delete;
auto operator=(UnityMethodArgs&& other) noexcept -> UnityMethodArgs&;
auto count() const noexcept -> std::size_t;
auto map() const noexcept -> const std::map<std::string_view, std::weak_ptr<UnityType>>&;
auto params() const noexcept -> const std::vector<std::pair<std::weak_ptr<UnityType>, std::string_view>>&;
};
class UnityMethod final {
std::uintptr_t native_ptr{};
std::uintptr_t native_call_ptr{};
std::uintptr_t native_class_ptr{};
std::weak_ptr<UnityClass> native_class;
std::weak_ptr<UnityType> native_type;
std::string_view native_name;
UnityMethodArgs native_args;
std::bitset<32> native_flags;
bool native_is_static{};
bool native_is_compile{};
std::function<std::optional<void*>()> compile_call;
friend auto process::fix_class_depend() -> void;
public:
UnityMethod(std::uintptr_t native_ptr,
std::uintptr_t native_call_ptr,
std::uintptr_t native_class_ptr,
const std::weak_ptr<UnityType>& native_type,
const std::string_view& native_name,
const std::function<UnityMethodArgs()>& args_callback,
const std::function<std::optional<void*>()>& compile,
const std::bitset<32>& native_flags);
auto name() const noexcept -> std::string_view;
auto ptr() const noexcept -> std::uintptr_t;
auto call() -> std::uintptr_t;
auto type() const noexcept -> std::weak_ptr<UnityType>;
auto args() const noexcept -> const UnityMethodArgs&;
auto parent() const noexcept -> std::weak_ptr<UnityClass>;
auto flags(attributes::MethodAttributes attributes) const noexcept -> bool;
auto is_static() const noexcept -> bool;
template<typename Return, typename... Args>
auto invoke(Args&&... args) -> Return;
private:
auto compile() -> void;
};
inline UnityAssembly::UnityAssembly(const std::string_view name, const std::uintptr_t ptr, const std::function<void(std::map<std::string_view, std::shared_ptr<UnityClass>>&)>& callback) :
native_ptr{ptr},
native_name{name} {
callback(unity_class);
}
inline auto UnityAssembly::name() const noexcept -> std::string_view {
return native_name;
}
inline auto UnityAssembly::ptr() const noexcept -> std::uintptr_t {
return native_ptr;
}
inline auto UnityAssembly::content() const noexcept -> auto {
return unity_class | std::views::values | std::views::transform([](const std::shared_ptr<UnityClass>& ptr)-> std::weak_ptr<UnityClass> {
return ptr;
});
}
inline auto UnityAssembly::operator[](const std::string_view& class_name) const -> std::optional<std::weak_ptr<UnityClass>> {
return util::try_find(mutex, class_name, unity_class);
}
inline UnityType::UnityType(const std::uintptr_t native_ptr, const std::uintptr_t native_object, std::string native_name, const std::size_t native_size) :
native_ptr{native_ptr},
native_object{native_object},
native_name{std::move(native_name)},
native_size{native_size} {}
inline auto UnityType::name() const noexcept -> const std::string& {
return native_name;
}
inline auto UnityType::ptr() const noexcept -> std::uintptr_t {
return native_ptr;
}
inline auto UnityType::size() const noexcept -> std::size_t {
return native_size;
}
inline auto UnityType::type() const noexcept -> api::csharp::Type* {
return std::bit_cast<api::csharp::Type*>(native_object);
}
inline UnityClass::UnityClass(const std::uintptr_t native_ptr,
const std::weak_ptr<UnityAssembly>& native_assembly,
const std::weak_ptr<UnityType>& native_type,
const std::string_view native_name,
const std::uintptr_t native_parent_ptr,
const std::string_view native_namespace,
const std::bitset<32> native_flags,
const std::function<void(std::map<std::string_view, std::shared_ptr<UnityField>>&)>& field_callback,
const std::function<void(std::multimap<std::string_view, std::shared_ptr<UnityMethod>>&)>& method_callback) :
native_ptr{native_ptr},
native_parent_ptr{native_parent_ptr},
native_assembly{native_assembly},
native_type{native_type},
native_name{native_name},
native_namespace{native_namespace},
native_flags{native_flags} {
field_callback(unity_filed);
method_callback(unity_method);
}
inline auto UnityClass::name() const noexcept -> std::string_view {
return native_name;
}
inline auto UnityClass::ptr() const noexcept -> std::uintptr_t {
return native_ptr;
}
inline auto UnityClass::parent() const noexcept -> std::weak_ptr<UnityClass> {
return native_parent;
}
inline auto UnityClass::type() const noexcept -> std::weak_ptr<UnityType> {
return native_type;
}
inline auto UnityClass::flags(attributes::TypeAttributes attributes) const noexcept -> bool {
return native_flags.test(static_cast<uint32_t>(attributes));
}
inline auto UnityClass::name_space() const noexcept -> std::string_view {
return native_namespace;
}
inline auto UnityClass::assembly() const noexcept -> std::weak_ptr<UnityAssembly> {
return native_assembly;
}
template<ClassMember T>
auto UnityClass::count() const -> std::size_t {
const auto& container = get_container<T>();
return container.size();
}
template<ClassMember T>
auto UnityClass::content() const -> auto {
return get_container<T>() | std::views::values | std::views::transform([](const std::shared_ptr<T>& ptr)-> std::weak_ptr<T> {
return ptr;
});
}
template<ClassMember T>
auto UnityClass::operator[](util::TypeIdentity<T> /* unused */, const std::string_view name, const std::vector<std::string_view>& args_type) const -> std::optional<std::weak_ptr<T>> {
if constexpr (std::is_same_v<T, UnityField>) {
return util::try_find(mutex, name, unity_filed);
} else {
std::shared_lock _(mutex);
const auto [fst, snd] = unity_method.equal_range(name);
if (fst == snd) {
return std::nullopt;
}
if (args_type.empty()) {
return fst->second;
}
return find_with_args(args_type, std::ranges::subrange(fst, snd));
}
}
template<ClassMember U>
auto UnityClass::get_container() const -> const auto& {
if constexpr (std::is_same_v<U, UnityField>) {
return unity_filed;
} else {
return unity_method;
}
}
inline auto UnityClass::find_with_args(const std::vector<std::string_view>& args, const std::ranges::subrange<MethodIt>& range) -> std::optional<std::weak_ptr<UnityMethod>> {
auto matches = [&](const auto& entry) -> auto {
const auto& params = entry.second->args().params();
if (params.size() != args.size()) {
return false;
}
return std::ranges::all_of(std::views::iota(0U, params.size()),
[&](std::size_t i) -> bool {
auto type_ptr = params[i].first.lock();
return type_ptr && (args[i] == "*" || type_ptr->name() == args[i]);
});
};
const auto it = std::ranges::find_if(range, matches);
if (it != range.end()) {
return it->second;
}
return std::nullopt;
}
inline UnityField::UnityField(const std::uintptr_t native_ptr,
const std::uintptr_t native_class_ptr,
const std::weak_ptr<UnityType>& native_type,
const std::string_view& native_name,
const std::size_t native_offset,
const std::bitset<32> native_flags) :
native_ptr{native_ptr},
native_class_ptr{native_class_ptr},
native_type{native_type},
native_name{native_name},
native_offset{native_offset},
native_flags{native_flags},
native_is_static(flags(attributes::FieldAttributes::Static)) {}
inline auto UnityField::name() const noexcept -> std::string_view {
return native_name;
}
inline auto UnityField::ptr() const noexcept -> std::uintptr_t {
return native_ptr;
}
inline auto UnityField::type() const noexcept -> std::weak_ptr<UnityType> {
return native_type;
}
inline auto UnityField::offset() const noexcept -> std::size_t {
return native_offset;
}
inline auto UnityField::parent() const noexcept -> std::weak_ptr<UnityClass> {
return native_class;
}
inline auto UnityField::flags(attributes::FieldAttributes attributes) const noexcept -> bool {
return native_flags.test(static_cast<uint32_t>(attributes));
}
inline auto UnityField::is_static() const noexcept -> bool {
return native_is_static;
}
inline UnityMethodArgs::UnityMethodArgs() = default;
inline UnityMethodArgs::~UnityMethodArgs() = default;
inline UnityMethodArgs::UnityMethodArgs(const std::map<std::string_view, std::shared_ptr<UnityType>>& types, std::vector<std::pair<std::weak_ptr<UnityType>, std::string_view>>& params) :
args(types | std::views::transform([](const auto& pair) -> auto {
return std::pair{pair.first, std::weak_ptr<UnityType>(pair.second)};
}) | std::ranges::to<std::map>()),
param_vec{std::move(params)} {}
inline UnityMethodArgs::UnityMethodArgs(UnityMethodArgs&& other) noexcept :
args(std::move(other.args)),
param_vec(std::move(other.param_vec)) {}
inline auto UnityMethodArgs::operator=(UnityMethodArgs&& other) noexcept -> UnityMethodArgs& {
args = std::move(other.args);
param_vec = std::move(other.param_vec);
return *this;
}
inline auto UnityMethodArgs::count() const noexcept -> std::size_t {
return args.size();
}
inline auto UnityMethodArgs::map() const noexcept -> const std::map<std::string_view, std::weak_ptr<UnityType>>& {
return args;
}
inline auto UnityMethodArgs::params() const noexcept -> const std::vector<std::pair<std::weak_ptr<UnityType>, std::string_view>>& {
return param_vec;
}
inline UnityMethod::UnityMethod(const std::uintptr_t native_ptr,
const std::uintptr_t native_call_ptr,
const std::uintptr_t native_class_ptr,
const std::weak_ptr<UnityType>& native_type,
const std::string_view& native_name,
const std::function<UnityMethodArgs()>& args_callback,
const std::function<std::optional<void*>()>& compile,
const std::bitset<32>& native_flags) :
native_ptr{native_ptr},
native_call_ptr{native_call_ptr},
native_class_ptr{native_class_ptr},
native_type{native_type},
native_name{native_name},
native_flags{native_flags},
native_is_static(flags(attributes::MethodAttributes::Static)),
native_is_compile(!compile),
compile_call{compile} {
native_args = args_callback();
}
inline auto UnityMethod::name() const noexcept -> std::string_view {
return native_name;
}
inline auto UnityMethod::ptr() const noexcept -> std::uintptr_t {
return native_ptr;
}
inline auto UnityMethod::call() -> std::uintptr_t {
if (native_is_compile) {
return native_call_ptr;
}
compile();
return native_call_ptr;
}
inline auto UnityMethod::type() const noexcept -> std::weak_ptr<UnityType> {
return native_type;
}
inline auto UnityMethod::args() const noexcept -> const UnityMethodArgs& {
return native_args;
}
inline auto UnityMethod::parent() const noexcept -> std::weak_ptr<UnityClass> {
return native_class;
}
inline auto UnityMethod::flags(attributes::MethodAttributes attributes) const noexcept -> bool {
return native_flags.test(static_cast<uint32_t>(attributes));
}
inline auto UnityMethod::is_static() const noexcept -> bool {
return native_is_static;
}
template<typename Return, typename... Args>
auto UnityMethod::invoke(Args&&... args) -> Return {
// TODO: use 'std::start_lifetime_as' in c++23 if compiler support
return std::bit_cast<Return(UNITY_CALLING_CONVENTION*)(Args...)>(call())(std::forward<Args>(args)...);
}
inline auto UnityMethod::compile() -> void {
const auto result = compile_call();
if (!result.has_value()) {
throw std::runtime_error("nullptr");
}
native_call_ptr = std::bit_cast<std::uintptr_t>(*result);
native_is_compile = result.has_value();
}
namespace details {
inline UnityMode mode;
inline void* module_handle;
inline void* unity_domain;
inline std::map<std::string_view, std::shared_ptr<UnityAssembly>> unity_assembly;
inline std::map<std::uintptr_t, std::shared_ptr<UnityType>> unity_types;
inline std::map<std::uintptr_t, std::weak_ptr<UnityClass>> unity_classes;
constexpr auto method = UTYPE(UnityMethod);
constexpr auto field = UTYPE(UnityField);
template<typename Return, typename... Args>
auto invoke_dyn_library(const std::string_view& func_name, Args&&... args) -> std::optional<std::conditional_t<std::is_void_v<Return>, std::monostate, Return>> {
using OptionalResult = std::optional<std::conditional_t<std::is_void_v<Return>, std::monostate, Return>>;
using FuncPtr = Return(UNITY_CALLING_CONVENTION*)(std::decay_t<Args>...);
static std::unordered_map<std::string_view, std::uintptr_t> func_address;
if (module_handle == nullptr) {
return std::nullopt;
}
auto iterator = func_address.find(func_name);
if (iterator == func_address.end()) {
std::uintptr_t address = 0;
#ifdef WINDOWS_MODE
address = std::bit_cast<std::uintptr_t>(GetProcAddress(static_cast<HMODULE>(module_handle), func_name.data()));
#else
address = std::bit_cast<std::uintptr_t>(dlsym(module_handle, func_name.data()));
#endif
if (address == 0) {
return std::nullopt;
}
iterator = func_address.emplace(func_name, address).first;
}
// TODO: use 'std::start_lifetime_as' in c++23 if compiler support
auto func = std::bit_cast<FuncPtr>(iterator->second);
try {
if constexpr (std::is_void_v<Return>) {
func(std::forward<Args>(args)...);
return OptionalResult{std::monostate{}};
} else {
return OptionalResult{func(std::forward<Args>(args)...)};
}
} catch (...) {
return std::nullopt;
}
}
inline auto auto_select(const std::string_view il2cpp, const std::string_view mono) -> std::string_view {
if (mode == UnityMode::IL2CPP) {
return il2cpp;
}
return mono;
}
inline auto do_thread_attach_detach(const std::string_view il2cpp_name, const std::string_view mono_name, const std::string_view jit_name) -> bool {
const std::string_view call_name = auto_select(il2cpp_name, mono_name);
if (mode == UnityMode::IL2CPP) {
using namespace std::chrono_literals;
while (!*invoke_dyn_library<bool>("il2cpp_is_vm_thread", nullptr)) {
std::this_thread::sleep_for(100ms);
}
return invoke_dyn_library<void*>(call_name).has_value();
}
return invoke_dyn_library<void*>(call_name).has_value() && invoke_dyn_library<void*>(jit_name).has_value();
}
inline auto thread_attach() -> bool {
return do_thread_attach_detach("il2cpp_thread_attach", "mono_thread_attach", "mono_jit_thread_attach");
}
inline auto thread_detach() -> bool {
return do_thread_attach_detach("il2cpp_thread_detach", "mono_thread_detach", "mono_jit_thread_detach");
}
inline auto update_domain() -> bool {
const std::string_view call_name = auto_select("il2cpp_domain_get", "mono_get_root_domain");
if (const auto result = invoke_dyn_library<void*>(call_name); result.has_value()) {
unity_domain = *result;
return true;
}
return false;
}
inline auto find_assembly_impl(const std::string_view name) -> std::optional<std::weak_ptr<UnityAssembly>> {
if (!unity_assembly.contains(name)) {
return std::nullopt;
}
return unity_assembly[name];
}
inline auto try_find_class(const std::string_view assembly_name, const std::string_view class_name) -> std::optional<std::weak_ptr<UnityClass>> {
const auto assembly_result = find_assembly_impl(assembly_name);
if (!assembly_result.has_value()) {
return std::nullopt;
}
const auto& assembly = *assembly_result->lock();
return assembly[class_name];
}
template<typename Ret, typename... Args>
auto try_find_method(const std::string_view assembly_name, const std::string_view class_name, const std::string_view method_name, const std::vector<std::string_view> args = {}) -> access::Method<Ret, Args...> {
const auto class_result = try_find_class(assembly_name, class_name);
if (!class_result.has_value()) {
return {0x0};
}
const auto& class_ = *class_result->lock();
const auto method_result = class_[method, method_name, args];
if (!method_result.has_value()) {
return {0x0};
}
return {method_result->lock()->call()};
}
template<typename T>
auto new_gc_handle(T* obj, bool pinned = true) -> std::shared_ptr<T> {
if (obj == nullptr) {
return nullptr;
}
std::uint32_t handle = 0;
auto result = details::invoke_dyn_library<uint32_t>(mode == UnityMode::IL2CPP ? "il2cpp_gchandle_new" : "mono_gchandle_new", obj, pinned);
if (result.has_value()) {
handle = *result;
}
if (handle == 0) {
return nullptr;
}
return std::shared_ptr<T>(obj,
[handle](T* /*ptr*/) -> auto {
details::invoke_dyn_library<void>(mode == UnityMode::IL2CPP ? "il2cpp_gchandle_free" : "mono_gchandle_free", handle);
});
}
} // namespace details
namespace process {
namespace impl {
inline auto try_find_type(void* type_ptr) -> std::shared_ptr<UnityType>& {
auto ptr = std::bit_cast<std::uintptr_t>(type_ptr);
const auto iterator = details::unity_types.find(ptr);
if (iterator != details::unity_types.end()) {
return iterator->second;
}
const auto type_name = details::invoke_dyn_library<char*>(details::mode == UnityMode::IL2CPP ? "il2cpp_type_get_name" : "mono_type_get_name", type_ptr);
const auto type_object = details::invoke_dyn_library<char*>(details::mode == UnityMode::IL2CPP ? "il2cpp_type_get_object" : "mono_type_get_object", type_ptr);
const auto type = std::make_shared<UnityType>(ptr, reinterpret_cast<std::uintptr_t>(*type_object), *type_name, 0);
if (details::mode == UnityMode::IL2CPP) {
details::invoke_dyn_library<void>("il2cpp_free", *type_name);
}
return details::unity_types[ptr] = type;
}
inline auto try_load_args_il2cpp(void* method_ptr) -> UnityMethodArgs {
const auto param_count_result = details::invoke_dyn_library<int>("il2cpp_method_get_param_count", method_ptr);
if (!param_count_result.has_value() || *param_count_result == 0) {
return {};
}
const int param_count = *param_count_result;
std::map<std::string_view, std::shared_ptr<UnityType>> args;
std::vector<std::pair<std::weak_ptr<UnityType>, std::string_view>> params;
for (auto index = 0; index < param_count; index++) {
const auto arg_type = details::invoke_dyn_library<void*>("il2cpp_method_get_param", method_ptr, index);
const auto arg_name = details::invoke_dyn_library<const char*>("il2cpp_method_get_param_name", method_ptr, index);
if (!arg_name.has_value() || !arg_type.has_value()) {
continue;
}