mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Simplify parent CHD class
This commit is contained in:
@@ -30,25 +30,31 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
/// <param name="filename">Filename respresenting the CHD file</param>
|
/// <param name="filename">Filename respresenting the CHD file</param>
|
||||||
public static CHDFile? Create(string filename)
|
public static CHDFile? Create(string filename)
|
||||||
{
|
{
|
||||||
using FileStream fs = File.OpenRead(filename);
|
using var fs = File.OpenRead(filename);
|
||||||
return Create(fs);
|
return Create(fs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new CHDFile from an input stream
|
/// Create a new CHDFile from an input stream
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="chdstream">Stream representing the CHD file</param>
|
/// <param name="stream">Stream representing the CHD file</param>
|
||||||
public static CHDFile? Create(Stream chdstream)
|
public static CHDFile? Create(Stream stream)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Validate that this is actually a valid CHD
|
// Get the detected CHD version
|
||||||
uint version = ValidateHeader(chdstream);
|
uint version = GetVersion(stream);
|
||||||
if (version == 0)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// Read and return the current CHD
|
// Read and return the current CHD
|
||||||
return ReadAsVersion(chdstream, version);
|
return version switch
|
||||||
|
{
|
||||||
|
1 => CHDFileV1.Deserialize(stream),
|
||||||
|
2 => CHDFileV2.Deserialize(stream),
|
||||||
|
3 => CHDFileV3.Deserialize(stream),
|
||||||
|
4 => CHDFileV4.Deserialize(stream),
|
||||||
|
5 => CHDFileV5.Deserialize(stream),
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
@@ -67,13 +73,13 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Header Parsing
|
#region Helpers
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validate the header values
|
/// Get the matching CHD version, if possible
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Matching version, 0 if none</returns>
|
/// <returns>Matching version, 0 if none</returns>
|
||||||
private static uint ValidateHeader(Stream stream)
|
private static uint GetVersion(Stream stream)
|
||||||
{
|
{
|
||||||
// Read the header values
|
// Read the header values
|
||||||
byte[] tagBytes = stream.ReadBytes(8);
|
byte[] tagBytes = stream.ReadBytes(8);
|
||||||
@@ -100,25 +106,6 @@ namespace SabreTools.FileTypes.CHD
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a stream as a particular CHD version
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="stream">CHD file as a stream</param>
|
|
||||||
/// <param name="version">CHD version to parse</param>
|
|
||||||
/// <returns>Populated CHD file, null on failure</returns>
|
|
||||||
private static CHDFile? ReadAsVersion(Stream stream, uint version)
|
|
||||||
{
|
|
||||||
return version switch
|
|
||||||
{
|
|
||||||
1 => CHDFileV1.Deserialize(stream),
|
|
||||||
2 => CHDFileV2.Deserialize(stream),
|
|
||||||
3 => CHDFileV3.Deserialize(stream),
|
|
||||||
4 => CHDFileV4.Deserialize(stream),
|
|
||||||
5 => CHDFileV5.Deserialize(stream),
|
|
||||||
_ => null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user