diff --git a/src/Inspectors/Reflection/CacheObject/CacheMethod.cs b/src/Inspectors/Reflection/CacheObject/CacheMethod.cs index 0b0fc92..5e1ed88 100644 --- a/src/Inspectors/Reflection/CacheObject/CacheMethod.cs +++ b/src/Inspectors/Reflection/CacheObject/CacheMethod.cs @@ -71,6 +71,9 @@ namespace UnityExplorer.Inspectors.Reflection } catch (Exception e) { + while (e.InnerException != null) + e = e.InnerException; + ExplorerCore.LogWarning($"Exception evaluating: {e.GetType()}, {e.Message}"); ReflectionException = ReflectionHelpers.ExceptionToString(e); } diff --git a/src/Tests/Tests.cs b/src/Tests/Tests.cs index 74a7ac0..d1d38e1 100644 --- a/src/Tests/Tests.cs +++ b/src/Tests/Tests.cs @@ -3,7 +3,11 @@ using System.Collections.Generic; using UnityExplorer.UI; using UnityEngine; using System; +using System.Runtime.InteropServices; +using System.Text; #if CPP +using UnhollowerBaseLib; +using UnityExplorer.Helpers; #endif namespace UnityExplorer.Tests @@ -19,7 +23,6 @@ namespace UnityExplorer.Tests "three", }; public static void StaticMethod() { } - } public class TestClass @@ -140,7 +143,8 @@ namespace UnityExplorer.Tests } #if CPP - TestTexture = UIManager.MakeSolidTexture(Color.white, 1000, 600); + //TestTexture = UIManager.MakeSolidTexture(Color.white, 1000, 600); + TestTexture = new Texture(); TestTexture.name = "TestTexture"; var r = new Rect(0, 0, TestTexture.width, TestTexture.height); diff --git a/src/UI/UIManager.cs b/src/UI/UIManager.cs index 0fda6db..69de64a 100644 --- a/src/UI/UIManager.cs +++ b/src/UI/UIManager.cs @@ -149,37 +149,5 @@ namespace UnityExplorer.UI return rootObj; } - - public static Sprite CreateSprite(Texture2D tex, Rect size = default) - { -#if CPP - Vector2 pivot = Vector2.zero; - Vector4 border = Vector4.zero; - - if (size == default) - { - size = new Rect(0, 0, tex.width, tex.height); - } - - return Sprite.CreateSprite_Injected(tex, ref size, ref pivot, 100f, 0u, SpriteMeshType.Tight, ref border, false); -#else - return Sprite.Create(tex, size, Vector2.zero); -#endif - } - - public static Texture2D MakeSolidTexture(Color color, int width, int height) - { - Color[] pixels = new Color[width * height]; - for (int i = 0; i < pixels.Length; i++) - { - pixels[i] = color; - } - - Texture2D tex = new Texture2D(width, height); - tex.SetPixels(pixels); - tex.Apply(); - - return tex; - } } } diff --git a/src/Unstrip/ImageConversionUnstrip.cs b/src/Unstrip/ImageConversionUnstrip.cs index e08e581..582c80c 100644 --- a/src/Unstrip/ImageConversionUnstrip.cs +++ b/src/Unstrip/ImageConversionUnstrip.cs @@ -10,6 +10,16 @@ namespace UnityExplorer.Unstrip { public static class ImageConversionUnstrip { + // LoadImage helper from a filepath + + public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable) + { + if (!File.Exists(filePath)) + return false; + + return tex.LoadImage(File.ReadAllBytes(filePath), markNonReadable); + } + #if CPP // byte[] ImageConversion.EncodeToPNG(this Texture2D image); @@ -17,8 +27,12 @@ namespace UnityExplorer.Unstrip public static byte[] EncodeToPNG(this Texture2D tex) { - IntPtr ptr = ICallHelper.GetICall("UnityEngine.ImageConversion::EncodeToPNG") - .Invoke(tex.Pointer); + var iCall = ICallHelper.GetICall("UnityEngine.ImageConversion::EncodeToPNG"); + + IntPtr ptr = iCall.Invoke(tex.Pointer); + + if (ptr == IntPtr.Zero) + return null; return new Il2CppStructArray(ptr); } @@ -38,19 +52,23 @@ namespace UnityExplorer.Unstrip return ret; } -#endif - // Helper for LoadImage from filepath + // Sprite Sprite.Create - public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable) + internal delegate IntPtr d_CreateSprite(IntPtr texture, ref Rect rect, ref Vector2 pivot, float pixelsPerUnit, + uint extrude, int meshType, ref Vector4 border, bool generateFallbackPhysicsShape); + + public static Sprite CreateSprite(Texture texture, Rect rect, Vector2 pivot, float pixelsPerUnit, uint extrude, Vector4 border) { - if (!File.Exists(filePath)) - { - return false; - } + var iCall = ICallHelper.GetICall("UnityEngine.Sprite::CreateSprite_Injected"); - byte[] data = File.ReadAllBytes(filePath); - return tex.LoadImage(data, markNonReadable); + var ptr = iCall.Invoke(texture.Pointer, ref rect, ref pivot, pixelsPerUnit, extrude, 1, ref border, false); + + if (ptr == IntPtr.Zero) + return null; + else + return new Sprite(ptr); } +#endif } } \ No newline at end of file