/// <summary>
        /// Check if an object is null, and if it's a UnityEngine.Object then also check if it was destroyed.
        /// </summary>
        public static bool IsNullOrDestroyed(this object obj, bool suppressWarning = true)
        {
            var unityObj = obj as Object;

            if (obj == null)
            {
                if (!suppressWarning)
                {
                    ExplorerCore.LogWarning("The target instance is null!");
                }

                return(true);
            }
            else if (obj is Object)
            {
                if (!unityObj)
                {
                    if (!suppressWarning)
                    {
                        ExplorerCore.LogWarning("The target UnityEngine.Object was destroyed!");
                    }

                    return(true);
                }
            }
            return(false);
        }
Beispiel #2
0
        public static string GetExampleInput(Type type)
        {
            if (!typeInputExamples.ContainsKey(type.AssemblyQualifiedName))
            {
                try
                {
                    if (type.IsEnum)
                    {
                        typeInputExamples.Add(type.AssemblyQualifiedName, Enum.GetNames(type).First());
                    }
                    else
                    {
                        var instance = Activator.CreateInstance(type);
                        typeInputExamples.Add(type.AssemblyQualifiedName, ToStringForInput(instance, type));
                    }
                }
                catch (Exception ex)
                {
                    ExplorerCore.LogWarning("Exception generating default instance for example input for '" + type.FullName + "'");
                    ExplorerCore.Log(ex);
                    return("");
                }
            }

            return(typeInputExamples[type.AssemblyQualifiedName]);
        }
        internal void Awake()
        {
            try
            {
#if CPP
                Camera.onPostRender = Camera.onPostRender == null
                   ? new Action <Camera>(OnPostRender)
                   : Il2CppSystem.Delegate.Combine(Camera.onPostRender,
                                                   (Camera.CameraCallback) new Action <Camera>(OnPostRender)).Cast <Camera.CameraCallback>();

                if (Camera.onPostRender == null || Camera.onPostRender.delegates == null)
                {
                    ExplorerCore.LogWarning("Failed to add Camera.onPostRender listener, falling back to LateUpdate instead!");
                    onPostRenderFailed = true;
                }
#else
                Camera.onPostRender += OnPostRender;
#endif
            }
            catch (Exception ex)
            {
                ExplorerCore.LogWarning($"Exception adding onPostRender listener: {ex.ReflectionExToString()}\r\nFalling back to LateUpdate!");
                onPostRenderFailed = true;
            }
        }
        public Il2CppSystem.Object BoxIl2CppObject(object value)
        {
            if (value == null)
            {
                return(null);
            }

            try
            {
                var type = value.GetType();
                if (!type.IsValueType)
                {
                    return(null);
                }

                if (type.IsEnum)
                {
                    return(Il2CppSystem.Enum.Parse(Il2CppType.From(type), value.ToString()));
                }

                if (type.IsPrimitive && AllTypes.TryGetValue($"Il2Cpp{type.FullName}", out Type cppType))
                {
                    return(BoxIl2CppObject(MakeIl2CppPrimitive(cppType, value), cppType));
                }

                return(BoxIl2CppObject(value, type));
            }
            catch (Exception ex)
            {
                ExplorerCore.LogWarning("Exception in BoxIl2CppObject: " + ex);
                return(null);
            }
        }
        // Unbox an il2cpp object to a struct or System primitive.
        public object UnboxCppObject(Il2CppObjectBase cppObj, Type toType)
        {
            if (!toType.IsValueType)
            {
                return(null);
            }

            try
            {
                if (toType.IsEnum)
                {
                    return(Enum.Parse(toType, cppObj.ToString()));
                }

                var name = toType.AssemblyQualifiedName;

                if (!unboxMethods.ContainsKey(name))
                {
                    unboxMethods.Add(name, typeof(Il2CppObjectBase)
                                     .GetMethod("Unbox")
                                     .MakeGenericMethod(toType));
                }

                return(unboxMethods[name].Invoke(cppObj, ArgumentUtility.EmptyArgs));
            }
            catch (Exception ex)
            {
                ExplorerCore.LogWarning("Exception Unboxing Il2Cpp object to struct: " + ex);
                return(null);
            }
        }
        internal override Type Internal_GetActualType(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var type = obj.GetType();

            try
            {
                if (IsString(obj))
                {
                    return(typeof(string));
                }

                if (IsIl2CppPrimitive(type))
                {
                    return(il2cppPrimitivesToMono[type.FullName]);
                }

                if (obj is Il2CppSystem.Object cppObject)
                {
                    var cppType = cppObject.GetIl2CppType();

                    // check if type is injected
                    IntPtr classPtr = il2cpp_object_get_class(cppObject.Pointer);
                    if (RuntimeSpecificsStore.IsInjected(classPtr))
                    {
                        // Note: This will fail on injected subclasses.
                        // - {Namespace}.{Class}.{Subclass} would be {Namespace}.{Subclass} when injected.
                        // Not sure on solution yet.
                        return(GetTypeByName(cppType.FullName) ?? type);
                    }

                    if (AllTypes.TryGetValue(cppType.FullName, out Type primitive) && primitive.IsPrimitive)
                    {
                        return(primitive);
                    }

                    return(GetUnhollowedType(cppType) ?? type);
                }
            }
            catch (Exception ex)
            {
                ExplorerCore.LogWarning("Exception in IL2CPP GetActualType: " + ex);
            }

            return(type);
        }
Beispiel #7
0
        public static string ToStringForInput(object obj, Type type)
        {
            if (type == null || obj == null)
            {
                return(null);
            }

            if (type == typeof(string))
            {
                return(obj as string);
            }

            if (type.IsEnum)
            {
                return(Enum.IsDefined(type, obj)
                        ? Enum.GetName(type, obj)
                        : obj.ToString());
            }

            try
            {
                if (customTypes.ContainsKey(type.FullName))
                {
                    return(customTypesToString[type.FullName].Invoke(obj));
                }
                else if (formattedTypes.Contains(type))
                {
                    return(ReflectionUtility.GetMethodInfo(type, "ToString", new Type[] { typeof(string), typeof(IFormatProvider) })
                           .Invoke(obj, new object[] { NUMBER_FORMAT, en_US })
                           as string);
                }
                else
                {
                    return(obj.ToString());
                }
            }
            catch (Exception ex)
            {
                ExplorerCore.LogWarning($"Exception formatting object for input: {ex}");
                return(null);
            }
        }