-
Notifications
You must be signed in to change notification settings - Fork 44
/
02_OtherStructures.cpp
202 lines (168 loc) · 7.13 KB
/
02_OtherStructures.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <BNM/UserSettings/GlobalSettings.hpp>
#include <BNM/Class.hpp>
#include <BNM/Field.hpp>
#include <BNM/Method.hpp>
#include <BNM/Property.hpp>
#include <BNM/Operators.hpp>
#include <BNM/BasicMonoStructures.hpp>
#include <BNM/ComplexMonoStructures.hpp>
#include <BNM/ClassesManagement.hpp>
using namespace BNM::Structures::Unity; // Vector3, Vector2 etc.
using namespace BNM::Structures; // Mono::String, Mono::Array etc.
void MonoArray() {
// Type[] - an array of any objects
Mono::Array<int> *array = nullptr;
//! It can be created in two ways
// * Array<Тип>::Create(размер или std::vector<Тип>) - does not get into the garbage collector
// * LoadClass().NewArray<Тип>(размер) - gets into the garbage collector
// Let's create it in 1 way
array = Mono::Array<int>::Create(10);
//! You can get the data using
auto dataPtr = array->GetData(); // Pointer to the C array
// or
auto dataVec = array->ToVector(); // std::vector<int>
// or
auto firstData = array->At(0); // First element of the array
}
void MonoList() {
// System.Collections.Generic.List<Тип> - a list of any objects
Mono::List<int> *list = nullptr;
//! It can only be created through a class:
// LoadClass().NewList<Type>(size) - gets into the garbage collector
// LoadClass().NewListUnsafe<Type>(size) - gets into the garbage collector, but all methods are handled by BNM
//! To avoid searching for the System.Int32 class (the int value class in C#)
//! You can use BNM::Defaults::Get<Type>()
//! BNM::Defaults::Get supports only basic types
auto intClass = BNM::Defaults::Get<int>().ToClass();
list = intClass.NewList<int>();
//! You can get the data using
auto dataPtr = list->GetData(); // Pointer to the C array
// or
auto dataVec = list->ToVector(); // std::vector<int>
// or
auto firstData = list->At(0); // The first element of the array
}
void MonoDictionary() {
// System.Collections.Generic.Dictionary<Key type, Value type> - dictionary
Mono::Dictionary<int, int> *dictionary;
// For more information about generic, see example 03
auto dictionaryClass = BNM::Class(BNM_OBFUSCATE("System.Collections.Generic"), BNM_OBFUSCATE("Dictionary`2"), BNM::Image(BNM_OBFUSCATE("mscorlib.dll")));
auto dictionary_int_int_Class = dictionaryClass.GetGeneric({BNM::Defaults::Get<int>(), BNM::Defaults::Get<int>()});
dictionary = (Mono::Dictionary<int, int> *) dictionary_int_int_Class.CreateNewObjectParameters();
//! You can get the data using
auto keys = dictionary->GetKeys(); // std::vector<Key type>
// or
auto values = dictionary->GetValues(); // std::vector<Value type>
// or
auto map = dictionary->ToMap(); // std::map<Key type, Value type>
// or
int value = 0;
if (dictionary->TryGet(1, &value))
; // Value found
}
// The example below can be compiled in Unity and it will work
namespace DelegatesAndActions {
// C# class
/*
public class Delegates : MonoBehaviour {
public delegate int JustDelegate(int x, int y);
public JustDelegate justDelegateDef;
public UnityAction<int, int> JustUnityAction;
public Action<int, int> JustAction;
public UnityEvent<int, int> JustEvent;
void Start() {
justDelegateDef += delegate(int x, int y) {
Log($"justDelegateDef(1) x: {x}, y: {y}");
return 1;
};
justDelegateDef += delegate(int x, int y) {
Log($"justDelegateDef(3) x: {x}, y: {y}");
return 3;
};
justDelegateDef += delegate(int x, int y) {
Log($"justDelegateDef(500) x: {x}, y: {y}");
return 500;
};
JustAction += delegate(int x, int y) {
Log($"JustAction x: {x}, y: {y}");
return;
};
JustUnityAction += delegate(int x, int y) {
Log($"JustUnityAction x: {x}, y: {y}");
return;
};
JustEvent.AddListener(delegate(int x, int y) {
Log($"JustEvent x: {x}, y: {y}");
return;
});
// Just output of messages
logClass = new AndroidJavaClass("android.util.Log");
}
// Just output of messages
private AndroidJavaClass logClass;
void Log(string s) { logClass.CallStatic<int>("e", "BNM_TargetApp", s); }
}
*/
// ClassesManagement is used here. It is described in more detail in Example 05.
struct Delegates : BNM::UnityEngine::MonoBehaviour {
BNM::MulticastDelegate<int> *justDelegateDef;
BNM::UnityEngine::UnityAction<int, int> *JustUnityAction;
BNM::Structures::Mono::Action<int, int> *JustAction;
BNM::UnityEngine::UnityEvent<int, int> *JustEvent;
void *logClass;
BNM_CustomClass(Delegates, BNM::CompileTimeClassBuilder(nullptr, BNM_OBFUSCATE("Delegates")).Build(), {}, {});
void Start() {
BNM_CallCustomMethodOrigin(Start, this);
BNM_LOG_DEBUG("justDelegateDef: %p", justDelegateDef);
BNM_LOG_DEBUG("JustUnityAction: %p", JustUnityAction);
BNM_LOG_DEBUG("JustAction: %p", JustAction);
BNM_LOG_DEBUG("JustEvent: %p", JustEvent);
if (justDelegateDef) justDelegateDef->Invoke(10, 60);
if (JustUnityAction) JustUnityAction->Invoke(70, 9);
if (JustAction) JustAction->Invoke(30, 42);
if (JustEvent) JustEvent->Invoke(7, 234);
}
BNM_CustomMethod(Start, false, BNM::Defaults::Get<void>(), BNM_OBFUSCATE("Start"));
BNM_CustomMethodSkipTypeMatch(Start);
BNM_CustomMethodMarkAsInvokeHook(Start);
};
// You will see something like this in log:
/*
ByNameModding justDelegateDef: 0x7986ef6900
ByNameModding JustUnityAction: 0x7986ef67e0
ByNameModding JustAction: 0x7986ef6870
ByNameModding JustEvent: 0x7986eea480
BNM_TargetApp justDelegateDef(1) x: 10, y: 60
BNM_TargetApp justDelegateDef(3) x: 10, y: 60
BNM_TargetApp justDelegateDef(500) x: 10, y: 60
BNM_TargetApp JustUnityAction x: 70, y: 9
BNM_TargetApp JustAction x: 30, y: 42
BNM_TargetApp JustEvent x: 7, y: 234
*/
}
void OnLoaded_Example_02() {
using namespace BNM;
//! Unity structures
// Mathematical structures
// Mathematical operations similar to those in Unity can be performed on these structures
Vector2 vector2;
Vector3 vector3;
Vector4 vector4;
Matrix3x3 matrix3x3;
Matrix4x4 matrix4x4;
Quaternion quaternion;
// Structures for Raycast
Ray ray;
RaycastHit raycastHit;
//! Mono structures
//! System.String, for more information, see Example 01
Mono::String *string;
//! Method describes Array
MonoArray();
//! Method describes List
MonoList();
//! Method describes Dictionary
MonoDictionary();
//! Namespace describes Delegates and Actions
using namespace DelegatesAndActions;
}