Exemplo n.º 1
0
        /// <summary>
        /// Converts a native value to a managed object of a value type.
        /// </summary>
        /// <typeparam name="T">The return type. The type should be a value type.</typeparam>
        /// <param name="value">The native value to convert.</param>
        /// <returns>A managed object representing the input <paramref name="value"/>.</returns>
        internal static unsafe T ObjectFromNative <T>(ulong *value)
        {
            if (typeof(T) == typeof(bool))
            {
                // Return proper boolean values (true if non-zero and false if zero)
                bool valueBool = *value != 0;
                return(NativeHelper <T> .PtrToStructure(new IntPtr(&valueBool)));
            }
            if (typeof(T) == typeof(IntPtr))             // Has to be before 'IsPrimitive' check
            {
                return(InstanceCreator <long, T> .Create((long)(*value)));
            }

            if (typeof(T).IsEnum)
            {
                return(NativeHelper <T> .Convert(*value));
            }
            if (typeof(T).IsPrimitive)
            {
                return(NativeHelper <T> .PtrToStructure(new IntPtr(value)));
            }

            if (typeof(T) == typeof(Math.Vector2))
            {
                var data = (float *)value;
                return(InstanceCreator <float, float, T> .Create(data[0], data[2]));
            }
            if (typeof(T) == typeof(Math.Vector3))
            {
                var data = (float *)value;
                return(InstanceCreator <float, float, float, T> .Create(data[0], data[2], data[4]));
            }

            if (typeof(T) == typeof(Model) || typeof(T) == typeof(WeaponAsset) || typeof(T) == typeof(RelationshipGroup))
            {
                return(InstanceCreator <int, T> .Create((int) *value));
            }

            throw new InvalidCastException(string.Concat("Unable to cast native value to object of type '", typeof(T), "'"));
        }
Exemplo n.º 2
0
            /// <summary>
            /// Converts a native value to a managed object of a value type.
            /// </summary>
            /// <typeparam name="T">The return type. The type should be a value type.</typeparam>
            /// <param name="value">The native value to convert.</param>
            /// <returns>A managed object representing the input <paramref name="value"/>.</returns>
            internal static unsafe T ObjectFromNative <T>(ulong *value)
            {
                if (typeof(T).IsEnum)
                {
                    return(NativeHelper <T> .Convert(*value));
                }

                if (typeof(T) == typeof(bool))
                {
                    return(NativeHelper <T> .PtrToStructure(new IntPtr(value)));
                }
                if (typeof(T) == typeof(int))
                {
                    return(NativeHelper <T> .PtrToStructure(new IntPtr(value)));
                }
                if (typeof(T) == typeof(uint))
                {
                    return(NativeHelper <T> .PtrToStructure(new IntPtr(value)));
                }
                if (typeof(T) == typeof(long))
                {
                    return(NativeHelper <T> .PtrToStructure(new IntPtr(value)));
                }
                if (typeof(T) == typeof(ulong))
                {
                    return(NativeHelper <T> .PtrToStructure(new IntPtr(value)));
                }
                if (typeof(T) == typeof(float))
                {
                    return(NativeHelper <T> .PtrToStructure(new IntPtr(value)));
                }
                if (typeof(T) == typeof(double))
                {
                    return(NativeHelper <T> .PtrToStructure(new IntPtr(value)));
                }

                if (typeof(T) == typeof(IntPtr))
                {
                    return(InstanceCreator <long, T> .Create((long)(*value)));
                }

                if (typeof(T) == typeof(Math.Vector2))
                {
                    var data = (float *)(value);

                    return(InstanceCreator <float, float, T> .Create(data[0], data[2]));
                }
                if (typeof(T) == typeof(Math.Vector3))
                {
                    var data = (float *)(value);

                    return(InstanceCreator <float, float, float, T> .Create(data[0], data[2], data[4]));
                }

                if (typeof(T) == typeof(Model) || typeof(T) == typeof(WeaponAsset) || typeof(T) == typeof(RelationshipGroup))
                {
                    return(InstanceCreator <int, T> .Create((int)(*value)));
                }

                throw new InvalidCastException(String.Concat("Unable to cast native value to object of type '", typeof(T), "'"));
            }