More progress, cleanups and fixes

This commit is contained in:
Sinai
2021-05-04 20:10:46 +10:00
parent 302ff29e36
commit a89d66cf81
30 changed files with 807 additions and 176 deletions

View File

@ -52,8 +52,6 @@ namespace UnityExplorer.Core.Config
{
try
{
ExplorerCore.Log("Loading internal data");
if (!File.Exists(INI_PATH))
return false;
@ -67,8 +65,6 @@ namespace UnityExplorer.Core.Config
configElement.BoxedValue = StringToConfigValue(config.Value, configElement.ElementType);
}
ExplorerCore.Log("Loaded");
return true;
}
catch (Exception ex)

View File

@ -78,13 +78,6 @@ namespace UnityExplorer.Core.Input
}
}
public static void UpdateIfNeeded()
{
if ((!ShouldActuallyUnlock && (Cursor.visible || Cursor.lockState == CursorLockMode.None))
|| (ShouldActuallyUnlock && (!Cursor.visible || Cursor.lockState != CursorLockMode.None)))
UpdateCursorControl();
}
public static void UpdateCursorControl()
{
try
@ -143,9 +136,6 @@ namespace UnityExplorer.Core.Input
public static void ReleaseEventSystem()
{
if (EventSystem.current != UIManager.EventSys)
return;
if (InputManager.CurrentType == InputType.InputSystem)
return;
@ -225,4 +215,198 @@ namespace UnityExplorer.Core.Input
}
}
}
}
}
// Was rewriting but something broke, not looking into it right now.
//using System;
//using UnityEngine;
//using UnityEngine.EventSystems;
//using UnityExplorer.Core.Input;
//using BF = System.Reflection.BindingFlags;
//using UnityExplorer.Core.Config;
//using UnityExplorer.Core;
//using UnityExplorer.UI;
//using System.Collections;
//#if ML
//using Harmony;
//#else
//using HarmonyLib;
//#endif
//namespace UnityExplorer.Core.Input
//{
// public class CursorUnlocker
// {
// public static bool Unlock
// {
// get => unlock;
// set
// {
// unlock = value;
// UpdateCursorControl();
// }
// }
// private static bool unlock;
// public static bool ShouldActuallyUnlock => UIManager.ShowMenu && Unlock;
// private static CursorLockMode lastLockMode;
// private static bool lastVisibleState;
// private static bool currentlySetting = false;
// private static Type CursorType
// => cursorType
// ?? (cursorType = ReflectionUtility.GetTypeByName("UnityEngine.Cursor"));
// private static Type cursorType;
// public static void Init()
// {
// SetupPatches();
// UpdateCursorControl();
// Unlock = ConfigManager.Force_Unlock_Mouse.Value;
// ConfigManager.Force_Unlock_Mouse.OnValueChanged += (bool val) => { Unlock = val; };
// if (ConfigManager.Aggressive_Force_Unlock.Value)
// RuntimeProvider.Instance.StartCoroutine(ForceUnlockCoroutine());
// }
// private static readonly WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame();
// private static IEnumerator ForceUnlockCoroutine()
// {
// while (true)
// {
// yield return _waitForEndOfFrame;
// UpdateCursorControl();
// }
// }
// public static void UpdateCursorControl()
// {
// currentlySetting = true;
// if (ShouldActuallyUnlock)
// {
// if (Cursor.lockState != CursorLockMode.None)
// Cursor.lockState = CursorLockMode.None;
// if (!Cursor.visible)
// Cursor.visible = true;
// SetEventSystem();
// }
// else
// {
// if (Cursor.lockState != lastLockMode)
// Cursor.lockState = lastLockMode;
// if (Cursor.visible != lastVisibleState)
// Cursor.visible = lastVisibleState;
// ReleaseEventSystem();
// }
// currentlySetting = false;
// }
// // Event system overrides
// private static bool m_settingEventSystem;
// private static EventSystem m_lastEventSystem;
// private static BaseInputModule m_lastInputModule;
// public static void SetEventSystem()
// {
// if (InputManager.CurrentType == InputType.InputSystem
// || !UIManager.EventSys
// || EventSystem.current == UIManager.EventSys)
// return;
// if (EventSystem.current && EventSystem.current != UIManager.EventSys)
// {
// m_lastEventSystem = EventSystem.current;
// m_lastEventSystem.enabled = false;
// }
// // Set to our current system
// m_settingEventSystem = true;
// UIManager.EventSys.enabled = true;
// EventSystem.current = UIManager.EventSys;
// InputManager.ActivateUIModule();
// m_settingEventSystem = false;
// }
// public static void ReleaseEventSystem()
// {
// if (InputManager.CurrentType == InputType.InputSystem
// || !UIManager.EventSys
// || EventSystem.current != UIManager.EventSys
// || !m_lastEventSystem)
// return;
// if (m_lastEventSystem.gameObject.activeInHierarchy)
// {
// m_lastEventSystem.enabled = true;
// m_settingEventSystem = true;
// EventSystem.current = m_lastEventSystem;
// m_lastInputModule?.ActivateModule();
// m_settingEventSystem = false;
// }
// }
// // Patches
// private static void SetupPatches()
// {
// if (CursorType == null)
// throw new Exception("Could not load Type 'UnityEngine.Cursor'!");
// lastLockMode = Cursor.lockState;
// lastVisibleState = Cursor.visible;
// // Let mod loader handle actual patching (only necessary until ML 0.3.1)
// ExplorerCore.Loader.SetupCursorPatches();
// }
// public static void Prefix_EventSystem_set_current(ref EventSystem value)
// {
// if (!m_settingEventSystem && value != UIManager.EventSys)
// {
// m_lastEventSystem = value;
// m_lastInputModule = value?.currentInputModule;
// if (ShouldActuallyUnlock)
// {
// value = UIManager.EventSys;
// value.enabled = true;
// }
// }
// }
// public static void Prefix_set_lockState(ref CursorLockMode value)
// {
// if (!currentlySetting)
// {
// lastLockMode = value;
// if (ShouldActuallyUnlock)
// value = CursorLockMode.None;
// }
// }
// public static void Prefix_set_visible(ref bool value)
// {
// if (!currentlySetting)
// {
// lastVisibleState = value;
// if (ShouldActuallyUnlock)
// value = true;
// }
// }
// }
//}

View File

@ -57,7 +57,13 @@ namespace UnityExplorer
/// <param name="obj">The object to cast</param>
/// <returns>The object, cast to the underlying Type if possible, otherwise the original object.</returns>
public static object TryCast(this object obj)
=> ReflectionProvider.Instance.Cast(obj, GetActualType(obj));
{
var type = GetActualType(obj);
if (type.IsValueType)
return obj;
return ReflectionProvider.Instance.Cast(obj, type);
}
/// <summary>
/// Cast an object to a Type, if possible.
@ -66,10 +72,19 @@ namespace UnityExplorer
/// <param name="castTo">The Type to cast to </param>
/// <returns>The object, cast to the Type provided if possible, otherwise the original object.</returns>
public static object TryCast(this object obj, Type castTo)
=> ReflectionProvider.Instance.Cast(obj, castTo);
{
if (castTo.IsValueType)
return obj;
return ReflectionProvider.Instance.Cast(obj, castTo);
}
public static T TryCast<T>(this object obj)
=> ReflectionProvider.Instance.TryCast<T>(obj);
{
var type = typeof(T);
if (type.IsValueType)
return (T)obj;
return ReflectionProvider.Instance.TryCast<T>(obj);
}
/// <summary>
/// Check if the provided Type is assignable to IEnumerable.

View File

@ -19,9 +19,7 @@ namespace UnityExplorer
public static AssetBundle LoadFromFile(string path)
{
var iCall = ICallManager.GetICall<d_LoadFromFile>("UnityEngine.AssetBundle::LoadFromFile_Internal");
var ptr = iCall.Invoke(IL2CPP.ManagedStringToIl2Cpp(path), 0u, 0UL);
return new AssetBundle(ptr);
}
@ -30,12 +28,20 @@ namespace UnityExplorer
public static AssetBundle LoadFromMemory(byte[] binary, uint crc = 0)
{
var iCall = ICallManager.GetICall<d_LoadFromMemory>("UnityEngine.AssetBundle::LoadFromMemory_Internal");
var ptr = iCall(((Il2CppStructArray<byte>) binary).Pointer, crc);
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 ~~~~~~~~~~~~
private readonly IntPtr m_bundlePtr = IntPtr.Zero;
@ -71,6 +77,15 @@ namespace UnityExplorer
return new UnityEngine.Object(ptr).TryCast<T>();
}
// public extern void Unload(bool unloadAllLoadedObjects);
internal delegate void d_Unload(IntPtr _this, bool unloadAllLoadedObjects);
public void Unload(bool unloadAssets = true)
{
var iCall = ICallManager.GetICall<d_Unload>("UnityEngine.AssetBundle::Unload");
iCall.Invoke(this.m_bundlePtr, unloadAssets);
}
}
}
#endif

View File

@ -133,35 +133,37 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp
var type = obj.GetType();
if (obj is Il2CppSystem.Object cppObject)
try
{
// weird specific case - if the object is an Il2CppSystem.Type, then return so manually.
if (cppObject is CppType)
return typeof(CppType);
if (!string.IsNullOrEmpty(type.Namespace))
if ((Il2CppSystem.Object)obj is Il2CppSystem.Object cppObject)
{
// Il2CppSystem-namespace objects should just return GetType,
// because using GetIl2CppType returns the System namespace type instead.
if (type.Namespace.StartsWith("System.") || type.Namespace.StartsWith("Il2CppSystem."))
return cppObject.GetType();
// weird specific case - if the object is an Il2CppSystem.Type, then return so manually.
if (cppObject is CppType)
return typeof(CppType);
if (type.FullName.StartsWith("System.") || type.FullName.StartsWith("Il2CppSystem."))
return type;
var cppType = cppObject.GetIl2CppType();
// check if type is injected
IntPtr classPtr = il2cpp_object_get_class(cppObject.Pointer);
if (RuntimeSpecificsStore.IsInjected(classPtr))
{
var typeByName = ReflectionUtility.GetTypeByName(cppType.FullName);
if (typeByName != null)
return typeByName;
}
// this should be fine for all other il2cpp objects
var getType = GetMonoType(cppType);
if (getType != null)
return getType;
}
var cppType = cppObject.GetIl2CppType();
// check if type is injected
IntPtr classPtr = il2cpp_object_get_class(cppObject.Pointer);
if (RuntimeSpecificsStore.IsInjected(classPtr))
{
var typeByName = ReflectionUtility.GetTypeByName(cppType.FullName);
if (typeByName != null)
return typeByName;
}
// this should be fine for all other il2cpp objects
var getType = GetMonoType(cppType);
if (getType != null)
return getType;
}
catch // (Exception ex)
{
// ExplorerCore.LogWarning("Exception in GetActualType: " + ex);
}
return type;

View File

@ -103,6 +103,11 @@ namespace UnityExplorer.Tests
public static Il2CppSystem.Collections.Hashtable testHashset;
public static Il2CppSystem.Collections.Generic.List<Il2CppSystem.Object> testList;
//public static Il2CppSystem.Nullable<Quaternion> NullableQuaternion;
//public static Il2CppSystem.Nullable<int> NullableInt = new Il2CppSystem.Nullable<int>(5);
//public static Il2CppSystem.Nullable<bool> NullableBool = new Il2CppSystem.Nullable<bool>(false);
#endif
static TestClass()
@ -111,6 +116,9 @@ namespace UnityExplorer.Tests
BigList.Add(i.ToString());
#if CPP
//NullableQuaternion = new Il2CppSystem.Nullable<Quaternion>();
//NullableQuaternion.value = Quaternion.identity;
testHashset = new Il2CppSystem.Collections.Hashtable();
testHashset.Add("key1", "itemOne");
testHashset.Add("key2", "itemTwo");