mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[CHDFile, FileTools] Move static functions to FileTools
This commit is contained in:
87
SabreTools.Library/External/CHDFile.cs
vendored
87
SabreTools.Library/External/CHDFile.cs
vendored
@@ -1,15 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
using SabreTools.Library.Data;
|
using SabreTools.Library.Data;
|
||||||
using SabreTools.Library.Items;
|
|
||||||
using SabreTools.Library.Tools;
|
|
||||||
|
|
||||||
#if MONO
|
#if MONO
|
||||||
using System.IO;
|
using System.IO;
|
||||||
#else
|
#else
|
||||||
using BinaryReader = System.IO.BinaryReader;
|
using BinaryReader = System.IO.BinaryReader;
|
||||||
using FileStream = System.IO.FileStream;
|
|
||||||
using SeekOrigin = System.IO.SeekOrigin;
|
using SeekOrigin = System.IO.SeekOrigin;
|
||||||
|
using Stream = System.IO.Stream;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace SabreTools.Library.External
|
namespace SabreTools.Library.External
|
||||||
@@ -89,8 +87,6 @@ namespace SabreTools.Library.External
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Instance Methods
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -313,86 +309,5 @@ namespace SabreTools.Library.External
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#endregion // Instance Methods
|
|
||||||
|
|
||||||
#region Static Methods
|
|
||||||
|
|
||||||
#region File Information
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get internal metadata from a CHD
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="filename">Filename of possible CHD</param>
|
|
||||||
/// <returns>A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Original code had a "writable" param. This is not required for metadata checking
|
|
||||||
/// </remarks>
|
|
||||||
public static DatItem GetCHDInfo(string filename)
|
|
||||||
{
|
|
||||||
FileStream fs = FileTools.TryOpenRead(filename);
|
|
||||||
DatItem datItem = GetCHDInfo(fs);
|
|
||||||
fs.Dispose();
|
|
||||||
return datItem;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get if file is a valid CHD
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="filename">Filename of possible CHD</param>
|
|
||||||
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
|
|
||||||
public static bool IsValidCHD(string filename)
|
|
||||||
{
|
|
||||||
DatItem datItem = GetCHDInfo(filename);
|
|
||||||
return datItem != null
|
|
||||||
&& datItem.Type == ItemType.Disk
|
|
||||||
&& ((Disk)datItem).SHA1 != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Stream Information
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get internal metadata from a CHD
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="fs">Stream of possible CHD</param>
|
|
||||||
/// <returns>A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Original code had a "writable" param. This is not required for metadata checking
|
|
||||||
/// </remarks>
|
|
||||||
public static DatItem GetCHDInfo(Stream fs)
|
|
||||||
{
|
|
||||||
// Create a blank Disk to populate and return
|
|
||||||
Disk datItem = new Disk();
|
|
||||||
|
|
||||||
// Get a CHD object to store the data
|
|
||||||
CHDFile chd = new CHDFile(fs);
|
|
||||||
|
|
||||||
// Get the SHA-1 from the chd
|
|
||||||
byte[] sha1 = chd.GetSHA1FromHeader();
|
|
||||||
|
|
||||||
// Set the SHA-1 of the Disk to return
|
|
||||||
datItem.SHA1 = (sha1 == null ? null : BitConverter.ToString(sha1).Replace("-", string.Empty).ToLowerInvariant());
|
|
||||||
|
|
||||||
return datItem;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get if stream is a valid CHD
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="fs">Stream of possible CHD</param>
|
|
||||||
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
|
|
||||||
public static bool IsValidCHD(Stream fs)
|
|
||||||
{
|
|
||||||
DatItem datItem = GetCHDInfo(fs);
|
|
||||||
return datItem != null
|
|
||||||
&& datItem.Type == ItemType.Disk
|
|
||||||
&& ((Disk)datItem).SHA1 != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#endregion //Static Methods
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -267,6 +267,35 @@ namespace SabreTools.Library.Tools
|
|||||||
return datItem;
|
return datItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get internal metadata from a CHD
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">Filename of possible CHD</param>
|
||||||
|
/// <returns>A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// Original code had a "writable" param. This is not required for metadata checking
|
||||||
|
/// </remarks>
|
||||||
|
public static DatItem GetCHDInfo(string input)
|
||||||
|
{
|
||||||
|
FileStream fs = FileTools.TryOpenRead(input);
|
||||||
|
DatItem datItem = GetCHDInfo(fs);
|
||||||
|
fs.Dispose();
|
||||||
|
return datItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get if file is a valid CHD
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">Filename of possible CHD</param>
|
||||||
|
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
|
||||||
|
public static bool IsValidCHD(string input)
|
||||||
|
{
|
||||||
|
DatItem datItem = GetCHDInfo(input);
|
||||||
|
return datItem != null
|
||||||
|
&& datItem.Type == ItemType.Disk
|
||||||
|
&& ((Disk)datItem).SHA1 != null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieve a list of files from a directory recursively in proper order
|
/// Retrieve a list of files from a directory recursively in proper order
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -739,7 +768,7 @@ namespace SabreTools.Library.Tools
|
|||||||
long offset = 0, bool keepReadOpen = false, bool ignorechd = true)
|
long offset = 0, bool keepReadOpen = false, bool ignorechd = true)
|
||||||
{
|
{
|
||||||
// We first check to see if it's a CHD
|
// We first check to see if it's a CHD
|
||||||
if (ignorechd == false && CHDFile.IsValidCHD(input))
|
if (ignorechd == false && IsValidCHD(input))
|
||||||
{
|
{
|
||||||
// Seek to the starting position, if one is set
|
// Seek to the starting position, if one is set
|
||||||
try
|
try
|
||||||
@@ -763,7 +792,7 @@ namespace SabreTools.Library.Tools
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the Disk from the information
|
// Get the Disk from the information
|
||||||
DatItem disk = CHDFile.GetCHDInfo(input);
|
DatItem disk = GetCHDInfo(input);
|
||||||
|
|
||||||
// Seek to the beginning of the stream if possible
|
// Seek to the beginning of the stream if possible
|
||||||
try
|
try
|
||||||
@@ -931,6 +960,44 @@ namespace SabreTools.Library.Tools
|
|||||||
return rom;
|
return rom;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get internal metadata from a CHD
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">Stream of possible CHD</param>
|
||||||
|
/// <returns>A Disk object with internal SHA-1 on success, null on error, empty Disk otherwise</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// Original code had a "writable" param. This is not required for metadata checking
|
||||||
|
/// </remarks>
|
||||||
|
public static DatItem GetCHDInfo(Stream input)
|
||||||
|
{
|
||||||
|
// Create a blank Disk to populate and return
|
||||||
|
Disk datItem = new Disk();
|
||||||
|
|
||||||
|
// Get a CHD object to store the data
|
||||||
|
CHDFile chd = new CHDFile(input);
|
||||||
|
|
||||||
|
// Get the SHA-1 from the chd
|
||||||
|
byte[] sha1 = chd.GetSHA1FromHeader();
|
||||||
|
|
||||||
|
// Set the SHA-1 of the Disk to return
|
||||||
|
datItem.SHA1 = (sha1 == null ? null : BitConverter.ToString(sha1).Replace("-", string.Empty).ToLowerInvariant());
|
||||||
|
|
||||||
|
return datItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get if stream is a valid CHD
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">Stream of possible CHD</param>
|
||||||
|
/// <returns>True if a the file is a valid CHD, false otherwise</returns>
|
||||||
|
public static bool IsValidCHD(Stream input)
|
||||||
|
{
|
||||||
|
DatItem datItem = GetCHDInfo(input);
|
||||||
|
return datItem != null
|
||||||
|
&& datItem.Type == ItemType.Disk
|
||||||
|
&& ((Disk)datItem).SHA1 != null;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Stream Manipulation
|
#region Stream Manipulation
|
||||||
|
|||||||
Reference in New Issue
Block a user