From c57148de24c270f4884da9ca6dbb50e470445c03 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 20 Oct 2024 00:06:40 -0400 Subject: [PATCH] Simplify parent CHD class --- SabreTools.FileTypes/CHD/CHDFile.cs | 47 +++++++++++------------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/SabreTools.FileTypes/CHD/CHDFile.cs b/SabreTools.FileTypes/CHD/CHDFile.cs index f0ff75f1..698838a0 100644 --- a/SabreTools.FileTypes/CHD/CHDFile.cs +++ b/SabreTools.FileTypes/CHD/CHDFile.cs @@ -30,25 +30,31 @@ namespace SabreTools.FileTypes.CHD /// Filename respresenting the CHD file public static CHDFile? Create(string filename) { - using FileStream fs = File.OpenRead(filename); + using var fs = File.OpenRead(filename); return Create(fs); } /// /// Create a new CHDFile from an input stream /// - /// Stream representing the CHD file - public static CHDFile? Create(Stream chdstream) + /// Stream representing the CHD file + public static CHDFile? Create(Stream stream) { try { - // Validate that this is actually a valid CHD - uint version = ValidateHeader(chdstream); - if (version == 0) - return null; + // Get the detected CHD version + uint version = GetVersion(stream); // 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 { @@ -67,13 +73,13 @@ namespace SabreTools.FileTypes.CHD #endregion - #region Header Parsing + #region Helpers /// - /// Validate the header values + /// Get the matching CHD version, if possible /// /// Matching version, 0 if none - private static uint ValidateHeader(Stream stream) + private static uint GetVersion(Stream stream) { // Read the header values byte[] tagBytes = stream.ReadBytes(8); @@ -100,25 +106,6 @@ namespace SabreTools.FileTypes.CHD }; } - /// - /// Read a stream as a particular CHD version - /// - /// CHD file as a stream - /// CHD version to parse - /// Populated CHD file, null on failure - 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 } }