using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using MonoMod;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("UnityEngine.Base.mm")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("UnityEngine.Base.mm")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("3c105af3-d405-4536-b45e-c764d6ed84c7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[Flags]
internal enum Platform
{
None = 0,
OS = 1,
X86 = 0,
X64 = 2,
NT = 4,
Unix = 8,
Unknown = 0x11,
Windows = 0x25,
MacOS = 0x49,
Linux = 0x89,
Android = 0x189,
iOS = 0x249,
Unknown64 = 0x13,
Windows64 = 0x27,
MacOS64 = 0x4B,
Linux64 = 0x8B,
Android64 = 0x18B,
iOS64 = 0x24B
}
internal static class PlatformHelper
{
public static readonly Platform Current;
static PlatformHelper()
{
PropertyInfo property = typeof(Environment).GetProperty("Platform", BindingFlags.Static | BindingFlags.NonPublic);
string text = (((object)property == null) ? Environment.OSVersion.Platform.ToString() : property.GetValue(null, new object[0]).ToString());
text = text.ToLowerInvariant();
Current = Platform.Unknown;
if (text.Contains("win"))
{
Current = Platform.Windows;
}
else if (text.Contains("mac") || text.Contains("osx"))
{
Current = Platform.MacOS;
}
else if (text.Contains("lin") || text.Contains("unix"))
{
Current = Platform.Linux;
}
if (Directory.Exists("/data") && File.Exists("/system/build.prop"))
{
Current = Platform.Android;
}
else if (Directory.Exists("/Applications") && Directory.Exists("/System"))
{
Current = Platform.iOS;
}
Current |= (Platform)((IntPtr.Size != 4) ? 2 : 0);
}
public static bool Is(Platform plat)
{
return (Current & plat) == plat;
}
}
internal static class PInvokeHelper
{
private delegate ulong d_pthread_self();
private static readonly IntPtr NULL = IntPtr.Zero;
private const int RTLD_NOW = 2;
private static IntPtr _Unity = NULL;
private static IntPtr _Mono = NULL;
private static IntPtr _PThread = NULL;
private static d_pthread_self pthread_self;
public static IntPtr Unity
{
get
{
if (_Unity != NULL)
{
return _Unity;
}
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return _Unity = GetModuleHandle(null);
}
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
IntPtr zero = IntPtr.Zero;
_Unity = dlopen(null, 2);
if ((zero = dlerror()) != IntPtr.Zero)
{
Debug.Log((object)"PInvokeHelper can't access the main assembly!");
Debug.Log((object)("dlerror: " + Marshal.PtrToStringAnsi(zero)));
return NULL;
}
return _Unity;
}
return NULL;
}
}
public static IntPtr Mono
{
get
{
if (_Mono != NULL)
{
return _Mono;
}
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return _Mono = GetModuleHandle("mono.dll");
}
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
IntPtr zero = IntPtr.Zero;
if (PlatformHelper.Is(Platform.MacOS))
{
_Mono = dlopen("./EtG_OSX.app/Contents/Frameworks/MonoEmbedRuntime/osx/libmono.0.dylib", 2);
}
else if (IntPtr.Size == 8)
{
_Mono = dlopen("./EtG_Data/Mono/x86_64/libmono.so", 2);
}
else
{
_Mono = dlopen("./EtG_Data/Mono/x86/libmono.so", 2);
}
if ((zero = dlerror()) != IntPtr.Zero)
{
Debug.Log((object)"PInvokeHelper can't access libmono.so!");
Debug.Log((object)("dlerror: " + Marshal.PtrToStringAnsi(zero)));
return NULL;
}
return _Mono;
}
return NULL;
}
}
public static IntPtr PThread
{
get
{
if (_PThread != NULL)
{
return _PThread;
}
if (Environment.OSVersion.Platform != PlatformID.Unix)
{
return NULL;
}
IntPtr zero = IntPtr.Zero;
_PThread = dlopen(PlatformHelper.Is(Platform.MacOS) ? "libpthread.dylib" : "libpthread.so.0", 2);
if ((zero = dlerror()) != IntPtr.Zero)
{
Debug.Log((object)"PInvokeHelper can't access libpthread.so!");
Debug.Log((object)("dlerror: " + Marshal.PtrToStringAnsi(zero)));
return NULL;
}
return _PThread;
}
}
public static ulong CurrentThreadId
{
get
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return GetCurrentThreadId();
}
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
d_pthread_self obj = pthread_self ?? PThread.GetDelegate<d_pthread_self>();
pthread_self = obj;
if (obj == null)
{
return 0uL;
}
return pthread_self();
}
return 0uL;
}
}
[DllImport("kernel32")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("dl")]
private static extern IntPtr dlopen([MarshalAs(UnmanagedType.LPTStr)] string filename, int flags);
[DllImport("dl")]
private static extern IntPtr dlsym(IntPtr handle, [MarshalAs(UnmanagedType.LPTStr)] string symbol);
[DllImport("dl")]
private static extern IntPtr dlerror();
public static IntPtr GetFunction(this IntPtr lib, string name)
{
if (lib == NULL)
{
return NULL;
}
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return GetProcAddress(lib, name);
}
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
IntPtr result = dlsym(lib, name);
IntPtr ptr;
if ((ptr = dlerror()) != IntPtr.Zero)
{
Debug.Log((object)("PInvokeHelper can't access " + name + "!"));
Debug.Log((object)("dlerror: " + Marshal.PtrToStringAnsi(ptr)));
return NULL;
}
return result;
}
return NULL;
}
public static T GetDelegate<T>(this IntPtr lib) where T : class
{
return lib.GetDelegate<T>(typeof(T).Name.Substring(2));
}
public static T GetDelegate<T>(this IntPtr lib, string name) where T : class
{
if (lib == NULL)
{
return null;
}
IntPtr function = lib.GetFunction(name);
if (function == NULL)
{
return null;
}
return function.AsDelegate<T>();
}
public static T GetDelegateAtRVA<T>(this IntPtr basea, long rva) where T : class
{
return new IntPtr(basea.ToInt64() + rva).AsDelegate<T>();
}
public static T AsDelegate<T>(this IntPtr s) where T : class
{
return Marshal.GetDelegateForFunctionPointer(s, typeof(T)) as T;
}
[DllImport("kernel32")]
private static extern uint GetCurrentThreadId();
}
public static class MonoDebug
{
private delegate void d_mono_debug_init(MonoDebugFormat init);
private delegate IntPtr d_mono_assembly_get_image(IntPtr assembly);
private delegate IntPtr d_mono_debug_open_image_from_memory(IntPtr image, IntPtr raw_contents, int size);
private delegate void d_mono_debug_domain_create(IntPtr domain);
private delegate void d_mono_jit_parse_options(int argc, string[] argv);
private delegate void d_mono_debugger_agent_init();
private delegate void d_runtime_initialized(IntPtr prof);
private delegate void d_appdomain_load(IntPtr prof, IntPtr domain, int result);
private delegate void d_thread_startup(IntPtr prof, ulong tid);
private delegate void d_assembly_load(IntPtr prof, IntPtr assembly, int result);
private static readonly IntPtr NULL = IntPtr.Zero;
private static Assembly _NULLASM;
private static readonly FieldInfo f_mono_assembly = typeof(Assembly).GetField("_mono_assembly", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo f_mono_app_domain = typeof(AppDomain).GetField("_mono_app_domain", BindingFlags.Instance | BindingFlags.NonPublic);
private const long WINDOWS_mono_debug_domain_create = 477960L;
private static d_mono_debug_init mono_debug_init;
private static d_mono_assembly_get_image mono_assembly_get_image;
private static d_mono_debug_open_image_from_memory mono_debug_open_image_from_memory;
private static d_mono_debug_domain_create mono_debug_domain_create;
private const long WINDOWS_mono_debugger_agent_init = 876112L;
private const long WINDOWS_runtime_initialized = 868992L;
private const long WINDOWS_appdomain_load = 869984L;
private const long WINDOWS_thread_startup = 869064L;
private const long WINDOWS_assembly_load = 870100L;
private static d_mono_jit_parse_options mono_jit_parse_options;
private static d_mono_debugger_agent_init mono_debugger_agent_init;
private static d_runtime_initialized runtime_initialized;
private static d_appdomain_load appdomain_load;
private static d_thread_startup thread_startup;
private static d_assembly_load assembly_load;
private static Assembly NULLASM
{
get
{
if ((object)_NULLASM == null)
{
_NULLASM = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("NULL"), AssemblyBuilderAccess.Run);
}
return _NULLASM;
}
}
public static bool Init(MonoDebugFormat format = MonoDebugFormat.MONO_DEBUG_FORMAT_MONO)
{
Debug.Log((object)"MonoDebug.Init!");
if (Application.isEditor || (object)Type.GetType("Mono.Runtime") == null)
{
return false;
}
Debug.Log((object)("Forcing Mono into debug mode: " + format));
mono_debug_init = mono_debug_init ?? PInvokeHelper.Mono.GetDelegate<d_mono_debug_init>();
mono_assembly_get_image = mono_assembly_get_image ?? PInvokeHelper.Mono.GetDelegate<d_mono_assembly_get_image>();
mono_debug_open_image_from_memory = mono_debug_open_image_from_memory ?? PInvokeHelper.Mono.GetDelegate<d_mono_debug_open_image_from_memory>();
mono_debug_domain_create = mono_debug_domain_create ?? PInvokeHelper.Mono.GetDelegate<d_mono_debug_domain_create>() ?? PInvokeHelper.Mono.GetDelegateAtRVA<d_mono_debug_domain_create>(477960L);
Assembly callingAssembly = Assembly.GetCallingAssembly();
IntPtr assembly = (IntPtr)f_mono_assembly.GetValue(callingAssembly);
IntPtr image = mono_assembly_get_image(assembly);
AppDomain currentDomain = AppDomain.CurrentDomain;
IntPtr domain = (IntPtr)f_mono_app_domain.GetValue(currentDomain);
Debug.Log((object)"Generating dynamic assembly to prevent mono_debug_open_image_from_memory invocation.");
IntPtr assembly2 = (IntPtr)f_mono_assembly.GetValue(NULLASM);
IntPtr image2 = mono_assembly_get_image(assembly2);
Debug.Log((object)"Precompiling mono_debug_open_image_from_memory.");
mono_debug_open_image_from_memory(image2, NULL, 0);
Debug.Log((object)"Precompiling mono_debug_domain_create.");
mono_debug_domain_create(NULL);
Debug.Log((object)"Invoking mono_debug_init.");
mono_debug_init(format);
Debug.Log((object)"Filling debug data for main domain and main assembly.");
mono_debug_domain_create(domain);
mono_debug_open_image_from_memory(image, NULL, 0);
Debug.Log((object)"Filling debug data for other assemblies.");
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
Assembly assembly3 = assemblies[i];
IntPtr assembly4 = (IntPtr)f_mono_assembly.GetValue(assembly3);
IntPtr intPtr = mono_assembly_get_image(assembly4);
Debug.Log((object)(i + ": " + assembly3.FullName + "; ASM: 0x" + assembly4.ToString("X8") + "; IMG: 0x" + intPtr.ToString("X8")));
if ((object)assembly3 == callingAssembly)
{
Debug.Log((object)"Skipping because already filled.");
}
else if (assembly3 is AssemblyBuilder)
{
Debug.Log((object)"Skipping because dynamic.");
}
else if (assembly3.GetName().Name == "mscorlib")
{
Debug.Log((object)"Skipping because mscorlib.");
}
else
{
mono_debug_open_image_from_memory(image, NULL, 0);
}
}
Debug.Log((object)"Done!");
return true;
}
public static bool SetupDebuggerAgent(string agent = null)
{
Debug.Log((object)"MonoDebug.SetupDebuggerAgent!");
if (Application.isEditor || (object)Type.GetType("Mono.Runtime") == null)
{
return false;
}
agent = agent ?? "transport=dt_socket,address=127.0.0.1:10000";
Debug.Log((object)("Telling Mono to listen to following debugger agent: " + agent));
mono_jit_parse_options = mono_jit_parse_options ?? PInvokeHelper.Mono.GetDelegate<d_mono_jit_parse_options>();
mono_jit_parse_options(1, new string[1] { "--debugger-agent=" + agent });
Debug.Log((object)"Done!");
return true;
}
public static bool InitDebuggerAgent()
{
Debug.Log((object)"MonoDebug.InitDebuggerAgent!");
if (Application.isEditor || (object)Type.GetType("Mono.Runtime") == null)
{
return false;
}
if (IntPtr.Size == 4)
{
Debug.Log((object)"x86 not supported!");
return false;
}
if (Environment.OSVersion.Platform == PlatformID.MacOSX)
{
Debug.Log((object)"Mac OS X not supported!");
return false;
}
Debug.Log((object)"Kick-starting Mono's debugger agent.");
Assembly callingAssembly = Assembly.GetCallingAssembly();
IntPtr assembly = (IntPtr)f_mono_assembly.GetValue(callingAssembly);
AppDomain currentDomain = AppDomain.CurrentDomain;
IntPtr domain = (IntPtr)f_mono_app_domain.GetValue(currentDomain);
Debug.Log((object)"mono_debugger_agent_init and the other functions are not public. Creating delegates from pointer.");
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
mono_debugger_agent_init = mono_debugger_agent_init ?? PInvokeHelper.Mono.GetDelegateAtRVA<d_mono_debugger_agent_init>(876112L);
runtime_initialized = runtime_initialized ?? PInvokeHelper.Mono.GetDelegateAtRVA<d_runtime_initialized>(868992L);
appdomain_load = appdomain_load ?? PInvokeHelper.Mono.GetDelegateAtRVA<d_appdomain_load>(869984L);
thread_startup = thread_startup ?? PInvokeHelper.Mono.GetDelegateAtRVA<d_thread_startup>(869064L);
assembly_load = assembly_load ?? PInvokeHelper.Mono.GetDelegateAtRVA<d_assembly_load>(870100L);
}
Debug.Log((object)"Running mono_debugger_agent_init and hoping that Mono won't die...");
mono_debugger_agent_init();
appdomain_load(NULL, domain, 0);
assembly_load(NULL, assembly, 0);
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly2 in assemblies)
{
IntPtr assembly3 = (IntPtr)f_mono_assembly.GetValue(assembly2);
if ((object)assembly2 != callingAssembly && !(assembly2 is AssemblyBuilder) && !(assembly2.GetName().Name == "mscorlib"))
{
assembly_load(NULL, assembly3, 0);
}
}
Debug.Log((object)("thread_startup " + PInvokeHelper.CurrentThreadId));
thread_startup(NULL, PInvokeHelper.CurrentThreadId);
Debug.Log((object)"aaand...");
runtime_initialized(NULL);
Debug.Log((object)"Done!");
return true;
}
}
public enum MonoDebugFormat
{
MONO_DEBUG_FORMAT_NONE,
MONO_DEBUG_FORMAT_MONO,
MONO_DEBUG_FORMAT_DEBUGGER
}
namespace MonoMod
{
public class MonoModConstructor : Attribute
{
}
internal class MonoModIgnore : Attribute
{
}
internal class MonoModLinkTo : Attribute
{
public MonoModLinkTo(string t)
{
}
public MonoModLinkTo(Type t)
{
}
public MonoModLinkTo(string t, string n)
{
}
public MonoModLinkTo(Type t, string n)
{
}
}
public class MonoModOriginal : Attribute
{
}
internal class MonoModOriginalName : Attribute
{
public MonoModOriginalName(string n)
{
}
}
}
namespace UnityEngine
{
public static class ETGModUnityEngineHooks
{
public static string SkipSuffix = "/SKIPHOOK";
public static Func<string, Type, Object> Load;
public static Func<string, Type, ResourceRequest> LoadAsync;
public static Func<string, Type, Object[]> LoadAll;
public static Func<Object, bool> UnloadAsset;
public static Func<Object, Object> Instantiate;
public static Action<Object> Construct;
public static Func<Component, Component> AddComponent;
}
internal class patch_Object
{
[MonoModConstructor]
public patch_Object()
{
ETGModUnityEngineHooks.Construct?.Invoke((Object)(object)((this is Object) ? this : null));
}
public static extern Object orig_Instantiate(Object original, Vector3 position, Quaternion rotation);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
Object val = orig_Instantiate(original, position, rotation);
if (ETGModUnityEngineHooks.Instantiate != null)
{
val = ETGModUnityEngineHooks.Instantiate(val);
}
return val;
}
public static extern Object orig_Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
Object val = orig_Instantiate(original, position, rotation, parent);
if (ETGModUnityEngineHooks.Instantiate != null)
{
val = ETGModUnityEngineHooks.Instantiate(val);
}
return val;
}
public static extern Object orig_Instantiate(Object original, Transform parent, bool worldPositionStays);
public static Object Instantiate(Object original, Transform parent, bool worldPositionStays)
{
Object val = orig_Instantiate(original, parent, worldPositionStays);
if (ETGModUnityEngineHooks.Instantiate != null)
{
val = ETGModUnityEngineHooks.Instantiate(val);
}
return val;
}
public static extern Object orig_Instantiate(Object original);
public static Object Instantiate(Object original)
{
Object val = orig_Instantiate(original);
if (ETGModUnityEngineHooks.Instantiate != null)
{
val = ETGModUnityEngineHooks.Instantiate(val);
}
return val;
}
public static extern T orig_Instantiate<T>(Object original, Vector3 position, Quaternion rotation) where T : Object;
public static T Instantiate<T>(T original) where T : Object
{
Object val = orig_Instantiate((Object)(object)original);
if (ETGModUnityEngineHooks.Instantiate != null)
{
val = ETGModUnityEngineHooks.Instantiate(val);
}
return (T)(object)val;
}
}
internal sealed class patch_Resources
{
[MonoModOriginal]
public static extern Object Ooad(string path, Type systemTypeInstance);
[MonoModOriginalName("Ooad")]
public static Object Load(string path, Type type)
{
if (path.EndsWith(ETGModUnityEngineHooks.SkipSuffix))
{
return Ooad(path.Substring(0, path.Length - ETGModUnityEngineHooks.SkipSuffix.Length), type);
}
return ETGModUnityEngineHooks.Load?.Invoke(path, type) ?? Ooad(path, type);
}
[MonoModOriginal]
public static extern ResourceRequest OoadAsync(string path, Type type);
[MonoModOriginalName("OoadAsync")]
public static ResourceRequest LoadAsync(string path, Type type)
{
if (path.EndsWith(ETGModUnityEngineHooks.SkipSuffix))
{
return LoadAsync(path.Substring(0, path.Length - ETGModUnityEngineHooks.SkipSuffix.Length), type);
}
return ETGModUnityEngineHooks.LoadAsync?.Invoke(path, type) ?? OoadAsync(path, type);
}
[MonoModOriginal]
public static extern Object[] OoadAll(string path, Type systemTypeInstance);
[MonoModOriginalName("OoadAll")]
public static Object[] LoadAll(string path, Type type)
{
if (path.EndsWith(ETGModUnityEngineHooks.SkipSuffix))
{
return OoadAll(path.Substring(0, path.Length - ETGModUnityEngineHooks.SkipSuffix.Length), type);
}
return ETGModUnityEngineHooks.LoadAll?.Invoke(path, type) ?? OoadAll(path, type);
}
[MonoModOriginal]
public static extern Object OetBuiltinResource(Type type, string path);
[MonoModOriginalName("OetBuiltinResource")]
public static Object GetBuiltinResource(Type type, string path)
{
return OetBuiltinResource(type, path);
}
[MonoModOriginal]
public static extern void OnloadAsset(Object assetToUnload);
[MonoModOriginalName("OnloadAsset")]
public static void UnloadAsset(Object asset)
{
bool? flag = ETGModUnityEngineHooks.UnloadAsset?.Invoke(asset);
if (!flag.HasValue || !flag.Value)
{
OnloadAsset(asset);
}
}
[MonoModOriginal]
public static extern AsyncOperation OnloadUnusedAssets();
[MonoModOriginalName("OnloadUnusedAssets")]
public static AsyncOperation UnloadUnusedAssets()
{
return OnloadUnusedAssets();
}
}
public class patch_TextAsset : TextAsset
{
public string textOverride { get; set; }
}
internal static class ETGModInstallerRenames
{
private static string[] items = new string[12]
{
"UnityEngine.Resources:Load", "Ooad", "UnityEngine.Resources:LoadAsync", "OoadAsync", "UnityEngine.Resources:LoadAll", "OoadAll", "UnityEngine.Resources:GetBuiltinResource", "OetBuiltinResource", "UnityEngine.Resources:UnloadAsset", "OnloadAsset",
"UnityEngine.Resources:UnloadUnusedAssets", "OnloadUnusedAssets"
};
}
internal class patch_GameObject
{
public extern Component orig_AddComponent(Type componentType);
public Component AddComponent(Type componentType)
{
Component val = orig_AddComponent(componentType);
if (ETGModUnityEngineHooks.AddComponent != null)
{
val = ETGModUnityEngineHooks.AddComponent(val);
}
return val;
}
}
}