mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Reduce unnecessary class structure
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.IO.Extensions;
|
||||
using SabreTools.Models.CHD;
|
||||
|
||||
namespace SabreTools.FileTypes.CHD
|
||||
{
|
||||
@@ -9,16 +10,14 @@ namespace SabreTools.FileTypes.CHD
|
||||
/// This is code adapted from chd.h and chd.cpp in MAME
|
||||
/// Additional archival code from https://github.com/rtissera/libchdr/blob/master/src/chd.h
|
||||
/// </summary>
|
||||
public abstract class CHDFile : BaseFile
|
||||
public class CHDFile : BaseFile
|
||||
{
|
||||
#region Private instance variables
|
||||
|
||||
protected const string Signature = "MComprHD";
|
||||
|
||||
/// <summary>
|
||||
/// Model representing the correct CHD header
|
||||
/// </summary>
|
||||
protected Models.CHD.Header? _header;
|
||||
protected Header? _header;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -48,11 +47,11 @@ namespace SabreTools.FileTypes.CHD
|
||||
// Read and return the current CHD
|
||||
return version switch
|
||||
{
|
||||
1 => CHDFileV1.Deserialize(stream),
|
||||
2 => CHDFileV2.Deserialize(stream),
|
||||
3 => CHDFileV3.Deserialize(stream),
|
||||
4 => CHDFileV4.Deserialize(stream),
|
||||
5 => CHDFileV5.Deserialize(stream),
|
||||
1 => DeserializeV1(stream),
|
||||
2 => DeserializeV2(stream),
|
||||
3 => DeserializeV3(stream),
|
||||
4 => DeserializeV4(stream),
|
||||
5 => DeserializeV5(stream),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
@@ -64,6 +63,179 @@ namespace SabreTools.FileTypes.CHD
|
||||
|
||||
#endregion
|
||||
|
||||
#region Deserializers
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V1
|
||||
/// </summary>
|
||||
private static CHDFile? DeserializeV1(Stream stream)
|
||||
{
|
||||
var header = new HeaderV1();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Constants.SignatureString)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != Constants.HeaderV1Size)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Flags = (Flags)stream.ReadUInt32BigEndian();
|
||||
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
|
||||
if (header.Compression > CompressionType.CHDCOMPRESSION_ZLIB)
|
||||
return null;
|
||||
|
||||
header.HunkSize = stream.ReadUInt32BigEndian();
|
||||
header.TotalHunks = stream.ReadUInt32BigEndian();
|
||||
header.Cylinders = stream.ReadUInt32BigEndian();
|
||||
header.Heads = stream.ReadUInt32BigEndian();
|
||||
header.Sectors = stream.ReadUInt32BigEndian();
|
||||
header.MD5 = stream.ReadBytes(16);
|
||||
header.ParentMD5 = stream.ReadBytes(16);
|
||||
|
||||
return new CHDFile { _header = header, MD5 = header.MD5 };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V2
|
||||
/// </summary>
|
||||
private static CHDFile? DeserializeV2(Stream stream)
|
||||
{
|
||||
var header = new HeaderV2();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Constants.SignatureString)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != Constants.HeaderV2Size)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Flags = (Flags)stream.ReadUInt32BigEndian();
|
||||
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
|
||||
if (header.Compression > CompressionType.CHDCOMPRESSION_ZLIB)
|
||||
return null;
|
||||
|
||||
header.HunkSize = stream.ReadUInt32BigEndian();
|
||||
header.TotalHunks = stream.ReadUInt32BigEndian();
|
||||
header.Cylinders = stream.ReadUInt32BigEndian();
|
||||
header.Heads = stream.ReadUInt32BigEndian();
|
||||
header.Sectors = stream.ReadUInt32BigEndian();
|
||||
header.MD5 = stream.ReadBytes(16);
|
||||
header.ParentMD5 = stream.ReadBytes(16);
|
||||
header.BytesPerSector = stream.ReadUInt32BigEndian();
|
||||
|
||||
return new CHDFile { _header = header, MD5 = header.MD5 };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V2
|
||||
/// </summary>
|
||||
private static CHDFile? DeserializeV3(Stream stream)
|
||||
{
|
||||
var header = new HeaderV3();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Constants.SignatureString)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != Constants.HeaderV3Size)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Flags = (Flags)stream.ReadUInt32BigEndian();
|
||||
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
|
||||
if (header.Compression > CompressionType.CHDCOMPRESSION_ZLIB_PLUS)
|
||||
return null;
|
||||
|
||||
header.TotalHunks = stream.ReadUInt32BigEndian();
|
||||
header.LogicalBytes = stream.ReadUInt64BigEndian();
|
||||
header.MetaOffset = stream.ReadUInt64BigEndian();
|
||||
header.MD5 = stream.ReadBytes(16);
|
||||
header.ParentMD5 = stream.ReadBytes(16);
|
||||
header.HunkBytes = stream.ReadUInt32BigEndian();
|
||||
header.SHA1 = stream.ReadBytes(20);
|
||||
header.ParentSHA1 = stream.ReadBytes(20);
|
||||
|
||||
return new CHDFile { _header = header, MD5 = header.MD5, SHA1 = header.SHA1 };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V4
|
||||
/// </summary>
|
||||
private static CHDFile? DeserializeV4(Stream stream)
|
||||
{
|
||||
var header = new HeaderV4();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Constants.SignatureString)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != Constants.HeaderV4Size)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Flags = (Flags)stream.ReadUInt32BigEndian();
|
||||
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
|
||||
if (header.Compression > CompressionType.CHDCOMPRESSION_AV)
|
||||
return null;
|
||||
|
||||
header.TotalHunks = stream.ReadUInt32BigEndian();
|
||||
header.LogicalBytes = stream.ReadUInt64BigEndian();
|
||||
header.MetaOffset = stream.ReadUInt64BigEndian();
|
||||
header.HunkBytes = stream.ReadUInt32BigEndian();
|
||||
header.SHA1 = stream.ReadBytes(20);
|
||||
header.ParentSHA1 = stream.ReadBytes(20);
|
||||
header.RawSHA1 = stream.ReadBytes(20);
|
||||
|
||||
return new CHDFile { _header = header, SHA1 = header.SHA1 };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V5
|
||||
/// </summary>
|
||||
private static CHDFile? DeserializeV5(Stream stream)
|
||||
{
|
||||
var header = new HeaderV5();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Constants.SignatureString)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != Constants.HeaderV5Size)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Compressors = new uint[4];
|
||||
for (int i = 0; i < header.Compressors.Length; i++)
|
||||
{
|
||||
header.Compressors[i] = stream.ReadUInt32BigEndian();
|
||||
}
|
||||
|
||||
header.LogicalBytes = stream.ReadUInt64BigEndian();
|
||||
header.MapOffset = stream.ReadUInt64BigEndian();
|
||||
header.MetaOffset = stream.ReadUInt64BigEndian();
|
||||
header.HunkBytes = stream.ReadUInt32BigEndian();
|
||||
header.UnitBytes = stream.ReadUInt32BigEndian();
|
||||
header.RawSHA1 = stream.ReadBytes(20);
|
||||
header.SHA1 = stream.ReadBytes(20);
|
||||
header.ParentSHA1 = stream.ReadBytes(20);
|
||||
|
||||
return new CHDFile { _header = header, SHA1 = header.SHA1 };
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
|
||||
/// <summary>
|
||||
@@ -82,17 +254,17 @@ namespace SabreTools.FileTypes.CHD
|
||||
stream.SeekIfPossible();
|
||||
|
||||
// Check the signature
|
||||
if (!string.Equals(tag, Signature, StringComparison.Ordinal))
|
||||
if (!string.Equals(tag, Constants.SignatureString, StringComparison.Ordinal))
|
||||
return 0;
|
||||
|
||||
// Match the version to header length
|
||||
return version switch
|
||||
return (version, length) switch
|
||||
{
|
||||
1 => length == CHDFileV1.HeaderSize ? version : 0,
|
||||
2 => length == CHDFileV2.HeaderSize ? version : 0,
|
||||
3 => length == CHDFileV3.HeaderSize ? version : 0,
|
||||
4 => length == CHDFileV4.HeaderSize ? version : 0,
|
||||
5 => length == CHDFileV5.HeaderSize ? version : 0,
|
||||
(1, Constants.HeaderV1Size) => version,
|
||||
(2, Constants.HeaderV2Size) => version,
|
||||
(3, Constants.HeaderV3Size) => version,
|
||||
(4, Constants.HeaderV4Size) => version,
|
||||
(5, Constants.HeaderV5Size) => version,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.IO.Extensions;
|
||||
using SabreTools.Models.CHD;
|
||||
|
||||
namespace SabreTools.FileTypes.CHD
|
||||
{
|
||||
/// <summary>
|
||||
/// CHD V1 File
|
||||
/// </summary>
|
||||
public class CHDFileV1 : CHDFile
|
||||
{
|
||||
internal const int HeaderSize = 76;
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V1
|
||||
/// </summary>
|
||||
internal static CHDFileV1? Deserialize(Stream stream)
|
||||
{
|
||||
var header = new HeaderV1();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Signature)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != HeaderSize)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Flags = (Flags)stream.ReadUInt32BigEndian();
|
||||
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
|
||||
if (header.Compression > CompressionType.CHDCOMPRESSION_ZLIB)
|
||||
return null;
|
||||
|
||||
header.HunkSize = stream.ReadUInt32BigEndian();
|
||||
header.TotalHunks = stream.ReadUInt32BigEndian();
|
||||
header.Cylinders = stream.ReadUInt32BigEndian();
|
||||
header.Heads = stream.ReadUInt32BigEndian();
|
||||
header.Sectors = stream.ReadUInt32BigEndian();
|
||||
header.MD5 = stream.ReadBytes(16);
|
||||
header.ParentMD5 = stream.ReadBytes(16);
|
||||
|
||||
return new CHDFileV1 { _header = header, MD5 = header.MD5 };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.IO.Extensions;
|
||||
using SabreTools.Models.CHD;
|
||||
|
||||
namespace SabreTools.FileTypes.CHD
|
||||
{
|
||||
/// <summary>
|
||||
/// CHD V2 File
|
||||
/// </summary>
|
||||
public class CHDFileV2 : CHDFile
|
||||
{
|
||||
internal const int HeaderSize = 80;
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V2
|
||||
/// </summary>
|
||||
internal static CHDFileV2? Deserialize(Stream stream)
|
||||
{
|
||||
var header = new HeaderV2();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Signature)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != HeaderSize)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Flags = (Flags)stream.ReadUInt32BigEndian();
|
||||
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
|
||||
if (header.Compression > CompressionType.CHDCOMPRESSION_ZLIB)
|
||||
return null;
|
||||
|
||||
header.HunkSize = stream.ReadUInt32BigEndian();
|
||||
header.TotalHunks = stream.ReadUInt32BigEndian();
|
||||
header.Cylinders = stream.ReadUInt32BigEndian();
|
||||
header.Heads = stream.ReadUInt32BigEndian();
|
||||
header.Sectors = stream.ReadUInt32BigEndian();
|
||||
header.MD5 = stream.ReadBytes(16);
|
||||
header.ParentMD5 = stream.ReadBytes(16);
|
||||
header.BytesPerSector = stream.ReadUInt32BigEndian();
|
||||
|
||||
return new CHDFileV2 { _header = header, MD5 = header.MD5 };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.IO.Extensions;
|
||||
using SabreTools.Models.CHD;
|
||||
|
||||
namespace SabreTools.FileTypes.CHD
|
||||
{
|
||||
/// <summary>
|
||||
/// CHD V3 File
|
||||
/// </summary>
|
||||
public class CHDFileV3 : CHDFile
|
||||
{
|
||||
internal const int HeaderSize = 120;
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V3
|
||||
/// </summary>
|
||||
internal static CHDFileV3? Deserialize(Stream stream)
|
||||
{
|
||||
var header = new HeaderV3();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Signature)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != HeaderSize)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Flags = (Flags)stream.ReadUInt32BigEndian();
|
||||
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
|
||||
if (header.Compression > CompressionType.CHDCOMPRESSION_ZLIB_PLUS)
|
||||
return null;
|
||||
|
||||
header.TotalHunks = stream.ReadUInt32BigEndian();
|
||||
header.LogicalBytes = stream.ReadUInt64BigEndian();
|
||||
header.MetaOffset = stream.ReadUInt64BigEndian();
|
||||
header.MD5 = stream.ReadBytes(16);
|
||||
header.ParentMD5 = stream.ReadBytes(16);
|
||||
header.HunkBytes = stream.ReadUInt32BigEndian();
|
||||
header.SHA1 = stream.ReadBytes(20);
|
||||
header.ParentSHA1 = stream.ReadBytes(20);
|
||||
|
||||
return new CHDFileV3 { _header = header, MD5 = header.MD5, SHA1 = header.SHA1 };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.IO.Extensions;
|
||||
using SabreTools.Models.CHD;
|
||||
|
||||
namespace SabreTools.FileTypes.CHD
|
||||
{
|
||||
/// <summary>
|
||||
/// CHD V4 File
|
||||
/// </summary>
|
||||
public class CHDFileV4 : CHDFile
|
||||
{
|
||||
internal const int HeaderSize = 108;
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V4
|
||||
/// </summary>
|
||||
internal static CHDFileV4? Deserialize(Stream stream)
|
||||
{
|
||||
var header = new HeaderV4();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Signature)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != HeaderSize)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Flags = (Flags)stream.ReadUInt32BigEndian();
|
||||
header.Compression = (CompressionType)stream.ReadUInt32BigEndian();
|
||||
if (header.Compression > CompressionType.CHDCOMPRESSION_AV)
|
||||
return null;
|
||||
|
||||
header.TotalHunks = stream.ReadUInt32BigEndian();
|
||||
header.LogicalBytes = stream.ReadUInt64BigEndian();
|
||||
header.MetaOffset = stream.ReadUInt64BigEndian();
|
||||
header.HunkBytes = stream.ReadUInt32BigEndian();
|
||||
header.SHA1 = stream.ReadBytes(20);
|
||||
header.ParentSHA1 = stream.ReadBytes(20);
|
||||
header.RawSHA1 = stream.ReadBytes(20);
|
||||
|
||||
return new CHDFileV4 { _header = header, SHA1 = header.SHA1 };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.IO.Extensions;
|
||||
using SabreTools.Models.CHD;
|
||||
|
||||
namespace SabreTools.FileTypes.CHD
|
||||
{
|
||||
/// <summary>
|
||||
/// CHD V5 File
|
||||
/// </summary>
|
||||
public class CHDFileV5 : CHDFile
|
||||
{
|
||||
internal const int HeaderSize = 124;
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate the header as if it's V5
|
||||
/// </summary>
|
||||
internal static CHDFileV5? Deserialize(Stream stream)
|
||||
{
|
||||
var header = new HeaderV5();
|
||||
|
||||
byte[] tagBytes = stream.ReadBytes(8);
|
||||
header.Tag = Encoding.ASCII.GetString(tagBytes);
|
||||
if (header.Tag != Signature)
|
||||
return null;
|
||||
|
||||
header.Length = stream.ReadUInt32BigEndian();
|
||||
if (header.Length != HeaderSize)
|
||||
return null;
|
||||
|
||||
header.Version = stream.ReadUInt32BigEndian();
|
||||
header.Compressors = new uint[4];
|
||||
for (int i = 0; i < header.Compressors.Length; i++)
|
||||
{
|
||||
header.Compressors[i] = stream.ReadUInt32BigEndian();
|
||||
}
|
||||
|
||||
header.LogicalBytes = stream.ReadUInt64BigEndian();
|
||||
header.MapOffset = stream.ReadUInt64BigEndian();
|
||||
header.MetaOffset = stream.ReadUInt64BigEndian();
|
||||
header.HunkBytes = stream.ReadUInt32BigEndian();
|
||||
header.UnitBytes = stream.ReadUInt32BigEndian();
|
||||
header.RawSHA1 = stream.ReadBytes(20);
|
||||
header.SHA1 = stream.ReadBytes(20);
|
||||
header.ParentSHA1 = stream.ReadBytes(20);
|
||||
|
||||
return new CHDFileV5 { _header = header, SHA1 = header.SHA1 };
|
||||
}
|
||||
}
|
||||
}
|
||||
17
SabreTools.FileTypes/CHD/Constants.cs
Normal file
17
SabreTools.FileTypes/CHD/Constants.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace SabreTools.FileTypes.CHD
|
||||
{
|
||||
internal static class Constants
|
||||
{
|
||||
public const string SignatureString = "MComprHD";
|
||||
|
||||
#region Header Sizes
|
||||
|
||||
public const int HeaderV1Size = 76;
|
||||
public const int HeaderV2Size = 80;
|
||||
public const int HeaderV3Size = 120;
|
||||
public const int HeaderV4Size = 108;
|
||||
public const int HeaderV5Size = 124;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,7 @@ namespace SabreTools.Test.DatItems
|
||||
{
|
||||
FileType.Folder => new Folder(),
|
||||
FileType.AaruFormat => new AaruFormat(),
|
||||
FileType.CHD => new CHDFileV5(),
|
||||
FileType.CHD => new CHDFile(),
|
||||
FileType.ZipArchive => new ZipArchive(),
|
||||
_ => new BaseFile(),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user