[FileTypes/] Move some things around

This commit is contained in:
Matt Nadareski
2017-11-02 01:03:36 -07:00
parent f11a0b1038
commit 6cd84ae8aa
11 changed files with 261 additions and 135 deletions

View File

@@ -3614,7 +3614,8 @@ namespace SabreTools.Library.DatFiles
// Special case for if we are in Romba mode (all names are supposed to be SHA-1 hashes)
if (Romba)
{
Rom rom = ArchiveTools.GetTorrentGZFileInfo(item);
GZipArchive archive = new GZipArchive(item);
Rom rom = archive.GetTorrentGZFileInfo();
// If the rom is valid, write it out
if (rom != null && rom.Name != null)
@@ -4014,7 +4015,8 @@ namespace SabreTools.Library.DatFiles
}
// If we have a path, we want to try to get the rom information
Rom fileinfo = ArchiveTools.GetTorrentGZFileInfo(foundpath);
GZipArchive archive = new GZipArchive(foundpath);
Rom fileinfo = archive.GetTorrentGZFileInfo();
// If the file information is null, then we continue
if (fileinfo == null)
@@ -4225,7 +4227,8 @@ namespace SabreTools.Library.DatFiles
usedInternally = true;
// Get the TGZ status for later
bool isTorrentGzip = (ArchiveTools.GetTorrentGZFileInfo(file) != null);
GZipArchive tgz = new GZipArchive(file);
bool isTorrentGzip = tgz.IsTorrent();
// Get the base archive first
BaseArchive archive = ArchiveTools.CreateArchiveFromExistingInput(file);
@@ -4319,7 +4322,9 @@ namespace SabreTools.Library.DatFiles
}
// If we have a very specifc TGZ->TGZ case, just copy it accordingly
if (isZip == false && ArchiveTools.GetTorrentGZFileInfo(file) != null && outputFormat == OutputFormat.TorrentGzip)
GZipArchive tgz = new GZipArchive(file);
Rom rom = tgz.GetTorrentGZFileInfo();
if (isZip == false && rom != null && outputFormat == OutputFormat.TorrentGzip)
{
// Get the proper output path
if (romba)
@@ -4399,7 +4404,9 @@ namespace SabreTools.Library.DatFiles
string machinename = null;
// If we have a very specifc TGZ->TGZ case, just copy it accordingly
if (isZip == false && ArchiveTools.GetTorrentGZFileInfo(file) != null && outputFormat == OutputFormat.TorrentGzip)
GZipArchive tgz = new GZipArchive(file);
Rom rom = tgz.GetTorrentGZFileInfo();
if (isZip == false && rom != null && outputFormat == OutputFormat.TorrentGzip)
{
// Get the proper output path
if (romba)
@@ -4674,7 +4681,8 @@ namespace SabreTools.Library.DatFiles
}
// If we have a path, we want to try to get the rom information
Rom fileinfo = ArchiveTools.GetTorrentGZFileInfo(foundpath);
GZipArchive tgz = new GZipArchive(foundpath);
Rom fileinfo = tgz.GetTorrentGZFileInfo();
// If the file information is null, then we continue
if (fileinfo == null)

View File

@@ -90,6 +90,11 @@ namespace SabreTools.Library.FileTypes
/// <returns>List of empty folders in the archive</returns>
public abstract List<string> GetEmptyFolders();
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
public abstract bool IsTorrent();
#endregion
#region Writing

View File

@@ -182,6 +182,14 @@ namespace SabreTools.Library.FileTypes
throw new NotImplementedException();
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
public override bool IsTorrent()
{
throw new NotImplementedException();
}
#endregion
#region Writing

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using SabreTools.Library.Data;
using SabreTools.Library.Items;
@@ -198,7 +199,7 @@ namespace SabreTools.Library.FileTypes
List<Rom> found = new List<Rom>();
string gamename = Path.GetFileNameWithoutExtension(_filename);
Rom possibleTgz = ArchiveTools.GetTorrentGZFileInfo(_filename);
Rom possibleTgz = GetTorrentGZFileInfo();
// If it was, then add it to the outputs and continue
if (possibleTgz != null && possibleTgz.Name != null)
@@ -255,6 +256,156 @@ namespace SabreTools.Library.FileTypes
return new List<string>();
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
public override bool IsTorrent()
{
// Check for the file existing first
if (!File.Exists(_filename))
{
return false;
}
string datum = Path.GetFileName(_filename).ToLowerInvariant();
long filesize = new FileInfo(_filename).Length;
// If we have the romba depot files, just skip them gracefully
if (datum == ".romba_size" || datum == ".romba_size.backup")
{
Globals.Logger.Verbose("Romba depot file found, skipping: {0}", _filename);
return false;
}
// Check if the name is the right length
if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz")) // TODO: When updating to SHA-256, this needs to update to Constants.SHA256Length
{
Globals.Logger.Warning("Non SHA-1 filename found, skipping: '{0}'", Path.GetFullPath(_filename));
return false;
}
// Check if the file is at least the minimum length
if (filesize < 40 /* bytes */)
{
Globals.Logger.Warning("Possibly corrupt file '{0}' with size {1}", Path.GetFullPath(_filename), Style.GetBytesReadable(filesize));
return false;
}
// Get the Romba-specific header data
byte[] header; // Get preamble header for checking
byte[] headermd5; // MD5
byte[] headercrc; // CRC
ulong headersz; // Int64 size
BinaryReader br = new BinaryReader(FileTools.TryOpenRead(_filename));
header = br.ReadBytes(12);
headermd5 = br.ReadBytes(16);
headercrc = br.ReadBytes(4);
headersz = br.ReadUInt64();
br.Dispose();
// If the header is not correct, return a blank rom
bool correct = true;
for (int i = 0; i < header.Length; i++)
{
// This is a temp fix to ignore the modification time and OS until romba can be fixed
if (i == 4 || i == 5 || i == 6 || i == 7 || i == 9)
{
continue;
}
correct &= (header[i] == Constants.TorrentGZHeader[i]);
}
if (!correct)
{
return false;
}
return true;
}
/// <summary>
/// Retrieve file information for a single torrent GZ file
/// </summary>
/// <returns>Populated DatItem object if success, empty one on error</returns>
public Rom GetTorrentGZFileInfo()
{
// Check for the file existing first
if (!File.Exists(_filename))
{
return null;
}
string datum = Path.GetFileName(_filename).ToLowerInvariant();
long filesize = new FileInfo(_filename).Length;
// If we have the romba depot files, just skip them gracefully
if (datum == ".romba_size" || datum == ".romba_size.backup")
{
Globals.Logger.Verbose("Romba depot file found, skipping: {0}", _filename);
return null;
}
// Check if the name is the right length
if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz")) // TODO: When updating to SHA-256, this needs to update to Constants.SHA256Length
{
Globals.Logger.Warning("Non SHA-1 filename found, skipping: '{0}'", Path.GetFullPath(_filename));
return null;
}
// Check if the file is at least the minimum length
if (filesize < 40 /* bytes */)
{
Globals.Logger.Warning("Possibly corrupt file '{0}' with size {1}", Path.GetFullPath(_filename), Style.GetBytesReadable(filesize));
return null;
}
// Get the Romba-specific header data
byte[] header; // Get preamble header for checking
byte[] headermd5; // MD5
byte[] headercrc; // CRC
ulong headersz; // Int64 size
BinaryReader br = new BinaryReader(FileTools.TryOpenRead(_filename));
header = br.ReadBytes(12);
headermd5 = br.ReadBytes(16);
headercrc = br.ReadBytes(4);
headersz = br.ReadUInt64();
br.Dispose();
// If the header is not correct, return a blank rom
bool correct = true;
for (int i = 0; i < header.Length; i++)
{
// This is a temp fix to ignore the modification time and OS until romba can be fixed
if (i == 4 || i == 5 || i == 6 || i == 7 || i == 9)
{
continue;
}
correct &= (header[i] == Constants.TorrentGZHeader[i]);
}
if (!correct)
{
return null;
}
// Now convert the data and get the right position
string gzmd5 = BitConverter.ToString(headermd5).Replace("-", string.Empty);
string gzcrc = BitConverter.ToString(headercrc).Replace("-", string.Empty);
long extractedsize = (long)headersz;
Rom rom = new Rom
{
Type = ItemType.Rom,
Name = Path.GetFileNameWithoutExtension(_filename).ToLowerInvariant(),
Size = extractedsize,
CRC = gzcrc.ToLowerInvariant(),
MD5 = gzmd5.ToLowerInvariant(),
SHA1 = Path.GetFileNameWithoutExtension(_filename).ToLowerInvariant(), // TODO: When updating to SHA-256, this needs to update to SHA256
MachineName = Path.GetFileNameWithoutExtension(_filename).ToLowerInvariant(),
};
return rom;
}
#endregion
#region Writing

View File

@@ -102,6 +102,14 @@ namespace SabreTools.Library.FileTypes
throw new NotImplementedException();
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
public override bool IsTorrent()
{
throw new NotImplementedException();
}
#endregion
#region Writing

View File

@@ -275,6 +275,14 @@ namespace SabreTools.Library.FileTypes
return empties;
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
public override bool IsTorrent()
{
throw new NotImplementedException();
}
#endregion
#region Writing

View File

@@ -280,6 +280,48 @@ namespace SabreTools.Library.FileTypes
return empties;
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
/// TODO: Finish reading T7z information
public override bool IsTorrent()
{
bool ist7z = false;
if (File.Exists(_filename))
{
try
{
Stream fread = FileTools.TryOpenRead(_filename);
uint ar, offs = 0;
fread.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[128];
ar = (uint)fread.Read(buffer, 0, 4 + Constants.Torrent7ZipSignature.Length + 4);
if (ar < (4 + Constants.Torrent7ZipSignature.Length + 4))
{
if (ar >= Constants.Torrent7ZipSignature.Length + 4)
{
ar -= (uint)(Constants.Torrent7ZipSignature.Length + 4);
}
if (ar <= Constants.Torrent7ZipHeader.Length)
{
ar = (uint)Constants.Torrent7ZipHeader.Length;
}
// memset(buffer+offs+ar,0,crcsz-ar)
}
fread.Dispose();
}
catch
{
Globals.Logger.Warning("File '{0}' could not be opened", _filename);
ist7z = false;
}
}
return ist7z;
}
#endregion
#region Writing

View File

@@ -278,6 +278,14 @@ namespace SabreTools.Library.FileTypes
return empties;
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
public override bool IsTorrent()
{
throw new NotImplementedException();
}
#endregion
#region Writing

View File

@@ -382,6 +382,14 @@ namespace SabreTools.Library.FileTypes
return empties;
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
public override bool IsTorrent()
{
throw new NotImplementedException();
}
#endregion
#region Writing

View File

@@ -111,6 +111,14 @@ namespace SabreTools.Library.FileTypes
throw new NotImplementedException();
}
/// <summary>
/// Check whether the input file is a standardized format
/// </summary>
public override bool IsTorrent()
{
throw new NotImplementedException();
}
#endregion
#region Writing

View File

@@ -131,91 +131,6 @@ namespace SabreTools.Library.Tools
#region Information
/// <summary>
/// Retrieve file information for a single torrent GZ file
/// </summary>
/// <param name="input">Filename to get information from</param>
/// <returns>Populated DatItem object if success, empty one on error</returns>
public static Rom GetTorrentGZFileInfo(string input)
{
// Check for the file existing first
if (!File.Exists(input))
{
return null;
}
string datum = Path.GetFileName(input).ToLowerInvariant();
long filesize = new FileInfo(input).Length;
// If we have the romba depot files, just skip them gracefully
if (datum == ".romba_size" || datum == ".romba_size.backup")
{
Globals.Logger.Verbose("Romba depot file found, skipping: {0}", input);
return null;
}
// Check if the name is the right length
if (!Regex.IsMatch(datum, @"^[0-9a-f]{" + Constants.SHA1Length + @"}\.gz")) // TODO: When updating to SHA-256, this needs to update to Constants.SHA256Length
{
Globals.Logger.Warning("Non SHA-1 filename found, skipping: '{0}'", Path.GetFullPath(input));
return null;
}
// Check if the file is at least the minimum length
if (filesize < 40 /* bytes */)
{
Globals.Logger.Warning("Possibly corrupt file '{0}' with size {1}", Path.GetFullPath(input), Style.GetBytesReadable(filesize));
return null;
}
// Get the Romba-specific header data
byte[] header; // Get preamble header for checking
byte[] headermd5; // MD5
byte[] headercrc; // CRC
ulong headersz; // Int64 size
BinaryReader br = new BinaryReader(FileTools.TryOpenRead(input));
header = br.ReadBytes(12);
headermd5 = br.ReadBytes(16);
headercrc = br.ReadBytes(4);
headersz = br.ReadUInt64();
br.Dispose();
// If the header is not correct, return a blank rom
bool correct = true;
for (int i = 0; i < header.Length; i++)
{
// This is a temp fix to ignore the modification time and OS until romba can be fixed
if (i == 4 || i == 5 || i == 6 || i == 7 || i == 9)
{
continue;
}
correct &= (header[i] == Constants.TorrentGZHeader[i]);
}
if (!correct)
{
return null;
}
// Now convert the data and get the right position
string gzmd5 = BitConverter.ToString(headermd5).Replace("-", string.Empty);
string gzcrc = BitConverter.ToString(headercrc).Replace("-", string.Empty);
long extractedsize = (long)headersz;
Rom rom = new Rom
{
Type = ItemType.Rom,
Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
Size = extractedsize,
CRC = gzcrc.ToLowerInvariant(),
MD5 = gzmd5.ToLowerInvariant(),
SHA1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), // TODO: When updating to SHA-256, this needs to update to SHA256
MachineName = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
};
return rom;
}
/// <summary>
/// Returns the archive type of an input file
/// </summary>
@@ -621,49 +536,6 @@ namespace SabreTools.Library.Tools
}
}
/// <summary>
/// (INCOMPLETE) Get the T7Z status of the file
/// </summary>
/// <param name="filename">Name of the file to check</param>
/// <returns>0 if the file isn't 7z, 1 if the file is t7z, 2 if the file is 7z</returns>
public static int IsT7z(string filename)
{
int ist7z = 0;
if (File.Exists(filename))
{
try
{
Stream fread = FileTools.TryOpenRead(filename);
uint ar, offs = 0;
fread.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[128];
ar = (uint)fread.Read(buffer, 0, 4 + Constants.Torrent7ZipSignature.Length + 4);
if (ar < (4 + Constants.Torrent7ZipSignature.Length + 4))
{
if (ar >= Constants.Torrent7ZipSignature.Length + 4)
{
ar -= (uint)(Constants.Torrent7ZipSignature.Length + 4);
}
if (ar <= Constants.Torrent7ZipHeader.Length)
{
ar = (uint)Constants.Torrent7ZipHeader.Length;
}
// memset(buffer+offs+ar,0,crcsz-ar)
}
fread.Dispose();
}
catch
{
Globals.Logger.Warning("File '{0}' could not be opened", filename);
ist7z = 0;
}
}
return ist7z;
}
#endregion
}
}