public unsafe void IndexOf() { // Pull random elements <Count> times and verify that IndexOf returns correct value. Random randomNumberGenerator = new Random(); // Fill in byte array with ascending numbers. int upperBound = 1000; IntPtr ptr = _currentProcess.Allocate(upperBound * sizeof(int)); var arrayPtr = new FixedArrayPtr <int>((ulong)ptr, upperBound); for (int x = 0; x < upperBound; x++) { arrayPtr.Set(ref x, x); } // Test IndexOf for (int x = 0; x < arrayPtr.Count; x++) { int randomIndex = randomNumberGenerator.Next(0, arrayPtr.Count); arrayPtr.Get(out var value, randomIndex); Assert.Equal(randomIndex, arrayPtr.IndexOf(value)); } // Test nonexisting item. int notInArray = upperBound + 1; Assert.Equal(-1, arrayPtr.IndexOf(notInArray)); // Cleanup _currentProcess.Free(ptr); }
public unsafe void CopyFromCopyTo() { // Allocate array space for a copy and create a new fixed pointer to it. IntPtr copyPtr = _currentProcess.Allocate(_fixedArrayPtr.ArraySize); var arrayCopyPtr = new FixedArrayPtr <AdventurePhysics>((ulong)copyPtr, _fixedArrayPtr.Count); // Copy from original to new array. AdventurePhysics[] physicsArray = new AdventurePhysics[_fixedArrayPtr.Count]; _fixedArrayPtr.CopyTo(physicsArray, _fixedArrayPtr.Count); arrayCopyPtr.CopyFrom(physicsArray, physicsArray.Length); // Check both copies are identical. for (int x = 0; x < physicsArray.Length; x++) { _fixedArrayPtr.Get(out var physicsOriginal, x); arrayCopyPtr.Get(out var physicsCopied, x); if (!physicsOriginal.Equals(physicsCopied)) { Assert.True(false, "All array entries between the two fixed array pointers should be equal."); } } // Cleanup _currentProcess.Free(copyPtr); }
/// <summary> /// Set up this function by copying over an array of Sonic Adventure physics. /// </summary> public FixedArrayPtr() { // Read in the Sonic Adventure physics array from file, then copy over to self-allocated memory. byte[] bytes = File.ReadAllBytes("phys.bin"); _currentProcess = new Reloaded.Memory.Sources.Memory(); _adventurePhysicsArray = _currentProcess.Allocate(bytes.Length); _currentProcess.WriteRaw(_adventurePhysicsArray, bytes); _fixedArrayPtr = new FixedArrayPtr <AdventurePhysics>((ulong)_adventurePhysicsArray, PhysicsArrayLength); }
public unsafe void UseEnumerator() { int[] numbers = { 0, 2, 3, 5, 7, 8, 88, 442 }; fixed(int *numbersPtr = numbers) { var arrayPtr = new FixedArrayPtr <int>((ulong)numbersPtr, numbers.Length); var numbersFromEnumerator = arrayPtr.Select(x => x).ToArray(); Assert.Equal(numbers.Length, numbersFromEnumerator.Length); Assert.Equal(numbers, numbersFromEnumerator); } }
public void MarshalledArrayElementSize() { int marshalledSizeOfStruct = 8 + 16; // 8 = 2 ints // 16 = Inline Array of ANSI Characters int nativeSizeOfStruct = 8 + IntPtr.Size; // Same as above, except treating string as pointer. // Actual elements not needed. IArrayPtr <MarshallingStruct> arrayPtr = new ArrayPtr <MarshallingStruct>(0); IArrayPtr <MarshallingStruct> fixedArrayPtr = new FixedArrayPtr <MarshallingStruct>(0, 0); arrayPtr.MarshalElements = true; fixedArrayPtr.MarshalElements = true; Assert.Equal(marshalledSizeOfStruct, arrayPtr.ElementSize); Assert.Equal(marshalledSizeOfStruct, fixedArrayPtr.ElementSize); arrayPtr.MarshalElements = false; fixedArrayPtr.MarshalElements = false; Assert.Equal(nativeSizeOfStruct, arrayPtr.ElementSize); Assert.Equal(nativeSizeOfStruct, fixedArrayPtr.ElementSize); }
public unsafe void GetElementPointerOutOfRange() { IArrayPtr <MarshallingStruct> fixedArrayPtr = new FixedArrayPtr <MarshallingStruct>(0, 0); Assert.Equal((long)(void *)-1, (long)fixedArrayPtr.GetPointerToElement(1)); }