Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 602770d

Browse files
committed
Cleanup
1 parent f26371f commit 602770d

File tree

7 files changed

+58
-154
lines changed

7 files changed

+58
-154
lines changed

src/Core/Runtime/Il2Cpp/AssetBundle.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ public static AssetBundle LoadFromMemory(byte[] binary, uint crc = 0)
3232
return new AssetBundle(ptr);
3333
}
3434

35-
// static void UnloadAllAssetBundles(bool unloadAllObjects);
36-
37-
internal delegate void d_UnloadAllAssetBundles(bool unloadAllObjects);
38-
39-
public static void UnloadAllAssetBundles(bool unloadAllObjects)
40-
{
41-
var iCall = ICallManager.GetICall<d_UnloadAllAssetBundles>("UnityEngine.AssetBundle::UnloadAllAssetBundles");
42-
iCall.Invoke(unloadAllObjects);
43-
}
44-
4535
// ~~~~~~~~~~~~ Instance ~~~~~~~~~~~~
4636

4737
private readonly IntPtr m_bundlePtr = IntPtr.Zero;
@@ -78,7 +68,8 @@ public T LoadAsset<T>(string name) where T : UnityEngine.Object
7868
return new UnityEngine.Object(ptr).TryCast<T>();
7969
}
8070

81-
// public extern void Unload(bool unloadAllLoadedObjects);
71+
// Unload(bool unloadAllLoadedObjects);
72+
8273
internal delegate void d_Unload(IntPtr _this, bool unloadAllLoadedObjects);
8374

8475
public void Unload(bool unloadAssets = true)

src/Core/Runtime/Il2Cpp/Il2CppProvider.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public override ScriptableObject CreateScriptable(Type type)
7373
return ScriptableObject.CreateInstance(Il2CppType.From(type));
7474
}
7575

76+
// Pretty disgusting but couldn't figure out a cleaner way yet unfortunately
7677
public override void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List<RaycastResult> list)
7778
{
7879
var il2cppList = new Il2CppSystem.Collections.Generic.List<RaycastResult>();
@@ -117,22 +118,17 @@ public override GameObject[] GetRootGameObjects(Scene scene)
117118
if (!scene.isLoaded)
118119
return new GameObject[0];
119120

120-
int handle = scene.handle;
121-
122-
if (handle == -1)
121+
if (scene.handle == -1)
123122
return new GameObject[0];
124123

125-
int count = GetRootCount(handle);
124+
int count = GetRootCount(scene.handle);
126125

127126
if (count < 1)
128127
return new GameObject[0];
129128

130129
var list = new Il2CppSystem.Collections.Generic.List<GameObject>(count);
131-
132130
var iCall = ICallManager.GetICall<d_GetRootGameObjects>("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal");
133-
134-
iCall.Invoke(handle, list.Pointer);
135-
131+
iCall.Invoke(scene.handle, list.Pointer);
136132
return list.ToArray();
137133
}
138134

src/Core/Runtime/Il2Cpp/Il2CppTextureUtil.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,6 @@ public override byte[] EncodeToPNG(Texture2D tex)
3838
return new Il2CppStructArray<byte>(ptr);
3939
}
4040

41-
// bool ImageConversion.LoadImage(this Texture2D tex, byte[] data, bool markNonReadable);
42-
43-
internal delegate bool d_LoadImage(IntPtr tex, IntPtr data, bool markNonReadable);
44-
45-
public override bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable)
46-
{
47-
var il2cppArray = (Il2CppStructArray<byte>)data;
48-
49-
var iCall = ICallManager.GetICall<d_LoadImage>("UnityEngine.ImageConversion::LoadImage");
50-
51-
return iCall.Invoke(tex.Pointer, il2cppArray.Pointer, markNonReadable);
52-
}
53-
5441
// Sprite Sprite.Create
5542

5643
public override Sprite CreateSprite(Texture2D texture)

src/Core/Runtime/Mono/MonoProvider.cs

Lines changed: 32 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using UnityEngine.EventSystems;
1111
using UnityEngine.SceneManagement;
1212
using UnityEngine.UI;
13+
using UnityExplorer;
1314

1415
namespace UnityExplorer.Core.Runtime.Mono
1516
{
@@ -18,7 +19,6 @@ public class MonoProvider : RuntimeProvider
1819
public override void Initialize()
1920
{
2021
ExplorerCore.Context = RuntimeContext.Mono;
21-
//Reflection = new MonoReflection();
2222
TextureUtil = new MonoTextureUtil();
2323
}
2424

@@ -28,60 +28,35 @@ public override void SetupEvents()
2828
}
2929

3030
private void Application_logMessageReceived(string condition, string stackTrace, LogType type)
31-
{
32-
ExplorerCore.LogUnity(condition, type);
33-
}
31+
=> ExplorerCore.LogUnity(condition, type);
3432

35-
public override void StartCoroutine(IEnumerator routine)
36-
{
37-
ExplorerBehaviour.Instance.StartCoroutine(routine);
38-
}
33+
public override void StartCoroutine(IEnumerator routine)
34+
=> ExplorerBehaviour.Instance.StartCoroutine(routine);
3935

4036
public override void Update()
4137
{
42-
4338
}
4439

45-
public override T AddComponent<T>(GameObject obj, Type type)
46-
{
47-
return (T)obj.AddComponent(type);
48-
}
40+
public override T AddComponent<T>(GameObject obj, Type type)
41+
=> (T)obj.AddComponent(type);
4942

50-
public override ScriptableObject CreateScriptable(Type type)
51-
{
52-
return ScriptableObject.CreateInstance(type);
53-
}
43+
public override ScriptableObject CreateScriptable(Type type)
44+
=> ScriptableObject.CreateInstance(type);
5445

5546
public override void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List<RaycastResult> list)
56-
{
57-
raycaster.Raycast(data, list);
58-
}
47+
=> raycaster.Raycast(data, list);
5948

60-
public override string LayerToName(int layer)
49+
public override string LayerToName(int layer)
6150
=> LayerMask.LayerToName(layer);
6251

63-
public override UnityEngine.Object[] FindObjectsOfTypeAll(Type type)
52+
public override UnityEngine.Object[] FindObjectsOfTypeAll(Type type)
6453
=> Resources.FindObjectsOfTypeAll(type);
6554

66-
//private static readonly FieldInfo fi_Scene_handle = typeof(Scene).GetField("m_Handle", ReflectionUtility.AllFlags);
67-
68-
//public override int GetSceneHandle(Scene scene)
69-
//{
70-
// return (int)fi_Scene_handle.GetValue(scene);
71-
//}
55+
public override GameObject[] GetRootGameObjects(Scene scene)
56+
=> scene.isLoaded ? scene.GetRootGameObjects() : new GameObject[0];
7257

73-
public override GameObject[] GetRootGameObjects(Scene scene)
74-
{
75-
if (!scene.isLoaded)
76-
return new GameObject[0];
77-
78-
return scene.GetRootGameObjects();
79-
}
80-
81-
public override int GetRootCount(Scene scene)
82-
{
83-
return scene.rootCount;
84-
}
58+
public override int GetRootCount(Scene scene)
59+
=> scene.rootCount;
8560

8661
public override void SetColorBlock(Selectable selectable, Color? normal = null, Color? highlighted = null, Color? pressed = null,
8762
Color? disabled = null)
@@ -103,59 +78,42 @@ public override void SetColorBlock(Selectable selectable, Color? normal = null,
10378
SetColorBlock(selectable, colors);
10479
}
10580

106-
public override void SetColorBlock(Selectable selectable, ColorBlock colors)
107-
{
108-
selectable.colors = colors;
109-
}
81+
public override void SetColorBlock(Selectable selectable, ColorBlock colors)
82+
=> selectable.colors = colors;
11083
}
11184
}
11285

11386
public static class MonoExtensions
11487
{
88+
// Helpers to use the same style of AddListener that IL2CPP uses.
89+
11590
public static void AddListener(this UnityEvent _event, Action listener)
116-
{
117-
_event.AddListener(new UnityAction(listener));
118-
}
91+
=> _event.AddListener(new UnityAction(listener));
11992

12093
public static void AddListener<T>(this UnityEvent<T> _event, Action<T> listener)
121-
{
122-
_event.AddListener(new UnityAction<T>(listener));
123-
}
94+
=> _event.AddListener(new UnityAction<T>(listener));
12495

12596
public static void RemoveListener(this UnityEvent _event, Action listener)
126-
{
127-
_event.RemoveListener(new UnityAction(listener));
128-
}
97+
=> _event.RemoveListener(new UnityAction(listener));
12998

13099
public static void RemoveListener<T>(this UnityEvent<T> _event, Action<T> listener)
131-
{
132-
_event.RemoveListener(new UnityAction<T>(listener));
133-
}
100+
=> _event.RemoveListener(new UnityAction<T>(listener));
134101

135-
public static void Clear(this StringBuilder sb)
136-
{
137-
sb.Remove(0, sb.Length);
138-
}
102+
// Doesn't exist in NET 3.5
139103

140-
private static PropertyInfo pi_childControlHeight;
104+
public static void Clear(this StringBuilder sb)
105+
=> sb.Remove(0, sb.Length);
141106

142-
public static void SetChildControlHeight(this HorizontalOrVerticalLayoutGroup group, bool value)
143-
{
144-
if (pi_childControlHeight == null)
145-
pi_childControlHeight = group.GetType().GetProperty("childControlHeight");
107+
// These properties don't exist in some earlier games, so null check before trying to set them.
146108

147-
pi_childControlHeight?.SetValue(group, value, null);
148-
}
109+
public static void SetChildControlHeight(this HorizontalOrVerticalLayoutGroup group, bool value)
110+
=> ReflectionUtility.GetPropertyInfo(typeof(HorizontalOrVerticalLayoutGroup), "childControlHeight")
111+
?.SetValue(group, value, null);
149112

150-
private static PropertyInfo pi_childControlWidth;
151113

152114
public static void SetChildControlWidth(this HorizontalOrVerticalLayoutGroup group, bool value)
153-
{
154-
if (pi_childControlWidth == null)
155-
pi_childControlWidth = group.GetType().GetProperty("childControlWidth");
156-
157-
pi_childControlWidth?.SetValue(group, value, null);
158-
}
115+
=> ReflectionUtility.GetPropertyInfo(typeof(HorizontalOrVerticalLayoutGroup), "childControlWidth")
116+
?.SetValue(group, value, null);
159117
}
160118

161119
#endif

src/Core/Runtime/Mono/MonoTextureUtil.cs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,28 @@ namespace UnityExplorer.Core.Runtime.Mono
1212
public class MonoTextureUtil : TextureUtilProvider
1313
{
1414
public override void Blit(Texture2D tex, RenderTexture rt)
15-
{
16-
Graphics.Blit(tex, rt);
17-
}
15+
=> Graphics.Blit(tex, rt);
1816

1917
public override Sprite CreateSprite(Texture2D texture)
20-
{
21-
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
22-
}
18+
=> Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
2319

24-
public override bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable)
25-
{
26-
return tex.LoadImage(data, markNonReadable);
27-
}
20+
//public override bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable)
21+
// => tex.LoadImage(data, markNonReadable);
2822

2923
public override Texture2D NewTexture2D(int width, int height)
30-
{
31-
return new Texture2D(width, height);
32-
}
24+
=> new Texture2D(width, height);
3325

3426
public override byte[] EncodeToPNG(Texture2D tex)
35-
{
36-
return EncodeToPNGSafe(tex);
37-
}
27+
=> EncodeToPNGSafe(tex);
3828

3929
private static MethodInfo EncodeToPNGMethod => m_encodeToPNGMethod ?? GetEncodeToPNGMethod();
4030
private static MethodInfo m_encodeToPNGMethod;
4131

4232
public static byte[] EncodeToPNGSafe(Texture2D tex)
4333
{
44-
var method = EncodeToPNGMethod;
45-
46-
if (method.IsStatic)
47-
return (byte[])method.Invoke(null, new object[] { tex });
48-
else
49-
return (byte[])method.Invoke(tex, ArgumentUtility.EmptyArgs);
34+
return EncodeToPNGMethod.IsStatic
35+
? (byte[])EncodeToPNGMethod.Invoke(null, new object[] { tex })
36+
: (byte[])EncodeToPNGMethod.Invoke(tex, ArgumentUtility.EmptyArgs);
5037
}
5138

5239
private static MethodInfo GetEncodeToPNGMethod()

src/Core/Runtime/RuntimeProvider.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public static void Init() =>
4040

4141
public abstract void Update();
4242

43-
//public virtual bool IsReferenceEqual(object a, object b) => ReferenceEquals(a, b);
44-
4543
// Unity API handlers
4644

4745
public abstract T AddComponent<T>(GameObject obj, Type type) where T : Component;
@@ -54,8 +52,6 @@ public static void Init() =>
5452

5553
public abstract void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List<RaycastResult> list);
5654

57-
//public abstract int GetSceneHandle(Scene scene);
58-
5955
public abstract GameObject[] GetRootGameObjects(Scene scene);
6056

6157
public abstract int GetRootCount(Scene scene);

0 commit comments

Comments
 (0)