This commit is contained in:
Sinai 2021-07-28 19:01:43 +10:00
parent f26371f95f
commit 602770d980
7 changed files with 58 additions and 154 deletions

View File

@ -32,16 +32,6 @@ namespace UnityExplorer
return new AssetBundle(ptr); 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<d_UnloadAllAssetBundles>("UnityEngine.AssetBundle::UnloadAllAssetBundles");
iCall.Invoke(unloadAllObjects);
}
// ~~~~~~~~~~~~ Instance ~~~~~~~~~~~~ // ~~~~~~~~~~~~ Instance ~~~~~~~~~~~~
private readonly IntPtr m_bundlePtr = IntPtr.Zero; private readonly IntPtr m_bundlePtr = IntPtr.Zero;
@ -78,7 +68,8 @@ namespace UnityExplorer
return new UnityEngine.Object(ptr).TryCast<T>(); return new UnityEngine.Object(ptr).TryCast<T>();
} }
// public extern void Unload(bool unloadAllLoadedObjects); // Unload(bool unloadAllLoadedObjects);
internal delegate void d_Unload(IntPtr _this, bool unloadAllLoadedObjects); internal delegate void d_Unload(IntPtr _this, bool unloadAllLoadedObjects);
public void Unload(bool unloadAssets = true) public void Unload(bool unloadAssets = true)

View File

@ -73,6 +73,7 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp
return ScriptableObject.CreateInstance(Il2CppType.From(type)); 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<RaycastResult> list) public override void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List<RaycastResult> list)
{ {
var il2cppList = new Il2CppSystem.Collections.Generic.List<RaycastResult>(); var il2cppList = new Il2CppSystem.Collections.Generic.List<RaycastResult>();
@ -117,22 +118,17 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp
if (!scene.isLoaded) if (!scene.isLoaded)
return new GameObject[0]; return new GameObject[0];
int handle = scene.handle; if (scene.handle == -1)
if (handle == -1)
return new GameObject[0]; return new GameObject[0];
int count = GetRootCount(handle); int count = GetRootCount(scene.handle);
if (count < 1) if (count < 1)
return new GameObject[0]; return new GameObject[0];
var list = new Il2CppSystem.Collections.Generic.List<GameObject>(count); var list = new Il2CppSystem.Collections.Generic.List<GameObject>(count);
var iCall = ICallManager.GetICall<d_GetRootGameObjects>("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal"); var iCall = ICallManager.GetICall<d_GetRootGameObjects>("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal");
iCall.Invoke(scene.handle, list.Pointer);
iCall.Invoke(handle, list.Pointer);
return list.ToArray(); return list.ToArray();
} }

View File

@ -38,19 +38,6 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp
return new Il2CppStructArray<byte>(ptr); return new Il2CppStructArray<byte>(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<byte>)data;
var iCall = ICallManager.GetICall<d_LoadImage>("UnityEngine.ImageConversion::LoadImage");
return iCall.Invoke(tex.Pointer, il2cppArray.Pointer, markNonReadable);
}
// Sprite Sprite.Create // Sprite Sprite.Create
public override Sprite CreateSprite(Texture2D texture) public override Sprite CreateSprite(Texture2D texture)

View File

@ -10,6 +10,7 @@ using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
using UnityEngine.UI; using UnityEngine.UI;
using UnityExplorer;
namespace UnityExplorer.Core.Runtime.Mono namespace UnityExplorer.Core.Runtime.Mono
{ {
@ -18,7 +19,6 @@ namespace UnityExplorer.Core.Runtime.Mono
public override void Initialize() public override void Initialize()
{ {
ExplorerCore.Context = RuntimeContext.Mono; ExplorerCore.Context = RuntimeContext.Mono;
//Reflection = new MonoReflection();
TextureUtil = new MonoTextureUtil(); TextureUtil = new MonoTextureUtil();
} }
@ -28,34 +28,23 @@ namespace UnityExplorer.Core.Runtime.Mono
} }
private void Application_logMessageReceived(string condition, string stackTrace, LogType type) private void Application_logMessageReceived(string condition, string stackTrace, LogType type)
{ => ExplorerCore.LogUnity(condition, type);
ExplorerCore.LogUnity(condition, type);
}
public override void StartCoroutine(IEnumerator routine) public override void StartCoroutine(IEnumerator routine)
{ => ExplorerBehaviour.Instance.StartCoroutine(routine);
ExplorerBehaviour.Instance.StartCoroutine(routine);
}
public override void Update() public override void Update()
{ {
} }
public override T AddComponent<T>(GameObject obj, Type type) public override T AddComponent<T>(GameObject obj, Type type)
{ => (T)obj.AddComponent(type);
return (T)obj.AddComponent(type);
}
public override ScriptableObject CreateScriptable(Type type) public override ScriptableObject CreateScriptable(Type type)
{ => ScriptableObject.CreateInstance(type);
return ScriptableObject.CreateInstance(type);
}
public override void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List<RaycastResult> list) public override void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List<RaycastResult> list)
{ => raycaster.Raycast(data, list);
raycaster.Raycast(data, list);
}
public override string LayerToName(int layer) public override string LayerToName(int layer)
=> LayerMask.LayerToName(layer); => LayerMask.LayerToName(layer);
@ -63,25 +52,11 @@ namespace UnityExplorer.Core.Runtime.Mono
public override UnityEngine.Object[] FindObjectsOfTypeAll(Type type) public override UnityEngine.Object[] FindObjectsOfTypeAll(Type type)
=> Resources.FindObjectsOfTypeAll(type); => Resources.FindObjectsOfTypeAll(type);
//private static readonly FieldInfo fi_Scene_handle = typeof(Scene).GetField("m_Handle", ReflectionUtility.AllFlags);
//public override int GetSceneHandle(Scene scene)
//{
// return (int)fi_Scene_handle.GetValue(scene);
//}
public override GameObject[] GetRootGameObjects(Scene scene) public override GameObject[] GetRootGameObjects(Scene scene)
{ => scene.isLoaded ? scene.GetRootGameObjects() : new GameObject[0];
if (!scene.isLoaded)
return new GameObject[0];
return scene.GetRootGameObjects();
}
public override int GetRootCount(Scene scene) public override int GetRootCount(Scene scene)
{ => scene.rootCount;
return scene.rootCount;
}
public override void SetColorBlock(Selectable selectable, Color? normal = null, Color? highlighted = null, Color? pressed = null, public override void SetColorBlock(Selectable selectable, Color? normal = null, Color? highlighted = null, Color? pressed = null,
Color? disabled = null) Color? disabled = null)
@ -104,58 +79,41 @@ namespace UnityExplorer.Core.Runtime.Mono
} }
public override void SetColorBlock(Selectable selectable, ColorBlock colors) public override void SetColorBlock(Selectable selectable, ColorBlock colors)
{ => selectable.colors = colors;
selectable.colors = colors;
}
} }
} }
public static class MonoExtensions public static class MonoExtensions
{ {
// Helpers to use the same style of AddListener that IL2CPP uses.
public static void AddListener(this UnityEvent _event, Action listener) public static void AddListener(this UnityEvent _event, Action listener)
{ => _event.AddListener(new UnityAction(listener));
_event.AddListener(new UnityAction(listener));
}
public static void AddListener<T>(this UnityEvent<T> _event, Action<T> listener) public static void AddListener<T>(this UnityEvent<T> _event, Action<T> listener)
{ => _event.AddListener(new UnityAction<T>(listener));
_event.AddListener(new UnityAction<T>(listener));
}
public static void RemoveListener(this UnityEvent _event, Action listener) public static void RemoveListener(this UnityEvent _event, Action listener)
{ => _event.RemoveListener(new UnityAction(listener));
_event.RemoveListener(new UnityAction(listener));
}
public static void RemoveListener<T>(this UnityEvent<T> _event, Action<T> listener) public static void RemoveListener<T>(this UnityEvent<T> _event, Action<T> listener)
{ => _event.RemoveListener(new UnityAction<T>(listener));
_event.RemoveListener(new UnityAction<T>(listener));
} // Doesn't exist in NET 3.5
public static void Clear(this StringBuilder sb) public static void Clear(this StringBuilder sb)
{ => sb.Remove(0, sb.Length);
sb.Remove(0, sb.Length);
}
private static PropertyInfo pi_childControlHeight; // 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) public static void SetChildControlHeight(this HorizontalOrVerticalLayoutGroup group, bool value)
{ => ReflectionUtility.GetPropertyInfo(typeof(HorizontalOrVerticalLayoutGroup), "childControlHeight")
if (pi_childControlHeight == null) ?.SetValue(group, value, null);
pi_childControlHeight = group.GetType().GetProperty("childControlHeight");
pi_childControlHeight?.SetValue(group, value, null);
}
private static PropertyInfo pi_childControlWidth;
public static void SetChildControlWidth(this HorizontalOrVerticalLayoutGroup group, bool value) public static void SetChildControlWidth(this HorizontalOrVerticalLayoutGroup group, bool value)
{ => ReflectionUtility.GetPropertyInfo(typeof(HorizontalOrVerticalLayoutGroup), "childControlWidth")
if (pi_childControlWidth == null) ?.SetValue(group, value, null);
pi_childControlWidth = group.GetType().GetProperty("childControlWidth");
pi_childControlWidth?.SetValue(group, value, null);
}
} }
#endif #endif

View File

@ -12,41 +12,28 @@ namespace UnityExplorer.Core.Runtime.Mono
public class MonoTextureUtil : TextureUtilProvider public class MonoTextureUtil : TextureUtilProvider
{ {
public override void Blit(Texture2D tex, RenderTexture rt) public override void Blit(Texture2D tex, RenderTexture rt)
{ => Graphics.Blit(tex, rt);
Graphics.Blit(tex, rt);
}
public override Sprite CreateSprite(Texture2D texture) public override Sprite CreateSprite(Texture2D texture)
{ => Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
}
public override bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable) //public override bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable)
{ // => tex.LoadImage(data, markNonReadable);
return tex.LoadImage(data, markNonReadable);
}
public override Texture2D NewTexture2D(int width, int height) public override Texture2D NewTexture2D(int width, int height)
{ => new Texture2D(width, height);
return new Texture2D(width, height);
}
public override byte[] EncodeToPNG(Texture2D tex) public override byte[] EncodeToPNG(Texture2D tex)
{ => EncodeToPNGSafe(tex);
return EncodeToPNGSafe(tex);
}
private static MethodInfo EncodeToPNGMethod => m_encodeToPNGMethod ?? GetEncodeToPNGMethod(); private static MethodInfo EncodeToPNGMethod => m_encodeToPNGMethod ?? GetEncodeToPNGMethod();
private static MethodInfo m_encodeToPNGMethod; private static MethodInfo m_encodeToPNGMethod;
public static byte[] EncodeToPNGSafe(Texture2D tex) public static byte[] EncodeToPNGSafe(Texture2D tex)
{ {
var method = EncodeToPNGMethod; return EncodeToPNGMethod.IsStatic
? (byte[])EncodeToPNGMethod.Invoke(null, new object[] { tex })
if (method.IsStatic) : (byte[])EncodeToPNGMethod.Invoke(tex, ArgumentUtility.EmptyArgs);
return (byte[])method.Invoke(null, new object[] { tex });
else
return (byte[])method.Invoke(tex, ArgumentUtility.EmptyArgs);
} }
private static MethodInfo GetEncodeToPNGMethod() private static MethodInfo GetEncodeToPNGMethod()

View File

@ -40,8 +40,6 @@ namespace UnityExplorer
public abstract void Update(); public abstract void Update();
//public virtual bool IsReferenceEqual(object a, object b) => ReferenceEquals(a, b);
// Unity API handlers // Unity API handlers
public abstract T AddComponent<T>(GameObject obj, Type type) where T : Component; public abstract T AddComponent<T>(GameObject obj, Type type) where T : Component;
@ -54,8 +52,6 @@ namespace UnityExplorer
public abstract void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List<RaycastResult> list); public abstract void GraphicRaycast(GraphicRaycaster raycaster, PointerEventData data, List<RaycastResult> list);
//public abstract int GetSceneHandle(Scene scene);
public abstract GameObject[] GetRootGameObjects(Scene scene); public abstract GameObject[] GetRootGameObjects(Scene scene);
public abstract int GetRootCount(Scene scene); public abstract int GetRootCount(Scene scene);

View File

@ -22,7 +22,7 @@ namespace UnityExplorer.Core.Runtime
public abstract void Blit(Texture2D tex, RenderTexture rt); 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); public abstract Sprite CreateSprite(Texture2D texture);
@ -43,27 +43,22 @@ namespace UnityExplorer.Core.Runtime
} }
} }
public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable) //public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable)
{ //{
if (!File.Exists(filePath)) // if (!File.Exists(filePath))
return false; // return false;
//
return Instance.LoadImage(tex, File.ReadAllBytes(filePath), markNonReadable); // return Instance.LoadImage(tex, File.ReadAllBytes(filePath), markNonReadable);
} //}
public static Texture2D Copy(Texture2D orig, Rect rect) public static Texture2D Copy(Texture2D orig, Rect rect)
{ {
Color[] pixels;
if (!IsReadable(orig)) if (!IsReadable(orig))
orig = ForceReadTexture(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); Texture2D newTex = Instance.NewTexture2D((int)rect.width, (int)rect.height);
newTex.SetPixels(pixels); newTex.SetPixels(pixels);
return newTex; return newTex;
} }
@ -92,7 +87,7 @@ namespace UnityExplorer.Core.Runtime
} }
catch (Exception e) catch (Exception e)
{ {
ExplorerCore.Log("Exception on ForceReadTexture: " + e.ToString()); ExplorerCore.Log($"Exception on ForceReadTexture: {e.ToString()}");
return default; return default;
} }
} }
@ -103,13 +98,11 @@ namespace UnityExplorer.Core.Runtime
Directory.CreateDirectory(dir); Directory.CreateDirectory(dir);
byte[] data; byte[] data;
string savepath = dir + @"\" + name + ".png"; string savepath = $@"{dir}\{name}.png";
// Make sure we can EncodeToPNG it. // Make sure we can EncodeToPNG it.
if (tex.format != TextureFormat.ARGB32 || !IsReadable(tex)) if (tex.format != TextureFormat.ARGB32 || !IsReadable(tex))
{
tex = ForceReadTexture(tex); tex = ForceReadTexture(tex);
}
if (isDTXnmNormal) if (isDTXnmNormal)
{ {
@ -120,13 +113,9 @@ namespace UnityExplorer.Core.Runtime
data = Instance.EncodeToPNG(tex); data = Instance.EncodeToPNG(tex);
if (data == null || !data.Any()) if (data == null || !data.Any())
{
ExplorerCore.LogWarning("Couldn't get any data for the texture!"); ExplorerCore.LogWarning("Couldn't get any data for the texture!");
}
else else
{
File.WriteAllBytes(savepath, data); File.WriteAllBytes(savepath, data);
}
} }
// Converts DTXnm-format Normal Map to RGBA-format Normal Map. // Converts DTXnm-format Normal Map to RGBA-format Normal Map.