[SimpleSort] Bring stability improvements to SimpleSort and the classes/methods it relies on

This commit is contained in:
Matt Nadareski
2016-09-18 12:29:33 -07:00
parent 4f43aecea2
commit 7f08c80341
4 changed files with 46 additions and 30 deletions

View File

@@ -406,7 +406,6 @@ namespace SabreTools.Helper
#endregion #endregion
// HERE BE DRAGONS
#region Find all files to rebuild and bucket by game #region Find all files to rebuild and bucket by game
// Create a dictionary of from/to Rom mappings // Create a dictionary of from/to Rom mappings

View File

@@ -629,7 +629,7 @@ namespace SabreTools.Helper
_zipFileInfo = new FileInfo(filename); _zipFileInfo = new FileInfo(filename);
// Now try to open the file // Now try to open the file
_zipstream = File.OpenWrite(filename); _zipstream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
ZipOpen = ZipOpenType.OpenWrite; ZipOpen = ZipOpenType.OpenWrite;
return ZipReturn.ZipGood; return ZipReturn.ZipGood;
} }

View File

@@ -350,7 +350,7 @@ namespace SabreTools.Helper
foreach (SkipperRule rule in skipper.Rules) foreach (SkipperRule rule in skipper.Rules)
{ {
// Always reset the stream back to the original place // Always reset the stream back to the original place
br.BaseStream.Seek(0, SeekOrigin.Begin); input.Seek(0, SeekOrigin.Begin);
// For each rule, make sure it passes each test // For each rule, make sure it passes each test
bool success = true; bool success = true;
@@ -363,15 +363,15 @@ namespace SabreTools.Helper
// First seek to the correct position // First seek to the correct position
if (test.Offset == null) if (test.Offset == null)
{ {
br.BaseStream.Seek(0, SeekOrigin.End); input.Seek(0, SeekOrigin.End);
} }
else if (test.Offset > 0 && test.Offset <= br.BaseStream.Length) else if (test.Offset > 0 && test.Offset <= input.Length)
{ {
br.BaseStream.Seek((long)test.Offset, SeekOrigin.Begin); input.Seek((long)test.Offset, SeekOrigin.Begin);
} }
else if (test.Offset < 0 && Math.Abs((long)test.Offset) <= br.BaseStream.Length) else if (test.Offset < 0 && Math.Abs((long)test.Offset) <= input.Length)
{ {
br.BaseStream.Seek((long)test.Offset, SeekOrigin.End); input.Seek((long)test.Offset, SeekOrigin.End);
} }
// Then read and compare bytewise // Then read and compare bytewise
@@ -402,15 +402,15 @@ namespace SabreTools.Helper
// First seek to the correct position // First seek to the correct position
if (test.Offset == null) if (test.Offset == null)
{ {
br.BaseStream.Seek(0, SeekOrigin.End); input.Seek(0, SeekOrigin.End);
} }
else if (test.Offset > 0 && test.Offset <= br.BaseStream.Length) else if (test.Offset > 0 && test.Offset <= input.Length)
{ {
br.BaseStream.Seek((long)test.Offset, SeekOrigin.Begin); input.Seek((long)test.Offset, SeekOrigin.Begin);
} }
else if (test.Offset < 0 && Math.Abs((long)test.Offset) <= br.BaseStream.Length) else if (test.Offset < 0 && Math.Abs((long)test.Offset) <= input.Length)
{ {
br.BaseStream.Seek((long)test.Offset, SeekOrigin.End); input.Seek((long)test.Offset, SeekOrigin.End);
} }
result = true; result = true;
@@ -446,7 +446,7 @@ namespace SabreTools.Helper
break; break;
case HeaderSkipTest.File: case HeaderSkipTest.File:
// First get the file size from stream // First get the file size from stream
long size = br.BaseStream.Length; long size = input.Length;
// If we have a null size, check that the size is a power of 2 // If we have a null size, check that the size is a power of 2
result = true; result = true;
@@ -474,16 +474,16 @@ namespace SabreTools.Helper
} }
} }
// If we're not keeping the stream open, dispose of the binary reader
if (!keepOpen)
{
br.Close();
br.Dispose();
}
// If we still have a success, then return this rule // If we still have a success, then return this rule
if (success) if (success)
{ {
// If we're not keeping the stream open, dispose of the binary reader
if (!keepOpen)
{
input.Close();
input.Dispose();
}
logger.User(" Matching rule found!"); logger.User(" Matching rule found!");
return rule; return rule;
} }
@@ -491,6 +491,13 @@ namespace SabreTools.Helper
} }
} }
// If we're not keeping the stream open, dispose of the binary reader
if (!keepOpen)
{
input.Close();
input.Dispose();
}
// If we have a blank rule, inform the user // If we have a blank rule, inform the user
if (skipperRule.Tests == null) if (skipperRule.Tests == null)
{ {
@@ -526,7 +533,7 @@ namespace SabreTools.Helper
} }
logger.User("Attempting to apply rule to '" + input + "'"); logger.User("Attempting to apply rule to '" + input + "'");
success = TransformStream(File.OpenRead(input), File.OpenWrite(output), rule, logger); success = TransformStream(File.Open(input, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), File.OpenWrite(output), rule, logger);
// If the output file has size 0, delete it // If the output file has size 0, delete it
if (new FileInfo(output).Length == 0) if (new FileInfo(output).Length == 0)
@@ -534,6 +541,7 @@ namespace SabreTools.Helper
try try
{ {
File.Delete(output); File.Delete(output);
success = false;
} }
catch catch
{ {
@@ -582,17 +590,17 @@ namespace SabreTools.Helper
{ {
success = false; success = false;
} }
else if (Math.Abs((long)rule.StartOffset) > br.BaseStream.Length) else if (Math.Abs((long)rule.StartOffset) > input.Length)
{ {
success = false; success = false;
} }
else if (rule.StartOffset > 0) else if (rule.StartOffset > 0)
{ {
br.BaseStream.Seek((long)rule.StartOffset, SeekOrigin.Begin); input.Seek((long)rule.StartOffset, SeekOrigin.Begin);
} }
else if (rule.StartOffset < 0) else if (rule.StartOffset < 0)
{ {
br.BaseStream.Seek((long)rule.StartOffset, SeekOrigin.End); input.Seek((long)rule.StartOffset, SeekOrigin.End);
} }
// Then read and apply the operation as you go // Then read and apply the operation as you go
@@ -600,8 +608,8 @@ namespace SabreTools.Helper
{ {
byte[] buffer = new byte[4]; byte[] buffer = new byte[4];
int pos = 0; int pos = 0;
while (br.BaseStream.Position < (rule.EndOffset != null ? rule.EndOffset : br.BaseStream.Length) while (input.Position < (rule.EndOffset != null ? rule.EndOffset : input.Length)
&& br.BaseStream.Position < br.BaseStream.Length) && input.Position < input.Length)
{ {
byte b = br.ReadByte(); byte b = br.ReadByte();
switch (rule.Operation) switch (rule.Operation)

View File

@@ -211,7 +211,7 @@ namespace SabreTools.Helper
Rom rom = roms[inputIndexMap[key]]; Rom rom = roms[inputIndexMap[key]];
// Open the input file for reading // Open the input file for reading
using (Stream readStream = File.OpenRead(inputFile)) using (Stream readStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{ {
ulong streamSize = (ulong)(new FileInfo(inputFile).Length); ulong streamSize = (ulong)(new FileInfo(inputFile).Length);
zipReturn = zipFile.OpenWriteStream(false, true, rom.Name, streamSize, CompressionMethod.Deflated, out writeStream); zipReturn = zipFile.OpenWriteStream(false, true, rom.Name, streamSize, CompressionMethod.Deflated, out writeStream);
@@ -278,7 +278,7 @@ namespace SabreTools.Helper
if (index < 0) if (index < 0)
{ {
// Open the input file for reading // Open the input file for reading
Stream freadStream = File.OpenRead(key); Stream freadStream = File.Open(key, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
ulong istreamSize = (ulong)(new FileInfo(key).Length); ulong istreamSize = (ulong)(new FileInfo(key).Length);
zipFile.OpenWriteStream(false, true, roms[-index - 1].Name, istreamSize, CompressionMethod.Deflated, out writeStream); zipFile.OpenWriteStream(false, true, roms[-index - 1].Name, istreamSize, CompressionMethod.Deflated, out writeStream);
@@ -710,8 +710,9 @@ namespace SabreTools.Helper
/// <param name="noMD5">True if MD5 hashes should not be calculated, false otherwise (default)</param> /// <param name="noMD5">True if MD5 hashes should not be calculated, false otherwise (default)</param>
/// <param name="noSHA1">True if SHA-1 hashes should not be calcluated, false otherwise (default)</param> /// <param name="noSHA1">True if SHA-1 hashes should not be calcluated, false otherwise (default)</param>
/// <param name="offset">Set a >0 number for getting hash for part of the file, 0 otherwise (default)</param> /// <param name="offset">Set a >0 number for getting hash for part of the file, 0 otherwise (default)</param>
/// <param name="keepReadOpen">True if the underlying read stream should be kept open, false otherwise</param>
/// <returns>Populated RomData object if success, empty one on error</returns> /// <returns>Populated RomData object if success, empty one on error</returns>
public static Rom GetSingleStreamInfo(Stream input, bool noMD5 = false, bool noSHA1 = false, long offset = 0) public static Rom GetSingleStreamInfo(Stream input, bool noMD5 = false, bool noSHA1 = false, long offset = 0, bool keepReadOpen = false)
{ {
// If we have a negative offset, zero it out since we don't support it yet // If we have a negative offset, zero it out since we don't support it yet
if (offset < 0) if (offset < 0)
@@ -777,6 +778,14 @@ namespace SabreTools.Helper
{ {
return new Rom(); return new Rom();
} }
finally
{
if (!keepReadOpen)
{
input.Close();
input.Dispose();
}
}
return rom; return rom;
} }