2016-02-23 06:52:48 +00:00
|
|
|
|
using System;
|
2021-12-21 02:37:45 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2016-02-23 06:52:48 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-02-23 06:52:48 +00:00
|
|
|
|
{
|
2020-03-20 22:18:59 +00:00
|
|
|
|
public static class BinLinker
|
2016-04-19 01:21:50 +00:00
|
|
|
|
{
|
2018-08-09 04:27:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Unpacks a BinLinkerAccessor generated file container into individual arrays.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileData">Packed data</param>
|
|
|
|
|
/// <param name="identifier">Signature expected in the first two bytes (ASCII)</param>
|
|
|
|
|
/// <returns>Unpacked array containing all files that were packed.</returns>
|
2021-12-21 02:37:45 +00:00
|
|
|
|
public static byte[][] Unpack(ReadOnlySpan<byte> fileData, string identifier)
|
2016-04-19 01:21:50 +00:00
|
|
|
|
{
|
2021-03-14 23:16:55 +00:00
|
|
|
|
#if DEBUG
|
2021-05-18 23:16:48 +00:00
|
|
|
|
System.Diagnostics.Debug.Assert(fileData.Length > 4);
|
|
|
|
|
System.Diagnostics.Debug.Assert(identifier[0] == fileData[0] && identifier[1] == fileData[1]);
|
2021-03-14 23:16:55 +00:00
|
|
|
|
#endif
|
2021-12-21 02:37:45 +00:00
|
|
|
|
MemoryMarshal.TryRead(fileData[4..], out int start);
|
|
|
|
|
MemoryMarshal.TryRead(fileData[2..], out ushort count);
|
|
|
|
|
var offsetBytes = fileData[8..(8 + (count * sizeof(int)))];
|
|
|
|
|
var offsets = MemoryMarshal.Cast<byte, int>(offsetBytes);
|
|
|
|
|
|
2017-03-17 06:16:11 +00:00
|
|
|
|
byte[][] returnData = new byte[count][];
|
2021-12-21 02:37:45 +00:00
|
|
|
|
for (int i = 0; i < offsets.Length; i++)
|
2016-04-19 01:21:50 +00:00
|
|
|
|
{
|
2021-12-21 02:37:45 +00:00
|
|
|
|
int end = offsets[i];
|
|
|
|
|
returnData[i] = fileData[start..end].ToArray();
|
2017-03-17 06:16:11 +00:00
|
|
|
|
start = end;
|
2016-04-19 01:21:50 +00:00
|
|
|
|
}
|
2017-03-17 06:16:11 +00:00
|
|
|
|
return returnData;
|
2016-04-19 01:21:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-23 06:52:48 +00:00
|
|
|
|
}
|