/// <summary> /// Imports a binary physics file. /// </summary> public void ImportFile(string filePath) { var file = File.ReadAllBytes(filePath); StructArray.FromArray <AdventurePhysics>(file, out var physics, true); // WARNING: Order of AllCharacters must match binary file! foreach (AllCharacters character in (AllCharacters[])Enum.GetValues(typeof(AllCharacters))) { Physics[character] = physics[(int)character]; } }
/// <summary> /// Obtains the AFS header from a specific file path. /// </summary> private AfsFileEntry[] GetEntriesFromFile(string filePath) { using FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8192); var data = new byte[sizeof(AfsHeader)]; stream.Read(data, 0, data.Length); Struct.FromArray(data, out AfsHeader header); data = new byte[sizeof(AfsFileEntry) * header.NumberOfFiles]; stream.Read(data, 0, data.Length); StructArray.FromArray(data, out AfsFileEntry[] entries); return(entries); }
/// <summary> /// Shows some functionality of the <see cref="Struct"/> and <see cref="StructArray"/> utility classes. /// </summary> /// <param name="memory">This object is used to perform memory read/write/free/allocate operations.</param> /// <param name="memoryLocation">Arbitrary location in memory where this tutorial will be held.</param> private static void StructUtilityExample(IMemory memory, IntPtr memoryLocation) { // Under the hood; the IMemory implementations may use a certain struct utility classes known as Struct // and StructArray which provide various methods for struct conversions and general work with structs. // Like earlier; let's load the adventure binary file. byte[] physicsData = File.ReadAllBytes($"phys.bin"); // But this time; do a direct conversion rather than reading from memory. // Note that you don't even need to specify item count this time arounnd. // This is auto-decided from array size, but can be manually overwritten. StructArray.FromArray(physicsData, out AdventurePhysics[] adventurePhysics); // Calculate total array size (in bytes). int arraySize = StructArray.GetSize <AdventurePhysics>(adventurePhysics.Length); // Get raw bytes for the struct. byte[] physicsDataBack = StructArray.GetBytes(adventurePhysics); // You can also read/write structures; as a shorthand to Memory class. StructArray.ToPtr(memoryLocation, adventurePhysics); StructArray.FromPtr(memoryLocation, out AdventurePhysics[] adventurePhysicsCopy, adventurePhysics.Length); // Beware of the double sided blade however. // A. Struct class allows you to change the source read/write source for FromPtr and ToPtr. // B. It affects both Struct and StructArray. // Note: There are also explicit overloads for FromPtr and ToPtr that let you use a source without modifying current source. Struct.Source = memory; // And of course the source is an implementation of IMemory. // Print details. if (physicsDataBack.SequenceEqual(physicsDataBack)) { Console.WriteLine($"Success: Original Physics Data and StructArray.GetBytes() are Equal"); } Console.WriteLine($"Struct Array Size: {arraySize}"); }