From 602770d980d85c703d56bfbe6312543edf534b29 Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Wed, 28 Jul 2021 19:01:43 +1000 Subject: [PATCH] Cleanup --- src/Core/Runtime/Il2Cpp/AssetBundle.cs | 13 +-- src/Core/Runtime/Il2Cpp/Il2CppProvider.cs | 12 +-- src/Core/Runtime/Il2Cpp/Il2CppTextureUtil.cs | 13 --- src/Core/Runtime/Mono/MonoProvider.cs | 106 ++++++------------- src/Core/Runtime/Mono/MonoTextureUtil.cs | 31 ++---- src/Core/Runtime/RuntimeProvider.cs | 4 - src/Core/Runtime/TextureUtilProvider.cs | 33 ++---- 7 files changed, 58 insertions(+), 154 deletions(-) diff --git a/src/Core/Runtime/Il2Cpp/AssetBundle.cs b/src/Core/Runtime/Il2Cpp/AssetBundle.cs index 52dbb0f..8055878 100644 --- a/src/Core/Runtime/Il2Cpp/AssetBundle.cs +++ b/src/Core/Runtime/Il2Cpp/AssetBundle.cs @@ -32,16 +32,6 @@ namespace UnityExplorer return new AssetBundle(ptr); } - // static void UnloadAllAssetBundles(bool unloadAllObjects); - - internal delegate void d_UnloadAllAssetBundles(bool unloadAllObjects); - - public static void UnloadAllAssetBundles(bool unloadAllObjects) - { - var iCall = ICallManager.GetICall("UnityEngine.AssetBundle::UnloadAllAssetBundles"); - iCall.Invoke(unloadAllObjects); - } - // ~~~~~~~~~~~~ Instance ~~~~~~~~~~~~ private readonly IntPtr m_bundlePtr = IntPtr.Zero; @@ -78,7 +68,8 @@ namespace UnityExplorer return new UnityEngine.Object(ptr).TryCast(); } - // public extern void Unload(bool unloadAllLoadedObjects); + // Unload(bool unloadAllLoadedObjects); + internal delegate void d_Unload(IntPtr _this, bool unloadAllLoadedObjects); public void Unload(bool unloadAssets = true) diff --git a/src/Core/Runtime/Il2Cpp/Il2CppProvider.cs b/src/Core/Runtime/Il2Cpp/Il2CppProvider.cs index 5b4e7b4..daefa33 100644 --- a/src/Core/Runtime/Il2Cpp/Il2CppProvider.cs +++ b/src/Core/Runtime/Il2Cpp/Il2CppProvider.cs @@ -73,6 +73,7 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp return ScriptableObject.CreateInstance(Il2CppType.From(type)); } + // Pretty disgusting but couldn't figure out a cleaner way yet unfortunately public override void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List list) { var il2cppList = new Il2CppSystem.Collections.Generic.List(); @@ -117,22 +118,17 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp if (!scene.isLoaded) return new GameObject[0]; - int handle = scene.handle; - - if (handle == -1) + if (scene.handle == -1) return new GameObject[0]; - int count = GetRootCount(handle); + int count = GetRootCount(scene.handle); if (count < 1) return new GameObject[0]; var list = new Il2CppSystem.Collections.Generic.List(count); - var iCall = ICallManager.GetICall("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal"); - - iCall.Invoke(handle, list.Pointer); - + iCall.Invoke(scene.handle, list.Pointer); return list.ToArray(); } diff --git a/src/Core/Runtime/Il2Cpp/Il2CppTextureUtil.cs b/src/Core/Runtime/Il2Cpp/Il2CppTextureUtil.cs index 973ae8d..eb811a7 100644 --- a/src/Core/Runtime/Il2Cpp/Il2CppTextureUtil.cs +++ b/src/Core/Runtime/Il2Cpp/Il2CppTextureUtil.cs @@ -38,19 +38,6 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp return new Il2CppStructArray(ptr); } - // bool ImageConversion.LoadImage(this Texture2D tex, byte[] data, bool markNonReadable); - - internal delegate bool d_LoadImage(IntPtr tex, IntPtr data, bool markNonReadable); - - public override bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable) - { - var il2cppArray = (Il2CppStructArray)data; - - var iCall = ICallManager.GetICall("UnityEngine.ImageConversion::LoadImage"); - - return iCall.Invoke(tex.Pointer, il2cppArray.Pointer, markNonReadable); - } - // Sprite Sprite.Create public override Sprite CreateSprite(Texture2D texture) diff --git a/src/Core/Runtime/Mono/MonoProvider.cs b/src/Core/Runtime/Mono/MonoProvider.cs index 0084e99..4d0b5f1 100644 --- a/src/Core/Runtime/Mono/MonoProvider.cs +++ b/src/Core/Runtime/Mono/MonoProvider.cs @@ -10,6 +10,7 @@ using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; using UnityEngine.UI; +using UnityExplorer; namespace UnityExplorer.Core.Runtime.Mono { @@ -18,7 +19,6 @@ namespace UnityExplorer.Core.Runtime.Mono public override void Initialize() { ExplorerCore.Context = RuntimeContext.Mono; - //Reflection = new MonoReflection(); TextureUtil = new MonoTextureUtil(); } @@ -28,60 +28,35 @@ namespace UnityExplorer.Core.Runtime.Mono } private void Application_logMessageReceived(string condition, string stackTrace, LogType type) - { - ExplorerCore.LogUnity(condition, type); - } + => ExplorerCore.LogUnity(condition, type); - public override void StartCoroutine(IEnumerator routine) - { - ExplorerBehaviour.Instance.StartCoroutine(routine); - } + public override void StartCoroutine(IEnumerator routine) + => ExplorerBehaviour.Instance.StartCoroutine(routine); public override void Update() { - } - public override T AddComponent(GameObject obj, Type type) - { - return (T)obj.AddComponent(type); - } + public override T AddComponent(GameObject obj, Type type) + => (T)obj.AddComponent(type); - public override ScriptableObject CreateScriptable(Type type) - { - return ScriptableObject.CreateInstance(type); - } + public override ScriptableObject CreateScriptable(Type type) + => ScriptableObject.CreateInstance(type); public override void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List list) - { - raycaster.Raycast(data, list); - } + => raycaster.Raycast(data, list); - public override string LayerToName(int layer) + public override string LayerToName(int layer) => LayerMask.LayerToName(layer); - public override UnityEngine.Object[] FindObjectsOfTypeAll(Type type) + public override UnityEngine.Object[] FindObjectsOfTypeAll(Type type) => Resources.FindObjectsOfTypeAll(type); - //private static readonly FieldInfo fi_Scene_handle = typeof(Scene).GetField("m_Handle", ReflectionUtility.AllFlags); + public override GameObject[] GetRootGameObjects(Scene scene) + => scene.isLoaded ? scene.GetRootGameObjects() : new GameObject[0]; - //public override int GetSceneHandle(Scene scene) - //{ - // return (int)fi_Scene_handle.GetValue(scene); - //} - - public override GameObject[] GetRootGameObjects(Scene scene) - { - if (!scene.isLoaded) - return new GameObject[0]; - - return scene.GetRootGameObjects(); - } - - public override int GetRootCount(Scene scene) - { - return scene.rootCount; - } + public override int GetRootCount(Scene scene) + => scene.rootCount; public override void SetColorBlock(Selectable selectable, Color? normal = null, Color? highlighted = null, Color? pressed = null, Color? disabled = null) @@ -103,59 +78,42 @@ namespace UnityExplorer.Core.Runtime.Mono SetColorBlock(selectable, colors); } - public override void SetColorBlock(Selectable selectable, ColorBlock colors) - { - selectable.colors = colors; - } + public override void SetColorBlock(Selectable selectable, ColorBlock colors) + => selectable.colors = colors; } } public static class MonoExtensions { + // Helpers to use the same style of AddListener that IL2CPP uses. + public static void AddListener(this UnityEvent _event, Action listener) - { - _event.AddListener(new UnityAction(listener)); - } + => _event.AddListener(new UnityAction(listener)); public static void AddListener(this UnityEvent _event, Action listener) - { - _event.AddListener(new UnityAction(listener)); - } + => _event.AddListener(new UnityAction(listener)); public static void RemoveListener(this UnityEvent _event, Action listener) - { - _event.RemoveListener(new UnityAction(listener)); - } + => _event.RemoveListener(new UnityAction(listener)); public static void RemoveListener(this UnityEvent _event, Action listener) - { - _event.RemoveListener(new UnityAction(listener)); - } + => _event.RemoveListener(new UnityAction(listener)); - public static void Clear(this StringBuilder sb) - { - sb.Remove(0, sb.Length); - } + // Doesn't exist in NET 3.5 - private static PropertyInfo pi_childControlHeight; + public static void Clear(this StringBuilder sb) + => sb.Remove(0, sb.Length); + + // These properties don't exist in some earlier games, so null check before trying to set them. public static void SetChildControlHeight(this HorizontalOrVerticalLayoutGroup group, bool value) - { - if (pi_childControlHeight == null) - pi_childControlHeight = group.GetType().GetProperty("childControlHeight"); + => ReflectionUtility.GetPropertyInfo(typeof(HorizontalOrVerticalLayoutGroup), "childControlHeight") + ?.SetValue(group, value, null); - pi_childControlHeight?.SetValue(group, value, null); - } - - private static PropertyInfo pi_childControlWidth; public static void SetChildControlWidth(this HorizontalOrVerticalLayoutGroup group, bool value) - { - if (pi_childControlWidth == null) - pi_childControlWidth = group.GetType().GetProperty("childControlWidth"); - - pi_childControlWidth?.SetValue(group, value, null); - } + => ReflectionUtility.GetPropertyInfo(typeof(HorizontalOrVerticalLayoutGroup), "childControlWidth") + ?.SetValue(group, value, null); } #endif \ No newline at end of file diff --git a/src/Core/Runtime/Mono/MonoTextureUtil.cs b/src/Core/Runtime/Mono/MonoTextureUtil.cs index f04da58..331b308 100644 --- a/src/Core/Runtime/Mono/MonoTextureUtil.cs +++ b/src/Core/Runtime/Mono/MonoTextureUtil.cs @@ -12,41 +12,28 @@ namespace UnityExplorer.Core.Runtime.Mono public class MonoTextureUtil : TextureUtilProvider { public override void Blit(Texture2D tex, RenderTexture rt) - { - Graphics.Blit(tex, rt); - } + => Graphics.Blit(tex, rt); public override Sprite CreateSprite(Texture2D texture) - { - return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); - } + => Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); - public override bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable) - { - return tex.LoadImage(data, markNonReadable); - } + //public override bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable) + // => tex.LoadImage(data, markNonReadable); public override Texture2D NewTexture2D(int width, int height) - { - return new Texture2D(width, height); - } + => new Texture2D(width, height); public override byte[] EncodeToPNG(Texture2D tex) - { - return EncodeToPNGSafe(tex); - } + => EncodeToPNGSafe(tex); private static MethodInfo EncodeToPNGMethod => m_encodeToPNGMethod ?? GetEncodeToPNGMethod(); private static MethodInfo m_encodeToPNGMethod; public static byte[] EncodeToPNGSafe(Texture2D tex) { - var method = EncodeToPNGMethod; - - if (method.IsStatic) - return (byte[])method.Invoke(null, new object[] { tex }); - else - return (byte[])method.Invoke(tex, ArgumentUtility.EmptyArgs); + return EncodeToPNGMethod.IsStatic + ? (byte[])EncodeToPNGMethod.Invoke(null, new object[] { tex }) + : (byte[])EncodeToPNGMethod.Invoke(tex, ArgumentUtility.EmptyArgs); } private static MethodInfo GetEncodeToPNGMethod() diff --git a/src/Core/Runtime/RuntimeProvider.cs b/src/Core/Runtime/RuntimeProvider.cs index 615e97b..2f6a098 100644 --- a/src/Core/Runtime/RuntimeProvider.cs +++ b/src/Core/Runtime/RuntimeProvider.cs @@ -40,8 +40,6 @@ namespace UnityExplorer public abstract void Update(); - //public virtual bool IsReferenceEqual(object a, object b) => ReferenceEquals(a, b); - // Unity API handlers public abstract T AddComponent(GameObject obj, Type type) where T : Component; @@ -54,8 +52,6 @@ namespace UnityExplorer public abstract void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List list); - //public abstract int GetSceneHandle(Scene scene); - public abstract GameObject[] GetRootGameObjects(Scene scene); public abstract int GetRootCount(Scene scene); diff --git a/src/Core/Runtime/TextureUtilProvider.cs b/src/Core/Runtime/TextureUtilProvider.cs index 16b5c3c..3cecfc2 100644 --- a/src/Core/Runtime/TextureUtilProvider.cs +++ b/src/Core/Runtime/TextureUtilProvider.cs @@ -22,7 +22,7 @@ namespace UnityExplorer.Core.Runtime public abstract void Blit(Texture2D tex, RenderTexture rt); - public abstract bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable); + //public abstract bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable); public abstract Sprite CreateSprite(Texture2D texture); @@ -43,27 +43,22 @@ namespace UnityExplorer.Core.Runtime } } - public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable) - { - if (!File.Exists(filePath)) - return false; - - return Instance.LoadImage(tex, File.ReadAllBytes(filePath), markNonReadable); - } + //public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable) + //{ + // if (!File.Exists(filePath)) + // return false; + // + // return Instance.LoadImage(tex, File.ReadAllBytes(filePath), markNonReadable); + //} public static Texture2D Copy(Texture2D orig, Rect rect) { - Color[] pixels; - if (!IsReadable(orig)) orig = ForceReadTexture(orig); - pixels = orig.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height); - + Color[] pixels = orig.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height); Texture2D newTex = Instance.NewTexture2D((int)rect.width, (int)rect.height); - newTex.SetPixels(pixels); - return newTex; } @@ -92,7 +87,7 @@ namespace UnityExplorer.Core.Runtime } catch (Exception e) { - ExplorerCore.Log("Exception on ForceReadTexture: " + e.ToString()); + ExplorerCore.Log($"Exception on ForceReadTexture: {e.ToString()}"); return default; } } @@ -103,13 +98,11 @@ namespace UnityExplorer.Core.Runtime Directory.CreateDirectory(dir); byte[] data; - string savepath = dir + @"\" + name + ".png"; + string savepath = $@"{dir}\{name}.png"; // Make sure we can EncodeToPNG it. if (tex.format != TextureFormat.ARGB32 || !IsReadable(tex)) - { tex = ForceReadTexture(tex); - } if (isDTXnmNormal) { @@ -120,13 +113,9 @@ namespace UnityExplorer.Core.Runtime data = Instance.EncodeToPNG(tex); if (data == null || !data.Any()) - { ExplorerCore.LogWarning("Couldn't get any data for the texture!"); - } else - { File.WriteAllBytes(savepath, data); - } } // Converts DTXnm-format Normal Map to RGBA-format Normal Map.