Create FileTypeTool and move static methods

This commit is contained in:
Matt Nadareski
2025-01-04 21:17:02 -05:00
parent c449f34410
commit 0cab005a78
12 changed files with 423 additions and 458 deletions

View File

@@ -105,16 +105,16 @@ namespace SabreTools.FileTypes.Archives
catch (EndOfStreamException ex)
{
// Catch this but don't count it as an error because SharpCompress is unsafe
logger.Verbose(ex);
_logger.Verbose(ex);
}
catch (InvalidOperationException ex)
{
logger.Warning(ex);
_logger.Warning(ex);
encounteredErrors = true;
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
encounteredErrors = true;
}
@@ -185,7 +185,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return (null, null);
}
}
@@ -224,7 +224,7 @@ namespace SabreTools.FileTypes.Archives
BaseFile gzipEntryRom = new();
// Perform a quickscan, if flagged to
if (AvailableHashTypes.Length == 1 && AvailableHashTypes[0] == HashType.CRC32)
if (_hashTypes.Length == 1 && _hashTypes[0] == HashType.CRC32)
{
gzipEntryRom.Filename = gamename;
@@ -239,7 +239,7 @@ namespace SabreTools.FileTypes.Archives
var gz = new gZip();
ZipReturn ret = gz.ZipFileOpen(Filename);
ret = gz.ZipFileOpenReadStream(0, out Stream? gzstream, out ulong streamSize);
gzipEntryRom = GetInfo(gzstream, hashes: AvailableHashTypes);
gzipEntryRom = GetInfo(gzstream, hashes: _hashTypes);
gzipEntryRom.Filename = gz.GetLocalFile(0).Filename;
gzipEntryRom.Parent = gamename;
gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null);
@@ -252,7 +252,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return null;
}
@@ -279,21 +279,21 @@ namespace SabreTools.FileTypes.Archives
// If we have the romba depot files, just skip them gracefully
if (datum == ".romba_size" || datum == ".romba_size.backup")
{
logger.Verbose($"Romba depot file found, skipping: {Filename}");
_logger.Verbose($"Romba depot file found, skipping: {Filename}");
return false;
}
// Check if the name is the right length
if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz"))
{
logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'");
_logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'");
return false;
}
// Check if the file is at least the minimum length
if (filesize < 40 /* bytes */)
{
logger.Warning($"Possibly corrupt file '{Path.GetFullPath(Filename)}' with size {filesize}");
_logger.Warning($"Possibly corrupt file '{Path.GetFullPath(Filename)}' with size {filesize}");
return false;
}
@@ -340,21 +340,21 @@ namespace SabreTools.FileTypes.Archives
// If we have the romba depot files, just skip them gracefully
if (datum == ".romba_size" || datum == ".romba_size.backup")
{
logger.Verbose($"Romba depot file found, skipping: {Filename}");
_logger.Verbose($"Romba depot file found, skipping: {Filename}");
return null;
}
// Check if the name is the right length
if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz"))
{
logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'");
_logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'");
return null;
}
// Check if the file is at least the minimum length
if (filesize < 40 /* bytes */)
{
logger.Warning($"Possibly corrupt file '{Path.GetFullPath(Filename)}' with size {filesize}");
_logger.Warning($"Possibly corrupt file '{Path.GetFullPath(Filename)}' with size {filesize}");
return null;
}
@@ -414,7 +414,7 @@ namespace SabreTools.FileTypes.Archives
// Check that the input file exists
if (!File.Exists(inputFile))
{
logger.Warning($"File '{inputFile}' does not exist!");
_logger.Warning($"File '{inputFile}' does not exist!");
return false;
}

View File

@@ -67,16 +67,16 @@ namespace SabreTools.FileTypes.Archives
catch (EndOfStreamException ex)
{
// Catch this but don't count it as an error because SharpCompress is unsafe
logger.Verbose(ex);
_logger.Verbose(ex);
}
catch (InvalidOperationException ex)
{
logger.Warning(ex);
_logger.Warning(ex);
encounteredErrors = true;
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
encounteredErrors = true;
}
@@ -168,7 +168,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return (null, null);
}
#else
@@ -201,7 +201,7 @@ namespace SabreTools.FileTypes.Archives
BaseFile rarEntryRom = new();
// Perform a quickscan, if flagged to
if (AvailableHashTypes.Length == 1 && AvailableHashTypes[0] == HashType.CRC32)
if (_hashTypes.Length == 1 && _hashTypes[0] == HashType.CRC32)
{
rarEntryRom.Size = entry.Size;
rarEntryRom.CRC = BitConverter.GetBytes(entry.Crc);
@@ -210,7 +210,7 @@ namespace SabreTools.FileTypes.Archives
else
{
using Stream entryStream = entry.OpenEntryStream();
rarEntryRom = GetInfo(entryStream, size: entry.Size, hashes: AvailableHashTypes);
rarEntryRom = GetInfo(entryStream, size: entry.Size, hashes: _hashTypes);
}
// Fill in common details and add to the list
@@ -225,7 +225,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return null;
}
@@ -271,7 +271,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
}
return empties;

View File

@@ -118,16 +118,16 @@ namespace SabreTools.FileTypes.Archives
catch (EndOfStreamException ex)
{
// Catch this but don't count it as an error because SharpCompress is unsafe
logger.Verbose(ex);
_logger.Verbose(ex);
}
catch (InvalidOperationException ex)
{
logger.Warning(ex);
_logger.Warning(ex);
encounteredErrors = true;
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
encounteredErrors = true;
}
@@ -221,7 +221,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return (null, null);
}
}
@@ -265,7 +265,7 @@ namespace SabreTools.FileTypes.Archives
// If we get a read error, log it and continue
if (zr != ZipReturn.ZipGood)
{
logger.Warning($"An error occurred while reading archive {Filename}: Zip Error - {zr}");
_logger.Warning($"An error occurred while reading archive {Filename}: Zip Error - {zr}");
zr = zf.ZipFileCloseReadStream();
continue;
}
@@ -274,7 +274,7 @@ namespace SabreTools.FileTypes.Archives
BaseFile zipEntryRom = new();
// Perform a quickscan, if flagged to
if (AvailableHashTypes.Length == 1 && AvailableHashTypes[0] == HashType.CRC32)
if (_hashTypes.Length == 1 && _hashTypes[0] == HashType.CRC32)
{
zipEntryRom.Size = (long)zf.GetLocalFile(i).UncompressedSize;
zipEntryRom.CRC = zf.GetLocalFile(i).CRC;
@@ -282,7 +282,7 @@ namespace SabreTools.FileTypes.Archives
// Otherwise, use the stream directly
else
{
zipEntryRom = GetInfo(readStream, size: (long)zf.GetLocalFile(i).UncompressedSize, hashes: AvailableHashTypes, keepReadOpen: true);
zipEntryRom = GetInfo(readStream, size: (long)zf.GetLocalFile(i).UncompressedSize, hashes: _hashTypes, keepReadOpen: true);
}
// Fill in common details and add to the list
@@ -297,7 +297,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return null;
}
@@ -347,7 +347,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
}
return empties;
@@ -560,7 +560,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
success = false;
}
finally
@@ -774,7 +774,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
success = false;
}

View File

@@ -68,16 +68,16 @@ namespace SabreTools.FileTypes.Archives
catch (EndOfStreamException ex)
{
// Catch this but don't count it as an error because SharpCompress is unsafe
logger.Verbose(ex);
_logger.Verbose(ex);
}
catch (InvalidOperationException ex)
{
logger.Warning(ex);
_logger.Warning(ex);
encounteredErrors = true;
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
encounteredErrors = true;
}
@@ -165,7 +165,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return (null, null);
}
#else
@@ -194,7 +194,7 @@ namespace SabreTools.FileTypes.Archives
BaseFile tarEntryRom = new();
// Perform a quickscan, if flagged to
if (AvailableHashTypes.Length == 1 && AvailableHashTypes[0] == HashType.CRC32)
if (_hashTypes.Length == 1 && _hashTypes[0] == HashType.CRC32)
{
tarEntryRom.Size = entry.Size;
tarEntryRom.CRC = BitConverter.GetBytes(entry.Crc);
@@ -203,7 +203,7 @@ namespace SabreTools.FileTypes.Archives
else
{
using Stream entryStream = entry.OpenEntryStream();
tarEntryRom = GetInfo(entryStream, size: entry.Size, hashes: AvailableHashTypes);
tarEntryRom = GetInfo(entryStream, size: entry.Size, hashes: _hashTypes);
}
// Fill in common details and add to the list
@@ -218,7 +218,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return null;
}
@@ -260,7 +260,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
}
return empties;
@@ -409,7 +409,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
success = false;
}
finally
@@ -578,7 +578,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
success = false;
}
finally

View File

@@ -80,16 +80,16 @@ namespace SabreTools.FileTypes.Archives
catch (EndOfStreamException ex)
{
// Catch this but don't count it as an error because SharpCompress is unsafe
logger.Verbose(ex);
_logger.Verbose(ex);
}
catch (InvalidOperationException ex)
{
logger.Warning(ex);
_logger.Warning(ex);
encounteredErrors = true;
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
encounteredErrors = true;
}
@@ -161,7 +161,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return (null, null);
}
#else
@@ -200,7 +200,7 @@ namespace SabreTools.FileTypes.Archives
BaseFile xzEntryRom = new();
// Perform a quickscan, if flagged to
if (AvailableHashTypes.Length == 1 && AvailableHashTypes[0] == HashType.CRC32)
if (_hashTypes.Length == 1 && _hashTypes[0] == HashType.CRC32)
{
xzEntryRom.Filename = gamename;
@@ -213,7 +213,7 @@ namespace SabreTools.FileTypes.Archives
else
{
var xzStream = new XZStream(File.OpenRead(Filename!));
xzEntryRom = GetInfo(xzStream, hashes: AvailableHashTypes);
xzEntryRom = GetInfo(xzStream, hashes: _hashTypes);
xzEntryRom.Filename = gamename;
xzStream.Dispose();
}
@@ -224,7 +224,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return null;
}
@@ -254,7 +254,7 @@ namespace SabreTools.FileTypes.Archives
// Check if the name is the right length
if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.xz"))
{
logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'");
_logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'");
return false;
}
@@ -276,7 +276,7 @@ namespace SabreTools.FileTypes.Archives
// Check if the name is the right length
if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.xz"))
{
logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'");
_logger.Warning($"Non SHA-1 filename found, skipping: '{Path.GetFullPath(Filename)}'");
return null;
}
@@ -301,7 +301,7 @@ namespace SabreTools.FileTypes.Archives
// Check that the input file exists
if (!File.Exists(inputFile))
{
logger.Warning($"File '{inputFile}' does not exist!");
_logger.Warning($"File '{inputFile}' does not exist!");
return false;
}

View File

@@ -157,16 +157,16 @@ namespace SabreTools.FileTypes.Archives
catch (EndOfStreamException ex)
{
// Catch this but don't count it as an error because SharpCompress is unsafe
logger.Verbose(ex);
_logger.Verbose(ex);
}
catch (InvalidOperationException ex)
{
logger.Warning(ex);
_logger.Warning(ex);
encounteredErrors = true;
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
encounteredErrors = true;
}
@@ -296,7 +296,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return (null, null);
}
}
@@ -345,7 +345,7 @@ namespace SabreTools.FileTypes.Archives
// If we get a read error, log it and continue
if (zr != ZipReturn.ZipGood || readStream == null)
{
logger.Warning($"An error occurred while reading archive {Filename}: Zip Error - {zr}");
_logger.Warning($"An error occurred while reading archive {Filename}: Zip Error - {zr}");
continue;
}
@@ -353,7 +353,7 @@ namespace SabreTools.FileTypes.Archives
var zipEntryRom = new BaseFile();
// Perform a quickscan, if flagged to
if (AvailableHashTypes.Length == 1 && AvailableHashTypes[0] == HashType.CRC32)
if (_hashTypes.Length == 1 && _hashTypes[0] == HashType.CRC32)
{
zipEntryRom.Size = (long)localFile.UncompressedSize;
zipEntryRom.CRC = localFile.CRC;
@@ -363,7 +363,7 @@ namespace SabreTools.FileTypes.Archives
{
zipEntryRom = GetInfo(readStream,
size: (long)localFile.UncompressedSize,
hashes: AvailableHashTypes,
hashes: _hashTypes,
keepReadOpen: true);
}
@@ -406,7 +406,7 @@ namespace SabreTools.FileTypes.Archives
var zipEntryRom = new BaseFile();
// Perform a quickscan, if flagged to
if (AvailableHashTypes.Length == 1 && AvailableHashTypes[0] == HashType.CRC32)
if (_hashTypes.Length == 1 && _hashTypes[0] == HashType.CRC32)
{
zipEntryRom.Size = localFile.Length;
#if NETCOREAPP
@@ -420,7 +420,7 @@ namespace SabreTools.FileTypes.Archives
{
zipEntryRom = GetInfo(readStream,
size: localFile.Length,
hashes: AvailableHashTypes,
hashes: _hashTypes,
keepReadOpen: false);
}
@@ -437,7 +437,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
return null;
}
@@ -532,7 +532,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
}
return empties;
@@ -734,7 +734,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
success = false;
}
finally
@@ -949,7 +949,7 @@ namespace SabreTools.FileTypes.Archives
}
catch (Exception ex)
{
logger.Error(ex);
_logger.Error(ex);
success = false;
}