[ALL] Massive code cleanup and reorganization

This commit is contained in:
Matt Nadareski
2016-10-24 12:58:57 -07:00
parent 88f11e5826
commit 9a3527921f
70 changed files with 11347 additions and 11220 deletions

View File

@@ -1,11 +1,14 @@
using Mono.Data.Sqlite;
using SabreTools.Helper;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml;
using Mono.Data.Sqlite;
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
using SabreTools.Helper.Tools;
namespace SabreTools
{
@@ -459,7 +462,7 @@ namespace SabreTools
private static void AddDatToDatabase(Rom dat, SqliteConnection dbc)
{
// Get the dat full path
string fullpath = Path.Combine(_dats, (dat.MachineName == "dats" ? "" : dat.MachineName), dat.Name);
string fullpath = Path.Combine(_dats, (dat.Machine.Name == "dats" ? "" : dat.Machine.Name), dat.Name);
// Parse the Dat if possible
_logger.User("Adding from '" + dat.Name + "'");

View File

@@ -1,9 +1,13 @@
using Mono.Data.Sqlite;
using SabreTools.Helper;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.Data.Sqlite;
using SabreTools.Helper;
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
using SabreTools.Helper.Tools;
namespace SabreTools
{
@@ -254,7 +258,7 @@ namespace SabreTools
Files = new SortedDictionary<string, List<DatItem>>(),
};
Logger logger = new Logger(false, "");
Logger logger = new Logger();
foreach (string input in inputs)
{
datdata.PopulateDatFromDir(input, false /* noMD5 */, false /* noSHA1 */, true /* bare */, false /* archivesAsFiles */,

View File

@@ -1,7 +1,9 @@
using Mono.Data.Sqlite;
using System;
using System;
using System.Collections.Generic;
using SabreTools.Helper;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
namespace SabreTools
{
@@ -54,7 +56,7 @@ namespace SabreTools
// Credits take precidence over all
if ((new List<string>(args)).Contains("--credits"))
{
Build.Credits();
Build.Help("Credits");
_logger.Close();
return;
}
@@ -62,7 +64,7 @@ namespace SabreTools
// If there's no arguments, show help
if (args.Length == 0)
{
Build.Help();
Build.Help("RombaSharp");
_logger.Close();
return;
}
@@ -213,7 +215,7 @@ namespace SabreTools
// If help is set, show the help screen
if (help)
{
Build.Help();
Build.Help("RombaSharp");
_logger.Close();
return;
}
@@ -223,7 +225,7 @@ namespace SabreTools
memstats ^ miss ^ progress ^ purgeBackup ^ purgeDelete ^ refreshDats ^ shutdown))
{
_logger.Error("Only one feature switch is allowed at a time");
Build.Help();
Build.Help("RombaSharp");
_logger.Close();
return;
}
@@ -232,7 +234,7 @@ namespace SabreTools
if (inputs.Count == 0 && (archive || build || depotRescan || dir2dat || fixdat || lookup || miss))
{
_logger.Error("This feature requires at least one input");
Build.Help();
Build.Help("RombaSharp");
_logger.Close();
return;
}
@@ -341,7 +343,7 @@ namespace SabreTools
// If nothing is set, show the help
else
{
Build.Help();
Build.Help("RombaSharp");
}
_logger.Close();

View File

@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace SabreTools.Helper
namespace SabreTools.Helper.Data
{
public static class Build
{
@@ -18,10 +17,9 @@ namespace SabreTools.Helper
/// Readies the console and outputs the header
/// </summary>
/// <param name="name">The name to be displayed as the program</param>
/// <remarks>Adapted from http://stackoverflow.com/questions/8200661/how-to-align-string-in-fixed-length-string</remarks>
public static void Start(string name)
{
// Dynamically create the header string
// Dynamically create the header string, adapted from http://stackoverflow.com/questions/8200661/how-to-align-string-in-fixed-length-string
int width = Console.WindowWidth - 3;
string border = "+" + new string('-', width) + "+";
string mid = name + " " + Constants.Version;
@@ -62,17 +60,28 @@ namespace SabreTools.Helper
/// <summary>
/// Show the help dialog for a given class
/// </summary>
public static void Help()
/// <param name="className">Name of the class to get help for, "Credits" for developer credits</param>
public static void Help(string className)
{
//http://stackoverflow.com/questions/14849367/how-to-determine-calling-method-and-class-name
StackTrace st = new StackTrace();
string className = st.GetFrame(1).GetMethod().ReflectedType.Name;
string barrier = "-----------------------------------------";
List<String> helptext = new List<string>();
List<string> helptext = new List<string>();
// Set the help text
switch (className)
{
case "Credits":
helptext.Add(barrier);
helptext.Add("Credits");
helptext.Add(barrier);
helptext.Add("");
helptext.Add("Programmer / Lead: Matt Nadareski (darksabre76)");
helptext.Add("Additional code: emuLOAD, @tractivo, motoschifo");
helptext.Add("Testing: emuLOAD, @tractivo, Kludge, Obiwantje, edc");
helptext.Add("Suggestions: edc, AcidX, Amiga12, EliUmniCk");
helptext.Add("Based on work by: The Wizard of DATz");
break;
case "RombaSharp":
helptext.Add(Resources.Resources.RombaSharp_Name + " - " + Resources.Resources.RombaSharp_Desc);
helptext.Add(barrier);
@@ -80,27 +89,56 @@ namespace SabreTools.Helper
helptext.Add("");
helptext.Add("Options:");
helptext.Add(" -?, -h, --help Show this help");
// Archive
helptext.Add(" archive Adds ROM files from the specified directories to depot");
helptext.Add(" -only-needed Only archive ROM files in database");
// Build
helptext.Add(" build For each specified DAT file it creates TZip files");
helptext.Add(" -copy Copy files instead of rebuilding");
// Stats
helptext.Add(" dbstats Prints db stats");
// Rescan Depots
helptext.Add(" depot-rescan Rescan a specific depot to get new information");
// Diffdat
helptext.Add(" diffdat Creates a DAT file for entries found in the new DAT");
helptext.Add(" -new= DAT to compare to");
// Dir2DAT / DATFromDir
helptext.Add(" dir2dat Creates a DAT file for the specified input directory");
helptext.Add(" -out= Filename to save out to");
// Export
helptext.Add(" export Exports db to export.csv");
// Fixdat
helptext.Add(" fixdat For each specified DAT file it creates a fix DAT");
// Lookup
helptext.Add(" lookup For each specified hash, look up available information");
// Memstats
helptext.Add(" memstats Prints memory stats");
// Miss
helptext.Add(" miss For each specified DAT file, create miss and have file");
helptext.Add(" progress Shows progress of currently running command [OBSOLETE]");
// Purge
helptext.Add(" purge-backup Moves DAT index entries for orphaned DATs");
helptext.Add(" purge-delete Deletes DAT index entries for orphaned DATs");
// Refresh DATs
helptext.Add(" refresh-dats Refreshes the DAT index from the files in the DAT root");
// Obsolete
helptext.Add(" progress Shows progress of currently running command [OBSOLETE]");
helptext.Add(" shutdown Gracefully shuts down server [OBSOLETE]");
break;
case "SabreTools":
helptext.Add(Resources.Resources.SabreTools_Name + " - " + Resources.Resources.SabreTools_Desc);
helptext.Add(barrier);
@@ -331,6 +369,7 @@ namespace SabreTools.Helper
helptext.Add("Filter parameters for size can use postfixes for inputs:");
helptext.Add(" e.g. 8kb => 8000 or 8kib => 8192");
break;
case "SimpleSort":
helptext.Add(Resources.Resources.SimpleSort_Name + " - " + Resources.Resources.SimpleSort_Desc);
helptext.Add(barrier);
@@ -388,23 +427,6 @@ namespace SabreTools.Helper
Pause();
}
/// <summary>
/// Display the credits for the program
/// </summary>
public static void Credits()
{
Console.WriteLine(@"-----------------------------------------
Credits
-----------------------------------------
Programmer / Lead: Matt Nadareski (darksabre76)
Additional code: emuLOAD, @tractivo, motoschifo
Testing: emuLOAD, @tractivo, Kludge, Obiwantje, edc
Suggestions: edc, AcidX, Amiga12, EliUmniCk
Based on work by: The Wizard of DATz");
Pause();
}
/// <summary>
/// Pause on console output
/// </summary>

View File

@@ -1,6 +1,6 @@
using System;
namespace SabreTools.Helper
namespace SabreTools.Helper.Data
{
public static class Constants
{
@@ -14,70 +14,21 @@ namespace SabreTools.Helper
public const long SizeZero = 0;
public const string CRCZero = "00000000";
public static byte[] CRCZeroBytes = new byte[] { 0x00, 0x00, 0x00, 0x00 };
public const string MD5Zero = "d41d8cd98f00b204e9800998ecf8427e";
public static byte[] MD5ZeroBytes = new byte[] { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e };
public const string SHA1Zero = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
public static byte[] SHA1ZeroBytes = new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 };
#endregion
#region Hash string length constants
#region Byte (1000-based) size comparisons
public const int CRCLength = 8;
public const int CRCBytesLength = 4;
public const int MD5Length = 32;
public const int MD5BytesLength = 16;
public const int SHA1Length = 40;
public const int SHA1BytesLength = 20;
#endregion
#region Regex File Name Patterns
public const string DefaultPattern = @"^(.+?) - (.+?) \((.*) (.*)\)\.dat$";
public const string DefaultSpecialPattern = @"^(.+?) - (.+?) \((.*) (.*)\)\.xml$";
public const string GoodPattern = @"^(Good.*?)_.*\.dat";
public const string GoodXmlPattern = @"^(Good.*?)_.*\.xml";
public const string MamePattern = @"^(.*)\.xml$";
public const string MaybeIntroPattern = @"(.*?) \[T-En\].*\((\d{8})\)\.dat$";
public const string NoIntroPattern = @"^(.*?) \((\d{8}-\d{6})_CM\).*\.dat$";
public const string NoIntroNumberedPattern = @"(.*? - .*?) \(\d.*?_CM\).*.dat";
public const string NoIntroSpecialPattern = @"(.*? - .*?) \((\d{8})\).*\.dat";
public const string NonGoodPattern = @"^(NonGood.*?)( .*?)?.xml";
public const string NonGoodSpecialPattern = @"^(NonGood.*?)( .*)?.dat";
public const string RedumpPattern = @"^(.*?) \((\d{8} \d{2}-\d{2}-\d{2})\)\.dat$";
public const string RedumpBiosPattern = @"^(.*?) \(\d+\) \((\d{4}-\d{2}-\d{2})\)\.dat$";
public const string TosecPattern = @"^(.*?) - .* \(TOSEC-v(\d{4}-\d{2}-\d{2})_CM\).*\.dat$";
public const string TosecSpecialPatternA = @"^(.*? - .*?) - .* \(TOSEC-v(\d{4}-\d{2}-\d{2})_CM\).*\.dat$";
public const string TosecSpecialPatternB = @"^(.*? - .*? - .*?) - .* \(TOSEC-v(\d{4}-\d{2}-\d{2})_CM\).*\.dat$";
public const string TruripPattern = @"^(.*) - .* \(trurip_XML\)\.dat$";
public const string ZandroPattern = @"^SMW-.*.xml";
#endregion
#region Regex Mapped Name Patterns
public const string RemappedPattern = @"^(.*) - (.*)$";
#endregion
#region Regex Date Patterns
public const string DefaultDatePattern = @"(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})";
public const string NoIntroDatePattern = @"(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})";
public const string NoIntroSpecialDatePattern = @"(\d{4})(\d{2})(\d{2})";
public const string RedumpDatePattern = @"(\d{4})(\d{2})(\d{2}) (\d{2})-(\d{2})-(\d{2})";
public const string TosecDatePattern = @"(\d{4})-(\d{2})-(\d{2})";
#endregion
#region Regex conversion patterns
public const string XmlPattern = @"<(.*?)>(.*?)</(.*?)>";
public const string HeaderPatternCMP = @"(^.*?) \($";
public const string ItemPatternCMP = @"^\s*(\S*?) (.*)";
public const string EndPatternCMP = @"^\s*\)\s*$";
public const long KiloByte = 1000;
public static long MegaByte = (long)Math.Pow(KiloByte, 2);
public static long GigaByte = (long)Math.Pow(KiloByte, 3);
public static long TeraByte = (long)Math.Pow(KiloByte, 4);
public static long PetaByte = (long)Math.Pow(KiloByte, 5);
public static long ExaByte = (long)Math.Pow(KiloByte, 6);
public static long ZettaByte = (long)Math.Pow(KiloByte, 7);
public static long YottaByte = (long)Math.Pow(KiloByte, 8);
#endregion
@@ -88,30 +39,25 @@ namespace SabreTools.Helper
public static long GibiByte = (long)Math.Pow(KibiByte, 3);
public static long TibiByte = (long)Math.Pow(KibiByte, 4);
public static long PibiByte = (long)Math.Pow(KibiByte, 5);
public static long ExiByte = (long)Math.Pow(KibiByte, 6);
public static long ZittiByte = (long)Math.Pow(KibiByte, 7);
public static long YittiByte = (long)Math.Pow(KibiByte, 8);
#endregion
#region Byte (1000-based) size comparisons
#region Database schema
public const long KiloByte = 1000;
public static long MegaByte = (long)Math.Pow(KiloByte, 2);
public static long GigaByte = (long)Math.Pow(KiloByte, 2);
public static long TeraByte = (long)Math.Pow(KiloByte, 2);
public static long PetaByte = (long)Math.Pow(KiloByte, 2);
public const string HeadererDbSchema = "Headerer";
public const string HeadererFileName = "Headerer.sqlite";
public const string HeadererConnectionString = "Data Source=" + HeadererFileName + ";Version = 3;";
#endregion
#region Magic numbers as byte arrays
#region Hash string length constants
public static byte[] SevenZipSigBytes = new byte[] { 0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c };
public static byte[] GzSigBytes = new byte[] { 0x1f, 0x8b };
public static byte[] RarSigBytes = new byte[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00 };
public static byte[] RarFiveSigBytes = new byte[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x01, 0x00 };
public static byte[] TarSigBytes = new byte[] { 0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00 };
public static byte[] TarZeroSigBytes = new byte[] { 0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30 };
public static byte[] ZipSigBytes = new byte[] { 0x50, 0x4b, 0x03, 0x04 };
public static byte[] ZipSigEmptyBytes = new byte[] { 0x50, 0x4b, 0x05, 0x06 };
public static byte[] ZipSigSpannedBytes = new byte[] { 0x50, 0x4b, 0x07, 0x08 };
public const int CRCLength = 8;
public const int MD5Length = 32;
public const int SHA1Length = 40;
#endregion
@@ -129,6 +75,15 @@ namespace SabreTools.Helper
#endregion
#region Regular Expressions
public const string XmlPattern = @"<(.*?)>(.*?)</(.*?)>";
public const string HeaderPatternCMP = @"(^.*?) \($";
public const string ItemPatternCMP = @"^\s*(\S*?) (.*)";
public const string EndPatternCMP = @"^\s*\)\s*$";
#endregion
#region TorrentZip, T7z, and TGZ headers
/* TorrentZip Header Format
@@ -184,13 +139,5 @@ namespace SabreTools.Helper
public const uint TorrentZipFileDateTime = 0x2198BC00;
#endregion
#region Database schema
public const string HeadererDbSchema = "Headerer";
public const string HeadererFileName = "Headerer.sqlite";
public const string HeadererConnectionString = "Data Source=" + HeadererFileName + ";Version = 3;";
#endregion
}
}

View File

@@ -1,5 +1,113 @@
namespace SabreTools.Helper
namespace SabreTools.Helper.Data
{
#region Archival
/// <summary>
/// Version of tool archive made by
/// </summary>
public enum ArchiveVersion : ushort
{
MSDOSandOS2 = 0,
Amiga = 1,
OpenVMS = 2,
UNIX = 3,
VMCMS = 4,
AtariST = 5,
OS2HPFS = 6,
Macintosh = 7,
ZSystem = 8,
CPM = 9,
WindowsNTFS = 10,
MVS = 11,
VSE = 12,
AcornRisc = 13,
VFAT = 14,
AlternateMVS = 15,
BeOS = 16,
Tandem = 17,
OS400 = 18,
OSXDarwin = 19,
TorrentZip = 20,
TorrentZip64 = 45,
}
/// <summary>
/// Compression method based on flag
/// </summary>
public enum CompressionMethod : ushort
{
Stored = 0,
Shrunk = 1,
ReducedCompressionFactor1 = 2,
ReducedCompressionFactor2 = 3,
ReducedCompressionFactor3 = 4,
ReducedCompressionFactor4 = 5,
Imploded = 6,
Tokenizing = 7,
Deflated = 8,
Delfate64 = 9,
PKWAREDataCompressionLibrary = 10,
BZIP2 = 12,
LZMA = 14,
IBMTERSE = 18,
IBMLZ77 = 19,
WavPak = 97,
PPMdVersionIRev1 = 98,
// Reserved and unused (SHOULD NOT BE USED)
Type11 = 11,
Type13 = 13,
Type15 = 15,
Type16 = 16,
Type17 = 17,
}
/// <summary>
/// Zip open type
/// </summary>
/// <remarks>https://raw.githubusercontent.com/gjefferyes/RomVault/5a93500001f0d068f32cf77a048950717507f733/ROMVault2/SupportedFiles/ZipEnums.cs</remarks>
public enum ZipOpenType
{
Closed,
OpenRead,
OpenWrite
}
/// <summary>
/// Zip testing type
/// </summary>
/// <remarks>https://raw.githubusercontent.com/gjefferyes/RomVault/5a93500001f0d068f32cf77a048950717507f733/ROMVault2/SupportedFiles/ZipEnums.cs</remarks>
public enum ZipReturn
{
ZipGood,
ZipFileLocked,
ZipFileCountError,
ZipSignatureError,
ZipExtraDataOnEndOfZip,
ZipUnsupportedCompression,
ZipLocalFileHeaderError,
ZipCentralDirError,
ZipEndOfCentralDirectoryError,
Zip64EndOfCentralDirError,
Zip64EndOfCentralDirectoryLocatorError,
ZipReadingFromOutputFile,
ZipWritingToInputFile,
ZipErrorGettingDataStream,
ZipCRCDecodeError,
ZipDecodeError,
ZipFileNameToLong,
ZipFileAlreadyOpen,
ZipCannotFastOpen,
ZipErrorOpeningFile,
ZipErrorFileNotFound,
ZipErrorReadingFile,
ZipErrorTimeStamp,
ZipErrorRollBackFile,
ZipUntested
}
#endregion
#region DatFile related
/// <summary>
@@ -127,127 +235,4 @@
}
#endregion
#region Miscellaneous
/// <summary>
/// Severity of the logging statement
/// </summary>
public enum LogLevel
{
VERBOSE = 0,
USER,
WARNING,
ERROR,
}
#endregion
#region Archival
/// <summary>
/// Version of tool archive made by
/// </summary>
public enum ArchiveVersion : ushort
{
MSDOSandOS2 = 0,
Amiga = 1,
OpenVMS = 2,
UNIX = 3,
VMCMS = 4,
AtariST = 5,
OS2HPFS = 6,
Macintosh = 7,
ZSystem = 8,
CPM = 9,
WindowsNTFS = 10,
MVS = 11,
VSE = 12,
AcornRisc = 13,
VFAT = 14,
AlternateMVS = 15,
BeOS = 16,
Tandem = 17,
OS400 = 18,
OSXDarwin = 19,
TorrentZip = 20,
TorrentZip64 = 45,
}
/// <summary>
/// Compression method based on flag
/// </summary>
public enum CompressionMethod : ushort
{
Stored = 0,
Shrunk = 1,
ReducedCompressionFactor1 = 2,
ReducedCompressionFactor2 = 3,
ReducedCompressionFactor3 = 4,
ReducedCompressionFactor4 = 5,
Imploded = 6,
Tokenizing = 7,
Deflated = 8,
Delfate64 = 9,
PKWAREDataCompressionLibrary = 10,
BZIP2 = 12,
LZMA = 14,
IBMTERSE = 18,
IBMLZ77 = 19,
WavPak = 97,
PPMdVersionIRev1 = 98,
// Reserved and unused (SHOULD NOT BE USED)
Type11 = 11,
Type13 = 13,
Type15 = 15,
Type16 = 16,
Type17 = 17,
}
/// <summary>
/// Zip testing type
/// </summary>
/// <remarks>https://raw.githubusercontent.com/gjefferyes/RomVault/5a93500001f0d068f32cf77a048950717507f733/ROMVault2/SupportedFiles/ZipEnums.cs</remarks>
public enum ZipReturn
{
ZipGood,
ZipFileLocked,
ZipFileCountError,
ZipSignatureError,
ZipExtraDataOnEndOfZip,
ZipUnsupportedCompression,
ZipLocalFileHeaderError,
ZipCentralDirError,
ZipEndOfCentralDirectoryError,
Zip64EndOfCentralDirError,
Zip64EndOfCentralDirectoryLocatorError,
ZipReadingFromOutputFile,
ZipWritingToInputFile,
ZipErrorGettingDataStream,
ZipCRCDecodeError,
ZipDecodeError,
ZipFileNameToLong,
ZipFileAlreadyOpen,
ZipCannotFastOpen,
ZipErrorOpeningFile,
ZipErrorFileNotFound,
ZipErrorReadingFile,
ZipErrorTimeStamp,
ZipErrorRollBackFile,
ZipUntested
}
/// <summary>
/// Zip open type
/// </summary>
/// <remarks>https://raw.githubusercontent.com/gjefferyes/RomVault/5a93500001f0d068f32cf77a048950717507f733/ROMVault2/SupportedFiles/ZipEnums.cs</remarks>
public enum ZipOpenType
{
Closed,
OpenRead,
OpenWrite
}
#endregion
}

View File

@@ -1,7 +1,9 @@
using System;
namespace SabreTools.Helper
namespace SabreTools.Helper.Data
{
#region Archival
/// <summary>
/// Determines the level to scan archives at
/// </summary>
@@ -29,61 +31,6 @@ namespace SabreTools.Helper
ZipBoth = ZipExternal | ZipInternal,
}
/// <summary>
/// Determines which diffs should be created
/// </summary>
[Flags]
public enum DiffMode
{
// Standard diffs
Dupes = 0x01,
NoDupes = 0x02,
Individuals = 0x04,
All = Dupes | NoDupes | Individuals,
// Cascaded diffs
Cascade = 0x08,
ReverseCascade = 0x10,
}
/// <summary>
/// Determines which type of duplicate a file is
/// </summary>
[Flags]
public enum DupeType
{
// Type of match
Hash = 0x01,
All = 0x02,
// Location of match
Internal = 0x10,
External = 0x20,
}
/// <summary>
/// Determines the DAT output format
/// </summary>
[Flags]
public enum OutputFormat
{
Logiqx = 0x0001,
ClrMamePro = 0x0002,
RomCenter = 0x0004,
DOSCenter = 0x0008,
MissFile = 0x0010,
SabreDat = 0x0020,
RedumpMD5 = 0x0040,
RedumpSHA1 = 0x0080,
RedumpSFV = 0x0100,
SoftwareList = 0x0200,
OfflineList = 0x0400,
TSV = 0x0800,
CSV = 0x1000,
ALL = 0xFFFF,
}
/// <summary>
/// Determines the archive general bit flags
/// </summary>
@@ -145,4 +92,69 @@ namespace SabreTools.Helper
TorrentZip = 0x1,
ExtraData = 0x2
}
#endregion
#region DatFile related
/// <summary>
/// Determines which diffs should be created
/// </summary>
[Flags]
public enum DiffMode
{
// Standard diffs
Dupes = 0x01,
NoDupes = 0x02,
Individuals = 0x04,
All = Dupes | NoDupes | Individuals,
// Cascaded diffs
Cascade = 0x08,
ReverseCascade = 0x10,
}
/// <summary>
/// Determines the DAT output format
/// </summary>
[Flags]
public enum OutputFormat
{
Logiqx = 0x0001,
ClrMamePro = 0x0002,
RomCenter = 0x0004,
DOSCenter = 0x0008,
MissFile = 0x0010,
SabreDat = 0x0020,
RedumpMD5 = 0x0040,
RedumpSHA1 = 0x0080,
RedumpSFV = 0x0100,
SoftwareList = 0x0200,
OfflineList = 0x0400,
TSV = 0x0800,
CSV = 0x1000,
ALL = 0xFFFF,
}
#endregion
#region DatItem related
/// <summary>
/// Determines which type of duplicate a file is
/// </summary>
[Flags]
public enum DupeType
{
// Type of match
Hash = 0x01,
All = 0x02,
// Location of match
Internal = 0x10,
External = 0x20,
}
#endregion
}

View File

@@ -13,7 +13,7 @@ using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SabreTools.Helper
namespace NaturalSort
{
public class NaturalComparer : Comparer<string>, IDisposable
{

View File

@@ -13,7 +13,7 @@ using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SabreTools.Helper
namespace NaturalSort
{
public class NaturalReversedComparer : Comparer<string>, IDisposable
{

View File

@@ -1,5 +1,4 @@

/*
/*
Copyright (c) 2012-2015 Eugene Larchenko (spct@mail.ru)
@@ -45,7 +44,9 @@ namespace OCRC
{
uint r = (uint)i;
for (int j = 0; j < 8; j++)
{
r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
}
Table[i] = r;
}
for (; i < 256 * CRC_NUM_TABLES; i++)
@@ -79,14 +80,19 @@ namespace OCRC
public void Update(byte[] data, int offset, int count)
{
new ArraySegment<byte>(data, offset, count); // check arguments
if (count == 0) return;
if (count == 0)
{
return;
}
var table = OptimizedCRC.Table;
uint crc = UnsignedValue;
for (; (offset & 7) != 0 && count != 0; count--)
{
crc = (crc >> 8) ^ table[(byte)crc ^ data[offset++]];
}
if (count >= 8)
{
@@ -116,7 +122,9 @@ namespace OCRC
}
while (count-- != 0)
{
crc = (crc >> 8) ^ table[(byte)crc ^ data[offset++]];
}
UnsignedValue = crc;
}

View File

@@ -1,10 +1,13 @@
using OCRC;
using System;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace SabreTools.Helper
using SabreTools.Helper.Data;
using OCRC;
namespace ROMVault2.SupportedFiles.Zip
{
/// <remarks>
/// Based on work by GordonJ for RomVault

View File

@@ -1,11 +1,16 @@
using OCRC;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SabreTools.Helper
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
using Ionic.Zlib;
using OCRC;
namespace ROMVault2.SupportedFiles.Zip
{
/// <remarks>
/// Based on work by GordonJ for RomVault

View File

@@ -25,12 +25,11 @@
//
// ------------------------------------------------------------------
using System;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
/// <summary>
/// Computes a CRC-32. The CRC-32 algorithm is parameterized - you
@@ -100,7 +99,9 @@ namespace SabreTools.Helper
public Int32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
{
if (input == null)
{
throw new Exception("The input stream must not be null.");
}
unchecked
{
@@ -109,7 +110,10 @@ namespace SabreTools.Helper
_TotalBytesRead = 0;
int count = input.Read(buffer, 0, readSize);
if (output != null) output.Write(buffer, 0, count);
if (output != null)
{
output.Write(buffer, 0, count);
}
_TotalBytesRead += count;
while (count > 0)
{
@@ -123,7 +127,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Get the CRC32 for the given (word,byte) combo. This is a
/// computation defined by PKzip for PKZIP 2.0 (weak) encryption.
@@ -141,7 +144,6 @@ namespace SabreTools.Helper
return (Int32)(crc32Table[(W ^ B) & 0xFF] ^ (W >> 8));
}
/// <summary>
/// Update the value for the running CRC32 using the given block of bytes.
/// This is useful when using the CRC32() class in a Stream.
@@ -152,7 +154,9 @@ namespace SabreTools.Helper
public void SlurpBlock(byte[] block, int offset, int count)
{
if (block == null)
{
throw new Exception("The data buffer must not be null.");
}
// bzip algorithm
for (int i = 0; i < count; i++)
@@ -228,8 +232,6 @@ namespace SabreTools.Helper
}
}
private static uint ReverseBits(uint data)
{
unchecked
@@ -255,8 +257,6 @@ namespace SabreTools.Helper
}
}
private void GenerateLookupTable()
{
crc32Table = new UInt32[256];
@@ -307,7 +307,6 @@ namespace SabreTools.Helper
#endif
}
private uint gf2_matrix_times(uint[] matrix, uint vec)
{
uint sum = 0;
@@ -328,8 +327,6 @@ namespace SabreTools.Helper
square[i] = gf2_matrix_times(mat, mat[i]);
}
/// <summary>
/// Combines the given CRC32 value with the current running total.
/// </summary>
@@ -347,7 +344,9 @@ namespace SabreTools.Helper
uint[] odd = new uint[32]; // odd-power-of-two zeros operator
if (length == 0)
{
return;
}
uint crc1 = ~_register;
uint crc2 = (uint)crc;
@@ -377,19 +376,23 @@ namespace SabreTools.Helper
gf2_matrix_square(even, odd);
if ((len2 & 1) == 1)
{
crc1 = gf2_matrix_times(even, crc1);
}
len2 >>= 1;
if (len2 == 0)
{
break;
}
// another iteration of the loop with odd and even swapped
gf2_matrix_square(odd, even);
if ((len2 & 1) == 1)
{
crc1 = gf2_matrix_times(odd, crc1);
}
len2 >>= 1;
} while (len2 != 0);
crc1 ^= crc2;
@@ -400,7 +403,6 @@ namespace SabreTools.Helper
return;
}
/// <summary>
/// Create an instance of the CRC32 class using the default settings: no
/// bit reversal, and a polynomial of 0xEDB88320.
@@ -431,7 +433,6 @@ namespace SabreTools.Helper
{
}
/// <summary>
/// Create an instance of the CRC32 class, specifying the polynomial and
/// whether to reverse data bits or not.
@@ -487,7 +488,6 @@ namespace SabreTools.Helper
private UInt32 _register = 0xFFFFFFFFU;
}
/// <summary>
/// A Stream that calculates a CRC32 (a checksum) on all bytes read,
/// or on all bytes written.
@@ -570,8 +570,10 @@ namespace SabreTools.Helper
: this(true, length, stream, null)
{
if (length < 0)
{
throw new ArgumentException("length");
}
}
/// <summary>
/// A constructor allowing the specification of the length of the stream
@@ -592,8 +594,10 @@ namespace SabreTools.Helper
: this(leaveOpen, length, stream, null)
{
if (length < 0)
{
throw new ArgumentException("length");
}
}
/// <summary>
/// A constructor allowing the specification of the length of the stream
@@ -616,9 +620,10 @@ namespace SabreTools.Helper
: this(leaveOpen, length, stream, crc32)
{
if (length < 0)
{
throw new ArgumentException("length");
}
}
// This ctor is private - no validation is done here. This is to allow the use
// of a (specific) negative value for the _lengthLimit, to indicate that there
@@ -635,7 +640,6 @@ namespace SabreTools.Helper
_leaveOpen = leaveOpen;
}
/// <summary>
/// Gets the total number of bytes run through the CRC32 calculator.
/// </summary>
@@ -700,12 +704,21 @@ namespace SabreTools.Helper
if (_lengthLimit != CrcCalculatorStream.UnsetLengthLimit)
{
if (_Crc32.TotalBytesRead >= _lengthLimit) return 0; // EOF
if (_Crc32.TotalBytesRead >= _lengthLimit)
{
return 0; // EOF
}
Int64 bytesRemaining = _lengthLimit - _Crc32.TotalBytesRead;
if (bytesRemaining < count) bytesToRead = (int)bytesRemaining;
if (bytesRemaining < count)
{
bytesToRead = (int)bytesRemaining;
}
}
int n = _innerStream.Read(buffer, offset, bytesToRead);
if (n > 0) _Crc32.SlurpBlock(buffer, offset, n);
if (n > 0)
{
_Crc32.SlurpBlock(buffer, offset, n);
}
return n;
}
@@ -717,7 +730,10 @@ namespace SabreTools.Helper
/// <param name="count">the number of bytes to write</param>
public override void Write(byte[] buffer, int offset, int count)
{
if (count > 0) _Crc32.SlurpBlock(buffer, offset, count);
if (count > 0)
{
_Crc32.SlurpBlock(buffer, offset, count);
}
_innerStream.Write(buffer, offset, count);
}
@@ -766,8 +782,13 @@ namespace SabreTools.Helper
get
{
if (_lengthLimit == CrcCalculatorStream.UnsetLengthLimit)
{
return _innerStream.Length;
else return _lengthLimit;
}
else
{
return _lengthLimit;
}
}
}
@@ -804,7 +825,6 @@ namespace SabreTools.Helper
throw new NotSupportedException();
}
void IDisposable.Dispose()
{
Close();
@@ -817,11 +837,11 @@ namespace SabreTools.Helper
{
base.Close();
if (!_leaveOpen)
{
_innerStream.Close();
}
}
}
public class CRC32Hash : HashAlgorithm
{
@@ -831,10 +851,12 @@ namespace SabreTools.Helper
{
_Crc32.Reset();
}
protected override void HashCore(byte[] buffer, int start, int length)
{
_Crc32.SlurpBlock(buffer, start, length);
}
protected override byte[] HashFinal()
{
uint crcValue =(uint) _Crc32.Crc32Result;

View File

@@ -66,12 +66,10 @@
//
// -----------------------------------------------------------------------
using System;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
internal enum BlockState
{
NeedMore = 0, // block not completed, need more input or more output
@@ -129,7 +127,6 @@ namespace SabreTools.Helper
return Table[(int)level];
}
static Config()
{
Table = new Config[] {
@@ -150,7 +147,6 @@ namespace SabreTools.Helper
private static readonly Config[] Table;
}
private CompressFunc DeflateFunction;
private static readonly System.String[] _ErrorMessage = new System.String[]
@@ -267,7 +263,6 @@ namespace SabreTools.Helper
internal CompressionLevel compressionLevel; // compression level (1..9)
internal CompressionStrategy compressionStrategy; // favor or force Huffman coding
internal short[] dyn_ltree; // literal and length tree
internal short[] dyn_dtree; // distance tree
internal short[] bl_tree; // Huffman tree for bit lengths
@@ -293,7 +288,6 @@ namespace SabreTools.Helper
internal int _lengthOffset; // index for literals or lengths
// Size of match buffer for literals/lengths. There are 4 reasons for
// limiting lit_bufsize to 64K:
// - frequencies can be kept in 16 bit counters
@@ -334,7 +328,6 @@ namespace SabreTools.Helper
// are always zero.
internal int bi_valid;
internal DeflateManager()
{
dyn_ltree = new short[HEAP_SIZE * 2];
@@ -342,7 +335,6 @@ namespace SabreTools.Helper
bl_tree = new short[(2 * InternalConstants.BL_CODES + 1) * 2]; // Huffman tree for bit lengths
}
// lm_init
private void _InitializeLazyMatch()
{
@@ -387,11 +379,17 @@ namespace SabreTools.Helper
{
// Initialize the trees.
for (int i = 0; i < InternalConstants.L_CODES; i++)
{
dyn_ltree[i * 2] = 0;
}
for (int i = 0; i < InternalConstants.D_CODES; i++)
{
dyn_dtree[i * 2] = 0;
}
for (int i = 0; i < InternalConstants.BL_CODES; i++)
{
bl_tree[i * 2] = 0;
}
dyn_ltree[END_BLOCK * 2] = 1;
opt_len = static_len = 0;
@@ -415,7 +413,9 @@ namespace SabreTools.Helper
}
// Exit if v is smaller than both sons
if (_IsSmaller(tree, v, heap[j], depth))
{
break;
}
// Exchange v with the smallest son
heap[k] = heap[j]; k = j;
@@ -432,7 +432,6 @@ namespace SabreTools.Helper
return (tn2 < tm2 || (tn2 == tm2 && depth[n] <= depth[m]));
}
// Scan a literal or distance tree to determine the frequencies of the codes
// in the bit length tree.
internal void scan_tree(short[] tree, int max_code)
@@ -465,7 +464,9 @@ namespace SabreTools.Helper
else if (curlen != 0)
{
if (curlen != prevlen)
{
bl_tree[curlen * 2]++;
}
bl_tree[InternalConstants.REP_3_6 * 2]++;
}
else if (count <= 10)
@@ -513,8 +514,10 @@ namespace SabreTools.Helper
for (max_blindex = InternalConstants.BL_CODES - 1; max_blindex >= 3; max_blindex--)
{
if (bl_tree[Tree.bl_order[max_blindex] * 2 + 1] != 0)
{
break;
}
}
// Update opt_len to include the bit length tree and counts
opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
@@ -701,7 +704,6 @@ namespace SabreTools.Helper
last_eob_len = 7;
}
// Save the match info and tally the frequency counts. Return true if
// the current block must be flushed.
internal bool _tr_tally(int dist, int lc)
@@ -758,8 +760,6 @@ namespace SabreTools.Helper
// 64K-1 bytes.
}
// Send the block data compressed using the given Huffman trees
internal void send_compressed_block(short[] ltree, short[] dtree)
{
@@ -822,8 +822,6 @@ namespace SabreTools.Helper
last_eob_len = ltree[END_BLOCK * 2 + 1];
}
// Set the data type to ASCII or BINARY, using a crude approximation:
// binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
// IN assertion: the fields freq of dyn_ltree are set and the total of all
@@ -848,8 +846,6 @@ namespace SabreTools.Helper
data_type = (sbyte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
}
// Flush the bit buffer, keeping at most 7 bits in it.
internal void bi_flush()
{
@@ -894,6 +890,7 @@ namespace SabreTools.Helper
last_eob_len = 8; // enough lookahead for inflate
if (header)
{
unchecked
{
//put_short((short)len);
@@ -903,6 +900,7 @@ namespace SabreTools.Helper
pending[pendingCount++] = (byte)~len;
pending[pendingCount++] = (byte)(~len >> 8);
}
}
put_bytes(window, buf, len);
}
@@ -942,10 +940,14 @@ namespace SabreTools.Helper
{
_fillWindow();
if (lookahead == 0 && flush == FlushType.None)
{
return BlockState.NeedMore;
}
if (lookahead == 0)
{
break; // flush the current block
}
}
strstart += lookahead;
lookahead = 0;
@@ -960,8 +962,10 @@ namespace SabreTools.Helper
flush_block_only(false);
if (_codec.AvailableBytesOut == 0)
{
return BlockState.NeedMore;
}
}
// Flush if we may have to slide, otherwise block_start may become
// negative and the data will be gone:
@@ -969,18 +973,21 @@ namespace SabreTools.Helper
{
flush_block_only(false);
if (_codec.AvailableBytesOut == 0)
{
return BlockState.NeedMore;
}
}
}
flush_block_only(flush == FlushType.Finish);
if (_codec.AvailableBytesOut == 0)
{
return (flush == FlushType.Finish) ? BlockState.FinishStarted : BlockState.NeedMore;
}
return flush == FlushType.Finish ? BlockState.FinishDone : BlockState.BlockDone;
}
// Send a stored block
internal void _tr_stored_block(int buf, int stored_len, bool eof)
{
@@ -1000,7 +1007,9 @@ namespace SabreTools.Helper
{
// Check if the file is ascii or binary
if (data_type == Z_UNKNOWN)
{
set_data_type();
}
// Construct the literal and distance trees
treeLiterals.build_tree(this);
@@ -1019,8 +1028,10 @@ namespace SabreTools.Helper
static_lenb = (static_len + 3 + 7) >> 3;
if (static_lenb <= opt_lenb)
{
opt_lenb = static_lenb;
}
}
else
{
opt_lenb = static_lenb = stored_len + 5; // force a stored block
@@ -1127,7 +1138,9 @@ namespace SabreTools.Helper
}
if (_codec.AvailableBytesIn == 0)
{
return;
}
// If there was no sliding:
// strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
@@ -1180,8 +1193,10 @@ namespace SabreTools.Helper
return BlockState.NeedMore;
}
if (lookahead == 0)
{
break; // flush the current block
}
}
// Insert the string window[strstart .. strstart+2] in the
// dictionary, and set hash_head to the head of the hash chain:
@@ -1261,18 +1276,24 @@ namespace SabreTools.Helper
{
flush_block_only(false);
if (_codec.AvailableBytesOut == 0)
{
return BlockState.NeedMore;
}
}
}
flush_block_only(flush == FlushType.Finish);
if (_codec.AvailableBytesOut == 0)
{
if (flush == FlushType.Finish)
{
return BlockState.FinishStarted;
}
else
{
return BlockState.NeedMore;
}
}
return flush == FlushType.Finish ? BlockState.FinishDone : BlockState.BlockDone;
}
@@ -1297,11 +1318,15 @@ namespace SabreTools.Helper
{
_fillWindow();
if (lookahead < MIN_LOOKAHEAD && flush == FlushType.None)
{
return BlockState.NeedMore;
}
if (lookahead == 0)
{
break; // flush the current block
}
}
// Insert the string window[strstart .. strstart+2] in the
// dictionary, and set hash_head to the head of the hash chain:
@@ -1380,9 +1405,11 @@ namespace SabreTools.Helper
{
flush_block_only(false);
if (_codec.AvailableBytesOut == 0)
{
return BlockState.NeedMore;
}
}
}
else if (match_available != 0)
{
@@ -1422,10 +1449,14 @@ namespace SabreTools.Helper
if (_codec.AvailableBytesOut == 0)
{
if (flush == FlushType.Finish)
{
return BlockState.FinishStarted;
}
else
{
return BlockState.NeedMore;
}
}
return flush == FlushType.Finish ? BlockState.FinishDone : BlockState.BlockDone;
}
@@ -1463,7 +1494,9 @@ namespace SabreTools.Helper
// Do not look for matches beyond the end of the input. This is necessary
// to make deflate deterministic.
if (niceLength > lookahead)
{
niceLength = lookahead;
}
do
{
@@ -1475,7 +1508,9 @@ namespace SabreTools.Helper
window[match + best_len - 1] != scan_end1 ||
window[match] != window[scan] ||
window[++match] != window[scan + 1])
{
continue;
}
// The check at best_len-1 can be removed because it will be made
// again later. (This heuristic is not always a win.)
@@ -1506,7 +1541,9 @@ namespace SabreTools.Helper
match_start = cur_match;
best_len = len;
if (len >= niceLength)
{
break;
}
scan_end1 = window[scan + best_len - 1];
scan_end = window[scan + best_len];
}
@@ -1514,7 +1551,9 @@ namespace SabreTools.Helper
while ((cur_match = (prev[cur_match & wmask] & 0xffff)) > limit && --chain_length != 0);
if (best_len <= lookahead)
{
return best_len;
}
return lookahead;
}
@@ -1527,7 +1566,6 @@ namespace SabreTools.Helper
set { _WantRfc1950HeaderBytes = value; }
}
internal int Initialize(ZlibCodec codec, CompressionLevel level)
{
return Initialize(codec, level, ZlibConstants.WindowBitsMax);
@@ -1550,10 +1588,14 @@ namespace SabreTools.Helper
// validation
if (windowBits < 9 || windowBits > 15)
{
throw new ZlibException("windowBits must be in the range 9..15.");
}
if (memLevel < 1 || memLevel > MEM_LEVEL_MAX)
{
throw new ZlibException(String.Format("memLevel must be in the range 1.. {0}", MEM_LEVEL_MAX));
}
_codec.dstate = this;
@@ -1593,7 +1635,6 @@ namespace SabreTools.Helper
return ZlibConstants.Z_OK;
}
internal void Reset()
{
_codec.TotalBytesIn = _codec.TotalBytesOut = 0;
@@ -1614,7 +1655,6 @@ namespace SabreTools.Helper
_InitializeLazyMatch();
}
internal int End()
{
if (status != INIT_STATE && status != BUSY_STATE && status != FINISH_STATE)
@@ -1631,7 +1671,6 @@ namespace SabreTools.Helper
return status == BUSY_STATE ? ZlibConstants.Z_DATA_ERROR : ZlibConstants.Z_OK;
}
private void SetDeflater()
{
switch (config.Flavor)
@@ -1648,7 +1687,6 @@ namespace SabreTools.Helper
}
}
internal int SetParams(CompressionLevel level, CompressionStrategy strategy)
{
int result = ZlibConstants.Z_OK;
@@ -1675,19 +1713,22 @@ namespace SabreTools.Helper
return result;
}
internal int SetDictionary(byte[] dictionary)
{
int length = dictionary.Length;
int index = 0;
if (dictionary == null || status != INIT_STATE)
{
throw new ZlibException("Stream error.");
}
_codec._Adler32 = Adler.Adler32(_codec._Adler32, dictionary, 0, dictionary.Length);
if (length < MIN_MATCH)
{
return ZlibConstants.Z_OK;
}
if (length > w_size - MIN_LOOKAHEAD)
{
length = w_size - MIN_LOOKAHEAD;
@@ -1713,8 +1754,6 @@ namespace SabreTools.Helper
return ZlibConstants.Z_OK;
}
internal int Deflate(FlushType flush)
{
int old_flush;
@@ -1742,10 +1781,14 @@ namespace SabreTools.Helper
int level_flags = (((int)compressionLevel - 1) & 0xff) >> 1;
if (level_flags > 3)
{
level_flags = 3;
}
header |= (level_flags << 6);
if (strstart != 0)
{
header |= PRESET_DICT;
}
header += 31 - (header % 31);
status = BUSY_STATE;
@@ -1863,10 +1906,14 @@ namespace SabreTools.Helper
}
if (flush != FlushType.Finish)
{
return ZlibConstants.Z_OK;
}
if (!WantRfc1950HeaderBytes || Rfc1950BytesEmitted)
{
return ZlibConstants.Z_STREAM_END;
}
// Write the zlib trailer (adler32)
pending[pendingCount++] = (byte)((_codec._Adler32 & 0xFF000000) >> 24);
@@ -1885,6 +1932,5 @@ namespace SabreTools.Helper
return pendingCount != 0 ? ZlibConstants.Z_OK : ZlibConstants.Z_STREAM_END;
}
}
}

View File

@@ -24,10 +24,9 @@
//
// ------------------------------------------------------------------
using System;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
/// <summary>
/// A class for compressing and decompressing streams using the Deflate algorithm.
@@ -322,7 +321,10 @@ namespace SabreTools.Helper
get { return (this._baseStream._flushMode); }
set
{
if (_disposed) throw new ObjectDisposedException("DeflateStream");
if (_disposed)
{
throw new ObjectDisposedException("DeflateStream");
}
this._baseStream._flushMode = value;
}
}
@@ -352,11 +354,18 @@ namespace SabreTools.Helper
}
set
{
if (_disposed) throw new ObjectDisposedException("DeflateStream");
if (_disposed)
{
throw new ObjectDisposedException("DeflateStream");
}
if (this._baseStream._workingBuffer != null)
{
throw new ZlibException("The working buffer is already set.");
}
if (value < ZlibConstants.WorkingBufferSizeMin)
{
throw new ZlibException(String.Format("Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin));
}
this._baseStream._bufferSize = value;
}
}
@@ -378,9 +387,11 @@ namespace SabreTools.Helper
set
{
if (_disposed) throw new ObjectDisposedException("DeflateStream");
{
this._baseStream.Strategy = value;
}
}
}
/// <summary> Returns the total number of bytes input so far.</summary>
virtual public long TotalIn
@@ -403,6 +414,7 @@ namespace SabreTools.Helper
#endregion
#region System.IO.Stream methods
/// <summary>
/// Dispose the stream.
/// </summary>
@@ -434,7 +446,9 @@ namespace SabreTools.Helper
if (!_disposed)
{
if (disposing && (this._baseStream != null))
{
this._baseStream.Close();
}
_disposed = true;
}
}
@@ -444,8 +458,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Indicates whether the stream can be read.
/// </summary>
@@ -456,7 +468,10 @@ namespace SabreTools.Helper
{
get
{
if (_disposed) throw new ObjectDisposedException("DeflateStream");
if (_disposed)
{
throw new ObjectDisposedException("DeflateStream");
}
return _baseStream._stream.CanRead;
}
}
@@ -472,7 +487,6 @@ namespace SabreTools.Helper
get { return false; }
}
/// <summary>
/// Indicates whether the stream can be written.
/// </summary>
@@ -483,7 +497,10 @@ namespace SabreTools.Helper
{
get
{
if (_disposed) throw new ObjectDisposedException("DeflateStream");
if (_disposed)
{
throw new ObjectDisposedException("DeflateStream");
}
return _baseStream._stream.CanWrite;
}
}
@@ -493,7 +510,10 @@ namespace SabreTools.Helper
/// </summary>
public override void Flush()
{
if (_disposed) throw new ObjectDisposedException("DeflateStream");
if (_disposed)
{
throw new ObjectDisposedException("DeflateStream");
}
_baseStream.Flush();
}
@@ -521,9 +541,13 @@ namespace SabreTools.Helper
get
{
if (this._baseStream._streamMode == ZlibBaseStream.StreamMode.Writer)
{
return this._baseStream._z.TotalBytesOut;
}
if (this._baseStream._streamMode == ZlibBaseStream.StreamMode.Reader)
{
return this._baseStream._z.TotalBytesIn;
}
return 0;
}
set { throw new NotImplementedException(); }
@@ -557,11 +581,13 @@ namespace SabreTools.Helper
/// <returns>the number of bytes actually read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
if (_disposed) throw new ObjectDisposedException("DeflateStream");
if (_disposed)
{
throw new ObjectDisposedException("DeflateStream");
}
return _baseStream.Read(buffer, offset, count);
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
@@ -616,11 +642,9 @@ namespace SabreTools.Helper
if (_disposed) throw new ObjectDisposedException("DeflateStream");
_baseStream.Write(buffer, offset, count);
}
#endregion
/// <summary>
/// Compress a string into a byte array using DEFLATE (RFC 1951).
/// </summary>
@@ -651,7 +675,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Compress a byte array into a new byte array using DEFLATE.
/// </summary>
@@ -682,7 +705,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Uncompress a DEFLATE'd byte array into a single string.
/// </summary>
@@ -708,7 +730,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Uncompress a DEFLATE'd byte array into a byte array.
/// </summary>
@@ -733,8 +754,6 @@ namespace SabreTools.Helper
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}
}
}
}
}

View File

@@ -26,11 +26,10 @@
//
// ------------------------------------------------------------------
using System;
using System.IO;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
/// <summary>
/// A class for compressing and decompressing GZIP streams.
@@ -119,8 +118,6 @@ namespace SabreTools.Helper
// all optional fields get 0, except for the OS, which gets 255.
//
/// <summary>
/// The comment on the GZIP stream.
/// </summary>
@@ -149,7 +146,10 @@ namespace SabreTools.Helper
}
set
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
_Comment = value;
}
}
@@ -182,15 +182,23 @@ namespace SabreTools.Helper
get { return _FileName; }
set
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
_FileName = value;
if (_FileName == null) return;
if (_FileName == null)
{
return;
}
if (_FileName.IndexOf("/") != -1)
{
_FileName = _FileName.Replace("/", "\\");
}
if (_FileName.EndsWith("\\"))
{
throw new Exception("Illegal filename");
}
if (_FileName.IndexOf("\\") != -1)
{
// trim any leading path
@@ -227,7 +235,6 @@ namespace SabreTools.Helper
string _Comment;
int _Crc32;
/// <summary>
/// Create a <c>GZipStream</c> using the specified <c>CompressionMode</c>.
/// </summary>
@@ -548,7 +555,10 @@ namespace SabreTools.Helper
{
get { return (this._baseStream._flushMode); }
set {
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
this._baseStream._flushMode = value;
}
}
@@ -578,16 +588,22 @@ namespace SabreTools.Helper
}
set
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
if (this._baseStream._workingBuffer != null)
{
throw new ZlibException("The working buffer is already set.");
}
if (value < ZlibConstants.WorkingBufferSizeMin)
{
throw new ZlibException(String.Format("Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin));
}
this._baseStream._bufferSize = value;
}
}
/// <summary> Returns the total number of bytes input so far.</summary>
virtual public long TotalIn
{
@@ -653,7 +669,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Indicates whether the stream can be read.
/// </summary>
@@ -664,7 +679,10 @@ namespace SabreTools.Helper
{
get
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
return _baseStream._stream.CanRead;
}
}
@@ -680,7 +698,6 @@ namespace SabreTools.Helper
get { return false; }
}
/// <summary>
/// Indicates whether the stream can be written.
/// </summary>
@@ -691,7 +708,10 @@ namespace SabreTools.Helper
{
get
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
return _baseStream._stream.CanWrite;
}
}
@@ -701,7 +721,10 @@ namespace SabreTools.Helper
/// </summary>
public override void Flush()
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
_baseStream.Flush();
}
@@ -729,9 +752,13 @@ namespace SabreTools.Helper
get
{
if (this._baseStream._streamMode == ZlibBaseStream.StreamMode.Writer)
{
return this._baseStream._z.TotalBytesOut + _headerByteCount;
}
if (this._baseStream._streamMode == ZlibBaseStream.StreamMode.Reader)
{
return this._baseStream._z.TotalBytesIn + this._baseStream._gzipHeaderByteCount;
}
return 0;
}
@@ -771,7 +798,10 @@ namespace SabreTools.Helper
/// <returns>the number of bytes actually read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
int n = _baseStream.Read(buffer, offset, count);
// Console.WriteLine("GZipStream::Read(buffer, off({0}), c({1}) = {2}", offset, count, n);
@@ -786,8 +816,6 @@ namespace SabreTools.Helper
return n;
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
@@ -832,7 +860,10 @@ namespace SabreTools.Helper
/// <param name="count">the number of bytes to write.</param>
public override void Write(byte[] buffer, int offset, int count)
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
if (_disposed)
{
throw new ObjectDisposedException("GZipStream");
}
if (_baseStream._streamMode == ZlibBaseStream.StreamMode.Undefined)
{
//Console.WriteLine("GZipStream: First write");
@@ -849,8 +880,8 @@ namespace SabreTools.Helper
_baseStream.Write(buffer, offset, count);
}
#endregion
#endregion
internal static readonly System.DateTime _unixEpoch = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
#if SILVERLIGHT || NETCF
@@ -859,7 +890,6 @@ namespace SabreTools.Helper
internal static readonly System.Text.Encoding iso8859dash1 = System.Text.Encoding.GetEncoding("iso-8859-1");
#endif
private int EmitHeader()
{
byte[] commentBytes = (Comment == null) ? null : iso8859dash1.GetBytes(Comment);
@@ -879,15 +909,22 @@ namespace SabreTools.Helper
header[i++] = 8;
byte flag = 0;
if (Comment != null)
{
flag ^= 0x10;
}
if (FileName != null)
{
flag ^= 0x8;
}
// flag
header[i++] = flag;
// mtime
if (!LastModified.HasValue) LastModified = DateTime.Now;
if (!LastModified.HasValue)
{
LastModified = DateTime.Now;
}
System.TimeSpan delta = LastModified.Value - _unixEpoch;
Int32 timet = (Int32)delta.TotalSeconds;
Array.Copy(BitConverter.GetBytes(timet), 0, header, i, 4);
@@ -923,8 +960,6 @@ namespace SabreTools.Helper
return header.Length; // bytes written
}
/// <summary>
/// Compress a string into a byte array using GZip.
/// </summary>
@@ -953,7 +988,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Compress a byte array into a new byte array using GZip.
/// </summary>
@@ -982,7 +1016,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Uncompress a GZip'ed byte array into a single string.
/// </summary>
@@ -1004,7 +1037,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Uncompress a GZip'ed byte array into a byte array.
/// </summary>
@@ -1027,7 +1059,5 @@ namespace SabreTools.Helper
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}
}
}
}

View File

@@ -59,15 +59,12 @@
//
// -----------------------------------------------------------------------
using System;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
sealed class InfTree
{
private const int MANY = 1440;
private const int Z_OK = 0;
@@ -158,8 +155,12 @@ namespace SabreTools.Helper
// Find minimum and maximum length, bound *m by those
l = m[0];
for (j = 1; j <= BMAX; j++)
{
if (c[j] != 0)
{
break;
}
}
k = j; // minimum code length
if (l < j)
{
@@ -168,8 +169,10 @@ namespace SabreTools.Helper
for (i = BMAX; i != 0; i--)
{
if (c[i] != 0)
{
break;
}
}
g = i; // maximum code length
if (l > i)
{
@@ -251,7 +254,9 @@ namespace SabreTools.Helper
{
// try smaller tables up to z bits
if ((f <<= 1) <= c[++xp])
{
break; // enough codes to use up j bits
}
f -= c[xp]; // else deduct codes from patterns
}
}

View File

@@ -61,10 +61,9 @@
//
// -----------------------------------------------------------------------
using System;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
sealed class InflateBlocks
{
@@ -137,11 +136,12 @@ namespace SabreTools.Helper
readAt = writeAt = 0;
if (checkfn != null)
{
_codec._Adler32 = check = Adler.Adler32(0, null, 0, 0);
}
return oldCheck;
}
internal int Process(int r)
{
int t; // temporary storage
@@ -153,7 +153,6 @@ namespace SabreTools.Helper
int m; // bytes to end of window or read pointer
// copy input/output information to locals (UPDATE macro restores)
p = _codec.NextIn;
n = _codec.AvailableBytesIn;
b = bitb;
@@ -162,7 +161,6 @@ namespace SabreTools.Helper
q = writeAt;
m = (int)(q < readAt ? readAt - q - 1 : end - q);
// process input based on current state
while (true)
{
@@ -314,14 +312,20 @@ namespace SabreTools.Helper
t = left;
if (t > n)
{
t = n;
}
if (t > m)
{
t = m;
}
Array.Copy(_codec.InputBuffer, p, window, q, t);
p += t; n -= t;
q += t; m -= t;
if ((left -= t) != 0)
{
break;
}
mode = last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE;
break;
@@ -379,7 +383,6 @@ namespace SabreTools.Helper
b >>= 14;
k -= 14;
index = 0;
mode = InflateBlockMode.BTREE;
goto case InflateBlockMode.BTREE;
@@ -639,7 +642,6 @@ namespace SabreTools.Helper
writeAt = q;
return Flush(r);
default:
r = ZlibConstants.Z_STREAM_ERROR;
@@ -653,7 +655,6 @@ namespace SabreTools.Helper
}
}
internal void Free()
{
Reset();
@@ -701,10 +702,14 @@ namespace SabreTools.Helper
}
if (nBytes > _codec.AvailableBytesOut)
{
nBytes = _codec.AvailableBytesOut;
}
if (nBytes != 0 && r == ZlibConstants.Z_BUF_ERROR)
{
r = ZlibConstants.Z_OK;
}
// update counters
_codec.AvailableBytesOut -= nBytes;
@@ -712,7 +717,9 @@ namespace SabreTools.Helper
// update check information
if (checkfn != null)
{
_codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes);
}
// copy as far as end of window
Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes);
@@ -725,8 +732,10 @@ namespace SabreTools.Helper
// wrap pointers
readAt = 0;
if (writeAt == end)
{
writeAt = 0;
}
}
else pass++;
}
@@ -735,7 +744,6 @@ namespace SabreTools.Helper
}
}
internal static class InternalInflateConstants
{
// And'ing with mask[n] masks the lower n bits
@@ -746,7 +754,6 @@ namespace SabreTools.Helper
0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff };
}
sealed class InflateCodes
{
// waiting for "i:"=input,
@@ -864,7 +871,9 @@ namespace SabreTools.Helper
while (k < j)
{
if (n != 0)
{
r = ZlibConstants.Z_OK;
}
else
{
blocks.bitb = b; blocks.bitk = k;
@@ -925,14 +934,15 @@ namespace SabreTools.Helper
blocks.writeAt = q;
return blocks.Flush(r);
case LENEXT: // i: getting length extra (have base)
j = bitsToGet;
while (k < j)
{
if (n != 0)
{
r = ZlibConstants.Z_OK;
}
else
{
blocks.bitb = b; blocks.bitk = k;
@@ -961,7 +971,9 @@ namespace SabreTools.Helper
while (k < j)
{
if (n != 0)
{
r = ZlibConstants.Z_OK;
}
else
{
blocks.bitb = b; blocks.bitk = k;
@@ -1003,14 +1015,15 @@ namespace SabreTools.Helper
blocks.writeAt = q;
return blocks.Flush(r);
case DISTEXT: // i: getting distance extra
j = bitsToGet;
while (k < j)
{
if (n != 0)
{
r = ZlibConstants.Z_OK;
}
else
{
blocks.bitb = b; blocks.bitk = k;
@@ -1070,7 +1083,9 @@ namespace SabreTools.Helper
blocks.window[q++] = blocks.window[f++]; m--;
if (f == blocks.end)
{
f = 0;
}
len--;
}
mode = START;
@@ -1157,7 +1172,6 @@ namespace SabreTools.Helper
}
}
// Called with number of bytes left to write in window at least 258
// (the maximum string length) and number of input bytes available
// at least ten. The ten bytes are six bytes for the longest length/
@@ -1402,7 +1416,6 @@ namespace SabreTools.Helper
}
}
internal sealed class InflateManager
{
// preset dictionary flag in zlib header
@@ -1472,7 +1485,9 @@ namespace SabreTools.Helper
internal int End()
{
if (blocks != null)
{
blocks.Free();
}
blocks = null;
return ZlibConstants.Z_OK;
}
@@ -1510,13 +1525,14 @@ namespace SabreTools.Helper
return ZlibConstants.Z_OK;
}
internal int Inflate(FlushType flush)
{
int b;
if (_codec.InputBuffer == null)
{
throw new ZlibException("InputBuffer is null. ");
}
// int f = (flush == FlushType.Finish)
// ? ZlibConstants.Z_BUF_ERROR
@@ -1531,7 +1547,10 @@ namespace SabreTools.Helper
switch (mode)
{
case InflateManagerMode.METHOD:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
@@ -1552,9 +1571,11 @@ namespace SabreTools.Helper
mode = InflateManagerMode.FLAG;
break;
case InflateManagerMode.FLAG:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
@@ -1574,7 +1595,10 @@ namespace SabreTools.Helper
break;
case InflateManagerMode.DICT4:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
@@ -1583,7 +1607,10 @@ namespace SabreTools.Helper
break;
case InflateManagerMode.DICT3:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
@@ -1593,7 +1620,10 @@ namespace SabreTools.Helper
case InflateManagerMode.DICT2:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
@@ -1603,7 +1633,10 @@ namespace SabreTools.Helper
case InflateManagerMode.DICT1:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
@@ -1611,14 +1644,12 @@ namespace SabreTools.Helper
mode = InflateManagerMode.DICT0;
return ZlibConstants.Z_NEED_DICT;
case InflateManagerMode.DICT0:
mode = InflateManagerMode.BAD;
_codec.Message = "need dictionary";
marker = 0; // can try inflateSync
return ZlibConstants.Z_STREAM_ERROR;
case InflateManagerMode.BLOCKS:
r = blocks.Process(r);
if (r == ZlibConstants.Z_DATA_ERROR)
@@ -1628,10 +1659,15 @@ namespace SabreTools.Helper
break;
}
if (r == ZlibConstants.Z_OK) r = f;
if (r == ZlibConstants.Z_OK)
{
r = f;
}
if (r != ZlibConstants.Z_STREAM_END)
{
return r;
}
r = f;
computedCheck = blocks.Reset();
@@ -1644,7 +1680,10 @@ namespace SabreTools.Helper
break;
case InflateManagerMode.CHECK4:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
@@ -1653,7 +1692,10 @@ namespace SabreTools.Helper
break;
case InflateManagerMode.CHECK3:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
@@ -1661,7 +1703,10 @@ namespace SabreTools.Helper
break;
case InflateManagerMode.CHECK2:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
@@ -1670,7 +1715,10 @@ namespace SabreTools.Helper
break;
case InflateManagerMode.CHECK1:
if (_codec.AvailableBytesIn == 0) return r;
if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
@@ -1692,19 +1740,18 @@ namespace SabreTools.Helper
default:
throw new ZlibException("Stream error.");
}
}
}
internal int SetDictionary(byte[] dictionary)
{
int index = 0;
int length = dictionary.Length;
if (mode != InflateManagerMode.DICT0)
{
throw new ZlibException("Stream error.");
}
if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
{
@@ -1723,7 +1770,6 @@ namespace SabreTools.Helper
return ZlibConstants.Z_OK;
}
private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
internal int Sync()
@@ -1740,7 +1786,9 @@ namespace SabreTools.Helper
marker = 0;
}
if ((n = _codec.AvailableBytesIn) == 0)
{
return ZlibConstants.Z_BUF_ERROR;
}
p = _codec.NextIn;
m = marker;
@@ -1782,7 +1830,6 @@ namespace SabreTools.Helper
return ZlibConstants.Z_OK;
}
// Returns true if inflate is currently at the end of a block generated
// by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
// implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH

View File

@@ -29,8 +29,7 @@ using System.Collections.Generic;
using System.Threading;
using System.IO;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
internal class WorkItem
{
@@ -306,7 +305,6 @@ namespace SabreTools.Helper
this.MaxBufferPairs = 16; // default
}
/// <summary>
/// The ZLIB strategy to be used during compression.
/// </summary>
@@ -397,8 +395,10 @@ namespace SabreTools.Helper
set
{
if (value < 4)
{
throw new ArgumentException("MaxBufferPairs",
"Value must be 4 or greater.");
}
_maxBufferPairs = value;
}
}
@@ -449,8 +449,10 @@ namespace SabreTools.Helper
set
{
if (value < 1024)
{
throw new ArgumentOutOfRangeException("BufferSize",
"BufferSize must be greater than 1024 bytes");
}
_bufferSize = value;
}
}
@@ -463,7 +465,6 @@ namespace SabreTools.Helper
/// </remarks>
public int Crc32 { get { return _Crc32; } }
/// <summary>
/// The total number of uncompressed bytes processed by the ParallelDeflateOutputStream.
/// </summary>
@@ -472,7 +473,6 @@ namespace SabreTools.Helper
/// </remarks>
public Int64 BytesProcessed { get { return _totalBytesProcessed; } }
private void _InitializePoolOfWorkItems()
{
_toWrite = new Queue<int>();
@@ -494,9 +494,6 @@ namespace SabreTools.Helper
_latestCompressed = -1;
}
/// <summary>
/// Write data to the stream.
/// </summary>
@@ -531,7 +528,9 @@ namespace SabreTools.Helper
// 3. if more data to be written, goto step 1
if (_isClosed)
{
throw new InvalidOperationException();
}
// dispense any exceptions that occurred on the BG threads
if (_pendingException != null)
@@ -553,7 +552,6 @@ namespace SabreTools.Helper
_firstWriteDone = true;
}
do
{
// may need to make buffers available
@@ -628,24 +626,28 @@ namespace SabreTools.Helper
workitem.inputBytesAvailable);
if (!ThreadPool.QueueUserWorkItem(_DeflateOne, workitem))
{
throw new Exception("Cannot enqueue workitem");
}
_currentlyFilling = -1; // will get a new buffer next time
}
else
{
_currentlyFilling = ix;
}
if (count > 0)
{
TraceOutput(TraceBits.WriteEnter, "Write more");
}
}
while (count > 0); // until no more to write
TraceOutput(TraceBits.WriteEnter, "Write exit");
return;
}
private void _FlushFinish()
{
// After writing a series of compressed buffers, each one closed
@@ -663,7 +665,9 @@ namespace SabreTools.Helper
rc = compressor.Deflate(FlushType.Finish);
if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
{
throw new Exception("deflating: " + compressor.Message);
}
if (buffer.Length - compressor.AvailableBytesOut > 0)
{
@@ -682,13 +686,17 @@ namespace SabreTools.Helper
_Crc32 = _runningCrc.Crc32Result;
}
private void _Flush(bool lastInput)
{
if (_isClosed)
{
throw new InvalidOperationException();
}
if (emitting) return;
if (emitting)
{
return;
}
// compress any partial buffer
if (_currentlyFilling >= 0)
@@ -709,8 +717,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Flush the stream.
/// </summary>
@@ -724,12 +730,13 @@ namespace SabreTools.Helper
throw pe;
}
if (_handlingException)
{
return;
}
_Flush(false);
}
/// <summary>
/// Close the stream.
/// </summary>
@@ -750,20 +757,25 @@ namespace SabreTools.Helper
}
if (_handlingException)
{
return;
}
if (_isClosed) return;
if (_isClosed)
{
return;
}
_Flush(true);
if (!_leaveOpen)
{
_outStream.Close();
}
_isClosed= true;
}
// workitem 10030 - implement a new Dispose method
/// <summary>Dispose the object</summary>
@@ -785,8 +797,6 @@ namespace SabreTools.Helper
Dispose(true);
}
/// <summary>The Dispose method</summary>
/// <param name="disposing">
/// indicates whether the Dispose method was invoked by user code.
@@ -796,7 +806,6 @@ namespace SabreTools.Helper
base.Dispose(disposing);
}
/// <summary>
/// Resets the stream for use with another stream.
/// </summary>
@@ -842,7 +851,10 @@ namespace SabreTools.Helper
TraceOutput(TraceBits.Session, "-------------------------------------------------------");
TraceOutput(TraceBits.Session, "Reset {0:X8} firstDone({1})", this.GetHashCode(), _firstWriteDone);
if (!_firstWriteDone) return;
if (!_firstWriteDone)
{
return;
}
// reset all status
_toWrite.Clear();
@@ -864,9 +876,6 @@ namespace SabreTools.Helper
_outStream = stream;
}
private void EmitPendingBuffers(bool doAll, bool mustWait)
{
// When combining parallel deflation with a ZipSegmentedStream, it's
@@ -877,10 +886,15 @@ namespace SabreTools.Helper
// method invokes this method AGAIN. This can lead to a deadlock.
// Therefore, failfast if re-entering.
if (emitting) return;
if (emitting)
{
return;
}
emitting = true;
if (doAll || mustWait)
{
_newlyCompressedBlob.WaitOne();
}
do
{
@@ -896,8 +910,10 @@ namespace SabreTools.Helper
try
{
if (_toWrite.Count > 0)
{
nextToWrite = _toWrite.Dequeue();
}
}
finally
{
Monitor.Exit(_toWrite);
@@ -930,7 +946,9 @@ namespace SabreTools.Helper
firstSkip = -1;
}
else if (firstSkip == -1)
{
firstSkip = nextToWrite;
}
continue;
}
@@ -959,12 +977,16 @@ namespace SabreTools.Helper
_toFill.Enqueue(workitem.index);
// don't wait next time through
if (millisecondsToWait == -1) millisecondsToWait = 0;
if (millisecondsToWait == -1)
{
millisecondsToWait = 0;
}
}
}
else
{
nextToWrite = -1;
}
} while (nextToWrite >= 0);
} while (doAll && (_lastWritten != _latestCompressed));
@@ -972,8 +994,6 @@ namespace SabreTools.Helper
emitting = false;
}
#if OLD
private void _PerpetualWriterMethod(object state)
{
@@ -988,8 +1008,10 @@ namespace SabreTools.Helper
_sessionReset.WaitOne();
TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch _sessionReset.WaitOne(done) PWM");
if (_isDisposed) break;
if (_isDisposed)
{
break;
}
TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch _sessionReset.Reset() PWM");
_sessionReset.Reset();
@@ -1144,9 +1166,6 @@ namespace SabreTools.Helper
}
#endif
private void _DeflateOne(Object wi)
{
// compress one buffer
@@ -1174,8 +1193,10 @@ namespace SabreTools.Helper
lock(_latestLock)
{
if (workitem.ordinal > _latestCompressed)
{
_latestCompressed = workitem.ordinal;
}
}
lock (_toWrite)
{
_toWrite.Enqueue(workitem.index);
@@ -1188,13 +1209,12 @@ namespace SabreTools.Helper
{
// expose the exception to the main thread
if (_pendingException != null)
{
_pendingException = exc1;
}
}
}
}
private bool DeflateOneSegment(WorkItem workitem)
{
@@ -1221,7 +1241,6 @@ namespace SabreTools.Helper
return true;
}
[System.Diagnostics.ConditionalAttribute("Trace")]
private void TraceOutput(TraceBits bits, string format, params object[] varParams)
{
@@ -1242,7 +1261,6 @@ namespace SabreTools.Helper
}
}
// used only when Trace is defined
[Flags]
enum TraceBits : uint
@@ -1267,8 +1285,6 @@ namespace SabreTools.Helper
All = 0xffffffff,
}
/// <summary>
/// Indicates whether the stream supports Seek operations.
/// </summary>
@@ -1280,7 +1296,6 @@ namespace SabreTools.Helper
get { return false; }
}
/// <summary>
/// Indicates whether the stream supports Read operations.
/// </summary>
@@ -1377,9 +1392,5 @@ namespace SabreTools.Helper
{
throw new NotSupportedException();
}
}
}

View File

@@ -60,8 +60,7 @@
//
// -----------------------------------------------------------------------
namespace SabreTools.Helper
namespace Ionic.Zlib
{
sealed class Tree
{
@@ -86,7 +85,6 @@ namespace SabreTools.Helper
internal static readonly sbyte[] bl_order = new sbyte[]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
// The lengths of the bit length codes are sent in order of decreasing
// probability, to avoid transmitting the lengths for unused bit
// length codes.
@@ -152,21 +150,18 @@ namespace SabreTools.Helper
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
};
internal static readonly int[] LengthBase = new int[]
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0
};
internal static readonly int[] DistanceBase = new int[]
{
0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
};
/// <summary>
/// Map from a distance to a distance code.
/// </summary>
@@ -207,7 +202,9 @@ namespace SabreTools.Helper
int overflow = 0; // number of elements with bit length too large
for (bits = 0; bits <= InternalConstants.MAX_BITS; bits++)
{
s.bl_count[bits] = 0;
}
// In a first pass, compute the optimal bit lengths (which may
// overflow in the case of the bit length tree).
@@ -225,19 +222,27 @@ namespace SabreTools.Helper
// We overwrite tree[n*2+1] which is no longer needed
if (n > max_code)
{
continue; // not a leaf node
}
s.bl_count[bits]++;
xbits = 0;
if (n >= base_Renamed)
{
xbits = extra[n - base_Renamed];
}
f = tree[n * 2];
s.opt_len += f * (bits + xbits);
if (stree != null)
{
s.static_len += f * (stree[n * 2 + 1] + xbits);
}
}
if (overflow == 0)
{
return;
}
// This happens for example on obj2 and pic of the Calgary corpus
// Find the first bit length which could increase:
@@ -245,7 +250,9 @@ namespace SabreTools.Helper
{
bits = max_length - 1;
while (s.bl_count[bits] == 0)
{
bits--;
}
s.bl_count[bits]--; // move one leaf down the tree
s.bl_count[bits + 1] = (short) (s.bl_count[bits + 1] + 2); // move one overflow item as its brother
s.bl_count[max_length]--;
@@ -262,7 +269,9 @@ namespace SabreTools.Helper
{
m = s.heap[--h];
if (m > max_code)
{
continue;
}
if (tree[m * 2 + 1] != bits)
{
s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]);
@@ -318,8 +327,9 @@ namespace SabreTools.Helper
s.depth[node] = 0;
s.opt_len--;
if (stree != null)
{
s.static_len -= stree[node * 2 + 1];
// node is 0 or 1 so it does not have extra bits
}// node is 0 or 1 so it does not have extra bits
}
this.max_code = max_code;
@@ -327,7 +337,9 @@ namespace SabreTools.Helper
// establish sub-heaps of increasing lengths:
for (n = s.heap_len / 2; n >= 1; n--)
{
s.pqdownheap(tree, n);
}
// Construct the Huffman tree by repeatedly combining the least two
// frequent nodes.
@@ -382,9 +394,12 @@ namespace SabreTools.Helper
// The distribution counts are first used to generate the code values
// without bit reversal.
for (bits = 1; bits <= InternalConstants.MAX_BITS; bits++)
unchecked {
{
unchecked
{
next_code[bits] = code = (short)((code + bl_count[bits - 1]) << 1);
}
}
// Check that the bit counts in bl_count are consistent. The last code
// must be all ones.
@@ -396,8 +411,9 @@ namespace SabreTools.Helper
{
int len = tree[n * 2 + 1];
if (len == 0)
{
continue;
// Now reverse the bits
}// Now reverse the bits
tree[n * 2] = unchecked((short) (bi_reverse(next_code[len]++, len)));
}
}

View File

@@ -86,12 +86,10 @@
//
// -----------------------------------------------------------------------
using System.Runtime.InteropServices;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
/// <summary>
/// Describes how to flush the current deflate operation.
/// </summary>
@@ -134,7 +132,6 @@ namespace SabreTools.Helper
Finish,
}
/// <summary>
/// The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress.
/// </summary>
@@ -240,7 +237,6 @@ namespace SabreTools.Helper
HuffmanOnly = 2,
}
/// <summary>
/// An enum to specify the direction of transcoding - whether to compress or decompress.
/// </summary>
@@ -256,7 +252,6 @@ namespace SabreTools.Helper
Decompress = 1,
}
/// <summary>
/// A general purpose exception class for exceptions in the Zlib library.
/// </summary>
@@ -282,7 +277,6 @@ namespace SabreTools.Helper
}
}
internal class SharedUtils
{
/// <summary>
@@ -327,7 +321,10 @@ namespace SabreTools.Helper
public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, byte[] target, int start, int count)
{
// Returns 0 bytes if not enough space in target
if (target.Length == 0) return 0;
if (target.Length == 0)
{
return 0;
}
char[] charArray = new char[target.Length];
int bytesRead = sourceTextReader.Read(charArray, start, count);
@@ -336,18 +333,18 @@ namespace SabreTools.Helper
if (bytesRead == 0) return -1;
for (int index = start; index < start + bytesRead; index++)
{
target[index] = (byte)charArray[index];
}
return bytesRead;
}
internal static byte[] ToByteArray(System.String sourceString)
{
return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
}
internal static char[] ToCharArray(byte[] byteArray)
{
return System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
@@ -374,7 +371,6 @@ namespace SabreTools.Helper
// repeat a zero length 11-138 times (7 bits of repeat count)
internal static readonly int REPZ_11_138 = 18;
}
internal sealed class StaticTree
@@ -442,6 +438,7 @@ namespace SabreTools.Helper
this.elems = elems;
this.maxLength = maxLength;
}
static StaticTree()
{
Literals = new StaticTree(lengthAndLiteralsTreeCodes, Tree.ExtraLengthBits, InternalConstants.LITERALS + 1, InternalConstants.L_CODES, InternalConstants.MAX_BITS);
@@ -450,8 +447,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Computes an Adler-32 checksum.
/// </summary>
@@ -470,7 +465,6 @@ namespace SabreTools.Helper
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private static readonly int NMAX = 5552;
#pragma warning disable 3001
#pragma warning disable 3002
@@ -492,7 +486,9 @@ namespace SabreTools.Helper
public static uint Adler32(uint adler, byte[] buf, int index, int len)
{
if (buf == null)
{
return 1;
}
uint s1 = (uint)(adler & 0xffff);
uint s2 = (uint)((adler >> 16) & 0xffff);
@@ -538,7 +534,5 @@ namespace SabreTools.Helper
}
#pragma warning restore 3001
#pragma warning restore 3002
}
}

View File

@@ -27,9 +27,8 @@
using System;
using System.IO;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
internal enum ZlibStreamFlavor { ZLIB = 1950, DEFLATE = 1951, GZIP = 1952 }
internal class ZlibBaseStream : System.IO.Stream
@@ -110,34 +109,40 @@ namespace SabreTools.Helper
}
}
private byte[] workingBuffer
{
get
{
if (_workingBuffer == null)
{
_workingBuffer = new byte[_bufferSize];
}
return _workingBuffer;
}
}
public override void Write(System.Byte[] buffer, int offset, int count)
{
// workitem 7159
// calculate the CRC on the unccompressed data (before writing)
if (crc != null)
{
crc.SlurpBlock(buffer, offset, count);
}
if (_streamMode == StreamMode.Undefined)
{
_streamMode = StreamMode.Writer;
}
else if (_streamMode != StreamMode.Writer)
{
throw new ZlibException("Cannot Write after Reading.");
}
if (count == 0)
{
return;
}
// first reference of z property will initialize the private var _z
z.InputBuffer = buffer;
@@ -153,7 +158,9 @@ namespace SabreTools.Helper
? _z.Deflate(_flushMode)
: _z.Inflate(_flushMode);
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
{
throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
}
//if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
_stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
@@ -162,17 +169,19 @@ namespace SabreTools.Helper
// If GZIP and de-compress, we're done when 8 bytes remain.
if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
{
done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
}
}
while (!done);
}
private void finish()
{
if (_z == null) return;
if (_z == null)
{
return;
}
if (_streamMode == StreamMode.Writer)
{
@@ -190,10 +199,14 @@ namespace SabreTools.Helper
{
string verb = (_wantCompress ? "de" : "in") + "flating";
if (_z.Message == null)
{
throw new ZlibException(String.Format("{0}: (rc = {1})", verb, rc));
}
else
{
throw new ZlibException(verb + ": " + _z.Message);
}
}
if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
{
@@ -236,7 +249,9 @@ namespace SabreTools.Helper
{
// workitem 8501: handle edge case (decompress empty stream)
if (_z.TotalBytesOut == 0L)
{
return;
}
// Read and potentially verify the GZIP trailer:
// CRC32 and size mod 2^32
@@ -268,11 +283,14 @@ namespace SabreTools.Helper
Int32 isize_actual = (Int32)(_z.TotalBytesOut & 0x00000000FFFFFFFF);
if (crc32_actual != crc32_expected)
{
throw new ZlibException(String.Format("Bad CRC32 in GZIP trailer. (actual({0:X8})!=expected({1:X8}))", crc32_actual, crc32_expected));
}
if (isize_actual != isize_expected)
{
throw new ZlibException(String.Format("Bad size in GZIP trailer. (actual({0})!=expected({1}))", isize_actual, isize_expected));
}
}
else
{
@@ -282,11 +300,12 @@ namespace SabreTools.Helper
}
}
private void end()
{
if (z == null)
{
return;
}
if (_wantCompress)
{
_z.EndDeflate();
@@ -301,7 +320,10 @@ namespace SabreTools.Helper
public override void Close()
{
if (_stream == null) return;
if (_stream == null)
{
return;
}
try
{
finish();
@@ -309,7 +331,10 @@ namespace SabreTools.Helper
finally
{
end();
if (!_leaveOpen) _stream.Close();
if (!_leaveOpen)
{
_stream.Close();
}
_stream = null;
}
}
@@ -329,7 +354,6 @@ namespace SabreTools.Helper
_stream.SetLength(value);
}
#if NOT
public int Read()
{
@@ -344,8 +368,6 @@ namespace SabreTools.Helper
private bool nomoreinput = false;
private string ReadZeroTerminatedString()
{
var list = new System.Collections.Generic.List<byte>();
@@ -359,16 +381,19 @@ namespace SabreTools.Helper
else
{
if (_buf1[0] == 0)
{
done = true;
}
else
{
list.Add(_buf1[0]);
}
}
} while (!done);
byte[] a = list.ToArray();
return GZipStream.iso8859dash1.GetString(a, 0, a.Length);
}
private int _ReadAndValidateGzipHeader()
{
int totalBytesRead = 0;
@@ -378,13 +403,19 @@ namespace SabreTools.Helper
// workitem 8501: handle edge case (decompress empty stream)
if (n == 0)
{
return 0;
}
if (n != 10)
{
throw new ZlibException("Not a valid GZIP stream.");
}
if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8)
{
throw new ZlibException("Bad GZIP header.");
}
Int32 timet = BitConverter.ToInt32(header, 4);
_GzipMtime = GZipStream._unixEpoch.AddSeconds(timet);
@@ -399,21 +430,27 @@ namespace SabreTools.Helper
byte[] extra = new byte[extraLength];
n = _stream.Read(extra, 0, extra.Length);
if (n != extraLength)
{
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
}
totalBytesRead += n;
}
if ((header[3] & 0x08) == 0x08)
{
_GzipFileName = ReadZeroTerminatedString();
}
if ((header[3] & 0x10) == 0x010)
{
_GzipComment = ReadZeroTerminatedString();
}
if ((header[3] & 0x02) == 0x02)
{
Read(_buf1, 0, 1); // CRC16, ignore
}
return totalBytesRead;
}
public override System.Int32 Read(System.Byte[] buffer, System.Int32 offset, System.Int32 count)
{
// According to MS documentation, any implementation of the IO.Stream.Read function must:
@@ -424,8 +461,10 @@ namespace SabreTools.Helper
if (_streamMode == StreamMode.Undefined)
{
if (!this._stream.CanRead) throw new ZlibException("The stream is not readable.");
// for the first read, set up some controls.
if (!this._stream.CanRead)
{
throw new ZlibException("The stream is not readable.");
}// for the first read, set up some controls.
_streamMode = StreamMode.Reader;
// (The first reference to _z goes through the private accessor which
// may initialize it.)
@@ -435,19 +474,41 @@ namespace SabreTools.Helper
_gzipHeaderByteCount = _ReadAndValidateGzipHeader();
// workitem 8501: handle edge case (decompress empty stream)
if (_gzipHeaderByteCount == 0)
{
return 0;
}
}
}
if (_streamMode != StreamMode.Reader)
{
throw new ZlibException("Cannot Read after Writing.");
}
if (count == 0) return 0;
if (nomoreinput && _wantCompress) return 0; // workitem 8557
if (buffer == null) throw new ArgumentNullException("buffer");
if (count < 0) throw new ArgumentOutOfRangeException("count");
if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");
if (count == 0)
{
return 0;
}
if (nomoreinput && _wantCompress)
{
return 0; // workitem 8557
}
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
if (count < 0)
{
throw new ArgumentOutOfRangeException("count");
}
if (offset < buffer.GetLowerBound(0))
{
throw new ArgumentOutOfRangeException("offset");
}
if ((offset + count) > buffer.GetLength(0))
{
throw new ArgumentOutOfRangeException("count");
}
int rc = 0;
@@ -470,8 +531,9 @@ namespace SabreTools.Helper
_z.NextIn = 0;
_z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length);
if (_z.AvailableBytesIn == 0)
{
nomoreinput = true;
}
}
// we have data in InputBuffer; now compress or decompress as appropriate
rc = (_wantCompress)
@@ -479,18 +541,23 @@ namespace SabreTools.Helper
: _z.Inflate(_flushMode);
if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR))
{
return 0;
}
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
{
throw new ZlibException(String.Format("{0}flating: rc={1} msg={2}", (_wantCompress ? "de" : "in"), rc, _z.Message));
}
if ((nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count))
{
break; // nothing more to read
}
}
//while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK);
while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK);
// workitem 8557
// is there more room in output?
if (_z.AvailableBytesOut > 0)
@@ -511,23 +578,23 @@ namespace SabreTools.Helper
rc = _z.Deflate(FlushType.Finish);
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
{
throw new ZlibException(String.Format("Deflating: rc={0} msg={1}", rc, _z.Message));
} }
}
}
}
rc = (count - _z.AvailableBytesOut);
// calculate CRC after reading
if (crc != null)
{
crc.SlurpBlock(buffer, offset, rc);
}
return rc;
}
public override System.Boolean CanRead
{
get { return this._stream.CanRead; }
@@ -561,7 +628,6 @@ namespace SabreTools.Helper
Undefined,
}
public static void CompressString(String s, Stream compressor)
{
byte[] uncompressed = System.Text.Encoding.UTF8.GetBytes(s);
@@ -620,8 +686,5 @@ namespace SabreTools.Helper
return output.ToArray();
}
}
}
}

View File

@@ -63,11 +63,10 @@
//
// -----------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
/// <summary>
/// Encoder and Decoder for ZLIB and DEFLATE (IETF RFC1950 and RFC1951).
@@ -175,13 +174,11 @@ namespace SabreTools.Helper
/// </remarks>
public CompressionStrategy Strategy = CompressionStrategy.Default;
/// <summary>
/// The Adler32 checksum on the data transferred through the codec so far. You probably don't need to look at this.
/// </summary>
public int Adler32 { get { return (int)_Adler32; } }
/// <summary>
/// Create a ZlibCodec.
/// </summary>
@@ -203,12 +200,18 @@ namespace SabreTools.Helper
if (mode == CompressionMode.Compress)
{
int rc = InitializeDeflate();
if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for deflate.");
if (rc != ZlibConstants.Z_OK)
{
throw new ZlibException("Cannot initialize for deflate.");
}
}
else if (mode == CompressionMode.Decompress)
{
int rc = InitializeInflate();
if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for inflate.");
if (rc != ZlibConstants.Z_OK)
{
throw new ZlibException("Cannot initialize for inflate.");
}
}
else throw new ZlibException("Invalid ZlibStreamFlavor.");
}
@@ -283,7 +286,10 @@ namespace SabreTools.Helper
public int InitializeInflate(int windowBits, bool expectRfc1950Header)
{
this.WindowBits = windowBits;
if (dstate != null) throw new ZlibException("You may not call InitializeInflate() after calling InitializeDeflate().");
if (dstate != null)
{
throw new ZlibException("You may not call InitializeInflate() after calling InitializeDeflate().");
}
istate = new InflateManager(expectRfc1950Header);
return istate.Initialize(this, windowBits);
}
@@ -354,11 +360,12 @@ namespace SabreTools.Helper
public int Inflate(FlushType flush)
{
if (istate == null)
{
throw new ZlibException("No Inflate State!");
}
return istate.Inflate(flush);
}
/// <summary>
/// Ends an inflation session.
/// </summary>
@@ -371,8 +378,9 @@ namespace SabreTools.Helper
public int EndInflate()
{
if (istate == null)
{
throw new ZlibException("No Inflate State!");
int ret = istate.End();
} int ret = istate.End();
istate = null;
return ret;
}
@@ -384,7 +392,9 @@ namespace SabreTools.Helper
public int SyncInflate()
{
if (istate == null)
{
throw new ZlibException("No Inflate State!");
}
return istate.Sync();
}
@@ -448,7 +458,6 @@ namespace SabreTools.Helper
return _InternalInitializeDeflate(true);
}
/// <summary>
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
/// and the explicit flag governing whether to emit an RFC1950 header byte pair.
@@ -469,7 +478,6 @@ namespace SabreTools.Helper
return _InternalInitializeDeflate(wantRfc1950Header);
}
/// <summary>
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
/// and the specified number of window bits.
@@ -506,7 +514,10 @@ namespace SabreTools.Helper
private int _InternalInitializeDeflate(bool wantRfc1950Header)
{
if (istate != null) throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate().");
if (istate != null)
{
throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate().");
}
dstate = new DeflateManager();
dstate.WantRfc1950HeaderBytes = wantRfc1950Header;
@@ -584,7 +595,9 @@ namespace SabreTools.Helper
public int Deflate(FlushType flush)
{
if (dstate == null)
{
throw new ZlibException("No Deflate State!");
}
return dstate.Deflate(flush);
}
@@ -598,7 +611,9 @@ namespace SabreTools.Helper
public int EndDeflate()
{
if (dstate == null)
{
throw new ZlibException("No Deflate State!");
}
// TODO: dinoch Tue, 03 Nov 2009 15:39 (test this)
//int ret = dstate.End();
dstate = null;
@@ -617,11 +632,12 @@ namespace SabreTools.Helper
public void ResetDeflate()
{
if (dstate == null)
{
throw new ZlibException("No Deflate State!");
}
dstate.Reset();
}
/// <summary>
/// Set the CompressionStrategy and CompressionLevel for a deflation session.
/// </summary>
@@ -631,11 +647,12 @@ namespace SabreTools.Helper
public int SetDeflateParams(CompressionLevel level, CompressionStrategy strategy)
{
if (dstate == null)
{
throw new ZlibException("No Deflate State!");
}
return dstate.SetParams(level, strategy);
}
/// <summary>
/// Set the dictionary to be used for either Inflation or Deflation.
/// </summary>
@@ -644,10 +661,14 @@ namespace SabreTools.Helper
public int SetDictionary(byte[] dictionary)
{
if (istate != null)
{
return istate.SetDictionary(dictionary);
}
if (dstate != null)
{
return dstate.SetDictionary(dictionary);
}
throw new ZlibException("No Inflate or Deflate state!");
}
@@ -661,9 +682,13 @@ namespace SabreTools.Helper
int len = dstate.pendingCount;
if (len > AvailableBytesOut)
{
len = AvailableBytesOut;
}
if (len == 0)
{
return;
}
if (dstate.pending.Length <= dstate.nextPending ||
OutputBuffer.Length <= NextOut ||
@@ -697,9 +722,13 @@ namespace SabreTools.Helper
int len = AvailableBytesIn;
if (len > size)
{
len = size;
}
if (len == 0)
{
return 0;
}
AvailableBytesIn -= len;
@@ -712,6 +741,5 @@ namespace SabreTools.Helper
TotalBytesIn += len;
return len;
}
}
}

View File

@@ -60,8 +60,7 @@
//
// -----------------------------------------------------------------------
namespace SabreTools.Helper
namespace Ionic.Zlib
{
/// <summary>
/// A bunch of constants used in the Zlib interface.
@@ -121,6 +120,5 @@ namespace SabreTools.Helper
/// </summary>
public const int WorkingBufferSizeMin = 1024;
}
}

View File

@@ -28,9 +28,8 @@
using System;
using System.IO;
namespace SabreTools.Helper
namespace Ionic.Zlib
{
/// <summary>
/// Represents a Zlib stream for compression or decompression.
/// </summary>
@@ -331,7 +330,10 @@ namespace SabreTools.Helper
get { return (this._baseStream._flushMode); }
set
{
if (_disposed) throw new ObjectDisposedException("ZlibStream");
if (_disposed)
{
throw new ObjectDisposedException("ZlibStream");
}
this._baseStream._flushMode = value;
}
}
@@ -361,11 +363,18 @@ namespace SabreTools.Helper
}
set
{
if (_disposed) throw new ObjectDisposedException("ZlibStream");
if (_disposed)
{
throw new ObjectDisposedException("ZlibStream");
}
if (this._baseStream._workingBuffer != null)
{
throw new ZlibException("The working buffer is already set.");
}
if (value < ZlibConstants.WorkingBufferSizeMin)
{
throw new ZlibException(String.Format("Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin));
}
this._baseStream._bufferSize = value;
}
}
@@ -416,7 +425,9 @@ namespace SabreTools.Helper
if (!_disposed)
{
if (disposing && (this._baseStream != null))
{
this._baseStream.Close();
}
_disposed = true;
}
}
@@ -426,7 +437,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Indicates whether the stream can be read.
/// </summary>
@@ -437,7 +447,10 @@ namespace SabreTools.Helper
{
get
{
if (_disposed) throw new ObjectDisposedException("ZlibStream");
if (_disposed)
{
throw new ObjectDisposedException("ZlibStream");
}
return _baseStream._stream.CanRead;
}
}
@@ -463,7 +476,10 @@ namespace SabreTools.Helper
{
get
{
if (_disposed) throw new ObjectDisposedException("ZlibStream");
if (_disposed)
{
throw new ObjectDisposedException("ZlibStream");
}
return _baseStream._stream.CanWrite;
}
}
@@ -473,7 +489,10 @@ namespace SabreTools.Helper
/// </summary>
public override void Flush()
{
if (_disposed) throw new ObjectDisposedException("ZlibStream");
if (_disposed)
{
throw new ObjectDisposedException("ZlibStream");
}
_baseStream.Flush();
}
@@ -501,9 +520,13 @@ namespace SabreTools.Helper
get
{
if (this._baseStream._streamMode == ZlibBaseStream.StreamMode.Writer)
{
return this._baseStream._z.TotalBytesOut;
}
if (this._baseStream._streamMode == ZlibBaseStream.StreamMode.Reader)
{
return this._baseStream._z.TotalBytesIn;
}
return 0;
}
@@ -545,7 +568,10 @@ namespace SabreTools.Helper
/// <returns>the number of bytes read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
if (_disposed) throw new ObjectDisposedException("ZlibStream");
if (_disposed)
{
throw new ObjectDisposedException("ZlibStream");
}
return _baseStream.Read(buffer, offset, count);
}
@@ -607,11 +633,14 @@ namespace SabreTools.Helper
/// <param name="count">the number of bytes to write.</param>
public override void Write(byte[] buffer, int offset, int count)
{
if (_disposed) throw new ObjectDisposedException("ZlibStream");
if (_disposed)
{
throw new ObjectDisposedException("ZlibStream");
}
_baseStream.Write(buffer, offset, count);
}
#endregion
#endregion
/// <summary>
/// Compress a string into a byte array using ZLIB.
@@ -642,7 +671,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Compress a byte array into a new byte array using ZLIB.
/// </summary>
@@ -671,7 +699,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Uncompress a ZLIB-compressed byte array into a single string.
/// </summary>
@@ -695,7 +722,6 @@ namespace SabreTools.Helper
}
}
/// <summary>
/// Uncompress a ZLIB-compressed byte array into a byte array.
/// </summary>
@@ -718,8 +744,5 @@ namespace SabreTools.Helper
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
[Serializable]
public class Archive : DatItem
@@ -26,50 +27,6 @@ namespace SabreTools.Helper
_itemType = ItemType.Archive;
}
/// <summary>
/// Create a new Archive object with the included information
/// </summary>
/// <param name="name">Name of the item, including extension</param>
/// <param name="machineName">Name for the machine/game</param>
/// <param name="comment">Comment for the machine/game</param>
/// <param name="machineDescription">Description for the machine/game</param>
/// <param name="year">Year for the machine/game</param>
/// <param name="manufacturer">Manufacturer name for the machine/game</param>
/// <param name="romOf">Set that this machine/game is a rom of</param>
/// <param name="cloneOf">Set that this machine/game is a clone of</param>
/// <param name="sampleOf">Set that this machine/game is a sample of</param>
/// <param name="sourceFile">Source file for the machine/game</param>
/// <param name="isBios">True if this game is a BIOS, false otherwise</param>
/// <param name="board">Name of the board for this machine/game</param>
/// <param name="rebuildTo">Name of the game to rebuild to</param>
/// <param name="systemId">System ID to be associated with</param>
/// <param name="systemName">System Name to be associated with</param>
/// <param name="sourceId">Source ID to be associated with</param>
/// <param name="sourceName">Source Name to be associated with</param>
public Archive(string name, string machineName, string comment, string machineDescription, string year,
string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile, bool isBios, string board, string rebuildTo,
int systemId, string systemName, int sourceId, string sourceName)
{
_name = name;
_itemType = ItemType.Archive;
_machineName = machineName;
_comment = comment;
_machineDescription = machineDescription;
_year = year;
_manufacturer = manufacturer;
_romOf = romOf;
_cloneOf = cloneOf;
_sampleOf = sampleOf;
_sourceFile = sourceFile;
_isBios = isBios;
_board = board;
_rebuildTo = rebuildTo;
_systemId = systemId;
_systemName = systemName;
_sourceId = sourceId;
_sourceName = sourceName;
}
#endregion
#region Comparision Methods

View File

@@ -1,6 +1,7 @@
using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
[Serializable]
public class BiosSet : DatItem
@@ -52,54 +53,6 @@ namespace SabreTools.Helper
_default = @default;
}
/// <summary>
/// Create a new Sample object with the included information
/// </summary>
/// <param name="name">Name of the item, including extension</param>
/// <param name="description">Description of the Bios set item</param>
/// <param name="default">True if this is the default BIOS, false if it is not, null for undefined</param>
/// <param name="machineName">Name for the machine/game</param>
/// <param name="comment">Comment for the machine/game</param>
/// <param name="machineDescription">Description for the machine/game</param>
/// <param name="year">Year for the machine/game</param>
/// <param name="manufacturer">Manufacturer name for the machine/game</param>
/// <param name="romOf">Set that this machine/game is a rom of</param>
/// <param name="cloneOf">Set that this machine/game is a clone of</param>
/// <param name="sampleOf">Set that this machine/game is a sample of</param>
/// <param name="sourceFile">Source file for the machine/game</param>
/// <param name="isBios">True if this game is a BIOS, false otherwise</param>
/// <param name="board">Name of the board for this machine/game</param>
/// <param name="rebuildTo">Name of the game to rebuild to</param>
/// <param name="systemId">System ID to be associated with</param>
/// <param name="systemName">System Name to be associated with</param>
/// <param name="sourceId">Source ID to be associated with</param>
/// <param name="sourceName">Source Name to be associated with</param>
public BiosSet(string name, string description, bool? @default, string machineName, string comment, string machineDescription, string year,
string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile, bool isBios, string board, string rebuildTo,
int systemId, string systemName, int sourceId, string sourceName)
{
_name = name;
_itemType = ItemType.BiosSet;
_description = description;
_default = @default;
_machineName = machineName;
_comment = comment;
_machineDescription = machineDescription;
_year = year;
_manufacturer = manufacturer;
_romOf = romOf;
_cloneOf = cloneOf;
_sampleOf = sampleOf;
_sourceFile = sourceFile;
_isBios = isBios;
_board = board;
_rebuildTo = rebuildTo;
_systemId = systemId;
_systemName = systemName;
_sourceId = sourceId;
_sourceName = sourceName;
}
#endregion
#region Comparision Methods

View File

@@ -1,5 +1,4 @@
using SharpCompress.Common;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -8,8 +7,12 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
using NaturalSort;
using SharpCompress.Common;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
[Serializable]
public class DatFile : ICloneable
@@ -331,9 +334,9 @@ namespace SabreTools.Helper
: rom.SystemID.ToString().PadLeft(10, '0')
+ "-"
+ rom.SourceID.ToString().PadLeft(10, '0') + "-")
+ (String.IsNullOrEmpty(rom.MachineName)
+ (String.IsNullOrEmpty(rom.Machine.Name)
? "Default"
: rom.MachineName.ToLowerInvariant());
: rom.Machine.Name.ToLowerInvariant());
newkey = HttpUtility.HtmlEncode(newkey);
if (sortable.ContainsKey(newkey))
{
@@ -1105,7 +1108,7 @@ namespace SabreTools.Helper
if ((diff & DiffMode.NoDupes) != 0)
{
DatItem newrom = rom;
newrom.MachineName += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
if (outerDiffData.Files.ContainsKey(key))
{
@@ -1127,7 +1130,7 @@ namespace SabreTools.Helper
if ((rom.Dupe & DupeType.External) != 0)
{
DatItem newrom = rom;
newrom.MachineName += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
if (dupeData.Files.ContainsKey(key))
{
@@ -1311,9 +1314,9 @@ namespace SabreTools.Helper
rootpath += (rootpath == "" ? "" : Path.DirectorySeparatorChar.ToString());
filename = filename.Remove(0, rootpath.Length);
newrom.MachineName = Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
newrom.Machine.Name = Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename) + Path.DirectorySeparatorChar
+ newrom.MachineName;
+ newrom.Machine.Name;
newroms.Add(newrom);
}
Files[key] = newroms;
@@ -1586,13 +1589,13 @@ namespace SabreTools.Helper
}
// Then populate it with information
item.MachineName = tempgamename;
item.MachineDescription = gamedesc;
item.CloneOf = cloneof;
item.RomOf = romof;
item.SampleOf = sampleof;
item.Manufacturer = manufacturer;
item.Year = year;
item.Machine.Name = tempgamename;
item.Machine.Description = gamedesc;
item.Machine.CloneOf = cloneof;
item.Machine.RomOf = romof;
item.Machine.SampleOf = sampleof;
item.Machine.Manufacturer = manufacturer;
item.Machine.Year = year;
item.SystemID = sysid;
item.SourceID = srcid;
@@ -2628,9 +2631,7 @@ namespace SabreTools.Helper
case "machine":
case "game":
case "software":
string temptype = xtr.Name, tempname = "", gamedesc = "", romof = "",
cloneof = "", sampleof = "", year = "", manufacturer = "", publisher = "",
partname = "", partinterface = "", areaname = "";
string temptype = xtr.Name, publisher = "", partname = "", partinterface = "", areaname = "";
bool? supported = null;
long? areasize = null;
List<Tuple<string, string>> infos = new List<Tuple<string, string>>();
@@ -2652,10 +2653,16 @@ namespace SabreTools.Helper
// Otherwise, add what is possible
subreader.MoveToContent();
tempname = xtr.GetAttribute("name");
romof = (xtr.GetAttribute("romof") != null ? xtr.GetAttribute("romof") : "");
cloneof = (xtr.GetAttribute("cloneof") != null ? xtr.GetAttribute("cloneof") : "");
sampleof = (xtr.GetAttribute("sampleof") != null ? xtr.GetAttribute("sampleof") : "");
// Create a new machine
Machine machine = new Machine
{
Name = xtr.GetAttribute("name"),
Description = xtr.GetAttribute("name"),
RomOf = xtr.GetAttribute("romof") ?? "",
CloneOf = xtr.GetAttribute("cloneof") ?? "",
SampleOf = xtr.GetAttribute("sampleof") ?? "",
};
if (subreader.GetAttribute("supported") != null)
{
switch (subreader.GetAttribute("supported"))
@@ -2671,16 +2678,16 @@ namespace SabreTools.Helper
if (superdat && !keep)
{
string tempout = Regex.Match(tempname, @".*?\\(.*)").Groups[1].Value;
string tempout = Regex.Match(machine.Name, @".*?\\(.*)").Groups[1].Value;
if (tempout != "")
{
tempname = tempout;
machine.Name = tempout;
}
}
// Get the name of the game from the parent
else if (superdat && keep && parent.Count > 0)
{
tempname = String.Join("\\", parent) + "\\" + tempname;
machine.Name = String.Join("\\", parent) + "\\" + machine.Name;
}
// Special offline list parts
@@ -2715,7 +2722,7 @@ namespace SabreTools.Helper
{
// For OfflineList only
case "title":
tempname = subreader.ReadElementContentAsString();
machine.Name = subreader.ReadElementContentAsString();
break;
case "releaseNumber":
releaseNumber = subreader.ReadElementContentAsString();
@@ -2731,8 +2738,15 @@ namespace SabreTools.Helper
ext = (subreader.GetAttribute("extension") != null ? subreader.GetAttribute("extension") : "");
DatItem olrom = new Rom(releaseNumber + " - " + tempname + ext, size, subreader.ReadElementContentAsString(), null, null, ItemStatus.None,
null, tempname, null, tempname, null, null, null, null, null, null, false, null, null, sysid, null, srcid, "");
DatItem olrom = new Rom
{
Name = releaseNumber + " - " + machine.Name + ext,
Size = size,
CRC = subreader.ReadElementContentAsString(),
ItemStatus = ItemStatus.None,
Machine = machine,
};
// Now process and add the rom
ParseAddHelper(olrom, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, itemStatus, trim, single, root, clean, logger, out key);
@@ -2768,17 +2782,17 @@ namespace SabreTools.Helper
// For Logiqx, SabreDAT, and Software List
case "description":
gamedesc = subreader.ReadElementContentAsString();
machine.Description = subreader.ReadElementContentAsString();
if (!softlist && temptype == "software")
{
tempname = gamedesc.Replace('/', '_').Replace("\"", "''");
machine.Name = machine.Description.Replace('/', '_').Replace("\"", "''");
}
break;
case "year":
year = subreader.ReadElementContentAsString();
machine.Year = subreader.ReadElementContentAsString();
break;
case "manufacturer":
manufacturer = subreader.ReadElementContentAsString();
machine.Manufacturer = subreader.ReadElementContentAsString();
break;
case "release":
empty = false;
@@ -2796,16 +2810,25 @@ namespace SabreTools.Helper
}
}
DatItem relrom = new Release(subreader.GetAttribute("name"), subreader.GetAttribute("region"), subreader.GetAttribute("language"), date, defaultrel);
relrom.Supported = supported;
relrom.Year = year;
relrom.Publisher = publisher;
relrom.Infos = infos;
relrom.PartName = partname;
relrom.PartInterface = partinterface;
relrom.Features = features;
relrom.AreaName = areaname;
relrom.AreaSize = areasize;
DatItem relrom = new Release
{
Name = subreader.GetAttribute("name"),
Region = subreader.GetAttribute("region"),
Language = subreader.GetAttribute("language"),
Date = date,
Default = defaultrel,
Machine = machine,
Supported = supported,
Publisher = publisher,
Infos = infos,
PartName = partname,
PartInterface = partinterface,
Features = features,
AreaName = areaname,
AreaSize = areasize,
};
// Now process and add the rom
ParseAddHelper(relrom, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, itemStatus, trim, single, root, clean, logger, out key);
@@ -2828,17 +2851,27 @@ namespace SabreTools.Helper
}
}
DatItem biosrom = new BiosSet(subreader.GetAttribute("name"), subreader.GetAttribute("description"), defaultbios,
tempname, null, gamedesc, null, null, romof, cloneof, sampleof, null, false, null, null, sysid, filename, srcid, null);
biosrom.Supported = supported;
biosrom.Year = year;
biosrom.Publisher = publisher;
biosrom.Infos = infos;
biosrom.PartName = partname;
biosrom.PartInterface = partinterface;
biosrom.Features = features;
biosrom.AreaName = areaname;
biosrom.AreaSize = areasize;
DatItem biosrom = new BiosSet
{
Name = subreader.GetAttribute("name"),
Description = subreader.GetAttribute("description"),
Default = defaultbios,
Machine = machine,
Supported = supported,
Publisher = publisher,
Infos = infos,
PartName = partname,
PartInterface = partinterface,
Features = features,
AreaName = areaname,
AreaSize = areasize,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
// Now process and add the rom
ParseAddHelper(biosrom, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, itemStatus, trim, single, root, clean, logger, out key);
@@ -2848,17 +2881,25 @@ namespace SabreTools.Helper
case "archive":
empty = false;
DatItem archiverom = new Archive(subreader.GetAttribute("name"), tempname, null, gamedesc, null, null,
romof, cloneof, sampleof, null, false, null, null, sysid, filename, srcid, null);
archiverom.Supported = supported;
archiverom.Year = year;
archiverom.Publisher = publisher;
archiverom.Infos = infos;
archiverom.PartName = partname;
archiverom.PartInterface = partinterface;
archiverom.Features = features;
archiverom.AreaName = areaname;
archiverom.AreaSize = areasize;
DatItem archiverom = new Archive
{
Name = subreader.GetAttribute("name"),
Machine = machine,
Supported = supported,
Publisher = publisher,
Infos = infos,
PartName = partname,
PartInterface = partinterface,
Features = features,
AreaName = areaname,
AreaSize = areasize,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
// Now process and add the rom
ParseAddHelper(archiverom, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, itemStatus, trim, single, root, clean, logger, out key);
@@ -2868,17 +2909,25 @@ namespace SabreTools.Helper
case "sample":
empty = false;
DatItem samplerom = new Sample(subreader.GetAttribute("name"), tempname, null, gamedesc, null, null,
romof, cloneof, sampleof, null, false, null, null, sysid, filename, srcid, null);
samplerom.Supported = supported;
samplerom.Year = year;
samplerom.Publisher = publisher;
samplerom.Infos = infos;
samplerom.PartName = partname;
samplerom.PartInterface = partinterface;
samplerom.Features = features;
samplerom.AreaName = areaname;
samplerom.AreaSize = areasize;
DatItem samplerom = new Sample
{
Name = subreader.GetAttribute("name"),
Machine = machine,
Supported = supported,
Publisher = publisher,
Infos = infos,
PartName = partname,
PartInterface = partinterface,
Features = features,
AreaName = areaname,
AreaSize = areasize,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
// Now process and add the rom
ParseAddHelper(samplerom, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, itemStatus, trim, single, root, clean, logger, out key);
@@ -2956,33 +3005,65 @@ namespace SabreTools.Helper
// If we're in clean mode, sanitize the game name
if (clean)
{
tempname = Style.CleanGameName(tempname.Split(Path.DirectorySeparatorChar));
machine.Name = Style.CleanGameName(machine.Name.Split(Path.DirectorySeparatorChar));
}
DatItem inrom;
switch (subreader.Name.ToLowerInvariant())
{
case "disk":
inrom = new Disk(subreader.GetAttribute("name"), subreader.GetAttribute("md5"), subreader.GetAttribute("sha1"),
its, tempname, null, gamedesc, null, null, romof, cloneof, sampleof, null, false, null, null, sysid,
filename, srcid, null);
inrom = new Disk
{
Name = subreader.GetAttribute("name"),
MD5 = subreader.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = subreader.GetAttribute("sha1")?.ToLowerInvariant(),
ItemStatus = its,
Machine = machine,
Supported = supported,
Publisher = publisher,
Infos = infos,
PartName = partname,
PartInterface = partinterface,
Features = features,
AreaName = areaname,
AreaSize = areasize,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
break;
case "rom":
default:
inrom = new Rom(subreader.GetAttribute("name"), size, subreader.GetAttribute("crc"), subreader.GetAttribute("md5"),
subreader.GetAttribute("sha1"), its, date, tempname, null, gamedesc, null, null, romof, cloneof, sampleof,
null, false, null, null, sysid, filename, srcid, null);
inrom = new Rom
{
Name = subreader.GetAttribute("name"),
Size = size,
CRC = subreader.GetAttribute("crc"),
MD5 = subreader.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = subreader.GetAttribute("sha1")?.ToLowerInvariant(),
ItemStatus = its,
Date = date,
Machine = machine,
Supported = supported,
Publisher = publisher,
Infos = infos,
PartName = partname,
PartInterface = partinterface,
Features = features,
AreaName = areaname,
AreaSize = areasize,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
break;
}
inrom.Supported = supported;
inrom.Year = year;
inrom.Publisher = publisher;
inrom.Infos = infos;
inrom.PartName = partname;
inrom.PartInterface = partinterface;
inrom.Features = features;
inrom.AreaName = areaname;
inrom.AreaSize = areasize;
// Now process and add the rom
ParseAddHelper(inrom, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, itemStatus, trim, single, root, clean, logger, out key);
@@ -3101,16 +3182,19 @@ namespace SabreTools.Helper
continue;
}
Machine dir = new Machine();
// Get the name of the game from the parent
tempname = String.Join("\\", parent);
dir.Name = String.Join("\\", parent);
dir.Description = dir.Name;
// If we aren't keeping names, trim out the path
if (!keep || !superdat)
{
string tempout = Regex.Match(tempname, @".*?\\(.*)").Groups[1].Value;
string tempout = Regex.Match(dir.Name, @".*?\\(.*)").Groups[1].Value;
if (tempout != "")
{
tempname = tempout;
dir.Name = tempout;
}
}
@@ -3118,16 +3202,38 @@ namespace SabreTools.Helper
switch (xtr.GetAttribute("type").ToLowerInvariant())
{
case "disk":
rom = new Disk(xtr.GetAttribute("name"), xtr.GetAttribute("md5")?.ToLowerInvariant(),
xtr.GetAttribute("sha1")?.ToLowerInvariant(), its, tempname, null, tempname, null, null,
null, null, null, null, false, null, null, sysid, filename, srcid, null);
rom = new Disk
{
Name = xtr.GetAttribute("name"),
MD5 = xtr.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = xtr.GetAttribute("sha1")?.ToLowerInvariant(),
ItemStatus = its,
Machine = dir,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
break;
case "rom":
default:
rom = new Rom(xtr.GetAttribute("name"), size, xtr.GetAttribute("crc")?.ToLowerInvariant(),
xtr.GetAttribute("md5")?.ToLowerInvariant(), xtr.GetAttribute("sha1")?.ToLowerInvariant(), its,
date, tempname, null, tempname, null, null, null, null, null, null, false, null, null, sysid, filename,
srcid, null);
rom = new Rom
{
Name = xtr.GetAttribute("name"),
Size = size,
CRC = xtr.GetAttribute("crc")?.ToLowerInvariant(),
MD5 = xtr.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = xtr.GetAttribute("sha1")?.ToLowerInvariant(),
ItemStatus = its,
Date = date,
Machine = dir,
SystemID = sysid,
System = filename,
SourceID = srcid,
};
break;
}
@@ -3193,8 +3299,21 @@ namespace SabreTools.Helper
{
string line = sr.ReadLine();
Rom rom = new Rom(line.Split(' ')[1].Replace("*", String.Empty), -1, null, line.Split(' ')[0], null, ItemStatus.None, null,
Path.GetFileNameWithoutExtension(filename), null, null, null, null, null, null, null, null, false, null, null, sysid, null, srcid, null);
Rom rom = new Rom
{
Name = line.Split(' ')[1].Replace("*", String.Empty),
Size = -1,
MD5 = line.Split(' ')[0],
ItemStatus = ItemStatus.None,
Machine = new Machine
{
Name = Path.GetFileNameWithoutExtension(filename),
},
SystemID = sysid,
SourceID = srcid,
};
// Now process and add the rom
string key = "";
@@ -3244,8 +3363,21 @@ namespace SabreTools.Helper
{
string line = sr.ReadLine();
Rom rom = new Rom(line.Split(' ')[0], -1, line.Split(' ')[1], null, null, ItemStatus.None, null,
Path.GetFileNameWithoutExtension(filename), null, null, null, null, null, null, null, null, false, null, null, sysid, null, srcid, null);
Rom rom = new Rom
{
Name = line.Split(' ')[0].Replace("*", String.Empty),
Size = -1,
CRC = line.Split(' ')[1],
ItemStatus = ItemStatus.None,
Machine = new Machine
{
Name = Path.GetFileNameWithoutExtension(filename),
},
SystemID = sysid,
SourceID = srcid,
};
// Now process and add the rom
string key = "";
@@ -3295,8 +3427,21 @@ namespace SabreTools.Helper
{
string line = sr.ReadLine();
Rom rom = new Rom(line.Split(' ')[1].Replace("*", String.Empty), -1, null, null, line.Split(' ')[0], ItemStatus.None, null,
Path.GetFileNameWithoutExtension(filename), null, null, null, null, null, null, null, null, false, null, null, sysid, null, srcid, null);
Rom rom = new Rom
{
Name = line.Split(' ')[1].Replace("*", String.Empty),
Size = -1,
SHA1 = line.Split(' ')[0],
ItemStatus = ItemStatus.None,
Machine = new Machine
{
Name = Path.GetFileNameWithoutExtension(filename),
},
SystemID = sysid,
SourceID = srcid,
};
// Now process and add the rom
string key = "";
@@ -3473,8 +3618,24 @@ namespace SabreTools.Helper
size = 0;
}
Rom rom = new Rom(rominfo[5], size, rominfo[6], null, null, ItemStatus.None, null, rominfo[3], null,
rominfo[4], null, null, rominfo[8], rominfo[1], null, null, false, null, null, sysid, null, srcid, null);
Rom rom = new Rom
{
Name = rominfo[5],
Size = size,
CRC = rominfo[6],
ItemStatus = ItemStatus.None,
Machine = new Machine
{
Name = rominfo[3],
Description = rominfo[4],
CloneOf = rominfo[1],
RomOf = rominfo[8],
},
SystemID = sysid,
SourceID = srcid,
};
// Now process and add the rom
string key = "";
@@ -3517,7 +3678,7 @@ namespace SabreTools.Helper
}
// If we're in cleaning mode, sanitize the game name
item.MachineName = (clean ? Style.CleanGameName(item.MachineName) : item.MachineName);
item.Machine.Name = (clean ? Style.CleanGameName(item.Machine.Name) : item.Machine.Name);
// If we have a Rom or a Disk, clean the hash data
if (item.Type == ItemType.Rom)
@@ -3585,14 +3746,14 @@ namespace SabreTools.Helper
// If we are in single game mode, rename all games
if (single)
{
item.MachineName = "!";
item.Machine.Name = "!";
}
// If we are in NTFS trim mode, trim the game name
if (trim)
{
// Windows max name length is 260
int usableLength = 260 - item.MachineName.Length - root.Length;
int usableLength = 260 - item.Machine.Name.Length - root.Length;
if (item.Name.Length > usableLength)
{
string ext = Path.GetExtension(item.Name);
@@ -3779,22 +3940,22 @@ namespace SabreTools.Helper
DatItem rom = roms[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.MachineName == null)
if (rom.Name == null || rom.Machine.Name == null)
{
logger.Warning("Null rom found!");
continue;
}
List<string> newsplit = rom.MachineName.Split('\\').ToList();
List<string> newsplit = rom.Machine.Name.Split('\\').ToList();
// If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
{
depth = WriteEndGame(sw, outputFormat, rom, splitpath, newsplit, lastgame, depth, out last, logger);
}
// If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
{
depth = WriteStartGame(sw, outputFormat, rom, newsplit, lastgame, depth, last, logger);
}
@@ -3806,7 +3967,7 @@ namespace SabreTools.Helper
&& ((Rom)rom).MD5 == "null"
&& ((Rom)rom).SHA1 == "null")
{
logger.Verbose("Empty folder found: " + rom.MachineName);
logger.Verbose("Empty folder found: " + rom.Machine.Name);
// If we're in a mode that doesn't allow for actual empty folders, add the blank info
if (outputFormat != OutputFormat.CSV
@@ -3825,7 +3986,7 @@ namespace SabreTools.Helper
else
{
splitpath = newsplit;
lastgame = rom.MachineName;
lastgame = rom.Machine.Name;
continue;
}
}
@@ -3835,7 +3996,7 @@ namespace SabreTools.Helper
// Set the new data to compare against
splitpath = newsplit;
lastgame = rom.MachineName;
lastgame = rom.Machine.Name;
}
}
@@ -4069,47 +4230,47 @@ namespace SabreTools.Helper
try
{
// No game should start with a path separator
if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString()))
if (rom.Machine.Name.StartsWith(Path.DirectorySeparatorChar.ToString()))
{
rom.MachineName = rom.MachineName.Substring(1);
rom.Machine.Name = rom.Machine.Name.Substring(1);
}
string state = "";
switch (outputFormat)
{
case OutputFormat.ClrMamePro:
state += "game (\n\tname \"" + rom.MachineName + "\"\n" +
state += "game (\n\tname \"" + rom.Machine.Name + "\"\n" +
(ExcludeOf ? "" :
(String.IsNullOrEmpty(rom.RomOf) ? "" : "\tromof \"" + rom.RomOf + "\"\n") +
(String.IsNullOrEmpty(rom.CloneOf) ? "" : "\tcloneof \"" + rom.CloneOf + "\"\n") +
(String.IsNullOrEmpty(rom.SampleOf) ? "" : "\tsampleof \"" + rom.SampleOf + "\"\n")
(String.IsNullOrEmpty(rom.Machine.RomOf) ? "" : "\tromof \"" + rom.Machine.RomOf + "\"\n") +
(String.IsNullOrEmpty(rom.Machine.CloneOf) ? "" : "\tcloneof \"" + rom.Machine.CloneOf + "\"\n") +
(String.IsNullOrEmpty(rom.Machine.SampleOf) ? "" : "\tsampleof \"" + rom.Machine.SampleOf + "\"\n")
) +
"\tdescription \"" + (String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription) + "\"\n" +
(String.IsNullOrEmpty(rom.Year) ? "" : "\tyear " + rom.Year + "\n") +
(String.IsNullOrEmpty(rom.Manufacturer) ? "" : "\tmanufacturer \"" + rom.Manufacturer + "\"\n");
"\tdescription \"" + (String.IsNullOrEmpty(rom.Machine.Description) ? rom.Machine.Name : rom.Machine.Description) + "\"\n" +
(String.IsNullOrEmpty(rom.Machine.Year) ? "" : "\tyear " + rom.Machine.Year + "\n") +
(String.IsNullOrEmpty(rom.Machine.Manufacturer) ? "" : "\tmanufacturer \"" + rom.Machine.Manufacturer + "\"\n");
break;
case OutputFormat.DOSCenter:
state += "game (\n\tname \"" + rom.MachineName + ".zip\"\n";
state += "game (\n\tname \"" + rom.Machine.Name + ".zip\"\n";
break;
case OutputFormat.Logiqx:
state += "\t<machine name=\"" + HttpUtility.HtmlEncode(rom.MachineName) + "\"" +
(rom.IsBios ? " isbios=\"yes\"" : "") +
state += "\t<machine name=\"" + HttpUtility.HtmlEncode(rom.Machine.Name) + "\"" +
(rom.Machine.IsBios ? " isbios=\"yes\"" : "") +
(ExcludeOf ? "" :
(String.IsNullOrEmpty(rom.CloneOf) || (rom.MachineName.ToLowerInvariant() == rom.CloneOf.ToLowerInvariant())
(String.IsNullOrEmpty(rom.Machine.CloneOf) || (rom.Machine.Name.ToLowerInvariant() == rom.Machine.CloneOf.ToLowerInvariant())
? ""
: " cloneof=\"" + HttpUtility.HtmlEncode(rom.CloneOf) + "\"") +
(String.IsNullOrEmpty(rom.RomOf) || (rom.MachineName.ToLowerInvariant() == rom.RomOf.ToLowerInvariant())
: " cloneof=\"" + HttpUtility.HtmlEncode(rom.Machine.CloneOf) + "\"") +
(String.IsNullOrEmpty(rom.Machine.RomOf) || (rom.Machine.Name.ToLowerInvariant() == rom.Machine.RomOf.ToLowerInvariant())
? ""
: " romof=\"" + HttpUtility.HtmlEncode(rom.RomOf) + "\"") +
(String.IsNullOrEmpty(rom.SampleOf) || (rom.MachineName.ToLowerInvariant() == rom.SampleOf.ToLowerInvariant())
: " romof=\"" + HttpUtility.HtmlEncode(rom.Machine.RomOf) + "\"") +
(String.IsNullOrEmpty(rom.Machine.SampleOf) || (rom.Machine.Name.ToLowerInvariant() == rom.Machine.SampleOf.ToLowerInvariant())
? ""
: " sampleof=\"" + HttpUtility.HtmlEncode(rom.SampleOf) + "\"")
: " sampleof=\"" + HttpUtility.HtmlEncode(rom.Machine.SampleOf) + "\"")
) +
">\n" +
(String.IsNullOrEmpty(rom.Comment) ? "" : "\t\t<comment>" + HttpUtility.HtmlEncode(rom.Comment) + "</comment>\n") +
"\t\t<description>" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) + "</description>\n" +
(String.IsNullOrEmpty(rom.Year) ? "" : "\t\t<year>" + HttpUtility.HtmlEncode(rom.Year) + "</year>\n") +
(String.IsNullOrEmpty(rom.Manufacturer) ? "" : "\t\t<manufacturer>" + HttpUtility.HtmlEncode(rom.Manufacturer) + "</manufacturer>\n");
(String.IsNullOrEmpty(rom.Machine.Comment) ? "" : "\t\t<comment>" + HttpUtility.HtmlEncode(rom.Machine.Comment) + "</comment>\n") +
"\t\t<description>" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.Machine.Description) ? rom.Machine.Name : rom.Machine.Description)) + "</description>\n" +
(String.IsNullOrEmpty(rom.Machine.Year) ? "" : "\t\t<year>" + HttpUtility.HtmlEncode(rom.Machine.Year) + "</year>\n") +
(String.IsNullOrEmpty(rom.Machine.Manufacturer) ? "" : "\t\t<manufacturer>" + HttpUtility.HtmlEncode(rom.Machine.Manufacturer) + "</manufacturer>\n");
break;
case OutputFormat.SabreDat:
for (int i = (last == -1 ? 0 : last); i < newsplit.Count; i++)
@@ -4124,21 +4285,21 @@ namespace SabreTools.Helper
depth = depth - (last == -1 ? 0 : last) + newsplit.Count;
break;
case OutputFormat.SoftwareList:
state += "\t<software name=\"" + HttpUtility.HtmlEncode(rom.MachineName) + "\""
state += "\t<software name=\"" + HttpUtility.HtmlEncode(rom.Machine.Name) + "\""
+ (rom.Supported != null ? " supported=\"" + (rom.Supported == true ? "yes" : "no") + "\"" : "") +
(ExcludeOf ? "" :
(String.IsNullOrEmpty(rom.CloneOf) || (rom.MachineName.ToLowerInvariant() == rom.CloneOf.ToLowerInvariant())
(String.IsNullOrEmpty(rom.Machine.CloneOf) || (rom.Machine.Name.ToLowerInvariant() == rom.Machine.CloneOf.ToLowerInvariant())
? ""
: " cloneof=\"" + HttpUtility.HtmlEncode(rom.CloneOf) + "\"") +
(String.IsNullOrEmpty(rom.RomOf) || (rom.MachineName.ToLowerInvariant() == rom.RomOf.ToLowerInvariant())
: " cloneof=\"" + HttpUtility.HtmlEncode(rom.Machine.CloneOf) + "\"") +
(String.IsNullOrEmpty(rom.Machine.RomOf) || (rom.Machine.Name.ToLowerInvariant() == rom.Machine.RomOf.ToLowerInvariant())
? ""
: " romof=\"" + HttpUtility.HtmlEncode(rom.RomOf) + "\"") +
(String.IsNullOrEmpty(rom.SampleOf) || (rom.MachineName.ToLowerInvariant() == rom.SampleOf.ToLowerInvariant())
: " romof=\"" + HttpUtility.HtmlEncode(rom.Machine.RomOf) + "\"") +
(String.IsNullOrEmpty(rom.Machine.SampleOf) || (rom.Machine.Name.ToLowerInvariant() == rom.Machine.SampleOf.ToLowerInvariant())
? ""
: " sampleof=\"" + HttpUtility.HtmlEncode(rom.SampleOf) + "\"")
: " sampleof=\"" + HttpUtility.HtmlEncode(rom.Machine.SampleOf) + "\"")
) + ">\n"
+ "\t\t<description>" + HttpUtility.HtmlEncode(rom.MachineDescription) + "</description>\n"
+ (rom.Year != null ? "\t\t<year>" + HttpUtility.HtmlEncode(rom.Year) + "</year>\n" : "")
+ "\t\t<description>" + HttpUtility.HtmlEncode(rom.Machine.Description) + "</description>\n"
+ (rom.Machine.Year != null ? "\t\t<year>" + HttpUtility.HtmlEncode(rom.Machine.Year) + "</year>\n" : "")
+ (rom.Publisher != null ? "\t\t<publisher>" + HttpUtility.HtmlEncode(rom.Publisher) + "</publisher>\n" : "");
foreach (Tuple<string, string> kvp in rom.Infos)
@@ -4185,7 +4346,7 @@ namespace SabreTools.Helper
{
case OutputFormat.ClrMamePro:
case OutputFormat.DOSCenter:
state += (String.IsNullOrEmpty(rom.SampleOf) ? "" : "\tsampleof \"" + rom.SampleOf + "\"\n") + ")\n";
state += (String.IsNullOrEmpty(rom.Machine.SampleOf) ? "" : "\tsampleof \"" + rom.Machine.SampleOf + "\"\n") + ")\n";
break;
case OutputFormat.Logiqx:
state += "\t</machine>\n";
@@ -4329,14 +4490,14 @@ namespace SabreTools.Helper
{
// Check for special strings in prefix and postfix
pre = pre
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%crc%", ((Rom)rom).CRC)
.Replace("%md5%", ((Rom)rom).MD5)
.Replace("%sha1%", ((Rom)rom).SHA1)
.Replace("%size%", ((Rom)rom).Size.ToString());
post = post
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%crc%", ((Rom)rom).CRC)
.Replace("%md5%", ((Rom)rom).MD5)
@@ -4347,12 +4508,12 @@ namespace SabreTools.Helper
{
// Check for special strings in prefix and postfix
pre = pre
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%md5%", ((Disk)rom).MD5)
.Replace("%sha1%", ((Disk)rom).SHA1);
post = post
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%md5%", ((Disk)rom).MD5)
.Replace("%sha1%", ((Disk)rom).SHA1);
@@ -4363,8 +4524,8 @@ namespace SabreTools.Helper
string inline = "\"" + FileName + "\""
+ ",\"" + Name + "\""
+ ",\"" + Description + "\""
+ ",\"" + rom.MachineName + "\""
+ ",\"" + rom.MachineDescription + "\""
+ ",\"" + rom.Machine.Name + "\""
+ ",\"" + rom.Machine.Description + "\""
+ "," + "\"rom\""
+ ",\"" + rom.Name + "\""
+ "," + "\"\""
@@ -4380,8 +4541,8 @@ namespace SabreTools.Helper
string inline = "\"" + FileName + "\""
+ ",\"" + Name + "\""
+ ",\"" + Description + "\""
+ ",\"" + rom.MachineName + "\""
+ ",\"" + rom.MachineDescription + "\""
+ ",\"" + rom.Machine.Name + "\""
+ ",\"" + rom.Machine.Description + "\""
+ "," + "\"disk\""
+ "," + "\"\""
+ ",\"" + rom.Name + "\""
@@ -4474,14 +4635,14 @@ namespace SabreTools.Helper
{
// Check for special strings in prefix and postfix
pre = pre
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%crc%", ((Rom)rom).CRC)
.Replace("%md5%", ((Rom)rom).MD5)
.Replace("%sha1%", ((Rom)rom).SHA1)
.Replace("%size%", ((Rom)rom).Size.ToString());
post = post
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%crc%", ((Rom)rom).CRC)
.Replace("%md5%", ((Rom)rom).MD5)
@@ -4492,12 +4653,12 @@ namespace SabreTools.Helper
{
// Check for special strings in prefix and postfix
pre = pre
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%md5%", ((Disk)rom).MD5)
.Replace("%sha1%", ((Disk)rom).SHA1);
post = post
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%md5%", ((Disk)rom).MD5)
.Replace("%sha1%", ((Disk)rom).SHA1);
@@ -4535,7 +4696,7 @@ namespace SabreTools.Helper
}
// Otherwise, use any flags
name = (UseGame ? rom.MachineName : rom.Name);
name = (UseGame ? rom.Machine.Name : rom.Name);
if (RepExt != "" || RemExt)
{
if (RemExt)
@@ -4553,13 +4714,13 @@ namespace SabreTools.Helper
}
if (!UseGame && GameName)
{
name = Path.Combine(rom.MachineName, name);
name = Path.Combine(rom.Machine.Name, name);
}
if (UseGame && rom.MachineName != lastgame)
if (UseGame && rom.Machine.Name != lastgame)
{
state += pre + name + post + "\n";
lastgame = rom.MachineName;
lastgame = rom.Machine.Name;
}
else if (!UseGame)
{
@@ -4617,46 +4778,46 @@ namespace SabreTools.Helper
case OutputFormat.RedumpMD5:
if (rom.Type == ItemType.Rom)
{
state += ((Rom)rom).MD5 + " *" + (GameName ? rom.MachineName + Path.DirectorySeparatorChar : "") + rom.Name + "\n";
state += ((Rom)rom).MD5 + " *" + (GameName ? rom.Machine.Name + Path.DirectorySeparatorChar : "") + rom.Name + "\n";
}
else if (rom.Type == ItemType.Disk)
{
state += ((Disk)rom).MD5 + " *" + (GameName ? rom.MachineName + Path.DirectorySeparatorChar : "") + rom.Name + "\n";
state += ((Disk)rom).MD5 + " *" + (GameName ? rom.Machine.Name + Path.DirectorySeparatorChar : "") + rom.Name + "\n";
}
break;
case OutputFormat.RedumpSFV:
if (rom.Type == ItemType.Rom)
{
state += (GameName ? rom.MachineName + Path.DirectorySeparatorChar : "") + rom.Name + " " + ((Rom)rom).CRC + "\n";
state += (GameName ? rom.Machine.Name + Path.DirectorySeparatorChar : "") + rom.Name + " " + ((Rom)rom).CRC + "\n";
}
break;
case OutputFormat.RedumpSHA1:
if (rom.Type == ItemType.Rom)
{
state += ((Rom)rom).SHA1 + " *" + (GameName ? rom.MachineName + Path.DirectorySeparatorChar : "") + rom.Name + "\n";
state += ((Rom)rom).SHA1 + " *" + (GameName ? rom.Machine.Name + Path.DirectorySeparatorChar : "") + rom.Name + "\n";
}
else if (rom.Type == ItemType.Disk)
{
state += ((Disk)rom).SHA1 + " *" + (GameName ? rom.MachineName + Path.DirectorySeparatorChar : "") + rom.Name + "\n";
state += ((Disk)rom).SHA1 + " *" + (GameName ? rom.Machine.Name + Path.DirectorySeparatorChar : "") + rom.Name + "\n";
}
break;
case OutputFormat.RomCenter:
if (rom.Type == ItemType.Rom)
{
state += "¬" + (String.IsNullOrEmpty(rom.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.CloneOf)) +
"¬" + (String.IsNullOrEmpty(rom.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.CloneOf)) +
"¬" + HttpUtility.HtmlEncode(rom.MachineName) +
"¬" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) +
state += "¬" + (String.IsNullOrEmpty(rom.Machine.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.Machine.CloneOf)) +
"¬" + (String.IsNullOrEmpty(rom.Machine.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.Machine.CloneOf)) +
"¬" + HttpUtility.HtmlEncode(rom.Machine.Name) +
"¬" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.Machine.Description) ? rom.Machine.Name : rom.Machine.Description)) +
"¬" + HttpUtility.HtmlEncode(rom.Name) +
"¬" + ((Rom)rom).CRC.ToLowerInvariant() +
"¬" + (((Rom)rom).Size != -1 ? ((Rom)rom).Size.ToString() : "") + "¬¬¬\n";
}
else if (rom.Type == ItemType.Disk)
{
state += "¬" + (String.IsNullOrEmpty(rom.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.CloneOf)) +
"¬" + (String.IsNullOrEmpty(rom.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.CloneOf)) +
"¬" + HttpUtility.HtmlEncode(rom.MachineName) +
"¬" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.MachineDescription) ? rom.MachineName : rom.MachineDescription)) +
state += "¬" + (String.IsNullOrEmpty(rom.Machine.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.Machine.CloneOf)) +
"¬" + (String.IsNullOrEmpty(rom.Machine.CloneOf) ? "" : HttpUtility.HtmlEncode(rom.Machine.CloneOf)) +
"¬" + HttpUtility.HtmlEncode(rom.Machine.Name) +
"¬" + HttpUtility.HtmlEncode((String.IsNullOrEmpty(rom.Machine.Description) ? rom.Machine.Name : rom.Machine.Description)) +
"¬" + HttpUtility.HtmlEncode(rom.Name) +
"¬¬¬¬¬\n";
}
@@ -4810,14 +4971,14 @@ namespace SabreTools.Helper
{
// Check for special strings in prefix and postfix
pre = pre
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%crc%", ((Rom)rom).CRC)
.Replace("%md5%", ((Rom)rom).MD5)
.Replace("%sha1%", ((Rom)rom).SHA1)
.Replace("%size%", ((Rom)rom).Size.ToString());
post = post
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%crc%", ((Rom)rom).CRC)
.Replace("%md5%", ((Rom)rom).MD5)
@@ -4828,12 +4989,12 @@ namespace SabreTools.Helper
{
// Check for special strings in prefix and postfix
pre = pre
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%md5%", ((Disk)rom).MD5)
.Replace("%sha1%", ((Disk)rom).SHA1);
post = post
.Replace("%game%", rom.MachineName)
.Replace("%game%", rom.Machine.Name)
.Replace("%name%", rom.Name)
.Replace("%md5%", ((Disk)rom).MD5)
.Replace("%sha1%", ((Disk)rom).SHA1);
@@ -4844,8 +5005,8 @@ namespace SabreTools.Helper
string inline = "\"" + FileName + "\""
+ "\t\"" + Name + "\""
+ "\t\"" + Description + "\""
+ "\t\"" + rom.MachineName + "\""
+ "\t\"" + rom.MachineDescription + "\""
+ "\t\"" + rom.Machine.Name + "\""
+ "\t\"" + rom.Machine.Description + "\""
+ "\t" + "\"rom\""
+ "\t\"" + rom.Name + "\""
+ "\t" + "\"\""
@@ -4861,8 +5022,8 @@ namespace SabreTools.Helper
string inline = "\"" + FileName + "\""
+ "\t\"" + Name + "\""
+ "\t\"" + Description + "\""
+ "\t\"" + rom.MachineName + "\""
+ "\t\"" + rom.MachineDescription + "\""
+ "\t\"" + rom.Machine.Name + "\""
+ "\t\"" + rom.Machine.Description + "\""
+ "\t" + "\"disk\""
+ "\t" + "\"\""
+ "\t\"" + rom.Name + "\""
@@ -5382,8 +5543,8 @@ namespace SabreTools.Helper
// Update rom information
datItem.Name = romname;
datItem.MachineName = gamename;
datItem.MachineDescription = gamename;
datItem.Machine.Name = gamename;
datItem.Machine.Description = gamename;
// Add the file information to the DAT
lock (Files)
@@ -5647,9 +5808,9 @@ namespace SabreTools.Helper
: rom.SystemID.ToString().PadLeft(10, '0')
+ "-"
+ rom.SourceID.ToString().PadLeft(10, '0') + "-")
+ (String.IsNullOrEmpty(rom.MachineName)
+ (String.IsNullOrEmpty(rom.Machine.Name)
? "Default"
: rom.MachineName.ToLowerInvariant());
: rom.Machine.Name.ToLowerInvariant());
newkey = HttpUtility.HtmlEncode(newkey);
if (sortable.ContainsKey(newkey))
{

View File

@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
using NaturalSort;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
[Serializable]
public abstract class DatItem : IEquatable<DatItem>, IComparable<DatItem>
@@ -15,18 +18,7 @@ namespace SabreTools.Helper
protected DupeType _dupeType;
// Machine information
protected string _machineName;
protected string _comment;
protected string _machineDescription;
protected string _year;
protected string _manufacturer;
protected string _romOf;
protected string _cloneOf;
protected string _sampleOf;
protected string _sourceFile;
protected bool _isBios;
protected string _board;
protected string _rebuildTo;
protected Machine _machine;
// Software list information
protected bool? _supported;
@@ -66,65 +58,10 @@ namespace SabreTools.Helper
}
// Machine information
public string MachineName
public Machine Machine
{
get { return _machineName; }
set { _machineName = value; }
}
public string Comment
{
get { return _comment; }
set { _comment = value; }
}
public string MachineDescription
{
get { return _machineDescription; }
set { _machineDescription = value; }
}
public string Year
{
get { return _year; }
set { _year = value; }
}
public string Manufacturer
{
get { return _manufacturer; }
set { _manufacturer = value; }
}
public string RomOf
{
get { return _romOf; }
set { _romOf = value; }
}
public string CloneOf
{
get { return _cloneOf; }
set { _cloneOf = value; }
}
public string SampleOf
{
get { return _sampleOf; }
set { _sampleOf = value; }
}
public string SourceFile
{
get { return _sourceFile; }
set { _sourceFile = value; }
}
public bool IsBios
{
get { return _isBios; }
set { _isBios = value; }
}
public string Board
{
get { return _board; }
set { _board = value; }
}
public string RebuildTo
{
get { return _rebuildTo; }
set { _rebuildTo = value; }
get { return _machine; }
set { _machine = value; }
}
// Software list information
@@ -258,7 +195,7 @@ namespace SabreTools.Helper
// If the duplicate is external already or should be, set it
if ((lastItem.Dupe & DupeType.External) != 0 || lastItem.SystemID != this.SystemID || lastItem.SourceID != this.SourceID)
{
if (lastItem.MachineName == this.MachineName && lastItem.Name == this.Name)
if (lastItem.Machine.Name == this.Machine.Name && lastItem.Name == this.Name)
{
output = DupeType.External | DupeType.All;
}
@@ -271,7 +208,7 @@ namespace SabreTools.Helper
// Otherwise, it's considered an internal dupe
else
{
if (lastItem.MachineName == this.MachineName && lastItem.Name == this.Name)
if (lastItem.Machine.Name == this.Machine.Name && lastItem.Name == this.Name)
{
output = DupeType.Internal | DupeType.All;
}
@@ -531,28 +468,28 @@ namespace SabreTools.Helper
{
if (gamename.StartsWith("*") && gamename.EndsWith("*"))
{
if (!MachineName.ToLowerInvariant().Contains(gamename.ToLowerInvariant().Replace("*", "")))
if (!Machine.Name.ToLowerInvariant().Contains(gamename.ToLowerInvariant().Replace("*", "")))
{
return false;
}
}
else if (gamename.StartsWith("*"))
{
if (!MachineName.EndsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase))
if (!Machine.Name.EndsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
}
else if (gamename.EndsWith("*"))
{
if (!MachineName.StartsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase))
if (!Machine.Name.StartsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
}
else
{
if (!String.Equals(MachineName, gamename, StringComparison.InvariantCultureIgnoreCase))
if (!String.Equals(Machine.Name, gamename, StringComparison.InvariantCultureIgnoreCase))
{
return false;
}
@@ -860,7 +797,7 @@ namespace SabreTools.Helper
{
saveditem.SystemID = file.SystemID;
saveditem.System = file.System;
saveditem.MachineName = file.MachineName;
saveditem.Machine.Name = file.Machine.Name;
saveditem.Name = file.Name;
}
@@ -869,7 +806,7 @@ namespace SabreTools.Helper
{
saveditem.SourceID = file.SourceID;
saveditem.Source = file.Source;
saveditem.MachineName = file.MachineName;
saveditem.Machine.Name = file.Machine.Name;
saveditem.Name = file.Name;
}
@@ -916,7 +853,7 @@ namespace SabreTools.Helper
{
if (x.SourceID == y.SourceID)
{
if (x.MachineName == y.MachineName)
if (x.Machine.Name == y.Machine.Name)
{
if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type == ItemType.Rom || y.Type == ItemType.Disk))
{
@@ -943,11 +880,11 @@ namespace SabreTools.Helper
return nc.Compare(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name));
}
}
return nc.Compare(x.MachineName, y.MachineName);
return nc.Compare(x.Machine.Name, y.Machine.Name);
}
return (norename ? nc.Compare(x.MachineName, y.MachineName) : x.SourceID - y.SourceID);
return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.SourceID - y.SourceID);
}
return (norename ? nc.Compare(x.MachineName, y.MachineName) : x.SystemID - y.SystemID);
return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.SystemID - y.SystemID);
});
return true;
}

View File

@@ -2,7 +2,7 @@
using System.Collections.Specialized;
using System.Linq;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
public class DatItemKV : IEquatable<DatItemKV>
{

View File

@@ -1,6 +1,7 @@
using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
[Serializable]
public class Disk : DatItem
@@ -65,56 +66,6 @@ namespace SabreTools.Helper
_itemStatus = itemStatus;
}
/// <summary>
/// Create a new Disk object with the included information
/// </summary>
/// <param name="name">Name of the item, including extension</param>
/// <param name="md5">String representation of the MD5</param>
/// <param name="sha1">String representation of the SHA-1</param>
/// <param name="itemStatus">Status of the current item</param>
/// <param name="machineName">Name for the machine/game</param>
/// <param name="comment">Comment for the machine/game</param>
/// <param name="machineDescription">Description for the machine/game</param>
/// <param name="year">Year for the machine/game</param>
/// <param name="manufacturer">Manufacturer name for the machine/game</param>
/// <param name="romOf">Set that this machine/game is a rom of</param>
/// <param name="cloneOf">Set that this machine/game is a clone of</param>
/// <param name="sampleOf">Set that this machine/game is a sample of</param>
/// <param name="sourceFile">Source file for the machine/game</param>
/// <param name="isBios">True if this game is a BIOS, false otherwise</param>
/// <param name="board">Name of the board for this machine/game</param>
/// <param name="rebuildTo">Name of the game to rebuild to</param>
/// <param name="systemId">System ID to be associated with</param>
/// <param name="systemName">System Name to be associated with</param>
/// <param name="sourceId">Source ID to be associated with</param>
/// <param name="sourceName">Source Name to be associated with</param>
public Disk(string name, string md5, string sha1, ItemStatus itemStatus, string machineName, string comment,
string machineDescription, string year, string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile,
bool isBios, string board, string rebuildTo, int systemId, string systemName, int sourceId, string sourceName)
{
_name = name;
_itemType = ItemType.Disk;
_md5 = md5?.ToLowerInvariant();
_sha1 = sha1?.ToLowerInvariant();
_itemStatus = itemStatus;
_machineName = machineName;
_comment = comment;
_machineDescription = machineDescription;
_year = year;
_manufacturer = manufacturer;
_romOf = romOf;
_cloneOf = cloneOf;
_sampleOf = sampleOf;
_sourceFile = sourceFile;
_isBios = isBios;
_board = board;
_rebuildTo = rebuildTo;
_systemId = systemId;
_systemName = systemName;
_sourceId = sourceId;
_sourceName = sourceName;
}
#endregion
#region Comparision Methods

View File

@@ -0,0 +1,113 @@
namespace SabreTools.Helper.Dats
{
public class Machine
{
#region Protected instance variables
// Machine information
protected string _name;
protected string _comment;
protected string _description;
protected string _year;
protected string _manufacturer;
protected string _romOf;
protected string _cloneOf;
protected string _sampleOf;
protected string _sourceFile;
protected bool _isBios;
protected string _board;
protected string _rebuildTo;
#endregion
#region Publicly facing variables
// Machine information
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Comment
{
get { return _comment; }
set { _comment = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
public string Year
{
get { return _year; }
set { _year = value; }
}
public string Manufacturer
{
get { return _manufacturer; }
set { _manufacturer = value; }
}
public string RomOf
{
get { return _romOf; }
set { _romOf = value; }
}
public string CloneOf
{
get { return _cloneOf; }
set { _cloneOf = value; }
}
public string SampleOf
{
get { return _sampleOf; }
set { _sampleOf = value; }
}
public string SourceFile
{
get { return _sourceFile; }
set { _sourceFile = value; }
}
public bool IsBios
{
get { return _isBios; }
set { _isBios = value; }
}
public string Board
{
get { return _board; }
set { _board = value; }
}
public string RebuildTo
{
get { return _rebuildTo; }
set { _rebuildTo = value; }
}
#endregion
#region Constructors
/// <summary>
/// Create a default, empty Machine object
/// </summary>
public Machine()
{
_name = "";
_description = "";
}
/// <summary>
/// Create a new Machine object with the included information
/// </summary>
/// <param name="name">Name of the machine</param>
/// <param name="description">Description of the machine</param>
public Machine(string name, string description)
{
_name = name;
_description = description;
}
#endregion
}
}

View File

@@ -1,6 +1,7 @@
using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
[Serializable]
public class Release : DatItem
@@ -72,58 +73,6 @@ namespace SabreTools.Helper
_default = @default;
}
/// <summary>
/// Create a new Release object with the included information
/// </summary>
/// <param name="name">Name of the item, including extension</param>
/// <param name="region">Region of the item</param>
/// <param name="language">Language of the item</param>
/// <param name="date">String representation of the Date</param>
/// <param name="default">True if this is the default BIOS, false if it is not, null for undefined</param>
/// <param name="machineName">Name for the machine/game</param>
/// <param name="comment">Comment for the machine/game</param>
/// <param name="machineDescription">Description for the machine/game</param>
/// <param name="year">Year for the machine/game</param>
/// <param name="manufacturer">Manufacturer name for the machine/game</param>
/// <param name="romOf">Set that this machine/game is a rom of</param>
/// <param name="cloneOf">Set that this machine/game is a clone of</param>
/// <param name="sampleOf">Set that this machine/game is a sample of</param>
/// <param name="sourceFile">Source file for the machine/game</param>
/// <param name="isBios">True if this game is a BIOS, false otherwise</param>
/// <param name="board">Name of the board for this machine/game</param>
/// <param name="rebuildTo">Name of the game to rebuild to</param>
/// <param name="systemId">System ID to be associated with</param>
/// <param name="systemName">System Name to be associated with</param>
/// <param name="sourceId">Source ID to be associated with</param>
/// <param name="sourceName">Source Name to be associated with</param>
public Release(string name, string region, string language, string date, bool? @default, string machineName, string comment,
string machineDescription, string year, string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile,
bool isBios, string board, string rebuildTo, int systemId, string systemName, int sourceId, string sourceName)
{
_name = name;
_itemType = ItemType.Release;
_region = region;
_language = language;
_date = date;
_default = @default;
_machineName = machineName;
_comment = comment;
_machineDescription = machineDescription;
_year = year;
_manufacturer = manufacturer;
_romOf = romOf;
_cloneOf = cloneOf;
_sampleOf = sampleOf;
_sourceFile = sourceFile;
_isBios = isBios;
_board = board;
_rebuildTo = rebuildTo;
_systemId = systemId;
_systemName = systemName;
_sourceId = sourceId;
_sourceName = sourceName;
}
#endregion
#region Comparision Methods

View File

@@ -1,6 +1,7 @@
using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
[Serializable]
public class Rom : Disk
@@ -55,8 +56,13 @@ namespace SabreTools.Helper
/// <param name="name"></param>
/// <param name="machineName"></param>
public Rom(string name, string machineName) :
this(name, -1, "null", "null", "null", ItemStatus.None, null, machineName, null, machineName, null, null, null, null, null, null, false, null, null, -1, null, -1, null)
this(name, -1, "null", "null", "null", ItemStatus.None, null)
{
_machine = new Machine
{
Name = machineName,
Description = machineName,
};
}
/// <summary>
@@ -81,62 +87,6 @@ namespace SabreTools.Helper
_date = date;
}
/// <summary>
/// Create a new Rom object with the included information
/// </summary>
/// <param name="name">Name of the item, including extension</param>
/// <param name="size">Long size of the item</param>
/// <param name="crc">String representation of the CRC</param>
/// <param name="md5">String representation of the MD5</param>
/// <param name="sha1">String representation of the SHA-1</param>
/// <param name="itemStatus">Status of the current item</param>
/// <param name="date">String representation of the Date</param>
/// <param name="machineName">Name for the machine/game</param>
/// <param name="comment">Comment for the machine/game</param>
/// <param name="machineDescription">Description for the machine/game</param>
/// <param name="year">Year for the machine/game</param>
/// <param name="manufacturer">Manufacturer name for the machine/game</param>
/// <param name="romOf">Set that this machine/game is a rom of</param>
/// <param name="cloneOf">Set that this machine/game is a clone of</param>
/// <param name="sampleOf">Set that this machine/game is a sample of</param>
/// <param name="sourceFile">Source file for the machine/game</param>
/// <param name="isBios">True if this game is a BIOS, false otherwise</param>
/// <param name="board">Name of the board for this machine/game</param>
/// <param name="rebuildTo">Name of the game to rebuild to</param>
/// <param name="systemId">System ID to be associated with</param>
/// <param name="systemName">System Name to be associated with</param>
/// <param name="sourceId">Source ID to be associated with</param>
/// <param name="sourceName">Source Name to be associated with</param>
public Rom(string name, long size, string crc, string md5, string sha1, ItemStatus itemStatus, string date, string machineName,
string comment, string machineDescription, string year, string manufacturer, string romOf, string cloneOf, string sampleOf,
string sourceFile, bool isBios, string board, string rebuildTo, int systemId, string systemName, int sourceId, string sourceName)
{
_name = name;
_itemType = ItemType.Rom;
_size = size;
_crc = crc?.ToLowerInvariant();
_md5 = md5?.ToLowerInvariant();
_sha1 = sha1?.ToLowerInvariant();
_itemStatus = itemStatus;
_date = date;
_machineName = machineName;
_comment = comment;
_machineDescription = machineDescription;
_year = year;
_manufacturer = manufacturer;
_romOf = romOf;
_cloneOf = cloneOf;
_sampleOf = sampleOf;
_sourceFile = sourceFile;
_isBios = isBios;
_board = board;
_rebuildTo = rebuildTo;
_systemId = systemId;
_systemName = systemName;
_sourceId = sourceId;
_sourceName = sourceName;
}
#endregion
#region Comparision Methods

View File

@@ -1,6 +1,7 @@
using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper
namespace SabreTools.Helper.Dats
{
[Serializable]
public class Sample : DatItem
@@ -26,50 +27,6 @@ namespace SabreTools.Helper
_itemType = ItemType.Sample;
}
/// <summary>
/// Create a new Sample object with the included information
/// </summary>
/// <param name="name">Name of the item, including extension</param>
/// <param name="machineName">Name for the machine/game</param>
/// <param name="comment">Comment for the machine/game</param>
/// <param name="machineDescription">Description for the machine/game</param>
/// <param name="year">Year for the machine/game</param>
/// <param name="manufacturer">Manufacturer name for the machine/game</param>
/// <param name="romOf">Set that this machine/game is a rom of</param>
/// <param name="cloneOf">Set that this machine/game is a clone of</param>
/// <param name="sampleOf">Set that this machine/game is a sample of</param>
/// <param name="sourceFile">Source file for the machine/game</param>
/// <param name="isBios">True if this game is a BIOS, false otherwise</param>
/// <param name="board">Name of the board for this machine/game</param>
/// <param name="rebuildTo">Name of the game to rebuild to</param>
/// <param name="systemId">System ID to be associated with</param>
/// <param name="systemName">System Name to be associated with</param>
/// <param name="sourceId">Source ID to be associated with</param>
/// <param name="sourceName">Source Name to be associated with</param>
public Sample(string name, string machineName, string comment, string machineDescription, string year,
string manufacturer, string romOf, string cloneOf, string sampleOf, string sourceFile, bool isBios, string board, string rebuildTo,
int systemId, string systemName, int sourceId, string sourceName)
{
_name = name;
_itemType = ItemType.Sample;
_machineName = machineName;
_comment = comment;
_machineDescription = machineDescription;
_year = year;
_manufacturer = manufacturer;
_romOf = romOf;
_cloneOf = cloneOf;
_sampleOf = sampleOf;
_sourceFile = sourceFile;
_isBios = isBios;
_board = board;
_rebuildTo = rebuildTo;
_systemId = systemId;
_systemName = systemName;
_sourceId = sourceId;
_sourceName = sourceName;
}
#endregion
#region Comparision Methods

View File

@@ -13,6 +13,17 @@ namespace SabreTools.Helper
/// </remarks>
public class Logger
{
/// <summary>
/// Severity of the logging statement
/// </summary>
private enum LogLevel
{
VERBOSE = 0,
USER,
WARNING,
ERROR,
}
// Private instance variables
private bool _tofile;
private bool _warnings;
@@ -24,6 +35,19 @@ namespace SabreTools.Helper
// Private required variables
private string _basepath = "logs" + Path.DirectorySeparatorChar;
/// <summary>
/// Initialize a console-only logger object
/// </summary>
public Logger()
{
_tofile = false;
_warnings = false;
_errors = false;
_filename = null;
Start();
}
/// <summary>
/// Initialize a Logger object with the given information
/// </summary>
@@ -33,6 +57,7 @@ namespace SabreTools.Helper
{
_tofile = tofile;
_warnings = false;
_errors = false;
_filename = Path.GetFileNameWithoutExtension(filename) + " (" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ")" + Path.GetExtension(filename);
if (!Directory.Exists(_basepath))
@@ -76,6 +101,8 @@ namespace SabreTools.Helper
/// <param name="suppress">True if all ending output is to be suppressed, false otherwise (default)</param>
/// <returns>True if the logging was ended correctly, false otherwise</returns>
public bool Close(bool suppress = false)
{
if (!suppress)
{
if (_warnings)
{
@@ -86,8 +113,6 @@ namespace SabreTools.Helper
Console.WriteLine("There were errors in the last run! Check the log for more details");
}
if (!suppress)
{
TimeSpan span = DateTime.Now.Subtract(_start);
string total = span.ToString(@"hh\:mm\:ss\.fffff");
if (!_tofile)

View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Xml;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
namespace SabreTools.Helper
{
@@ -37,6 +39,9 @@ namespace SabreTools.Helper
#region Constructors
/// <summary>
/// Create an empty Skipper object
/// </summary>
public Skipper()
{
Name = "";
@@ -46,12 +51,16 @@ namespace SabreTools.Helper
SourceFile = "";
}
/// <summary>
/// Create a Skipper object parsed from an input file
/// </summary>
/// <param name="filename">Name of the file to parse</param>
public Skipper(string filename)
{
Rules = new List<SkipperRule>();
SourceFile = Path.GetFileNameWithoutExtension(filename);
Logger logger = new Logger(false, "");
Logger logger = new Logger();
XmlReader xtr = FileTools.GetXmlTextReader(filename, logger);
if (xtr == null)

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.Helper.Data;
namespace SabreTools.Helper
{

View File

@@ -108,11 +108,12 @@
<Compile Include="Objects\Dat\DatItem.cs" />
<Compile Include="Objects\Dat\DatItemKV.cs" />
<Compile Include="Objects\Dat\Disk.cs" />
<Compile Include="Objects\Dat\Machine.cs" />
<Compile Include="Objects\Dat\Release.cs" />
<Compile Include="Objects\Dat\Sample.cs" />
<Compile Include="Objects\Dat\Rom.cs" />
<Compile Include="Objects\Archive\ZipFileEntry.cs" />
<Compile Include="Objects\Archive\ZipFile.cs" />
<Compile Include="External\SupportedFiles\ZipFileEntry.cs" />
<Compile Include="External\SupportedFiles\ZipFile.cs" />
<Compile Include="Objects\Skippers\Skipper.cs" />
<Compile Include="Objects\Skippers\SkipperRule.cs" />
<Compile Include="Resources\Resources.de-DE.Designer.cs">

View File

@@ -1,14 +1,20 @@
using SharpCompress.Archive;
using SharpCompress.Archive.SevenZip;
using SharpCompress.Common;
using SharpCompress.Reader;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace SabreTools.Helper
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
using Ionic.Zlib;
using ROMVault2.SupportedFiles.Zip;
using SharpCompress.Archive;
using SharpCompress.Archive.SevenZip;
using SharpCompress.Common;
using SharpCompress.Reader;
namespace SabreTools.Helper.Tools
{
public static class ArchiveTools
{
@@ -28,52 +34,16 @@ namespace SabreTools.Helper
public static bool CopyFileBetweenArchives(string inputArchive, string outDir, string sourceEntryName, Rom destEntry, Logger logger)
{
string temp = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
string realName = ExtractSingleItemFromArchive(inputArchive, sourceEntryName, temp, logger);
string realName = ExtractItem(inputArchive, sourceEntryName, temp, logger);
bool success = WriteToArchive(realName, outDir, destEntry, logger);
Directory.Delete(temp, true);
return success;
}
/// <summary>
/// Convert a compressed DeflateStream to a compressed GZipStream
/// </summary>
/// <param name="input">DeflateStream to convert</param>
/// <returns>Converted GZipStream</returns>
public static Stream DeflateStreamToGZipStream(Stream input)
{
DeflateStream ds = new DeflateStream(input, CompressionMode.Decompress);
GZipStream gz = new GZipStream(ds, CompressionMode.Compress);
return gz;
}
/// <summary>
/// Convert a compressed GZipStream to a compressed DeflateStream
/// </summary>
/// <param name="input">GZipStream to convert</param>
/// <returns>Converted DeflateStream</returns>
public static Stream GZipStreamToDeflateStream(Stream input)
{
GZipStream gz = new GZipStream(input, CompressionMode.Decompress);
DeflateStream ds = new DeflateStream(gz, CompressionMode.Compress);
return ds;
}
#endregion
#region Extraction
/// <summary>
/// Attempt to extract a file as an archive
/// </summary>
/// <param name="input">Name of the file to be extracted</param>
/// <param name="tempDir">Temporary directory for archive extraction</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns>True if the extraction was a success, false otherwise</returns>
public static bool ExtractArchive(string input, string tempDir, Logger logger)
{
return ExtractArchive(input, tempDir, GetArchiveScanLevelFromNumbers(0, 2, 2, 0), logger);
}
/// <summary>
/// Attempt to extract a file as an archive
/// </summary>
@@ -230,10 +200,10 @@ namespace SabreTools.Helper
/// <param name="tempDir">Temporary directory for archive extraction</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns>Name of the extracted file, null on error</returns>
public static string ExtractSingleItemFromArchive(string input, string entryName, string tempDir, Logger logger)
public static string ExtractItem(string input, string entryName, string tempDir, Logger logger)
{
string realEntry = "";
Stream ms = ExtractSingleStreamFromArchive(input, entryName, out realEntry, logger);
Stream ms = ExtractStream(input, entryName, out realEntry, logger);
realEntry = Path.GetFullPath(Path.Combine(tempDir, realEntry));
if (!Directory.Exists(Path.GetDirectoryName(realEntry)))
@@ -259,13 +229,13 @@ namespace SabreTools.Helper
}
/// <summary>
/// Attempt to extract a file from an archive
/// Attempt to extract a stream from an archive
/// </summary>
/// <param name="input">Name of the archive to be extracted</param>
/// <param name="entryName">Name of the entry to be extracted</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns>Name of the extracted file, null on error</returns>
public static Stream ExtractSingleStreamFromArchive(string input, string entryName, out string realEntry, Logger logger)
public static Stream ExtractStream(string input, string entryName, out string realEntry, Logger logger)
{
// Set the real entry name
realEntry = "";
@@ -436,9 +406,13 @@ namespace SabreTools.Helper
{
Type = ItemType.Rom,
Name = newname,
MachineName = gamename,
Size = newsize,
CRC = newcrc,
Machine = new Machine
{
Name = gamename,
},
});
}
}
@@ -457,9 +431,13 @@ namespace SabreTools.Helper
{
Type = ItemType.Rom,
Name = reader.Entry.Key,
MachineName = gamename,
Size = (size == 0 ? reader.Entry.Size : size),
CRC = (crc == "" ? reader.Entry.Crc.ToString("X").ToLowerInvariant() : crc),
Machine = new Machine
{
Name = gamename,
},
});
}
}
@@ -538,12 +516,16 @@ namespace SabreTools.Helper
Rom rom = new Rom
{
Type = ItemType.Rom,
MachineName = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
Size = extractedsize,
CRC = gzcrc.ToLowerInvariant(),
MD5 = gzmd5.ToLowerInvariant(),
SHA1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
Machine = new Machine
{
Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
},
};
return rom;
@@ -735,11 +717,12 @@ namespace SabreTools.Helper
}
/// <summary>
/// Read the information from an input 7z file
/// (UNIMPLEMENTED) Read the information from an input 7z file
/// </summary>
/// <param name="input">Name of the input file to check</param>
/// <param name="logger">Logger object for file and console output</param>
/// <remarks>http://cpansearch.perl.org/src/BJOERN/Compress-Deflate7-1.0/7zip/DOC/7zFormat.txt</remarks>
/// <remarks>
/// http://cpansearch.perl.org/src/BJOERN/Compress-Deflate7-1.0/7zip/DOC/7zFormat.txt</remarks>
public static void GetSevenZipFileInfo(string input, Logger logger)
{
BinaryReader br = new BinaryReader(File.OpenRead(input));
@@ -803,7 +786,7 @@ namespace SabreTools.Helper
}
// Get the output archive name from the first rebuild rom
string archiveFileName = Path.Combine(outDir, roms[0].MachineName + ".zip");
string archiveFileName = Path.Combine(outDir, roms[0].Machine.Name + ".zip");
// Set internal variables
Stream writeStream = null;

View File

@@ -1,8 +1,9 @@
using Mono.Data.Sqlite;
using System;
using System;
using System.IO;
using SabreTools.Helper.Data;
using Mono.Data.Sqlite;
namespace SabreTools.Helper
namespace SabreTools.Helper.Tools
{
/// <summary>
/// All general database operations

View File

@@ -1,6 +1,4 @@
using Mono.Data.Sqlite;
using OCRC;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -8,8 +6,13 @@ using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
using Mono.Data.Sqlite;
using NaturalSort;
using OCRC;
namespace SabreTools.Helper
namespace SabreTools.Helper.Tools
{
public static class FileTools
{
@@ -238,31 +241,6 @@ namespace SabreTools.Helper
return xtr;
}
/// <summary>
/// Remove an arbitrary number of bytes from the inputted file
/// </summary>
/// <param name="input">File to be cropped</param>
/// <param name="output">Outputted file</param>
/// <param name="bytesToRemoveFromHead">Bytes to be removed from head of file</param>
/// <param name="bytesToRemoveFromTail">Bytes to be removed from tail of file</param>
public static void RemoveBytesFromFile(string input, string output, long bytesToRemoveFromHead, long bytesToRemoveFromTail)
{
// If any of the inputs are invalid, skip
if (!File.Exists(input) || new FileInfo(input).Length <= (bytesToRemoveFromHead + bytesToRemoveFromTail))
{
return;
}
// Get the streams
FileStream fsr = File.OpenRead(input);
FileStream fsw = File.OpenWrite(output);
RemoveBytesFromStream(fsr, fsw, bytesToRemoveFromHead, bytesToRemoveFromTail);
fsr.Dispose();
fsw.Dispose();
}
/// <summary>
/// Add an aribtrary number of bytes to the inputted file
/// </summary>
@@ -409,7 +387,7 @@ namespace SabreTools.Helper
logger.User("Creating reheadered file: " +
(outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file))) + sub);
FileTools.AppendBytesToFile(file,
AppendBytesToFile(file,
(outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file))) + sub, header, string.Empty);
logger.User("Reheadered file created!");
}
@@ -833,7 +811,7 @@ namespace SabreTools.Helper
if (toFolder)
{
// Copy file to output directory
string gamedir = Path.Combine(outDir, found.MachineName);
string gamedir = Path.Combine(outDir, found.Machine.Name);
if (!Directory.Exists(gamedir))
{
Directory.CreateDirectory(gamedir);
@@ -900,7 +878,7 @@ namespace SabreTools.Helper
if (toFolder)
{
// Copy file to output directory
string gamedir = Path.Combine(outDir, found.MachineName);
string gamedir = Path.Combine(outDir, found.Machine.Name);
if (!Directory.Exists(gamedir))
{
Directory.CreateDirectory(gamedir);
@@ -949,7 +927,7 @@ namespace SabreTools.Helper
if (toFolder)
{
// Copy file to output directory
string gamedir = Path.Combine(outDir, found.MachineName);
string gamedir = Path.Combine(outDir, found.Machine.Name);
if (!Directory.Exists(gamedir))
{
Directory.CreateDirectory(gamedir);
@@ -1025,10 +1003,10 @@ namespace SabreTools.Helper
{
// Copy file to output directory
logger.Verbose("Rebuilding file '" + Path.GetFileName(rom.Name) + "' to '" + found.Name + "'");
string outfile = ArchiveTools.ExtractSingleItemFromArchive(input, rom.Name, tempDir, logger);
string outfile = ArchiveTools.ExtractItem(input, rom.Name, tempDir, logger);
if (File.Exists(outfile))
{
string gamedir = Path.Combine(outDir, found.MachineName);
string gamedir = Path.Combine(outDir, found.Machine.Name);
if (!Directory.Exists(gamedir))
{
Directory.CreateDirectory(gamedir);
@@ -1048,7 +1026,7 @@ namespace SabreTools.Helper
if (Build.MonoEnvironment || tgz)
{
string outfile = ArchiveTools.ExtractSingleItemFromArchive(input, rom.Name, tempDir, logger);
string outfile = ArchiveTools.ExtractItem(input, rom.Name, tempDir, logger);
if (File.Exists(outfile))
{
if (tgz)
@@ -1333,7 +1311,7 @@ namespace SabreTools.Helper
}
// Otherwise, set the machine name as the full path to the file
rom.MachineName = Path.GetDirectoryName(Path.GetFullPath(file));
rom.Machine.Name = Path.GetDirectoryName(Path.GetFullPath(file));
// Add the rom information to the Dat
string key = rom.Size + "-" + rom.CRC;
@@ -1362,7 +1340,7 @@ namespace SabreTools.Helper
rule.TransformStream(input, output, logger, false, true);
Rom romNH = FileTools.GetStreamInfo(output, output.Length);
romNH.Name = "HEAD::" + rom.Name;
romNH.MachineName = rom.MachineName;
romNH.Machine.Name = rom.Machine.Name;
// Add the rom information to the Dat
key = romNH.Size + "-" + romNH.CRC;
@@ -1564,64 +1542,6 @@ namespace SabreTools.Helper
#region Stream Manipulation
// <summary>
/// Remove an arbitrary number of bytes from the inputted stream
/// </summary>
/// <param name="input">Stream to be cropped</param>
/// <param name="output">Stream to output to</param>
/// <param name="bytesToRemoveFromHead">Bytes to be removed from head of stream</param>
/// <param name="bytesToRemoveFromTail">Bytes to be removed from tail of stream</param>
public static void RemoveBytesFromStream(Stream input, Stream output, long bytesToRemoveFromHead, long bytesToRemoveFromTail)
{
// Read the input file and write to the fail
BinaryReader br = new BinaryReader(input);
BinaryWriter bw = new BinaryWriter(output);
int bufferSize = 1024;
long adjustedLength = br.BaseStream.Length - bytesToRemoveFromTail;
// Seek to the correct position
br.BaseStream.Seek((bytesToRemoveFromHead < 0 ? 0 : bytesToRemoveFromHead), SeekOrigin.Begin);
// Now read the file in chunks and write out
byte[] buffer = new byte[bufferSize];
while (br.BaseStream.Position <= (adjustedLength - bufferSize))
{
buffer = br.ReadBytes(bufferSize);
bw.Write(buffer);
}
// For the final chunk, if any, write out only that number of bytes
int length = (int)(adjustedLength - br.BaseStream.Position);
buffer = new byte[length];
buffer = br.ReadBytes(length);
bw.Write(buffer);
}
/// <summary>
/// Add an aribtrary number of bytes to the inputted stream
/// </summary>
/// <param name="input">Stream to be appended to</param>
/// <param name="output">Outputted stream</param>
/// <param name="bytesToAddToHead">String representing bytes to be added to head of stream</param>
/// <param name="bytesToAddToTail">String representing bytes to be added to tail of stream</param>
public static void AppendBytesToStream(Stream input, Stream output, string bytesToAddToHead, string bytesToAddToTail)
{
// Source: http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa
byte[] bytesToAddToHeadArray = new byte[bytesToAddToHead.Length / 2];
for (int i = 0; i < bytesToAddToHead.Length; i += 2)
{
bytesToAddToHeadArray[i / 2] = Convert.ToByte(bytesToAddToHead.Substring(i, 2), 16);
}
byte[] bytesToAddToTailArray = new byte[bytesToAddToTail.Length / 2];
for (int i = 0; i < bytesToAddToTail.Length; i += 2)
{
bytesToAddToTailArray[i / 2] = Convert.ToByte(bytesToAddToTail.Substring(i, 2), 16);
}
AppendBytesToStream(input, output, bytesToAddToHeadArray, bytesToAddToTailArray);
}
/// <summary>
/// Add an aribtrary number of bytes to the inputted stream
/// </summary>

View File

@@ -5,147 +5,16 @@ using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
namespace SabreTools.Helper
namespace SabreTools.Helper.Tools
{
/// <summary>
/// Include character normalization and replacement mappings
/// </summary>
public static class Style
{
#region WoD-based String Cleaning
/// <summary>
/// Replace accented characters
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
public static string NormalizeChars(string input)
{
string[,] charmap = {
{ "Á", "A" }, { "á", "a" },
{ "À", "A" }, { "à", "a" },
{ "Â", "A" }, { "â", "a" },
{ "Ä", "Ae" }, { "ä", "ae" },
{ "Ã", "A" }, { "ã", "a" },
{ "Å", "A" }, { "å", "a" },
{ "Æ", "Ae" }, { "æ", "ae" },
{ "Ç", "C" }, { "ç", "c" },
{ "Ð", "D" }, { "ð", "d" },
{ "É", "E" }, { "é", "e" },
{ "È", "E" }, { "è", "e" },
{ "Ê", "E" }, { "ê", "e" },
{ "Ë", "E" }, { "ë", "e" },
{ "ƒ", "f" },
{ "Í", "I" }, { "í", "i" },
{ "Ì", "I" }, { "ì", "i" },
{ "Î", "I" }, { "î", "i" },
{ "Ï", "I" }, { "ï", "i" },
{ "Ñ", "N" }, { "ñ", "n" },
{ "Ó", "O" }, { "ó", "o" },
{ "Ò", "O" }, { "ò", "o" },
{ "Ô", "O" }, { "ô", "o" },
{ "Ö", "Oe" }, { "ö", "oe" },
{ "Õ", "O" }, { "õ", "o" },
{ "Ø", "O" }, { "ø", "o" },
{ "Š", "S" }, { "š", "s" },
{ "ß", "ss" },
{ "Þ", "B" }, { "þ", "b" },
{ "Ú", "U" }, { "ú", "u" },
{ "Ù", "U" }, { "ù", "u" },
{ "Û", "U" }, { "û", "u" },
{ "Ü", "Ue" }, { "ü", "ue" },
{ "ÿ", "y" },
{ "Ý", "Y" }, { "ý", "y" },
{ "Ž", "Z" }, { "ž", "z" },
};
for (int i = 0; i < charmap.GetLength(0); i++)
{
input = input.Replace(charmap[i, 0], charmap[i, 1]);
}
return input;
}
/// <summary>
/// Replace special characters and patterns
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
public static string SearchPattern(string input)
{
string[,] charmap = {
{ @"~", " - " },
{ @"_", " " },
{ @":", " " },
{ @">", ")" },
{ @"<", "(" },
{ @"\|", "-" },
{ "\"", "'" },
{ @"\*", "." },
{ @"\\", "-" },
{ @"/", "-" },
{ @"\?", " " },
{ @"\(([^)(]*)\(([^)]*)\)([^)(]*)\)", " " },
{ @"\(([^)]+)\)", " " },
{ @"\[([^]]+)\]", " " },
{ @"\{([^}]+)\}", " " },
{ @"(ZZZJUNK|ZZZ-UNK-|ZZZ-UNK |zzz unknow |zzz unk |Copy of |[.][a-z]{3}[.][a-z]{3}[.]|[.][a-z]{3}[.])", " " },
{ @" (r|rev|v|ver)\s*[\d\.]+[^\s]*", " " },
{ @"(( )|(\A))(\d{6}|\d{8})(( )|(\Z))", " " },
{ @"(( )|(\A))(\d{1,2})-(\d{1,2})-(\d{4}|\d{2})", " " },
{ @"(( )|(\A))(\d{4}|\d{2})-(\d{1,2})-(\d{1,2})", " " },
{ @"[-]+", "-" },
{ @"\A\s*\)", " " },
{ @"\A\s*(,|-)", " " },
{ @"\s+", " " },
{ @"\s+,", "," },
{ @"\s*(,|-)\s*\Z", " " },
};
for (int i = 0; i < charmap.GetLength(0); i++)
{
input = Regex.Replace(input, charmap[i, 0], charmap[i, 1]);
}
return input;
}
/// <summary>
/// Convert Cyrillic lettering to Latin lettering
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
public static string RussianToLatin(string input)
{
string[,] charmap = {
{ "А", "A" }, { "Б", "B" }, { "В", "V" }, { "Г", "G" }, { "Д", "D" },
{ "Е", "E" }, { "Ё", "Yo" }, { "Ж", "Zh" }, { "З", "Z" }, { "И", "I" },
{ "Й", "J" }, { "К", "K" }, { "Л", "L" }, { "М", "M" }, { "Н", "N" },
{ "О", "O" }, { "П", "P" }, { "Р", "R" }, { "С", "S" }, { "Т", "T" },
{ "У", "U" }, { "Ф", "f" }, { "Х", "Kh" }, { "Ц", "Ts" }, { "Ч", "Ch" },
{ "Ш", "Sh" }, { "Щ", "Sch" }, { "Ъ", "" }, { "Ы", "y" }, { "Ь", "" },
{ "Э", "e" }, { "Ю", "yu" }, { "Я", "ya" }, { "а", "a" }, { "б", "b" },
{ "в", "v" }, { "г", "g" }, { "д", "d" }, { "е", "e" }, { "ё", "yo" },
{ "ж", "zh" }, { "з", "z" }, { "и", "i" }, { "й", "j" }, { "к", "k" },
{ "л", "l" }, { "м", "m" }, { "н", "n" }, { "о", "o" }, { "п", "p" },
{ "р", "r" }, { "с", "s" }, { "т", "t" }, { "у", "u" }, { "ф", "f" },
{ "х", "kh" }, { "ц", "ts" }, { "ч", "ch" }, { "ш", "sh" }, { "щ", "sch" },
{ "ъ", "" }, { "ы", "y" }, { "ь", "" }, { "э", "e" }, { "ю", "yu" },
{ "я", "ya" },
};
for (int i = 0; i < charmap.GetLength(0); i++)
{
input = input.Replace(charmap[i, 0], charmap[i, 1]);
}
return input;
}
#endregion
#region DAT Cleaning
/// <summary>
@@ -324,9 +193,9 @@ namespace SabreTools.Helper
public static string CleanGameName(string game)
{
///Run the name through the filters to make sure that it's correct
game = Style.NormalizeChars(game);
game = Style.RussianToLatin(game);
game = Style.SearchPattern(game);
game = NormalizeChars(game);
game = RussianToLatin(game);
game = SearchPattern(game);
game = new Regex(@"(([[(].*[\)\]] )?([^([]+))").Match(game).Groups[1].Value;
game = game.TrimStart().TrimEnd();
@@ -370,52 +239,6 @@ namespace SabreTools.Helper
return hash;
}
/// <summary>
/// Clean a hash byte array and pad to the correct size
/// </summary>
/// <param name="hash">Hash byte array to sanitize</param>
/// <param name="padding">Amount of bytes to pad to</param>
/// <returns>Cleaned byte array</returns>
public static byte[] CleanHashData(byte[] hash, int padding)
{
// If we have a null hash or a <=0 padding, return the hash
if (hash == null || padding <= 0)
{
return hash;
}
// If we have a hash longer than the padding, trim and return
if (hash.Length > padding)
{
return hash.Take(padding).ToArray();
}
// If we have a hash of the correct length, return
if (hash.Length == padding)
{
return hash;
}
// Otherwise get the output byte array of the correct length
byte[] newhash = new byte[padding];
// Then write the proper number of empty bytes
int padNeeded = padding - hash.Length;
int index = 0;
for (index = 0; index < padNeeded; index++)
{
newhash[index] = 0x00;
}
// Now add the original hash
for (int i = 0; i < hash.Length; i++)
{
newhash[index + i] = hash[index];
}
return newhash;
}
#endregion
#region String Manipulation
@@ -462,6 +285,139 @@ namespace SabreTools.Helper
#endregion
#region WoD-based String Cleaning
/// <summary>
/// Replace accented characters
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
public static string NormalizeChars(string input)
{
string[,] charmap = {
{ "Á", "A" }, { "á", "a" },
{ "À", "A" }, { "à", "a" },
{ "Â", "A" }, { "â", "a" },
{ "Ä", "Ae" }, { "ä", "ae" },
{ "Ã", "A" }, { "ã", "a" },
{ "Å", "A" }, { "å", "a" },
{ "Æ", "Ae" }, { "æ", "ae" },
{ "Ç", "C" }, { "ç", "c" },
{ "Ð", "D" }, { "ð", "d" },
{ "É", "E" }, { "é", "e" },
{ "È", "E" }, { "è", "e" },
{ "Ê", "E" }, { "ê", "e" },
{ "Ë", "E" }, { "ë", "e" },
{ "ƒ", "f" },
{ "Í", "I" }, { "í", "i" },
{ "Ì", "I" }, { "ì", "i" },
{ "Î", "I" }, { "î", "i" },
{ "Ï", "I" }, { "ï", "i" },
{ "Ñ", "N" }, { "ñ", "n" },
{ "Ó", "O" }, { "ó", "o" },
{ "Ò", "O" }, { "ò", "o" },
{ "Ô", "O" }, { "ô", "o" },
{ "Ö", "Oe" }, { "ö", "oe" },
{ "Õ", "O" }, { "õ", "o" },
{ "Ø", "O" }, { "ø", "o" },
{ "Š", "S" }, { "š", "s" },
{ "ß", "ss" },
{ "Þ", "B" }, { "þ", "b" },
{ "Ú", "U" }, { "ú", "u" },
{ "Ù", "U" }, { "ù", "u" },
{ "Û", "U" }, { "û", "u" },
{ "Ü", "Ue" }, { "ü", "ue" },
{ "ÿ", "y" },
{ "Ý", "Y" }, { "ý", "y" },
{ "Ž", "Z" }, { "ž", "z" },
};
for (int i = 0; i < charmap.GetLength(0); i++)
{
input = input.Replace(charmap[i, 0], charmap[i, 1]);
}
return input;
}
/// <summary>
/// Replace special characters and patterns
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
public static string SearchPattern(string input)
{
string[,] charmap = {
{ @"~", " - " },
{ @"_", " " },
{ @":", " " },
{ @">", ")" },
{ @"<", "(" },
{ @"\|", "-" },
{ "\"", "'" },
{ @"\*", "." },
{ @"\\", "-" },
{ @"/", "-" },
{ @"\?", " " },
{ @"\(([^)(]*)\(([^)]*)\)([^)(]*)\)", " " },
{ @"\(([^)]+)\)", " " },
{ @"\[([^]]+)\]", " " },
{ @"\{([^}]+)\}", " " },
{ @"(ZZZJUNK|ZZZ-UNK-|ZZZ-UNK |zzz unknow |zzz unk |Copy of |[.][a-z]{3}[.][a-z]{3}[.]|[.][a-z]{3}[.])", " " },
{ @" (r|rev|v|ver)\s*[\d\.]+[^\s]*", " " },
{ @"(( )|(\A))(\d{6}|\d{8})(( )|(\Z))", " " },
{ @"(( )|(\A))(\d{1,2})-(\d{1,2})-(\d{4}|\d{2})", " " },
{ @"(( )|(\A))(\d{4}|\d{2})-(\d{1,2})-(\d{1,2})", " " },
{ @"[-]+", "-" },
{ @"\A\s*\)", " " },
{ @"\A\s*(,|-)", " " },
{ @"\s+", " " },
{ @"\s+,", "," },
{ @"\s*(,|-)\s*\Z", " " },
};
for (int i = 0; i < charmap.GetLength(0); i++)
{
input = Regex.Replace(input, charmap[i, 0], charmap[i, 1]);
}
return input;
}
/// <summary>
/// Convert Cyrillic lettering to Latin lettering
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
public static string RussianToLatin(string input)
{
string[,] charmap = {
{ "А", "A" }, { "Б", "B" }, { "В", "V" }, { "Г", "G" }, { "Д", "D" },
{ "Е", "E" }, { "Ё", "Yo" }, { "Ж", "Zh" }, { "З", "Z" }, { "И", "I" },
{ "Й", "J" }, { "К", "K" }, { "Л", "L" }, { "М", "M" }, { "Н", "N" },
{ "О", "O" }, { "П", "P" }, { "Р", "R" }, { "С", "S" }, { "Т", "T" },
{ "У", "U" }, { "Ф", "f" }, { "Х", "Kh" }, { "Ц", "Ts" }, { "Ч", "Ch" },
{ "Ш", "Sh" }, { "Щ", "Sch" }, { "Ъ", "" }, { "Ы", "y" }, { "Ь", "" },
{ "Э", "e" }, { "Ю", "yu" }, { "Я", "ya" }, { "а", "a" }, { "б", "b" },
{ "в", "v" }, { "г", "g" }, { "д", "d" }, { "е", "e" }, { "ё", "yo" },
{ "ж", "zh" }, { "з", "z" }, { "и", "i" }, { "й", "j" }, { "к", "k" },
{ "л", "l" }, { "м", "m" }, { "н", "n" }, { "о", "o" }, { "п", "p" },
{ "р", "r" }, { "с", "s" }, { "т", "t" }, { "у", "u" }, { "ф", "f" },
{ "х", "kh" }, { "ц", "ts" }, { "ч", "ch" }, { "ш", "sh" }, { "щ", "sch" },
{ "ъ", "" }, { "ы", "y" }, { "ь", "" }, { "э", "e" }, { "ю", "yu" },
{ "я", "ya" },
};
for (int i = 0; i < charmap.GetLength(0); i++)
{
input = input.Replace(charmap[i, 0], charmap[i, 1]);
}
return input;
}
#endregion
#region Externally sourced methods
/// <summary>

View File

@@ -1,5 +1,6 @@
using SabreTools.Helper;
using System;
using System;
using SabreTools.Helper.Data;
namespace SabreTools
{

View File

@@ -1,9 +1,12 @@
using SabreTools.Helper;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
using SabreTools.Helper.Tools;
namespace SabreTools
{
public partial class SabreTools
@@ -175,7 +178,7 @@ namespace SabreTools
else
{
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
}
}
}
@@ -212,7 +215,7 @@ namespace SabreTools
{
_logger.Error(input + " is not a valid file or folder!");
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
return;
}
}
@@ -243,7 +246,7 @@ namespace SabreTools
{
_logger.Error(input + " is not a valid file or folder!");
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
return;
}
}
@@ -368,7 +371,7 @@ namespace SabreTools
{
_logger.Error(input + " is not a valid file or folder!");
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
return;
}
}

View File

@@ -1,8 +1,11 @@
using SabreTools.Helper;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.Helper;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
namespace SabreTools
{
/// <summary>
@@ -33,7 +36,7 @@ namespace SabreTools
// Credits take precidence over all
if ((new List<string>(args)).Contains("--credits"))
{
Build.Credits();
Build.Help("Credits");
_logger.Close();
return;
}
@@ -41,7 +44,7 @@ namespace SabreTools
// If there's no arguments, show help
if (args.Length == 0)
{
Build.Help();
Build.Help("SabreTools");
_logger.Close();
return;
}
@@ -456,7 +459,7 @@ namespace SabreTools
{
_logger.Error("DAT must be a valid file: " + args[i]);
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
_logger.Close();
return;
}
@@ -536,7 +539,7 @@ namespace SabreTools
{
_logger.Error("Invalid input detected: " + args[i]);
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
Console.WriteLine();
_logger.Error("Invalid input detected: " + args[i]);
_logger.Close();
@@ -706,7 +709,7 @@ namespace SabreTools
{
_logger.Error("DAT must be a valid file: " + split[1]);
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
_logger.Close();
return;
}
@@ -773,7 +776,7 @@ namespace SabreTools
{
_logger.Error("Invalid input detected: " + args[i]);
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
Console.WriteLine();
_logger.Error("Invalid input detected: " + args[i]);
_logger.Close();
@@ -880,7 +883,7 @@ namespace SabreTools
{
_logger.Error("Invalid input detected: " + args[i]);
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
Console.WriteLine();
_logger.Error("Invalid input detected: " + args[i]);
_logger.Close();
@@ -897,7 +900,7 @@ namespace SabreTools
{
_logger.Error("Invalid input detected: " + args[i]);
Console.WriteLine();
Build.Help();
Build.Help("SabreTools");
Console.WriteLine();
_logger.Error("Invalid input detected: " + args[i]);
_logger.Close();
@@ -910,7 +913,7 @@ namespace SabreTools
// If help is set, show the help screen
if (help)
{
Build.Help();
Build.Help("SabreTools");
_logger.Close();
return;
}
@@ -919,7 +922,7 @@ namespace SabreTools
if (!(datFromDir ^ headerer ^ splitByExt ^ splitByHash ^ splitByType ^ stats ^ update))
{
_logger.Error("Only one feature switch is allowed at a time");
Build.Help();
Build.Help("SabreTools");
_logger.Close();
return;
}
@@ -929,7 +932,7 @@ namespace SabreTools
&& (datFromDir || headerer || splitByExt || splitByHash || splitByType || stats || update))
{
_logger.Error("This feature requires at least one input");
Build.Help();
Build.Help("SabreTools");
_logger.Close();
return;
}
@@ -1026,7 +1029,7 @@ namespace SabreTools
// If nothing is set, show the help
else
{
Build.Help();
Build.Help("SabreTools");
}
_logger.Close();

View File

@@ -1,7 +1,10 @@
using SabreTools.Helper;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.Helper;
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
using SabreTools.Helper.Tools;
namespace SabreTools
{
@@ -25,14 +28,14 @@ namespace SabreTools
// Credits take precidence over all
if ((new List<string>(args)).Contains("--credits"))
{
Build.Credits();
Build.Help("Credits");
return;
}
// If there's no arguments, show help
if (args.Length == 0)
{
Build.Help();
Build.Help("SimpleSort");
logger.Close();
return;
}
@@ -136,7 +139,7 @@ namespace SabreTools
{
logger.Error("DAT must be a valid file: " + args[i]);
Console.WriteLine();
Build.Help();
Build.Help("SimpleSort");
logger.Close();
return;
}
@@ -208,7 +211,7 @@ namespace SabreTools
{
logger.Error("DAT must be a valid file: " + split[1]);
Console.WriteLine();
Build.Help();
Build.Help("SimpleSort");
logger.Close();
return;
}
@@ -256,7 +259,7 @@ namespace SabreTools
{
logger.Error("Invalid input detected: " + args[i]);
Console.WriteLine();
Build.Help();
Build.Help("SimpleSort");
Console.WriteLine();
logger.Error("Invalid input detected: " + args[i]);
logger.Close();
@@ -273,7 +276,7 @@ namespace SabreTools
{
logger.Error("Invalid input detected: " + args[i]);
Console.WriteLine();
Build.Help();
Build.Help("SimpleSort");
Console.WriteLine();
logger.Error("Invalid input detected: " + args[i]);
logger.Close();
@@ -286,7 +289,7 @@ namespace SabreTools
// If help is set, show the help screen
if (help)
{
Build.Help();
Build.Help("SimpleSort");
logger.Close();
return;
}
@@ -295,7 +298,7 @@ namespace SabreTools
if (inputs.Count == 0 && ((sort && !verify) || convert))
{
logger.Error("This feature requires at least one input");
Build.Help();
Build.Help("SimpleSort");
logger.Close();
return;
}
@@ -318,7 +321,7 @@ namespace SabreTools
else
{
logger.Error("A datfile is required to use this feature");
Build.Help();
Build.Help("SimpleSort");
logger.Close();
return;
}
@@ -327,7 +330,7 @@ namespace SabreTools
// If nothing is set, show the help
else
{
Build.Help();
Build.Help("SimpleSort");
}
logger.Close();