Beispiel #1
0
        private static Il2CppSystem.Type TypeFromPointerInternal(IntPtr classPointer, string typeName, bool throwOnFailure)
        {
            if (classPointer == IntPtr.Zero)
            {
                if (throwOnFailure)
                {
                    throw new ArgumentException($"{typeName} does not have a corresponding IL2CPP class pointer");
                }
                else
                {
                    return(null);
                }
            }
            var il2CppType = IL2CPP.il2cpp_class_get_type(classPointer);

            if (il2CppType == IntPtr.Zero)
            {
                if (throwOnFailure)
                {
                    throw new ArgumentException($"{typeName} does not have a corresponding IL2CPP type pointer");
                }
                else
                {
                    return(null);
                }
            }
            return(Il2CppSystem.Type.internal_from_handle(il2CppType));
        }
Beispiel #2
0
        /// <summary>
        /// Version of TryCast without the generic restriction
        /// </summary>
        private static bool TryCast <T>(Il2CppObjectBase obj, out T t)
        {
            t = default;
            var nativeClassPtr = Il2CppClassPointerStore <T> .NativeClassPtr;

            if (nativeClassPtr == IntPtr.Zero)
            {
                MelonLogger.Warning($"{typeof(T)} is not an Il2Cpp reference type");
                return(false);
            }

            var num = IL2CPP.il2cpp_object_get_class(obj.Pointer);

            if (!IL2CPP.il2cpp_class_is_assignable_from(nativeClassPtr, num))
            {
                MelonLogger.Warning($"{obj.GetType()} is not a {typeof(T)}");
                return(false);
            }

            if (RuntimeSpecificsStore.IsInjected(num))
            {
                t = (T)ClassInjectorBase.GetMonoObjectFromIl2CppPointer(obj.Pointer);
                return(true);
            }

            var type = Il2CppClassPointerStore <T> .CreatedTypeRedirect;

            if ((object)type == null)
            {
                type = typeof(T);
            }

            t = (T)Activator.CreateInstance(type, obj.Pointer);
            return(true);
        }
        public override void OnApplicationStart()
        {
            ModPrefs.RegisterCategory(ModCategory, "Mirror Resolution");
            ModPrefs.RegisterPrefInt(ModCategory, MaxResPref, 4096, "Max eye texture size");
            ModPrefs.RegisterPrefInt(ModCategory, MirrorMsaaPref, 0, "Mirror MSAA (0=default)");
            ModPrefs.RegisterPrefBool(ModCategory, AllMirrorsAutoPref, false, "Force auto resolution");

            unsafe
            {
                var methodInfo            = Il2CppType.Of <VRC_MirrorReflection>().GetMethod(nameof(VRC_MirrorReflection.GetReflectionData), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                var originalMethodPointer = *(IntPtr *)IL2CPP.il2cpp_method_get_from_reflection(methodInfo.Pointer);
                CompatHook((IntPtr)(&originalMethodPointer), typeof(MirrorResolutionUnlimiterMod).GetMethod(nameof(GetReflectionData), System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) !.MethodHandle.GetFunctionPointer());
            }

            OnModSettingsApplied();

            if (AppDomain.CurrentDomain.GetAssemblies().Any(it => it.GetName().Name.StartsWith("UIExpansionKit")))
            {
                MelonModLogger.Log("Adding UIExpansionKit buttons");
                typeof(UiExtensionsAddon)
                .GetMethod(nameof(UiExtensionsAddon.Init),
                           System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static) !
                .Invoke(null, new object[0]);
            }
        }
        public static IntPtr GetNestedTypeViaReflection(IntPtr enclosingClass, string nestedTypeName)
        {
            var reflectionType = Type.internal_from_handle(IL2CPP.il2cpp_class_get_type(enclosingClass));
            var nestedType     = reflectionType.GetNestedType(nestedTypeName, BindingFlags.Public | BindingFlags.NonPublic);

            return(nestedType != null?IL2CPP.il2cpp_class_from_system_type(nestedType.Pointer) : IntPtr.Zero);
        }
Beispiel #5
0
        public static void Setup()
        {
            IL2CPP.il2cpp_thread_attach(IL2CPP.il2cpp_domain_get());
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Console.WriteLine(Environment.Version);
            Console.WriteLine(Application.unityVersion);
            Console.WriteLine(Directory.GetCurrentDirectory());
            UnhollowerBaseLib.Runtime.UnityVersionHandler.Initialize(2018, 4, 20);
            LogSupport.RemoveAllHandlers();
            LogSupport.TraceHandler   += LogSupport_TraceHandler;
            LogSupport.ErrorHandler   += LogSupport_TraceHandler;
            LogSupport.InfoHandler    += LogSupport_TraceHandler;
            LogSupport.WarningHandler += LogSupport_TraceHandler;

            ClassInjector.Detour = new DoHookDetour();
            //ClassInjector.DoHook?.GetInvocationList().ToList().ForEach(d => ClassInjector.DoHook -= (Action<IntPtr, IntPtr>)d);
            //ClassInjector.DoHook += JmpPatch;
            ClassInjector.RegisterTypeInIl2Cpp <ModManager>();
            while (BaseObject = GameObject.Find("ModManager"))
            {
                GameObject.DestroyImmediate(BaseObject);
            }
            BaseObject = new GameObject("ModManager");
            GameObject.DontDestroyOnLoad(BaseObject);
            var modMgr = BaseObject.AddComponent <ModManager>();
            var types  = Assembly.GetExecutingAssembly().GetTypes().ToList().Where(t => t.BaseType == typeof(ModBase) && !t.IsNested);

            foreach (var type in types)
            {
                modMgr.Mods.Add((ModBase)Activator.CreateInstance(type));
            }
        }
        private static Il2CppMethodInfo *ConvertMethodInfo(MethodInfo monoMethod, INativeClassStruct declaringClass)
        {
            var converted = (Il2CppMethodInfo *)Marshal.AllocHGlobal(Marshal.SizeOf <Il2CppMethodInfo>());

            *converted = default;
            converted->name  = Marshal.StringToHGlobalAnsi(monoMethod.Name);
            converted->klass = declaringClass.ClassPointer;

            var parameters = monoMethod.GetParameters();

            if (parameters.Length > 0)
            {
                converted->parameters_count = (byte)parameters.Length;
                var paramsArray = (Il2CppParameterInfo *)Marshal.AllocHGlobal(Marshal.SizeOf <Il2CppParameterInfo>() * parameters.Length);
                converted->parameters = paramsArray;
                for (var i = 0; i < parameters.Length; i++)
                {
                    var parameterInfo = parameters[i];
                    paramsArray[i].name           = Marshal.StringToHGlobalAnsi(parameterInfo.Name);
                    paramsArray[i].position       = i;
                    paramsArray[i].token          = 0;
                    paramsArray[i].parameter_type = (Il2CppTypeStruct *)IL2CPP.il2cpp_class_get_type(ReadClassPointerForType(parameterInfo.ParameterType));
                }
            }

            converted->invoker_method = Marshal.GetFunctionPointerForDelegate(GetOrCreateInvoker(monoMethod));
            converted->methodPointer  = Marshal.GetFunctionPointerForDelegate(GetOrCreateTrampoline(monoMethod));
            converted->slot           = ushort.MaxValue;
            converted->return_type    = (Il2CppTypeStruct *)IL2CPP.il2cpp_class_get_type(ReadClassPointerForType(monoMethod.ReturnType));

            converted->flags = Il2CppMethodFlags.METHOD_ATTRIBUTE_PUBLIC |
                               Il2CppMethodFlags.METHOD_ATTRIBUTE_HIDE_BY_SIG;

            return(converted);
        }
Beispiel #7
0
 internal IL2CPP_Field(IntPtr ptr) : base(ptr)
 {
     Ptr        = ptr;
     Name       = Marshal.PtrToStringAnsi(IL2CPP.il2cpp_field_get_name(Ptr));
     Flags      = (IL2CPP_BindingFlags)IL2CPP.il2cpp_field_get_flags(Ptr);
     ReturnType = new IL2CPP_Type(IL2CPP.il2cpp_field_get_type(Ptr));
 }
Beispiel #8
0
        private static void LoadImage(Texture2D tex, byte[] data, bool markNonReadable)
        {
            _iCallLoadImage ??= IL2CPP.ResolveICall <DLoadImage>("UnityEngine.ImageConversion::LoadImage");
            var il2CPPArray = (Il2CppStructArray <byte>)data;

            _iCallLoadImage.Invoke(tex.Pointer, il2CPPArray.Pointer, markNonReadable);
        }
        private unsafe static IntPtr OurGetInputString()
        {
            string input    = IL2CPP.Il2CppStringToManaged(originalInputStringGetter());
            IntPtr vr_input = IL2CPP.ManagedStringToIl2Cpp(VRKeyboard.GetKeyboardInput() + input);

            return(vr_input);
        }
Beispiel #10
0
        internal IL2CPP_Class(IntPtr ptr) : base(ptr)
        {
            // Setup Information
            Ptr       = ptr;
            Name      = Marshal.PtrToStringAnsi(IL2CPP.il2cpp_class_get_name(Ptr));
            Namespace = Marshal.PtrToStringAnsi(IL2CPP.il2cpp_class_get_namespace(Ptr));
            Flags     = (IL2CPP_BindingFlags)IL2CPP.il2cpp_class_get_flags(Ptr);

            // Map out Methods
            IntPtr method_iter = IntPtr.Zero;
            IntPtr method;
            List <IL2CPP_Method> methodsList = new List <IL2CPP_Method>();

            while ((method = IL2CPP.il2cpp_class_get_methods(Ptr, ref method_iter)) != IntPtr.Zero)
            {
                methodsList.Add(new IL2CPP_Method(method));
            }
            MethodList = methodsList.ToArray();

            // Map out Fields
            IntPtr field_iter = IntPtr.Zero;
            IntPtr field;
            List <IL2CPP_Field> fieldList = new List <IL2CPP_Field>();

            while ((field = IL2CPP.il2cpp_class_get_fields(Ptr, ref field_iter)) != IntPtr.Zero)
            {
                fieldList.Add(new IL2CPP_Field(field));
            }
            FieldList = fieldList.ToArray();

            // Map out Events
            IntPtr evt_iter = IntPtr.Zero;
            IntPtr evt;
            List <IL2CPP_Event> eventList = new List <IL2CPP_Event>();

            while ((evt = IL2CPP.il2cpp_class_get_events(Ptr, ref evt_iter)) != IntPtr.Zero)
            {
                eventList.Add(new IL2CPP_Event(evt));
            }
            EventList = eventList.ToArray();

            // Map out Nested Types
            //IntPtr nestedtype_iter = IntPtr.Zero;
            //IntPtr nestedtype;
            //List<IL2CPP_Class> nestedTypeList = new List<IL2CPP_Class>();
            //while ((nestedtype = IL2CPP.il2cpp_class_get_nested_types(Ptr, ref nestedtype_iter)) != IntPtr.Zero)
            //    nestedTypeList.Add(new IL2CPP_Class(nestedtype));
            //NestedTypeList = nestedTypeList.ToArray();

            // Map out Properties
            IntPtr property_iter = IntPtr.Zero;
            IntPtr property;
            List <IL2CPP_Property> propertyList = new List <IL2CPP_Property>();

            while ((property = IL2CPP.il2cpp_class_get_properties(Ptr, ref property_iter)) != IntPtr.Zero)
            {
                propertyList.Add(new IL2CPP_Property(property));
            }
            PropertyList = propertyList.ToArray();
        }
Beispiel #11
0
        public static bool CanBeCast <T>(Il2CppObjectBase obj) where T : Il2CppObjectBase
        {
            IntPtr nativeClassPtr = Il2CppClassPointerStore <T> .NativeClassPtr;
            IntPtr num            = IL2CPP.il2cpp_object_get_class(obj.Pointer);

            return(IL2CPP.il2cpp_class_is_assignable_from(nativeClassPtr, num));
        }
        public void ExecuteCode(string code)
        {
            IL2CPP.il2cpp_thread_attach(IL2CPP.il2cpp_domain_get());
            try
            {
                var result = myengine.GetValue(myengine.Execute(code).GetCompletionValue());
                //filter out statements which dont return data
                if (result.Type == Types.None || result.Type == Types.Null || result.Type == Types.Undefined)
                {
                    return;
                }

                var str = TypeConverter.ToString(myengine.Json.Stringify(myengine.Json,
                                                                         Arguments.From(result, Undefined.Instance, "  ")));
                engineOut = $"=> {str}";
            }
            catch (JavaScriptException je)
            {
                engineOut = je.ToString();
            }
            catch (Exception e)
            {
                engineOut = e.ToString();
            }
        }
Beispiel #13
0
        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));
        }
        static Il2CppAssetBundleRequest()
        {
            UnhollowerRuntimeLib.ClassInjector.RegisterTypeInIl2Cpp <Il2CppAssetBundleRequest>();

            get_assetDelegateField     = IL2CPP.ResolveICall <get_assetDelegate>("UnityEngine.AssetBundleRequest::get_asset");
            get_allAssetsDelegateField = IL2CPP.ResolveICall <get_allAssetsDelegate>("UnityEngine.AssetBundleRequest::get_allAssets");
        }
        private static ISupportModule Initialize()
        {
            LogSupport.RemoveAllHandlers();
            if (true)
            {
                LogSupport.InfoHandler += MelonLogger.Log;
            }
            LogSupport.WarningHandler += MelonLogger.LogWarning;
            LogSupport.ErrorHandler   += MelonLogger.LogError;
            if (true)
            {
                LogSupport.TraceHandler += MelonLogger.Log;
            }

            ClassInjector.DoHook += Imports.Hook;
            GetUnityVersionNumbers(out var major, out var minor, out var patch);
            UnityVersionHandler.Initialize(major, minor, patch);

            // Il2CppSystem.Console.SetOut(new Il2CppSystem.IO.StreamWriter(Il2CppSystem.IO.Stream.Null));
            try
            {
                var il2CppSystemAssembly = Assembly.Load("Il2Cppmscorlib");

                var consoleType      = il2CppSystemAssembly.GetType("Il2CppSystem.Console");
                var streamWriterType = il2CppSystemAssembly.GetType("Il2CppSystem.IO.StreamWriter");
                var streamType       = il2CppSystemAssembly.GetType("Il2CppSystem.IO.Stream");

                var setOutMethod     = consoleType.GetMethod("SetOut", BindingFlags.Static | BindingFlags.Public);
                var nullStreamField  = streamType.GetProperty("Null", BindingFlags.Static | BindingFlags.Public).GetGetMethod();
                var streamWriterCtor = streamWriterType.GetConstructor(new[] { streamType });

                var nullStream  = nullStreamField.Invoke(null, new object[0]);
                var steamWriter = streamWriterCtor.Invoke(new[] { nullStream });
                setOutMethod.Invoke(null, new[] { steamWriter });
            }
            catch (Exception ex)
            {
                MelonLogger.LogError($"Console cleaning failed: {ex}");
            }

            SetAsLastSiblingDelegateField = IL2CPP.ResolveICall <SetAsLastSiblingDelegate>("UnityEngine.Transform::SetAsLastSibling");

            ClassInjector.RegisterTypeInIl2Cpp <MelonLoaderComponent>();
            //MelonLogger.Log("ClassInjector.RegisterTypeInIl2Cpp<MelonLoaderComponent> done");
            MelonLoaderComponent.Create();
            //MelonLogger.Log("MelonLoaderComponent.Create() done");
            SceneManager.sceneLoaded = (
                (SceneManager.sceneLoaded == null)
                ? new Action <Scene, LoadSceneMode>(OnSceneLoad)
                : Il2CppSystem.Delegate.Combine(SceneManager.sceneLoaded, (UnityAction <Scene, LoadSceneMode>) new Action <Scene, LoadSceneMode>(OnSceneLoad)).Cast <UnityAction <Scene, LoadSceneMode> >()
                );
            Camera.onPostRender = (
                (Camera.onPostRender == null)
                ? new Action <Camera>(OnPostRender)
                : Il2CppSystem.Delegate.Combine(Camera.onPostRender, (Camera.CameraCallback) new Action <Camera>(OnPostRender)).Cast <Camera.CameraCallback>()
                );

            return(new Module());
        }
Beispiel #16
0
 internal static void Create()
 {
     Main.obj = new GameObject();
     DontDestroyOnLoad(Main.obj);
     Main.comp = new MelonLoaderComponent(Main.obj.AddComponent(UnhollowerRuntimeLib.Il2CppType.Of <MelonLoaderComponent>()).Pointer);
     Main.SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(Main.obj.transform));
     Main.SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(Main.comp.transform));
 }
Beispiel #17
0
 internal static void Create()
 {
     Main.obj = new GameObject("MelonLoader");
     DontDestroyOnLoad(Main.obj);
     Main.comp = Main.obj.AddComponent <MelonLoaderComponent>();
     Main.SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(Main.obj.transform));
     Main.SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(Main.comp.transform));
 }
 static Il2CppImageConversionManager()
 {
     EncodeToTGADelegateField = IL2CPP.ResolveICall <TextureOnlyDelegate>("UnityEngine.ImageConversion::EncodeToTGA");
     EncodeToEXRDelegateField = IL2CPP.ResolveICall <TextureAndFlagDelegate>("UnityEngine.ImageConversion::EncodeToEXR");
     EncodeToPNGDelegateField = IL2CPP.ResolveICall <TextureOnlyDelegate>("UnityEngine.ImageConversion::EncodeToPNG");
     EncodeToJPGDelegateField = IL2CPP.ResolveICall <TextureAndQualityDelegate>("UnityEngine.ImageConversion::EncodeToJPG");
     LoadImageDelegateField   = IL2CPP.ResolveICall <LoadImageDelegate>("UnityEngine.ImageConversion::LoadImage");
 }
        public static IntPtr GetIl2CppMethod(IntPtr?clazz, string methodName, string returnType, params string[] types)
        {
            if (!clazz.HasValue || clazz == IntPtr.Zero)
            {
                return(IntPtr.Zero);
            }

            return(IL2CPP.GetIl2CppMethod(clazz.Value, false, methodName, returnType, types));
        }
Beispiel #20
0
        private unsafe static void ChatMsgPatch(IntPtr thisPtr, MarshChatMsg msg)
        {
            string chatStr = IL2CPP.Il2CppStringToManaged(msg.msg.m_data);

            if (!CommandParser.HandleMessage(chatStr))
            {
                ourChatDelegate(thisPtr, msg);
            }
        }
Beispiel #21
0
        private bool TrySetString(IntPtr keyPtr, IntPtr valuePtr)
        {
            myHadChanges = true;

            var key = IL2CPP.Il2CppStringToManaged(keyPtr);

            myPrefs[key] = IL2CPP.Il2CppStringToManaged(valuePtr);
            return(true);
        }
Beispiel #22
0
        private bool TrySetInt(IntPtr keyPtr, int value)
        {
            myHadChanges = true;

            var key = IL2CPP.Il2CppStringToManaged(keyPtr);

            myPrefs[key] = value;
            return(true);
        }
Beispiel #23
0
        private static bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable)
        {
            if (iCall_LoadImage == null)
            {
                iCall_LoadImage = IL2CPP.ResolveICall <d_LoadImage>("UnityEngine.ImageConversion::LoadImage");
            }
            var il2cppArray = (Il2CppStructArray <byte>)data;

            return(iCall_LoadImage.Invoke(tex.Pointer, il2cppArray.Pointer, markNonReadable));
        }
Beispiel #24
0
        private unsafe static bool SafeInvokeMoveNext(IntPtr methodPtr, IntPtr thisPtr)
        {
            var exc = IntPtr.Zero;

            ((Il2CppMethodInfo *)ourInvokeMethodInfo)->methodPointer = methodPtr;
            var result = IL2CPP.il2cpp_runtime_invoke(ourInvokeMethodInfo, thisPtr, (void **)IntPtr.Zero, ref exc);

            Il2CppException.RaiseExceptionIfNecessary(exc);
            return(*(bool *)IL2CPP.il2cpp_object_unbox(result));
        }
Beispiel #25
0
        /// <summary>
        /// Invokes the method with the provided 'this' reference and parameters.
        /// <para>An <see cref="InvalidOperationException"/> is thrown if the Invoke fails</para>
        /// </summary>
        /// <param name="obj">The 'this' reference to call the method on.
        /// If this <see cref="IL2CPP_Method"/> is of a static method, provide a null for this parameter</param>
        /// <param name="paramtbl">Parameters to supply to this method. Includes the generic parameters (if there are any)</param>
        /// <returns>The resultant object from the Invoke</returns>
        /// <exception cref="InvalidOperationException"></exception>
        public IL2CPP_Object Invoke(IntPtr obj, params IntPtr[] paramtbl)
        {
            IntPtr returnval = IL2CPP.InvokeMethod(Ptr, obj, paramtbl);

            if (returnval == IntPtr.Zero)
            {
                return(null);
            }
            return(new IL2CPP_Object(returnval, GetReturnType()));
        }
        private static unsafe void FindMetadataInitForMethod()
        {
            var unityObjectCctor = AppDomain.CurrentDomain.GetAssemblies()
                                   .Single(it => it.GetSimpleName() == "UnityEngine.CoreModule").GetType("UnityEngine.Object")
                                   .GetConstructors(BindingFlags.Static | BindingFlags.NonPublic).Single();
            var nativeMethodInfo = IL2CPP.il2cpp_method_get_from_reflection(unityObjectCctor.Pointer);

            ourMetadataInitForMethodPointer  = XrefScannerLowLevel.JumpTargets(*(IntPtr *)nativeMethodInfo).First();
            ourMetadataInitForMethodDelegate = Marshal.GetDelegateForFunctionPointer <InitMetadataForMethod>(ourMetadataInitForMethodPointer);
        }
        public static void HookAll()
        {
            Log.Info($"Patching all terminal functions...");

            FastNativeDetour.CreateAndApply(IL2CPP.il2cpp_resolve_icall("UnityEngine.Input::" + "get_inputString"),
                                            OurGetInputString, out originalInputStringGetter, CallingConvention.Cdecl);

            FastNativeDetour.CreateAndApply(IL2CPP.il2cpp_resolve_icall("UnityEngine.Input::" + "get_anyKeyDown"),
                                            OurGetAnyInput, out originalAnyInputDownGetter, CallingConvention.Cdecl);
        }
Beispiel #28
0
        public static unsafe string IntPtrToString(IntPtr ptr)
        {
            int length = IL2CPP.il2cpp_string_length(ptr);

            if (length <= 0)
            {
                return(string.Empty);
            }
            return(new string(IL2CPP.il2cpp_string_chars(ptr)));
        }
Beispiel #29
0
        private void SiblingFix()
        {
#if SM_Il2Cpp
            SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(gameObject.transform));
            SetAsLastSiblingDelegateField(IL2CPP.Il2CppObjectBaseToPtrNotNull(transform));
#else
            SetAsLastSiblingMethod?.Invoke(gameObject.transform, new object[0]);
            SetAsLastSiblingMethod?.Invoke(transform, new object[0]);
#endif
        }
        public static AssetBundleProxy LoadFromFile(string path, uint crc, ulong offset)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new System.ArgumentException(nameof(path), "The path cannot be null or empty.");
            }

            IntPtr ptr = LoadFromFile_InternalDelegateField(IL2CPP.ManagedStringToIl2Cpp(path), crc, offset);

            return(ptr != IntPtr.Zero ? new AssetBundleProxy(ptr) : null);
        }