0.2.0-dev Download ZIP
81 lines ·3.1 KB Raw
| 1 | using System.Buffers.Binary; |
| 2 | using System.Text; |
| 3 | |
| 4 | namespace EvilEngine.Adapters.Tests.Support; |
| 5 | |
| 6 | public static class Ps3Files |
| 7 | { |
| 8 | public static byte[] ParamSfo(params (string Key, object Value)[] values) |
| 9 | { |
| 10 | var keys = new MemoryStream(); |
| 11 | var data = new MemoryStream(); |
| 12 | var entries = new List<(int Key, ushort Format, int Length, int Max, int Data)>(); |
| 13 | foreach (var (key, value) in values.OrderBy(value => value.Key, StringComparer.Ordinal)) |
| 14 | { |
| 15 | var keyAt = (int)keys.Length; |
| 16 | keys.Write(Encoding.ASCII.GetBytes(key + "\0")); |
| 17 | var dataAt = (int)data.Length; |
| 18 | byte[] bytes; |
| 19 | ushort format; |
| 20 | if (value is uint number) |
| 21 | { |
| 22 | bytes = new byte[4]; |
| 23 | BinaryPrimitives.WriteUInt32LittleEndian(bytes, number); |
| 24 | format = 0x0404; |
| 25 | } |
| 26 | else |
| 27 | { |
| 28 | bytes = Encoding.UTF8.GetBytes((string)value + "\0"); |
| 29 | format = 0x0204; |
| 30 | } |
| 31 | |
| 32 | var max = (bytes.Length + 3) & ~3; |
| 33 | data.Write(bytes); |
| 34 | data.Write(new byte[max - bytes.Length]); |
| 35 | entries.Add((keyAt, format, bytes.Length, max, dataAt)); |
| 36 | } |
| 37 | |
| 38 | while (keys.Length % 4 != 0) |
| 39 | keys.WriteByte(0); |
| 40 | |
| 41 | var keyTable = 20 + entries.Count * 16; |
| 42 | var dataTable = keyTable + (int)keys.Length; |
| 43 | var sfo = new byte[dataTable + data.Length]; |
| 44 | "\0PSF"u8.CopyTo(sfo); |
| 45 | BinaryPrimitives.WriteUInt32LittleEndian(sfo.AsSpan(4), 0x101); |
| 46 | BinaryPrimitives.WriteUInt32LittleEndian(sfo.AsSpan(8), (uint)keyTable); |
| 47 | BinaryPrimitives.WriteUInt32LittleEndian(sfo.AsSpan(12), (uint)dataTable); |
| 48 | BinaryPrimitives.WriteUInt32LittleEndian(sfo.AsSpan(16), (uint)entries.Count); |
| 49 | for (var i = 0; i < entries.Count; i++) |
| 50 | { |
| 51 | var entry = sfo.AsSpan(20 + i * 16); |
| 52 | BinaryPrimitives.WriteUInt16LittleEndian(entry, (ushort)entries[i].Key); |
| 53 | BinaryPrimitives.WriteUInt16LittleEndian(entry[2..], entries[i].Format); |
| 54 | BinaryPrimitives.WriteUInt32LittleEndian(entry[4..], (uint)entries[i].Length); |
| 55 | BinaryPrimitives.WriteUInt32LittleEndian(entry[8..], (uint)entries[i].Max); |
| 56 | BinaryPrimitives.WriteUInt32LittleEndian(entry[12..], (uint)entries[i].Data); |
| 57 | } |
| 58 | |
| 59 | keys.ToArray().CopyTo(sfo, keyTable); |
| 60 | data.ToArray().CopyTo(sfo, dataTable); |
| 61 | return sfo; |
| 62 | } |
| 63 | |
| 64 | public static byte[] DiscSfb(string titleId) |
| 65 | { |
| 66 | var sfb = new byte[0x600]; |
| 67 | ".SFB"u8.CopyTo(sfb); |
| 68 | BinaryPrimitives.WriteUInt32BigEndian(sfb.AsSpan(4), 0x10000); |
| 69 | Entry(sfb, 0x20, "HYBRID_FLAG", 0x200, "gu"); |
| 70 | Entry(sfb, 0x40, "TITLE_ID", 0x220, titleId); |
| 71 | return sfb; |
| 72 | } |
| 73 | |
| 74 | private static void Entry(byte[] sfb, int at, string key, int offset, string value) |
| 75 | { |
| 76 | Encoding.ASCII.GetBytes(key).CopyTo(sfb, at); |
| 77 | BinaryPrimitives.WriteUInt32BigEndian(sfb.AsSpan(at + 16), (uint)offset); |
| 78 | BinaryPrimitives.WriteUInt32BigEndian(sfb.AsSpan(at + 20), 0x10); |
| 79 | Encoding.ASCII.GetBytes(value).CopyTo(sfb, offset); |
| 80 | } |
| 81 | } |