mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[ArchiveTools, DATFromDir, DatTools, SimpleSort] More code modularization to make it more stable and clean
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using SharpCompress.Archive;
|
||||
using SharpCompress.Common;
|
||||
@@ -151,5 +153,85 @@ namespace SabreTools.Helper
|
||||
|
||||
return !encounteredErrors;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve file information for a single torrent GZ file
|
||||
/// </summary>
|
||||
/// <param name="input">Filename to get information from</param>
|
||||
/// <returns>Populated RomData object if success, empty one on error</returns>
|
||||
public static RomData GetTorrentGZFileInfo(string input, Logger logger)
|
||||
{
|
||||
string datum = Path.GetFileName(input).ToLowerInvariant();
|
||||
long filesize = new FileInfo(input).Length;
|
||||
|
||||
// Check if the name is the right length
|
||||
if (!Regex.IsMatch(datum, @"^[0-9a-f]{40}\.gz"))
|
||||
{
|
||||
logger.Warning("Non SHA-1 filename found, skipping: '" + datum + "'");
|
||||
return new RomData();
|
||||
}
|
||||
|
||||
// Check if the file is at least the minimum length
|
||||
if (filesize < 32 /* bytes */)
|
||||
{
|
||||
logger.Warning("Possibly corrupt file '" + input + "' with size " + Style.GetBytesReadable(filesize));
|
||||
return new RomData();
|
||||
}
|
||||
|
||||
// Get the Romba-specific header data
|
||||
byte[] header;
|
||||
byte[] footer;
|
||||
using (FileStream itemstream = File.OpenRead(input))
|
||||
{
|
||||
using (BinaryReader br = new BinaryReader(itemstream))
|
||||
{
|
||||
header = br.ReadBytes(32);
|
||||
br.BaseStream.Seek(-4, SeekOrigin.End);
|
||||
footer = br.ReadBytes(4);
|
||||
}
|
||||
}
|
||||
|
||||
// Now convert the data and get the right positions
|
||||
string headerstring = BitConverter.ToString(header).Replace("-", string.Empty);
|
||||
string gzmd5 = headerstring.Substring(24, 32);
|
||||
string gzcrc = headerstring.Substring(56, 8);
|
||||
string gzsize = BitConverter.ToString(footer.Reverse().ToArray()).Replace("-", string.Empty);
|
||||
long extractedsize = Convert.ToInt64(gzsize, 16);
|
||||
|
||||
// Only try to add if the file size is greater than 750 MiB
|
||||
if (filesize >= (750 * Constants.MibiByte))
|
||||
{
|
||||
// ISIZE is mod 4GiB, so we add that if the ISIZE is smaller than the filesize and greater than 1% different
|
||||
bool shouldfollowup = false;
|
||||
if (extractedsize < filesize && (100 * extractedsize / filesize) < 99 /* percent */)
|
||||
{
|
||||
logger.Log("mancalc - Filename: '" + Path.GetFullPath(input) + "'\nExtracted file size: " +
|
||||
extractedsize + ", " + Style.GetBytesReadable(extractedsize) + "\nArchive file size: " + filesize + ", " + Style.GetBytesReadable(filesize));
|
||||
}
|
||||
while (extractedsize < filesize && (100 * extractedsize / filesize) < 99 /* percent */)
|
||||
{
|
||||
extractedsize += (4 * Constants.GibiByte);
|
||||
shouldfollowup = true;
|
||||
}
|
||||
if (shouldfollowup)
|
||||
{
|
||||
logger.Log("Filename: '" + Path.GetFullPath(input) + "'\nFinal file size: " + extractedsize + ", " + Style.GetBytesReadable(extractedsize) +
|
||||
"\nExtracted CRC: " + gzcrc + "\nExtracted MD5: " + gzmd5 + "\nSHA-1: " + Path.GetFileNameWithoutExtension(input));
|
||||
}
|
||||
}
|
||||
|
||||
RomData rom = new RomData
|
||||
{
|
||||
Type = "rom",
|
||||
Game = Path.GetFileNameWithoutExtension(input),
|
||||
Name = Path.GetFileNameWithoutExtension(input),
|
||||
Size = extractedsize,
|
||||
CRC = gzcrc,
|
||||
MD5 = gzmd5,
|
||||
SHA1 = Path.GetFileNameWithoutExtension(input),
|
||||
};
|
||||
|
||||
return rom;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
|
||||
using DamienG.Security.Cryptography;
|
||||
|
||||
namespace SabreTools.Helper
|
||||
{
|
||||
public class DatTools
|
||||
@@ -1831,7 +1834,8 @@ namespace SabreTools.Helper
|
||||
/// <param name="nodump">Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump)</param>
|
||||
/// <param name="logger">Logging object for console and file output</param>
|
||||
/// <returns>Returns filtered DatData object</returns>
|
||||
public static DatData Filter(DatData datdata, string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, string md5, string sha1, bool? nodump, Logger logger)
|
||||
public static DatData Filter(DatData datdata, string gamename, string romname, string romtype, long sgt,
|
||||
long slt, long seq, string crc, string md5, string sha1, bool? nodump, Logger logger)
|
||||
{
|
||||
// Now loop through and create a new Rom dictionary using filtered values
|
||||
Dictionary<string, List<RomData>> dict = new Dictionary<string, List<RomData>>();
|
||||
@@ -1981,5 +1985,52 @@ namespace SabreTools.Helper
|
||||
|
||||
return datdata;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve file information for a single file
|
||||
/// </summary>
|
||||
/// <param name="input">Filename to get information from</param>
|
||||
/// <returns>Populated RomData object if success, empty one on error</returns>
|
||||
public static RomData GetSingleFileInfo(string input)
|
||||
{
|
||||
RomData rom = new RomData
|
||||
{
|
||||
Name = Path.GetFileName(input),
|
||||
Type = "rom",
|
||||
Size = (new FileInfo(input)).Length,
|
||||
CRC = string.Empty,
|
||||
MD5 = string.Empty,
|
||||
SHA1 = string.Empty,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
Crc32 crc = new Crc32();
|
||||
MD5 md5 = MD5.Create();
|
||||
SHA1 sha1 = SHA1.Create();
|
||||
|
||||
using (FileStream fs = File.Open(input, FileMode.Open))
|
||||
{
|
||||
foreach (byte b in crc.ComputeHash(fs))
|
||||
{
|
||||
rom.CRC += b.ToString("x2").ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
using (FileStream fs = File.Open(input, FileMode.Open))
|
||||
{
|
||||
rom.MD5 = BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", "").ToLowerInvariant();
|
||||
}
|
||||
using (FileStream fs = File.Open(input, FileMode.Open))
|
||||
{
|
||||
rom.SHA1 = BitConverter.ToString(sha1.ComputeHash(fs)).Replace("-", "").ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return new RomData();
|
||||
}
|
||||
|
||||
return rom;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user