Пример #1
0
        public bool ReadAddresses()
        {
            if (_memory == null)
            {
                var tempProcess = MemoryHelpers.GetProcessIfRunning(ProcessNames[0]);
                if (tempProcess == null)
                {
                    return(false);
                }

                _memory = new Memory(tempProcess);
            }

            if (_memory == null)
            {
                return(false);
            }

            var boostOffsets = _pointer.GameAddresses.First(ga => ga.Description == "Boost").ToString();

            boostAddress = _memory.GetAddress("\"RocketLeague.exe\"" + boostOffsets);

            var speedOffsets = _pointer.GameAddresses.First(ga => ga.Description == "Speed").ToString();

            speedAddress = _memory.GetAddress("\"RocketLeague.exe\"" + speedOffsets);

            return(true);
        }
Пример #2
0
        public override float ReadRawValueFrom(IntPtr statePtr)
        {
            var valuePtr = new IntPtr(statePtr.ToInt64() + (int)m_StateBlock.byteOffset);
            var intValue = MemoryHelpers.ReadIntFromMultipleBits(valuePtr, m_StateBlock.bitOffset, m_StateBlock.sizeInBits);

            var value = 0.0f;

            if (minValue > maxValue)
            {
                // If no wrapping point is set, default to wrapping around exactly
                // at the point of minValue.
                if (wrapAtValue == nullValue)
                {
                    wrapAtValue = minValue;
                }

                if ((intValue >= minValue && intValue <= wrapAtValue) ||
                    (intValue != nullValue && intValue <= maxValue))
                {
                    value = 1.0f;
                }
            }
            else
            {
                value = intValue >= minValue && intValue <= maxValue ? 1.0f : 0.0f;
            }

            return(Preprocess(value));
        }
Пример #3
0
        internal unsafe void __MarshalTo(ref __Native @ref)
        {
            @ref.NumEntries = Elements?.Length ?? 0;
            if (@ref.NumEntries > 0)
            {
                var nativeElements = (StreamOutputElement.__Native *)Interop.Alloc <StreamOutputElement.__Native>(@ref.NumEntries);
                for (int i = 0; i < @ref.NumEntries; i++)
                {
                    Elements[i].__MarshalTo(ref nativeElements[i]);
                }

                @ref.pSODeclaration = nativeElements;
            }

            @ref.NumStrides = Strides?.Length ?? 0;
            if (@ref.NumStrides > 0)
            {
                var nativeStrides = Interop.Alloc <int>(@ref.NumStrides);
                fixed(int *src = &Strides[0])
                {
                    MemoryHelpers.CopyMemory(nativeStrides, (IntPtr)src, @ref.NumStrides * sizeof(int));
                }

                @ref.pBufferStrides = nativeStrides;
            }

            @ref.RasterizedStream = RasterizedStream;
        }
Пример #4
0
        public float ReadFloat(void *statePtr)
        {
            Debug.Assert(sizeInBits != 0);

            var valuePtr = (byte *)statePtr + (int)byteOffset;

            float value;

            if (format == kTypeFloat)
            {
                Debug.Assert(sizeInBits == 32, "FLT state must have sizeInBits=32");
                Debug.Assert(bitOffset == 0, "FLT state must be byte-aligned");
                value = *(float *)valuePtr;
            }
            else if (format == kTypeBit)
            {
                if (sizeInBits != 1)
                {
                    throw new NotImplementedException("Cannot yet convert multi-bit fields to floats");
                }

                value = MemoryHelpers.ReadSingleBit(valuePtr, bitOffset) ? 1.0f : 0.0f;
            }
            // If a control with an integer-based representation does not use the full range
            // of its integer size (e.g. only goes from [0..128]), processors or the parameters
            // above have to be used to re-process the resulting float values.
            else if (format == kTypeShort)
            {
                Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16");
                Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned");
                ////REVIEW: What's better here? This code reaches a clean -1 but doesn't reach a clean +1 as the range is [-32768..32767].
                ////        Should we cut off at -32767? Or just live with the fact that 0.999 is as high as it gets?
                value = *(short *)valuePtr / 32768.0f;
            }
            else if (format == kTypeUShort)
            {
                Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16");
                Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned");
                value = *(ushort *)valuePtr / 65535.0f;
            }
            else if (format == kTypeByte)
            {
                Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8");
                Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned");
                value = *valuePtr / 255.0f;
            }
            else if (format == kTypeSByte)
            {
                Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8");
                Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned");
                ////REVIEW: Same problem here as with 'short'
                value = *(sbyte *)valuePtr / 128.0f;
            }
            else
            {
                throw new Exception(string.Format("State format '{0}' is not supported as floating-point format", format));
            }

            return(value);
        }
Пример #5
0
        public unsafe void WriteFloat(IntPtr statePtr, float value)
        {
            var valuePtr = new IntPtr(statePtr.ToInt64() + (int)byteOffset);

            if (format == kTypeFloat)
            {
                *(float *)valuePtr = value;
            }
            else if (format == kTypeBit)
            {
                if (sizeInBits != 1)
                {
                    throw new NotImplementedException("Cannot yet convert multi-bit fields to floats");
                }

                MemoryHelpers.WriteSingleBit(valuePtr, bitOffset, value >= 0.5f);
            }
            else if (format == kTypeShort)
            {
                *(short *)valuePtr = (short)(value * 65535.0f);
            }
            else if (format == kTypeByte)
            {
                *(byte *)valuePtr = (byte)(value * 255.0f);
            }
            else
            {
                throw new Exception(string.Format("State format '{0}' is not supported as floating-point format", format));
            }
        }
Пример #6
0
        public override void Update()
        {
            if (_memory == null)
            {
                var tempProcess = MemoryHelpers.GetProcessIfRunning(ProcessNames[0]);
                if (tempProcess == null)
                {
                    return;
                }

                _memory = new Memory(tempProcess);
            }

            if (ProfileModel == null || DataModel == null || _memory == null)
            {
                return;
            }

            var offsets      = _pointer.GameAddresses.First(ga => ga.Description == "Boost").ToString();
            var boostAddress = _memory.GetAddress("\"RocketLeague.exe\"" + offsets);
            var boostInt     = (int)(_memory.ReadFloat(boostAddress) * 100);

            if (boostInt > 100)
            {
                boostInt = 100;
            }
            if (boostInt < 0)
            {
                boostInt = 0;
            }

            ((RocketLeagueDataModel)DataModel).Boost = boostInt;
        }
Пример #7
0
        public override unsafe float ReadUnprocessedValueFromState(void *statePtr)
        {
            var valuePtr = (byte *)statePtr + (int)m_StateBlock.byteOffset;
            // Note that all signed data in state buffers is in excess-K format.
            var intValue = MemoryHelpers.ReadTwosComplementMultipleBitsAsInt(valuePtr, m_StateBlock.bitOffset, m_StateBlock.sizeInBits);

            var value = 0.0f;

            if (minValue > maxValue)
            {
                // If no wrapping point is set, default to wrapping around exactly
                // at the point of minValue.
                if (wrapAtValue == nullValue)
                {
                    wrapAtValue = minValue;
                }

                if ((intValue >= minValue && intValue <= wrapAtValue) ||
                    (intValue != nullValue && intValue <= maxValue))
                {
                    value = 1.0f;
                }
            }
            else
            {
                value = intValue >= minValue && intValue <= maxValue ? 1.0f : 0.0f;
            }

            return(Preprocess(value));
        }
Пример #8
0
    public unsafe void Utilities_CanCompareMemoryBitRegions()
    {
        using (var array1 = new NativeArray <byte>(6, Allocator.Temp))
            using (var array2 = new NativeArray <byte>(6, Allocator.Temp))
            {
                var array1Ptr = (byte *)array1.GetUnsafePtr();
                var array2Ptr = (byte *)array2.GetUnsafePtr();

                MemoryHelpers.SetBitsInBuffer(array1Ptr, 0, 2, 1, true);

                Assert.That(MemoryHelpers.MemCmpBitRegion(array1Ptr, array2Ptr, 2, 1), Is.False);

                MemoryHelpers.SetBitsInBuffer(array2Ptr, 0, 2, 1, true);

                Assert.That(MemoryHelpers.MemCmpBitRegion(array1Ptr, array2Ptr, 2, 1), Is.True);

                UnsafeUtility.MemClear(array1Ptr, 6);
                UnsafeUtility.MemClear(array2Ptr, 6);

                MemoryHelpers.SetBitsInBuffer(array1Ptr, 0, 5, 24, true);

                Assert.That(MemoryHelpers.MemCmpBitRegion(array1Ptr, array2Ptr, 5, 24), Is.False);

                MemoryHelpers.SetBitsInBuffer(array2Ptr, 0, 5, 24, true);

                Assert.That(MemoryHelpers.MemCmpBitRegion(array1Ptr, array2Ptr, 5, 24), Is.True);
            }
    }
Пример #9
0
        ////TODO: pass state ptr *NOT* value ptr (it's confusing)
        // We don't allow custom default values for state so all zeros indicates
        // default states for us.
        // NOTE: The given argument should point directly to the value *not* to the
        //       base state to which the state block offset has to be added.
        internal unsafe bool CheckStateIsAllZeros(IntPtr valuePtr = new IntPtr())
        {
            if (valuePtr == IntPtr.Zero)
            {
                valuePtr = new IntPtr(currentStatePtr.ToInt64() + (int)m_StateBlock.byteOffset);
            }

            // Bitfield value.
            if (m_StateBlock.sizeInBits % 8 != 0 || m_StateBlock.bitOffset != 0)
            {
                if (m_StateBlock.sizeInBits > 1)
                {
                    throw new NotImplementedException("multi-bit zero check");
                }

                return(MemoryHelpers.ReadSingleBit(valuePtr, m_StateBlock.bitOffset) == false);
            }

            // Multi-byte value.
            var ptr      = (byte *)valuePtr;
            var numBytes = m_StateBlock.alignedSizeInBytes;

            for (var i = 0; i < numBytes; ++i, ++ptr)
            {
                if (*ptr != 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #10
0
 public unsafe AudioBuffer(int sizeInBytes, BufferFlags flags = BufferFlags.EndOfStream)
 {
     Flags            = flags;
     AudioBytes       = sizeInBytes;
     AudioDataPointer = new IntPtr(MemoryHelpers.AllocateMemory((nuint)sizeInBytes));
     _ownsBuffer      = true;
 }
        public void WriteDouble(void *statePtr, double value)
        {
            var valuePtr = (byte *)statePtr + (int)byteOffset;

            var fmt = (int)format;

            if (fmt == kFormatFloat)
            {
                Debug.Assert(sizeInBits == 32, "FLT state must have sizeInBits=32");
                Debug.Assert(bitOffset == 0, "FLT state must be byte-aligned");
                *(float *)valuePtr = (float)value;
            }
            else if (fmt == kFormatBit)
            {
                if (sizeInBits == 1)
                {
                    MemoryHelpers.WriteSingleBit(valuePtr, bitOffset, value >= 0.5f);
                }
                else
                {
                    var maxValue = (1 << (int)sizeInBits) - 1;
                    var intValue = (int)(value * maxValue);
                    MemoryHelpers.WriteIntFromMultipleBits(valuePtr, bitOffset, sizeInBits, intValue);
                }
            }
            else if (fmt == kFormatShort)
            {
                Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16");
                Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned");
                *(short *)valuePtr = (short)(value * 32768.0f);
            }
            else if (fmt == kFormatUShort)
            {
                Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16");
                Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned");
                *(ushort *)valuePtr = (ushort)(value * 65535.0f);
            }
            else if (fmt == kFormatByte)
            {
                Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8");
                Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned");
                *valuePtr = (byte)(value * 255.0f);
            }
            else if (fmt == kFormatSByte)
            {
                Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8");
                Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned");
                *(sbyte *)valuePtr = (sbyte)(value * 128.0f);
            }
            else if (fmt == kFormatDouble)
            {
                Debug.Assert(sizeInBits == 64, "DBL state must have sizeInBits=64");
                Debug.Assert(bitOffset == 0, "DBL state must be byte-aligned");
                *(double *)valuePtr = value;
            }
            else
            {
                throw new InvalidOperationException($"State format '{format}' is not supported as floating-point format");
            }
        }
Пример #12
0
        public static string CreateFakeString(string targetString = null)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(targetString ?? "hello worD");
            string str   = MemoryHelpers.MakeString(bytes);

            return(str);
        }
Пример #13
0
 public AudioBuffer(int sizeInBytes, BufferFlags flags = BufferFlags.EndOfStream)
 {
     Flags            = flags;
     AudioBytes       = sizeInBytes;
     AudioDataPointer = MemoryHelpers.AllocateMemory(sizeInBytes);
     _ownsBuffer      = true;
 }
Пример #14
0
 public InterfaceArray(params T[] array)
 {
     if (array != null)
     {
         var length = unchecked ((uint)array.Length);
         if (length != 0)
         {
             _values        = new T[length];
             _nativePointer = MemoryHelpers.AllocateMemory(length * (uint)IntPtr.Size);
             for (var i = 0; i < length; i++)
             {
                 this[i] = array[i];
             }
         }
         else
         {
             _nativePointer = default;
             _values        = null;
         }
     }
     else
     {
         _nativePointer = default;
         _values        = null;
     }
 }
Пример #15
0
        public void Write(void *statePtr, PrimitiveValue value)
        {
            var valuePtr = (byte *)statePtr + (int)byteOffset;

            if (format == kTypeBit || format == kTypeSBit)
            {
                if (sizeInBits > 32)
                {
                    throw new NotImplementedException(
                              "Cannot yet write primitive values into bitfields wider than 32 bits");
                }

                if (sizeInBits == 1)
                {
                    MemoryHelpers.WriteSingleBit(valuePtr, bitOffset, value.ToBool());
                }
                else
                {
                    MemoryHelpers.WriteIntFromMultipleBits(valuePtr, bitOffset, sizeInBits, value.ToInt());
                }
            }
            else if (format == kTypeFloat)
            {
                Debug.Assert(sizeInBits == 32, "FLT state must have sizeInBits=32");
                Debug.Assert(bitOffset == 0, "FLT state must be byte-aligned");
                *(float *)valuePtr = value.ToFloat();
            }
            else
            {
                throw new NotImplementedException(string.Format(
                                                      "Writing primitive value of type '{0}' into state block with format '{1}'", value.valueType,
                                                      format));
            }
        }
Пример #16
0
        public override void Update()
        {
            if (_memory == null)
            {
                var tempProcess = MemoryHelpers.GetProcessIfRunning(ProcessNames[0]);
                if (tempProcess == null)
                {
                    return;
                }

                _memory = new Memory(tempProcess);
            }

            if (ProfileModel == null || DataModel == null || _memory == null)
            {
                return;
            }

            var offsets       = _pointer.GameAddresses.First(ga => ga.Description == "PlayerBase").ToString();
            var baseAddress   = _memory.GetAddress("\"Terraria.exe\"" + offsets);
            var basePointer   = new IntPtr(_memory.ReadInt32(baseAddress));
            var playerPointer = new IntPtr(_memory.ReadInt32(basePointer + 0x18));

            var dataModel = (TerrariaDataModel)DataModel;

            dataModel.Hp        = _memory.ReadInt32(playerPointer + 0x340);
            dataModel.MaxHp     = _memory.ReadInt32(playerPointer + 0x338);
            dataModel.Mana      = _memory.ReadInt32(playerPointer + 0x344);
            dataModel.MaxMana   = _memory.ReadInt32(playerPointer + 0x348);
            dataModel.Breath    = _memory.ReadInt32(playerPointer + 0x2B4);
            dataModel.MaxBreath = _memory.ReadInt32(playerPointer + 0x2B0);
            dataModel.InWater   = Convert.ToBoolean(_memory.ReadInt32(playerPointer + 0x1D));
            dataModel.InLava    = Convert.ToBoolean(_memory.ReadInt32(playerPointer + 0x20));
        }
Пример #17
0
        ////TODO: expose the checks for default state

        /// <summary>
        /// Check if the given state corresponds to the default state of the control/device.
        /// </summary>
        /// <param name="statePtr">Pointer to a state buffer or null to use <see cref="currentStatePtr"/>.</param>
        /// <param name="maskPtr">If not null, any bits set to true in the buffer will be ignored. This can be used
        /// to mask out noise.</param>
        /// <returns>True if the control/device is in its default state.</returns>
        /// <remarks>
        /// Note that default does not equate all zeroes. Stick axes, for example, that are stored as unsigned byte
        /// values will have their resting position at 127 and not at 0. This is why we explicitly store default
        /// state in a memory buffer instead of assuming zeroes.
        /// </remarks>
        /// <seealso cref="InputStateBuffers.defaultStateBuffer"/>
        internal unsafe bool CheckStateIsAtDefault(void *statePtr = null, void *maskPtr = null)
        {
            ////REVIEW: for compound controls, do we want to go check leaves so as to not pick up on non-control noise in the state?
            ////        e.g. from HID input reports

            if (statePtr == null)
            {
                statePtr = currentStatePtr;
            }

            var defaultValuePtr = (byte *)defaultStatePtr + (int)m_StateBlock.byteOffset;
            var valuePtr        = (byte *)statePtr + (int)m_StateBlock.byteOffset;

            if (m_StateBlock.sizeInBits == 1)
            {
                // If we have a mask and the bit is set in the mask, the control is to be ignored
                // and thus we consider it at default value.
                if (maskPtr != null && MemoryHelpers.ReadSingleBit(maskPtr, m_StateBlock.bitOffset))
                {
                    return(true);
                }

                return(MemoryHelpers.ReadSingleBit(valuePtr, m_StateBlock.bitOffset) ==
                       MemoryHelpers.ReadSingleBit(defaultValuePtr, m_StateBlock.bitOffset));
            }

            return(MemoryHelpers.MemCmpBitRegion(defaultValuePtr, valuePtr,
                                                 m_StateBlock.bitOffset, m_StateBlock.sizeInBits));
        }
Пример #18
0
        /// <summary>
        /// Compare the control's stored state in <paramref name="firstStatePtr"/> to <paramref name="secondStatePtr"/>.
        /// </summary>
        /// <param name="firstStatePtr">Memory containing the control's <see cref="InputControl.stateBlock"/>.</param>
        /// <param name="secondStatePtr">Memory containing the control's <see cref="InputControl.stateBlock"/></param>
        /// <param name="maskPtr">Optional mask. If supplied, it will be used to mask the comparison between
        /// <paramref name="firstStatePtr"/> and <paramref name="secondStatePtr"/> such that any bit not set in the
        /// mask will be ignored even if different between the two states. This can be used, for example, to ignore
        /// noise in the state (<see cref="InputControl.noiseMaskPtr"/>).</param>
        /// <returns>True if the state is equivalent in both memory buffers.</returns>
        /// <remarks>
        /// Unlike <see cref="InputControl.CompareValue"/>, this method only compares raw memory state. If used on a stick, for example,
        /// it may mean that this method returns false for two stick values that would compare equal using <see cref="CompareValue"/>
        /// (e.g. if both stick values fall below the deadzone).
        /// </remarks>
        /// <seealso cref="InputControl.CompareValue"/>
        public static unsafe bool CompareState(this InputControl control, void *firstStatePtr, void *secondStatePtr, void *maskPtr = null)
        {
            ////REVIEW: for compound controls, do we want to go check leaves so as to not pick up on non-control noise in the state?
            ////        e.g. from HID input reports; or should we just leave that to maskPtr?

            var firstPtr  = (byte *)firstStatePtr + (int)control.m_StateBlock.byteOffset;
            var secondPtr = (byte *)secondStatePtr + (int)control.m_StateBlock.byteOffset;
            var mask      = maskPtr != null ? (byte *)maskPtr + (int)control.m_StateBlock.byteOffset : null;

            if (control.m_StateBlock.sizeInBits == 1)
            {
                // If we have a mask and the bit is set in the mask, the control is to be ignored
                // and thus we consider it at default value.
                if (mask != null && MemoryHelpers.ReadSingleBit(mask, control.m_StateBlock.bitOffset))
                {
                    return(true);
                }

                return(MemoryHelpers.ReadSingleBit(secondPtr, control.m_StateBlock.bitOffset) ==
                       MemoryHelpers.ReadSingleBit(firstPtr, control.m_StateBlock.bitOffset));
            }

            return(MemoryHelpers.MemCmpBitRegion(firstPtr, secondPtr,
                                                 control.m_StateBlock.bitOffset, control.m_StateBlock.sizeInBits, mask));
        }
Пример #19
0
        /// <summary>
        /// Called when the InputNoiseFilter gets applied to a device, calls down to any individual FilteredElements that need to do any work.
        /// </summary>
        /// <param name="device">The device you want to apply filtering to.</param>
        internal void Apply(InputDevice device)
        {
            if (device == null)
            {
                throw new ArgumentException("No device supplied to apply InputNoiseFilter to.", "device");
            }

            if (IsEmpty())
            {
                return;
            }

            var noiseBitmaskPtr = InputStateBuffers.s_NoiseBitmaskBuffer;

            if (noiseBitmaskPtr == IntPtr.Zero)
            {
                return;
            }

            MemoryHelpers.SetBitsInBuffer(noiseBitmaskPtr, device, true);

            for (var i = 0; i < elements.Length; i++)
            {
                elements[i].Apply(noiseBitmaskPtr, device);
            }
        }
Пример #20
0
        private unsafe string ReadRawValueAsString(InputControl control, byte[] state)
        {
            fixed(byte *statePtr = state)
            {
                var ptr    = statePtr + control.m_StateBlock.byteOffset - m_RootControl.m_StateBlock.byteOffset;
                var format = control.m_StateBlock.format;

                object value = null;

                if (format == InputStateBlock.kTypeBit)
                {
                    if (MemoryHelpers.ReadSingleBit(ptr, control.m_StateBlock.bitOffset))
                    {
                        value = "1";
                    }
                    else
                    {
                        value = "0";
                    }
                }
                else if (format == InputStateBlock.kTypeByte || format == InputStateBlock.kTypeSByte)
                {
                    value = *ptr;
                }
                else if (format == InputStateBlock.kTypeShort)
                {
                    value = *(short *)ptr;
                }
                else if (format == InputStateBlock.kTypeUShort)
                {
                    value = *(ushort *)ptr;
                }
                else if (format == InputStateBlock.kTypeInt)
                {
                    value = *(int *)ptr;
                }
                else if (format == InputStateBlock.kTypeUInt)
                {
                    value = *(uint *)ptr;
                }
                else if (format == InputStateBlock.kTypeFloat)
                {
                    value = *(float *)ptr;
                }
                else if (format == InputStateBlock.kTypeDouble)
                {
                    value = *(double *)ptr;
                }

                // Stringify enum values, for. ex., PointerPhase
                if (value != null && control.valueType.IsEnum)
                {
                    var intValue = Convert.ToInt32(value);
                    value = Enum.ToObject(control.valueType, intValue);
                }

                return(value != null?value.ToString() : null);
            }
        }
Пример #21
0
        public static unsafe AudioBuffer Create <T>(ReadOnlySpan <T> data, BufferFlags flags = BufferFlags.EndOfStream) where T : struct
        {
            var sizeInBytes = data.Length * Unsafe.SizeOf <T>();
            var dataPtr     = MemoryHelpers.AllocateMemory(data.Length);

            MemoryHelpers.CopyMemory(dataPtr, data);
            return(new AudioBuffer(flags, dataPtr, sizeInBytes, true));
        }
Пример #22
0
        public static unsafe AudioBuffer Create <T>(ReadOnlySpan <T> data, BufferFlags flags = BufferFlags.EndOfStream) where T : unmanaged
        {
            int    sizeInBytes = data.Length * sizeof(T);
            IntPtr dataPtr     = MemoryHelpers.AllocateMemory(data.Length);

            MemoryHelpers.CopyMemory(dataPtr, data);
            return(new AudioBuffer(flags, dataPtr, sizeInBytes, true));
        }
            private static void AddBeziersImpl(IntPtr thisPtr, IntPtr beziers, int beziersCount)
            {
                var shadow         = ToShadow <ID2D1SimplifiedGeometrySinkShadow>(thisPtr);
                var callback       = (ID2D1SimplifiedGeometrySink)shadow.Callback;
                var managedBeziers = new BezierSegment[beziersCount];

                MemoryHelpers.Read(beziers, managedBeziers, 0, beziersCount);
                callback.AddBeziers(managedBeziers);
            }
            private static void AddLinesImpl(IntPtr thisPtr, IntPtr points, int pointsCount)
            {
                var shadow        = ToShadow <ID2D1SimplifiedGeometrySinkShadow>(thisPtr);
                var callback      = (ID2D1SimplifiedGeometrySink)shadow.Callback;
                var managedPoints = new PointF[pointsCount];

                MemoryHelpers.Read(points, managedPoints, 0, pointsCount);
                callback.AddLines(managedPoints);
            }
Пример #25
0
 public void OutInterfaceParameters()
 {
     using (SetupTests(false, out var nativeView, out _))
     {
         var test = nativeView.CloneInstance();
         Assert.Equal(1, test.Add(0, 1));
         MemoryHelpers.Dispose(ref test);
     }
 }
Пример #26
0
 public void Dispose()
 {
     if (_ownsBuffer &&
         AudioDataPointer != IntPtr.Zero)
     {
         MemoryHelpers.FreeMemory(AudioDataPointer);
         AudioDataPointer = IntPtr.Zero;
     }
 }
Пример #27
0
        public unsafe int ReadInt(IntPtr statePtr)
        {
            Debug.Assert(sizeInBits != 0);

            var valuePtr = (byte *)statePtr.ToPointer() + (int)byteOffset;

            int value;

            if (format == kTypeInt || format == kTypeUInt)
            {
                Debug.Assert(sizeInBits == 32, "INT and UINT state must have sizeInBits=32");
                Debug.Assert(bitOffset == 0, "INT and UINT state must be byte-aligned");
                value = *(int *)valuePtr;
            }
            else if (format == kTypeBit)
            {
                if (sizeInBits == 1)
                {
                    value = MemoryHelpers.ReadSingleBit(new IntPtr(valuePtr), bitOffset) ? 1 : 0;
                }
                else
                {
                    value = MemoryHelpers.ReadIntFromMultipleBits(new IntPtr(valuePtr), bitOffset, sizeInBits);
                }
            }
            else if (format == kTypeByte)
            {
                Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8");
                Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned");
                value = *valuePtr;
            }
            else if (format == kTypeSByte)
            {
                Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8");
                Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned");
                value = *(sbyte *)valuePtr;
            }
            else if (format == kTypeShort)
            {
                Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16");
                Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned");
                value = *(short *)valuePtr;
            }
            else if (format == kTypeUShort)
            {
                Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16");
                Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned");
                value = *(ushort *)valuePtr;
            }
            else
            {
                throw new Exception(string.Format("State format '{0}' is not supported as integer format", format));
            }

            return(value);
        }
Пример #28
0
    /// <summary>
    /// Initializes a new instance of the <see cref="DataStream"/> class and allocates a new buffer to use as a backing store.
    /// </summary>
    /// <param name="sizeInBytes">The size of the buffer to be allocated, in bytes.</param>
    /// <param name="canRead"><c>true</c> if reading from the buffer should be allowed; otherwise, <c>false</c>.</param>
    /// <param name = "canWrite"><c>true</c> if writing to the buffer should be allowed; otherwise, <c>false</c>.</param>
    public DataStream(int sizeInBytes, bool canRead, bool canWrite)
    {
        Debug.Assert(sizeInBytes > 0);

        _buffer     = (byte *)MemoryHelpers.AllocateMemory((nuint)sizeInBytes);
        _size       = sizeInBytes;
        _ownsBuffer = true;
        CanRead     = canRead;
        CanWrite    = canWrite;
    }
Пример #29
0
            /// <summary>
            /// Called when the InputNoiseFilter gets applied to a device, marks out any controls that can be wholly bitmasked.
            /// </summary>
            /// <param name="noiseFilterBuffer">The noise filter buffer for doing whole control filtering.</param>
            /// <param name="device">The device you want to apply filtering to.</param>
            public void Apply(IntPtr noiseFilterBuffer, InputDevice device)
            {
                if (controlIndex >= device.allControls.Count)
                {
                    throw new IndexOutOfRangeException("InputNoiseFilter has array index beyond total size of device's controls");
                }

                var control = device.allControls[controlIndex];

                MemoryHelpers.SetBitsInBuffer(noiseFilterBuffer, control, false);
            }
Пример #30
0
 public KeyboardState(params Key[] pressedKeys)
 {
     fixed(byte *keysPtr = keys)
     {
         UnsafeUtility.MemClear(keysPtr, kSizeInBytes);
         for (var i = 0; i < pressedKeys.Length; ++i)
         {
             MemoryHelpers.WriteSingleBit(keysPtr, (uint)pressedKeys[i], true);
         }
     }
 }