[DatFile, Machine] Fix description-to-name

This commit is contained in:
Matt Nadareski
2017-07-17 13:47:51 -07:00
parent a5ef8aeb0e
commit e4776cb43a
3 changed files with 89 additions and 74 deletions

View File

@@ -5,7 +5,7 @@ using SabreTools.Library.Data;
namespace SabreTools.Library.Dats
{
public struct Machine
public class Machine
{
#region Protected instance variables
@@ -35,6 +35,28 @@ namespace SabreTools.Library.Dats
#region Constructors
/// <summary>
/// Create a new Machine object
/// </summary>
public Machine()
{
Name = null;
Comment = null;
Description = null;
Year = null;
Manufacturer = null;
RomOf = null;
CloneOf = null;
SampleOf = null;
SourceFile = null;
Runnable = null;
Board = null;
RebuildTo = null;
Devices = null;
MachineType = MachineType.NULL;
_guid = new Guid();
}
/// <summary>
/// Create a new Machine object with the included information
/// </summary>
@@ -63,54 +85,40 @@ namespace SabreTools.Library.Dats
#region Equality comparerers
/// <summary>
/// Override the equality comparer
/// </summary>
public static bool operator ==(Machine a, Machine b)
{
return (a.Name == b.Name
&& a.Comment == b.Comment
&& a.Description == b.Description
&& a.Year == b.Year
&& a.Manufacturer == b.Manufacturer
&& a.RomOf == b.RomOf
&& a.CloneOf == b.CloneOf
&& a.SampleOf == b.SampleOf
&& a.SourceFile == b.SourceFile
&& a.Runnable == b.Runnable
&& a.Board == b.Board
&& a.RebuildTo == b.RebuildTo
&& a.Devices == b.Devices
&& a.MachineType == b.MachineType);
}
/// <summary>
/// Override the inequality comparer
/// </summary>
public static bool operator !=(Machine a, Machine b)
{
return !(a == b);
}
/// <summary>
/// Override the Equals method
/// </summary>
public override bool Equals(object o)
{
if (o.GetType() != typeof(Machine))
if (this == null && o == null)
{
return true;
}
else if (this == null || o == null)
{
return false;
}
else if (o.GetType() != typeof(Machine))
{
return false;
}
return this == (Machine)o;
}
Machine b = (Machine)o;
/// <summary>
/// Override the GetHashCode method
/// </summary>
public override int GetHashCode()
{
return OCRC.OptimizedCRC.Compute(_guid.ToByteArray());
return (this.Name == b.Name
&& this.Comment == b.Comment
&& this.Description == b.Description
&& this.Year == b.Year
&& this.Manufacturer == b.Manufacturer
&& this.RomOf == b.RomOf
&& this.CloneOf == b.CloneOf
&& this.SampleOf == b.SampleOf
&& this.SourceFile == b.SourceFile
&& this.Runnable == b.Runnable
&& this.Board == b.Board
&& this.RebuildTo == b.RebuildTo
&& this.Devices == b.Devices
&& this.MachineType == b.MachineType);
}
#endregion

View File

@@ -604,7 +604,7 @@ namespace SabreTools.Library.Dats
innerDatdata.Filter(filter, trim, single, root);
// Try to output the file
innerDatdata.WriteToFile((realOutDir == Environment.CurrentDirectory ? Path.GetDirectoryName(inputFileName) : realOutDir), overwrite: (realOutDir != Environment.CurrentDirectory));
innerDatdata.WriteToFile((realOutDir == Environment.CurrentDirectory ? Path.GetDirectoryName(inputFileName) : realOutDir), overwrite: (realOutDir != Environment.CurrentDirectory));
}
else if (Directory.Exists(inputFileName))
{

View File

@@ -181,48 +181,55 @@ namespace SabreTools.Library.Dats
/// <param name="root">String representing root directory to compare against for length calculation</param>
public void Filter(Filter filter, bool single, bool trim, string root)
{
// Loop over every key in the dictionary
List<string> keys = Keys.ToList();
Parallel.ForEach(keys, key =>
try
{
// For every item in the current key
List<DatItem> items = this[key];
List<DatItem> newitems = new List<DatItem>();
foreach (DatItem item in items)
// Loop over every key in the dictionary
List<string> keys = Keys.ToList();
foreach (string key in keys)
{
// If the rom passes the filter, include it
if (filter.ItemPasses(item))
// For every item in the current key
List<DatItem> items = this[key];
List<DatItem> newitems = new List<DatItem>();
foreach (DatItem item in items)
{
// If we are in single game mode, rename all games
if (single)
// If the rom passes the filter, include it
if (filter.ItemPasses(item))
{
item.Machine.UpdateName("!");
}
// If we are in NTFS trim mode, trim the game name
if (trim)
{
// Windows max name length is 260
int usableLength = 260 - item.Machine.Name.Length - root.Length;
if (item.Name.Length > usableLength)
// If we are in single game mode, rename all games
if (single)
{
string ext = Path.GetExtension(item.Name);
item.Name = item.Name.Substring(0, usableLength - ext.Length);
item.Name += ext;
item.Machine.UpdateName("!");
}
// If we are in NTFS trim mode, trim the game name
if (trim)
{
// Windows max name length is 260
int usableLength = 260 - item.Machine.Name.Length - root.Length;
if (item.Name.Length > usableLength)
{
string ext = Path.GetExtension(item.Name);
item.Name = item.Name.Substring(0, usableLength - ext.Length);
item.Name += ext;
}
}
// Lock the list and add the item back
lock (newitems)
{
newitems.Add(item);
}
}
// Lock the list and add the item back
lock (newitems)
{
newitems.Add(item);
}
}
}
Remove(key);
AddRange(key, newitems);
});
Remove(key);
AddRange(key, newitems);
}
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
}
}
/// <summary>
@@ -281,7 +288,7 @@ namespace SabreTools.Library.Dats
}
// Add the new item to the output list
items.Add(item);
newItems.Add(item);
}
// Replace the old list of roms with the new one