[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 System;
using SabreTools.Helper;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Xml; using System.Xml;
using Mono.Data.Sqlite;
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
using SabreTools.Helper.Tools;
namespace SabreTools namespace SabreTools
{ {
@@ -459,7 +462,7 @@ namespace SabreTools
private static void AddDatToDatabase(Rom dat, SqliteConnection dbc) private static void AddDatToDatabase(Rom dat, SqliteConnection dbc)
{ {
// Get the dat full path // 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 // Parse the Dat if possible
_logger.User("Adding from '" + dat.Name + "'"); _logger.User("Adding from '" + dat.Name + "'");

View File

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

View File

@@ -1,8 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
namespace SabreTools.Helper namespace SabreTools.Helper.Data
{ {
public static class Build public static class Build
{ {
@@ -18,10 +17,9 @@ namespace SabreTools.Helper
/// Readies the console and outputs the header /// Readies the console and outputs the header
/// </summary> /// </summary>
/// <param name="name">The name to be displayed as the program</param> /// <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) 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; int width = Console.WindowWidth - 3;
string border = "+" + new string('-', width) + "+"; string border = "+" + new string('-', width) + "+";
string mid = name + " " + Constants.Version; string mid = name + " " + Constants.Version;
@@ -62,17 +60,28 @@ namespace SabreTools.Helper
/// <summary> /// <summary>
/// Show the help dialog for a given class /// Show the help dialog for a given class
/// </summary> /// </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 //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 = "-----------------------------------------"; string barrier = "-----------------------------------------";
List<String> helptext = new List<string>(); List<string> helptext = new List<string>();
// Set the help text // Set the help text
switch (className) 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": case "RombaSharp":
helptext.Add(Resources.Resources.RombaSharp_Name + " - " + Resources.Resources.RombaSharp_Desc); helptext.Add(Resources.Resources.RombaSharp_Name + " - " + Resources.Resources.RombaSharp_Desc);
helptext.Add(barrier); helptext.Add(barrier);
@@ -80,27 +89,56 @@ namespace SabreTools.Helper
helptext.Add(""); helptext.Add("");
helptext.Add("Options:"); helptext.Add("Options:");
helptext.Add(" -?, -h, --help Show this help"); helptext.Add(" -?, -h, --help Show this help");
// Archive
helptext.Add(" archive Adds ROM files from the specified directories to depot"); helptext.Add(" archive Adds ROM files from the specified directories to depot");
helptext.Add(" -only-needed Only archive ROM files in database"); 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(" build For each specified DAT file it creates TZip files");
helptext.Add(" -copy Copy files instead of rebuilding"); helptext.Add(" -copy Copy files instead of rebuilding");
// Stats
helptext.Add(" dbstats Prints db stats"); helptext.Add(" dbstats Prints db stats");
// Rescan Depots
helptext.Add(" depot-rescan Rescan a specific depot to get new information"); 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(" diffdat Creates a DAT file for entries found in the new DAT");
helptext.Add(" -new= DAT to compare to"); helptext.Add(" -new= DAT to compare to");
// Dir2DAT / DATFromDir
helptext.Add(" dir2dat Creates a DAT file for the specified input directory"); helptext.Add(" dir2dat Creates a DAT file for the specified input directory");
helptext.Add(" -out= Filename to save out to"); helptext.Add(" -out= Filename to save out to");
// Export
helptext.Add(" export Exports db to export.csv"); helptext.Add(" export Exports db to export.csv");
// Fixdat
helptext.Add(" fixdat For each specified DAT file it creates a fix DAT"); 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"); helptext.Add(" lookup For each specified hash, look up available information");
// Memstats
helptext.Add(" memstats Prints memory stats"); helptext.Add(" memstats Prints memory stats");
// Miss
helptext.Add(" miss For each specified DAT file, create miss and have file"); 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-backup Moves DAT index entries for orphaned DATs");
helptext.Add(" purge-delete Deletes 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"); 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]"); helptext.Add(" shutdown Gracefully shuts down server [OBSOLETE]");
break; break;
case "SabreTools": case "SabreTools":
helptext.Add(Resources.Resources.SabreTools_Name + " - " + Resources.Resources.SabreTools_Desc); helptext.Add(Resources.Resources.SabreTools_Name + " - " + Resources.Resources.SabreTools_Desc);
helptext.Add(barrier); helptext.Add(barrier);
@@ -331,6 +369,7 @@ namespace SabreTools.Helper
helptext.Add("Filter parameters for size can use postfixes for inputs:"); helptext.Add("Filter parameters for size can use postfixes for inputs:");
helptext.Add(" e.g. 8kb => 8000 or 8kib => 8192"); helptext.Add(" e.g. 8kb => 8000 or 8kib => 8192");
break; break;
case "SimpleSort": case "SimpleSort":
helptext.Add(Resources.Resources.SimpleSort_Name + " - " + Resources.Resources.SimpleSort_Desc); helptext.Add(Resources.Resources.SimpleSort_Name + " - " + Resources.Resources.SimpleSort_Desc);
helptext.Add(barrier); helptext.Add(barrier);
@@ -388,23 +427,6 @@ namespace SabreTools.Helper
Pause(); 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> /// <summary>
/// Pause on console output /// Pause on console output
/// </summary> /// </summary>

View File

@@ -1,6 +1,6 @@
using System; using System;
namespace SabreTools.Helper namespace SabreTools.Helper.Data
{ {
public static class Constants public static class Constants
{ {
@@ -14,70 +14,21 @@ namespace SabreTools.Helper
public const long SizeZero = 0; public const long SizeZero = 0;
public const string CRCZero = "00000000"; public const string CRCZero = "00000000";
public static byte[] CRCZeroBytes = new byte[] { 0x00, 0x00, 0x00, 0x00 };
public const string MD5Zero = "d41d8cd98f00b204e9800998ecf8427e"; 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 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 #endregion
#region Hash string length constants #region Byte (1000-based) size comparisons
public const int CRCLength = 8; public const long KiloByte = 1000;
public const int CRCBytesLength = 4; public static long MegaByte = (long)Math.Pow(KiloByte, 2);
public const int MD5Length = 32; public static long GigaByte = (long)Math.Pow(KiloByte, 3);
public const int MD5BytesLength = 16; public static long TeraByte = (long)Math.Pow(KiloByte, 4);
public const int SHA1Length = 40; public static long PetaByte = (long)Math.Pow(KiloByte, 5);
public const int SHA1BytesLength = 20; public static long ExaByte = (long)Math.Pow(KiloByte, 6);
public static long ZettaByte = (long)Math.Pow(KiloByte, 7);
#endregion public static long YottaByte = (long)Math.Pow(KiloByte, 8);
#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*$";
#endregion #endregion
@@ -88,30 +39,25 @@ namespace SabreTools.Helper
public static long GibiByte = (long)Math.Pow(KibiByte, 3); public static long GibiByte = (long)Math.Pow(KibiByte, 3);
public static long TibiByte = (long)Math.Pow(KibiByte, 4); public static long TibiByte = (long)Math.Pow(KibiByte, 4);
public static long PibiByte = (long)Math.Pow(KibiByte, 5); 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 #endregion
#region Byte (1000-based) size comparisons #region Database schema
public const long KiloByte = 1000; public const string HeadererDbSchema = "Headerer";
public static long MegaByte = (long)Math.Pow(KiloByte, 2); public const string HeadererFileName = "Headerer.sqlite";
public static long GigaByte = (long)Math.Pow(KiloByte, 2); public const string HeadererConnectionString = "Data Source=" + HeadererFileName + ";Version = 3;";
public static long TeraByte = (long)Math.Pow(KiloByte, 2);
public static long PetaByte = (long)Math.Pow(KiloByte, 2);
#endregion #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 const int CRCLength = 8;
public static byte[] GzSigBytes = new byte[] { 0x1f, 0x8b }; public const int MD5Length = 32;
public static byte[] RarSigBytes = new byte[] { 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00 }; public const int SHA1Length = 40;
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 };
#endregion #endregion
@@ -129,6 +75,15 @@ namespace SabreTools.Helper
#endregion #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 #region TorrentZip, T7z, and TGZ headers
/* TorrentZip Header Format /* TorrentZip Header Format
@@ -184,13 +139,5 @@ namespace SabreTools.Helper
public const uint TorrentZipFileDateTime = 0x2198BC00; public const uint TorrentZipFileDateTime = 0x2198BC00;
#endregion #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 #region DatFile related
/// <summary> /// <summary>
@@ -127,127 +235,4 @@
} }
#endregion #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; using System;
namespace SabreTools.Helper namespace SabreTools.Helper.Data
{ {
#region Archival
/// <summary> /// <summary>
/// Determines the level to scan archives at /// Determines the level to scan archives at
/// </summary> /// </summary>
@@ -29,61 +31,6 @@ namespace SabreTools.Helper
ZipBoth = ZipExternal | ZipInternal, 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> /// <summary>
/// Determines the archive general bit flags /// Determines the archive general bit flags
/// </summary> /// </summary>
@@ -145,4 +92,69 @@ namespace SabreTools.Helper
TorrentZip = 0x1, TorrentZip = 0x1,
ExtraData = 0x2 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.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace SabreTools.Helper namespace NaturalSort
{ {
public class NaturalComparer : Comparer<string>, IDisposable public class NaturalComparer : Comparer<string>, IDisposable
{ {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -61,10 +61,9 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using System; using System;
namespace SabreTools.Helper namespace Ionic.Zlib
{ {
sealed class InflateBlocks sealed class InflateBlocks
{ {
@@ -137,11 +136,12 @@ namespace SabreTools.Helper
readAt = writeAt = 0; readAt = writeAt = 0;
if (checkfn != null) if (checkfn != null)
{
_codec._Adler32 = check = Adler.Adler32(0, null, 0, 0); _codec._Adler32 = check = Adler.Adler32(0, null, 0, 0);
}
return oldCheck; return oldCheck;
} }
internal int Process(int r) internal int Process(int r)
{ {
int t; // temporary storage int t; // temporary storage
@@ -153,7 +153,6 @@ namespace SabreTools.Helper
int m; // bytes to end of window or read pointer int m; // bytes to end of window or read pointer
// copy input/output information to locals (UPDATE macro restores) // copy input/output information to locals (UPDATE macro restores)
p = _codec.NextIn; p = _codec.NextIn;
n = _codec.AvailableBytesIn; n = _codec.AvailableBytesIn;
b = bitb; b = bitb;
@@ -162,7 +161,6 @@ namespace SabreTools.Helper
q = writeAt; q = writeAt;
m = (int)(q < readAt ? readAt - q - 1 : end - q); m = (int)(q < readAt ? readAt - q - 1 : end - q);
// process input based on current state // process input based on current state
while (true) while (true)
{ {
@@ -314,14 +312,20 @@ namespace SabreTools.Helper
t = left; t = left;
if (t > n) if (t > n)
{
t = n; t = n;
}
if (t > m) if (t > m)
{
t = m; t = m;
}
Array.Copy(_codec.InputBuffer, p, window, q, t); Array.Copy(_codec.InputBuffer, p, window, q, t);
p += t; n -= t; p += t; n -= t;
q += t; m -= t; q += t; m -= t;
if ((left -= t) != 0) if ((left -= t) != 0)
{
break; break;
}
mode = last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE; mode = last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE;
break; break;
@@ -379,7 +383,6 @@ namespace SabreTools.Helper
b >>= 14; b >>= 14;
k -= 14; k -= 14;
index = 0; index = 0;
mode = InflateBlockMode.BTREE; mode = InflateBlockMode.BTREE;
goto case InflateBlockMode.BTREE; goto case InflateBlockMode.BTREE;
@@ -639,7 +642,6 @@ namespace SabreTools.Helper
writeAt = q; writeAt = q;
return Flush(r); return Flush(r);
default: default:
r = ZlibConstants.Z_STREAM_ERROR; r = ZlibConstants.Z_STREAM_ERROR;
@@ -653,7 +655,6 @@ namespace SabreTools.Helper
} }
} }
internal void Free() internal void Free()
{ {
Reset(); Reset();
@@ -701,10 +702,14 @@ namespace SabreTools.Helper
} }
if (nBytes > _codec.AvailableBytesOut) if (nBytes > _codec.AvailableBytesOut)
{
nBytes = _codec.AvailableBytesOut; nBytes = _codec.AvailableBytesOut;
}
if (nBytes != 0 && r == ZlibConstants.Z_BUF_ERROR) if (nBytes != 0 && r == ZlibConstants.Z_BUF_ERROR)
{
r = ZlibConstants.Z_OK; r = ZlibConstants.Z_OK;
}
// update counters // update counters
_codec.AvailableBytesOut -= nBytes; _codec.AvailableBytesOut -= nBytes;
@@ -712,7 +717,9 @@ namespace SabreTools.Helper
// update check information // update check information
if (checkfn != null) if (checkfn != null)
{
_codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes); _codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes);
}
// copy as far as end of window // copy as far as end of window
Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes); Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes);
@@ -725,8 +732,10 @@ namespace SabreTools.Helper
// wrap pointers // wrap pointers
readAt = 0; readAt = 0;
if (writeAt == end) if (writeAt == end)
{
writeAt = 0; writeAt = 0;
} }
}
else pass++; else pass++;
} }
@@ -735,7 +744,6 @@ namespace SabreTools.Helper
} }
} }
internal static class InternalInflateConstants internal static class InternalInflateConstants
{ {
// And'ing with mask[n] masks the lower n bits // And'ing with mask[n] masks the lower n bits
@@ -746,7 +754,6 @@ namespace SabreTools.Helper
0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff }; 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff };
} }
sealed class InflateCodes sealed class InflateCodes
{ {
// waiting for "i:"=input, // waiting for "i:"=input,
@@ -864,7 +871,9 @@ namespace SabreTools.Helper
while (k < j) while (k < j)
{ {
if (n != 0) if (n != 0)
{
r = ZlibConstants.Z_OK; r = ZlibConstants.Z_OK;
}
else else
{ {
blocks.bitb = b; blocks.bitk = k; blocks.bitb = b; blocks.bitk = k;
@@ -925,14 +934,15 @@ namespace SabreTools.Helper
blocks.writeAt = q; blocks.writeAt = q;
return blocks.Flush(r); return blocks.Flush(r);
case LENEXT: // i: getting length extra (have base) case LENEXT: // i: getting length extra (have base)
j = bitsToGet; j = bitsToGet;
while (k < j) while (k < j)
{ {
if (n != 0) if (n != 0)
{
r = ZlibConstants.Z_OK; r = ZlibConstants.Z_OK;
}
else else
{ {
blocks.bitb = b; blocks.bitk = k; blocks.bitb = b; blocks.bitk = k;
@@ -961,7 +971,9 @@ namespace SabreTools.Helper
while (k < j) while (k < j)
{ {
if (n != 0) if (n != 0)
{
r = ZlibConstants.Z_OK; r = ZlibConstants.Z_OK;
}
else else
{ {
blocks.bitb = b; blocks.bitk = k; blocks.bitb = b; blocks.bitk = k;
@@ -1003,14 +1015,15 @@ namespace SabreTools.Helper
blocks.writeAt = q; blocks.writeAt = q;
return blocks.Flush(r); return blocks.Flush(r);
case DISTEXT: // i: getting distance extra case DISTEXT: // i: getting distance extra
j = bitsToGet; j = bitsToGet;
while (k < j) while (k < j)
{ {
if (n != 0) if (n != 0)
{
r = ZlibConstants.Z_OK; r = ZlibConstants.Z_OK;
}
else else
{ {
blocks.bitb = b; blocks.bitk = k; blocks.bitb = b; blocks.bitk = k;
@@ -1070,7 +1083,9 @@ namespace SabreTools.Helper
blocks.window[q++] = blocks.window[f++]; m--; blocks.window[q++] = blocks.window[f++]; m--;
if (f == blocks.end) if (f == blocks.end)
{
f = 0; f = 0;
}
len--; len--;
} }
mode = START; mode = START;
@@ -1157,7 +1172,6 @@ namespace SabreTools.Helper
} }
} }
// Called with number of bytes left to write in window at least 258 // Called with number of bytes left to write in window at least 258
// (the maximum string length) and number of input bytes available // (the maximum string length) and number of input bytes available
// at least ten. The ten bytes are six bytes for the longest length/ // at least ten. The ten bytes are six bytes for the longest length/
@@ -1402,7 +1416,6 @@ namespace SabreTools.Helper
} }
} }
internal sealed class InflateManager internal sealed class InflateManager
{ {
// preset dictionary flag in zlib header // preset dictionary flag in zlib header
@@ -1472,7 +1485,9 @@ namespace SabreTools.Helper
internal int End() internal int End()
{ {
if (blocks != null) if (blocks != null)
{
blocks.Free(); blocks.Free();
}
blocks = null; blocks = null;
return ZlibConstants.Z_OK; return ZlibConstants.Z_OK;
} }
@@ -1510,13 +1525,14 @@ namespace SabreTools.Helper
return ZlibConstants.Z_OK; return ZlibConstants.Z_OK;
} }
internal int Inflate(FlushType flush) internal int Inflate(FlushType flush)
{ {
int b; int b;
if (_codec.InputBuffer == null) if (_codec.InputBuffer == null)
{
throw new ZlibException("InputBuffer is null. "); throw new ZlibException("InputBuffer is null. ");
}
// int f = (flush == FlushType.Finish) // int f = (flush == FlushType.Finish)
// ? ZlibConstants.Z_BUF_ERROR // ? ZlibConstants.Z_BUF_ERROR
@@ -1531,7 +1547,10 @@ namespace SabreTools.Helper
switch (mode) switch (mode)
{ {
case InflateManagerMode.METHOD: case InflateManagerMode.METHOD:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.AvailableBytesIn--;
_codec.TotalBytesIn++; _codec.TotalBytesIn++;
@@ -1552,9 +1571,11 @@ namespace SabreTools.Helper
mode = InflateManagerMode.FLAG; mode = InflateManagerMode.FLAG;
break; break;
case InflateManagerMode.FLAG: case InflateManagerMode.FLAG:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.AvailableBytesIn--;
_codec.TotalBytesIn++; _codec.TotalBytesIn++;
@@ -1574,7 +1595,10 @@ namespace SabreTools.Helper
break; break;
case InflateManagerMode.DICT4: case InflateManagerMode.DICT4:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.AvailableBytesIn--;
_codec.TotalBytesIn++; _codec.TotalBytesIn++;
@@ -1583,7 +1607,10 @@ namespace SabreTools.Helper
break; break;
case InflateManagerMode.DICT3: case InflateManagerMode.DICT3:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.AvailableBytesIn--;
_codec.TotalBytesIn++; _codec.TotalBytesIn++;
@@ -1593,7 +1620,10 @@ namespace SabreTools.Helper
case InflateManagerMode.DICT2: case InflateManagerMode.DICT2:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.AvailableBytesIn--;
_codec.TotalBytesIn++; _codec.TotalBytesIn++;
@@ -1603,7 +1633,10 @@ namespace SabreTools.Helper
case InflateManagerMode.DICT1: case InflateManagerMode.DICT1:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++; _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff); expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
@@ -1611,14 +1644,12 @@ namespace SabreTools.Helper
mode = InflateManagerMode.DICT0; mode = InflateManagerMode.DICT0;
return ZlibConstants.Z_NEED_DICT; return ZlibConstants.Z_NEED_DICT;
case InflateManagerMode.DICT0: case InflateManagerMode.DICT0:
mode = InflateManagerMode.BAD; mode = InflateManagerMode.BAD;
_codec.Message = "need dictionary"; _codec.Message = "need dictionary";
marker = 0; // can try inflateSync marker = 0; // can try inflateSync
return ZlibConstants.Z_STREAM_ERROR; return ZlibConstants.Z_STREAM_ERROR;
case InflateManagerMode.BLOCKS: case InflateManagerMode.BLOCKS:
r = blocks.Process(r); r = blocks.Process(r);
if (r == ZlibConstants.Z_DATA_ERROR) if (r == ZlibConstants.Z_DATA_ERROR)
@@ -1628,10 +1659,15 @@ namespace SabreTools.Helper
break; break;
} }
if (r == ZlibConstants.Z_OK) r = f; if (r == ZlibConstants.Z_OK)
{
r = f;
}
if (r != ZlibConstants.Z_STREAM_END) if (r != ZlibConstants.Z_STREAM_END)
{
return r; return r;
}
r = f; r = f;
computedCheck = blocks.Reset(); computedCheck = blocks.Reset();
@@ -1644,7 +1680,10 @@ namespace SabreTools.Helper
break; break;
case InflateManagerMode.CHECK4: case InflateManagerMode.CHECK4:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.AvailableBytesIn--;
_codec.TotalBytesIn++; _codec.TotalBytesIn++;
@@ -1653,7 +1692,10 @@ namespace SabreTools.Helper
break; break;
case InflateManagerMode.CHECK3: case InflateManagerMode.CHECK3:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++; _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000); expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
@@ -1661,7 +1703,10 @@ namespace SabreTools.Helper
break; break;
case InflateManagerMode.CHECK2: case InflateManagerMode.CHECK2:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.AvailableBytesIn--;
_codec.TotalBytesIn++; _codec.TotalBytesIn++;
@@ -1670,7 +1715,10 @@ namespace SabreTools.Helper
break; break;
case InflateManagerMode.CHECK1: case InflateManagerMode.CHECK1:
if (_codec.AvailableBytesIn == 0) return r; if (_codec.AvailableBytesIn == 0)
{
return r;
}
r = f; r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++; _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff); expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
@@ -1692,19 +1740,18 @@ namespace SabreTools.Helper
default: default:
throw new ZlibException("Stream error."); throw new ZlibException("Stream error.");
} }
} }
} }
internal int SetDictionary(byte[] dictionary) internal int SetDictionary(byte[] dictionary)
{ {
int index = 0; int index = 0;
int length = dictionary.Length; int length = dictionary.Length;
if (mode != InflateManagerMode.DICT0) if (mode != InflateManagerMode.DICT0)
{
throw new ZlibException("Stream error."); throw new ZlibException("Stream error.");
}
if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32) if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
{ {
@@ -1723,7 +1770,6 @@ namespace SabreTools.Helper
return ZlibConstants.Z_OK; return ZlibConstants.Z_OK;
} }
private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff }; private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
internal int Sync() internal int Sync()
@@ -1740,7 +1786,9 @@ namespace SabreTools.Helper
marker = 0; marker = 0;
} }
if ((n = _codec.AvailableBytesIn) == 0) if ((n = _codec.AvailableBytesIn) == 0)
{
return ZlibConstants.Z_BUF_ERROR; return ZlibConstants.Z_BUF_ERROR;
}
p = _codec.NextIn; p = _codec.NextIn;
m = marker; m = marker;
@@ -1782,7 +1830,6 @@ namespace SabreTools.Helper
return ZlibConstants.Z_OK; return ZlibConstants.Z_OK;
} }
// Returns true if inflate is currently at the end of a block generated // 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 // 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 // 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.Threading;
using System.IO; using System.IO;
namespace Ionic.Zlib
namespace SabreTools.Helper
{ {
internal class WorkItem internal class WorkItem
{ {
@@ -306,7 +305,6 @@ namespace SabreTools.Helper
this.MaxBufferPairs = 16; // default this.MaxBufferPairs = 16; // default
} }
/// <summary> /// <summary>
/// The ZLIB strategy to be used during compression. /// The ZLIB strategy to be used during compression.
/// </summary> /// </summary>
@@ -397,8 +395,10 @@ namespace SabreTools.Helper
set set
{ {
if (value < 4) if (value < 4)
{
throw new ArgumentException("MaxBufferPairs", throw new ArgumentException("MaxBufferPairs",
"Value must be 4 or greater."); "Value must be 4 or greater.");
}
_maxBufferPairs = value; _maxBufferPairs = value;
} }
} }
@@ -449,8 +449,10 @@ namespace SabreTools.Helper
set set
{ {
if (value < 1024) if (value < 1024)
{
throw new ArgumentOutOfRangeException("BufferSize", throw new ArgumentOutOfRangeException("BufferSize",
"BufferSize must be greater than 1024 bytes"); "BufferSize must be greater than 1024 bytes");
}
_bufferSize = value; _bufferSize = value;
} }
} }
@@ -463,7 +465,6 @@ namespace SabreTools.Helper
/// </remarks> /// </remarks>
public int Crc32 { get { return _Crc32; } } public int Crc32 { get { return _Crc32; } }
/// <summary> /// <summary>
/// The total number of uncompressed bytes processed by the ParallelDeflateOutputStream. /// The total number of uncompressed bytes processed by the ParallelDeflateOutputStream.
/// </summary> /// </summary>
@@ -472,7 +473,6 @@ namespace SabreTools.Helper
/// </remarks> /// </remarks>
public Int64 BytesProcessed { get { return _totalBytesProcessed; } } public Int64 BytesProcessed { get { return _totalBytesProcessed; } }
private void _InitializePoolOfWorkItems() private void _InitializePoolOfWorkItems()
{ {
_toWrite = new Queue<int>(); _toWrite = new Queue<int>();
@@ -494,9 +494,6 @@ namespace SabreTools.Helper
_latestCompressed = -1; _latestCompressed = -1;
} }
/// <summary> /// <summary>
/// Write data to the stream. /// Write data to the stream.
/// </summary> /// </summary>
@@ -531,7 +528,9 @@ namespace SabreTools.Helper
// 3. if more data to be written, goto step 1 // 3. if more data to be written, goto step 1
if (_isClosed) if (_isClosed)
{
throw new InvalidOperationException(); throw new InvalidOperationException();
}
// dispense any exceptions that occurred on the BG threads // dispense any exceptions that occurred on the BG threads
if (_pendingException != null) if (_pendingException != null)
@@ -553,7 +552,6 @@ namespace SabreTools.Helper
_firstWriteDone = true; _firstWriteDone = true;
} }
do do
{ {
// may need to make buffers available // may need to make buffers available
@@ -628,24 +626,28 @@ namespace SabreTools.Helper
workitem.inputBytesAvailable); workitem.inputBytesAvailable);
if (!ThreadPool.QueueUserWorkItem(_DeflateOne, workitem)) if (!ThreadPool.QueueUserWorkItem(_DeflateOne, workitem))
{
throw new Exception("Cannot enqueue workitem"); throw new Exception("Cannot enqueue workitem");
}
_currentlyFilling = -1; // will get a new buffer next time _currentlyFilling = -1; // will get a new buffer next time
} }
else else
{
_currentlyFilling = ix; _currentlyFilling = ix;
}
if (count > 0) if (count > 0)
{
TraceOutput(TraceBits.WriteEnter, "Write more"); TraceOutput(TraceBits.WriteEnter, "Write more");
} }
}
while (count > 0); // until no more to write while (count > 0); // until no more to write
TraceOutput(TraceBits.WriteEnter, "Write exit"); TraceOutput(TraceBits.WriteEnter, "Write exit");
return; return;
} }
private void _FlushFinish() private void _FlushFinish()
{ {
// After writing a series of compressed buffers, each one closed // After writing a series of compressed buffers, each one closed
@@ -663,7 +665,9 @@ namespace SabreTools.Helper
rc = compressor.Deflate(FlushType.Finish); rc = compressor.Deflate(FlushType.Finish);
if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
{
throw new Exception("deflating: " + compressor.Message); throw new Exception("deflating: " + compressor.Message);
}
if (buffer.Length - compressor.AvailableBytesOut > 0) if (buffer.Length - compressor.AvailableBytesOut > 0)
{ {
@@ -682,13 +686,17 @@ namespace SabreTools.Helper
_Crc32 = _runningCrc.Crc32Result; _Crc32 = _runningCrc.Crc32Result;
} }
private void _Flush(bool lastInput) private void _Flush(bool lastInput)
{ {
if (_isClosed) if (_isClosed)
{
throw new InvalidOperationException(); throw new InvalidOperationException();
}
if (emitting) return; if (emitting)
{
return;
}
// compress any partial buffer // compress any partial buffer
if (_currentlyFilling >= 0) if (_currentlyFilling >= 0)
@@ -709,8 +717,6 @@ namespace SabreTools.Helper
} }
} }
/// <summary> /// <summary>
/// Flush the stream. /// Flush the stream.
/// </summary> /// </summary>
@@ -724,12 +730,13 @@ namespace SabreTools.Helper
throw pe; throw pe;
} }
if (_handlingException) if (_handlingException)
{
return; return;
}
_Flush(false); _Flush(false);
} }
/// <summary> /// <summary>
/// Close the stream. /// Close the stream.
/// </summary> /// </summary>
@@ -750,20 +757,25 @@ namespace SabreTools.Helper
} }
if (_handlingException) if (_handlingException)
{
return; return;
}
if (_isClosed) return; if (_isClosed)
{
return;
}
_Flush(true); _Flush(true);
if (!_leaveOpen) if (!_leaveOpen)
{
_outStream.Close(); _outStream.Close();
}
_isClosed= true; _isClosed= true;
} }
// workitem 10030 - implement a new Dispose method // workitem 10030 - implement a new Dispose method
/// <summary>Dispose the object</summary> /// <summary>Dispose the object</summary>
@@ -785,8 +797,6 @@ namespace SabreTools.Helper
Dispose(true); Dispose(true);
} }
/// <summary>The Dispose method</summary> /// <summary>The Dispose method</summary>
/// <param name="disposing"> /// <param name="disposing">
/// indicates whether the Dispose method was invoked by user code. /// indicates whether the Dispose method was invoked by user code.
@@ -796,7 +806,6 @@ namespace SabreTools.Helper
base.Dispose(disposing); base.Dispose(disposing);
} }
/// <summary> /// <summary>
/// Resets the stream for use with another stream. /// Resets the stream for use with another stream.
/// </summary> /// </summary>
@@ -842,7 +851,10 @@ namespace SabreTools.Helper
TraceOutput(TraceBits.Session, "-------------------------------------------------------"); TraceOutput(TraceBits.Session, "-------------------------------------------------------");
TraceOutput(TraceBits.Session, "Reset {0:X8} firstDone({1})", this.GetHashCode(), _firstWriteDone); TraceOutput(TraceBits.Session, "Reset {0:X8} firstDone({1})", this.GetHashCode(), _firstWriteDone);
if (!_firstWriteDone) return; if (!_firstWriteDone)
{
return;
}
// reset all status // reset all status
_toWrite.Clear(); _toWrite.Clear();
@@ -864,9 +876,6 @@ namespace SabreTools.Helper
_outStream = stream; _outStream = stream;
} }
private void EmitPendingBuffers(bool doAll, bool mustWait) private void EmitPendingBuffers(bool doAll, bool mustWait)
{ {
// When combining parallel deflation with a ZipSegmentedStream, it's // 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. // method invokes this method AGAIN. This can lead to a deadlock.
// Therefore, failfast if re-entering. // Therefore, failfast if re-entering.
if (emitting) return; if (emitting)
{
return;
}
emitting = true; emitting = true;
if (doAll || mustWait) if (doAll || mustWait)
{
_newlyCompressedBlob.WaitOne(); _newlyCompressedBlob.WaitOne();
}
do do
{ {
@@ -896,8 +910,10 @@ namespace SabreTools.Helper
try try
{ {
if (_toWrite.Count > 0) if (_toWrite.Count > 0)
{
nextToWrite = _toWrite.Dequeue(); nextToWrite = _toWrite.Dequeue();
} }
}
finally finally
{ {
Monitor.Exit(_toWrite); Monitor.Exit(_toWrite);
@@ -930,7 +946,9 @@ namespace SabreTools.Helper
firstSkip = -1; firstSkip = -1;
} }
else if (firstSkip == -1) else if (firstSkip == -1)
{
firstSkip = nextToWrite; firstSkip = nextToWrite;
}
continue; continue;
} }
@@ -959,12 +977,16 @@ namespace SabreTools.Helper
_toFill.Enqueue(workitem.index); _toFill.Enqueue(workitem.index);
// don't wait next time through // don't wait next time through
if (millisecondsToWait == -1) millisecondsToWait = 0; if (millisecondsToWait == -1)
{
millisecondsToWait = 0;
}
} }
} }
else else
{
nextToWrite = -1; nextToWrite = -1;
}
} while (nextToWrite >= 0); } while (nextToWrite >= 0);
} while (doAll && (_lastWritten != _latestCompressed)); } while (doAll && (_lastWritten != _latestCompressed));
@@ -972,8 +994,6 @@ namespace SabreTools.Helper
emitting = false; emitting = false;
} }
#if OLD #if OLD
private void _PerpetualWriterMethod(object state) private void _PerpetualWriterMethod(object state)
{ {
@@ -988,8 +1008,10 @@ namespace SabreTools.Helper
_sessionReset.WaitOne(); _sessionReset.WaitOne();
TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch _sessionReset.WaitOne(done) PWM"); 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"); TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch _sessionReset.Reset() PWM");
_sessionReset.Reset(); _sessionReset.Reset();
@@ -1144,9 +1166,6 @@ namespace SabreTools.Helper
} }
#endif #endif
private void _DeflateOne(Object wi) private void _DeflateOne(Object wi)
{ {
// compress one buffer // compress one buffer
@@ -1174,8 +1193,10 @@ namespace SabreTools.Helper
lock(_latestLock) lock(_latestLock)
{ {
if (workitem.ordinal > _latestCompressed) if (workitem.ordinal > _latestCompressed)
{
_latestCompressed = workitem.ordinal; _latestCompressed = workitem.ordinal;
} }
}
lock (_toWrite) lock (_toWrite)
{ {
_toWrite.Enqueue(workitem.index); _toWrite.Enqueue(workitem.index);
@@ -1188,13 +1209,12 @@ namespace SabreTools.Helper
{ {
// expose the exception to the main thread // expose the exception to the main thread
if (_pendingException != null) if (_pendingException != null)
{
_pendingException = exc1; _pendingException = exc1;
} }
} }
} }
}
private bool DeflateOneSegment(WorkItem workitem) private bool DeflateOneSegment(WorkItem workitem)
{ {
@@ -1221,7 +1241,6 @@ namespace SabreTools.Helper
return true; return true;
} }
[System.Diagnostics.ConditionalAttribute("Trace")] [System.Diagnostics.ConditionalAttribute("Trace")]
private void TraceOutput(TraceBits bits, string format, params object[] varParams) private void TraceOutput(TraceBits bits, string format, params object[] varParams)
{ {
@@ -1242,7 +1261,6 @@ namespace SabreTools.Helper
} }
} }
// used only when Trace is defined // used only when Trace is defined
[Flags] [Flags]
enum TraceBits : uint enum TraceBits : uint
@@ -1267,8 +1285,6 @@ namespace SabreTools.Helper
All = 0xffffffff, All = 0xffffffff,
} }
/// <summary> /// <summary>
/// Indicates whether the stream supports Seek operations. /// Indicates whether the stream supports Seek operations.
/// </summary> /// </summary>
@@ -1280,7 +1296,6 @@ namespace SabreTools.Helper
get { return false; } get { return false; }
} }
/// <summary> /// <summary>
/// Indicates whether the stream supports Read operations. /// Indicates whether the stream supports Read operations.
/// </summary> /// </summary>
@@ -1377,9 +1392,5 @@ namespace SabreTools.Helper
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
using System; using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper namespace SabreTools.Helper.Dats
{ {
[Serializable] [Serializable]
public class Archive : DatItem public class Archive : DatItem
@@ -26,50 +27,6 @@ namespace SabreTools.Helper
_itemType = ItemType.Archive; _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 #endregion
#region Comparision Methods #region Comparision Methods

View File

@@ -1,6 +1,7 @@
using System; using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper namespace SabreTools.Helper.Dats
{ {
[Serializable] [Serializable]
public class BiosSet : DatItem public class BiosSet : DatItem
@@ -52,54 +53,6 @@ namespace SabreTools.Helper
_default = @default; _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 #endregion
#region Comparision Methods #region Comparision Methods

View File

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

View File

@@ -1,8 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
using NaturalSort;
namespace SabreTools.Helper namespace SabreTools.Helper.Dats
{ {
[Serializable] [Serializable]
public abstract class DatItem : IEquatable<DatItem>, IComparable<DatItem> public abstract class DatItem : IEquatable<DatItem>, IComparable<DatItem>
@@ -15,18 +18,7 @@ namespace SabreTools.Helper
protected DupeType _dupeType; protected DupeType _dupeType;
// Machine information // Machine information
protected string _machineName; protected Machine _machine;
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;
// Software list information // Software list information
protected bool? _supported; protected bool? _supported;
@@ -66,65 +58,10 @@ namespace SabreTools.Helper
} }
// Machine information // Machine information
public string MachineName public Machine Machine
{ {
get { return _machineName; } get { return _machine; }
set { _machineName = value; } set { _machine = 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; }
} }
// Software list information // Software list information
@@ -258,7 +195,7 @@ namespace SabreTools.Helper
// If the duplicate is external already or should be, set it // 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.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; output = DupeType.External | DupeType.All;
} }
@@ -271,7 +208,7 @@ namespace SabreTools.Helper
// Otherwise, it's considered an internal dupe // Otherwise, it's considered an internal dupe
else 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; output = DupeType.Internal | DupeType.All;
} }
@@ -531,28 +468,28 @@ namespace SabreTools.Helper
{ {
if (gamename.StartsWith("*") && gamename.EndsWith("*")) if (gamename.StartsWith("*") && gamename.EndsWith("*"))
{ {
if (!MachineName.ToLowerInvariant().Contains(gamename.ToLowerInvariant().Replace("*", ""))) if (!Machine.Name.ToLowerInvariant().Contains(gamename.ToLowerInvariant().Replace("*", "")))
{ {
return false; return false;
} }
} }
else if (gamename.StartsWith("*")) else if (gamename.StartsWith("*"))
{ {
if (!MachineName.EndsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) if (!Machine.Name.EndsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase))
{ {
return false; return false;
} }
} }
else if (gamename.EndsWith("*")) else if (gamename.EndsWith("*"))
{ {
if (!MachineName.StartsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase)) if (!Machine.Name.StartsWith(gamename.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase))
{ {
return false; return false;
} }
} }
else else
{ {
if (!String.Equals(MachineName, gamename, StringComparison.InvariantCultureIgnoreCase)) if (!String.Equals(Machine.Name, gamename, StringComparison.InvariantCultureIgnoreCase))
{ {
return false; return false;
} }
@@ -860,7 +797,7 @@ namespace SabreTools.Helper
{ {
saveditem.SystemID = file.SystemID; saveditem.SystemID = file.SystemID;
saveditem.System = file.System; saveditem.System = file.System;
saveditem.MachineName = file.MachineName; saveditem.Machine.Name = file.Machine.Name;
saveditem.Name = file.Name; saveditem.Name = file.Name;
} }
@@ -869,7 +806,7 @@ namespace SabreTools.Helper
{ {
saveditem.SourceID = file.SourceID; saveditem.SourceID = file.SourceID;
saveditem.Source = file.Source; saveditem.Source = file.Source;
saveditem.MachineName = file.MachineName; saveditem.Machine.Name = file.Machine.Name;
saveditem.Name = file.Name; saveditem.Name = file.Name;
} }
@@ -916,7 +853,7 @@ namespace SabreTools.Helper
{ {
if (x.SourceID == y.SourceID) 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)) 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(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; return true;
} }

View File

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

View File

@@ -1,6 +1,7 @@
using System; using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper namespace SabreTools.Helper.Dats
{ {
[Serializable] [Serializable]
public class Disk : DatItem public class Disk : DatItem
@@ -65,56 +66,6 @@ namespace SabreTools.Helper
_itemStatus = itemStatus; _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 #endregion
#region Comparision Methods #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 System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper namespace SabreTools.Helper.Dats
{ {
[Serializable] [Serializable]
public class Release : DatItem public class Release : DatItem
@@ -72,58 +73,6 @@ namespace SabreTools.Helper
_default = @default; _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 #endregion
#region Comparision Methods #region Comparision Methods

View File

@@ -1,6 +1,7 @@
using System; using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper namespace SabreTools.Helper.Dats
{ {
[Serializable] [Serializable]
public class Rom : Disk public class Rom : Disk
@@ -55,8 +56,13 @@ namespace SabreTools.Helper
/// <param name="name"></param> /// <param name="name"></param>
/// <param name="machineName"></param> /// <param name="machineName"></param>
public Rom(string name, string machineName) : 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> /// <summary>
@@ -81,62 +87,6 @@ namespace SabreTools.Helper
_date = date; _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 #endregion
#region Comparision Methods #region Comparision Methods

View File

@@ -1,6 +1,7 @@
using System; using System;
using SabreTools.Helper.Data;
namespace SabreTools.Helper namespace SabreTools.Helper.Dats
{ {
[Serializable] [Serializable]
public class Sample : DatItem public class Sample : DatItem
@@ -26,50 +27,6 @@ namespace SabreTools.Helper
_itemType = ItemType.Sample; _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 #endregion
#region Comparision Methods #region Comparision Methods

View File

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

View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Xml; using System.Xml;
using SabreTools.Helper.Data;
using SabreTools.Helper.Tools;
namespace SabreTools.Helper namespace SabreTools.Helper
{ {
@@ -37,6 +39,9 @@ namespace SabreTools.Helper
#region Constructors #region Constructors
/// <summary>
/// Create an empty Skipper object
/// </summary>
public Skipper() public Skipper()
{ {
Name = ""; Name = "";
@@ -46,12 +51,16 @@ namespace SabreTools.Helper
SourceFile = ""; 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) public Skipper(string filename)
{ {
Rules = new List<SkipperRule>(); Rules = new List<SkipperRule>();
SourceFile = Path.GetFileNameWithoutExtension(filename); SourceFile = Path.GetFileNameWithoutExtension(filename);
Logger logger = new Logger(false, ""); Logger logger = new Logger();
XmlReader xtr = FileTools.GetXmlTextReader(filename, logger); XmlReader xtr = FileTools.GetXmlTextReader(filename, logger);
if (xtr == null) if (xtr == null)

View File

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

View File

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

View File

@@ -1,14 +1,20 @@
using SharpCompress.Archive; using System;
using SharpCompress.Archive.SevenZip;
using SharpCompress.Common;
using SharpCompress.Reader;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; 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 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) public static bool CopyFileBetweenArchives(string inputArchive, string outDir, string sourceEntryName, Rom destEntry, Logger logger)
{ {
string temp = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); 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); bool success = WriteToArchive(realName, outDir, destEntry, logger);
Directory.Delete(temp, true); Directory.Delete(temp, true);
return success; 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 #endregion
#region Extraction #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> /// <summary>
/// Attempt to extract a file as an archive /// Attempt to extract a file as an archive
/// </summary> /// </summary>
@@ -230,10 +200,10 @@ namespace SabreTools.Helper
/// <param name="tempDir">Temporary directory for archive extraction</param> /// <param name="tempDir">Temporary directory for archive extraction</param>
/// <param name="logger">Logger object for file and console output</param> /// <param name="logger">Logger object for file and console output</param>
/// <returns>Name of the extracted file, null on error</returns> /// <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 = ""; 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)); realEntry = Path.GetFullPath(Path.Combine(tempDir, realEntry));
if (!Directory.Exists(Path.GetDirectoryName(realEntry))) if (!Directory.Exists(Path.GetDirectoryName(realEntry)))
@@ -259,13 +229,13 @@ namespace SabreTools.Helper
} }
/// <summary> /// <summary>
/// Attempt to extract a file from an archive /// Attempt to extract a stream from an archive
/// </summary> /// </summary>
/// <param name="input">Name of the archive to be extracted</param> /// <param name="input">Name of the archive to be extracted</param>
/// <param name="entryName">Name of the entry 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> /// <param name="logger">Logger object for file and console output</param>
/// <returns>Name of the extracted file, null on error</returns> /// <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 // Set the real entry name
realEntry = ""; realEntry = "";
@@ -436,9 +406,13 @@ namespace SabreTools.Helper
{ {
Type = ItemType.Rom, Type = ItemType.Rom,
Name = newname, Name = newname,
MachineName = gamename,
Size = newsize, Size = newsize,
CRC = newcrc, CRC = newcrc,
Machine = new Machine
{
Name = gamename,
},
}); });
} }
} }
@@ -457,9 +431,13 @@ namespace SabreTools.Helper
{ {
Type = ItemType.Rom, Type = ItemType.Rom,
Name = reader.Entry.Key, Name = reader.Entry.Key,
MachineName = gamename,
Size = (size == 0 ? reader.Entry.Size : size), Size = (size == 0 ? reader.Entry.Size : size),
CRC = (crc == "" ? reader.Entry.Crc.ToString("X").ToLowerInvariant() : crc), 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 Rom rom = new Rom
{ {
Type = ItemType.Rom, Type = ItemType.Rom,
MachineName = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
Size = extractedsize, Size = extractedsize,
CRC = gzcrc.ToLowerInvariant(), CRC = gzcrc.ToLowerInvariant(),
MD5 = gzmd5.ToLowerInvariant(), MD5 = gzmd5.ToLowerInvariant(),
SHA1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(), SHA1 = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
Machine = new Machine
{
Name = Path.GetFileNameWithoutExtension(input).ToLowerInvariant(),
},
}; };
return rom; return rom;
@@ -735,11 +717,12 @@ namespace SabreTools.Helper
} }
/// <summary> /// <summary>
/// Read the information from an input 7z file /// (UNIMPLEMENTED) Read the information from an input 7z file
/// </summary> /// </summary>
/// <param name="input">Name of the input file to check</param> /// <param name="input">Name of the input file to check</param>
/// <param name="logger">Logger object for file and console output</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) public static void GetSevenZipFileInfo(string input, Logger logger)
{ {
BinaryReader br = new BinaryReader(File.OpenRead(input)); BinaryReader br = new BinaryReader(File.OpenRead(input));
@@ -803,7 +786,7 @@ namespace SabreTools.Helper
} }
// Get the output archive name from the first rebuild rom // 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 // Set internal variables
Stream writeStream = null; Stream writeStream = null;

View File

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

View File

@@ -1,6 +1,4 @@
using Mono.Data.Sqlite; using System;
using OCRC;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -8,8 +6,13 @@ using System.Security.Cryptography;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml; using System.Xml;
using System.Xml.Schema; 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 public static class FileTools
{ {
@@ -238,31 +241,6 @@ namespace SabreTools.Helper
return xtr; 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> /// <summary>
/// Add an aribtrary number of bytes to the inputted file /// Add an aribtrary number of bytes to the inputted file
/// </summary> /// </summary>
@@ -409,7 +387,7 @@ namespace SabreTools.Helper
logger.User("Creating reheadered file: " + logger.User("Creating reheadered file: " +
(outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file))) + sub); (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); (outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file))) + sub, header, string.Empty);
logger.User("Reheadered file created!"); logger.User("Reheadered file created!");
} }
@@ -833,7 +811,7 @@ namespace SabreTools.Helper
if (toFolder) if (toFolder)
{ {
// Copy file to output directory // Copy file to output directory
string gamedir = Path.Combine(outDir, found.MachineName); string gamedir = Path.Combine(outDir, found.Machine.Name);
if (!Directory.Exists(gamedir)) if (!Directory.Exists(gamedir))
{ {
Directory.CreateDirectory(gamedir); Directory.CreateDirectory(gamedir);
@@ -900,7 +878,7 @@ namespace SabreTools.Helper
if (toFolder) if (toFolder)
{ {
// Copy file to output directory // Copy file to output directory
string gamedir = Path.Combine(outDir, found.MachineName); string gamedir = Path.Combine(outDir, found.Machine.Name);
if (!Directory.Exists(gamedir)) if (!Directory.Exists(gamedir))
{ {
Directory.CreateDirectory(gamedir); Directory.CreateDirectory(gamedir);
@@ -949,7 +927,7 @@ namespace SabreTools.Helper
if (toFolder) if (toFolder)
{ {
// Copy file to output directory // Copy file to output directory
string gamedir = Path.Combine(outDir, found.MachineName); string gamedir = Path.Combine(outDir, found.Machine.Name);
if (!Directory.Exists(gamedir)) if (!Directory.Exists(gamedir))
{ {
Directory.CreateDirectory(gamedir); Directory.CreateDirectory(gamedir);
@@ -1025,10 +1003,10 @@ namespace SabreTools.Helper
{ {
// Copy file to output directory // Copy file to output directory
logger.Verbose("Rebuilding file '" + Path.GetFileName(rom.Name) + "' to '" + found.Name + "'"); 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)) if (File.Exists(outfile))
{ {
string gamedir = Path.Combine(outDir, found.MachineName); string gamedir = Path.Combine(outDir, found.Machine.Name);
if (!Directory.Exists(gamedir)) if (!Directory.Exists(gamedir))
{ {
Directory.CreateDirectory(gamedir); Directory.CreateDirectory(gamedir);
@@ -1048,7 +1026,7 @@ namespace SabreTools.Helper
if (Build.MonoEnvironment || tgz) 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 (File.Exists(outfile))
{ {
if (tgz) if (tgz)
@@ -1333,7 +1311,7 @@ namespace SabreTools.Helper
} }
// Otherwise, set the machine name as the full path to the file // 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 // Add the rom information to the Dat
string key = rom.Size + "-" + rom.CRC; string key = rom.Size + "-" + rom.CRC;
@@ -1362,7 +1340,7 @@ namespace SabreTools.Helper
rule.TransformStream(input, output, logger, false, true); rule.TransformStream(input, output, logger, false, true);
Rom romNH = FileTools.GetStreamInfo(output, output.Length); Rom romNH = FileTools.GetStreamInfo(output, output.Length);
romNH.Name = "HEAD::" + rom.Name; romNH.Name = "HEAD::" + rom.Name;
romNH.MachineName = rom.MachineName; romNH.Machine.Name = rom.Machine.Name;
// Add the rom information to the Dat // Add the rom information to the Dat
key = romNH.Size + "-" + romNH.CRC; key = romNH.Size + "-" + romNH.CRC;
@@ -1564,64 +1542,6 @@ namespace SabreTools.Helper
#region Stream Manipulation #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> /// <summary>
/// Add an aribtrary number of bytes to the inputted stream /// Add an aribtrary number of bytes to the inputted stream
/// </summary> /// </summary>

View File

@@ -5,147 +5,16 @@ using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Web; using System.Web;
using SabreTools.Helper.Data;
using SabreTools.Helper.Dats;
namespace SabreTools.Helper namespace SabreTools.Helper.Tools
{ {
/// <summary> /// <summary>
/// Include character normalization and replacement mappings /// Include character normalization and replacement mappings
/// </summary> /// </summary>
public static class Style 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 #region DAT Cleaning
/// <summary> /// <summary>
@@ -324,9 +193,9 @@ namespace SabreTools.Helper
public static string CleanGameName(string game) public static string CleanGameName(string game)
{ {
///Run the name through the filters to make sure that it's correct ///Run the name through the filters to make sure that it's correct
game = Style.NormalizeChars(game); game = NormalizeChars(game);
game = Style.RussianToLatin(game); game = RussianToLatin(game);
game = Style.SearchPattern(game); game = SearchPattern(game);
game = new Regex(@"(([[(].*[\)\]] )?([^([]+))").Match(game).Groups[1].Value; game = new Regex(@"(([[(].*[\)\]] )?([^([]+))").Match(game).Groups[1].Value;
game = game.TrimStart().TrimEnd(); game = game.TrimStart().TrimEnd();
@@ -370,52 +239,6 @@ namespace SabreTools.Helper
return hash; 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 #endregion
#region String Manipulation #region String Manipulation
@@ -462,6 +285,139 @@ namespace SabreTools.Helper
#endregion #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 #region Externally sourced methods
/// <summary> /// <summary>

View File

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

View File

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

View File

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

View File

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