2025-09-08 21:20:47 -04:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2025-09-27 22:02:10 -04:00
|
|
|
using System.Text.RegularExpressions;
|
2025-09-26 13:06:18 -04:00
|
|
|
using SabreTools.Data.Models.SecuROM;
|
2025-09-08 21:20:47 -04:00
|
|
|
using SabreTools.IO.Extensions;
|
2026-03-24 19:17:25 -04:00
|
|
|
using SabreTools.Numerics.Extensions;
|
|
|
|
|
using SabreTools.Text.Extensions;
|
2025-09-27 21:47:55 -04:00
|
|
|
using static SabreTools.Data.Models.SecuROM.Constants;
|
2025-09-08 21:20:47 -04:00
|
|
|
|
2026-01-27 12:03:01 -05:00
|
|
|
#pragma warning disable IDE0017 // Simplify object initialization
|
|
|
|
|
#pragma warning disable SYSLIB1045 // Convert to 'GeneratedRegexAttribute'
|
2025-09-26 14:57:20 -04:00
|
|
|
namespace SabreTools.Serialization.Readers
|
2025-09-08 21:20:47 -04:00
|
|
|
{
|
2025-09-26 15:02:43 -04:00
|
|
|
public class SecuROMAddD : BaseBinaryReader<AddD>
|
2025-09-08 21:20:47 -04:00
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
2025-09-22 20:07:18 -04:00
|
|
|
public override AddD? Deserialize(Stream? data)
|
2025-09-08 21:20:47 -04:00
|
|
|
{
|
|
|
|
|
// If the data is invalid
|
2026-01-25 14:30:18 -05:00
|
|
|
if (data is null || !data.CanRead)
|
2025-09-08 21:20:47 -04:00
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Cache the current offset
|
|
|
|
|
long initialOffset = data.Position;
|
|
|
|
|
|
2025-09-27 21:46:03 -04:00
|
|
|
var addD = ParseAddD(data);
|
2025-09-27 21:47:55 -04:00
|
|
|
if (addD.Signature != AddDMagicString)
|
2025-09-08 21:20:47 -04:00
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return addD;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Ignore the actual error
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-27 21:46:03 -04:00
|
|
|
/// Parse a Stream into an AddD
|
2025-09-08 21:20:47 -04:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Stream to parse</param>
|
2025-09-27 21:46:03 -04:00
|
|
|
/// <returns>Filled AddD on success, null on error</returns>
|
|
|
|
|
private static AddD ParseAddD(Stream data)
|
2025-09-08 21:20:47 -04:00
|
|
|
{
|
2025-09-22 20:07:18 -04:00
|
|
|
var obj = new AddD();
|
2025-09-08 21:20:47 -04:00
|
|
|
|
2025-09-27 21:47:55 -04:00
|
|
|
byte[] signatureBytes = data.ReadBytes(4);
|
|
|
|
|
obj.Signature = Encoding.ASCII.GetString(signatureBytes);
|
2025-09-08 21:20:47 -04:00
|
|
|
obj.EntryCount = data.ReadUInt32LittleEndian();
|
2025-10-31 13:59:28 -04:00
|
|
|
obj.Version = data.ReadNullTerminatedAnsiString() ?? string.Empty;
|
2025-09-08 21:20:47 -04:00
|
|
|
byte[] buildBytes = data.ReadBytes(4);
|
2025-09-27 21:46:03 -04:00
|
|
|
obj.Build = Encoding.ASCII.GetString(buildBytes);
|
2025-09-27 22:02:10 -04:00
|
|
|
obj.Unknown1 = data.ReadBytes(44);
|
2025-09-27 21:46:03 -04:00
|
|
|
|
2025-09-27 22:02:10 -04:00
|
|
|
// Peek at the next 10 bytes
|
|
|
|
|
long currentOffset = data.Position;
|
|
|
|
|
byte[] temp = data.ReadBytes(10);
|
|
|
|
|
string tempString = Encoding.ASCII.GetString(temp);
|
2025-10-27 22:43:56 -04:00
|
|
|
data.SeekIfPossible(currentOffset, SeekOrigin.Begin);
|
2025-09-27 22:02:10 -04:00
|
|
|
|
|
|
|
|
// If the temp string is a regex match for an ID
|
|
|
|
|
if (Regex.IsMatch(tempString, @"[0-9]{6}-[0-9]{3}"))
|
|
|
|
|
{
|
|
|
|
|
byte[] productIdBytes = data.ReadBytes(10);
|
|
|
|
|
obj.ProductId = Encoding.ASCII.GetString(productIdBytes);
|
|
|
|
|
obj.Unknown2 = data.ReadBytes(58);
|
|
|
|
|
}
|
2025-09-08 21:20:47 -04:00
|
|
|
|
2025-09-22 20:07:18 -04:00
|
|
|
obj.Entries = new AddDEntry[obj.EntryCount];
|
2025-09-08 21:20:47 -04:00
|
|
|
for (int i = 0; i < obj.Entries.Length; i++)
|
|
|
|
|
{
|
2025-09-27 21:46:03 -04:00
|
|
|
var entry = ParseAddDEntry(data);
|
2025-09-08 21:20:47 -04:00
|
|
|
obj.Entries[i] = entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-27 21:46:03 -04:00
|
|
|
/// Parse a Stream into an AddDEntry
|
2025-09-08 21:20:47 -04:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Stream to parse</param>
|
2025-09-27 21:46:03 -04:00
|
|
|
/// <returns>Filled AddDEntry on success, null on error</returns>
|
|
|
|
|
private static AddDEntry ParseAddDEntry(Stream data)
|
2025-09-08 21:20:47 -04:00
|
|
|
{
|
2025-09-22 20:07:18 -04:00
|
|
|
var obj = new AddDEntry();
|
2025-09-08 21:20:47 -04:00
|
|
|
|
|
|
|
|
obj.PhysicalOffset = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.Length = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.Unknown08h = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.Unknown0Ch = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.Unknown10h = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.Unknown14h = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.Unknown18h = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.Unknown1Ch = data.ReadUInt32LittleEndian();
|
2025-10-31 13:59:28 -04:00
|
|
|
obj.FileName = data.ReadNullTerminatedAnsiString() ?? string.Empty;
|
2025-09-08 21:20:47 -04:00
|
|
|
obj.Unknown2Ch = data.ReadUInt32LittleEndian();
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|