Repository navigation
Expand file tree
/
Copy pathUnityUtils.ts
More file actions
186 lines (149 loc) · 6.9 KB
/
Copy pathUnityUtils.ts
File metadata and controls
186 lines (149 loc) · 6.9 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
import { AssemblyHelper } from "../core/AssemblyHelper";
import { Logger } from "../logger/Logger";
export class UnityUtils {
private static readonly tag = "UnityUtils";
// Classes
private static Resources: Il2Cpp.Class;
private static Vector3: Il2Cpp.Class;
private static Vector2: Il2Cpp.Class;
private static Object: Il2Cpp.Class;
private static SceneManager: Il2Cpp.Class;
private static SystemBoolean: Il2Cpp.Class;
private static SystemString: Il2Cpp.Class;
private static SystemInt: Il2Cpp.Class;
static SystemActionBool: Il2Cpp.Class;
static SystemActionBoolInt: Il2Cpp.Class;
static SystemActionBoolString: Il2Cpp.Class;
static GenericListString: Il2Cpp.Class;
static init() {
this.Resources = AssemblyHelper.CoreModule.class("UnityEngine.Resources");
this.Vector3 = AssemblyHelper.CoreModule.class("UnityEngine.Vector3");
this.Vector2 = AssemblyHelper.CoreModule.class("UnityEngine.Vector2");
this.Object = AssemblyHelper.CoreModule.class("UnityEngine.Object");
this.SceneManager = AssemblyHelper.CoreModule.class("UnityEngine.SceneManagement.SceneManager");
this.SystemBoolean = Il2Cpp.corlib.class("System.Boolean");
this.SystemString = Il2Cpp.corlib.class("System.String");
this.SystemInt = Il2Cpp.corlib.class("System.Int32");
this.SystemActionBool = Il2Cpp.corlib.class("System.Action`1").inflate(this.SystemBoolean);
this.SystemActionBoolInt = Il2Cpp.corlib.class("System.Action`2").inflate(this.SystemBoolean, this.SystemInt);
this.SystemActionBoolString = Il2Cpp.corlib.class("System.Action`2").inflate(this.SystemBoolean, this.SystemString);
this.GenericListString = Il2Cpp.corlib.class("System.Collections.Generic.List`1").inflate(this.SystemString);
Logger.info(`[${this.tag}::init] Initialized`);
}
/** Wrapper over UnityEngine::SceneManagement::SceneManager::LoadScene */
static LoadScene(scene: string): void {
this.SceneManager.method<void>("LoadScene", 1).invoke(Il2Cpp.string(scene));
}
/** Wrapper over UnityEngine::Resources::FindObjectsOfTypeAll */
static FindObjectsOfTypeAll(klass: Il2Cpp.Class): Il2Cpp.Array<Il2Cpp.Object> {
return this.Resources.method<Il2Cpp.Array<Il2Cpp.Object>>("FindObjectsOfTypeAll", 1).invoke(klass.type.object);
}
/** Wrapper over UnityEngine::Object::FindObjectsOfType */
static FindObjectsOfType(klass: Il2Cpp.Class): Il2Cpp.Array<Il2Cpp.Object> {
return this.Object.method<Il2Cpp.Array<Il2Cpp.Object>>("FindObjectsOfType", 1).invoke(klass.type.object);
}
/** Wrapper over UnityEngine::Object::GetInstanceID */
static GetInstanceID(object: Il2Cpp.Object): number {
return object.method<number>("GetInstanceID").invoke();
}
/** Wrapper over UnityEngine::Object::FindObjectFromInstanceID */
static FindObjectFromInstanceID(id: number): Il2Cpp.Object | undefined {
const object = this.Object.method<Il2Cpp.Object>("FindObjectFromInstanceID", 1).invoke(id);
if (object.isNull()) return undefined;
return object;
}
/** Wrapper over klass::get_Instance */
static getInstance(klass: Il2Cpp.Class): Il2Cpp.Object | undefined {
const instanceMethod = klass.tryMethod<Il2Cpp.Object>("get_Instance");
if (!instanceMethod) {
Logger.error(`[${this.tag}::getInstance] ${klass.name} is missing get_Instance`);
return undefined;
}
return instanceMethod.invoke();
}
/** Wrapper over UnityEngine::Vector3::.ctor */
static createVector3(x: number, y: number, z: number): Il2Cpp.ValueType {
return this.createInstance(this.Vector3, x, y, z).unbox();
}
/** Wrapper over UnityEngine::Vector2::.ctor */
static createVector2(x: number, y: number): Il2Cpp.ValueType {
return this.createInstance(this.Vector2, x, y).unbox();
}
/** Wrapper over System.Collections.Generic.List<Il2Cpp.String> */
static createStringList(items: string[]): Il2Cpp.Object {
const list = UnityUtils.createInstance(this.GenericListString);
const Add = list.method("Add");
for (const item of items) {
Add.invoke(Il2Cpp.string(item));
}
return list;
}
/** Wrapper over constructor.name */
static getTypeName(object: any): string {
return object.constructor.name;
}
/** Wrapper over UnityEngine::Compontent::get_gameObject */
static getGameObject(component: Il2Cpp.Object): Il2Cpp.Object {
return component.method<Il2Cpp.Object>("get_gameObject").invoke();
}
/** Wrapper over UnityEngine::Compontent::get_transform */
static getTransform(component: Il2Cpp.Object): Il2Cpp.Object {
return component.method<Il2Cpp.Object>("get_transform").invoke();
}
/**
* Wrapper over UnityEngine::Transform::set_localScale
*
* @param scale UnityEngine::Vector3
*/
static setLocalScale(transform: Il2Cpp.Object, scale: Il2Cpp.ValueType): void {
transform.method("set_localScale").invoke(scale);
}
/** Wrapper over UnityEngine::GameObject::SetActive */
static SetActive(gameObject: Il2Cpp.Object, active: boolean): void {
gameObject.method<void>("SetActive").invoke(active);
}
/** Creates a callback wrapper over UnityUtils.runInMain */
static run<T extends any[]>(action: (...args: T) => void): (...args: T) => void {
return (...args: T) => {
UnityUtils.runInMain(() => action(...args));
};
}
/**
* Wrapper over Il2Cpp.perform(block, "main")
*
* From Java.scheduleOnMainThread you need call from main thread
*/
static runInMain<T>(block: () => T | Promise<T>): Promise<T> {
return Il2Cpp.perform(block, "main");
}
/**
* Wrapper over klass.alloc(), .ctor(...parameters)
*
* @param parameters args for .ctor()
*/
static createInstance(klass: Il2Cpp.Class, ...parameters: Il2Cpp.Parameter.Type[]): Il2Cpp.Object {
const instance = klass.alloc();
instance.method(".ctor").invoke(...parameters);
return instance;
}
/**
* Wrapper over ::GetComponent
*
* @param object UnityEngine::Component or UnityEngine::GameObject
* @param componentClass component to find
*/
static getComponentFromObject(object: Il2Cpp.Object, componentClass: Il2Cpp.Class): Il2Cpp.Object | undefined {
const componentClassType = componentClass.type.object;
const component = object.method<Il2Cpp.Object>("GetComponent", 1).invoke(componentClassType);
if (component.isNull()) return undefined;
return component;
}
/**
* Wrapper over set_enabled in component
*
* Make sure components has it. I'm too lazy
*/
static setEnabledComponent(component: Il2Cpp.Object, state: boolean): void {
return component.method<void>("set_enabled").invoke(state);
}
}