2024-11-13 03:55:33 -05:00
|
|
|
|
using System.IO;
|
2024-10-20 00:24:00 -04:00
|
|
|
|
using SabreTools.Models.CHD;
|
2017-10-31 01:06:56 -07:00
|
|
|
|
|
2020-12-10 22:31:23 -08:00
|
|
|
|
namespace SabreTools.FileTypes.CHD
|
2017-10-31 01:06:56 -07:00
|
|
|
|
{
|
2024-10-20 00:24:00 -04:00
|
|
|
|
public class CHDFile : BaseFile
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2025-01-05 21:11:14 -05:00
|
|
|
|
#region Fields
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Internal MD5 hash of the file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public byte[]? InternalMD5 { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Internal SHA-1 hash of the file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public byte[]? InternalSHA1 { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2019-02-08 20:51:44 -08:00
|
|
|
|
#region Private instance variables
|
|
|
|
|
|
|
2024-10-20 00:03:29 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Model representing the correct CHD header
|
|
|
|
|
|
/// </summary>
|
2024-10-20 00:24:00 -04:00
|
|
|
|
protected Header? _header;
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new CHDFile from an input file
|
|
|
|
|
|
/// </summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="filename">Filename respresenting the CHD file</param>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static CHDFile? Create(string filename)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2025-01-04 23:52:16 -05:00
|
|
|
|
using Stream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
2020-12-14 16:01:28 -08:00
|
|
|
|
return Create(fs);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// Create a new CHDFile from an input stream
|
2019-02-08 20:51:44 -08:00
|
|
|
|
/// </summary>
|
2024-10-20 00:06:40 -04:00
|
|
|
|
/// <param name="stream">Stream representing the CHD file</param>
|
|
|
|
|
|
public static CHDFile? Create(Stream stream)
|
2019-02-08 20:51:44 -08:00
|
|
|
|
{
|
2020-07-19 21:59:34 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-11-13 03:55:33 -05:00
|
|
|
|
var header = Serialization.Deserializers.CHD.DeserializeStream(stream);
|
|
|
|
|
|
return header switch
|
2024-10-20 00:06:40 -04:00
|
|
|
|
{
|
2025-01-05 21:11:14 -05:00
|
|
|
|
HeaderV1 v1 => new CHDFile
|
|
|
|
|
|
{
|
|
|
|
|
|
_header = header,
|
|
|
|
|
|
InternalMD5 = v1.MD5,
|
|
|
|
|
|
},
|
|
|
|
|
|
HeaderV2 v2 => new CHDFile
|
|
|
|
|
|
{
|
|
|
|
|
|
_header = header,
|
|
|
|
|
|
InternalMD5 = v2.MD5,
|
|
|
|
|
|
},
|
|
|
|
|
|
HeaderV3 v3 => new CHDFile
|
|
|
|
|
|
{
|
|
|
|
|
|
_header = header,
|
|
|
|
|
|
InternalMD5 = v3.MD5,
|
|
|
|
|
|
InternalSHA1 = v3.SHA1,
|
|
|
|
|
|
},
|
|
|
|
|
|
HeaderV4 v4 => new CHDFile
|
|
|
|
|
|
{
|
|
|
|
|
|
_header = header,
|
|
|
|
|
|
InternalSHA1 = v4.SHA1,
|
|
|
|
|
|
},
|
|
|
|
|
|
HeaderV5 v5 => new CHDFile
|
|
|
|
|
|
{
|
|
|
|
|
|
_header = header,
|
|
|
|
|
|
InternalSHA1 = v5.SHA1,
|
|
|
|
|
|
},
|
2024-10-20 00:06:40 -04:00
|
|
|
|
_ => null,
|
|
|
|
|
|
};
|
2020-07-19 21:59:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#endregion
|
2019-02-08 20:51:44 -08:00
|
|
|
|
}
|
2017-10-31 01:06:56 -07:00
|
|
|
|
}
|