Пример #1
0
            /// <summary>
            /// Set the value stored in the <see cref="GlobalVariable"/>.
            /// </summary>
            /// <param name="value">The new value to assign to the <see cref="GlobalVariable"/>.</param>
            public unsafe void Write <T>(T value)
            {
                if (typeof(T) == typeof(string))
                {
                    throw new InvalidOperationException("Cannot write string values via 'Write<string>', use 'WriteString' instead.");
                }

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

                    data[0] = val.X;
                    data[2] = val.Y;
                    return;
                }
                if (typeof(T) == typeof(Math.Vector3))
                {
                    var val  = (Math.Vector3)(object)(value);
                    var data = (float *)(MemoryAddress.ToPointer());

                    data[0] = val.X;
                    data[2] = val.Y;
                    data[4] = val.Z;
                    return;
                }

                if (typeof(T) == typeof(bool))
                {
                    *(ulong *)(MemoryAddress.ToPointer()) = NativeHelper <ulong> .Convert(value);
                }
                else if (typeof(T) == typeof(double))
                {
                    *(ulong *)(MemoryAddress.ToPointer()) = 0;                    // padding
                    *(float *)(MemoryAddress.ToPointer()) = NativeHelper <float> .Convert(value);
                }
                else if (typeof(T).IsPrimitive)
                {
                    if (typeof(T) == typeof(IntPtr))
                    {
                        *(long *)(MemoryAddress.ToPointer()) = NativeHelper <long> .Convert(value);
                    }
                    else
                    {
                        *(ulong *)(MemoryAddress.ToPointer()) = NativeHelper <ulong> .Convert(value);
                    }
                }
                else
                {
                    *(ulong *)(MemoryAddress.ToPointer()) = Function.ObjectToNative(value);
                }
            }
Пример #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) == 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), "'"));
        }
Пример #3
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), "'"));
            }