public void ReadWriteWithMarshalling(Reloaded.Memory.Sources.IMemory memorySource) { // Prepare IMemoryTools.SwapExternalMemorySource(ref memorySource, _helloWorldProcess); IntPtr pointer = memorySource.Allocate(0x100); /* Start Test */ // Random marshal struct read/write. for (int x = 0; x < 100; x++) { MarshallingStruct randomIntStruct = MarshallingStruct.BuildRandomStruct(); memorySource.Write(pointer, ref randomIntStruct, true); memorySource.Read(pointer, out MarshallingStruct randomValueCopy, true); // Test for equality. Assert.Equal(randomIntStruct, randomValueCopy); // Test references: // If marshalling did not take place, write function would have written pointer to string and read it back in. // If marshalling did take place, a new string was created with the value of the string found in memory. // Set marshal parameter to false in read/write operation above to test this. Assert.False(object.ReferenceEquals(randomIntStruct.Name, randomValueCopy.Name)); } /* End Test */ // Cleanup memorySource.Free(pointer); }
/* Construction/Destruction */ public RandomMarshallingStructGenerator(int megabytes) { int totalBytes = Mathematics.MegaBytesToBytes(megabytes); int structs = Mathematics.BytesToStructCount <RandomIntStruct>(totalBytes); Structs = new MarshallingStruct[structs]; for (int x = 0; x < structs; x++) { Structs[x] = MarshallingStruct.BuildRandomStruct(); } Bytes = StructArray.GetBytes(Structs, true); File.WriteAllBytes(TestFileName, Bytes); }
public void WriteManagedStruct() { var marshallingStruct = new MarshallingStruct { Name = "Chuck Norris", CompressedSize = 420, UncompressedFileSize = 942 }; using (var extendedStream = new LittleEndianMemoryStream(new Reloaded.Memory.Streams.ExtendedMemoryStream())) { extendedStream.Write(marshallingStruct); Struct.FromArray <MarshallingStruct>(extendedStream.ToArray(), out var newStruct); Assert.Equal(marshallingStruct, newStruct); }; }