[DatFile] Make Machine a struct for memory usage

This commit is contained in:
Matt Nadareski
2017-06-16 16:24:26 -07:00
parent 1500875822
commit b59bcd7c20
17 changed files with 519 additions and 203 deletions

View File

@@ -28,7 +28,7 @@ namespace SabreTools.Library.Dats
Type = this.Type,
Dupe = this.Dupe,
Machine = (Machine)this.Machine.Clone(),
Machine = this.Machine,
Supported = this.Supported,
Publisher = this.Publisher,

View File

@@ -50,7 +50,7 @@ namespace SabreTools.Library.Dats
Type = this.Type,
Dupe = this.Dupe,
Machine = (Machine)this.Machine.Clone(),
Machine = this.Machine,
Supported = this.Supported,
Publisher = this.Publisher,

View File

@@ -0,0 +1,217 @@
using System.Collections.Generic;
using SabreTools.Library.Data;
namespace SabreTools.Library.Dats
{
public struct DatHeader
{
#region Private instance variables
// Data common to most DAT types
private string _fileName;
private string _name;
private string _description;
private string _rootDir;
private string _category;
private string _version;
private string _date;
private string _author;
private string _email;
private string _homepage;
private string _url;
private string _comment;
private string _header;
private string _type; // Generally only used for SuperDAT
private ForceMerging _forceMerging;
private ForceNodump _forceNodump;
private ForcePacking _forcePacking;
private DatFormat _datFormat;
private bool _excludeOf;
private bool _mergeRoms;
private Hash _stripHash;
private bool _oneGameOneRegion;
private List<string> _regions;
// Data specific to the Miss DAT type
private bool _useGame;
private string _prefix;
private string _postfix;
private bool _quotes;
private string _repExt;
private string _addExt;
private bool _remExt;
private bool _gameName;
private bool _romba;
#endregion
#region Publicly facing variables
// Data common to most DAT types
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
public string RootDir
{
get { return _rootDir; }
set { _rootDir = value; }
}
public string Category
{
get { return _category; }
set { _category = value; }
}
public string Version
{
get { return _version; }
set { _version = value; }
}
public string Date
{
get { return _date; }
set { _date = value; }
}
public string Author
{
get { return _author; }
set { _author = value; }
}
public string Email
{
get { return _email; }
set { _email = value; }
}
public string Homepage
{
get { return _homepage; }
set { _homepage = value; }
}
public string Url
{
get { return _url; }
set { _url = value; }
}
public string Comment
{
get { return _comment; }
set { _comment = value; }
}
public string Header
{
get { return _header; }
set { _header = value; }
}
public string Type // Generally only used for SuperDAT
{
get { return _type; }
set { _type = value; }
}
public ForceMerging ForceMerging
{
get { return _forceMerging; }
set { _forceMerging = value; }
}
public ForceNodump ForceNodump
{
get { return _forceNodump; }
set { _forceNodump = value; }
}
public ForcePacking ForcePacking
{
get { return _forcePacking; }
set { _forcePacking = value; }
}
public DatFormat DatFormat
{
get { return _datFormat; }
set { _datFormat = value; }
}
public bool ExcludeOf
{
get { return _excludeOf; }
set { _excludeOf = value; }
}
public bool MergeRoms
{
get { return _mergeRoms; }
set { _mergeRoms = value; }
}
public Hash StripHash
{
get { return _stripHash; }
set { _stripHash = value; }
}
public bool OneGameOneRegion
{
get { return _oneGameOneRegion; }
set { _oneGameOneRegion = value; }
}
public List<string> Regions
{
get { return _regions; }
set { _regions = value; }
}
// Data specific to the Miss DAT type
public bool UseGame
{
get { return _useGame; }
set { _useGame = value; }
}
public string Prefix
{
get { return _prefix; }
set { _prefix = value; }
}
public string Postfix
{
get { return _postfix; }
set { _postfix = value; }
}
public bool Quotes
{
get { return _quotes; }
set { _quotes = value; }
}
public string RepExt
{
get { return _repExt; }
set { _repExt = value; }
}
public string AddExt
{
get { return _addExt; }
set { _addExt = value; }
}
public bool RemExt
{
get { return _remExt; }
set { _remExt = value; }
}
public bool GameName
{
get { return _gameName; }
set { _gameName = value; }
}
public bool Romba
{
get { return _romba; }
set { _romba = value; }
}
#endregion
}
}

View File

@@ -608,7 +608,7 @@ namespace SabreTools.Library.Dats
{
saveditem.SystemID = file.SystemID;
saveditem.System = file.System;
saveditem.Machine = (Machine)file.Machine.Clone();
saveditem.Machine = file.Machine;
saveditem.Name = file.Name;
}
@@ -617,7 +617,7 @@ namespace SabreTools.Library.Dats
{
saveditem.SourceID = file.SourceID;
saveditem.Source = file.Source;
saveditem.Machine = (Machine)file.Machine.Clone();
saveditem.Machine = file.Machine;
saveditem.Name = file.Name;
}

View File

@@ -79,7 +79,7 @@ namespace SabreTools.Library.Dats
Type = this.Type,
Dupe = this.Dupe,
Machine = (Machine)this.Machine.Clone(),
Machine = this.Machine,
Supported = this.Supported,
Publisher = this.Publisher,

View File

@@ -5,25 +5,26 @@ using SabreTools.Library.Data;
namespace SabreTools.Library.Dats
{
public class Machine : ICloneable
public struct 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? _runnable;
protected string _board;
protected string _rebuildTo;
protected List<string> _devices;
protected MachineType _machineType;
private string _name;
private string _comment;
private string _description;
private string _year;
private string _manufacturer;
private string _romOf;
private string _cloneOf;
private string _sampleOf;
private string _sourceFile;
private bool? _runnable;
private string _board;
private string _rebuildTo;
private List<string> _devices;
private MachineType _machineType;
private Guid _guid;
#endregion
@@ -105,16 +106,6 @@ namespace SabreTools.Library.Dats
#region Constructors
/// <summary>
/// Create a default, empty Machine object
/// </summary>
public Machine()
{
_name = "";
_description = "";
_runnable = null;
}
/// <summary>
/// Create a new Machine object with the included information
/// </summary>
@@ -123,33 +114,134 @@ namespace SabreTools.Library.Dats
public Machine(string name, string description)
{
_name = name;
_comment = null;
_description = description;
_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();
}
#endregion
#region Cloneing
#region Equality comparerers
public object Clone()
/// <summary>
/// Override the equality comparer
/// </summary>
public static bool operator ==(Machine a, Machine b)
{
return new Machine()
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)
{
Name = _name,
Comment = _comment,
Description = _description,
Year = _year,
Manufacturer = _manufacturer,
RomOf = _romOf,
CloneOf = _cloneOf,
SampleOf = _sampleOf,
SourceFile = _sourceFile,
Runnable = _runnable,
Board = _board,
RebuildTo = _rebuildTo,
Devices = _devices,
MachineType = _machineType,
};
return !(a == b);
}
/// <summary>
/// Override the Equals method
/// </summary>
public override bool Equals(object o)
{
if (o.GetType() != typeof(Machine))
{
return false;
}
return this == (Machine)o;
}
/// <summary>
/// Override the GetHashCode method
/// </summary>
public override int GetHashCode()
{
return OCRC.OptimizedCRC.Compute(_guid.ToByteArray());
}
#endregion
#region Update fields
/// <summary>
/// Append a string to the description
/// </summary>
public void AppendDescription(string append)
{
UpdateDescription(this.Description + append);
}
/// <summary>
/// Append a string to the name
/// </summary>
public void AppendName(string append)
{
UpdateName(this.Name + append);
}
/// <summary>
/// Update the cloneof
/// </summary>
public void UpdateCloneOf(string update)
{
_cloneOf = update;
}
/// <summary>
/// Update the description
/// </summary>
public void UpdateDescription(string update)
{
_description = update;
}
/// <summary>
/// Update the romof
/// </summary>
public void UpdateRomOf(string update)
{
_romOf = update;
}
/// <summary>
/// Update the sampleof
/// </summary>
public void UpdateSampleOf(string update)
{
_sampleOf = update;
}
/// <summary>
/// Update the name
/// </summary>
public void UpdateName(string update)
{
_name = update;
}
#endregion

View File

@@ -251,8 +251,8 @@ namespace SabreTools.Library.Dats
intDat.WriteToFile(interOutDir);
}
// Due to possible memory requirements, each DAT, after written, will be nulled out
intDat = null;
// Due to possible memory requirements, we force a garbage collection
GC.Collect();
});
}
@@ -467,7 +467,7 @@ namespace SabreTools.Library.Dats
if ((diff & DiffMode.NoDupes) != 0)
{
DatItem newrom = item.Clone() as DatItem;
newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
newrom.Machine.AppendName(" (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")");
outerDiffData.Add(key, newrom);
}
@@ -480,7 +480,7 @@ namespace SabreTools.Library.Dats
if ((item.Dupe & DupeType.External) != 0)
{
DatItem newrom = item.Clone() as DatItem;
newrom.Machine.Name += " (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")";
newrom.Machine.AppendName(" (" + Path.GetFileNameWithoutExtension(inputs[newrom.SystemID].Split('¬')[0]) + ")");
dupeData.Add(key, newrom);
}
@@ -549,9 +549,9 @@ namespace SabreTools.Library.Dats
rootpath += (rootpath == "" ? "" : Path.DirectorySeparatorChar.ToString());
filename = filename.Remove(0, rootpath.Length);
newItem.Machine.Name = Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
newItem.Machine.UpdateName(Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename) + Path.DirectorySeparatorChar
+ newItem.Machine.Name;
+ newItem.Machine.Name);
newItems.Add(newItem);
}

View File

@@ -402,7 +402,7 @@ namespace SabreTools.Library.Dats
// Update rom information
datItem.Name = romname;
if (datItem.Machine == null)
if (datItem.Machine == default(Machine))
{
datItem.Machine = new Machine
{
@@ -412,8 +412,8 @@ namespace SabreTools.Library.Dats
}
else
{
datItem.Machine.Name = gamename;
datItem.Machine.Description = gamename;
datItem.Machine.UpdateName(gamename);
datItem.Machine.UpdateDescription(gamename);
}
// Add the file information to the DAT

View File

@@ -36,6 +36,12 @@ namespace SabreTools.Library.Dats
return;
}
// If we have a situation where there's no dictionary or no keys at all, we skip
if (_files == null || _files.Count == 0)
{
return;
}
// Set the sorted type
_sortedBy = bucketBy;
@@ -190,7 +196,7 @@ namespace SabreTools.Library.Dats
// If we are in single game mode, rename all games
if (single)
{
item.Machine.Name = "!";
item.Machine.UpdateName("!");
}
// If we are in NTFS trim mode, trim the game name
@@ -253,25 +259,25 @@ namespace SabreTools.Library.Dats
// Update machine name
if (!String.IsNullOrEmpty(item.Machine.Name) && mapping.ContainsKey(item.Machine.Name))
{
item.Machine.Name = mapping[item.Machine.Name];
item.Machine.UpdateName(mapping[item.Machine.Name]);
}
// Update cloneof
if (!String.IsNullOrEmpty(item.Machine.CloneOf) && mapping.ContainsKey(item.Machine.CloneOf))
{
item.Machine.CloneOf = mapping[item.Machine.CloneOf];
item.Machine.UpdateCloneOf(mapping[item.Machine.CloneOf]);
}
// Update romof
if (!String.IsNullOrEmpty(item.Machine.RomOf) && mapping.ContainsKey(item.Machine.RomOf))
{
item.Machine.RomOf = mapping[item.Machine.RomOf];
item.Machine.UpdateRomOf(mapping[item.Machine.RomOf]);
}
// Update sampleof
if (!String.IsNullOrEmpty(item.Machine.SampleOf) && mapping.ContainsKey(item.Machine.SampleOf))
{
item.Machine.SampleOf = mapping[item.Machine.SampleOf];
item.Machine.UpdateSampleOf(mapping[item.Machine.SampleOf]);
}
// Add the new item to the output list
@@ -759,7 +765,7 @@ namespace SabreTools.Library.Dats
string romof = this[parent][0].Machine.RomOf;
foreach (DatItem item in items)
{
item.Machine.RomOf = romof;
item.Machine.UpdateRomOf(romof);
}
}
}
@@ -1025,7 +1031,7 @@ namespace SabreTools.Library.Dats
string romof = this[parent][0].Machine.RomOf;
foreach (DatItem item in items)
{
item.Machine.RomOf = romof;
item.Machine.UpdateRomOf(romof);
}
}
}
@@ -1041,8 +1047,8 @@ namespace SabreTools.Library.Dats
List<DatItem> items = this[game];
foreach (DatItem item in items)
{
item.Machine.CloneOf = null;
item.Machine.RomOf = null;
item.Machine.UpdateCloneOf(null);
item.Machine.UpdateRomOf(null);
}
}
}

View File

@@ -3018,14 +3018,14 @@ namespace SabreTools.Library.Dats
}
// If we're in cleaning mode, sanitize the game name
item.Machine.Name = (clean ? Style.CleanGameName(item.Machine.Name) : item.Machine.Name);
item.Machine.UpdateName((clean ? Style.CleanGameName(item.Machine.Name) : item.Machine.Name));
// If we're stripping unicode characters, do so from all relevant things
if (remUnicode)
{
item.Name = Style.RemoveUnicodeCharacters(item.Name);
item.Machine.Name = Style.RemoveUnicodeCharacters(item.Machine.Name);
item.Machine.Description = Style.RemoveUnicodeCharacters(item.Machine.Description);
item.Machine.UpdateName(Style.RemoveUnicodeCharacters(item.Machine.Name));
item.Machine.UpdateDescription(Style.RemoveUnicodeCharacters(item.Machine.Description));
}
// If we have a Rom or a Disk, clean the hash data

View File

@@ -645,8 +645,8 @@ namespace SabreTools.Library.Dats
// If we are coming from an archive, set the correct machine name
if (machinename != null)
{
item.Machine.Name = machinename;
item.Machine.Description = machinename;
item.Machine.UpdateName(machinename);
item.Machine.UpdateDescription(machinename);
}
Globals.Logger.User("No matches found for '" + Style.GetFileName(rom.Name) + "', rebuilding accordingly from inverse flag...");

View File

@@ -435,8 +435,8 @@ namespace SabreTools.Library.Dats
// Clean the input list and set all games to be pathless
List<DatItem> items = this[key];
items.ForEach(item => item.Machine.Name = Style.GetFileName(item.Machine.Name));
items.ForEach(item => item.Machine.Description = Style.GetFileName(item.Machine.Description));
items.ForEach(item => item.Machine.UpdateName(Style.GetFileName(item.Machine.Name)));
items.ForEach(item => item.Machine.UpdateDescription(Style.GetFileName(item.Machine.Description)));
// Now add the game to the output DAT
tempDat.AddRange(key, items);

View File

@@ -474,7 +474,7 @@ namespace SabreTools.Library.Dats
// No game should start with a path separator
if (rom.Machine.Name.StartsWith(Path.DirectorySeparatorChar.ToString()))
{
rom.Machine.Name = rom.Machine.Name.Substring(1);
rom.Machine.UpdateName(rom.Machine.Name.Substring(1));
}
string state = "";

View File

@@ -67,7 +67,7 @@ namespace SabreTools.Library.Dats
Type = this.Type,
Dupe = this.Dupe,
Machine = (Machine)this.Machine.Clone(),
Machine = this.Machine,
Supported = this.Supported,
Publisher = this.Publisher,

View File

@@ -107,7 +107,7 @@ namespace SabreTools.Library.Dats
Type = this.Type,
Dupe = this.Dupe,
Machine = (Machine)this.Machine.Clone(),
Machine = this.Machine,
Supported = this.Supported,
Publisher = this.Publisher,

View File

@@ -29,7 +29,7 @@ namespace SabreTools.Library.Dats
Type = this.Type,
Dupe = this.Dupe,
Machine = (Machine)this.Machine.Clone(),
Machine = this.Machine,
Supported = this.Supported,
Publisher = this.Publisher,

View File

@@ -115,6 +115,7 @@
<Compile Include="Data\Constants.cs" />
<Compile Include="Data\Flags.cs" />
<Compile Include="Data\Globals.cs" />
<Compile Include="Dats\DatHeader.cs" />
<Compile Include="Dats\Partials\DatFile.Manipulate.cs" />
<Compile Include="Dats\Partials\DatFile.ConvertUpdate.cs" />
<Compile Include="Dats\Partials\DatFile.DFD.cs" />