Ejemplo n.º 1
0
            private static bool CheckFunc(ScriptType type)
            {
                if (!type.IsPublic)
                {
                    return(false);
                }
                var objectType = new ScriptType(typeof(FlaxEngine.Object));

                return(type.IsEnum || type == objectType || objectType.IsAssignableFrom(type));
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets all the derived types from the given base type (excluding that type) within the given assembly.
        /// </summary>
        /// <param name="assembly">The target assembly to check its types.</param>
        /// <param name="baseType">The base type.</param>
        /// <param name="result">The result collection. Elements will be added to it. Clear it before usage.</param>
        /// <param name="checkFunc">Additional callback used to check if the given type is valid. Returns true if add type, otherwise false.</param>
        public static void GetDerivedTypes(Assembly assembly, ScriptType baseType, List <ScriptType> result, Func <ScriptType, bool> checkFunc)
        {
            var types = assembly.GetTypes();

            for (int i = 0; i < types.Length; i++)
            {
                var t = new ScriptType(types[i]);
                if (baseType.IsAssignableFrom(t) && t != baseType && checkFunc(t))
                {
                    result.Add(t);
                }
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public void GetDerivedTypes(ScriptType baseType, List <ScriptType> result, Func <ScriptType, bool> checkFunc)
        {
            var visualScripts = VisualScriptItem.VisualScripts;

            for (var i = 0; i < visualScripts.Count; i++)
            {
                var t = visualScripts[i].ScriptType;
                if (baseType.IsAssignableFrom(t) && t != baseType && checkFunc(t))
                {
                    result.Add(t);
                }
            }
        }
        /// <summary>
        /// Checks if can use direct conversion from one type to another.
        /// </summary>
        /// <param name="from">Source type.</param>
        /// <param name="to">Target type.</param>
        /// <returns>True if can use direct conversion, otherwise false.</returns>
        public bool CanUseDirectCast(ScriptType from, ScriptType to)
        {
            if (from == ScriptType.Null || to == ScriptType.Null)
            {
                return(false);
            }
            bool result = from == to || to.IsAssignableFrom(from);

            if (!result)
            {
                // Implicit casting is supported for reference types
                var toIsReference   = to.IsReference;
                var fromIsReference = from.IsReference;
                if (toIsReference && !fromIsReference)
                {
                    var toTypeName = to.TypeName;
                    return(from.TypeName == toTypeName.Substring(0, toTypeName.Length - 1));
                }
                if (!toIsReference && fromIsReference)
                {
                    var fromTypeName = from.TypeName;
                    return(to.TypeName == fromTypeName.Substring(0, fromTypeName.Length - 1));
                }

                // Implicit casting is supported for object reference to test whenever it is valid
                var toType = to.Type;
                if (_supportsImplicitCastFromObjectToBoolean && toType == typeof(bool) && new ScriptType(typeof(FlaxEngine.Object)).IsAssignableFrom(from))
                {
                    return(true);
                }

                // Implicit casting is supported for primitive types
                var fromType = from.Type;
                if (fromType == typeof(bool) ||
                    fromType == typeof(byte) ||
                    fromType == typeof(char) ||
                    fromType == typeof(short) ||
                    fromType == typeof(ushort) ||
                    fromType == typeof(int) ||
                    fromType == typeof(uint) ||
                    fromType == typeof(long) ||
                    fromType == typeof(ulong) ||
                    fromType == typeof(float) ||
                    fromType == typeof(double) ||
                    fromType == typeof(Vector2) ||
                    fromType == typeof(Vector3) ||
                    fromType == typeof(Vector4) ||
                    fromType == typeof(Color) ||
                    fromType == typeof(Quaternion))
                {
                    if (toType == typeof(bool) ||
                        toType == typeof(byte) ||
                        toType == typeof(char) ||
                        toType == typeof(short) ||
                        toType == typeof(ushort) ||
                        toType == typeof(int) ||
                        toType == typeof(uint) ||
                        toType == typeof(long) ||
                        toType == typeof(ulong) ||
                        toType == typeof(float) ||
                        toType == typeof(double) ||
                        toType == typeof(Vector2) ||
                        toType == typeof(Vector3) ||
                        toType == typeof(Vector4) ||
                        toType == typeof(Color) ||
                        toType == typeof(Quaternion))
                    {
                        result = true;
                    }
                }
            }
            return(result);
        }