mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Extract out DatItems namespace
This commit is contained in:
284
SabreTools.DatItems/Adjuster.cs
Normal file
284
SabreTools.DatItems/Adjuster.cs
Normal file
@@ -0,0 +1,284 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which Adjuster(s) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("adjuster"), XmlRoot("adjuster")]
|
||||
public class Adjuster : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determine whether the value is default
|
||||
/// </summary>
|
||||
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("default")]
|
||||
public bool? Default { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DefaultSpecified { get { return Default != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Conditions associated with the adjustment
|
||||
/// </summary>
|
||||
[JsonProperty("conditions", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("conditions")]
|
||||
public List<Condition> Conditions { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ConditionsSpecified { get { return Conditions != null && Conditions.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Adjuster-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Default))
|
||||
Default = mappings[Field.DatItem_Default].AsYesNo();
|
||||
|
||||
// Field.DatItem_Conditions does not apply here
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
condition.SetFields(mappings, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Adjuster object
|
||||
/// </summary>
|
||||
public Adjuster()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Adjuster;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Adjuster()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Default = this.Default,
|
||||
Conditions = this.Conditions,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Adjuster, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Adjuster
|
||||
Adjuster newOther = other as Adjuster;
|
||||
|
||||
// If the Adjuster information matches
|
||||
bool match = (Name == newOther.Name && Default == newOther.Default);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the conditions match
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
match &= newOther.Conditions.Contains(condition);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on default
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Default, Default))
|
||||
return false;
|
||||
|
||||
// Filter on individual conditions
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
if (!condition.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Default))
|
||||
Default = null;
|
||||
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
condition.RemoveFields(fields, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Adjuster to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Adjuster)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Adjuster newItem = item as Adjuster;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Default))
|
||||
Default = newItem.Default;
|
||||
|
||||
// DatItem_Condition_* doesn't make sense here
|
||||
// since not every condition under the other item
|
||||
// can replace every condition under this item
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
157
SabreTools.DatItems/Analog.cs
Normal file
157
SabreTools.DatItems/Analog.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single analog item
|
||||
/// </summary>
|
||||
[JsonObject("analog"), XmlRoot("analog")]
|
||||
public class Analog : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Analog mask value
|
||||
/// </summary>
|
||||
[JsonProperty("mask", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("mask")]
|
||||
public string Mask { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Analog-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Analog_Mask))
|
||||
Mask = mappings[Field.DatItem_Analog_Mask];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Analog object
|
||||
/// </summary>
|
||||
public Analog()
|
||||
{
|
||||
ItemType = ItemType.Analog;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Analog()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Mask = this.Mask,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Analog, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Analog
|
||||
Analog newOther = other as Analog;
|
||||
|
||||
// If the Feature information matches
|
||||
return (Mask == newOther.Mask);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on mask
|
||||
if (!filter.PassStringFilter(filter.DatItem_Analog_Mask, Mask))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Analog_Mask))
|
||||
Mask = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Analog to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Analog)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Analog newItem = item as Analog;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Analog_Mask))
|
||||
Mask = newItem.Mask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
205
SabreTools.DatItems/Archive.cs
Normal file
205
SabreTools.DatItems/Archive.cs
Normal file
@@ -0,0 +1,205 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents generic archive files to be included in a set
|
||||
/// </summary>
|
||||
[JsonObject("archive"), XmlRoot("archive")]
|
||||
public class Archive : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Archive-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Archive object
|
||||
/// </summary>
|
||||
public Archive()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Archive;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Archive()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have an archive, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as an archive
|
||||
Archive newOther = other as Archive;
|
||||
|
||||
// If the archive information matches
|
||||
return (Name == newOther.Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Archive to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Archive)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Archive newItem = item as Archive;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
30
SabreTools.DatItems/Auxiliary.cs
Normal file
30
SabreTools.DatItems/Auxiliary.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
/// <summary>
|
||||
/// This holds all of the auxiliary types needed for proper parsing
|
||||
/// </summary>
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
#region DatItem
|
||||
|
||||
#region OpenMSX
|
||||
|
||||
/// <summary>
|
||||
/// Represents the OpenMSX original value
|
||||
/// </summary>
|
||||
[JsonObject("original"), XmlRoot("original")]
|
||||
public class Original
|
||||
{
|
||||
[JsonProperty("value")]
|
||||
public bool? Value { get; set; }
|
||||
|
||||
[JsonProperty("content")]
|
||||
public string Content { get; set; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion //DatItem
|
||||
}
|
||||
252
SabreTools.DatItems/BiosSet.cs
Normal file
252
SabreTools.DatItems/BiosSet.cs
Normal file
@@ -0,0 +1,252 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which BIOS(es) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("biosset"), XmlRoot("biosset")]
|
||||
public class BiosSet : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Description of the BIOS
|
||||
/// </summary>
|
||||
[JsonProperty("description", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("description")]
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determine whether the BIOS is default
|
||||
/// </summary>
|
||||
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("default")]
|
||||
public bool? Default { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DefaultSpecified { get { return Default != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle BiosSet-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Description))
|
||||
Description = mappings[Field.DatItem_Description];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Default))
|
||||
Default = mappings[Field.DatItem_Default].AsYesNo();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty BiosSet object
|
||||
/// </summary>
|
||||
public BiosSet()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.BiosSet;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new BiosSet()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Description = this.Description,
|
||||
Default = this.Default,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a BiosSet, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a BiosSet
|
||||
BiosSet newOther = other as BiosSet;
|
||||
|
||||
// If the BiosSet information matches
|
||||
return (Name == newOther.Name
|
||||
&& Description == newOther.Description
|
||||
&& Default == newOther.Default);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on description
|
||||
if (!filter.PassStringFilter(filter.DatItem_Description, Description))
|
||||
return false;
|
||||
|
||||
// Filter on default
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Default, Default))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Description))
|
||||
Description = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Default))
|
||||
Default = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a BiosSet to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.BiosSet)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
BiosSet newItem = item as BiosSet;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Description))
|
||||
Description = newItem.Description;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Default))
|
||||
Default = newItem.Default;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
60
SabreTools.DatItems/Blank.cs
Normal file
60
SabreTools.DatItems/Blank.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a blank set from an input DAT
|
||||
/// </summary>
|
||||
[JsonObject("blank"), XmlRoot("blank")]
|
||||
public class Blank : DatItem
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Archive object
|
||||
/// </summary>
|
||||
public Blank()
|
||||
{
|
||||
ItemType = ItemType.Blank;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Blank()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a blank, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Blank
|
||||
Blank newOther = other as Blank;
|
||||
|
||||
// If the archive information matches
|
||||
return (Machine == newOther.Machine);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
282
SabreTools.DatItems/Chip.cs
Normal file
282
SabreTools.DatItems/Chip.cs
Normal file
@@ -0,0 +1,282 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which Chip(s) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("chip"), XmlRoot("chip")]
|
||||
public class Chip : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Internal tag
|
||||
/// </summary>
|
||||
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("tag")]
|
||||
public string Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Type of the chip
|
||||
/// </summary>
|
||||
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("type")]
|
||||
public ChipType ChipType { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ChipTypeSpecified { get { return ChipType != ChipType.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Clock speed
|
||||
/// </summary>
|
||||
[JsonProperty("clock", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("clock")]
|
||||
public long? Clock { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ClockTypeSpecified { get { return Clock != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Chip-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
||||
Tag = mappings[Field.DatItem_Tag];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_ChipType))
|
||||
ChipType = mappings[Field.DatItem_ChipType].AsChipType();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Clock))
|
||||
Clock = Sanitizer.CleanLong(mappings[Field.DatItem_Clock]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Chip object
|
||||
/// </summary>
|
||||
public Chip()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Chip;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Chip()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Tag = this.Tag,
|
||||
ChipType = this.ChipType,
|
||||
Clock = this.Clock,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a chip, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a chip
|
||||
Chip newOther = other as Chip;
|
||||
|
||||
// If the chip information matches
|
||||
return (Name == newOther.Name
|
||||
&& Tag == newOther.Tag
|
||||
&& ChipType == newOther.ChipType
|
||||
&& Clock == newOther.Clock);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// DatItem_Tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
||||
return false;
|
||||
|
||||
// DatItem_ChipType
|
||||
if (filter.DatItem_ChipType.MatchesPositive(ChipType.NULL, ChipType) == false)
|
||||
return false;
|
||||
if (filter.DatItem_ChipType.MatchesNegative(ChipType.NULL, ChipType) == true)
|
||||
return false;
|
||||
|
||||
// DatItem_Clock
|
||||
if (!filter.PassLongFilter(filter.DatItem_Clock, Clock))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_ChipType))
|
||||
ChipType = ChipType.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Clock))
|
||||
Clock = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Chip to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Chip)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Chip newItem = item as Chip;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = newItem.Tag;
|
||||
|
||||
if (fields.Contains(Field.DatItem_ChipType))
|
||||
ChipType = newItem.ChipType;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Clock))
|
||||
Clock = newItem.Clock;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
315
SabreTools.DatItems/Condition.cs
Normal file
315
SabreTools.DatItems/Condition.cs
Normal file
@@ -0,0 +1,315 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a condition on a machine or other item
|
||||
/// </summary>
|
||||
[JsonObject("condition"), XmlRoot("condition")]
|
||||
public class Condition : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Condition tag value
|
||||
/// </summary>
|
||||
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("tag")]
|
||||
public string Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Condition mask
|
||||
/// </summary>
|
||||
[JsonProperty("mask", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("mask")]
|
||||
public string Mask { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Condition relationship
|
||||
/// </summary>
|
||||
[JsonProperty("relation", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("relation")]
|
||||
public Relation Relation { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool RelationSpecified { get { return Relation != Relation.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Condition value
|
||||
/// </summary>
|
||||
[JsonProperty("value", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("value")]
|
||||
public string Value { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
SetFields(mappings, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
public void SetFields(Dictionary<Field, string> mappings, bool sub)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Condition-specific fields
|
||||
if (sub)
|
||||
{
|
||||
if (mappings.Keys.Contains(Field.DatItem_Condition_Tag))
|
||||
Tag = mappings[Field.DatItem_Condition_Tag];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Condition_Mask))
|
||||
Mask = mappings[Field.DatItem_Condition_Mask];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Condition_Relation))
|
||||
Relation = mappings[Field.DatItem_Condition_Relation].AsRelation();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Condition_Value))
|
||||
Value = mappings[Field.DatItem_Condition_Value];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
||||
Tag = mappings[Field.DatItem_Tag];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Mask))
|
||||
Mask = mappings[Field.DatItem_Mask];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Relation))
|
||||
Relation = mappings[Field.DatItem_Relation].AsRelation();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Value))
|
||||
Value = mappings[Field.DatItem_Value];
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Condition object
|
||||
/// </summary>
|
||||
public Condition()
|
||||
{
|
||||
ItemType = ItemType.Condition;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Condition()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Tag = this.Tag,
|
||||
Mask = this.Mask,
|
||||
Relation = this.Relation,
|
||||
Value = this.Value,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Condition, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Condition
|
||||
Condition newOther = other as Condition;
|
||||
|
||||
// If the Feature information matches
|
||||
return (Tag == newOther.Tag
|
||||
&& Mask == newOther.Mask
|
||||
&& Relation == newOther.Relation
|
||||
&& Value == newOther.Value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
if (sub)
|
||||
{
|
||||
// Filter on tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Condition_Tag, Tag))
|
||||
return false;
|
||||
|
||||
// Filter on mask
|
||||
if (!filter.PassStringFilter(filter.DatItem_Condition_Mask, Mask))
|
||||
return false;
|
||||
|
||||
// Filter on relation
|
||||
if (filter.DatItem_Condition_Relation.MatchesPositive(Relation.NULL, Relation) == false)
|
||||
return false;
|
||||
if (filter.DatItem_Condition_Relation.MatchesNegative(Relation.NULL, Relation) == true)
|
||||
return false;
|
||||
|
||||
// Filter on value
|
||||
if (!filter.PassStringFilter(filter.DatItem_Condition_Value, Value))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Filter on tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
||||
return false;
|
||||
|
||||
// Filter on mask
|
||||
if (!filter.PassStringFilter(filter.DatItem_Mask, Mask))
|
||||
return false;
|
||||
|
||||
// Filter on relation
|
||||
if (filter.DatItem_Relation.MatchesPositive(Relation.NULL, Relation) == false)
|
||||
return false;
|
||||
if (filter.DatItem_Relation.MatchesNegative(Relation.NULL, Relation) == true)
|
||||
return false;
|
||||
|
||||
// Filter on value
|
||||
if (!filter.PassStringFilter(filter.DatItem_Value, Value))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
RemoveFields(fields, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
public void RemoveFields(List<Field> fields, bool sub)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (sub)
|
||||
{
|
||||
if (fields.Contains(Field.DatItem_Condition_Tag))
|
||||
Tag = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Condition_Mask))
|
||||
Mask = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Condition_Relation))
|
||||
Relation = Relation.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Condition_Value))
|
||||
Value = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Mask))
|
||||
Mask = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Relation))
|
||||
Relation = Relation.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Value))
|
||||
Value = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Condition to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Condition)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Condition newItem = item as Condition;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = newItem.Tag;
|
||||
else if (fields.Contains(Field.DatItem_Condition_Tag))
|
||||
Tag = newItem.Tag;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Mask))
|
||||
Mask = newItem.Mask;
|
||||
else if (fields.Contains(Field.DatItem_Condition_Mask))
|
||||
Mask = newItem.Mask;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Relation))
|
||||
Relation = newItem.Relation;
|
||||
else if (fields.Contains(Field.DatItem_Condition_Relation))
|
||||
Relation = newItem.Relation;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Value))
|
||||
Value = newItem.Value;
|
||||
else if (fields.Contains(Field.DatItem_Condition_Value))
|
||||
Value = newItem.Value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
403
SabreTools.DatItems/Configuration.cs
Normal file
403
SabreTools.DatItems/Configuration.cs
Normal file
@@ -0,0 +1,403 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which Configuration(s) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("configuration"), XmlRoot("configuration")]
|
||||
public class Configuration : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tag associated with the configuration
|
||||
/// </summary>
|
||||
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("tag")]
|
||||
public string Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Mask associated with the configuration
|
||||
/// </summary>
|
||||
[JsonProperty("mask", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("mask")]
|
||||
public string Mask { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Conditions associated with the configuration
|
||||
/// </summary>
|
||||
[JsonProperty("conditions", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("conditions")]
|
||||
public List<Condition> Conditions { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ConditionsSpecified { get { return Conditions != null && Conditions.Count > 0; } }
|
||||
|
||||
/// <summary>
|
||||
/// Locations associated with the configuration
|
||||
/// </summary>
|
||||
[JsonProperty("locations", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("locations")]
|
||||
public List<Location> Locations { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool LocationsSpecified { get { return Locations != null && Locations.Count > 0; } }
|
||||
|
||||
/// <summary>
|
||||
/// Settings associated with the configuration
|
||||
/// </summary>
|
||||
[JsonProperty("settings", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("settings")]
|
||||
public List<Setting> Settings { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool SettingsSpecified { get { return Settings != null && Settings.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Configuration-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
||||
Tag = mappings[Field.DatItem_Tag];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Mask))
|
||||
Mask = mappings[Field.DatItem_Mask];
|
||||
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
condition.SetFields(mappings, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (LocationsSpecified)
|
||||
{
|
||||
foreach (Location location in Locations)
|
||||
{
|
||||
location.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
|
||||
if (SettingsSpecified)
|
||||
{
|
||||
foreach (Setting setting in Settings)
|
||||
{
|
||||
setting.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Configuration object
|
||||
/// </summary>
|
||||
public Configuration()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Configuration;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Configuration()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Tag = this.Tag,
|
||||
Mask = this.Mask,
|
||||
Conditions = this.Conditions,
|
||||
Locations = this.Locations,
|
||||
Settings = this.Settings,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Configuration, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Configuration
|
||||
Configuration newOther = other as Configuration;
|
||||
|
||||
// If the Configuration information matches
|
||||
bool match = (Name == newOther.Name
|
||||
&& Tag == newOther.Tag
|
||||
&& Mask == newOther.Mask);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the conditions match
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
match &= newOther.Conditions.Contains(condition);
|
||||
}
|
||||
}
|
||||
|
||||
// If the locations match
|
||||
if (LocationsSpecified)
|
||||
{
|
||||
foreach (Location location in Locations)
|
||||
{
|
||||
match &= newOther.Locations.Contains(location);
|
||||
}
|
||||
}
|
||||
|
||||
// If the settings match
|
||||
if (SettingsSpecified)
|
||||
{
|
||||
foreach (Setting setting in Settings)
|
||||
{
|
||||
match &= newOther.Settings.Contains(setting);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
||||
return false;
|
||||
|
||||
// Filter on mask
|
||||
if (!filter.PassStringFilter(filter.DatItem_Mask, Mask))
|
||||
return false;
|
||||
|
||||
// Filter on individual conditions
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
if (!condition.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter on individual locations
|
||||
if (LocationsSpecified)
|
||||
{
|
||||
foreach (Location location in Locations)
|
||||
{
|
||||
if (!location.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter on individual conditions
|
||||
if (SettingsSpecified)
|
||||
{
|
||||
foreach (Setting setting in Settings)
|
||||
{
|
||||
if (!setting.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Mask))
|
||||
Mask = null;
|
||||
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
condition.RemoveFields(fields, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (LocationsSpecified)
|
||||
{
|
||||
foreach (Location location in Locations)
|
||||
{
|
||||
location.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
|
||||
if (SettingsSpecified)
|
||||
{
|
||||
foreach (Setting setting in Settings)
|
||||
{
|
||||
setting.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Configuration to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Configuration)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Configuration newItem = item as Configuration;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = newItem.Tag;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Mask))
|
||||
Mask = newItem.Mask;
|
||||
|
||||
// DatItem_Condition_* doesn't make sense here
|
||||
// since not every condition under the other item
|
||||
// can replace every condition under this item
|
||||
|
||||
// DatItem_Location_* doesn't make sense here
|
||||
// since not every location under the other item
|
||||
// can replace every location under this item
|
||||
|
||||
// DatItem_Setting_* doesn't make sense here
|
||||
// since not every setting under the other item
|
||||
// can replace every setting under this item
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
431
SabreTools.DatItems/Control.cs
Normal file
431
SabreTools.DatItems/Control.cs
Normal file
@@ -0,0 +1,431 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents control for an input
|
||||
/// </summary>
|
||||
[JsonObject("control"), XmlRoot("control")]
|
||||
public class Control : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// General type of input
|
||||
/// </summary>
|
||||
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("type")]
|
||||
public ControlType ControlType { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ControlTypeSpecified { get { return ControlType != ControlType.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Player which the input belongs to
|
||||
/// </summary>
|
||||
[JsonProperty("player", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("player")]
|
||||
public long? Player { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool PlayerSpecified { get { return Player != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Total number of buttons
|
||||
/// </summary>
|
||||
[JsonProperty("buttons", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("buttons")]
|
||||
public long? Buttons { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ButtonsSpecified { get { return Buttons != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Total number of non-optional buttons
|
||||
/// </summary>
|
||||
[JsonProperty("reqbuttons", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("reqbuttons")]
|
||||
public long? RequiredButtons { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool RequiredButtonsSpecified { get { return RequiredButtons != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Analog minimum value
|
||||
/// </summary>
|
||||
[JsonProperty("minimum", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("minimum")]
|
||||
public long? Minimum { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool MinimumSpecified { get { return Minimum != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Analog maximum value
|
||||
/// </summary>
|
||||
[JsonProperty("maximum", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("maximum")]
|
||||
public long? Maximum { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool MaximumSpecified { get { return Maximum != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Default analog sensitivity
|
||||
/// </summary>
|
||||
[JsonProperty("sensitivity", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("sensitivity")]
|
||||
public long? Sensitivity { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool SensitivitySpecified { get { return Sensitivity != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Default analog keydelta
|
||||
/// </summary>
|
||||
[JsonProperty("keydelta", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("keydelta")]
|
||||
public long? KeyDelta { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool KeyDeltaSpecified { get { return KeyDelta != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Default analog reverse setting
|
||||
/// </summary>
|
||||
[JsonProperty("reverse", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("reverse")]
|
||||
public bool? Reverse { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ReverseSpecified { get { return Reverse != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// First set of ways
|
||||
/// </summary>
|
||||
[JsonProperty("ways", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("ways")]
|
||||
public string Ways { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Second set of ways
|
||||
/// </summary>
|
||||
[JsonProperty("ways2", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("ways2")]
|
||||
public string Ways2 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Third set of ways
|
||||
/// </summary>
|
||||
[JsonProperty("ways3", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("ways3")]
|
||||
public string Ways3 { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Control-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Type))
|
||||
ControlType = mappings[Field.DatItem_Control_Type].AsControlType();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Player))
|
||||
Player = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Player]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Buttons))
|
||||
Buttons = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Buttons]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_RequiredButtons))
|
||||
RequiredButtons = Sanitizer.CleanLong(mappings[Field.DatItem_Control_RequiredButtons]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Minimum))
|
||||
Minimum = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Minimum]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Maximum))
|
||||
Maximum = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Maximum]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Sensitivity))
|
||||
Sensitivity = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Sensitivity]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_KeyDelta))
|
||||
KeyDelta = Sanitizer.CleanLong(mappings[Field.DatItem_Control_KeyDelta]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Reverse))
|
||||
Reverse = mappings[Field.DatItem_Control_Reverse].AsYesNo();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Ways))
|
||||
Ways = mappings[Field.DatItem_Control_Ways];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Ways2))
|
||||
Ways2 = mappings[Field.DatItem_Control_Ways2];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Control_Ways3))
|
||||
Ways3 = mappings[Field.DatItem_Control_Ways3];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Control object
|
||||
/// </summary>
|
||||
public Control()
|
||||
{
|
||||
ItemType = ItemType.Control;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Control()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
ControlType = this.ControlType,
|
||||
Player = this.Player,
|
||||
Buttons = this.Buttons,
|
||||
RequiredButtons = this.RequiredButtons,
|
||||
Minimum = this.Minimum,
|
||||
Maximum = this.Maximum,
|
||||
Sensitivity = this.Sensitivity,
|
||||
KeyDelta = this.KeyDelta,
|
||||
Reverse = this.Reverse,
|
||||
Ways = this.Ways,
|
||||
Ways2 = this.Ways2,
|
||||
Ways3 = this.Ways3,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Control, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Control
|
||||
Control newOther = other as Control;
|
||||
|
||||
// If the Control information matches
|
||||
return (ControlType == newOther.ControlType
|
||||
&& Player == newOther.Player
|
||||
&& Buttons == newOther.Buttons
|
||||
&& RequiredButtons == newOther.RequiredButtons
|
||||
&& Minimum == newOther.Minimum
|
||||
&& Maximum == newOther.Maximum
|
||||
&& Sensitivity == newOther.Sensitivity
|
||||
&& KeyDelta == newOther.KeyDelta
|
||||
&& Reverse == newOther.Reverse
|
||||
&& Ways == newOther.Ways
|
||||
&& Ways2 == newOther.Ways2
|
||||
&& Ways3 == newOther.Ways3);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on control type
|
||||
if (filter.DatItem_Control_Type.MatchesPositive(ControlType.NULL, ControlType) == false)
|
||||
return false;
|
||||
if (filter.DatItem_Control_Type.MatchesNegative(ControlType.NULL, ControlType) == true)
|
||||
return false;
|
||||
|
||||
// Filter on player
|
||||
if (!filter.PassLongFilter(filter.DatItem_Control_Player, Player))
|
||||
return false;
|
||||
|
||||
// Filter on buttons
|
||||
if (!filter.PassLongFilter(filter.DatItem_Control_Buttons, Buttons))
|
||||
return false;
|
||||
|
||||
// Filter on reqbuttons
|
||||
if (!filter.PassLongFilter(filter.DatItem_Control_ReqButtons, RequiredButtons))
|
||||
return false;
|
||||
|
||||
// Filter on minimum
|
||||
if (!filter.PassLongFilter(filter.DatItem_Control_Minimum, Minimum))
|
||||
return false;
|
||||
|
||||
// Filter on maximum
|
||||
if (!filter.PassLongFilter(filter.DatItem_Control_Maximum, Maximum))
|
||||
return false;
|
||||
|
||||
// Filter on sensitivity
|
||||
if (!filter.PassLongFilter(filter.DatItem_Control_Sensitivity, Sensitivity))
|
||||
return false;
|
||||
|
||||
// Filter on keydelta
|
||||
if (!filter.PassLongFilter(filter.DatItem_Control_KeyDelta, KeyDelta))
|
||||
return false;
|
||||
|
||||
// Filter on reverse
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Control_Reverse, Reverse))
|
||||
return false;
|
||||
|
||||
// Filter on ways
|
||||
if (!filter.PassStringFilter(filter.DatItem_Control_Ways, Ways))
|
||||
return false;
|
||||
|
||||
// Filter on ways2
|
||||
if (!filter.PassStringFilter(filter.DatItem_Control_Ways2, Ways2))
|
||||
return false;
|
||||
|
||||
// Filter on ways3
|
||||
if (!filter.PassStringFilter(filter.DatItem_Control_Ways3, Ways3))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Control_Type))
|
||||
ControlType = ControlType.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Player))
|
||||
Player = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Buttons))
|
||||
Buttons = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_RequiredButtons))
|
||||
RequiredButtons = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Minimum))
|
||||
Minimum = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Maximum))
|
||||
Maximum = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Sensitivity))
|
||||
Sensitivity = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_KeyDelta))
|
||||
KeyDelta = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Reverse))
|
||||
Reverse = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Ways))
|
||||
Ways = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Ways2))
|
||||
Ways2 = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Ways3))
|
||||
Ways3 = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Control to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Control)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Control newItem = item as Control;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Control_Type))
|
||||
ControlType = newItem.ControlType;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Player))
|
||||
Player = newItem.Player;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Buttons))
|
||||
Buttons = newItem.Buttons;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_RequiredButtons))
|
||||
RequiredButtons = newItem.RequiredButtons;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Minimum))
|
||||
Minimum = newItem.Minimum;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Maximum))
|
||||
Maximum = newItem.Maximum;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Sensitivity))
|
||||
Sensitivity = newItem.Sensitivity;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_KeyDelta))
|
||||
KeyDelta = newItem.KeyDelta;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Reverse))
|
||||
Reverse = newItem.Reverse;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Ways))
|
||||
Ways = newItem.Ways;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Ways2))
|
||||
Ways2 = newItem.Ways2;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Control_Ways3))
|
||||
Ways3 = newItem.Ways3;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
1164
SabreTools.DatItems/DatItem.cs
Normal file
1164
SabreTools.DatItems/DatItem.cs
Normal file
File diff suppressed because it is too large
Load Diff
284
SabreTools.DatItems/DataArea.cs
Normal file
284
SabreTools.DatItems/DataArea.cs
Normal file
@@ -0,0 +1,284 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// SoftwareList dataarea information
|
||||
/// </summary>
|
||||
/// <remarks>One DataArea can contain multiple Rom items</remarks>
|
||||
[JsonObject("dataarea"), XmlRoot("dataarea")]
|
||||
public class DataArea : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Total size of the area
|
||||
/// </summary>
|
||||
[JsonProperty("size", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("size")]
|
||||
public long? Size { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool SizeSpecified { get { return Size != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Word width for the area
|
||||
/// </summary>
|
||||
[JsonProperty("width", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("width")]
|
||||
public long? Width { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool WidthSpecified { get { return Width != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Byte endianness of the area
|
||||
/// </summary>
|
||||
[JsonProperty("endianness", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("endianness")]
|
||||
public Endianness Endianness { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool EndiannessSpecified { get { return Endianness != Endianness.NULL; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle DataArea-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_AreaName))
|
||||
Name = mappings[Field.DatItem_AreaName];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_AreaSize))
|
||||
Size = Sanitizer.CleanLong(mappings[Field.DatItem_AreaSize]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_AreaWidth))
|
||||
Width = Sanitizer.CleanLong(mappings[Field.DatItem_AreaWidth]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_AreaEndianness))
|
||||
Endianness = mappings[Field.DatItem_AreaEndianness].AsEndianness();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty DataArea object
|
||||
/// </summary>
|
||||
public DataArea()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.DataArea;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new DataArea()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Size = this.Size,
|
||||
Width = this.Width,
|
||||
Endianness = this.Endianness,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a DataArea, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a DataArea
|
||||
DataArea newOther = other as DataArea;
|
||||
|
||||
// If the DataArea information matches
|
||||
return (Name == newOther.Name
|
||||
&& Size == newOther.Size
|
||||
&& Width == newOther.Width
|
||||
&& Endianness == newOther.Endianness);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on area name
|
||||
if (!filter.PassStringFilter(filter.DatItem_AreaName, Name))
|
||||
return false;
|
||||
|
||||
// Filter on area size
|
||||
if (!filter.PassLongFilter(filter.DatItem_AreaSize, Size))
|
||||
return false;
|
||||
|
||||
// Filter on area width
|
||||
if (!filter.PassLongFilter(filter.DatItem_AreaWidth, Width))
|
||||
return false;
|
||||
|
||||
// Filter on area endianness
|
||||
if (filter.DatItem_AreaEndianness.MatchesPositive(Endianness.NULL, Endianness) == false)
|
||||
return false;
|
||||
if (filter.DatItem_AreaEndianness.MatchesNegative(Endianness.NULL, Endianness) == true)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_AreaName))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_AreaSize))
|
||||
Size = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_AreaWidth))
|
||||
Width = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_AreaEndianness))
|
||||
Endianness = Endianness.NULL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a DataArea to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.DataArea)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
DataArea newItem = item as DataArea;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_AreaName))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_AreaSize))
|
||||
Size = newItem.Size;
|
||||
|
||||
if (fields.Contains(Field.DatItem_AreaWidth))
|
||||
Width = newItem.Width;
|
||||
|
||||
if (fields.Contains(Field.DatItem_AreaEndianness))
|
||||
Endianness = newItem.Endianness;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
360
SabreTools.DatItems/Device.cs
Normal file
360
SabreTools.DatItems/Device.cs
Normal file
@@ -0,0 +1,360 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single device on the machine
|
||||
/// </summary>
|
||||
[JsonObject("device"), XmlRoot("device")]
|
||||
public class Device : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Device type
|
||||
/// </summary>
|
||||
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("type")]
|
||||
public DeviceType DeviceType { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DeviceTypeSpecified { get { return DeviceType != DeviceType.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Device tag
|
||||
/// </summary>
|
||||
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("tag")]
|
||||
public string Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fixed image format
|
||||
/// </summary>
|
||||
[JsonProperty("fixed_image", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("fixed_image")]
|
||||
public string FixedImage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the devices is mandatory
|
||||
/// </summary>
|
||||
/// <remarks>Only value used seems to be 1. Used like bool, but actually int</remarks>
|
||||
[JsonProperty("mandatory", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("mandatory")]
|
||||
public long? Mandatory { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool MandatorySpecified { get { return Mandatory != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Device interface
|
||||
/// </summary>
|
||||
[JsonProperty("interface", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("interface")]
|
||||
public string Interface { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Device instances
|
||||
/// </summary>
|
||||
[JsonProperty("instances", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("instances")]
|
||||
public List<Instance> Instances { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool InstancesSpecified { get { return Instances != null && Instances.Count > 0; } }
|
||||
|
||||
/// <summary>
|
||||
/// Device extensions
|
||||
/// </summary>
|
||||
[JsonProperty("extensions", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("extensions")]
|
||||
public List<Extension> Extensions { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ExtensionsSpecified { get { return Extensions != null && Extensions.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Device-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_DeviceType))
|
||||
DeviceType = mappings[Field.DatItem_DeviceType].AsDeviceType();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
||||
Tag = mappings[Field.DatItem_Tag];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_FixedImage))
|
||||
FixedImage = mappings[Field.DatItem_FixedImage];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Mandatory))
|
||||
Mandatory = Sanitizer.CleanLong(mappings[Field.DatItem_Mandatory]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Interface))
|
||||
Interface = mappings[Field.DatItem_Interface];
|
||||
|
||||
if (InstancesSpecified)
|
||||
{
|
||||
foreach (Instance instance in Instances)
|
||||
{
|
||||
instance.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
|
||||
if (ExtensionsSpecified)
|
||||
{
|
||||
foreach (Extension extension in Extensions)
|
||||
{
|
||||
extension.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Device object
|
||||
/// </summary>
|
||||
public Device()
|
||||
{
|
||||
ItemType = ItemType.Device;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Device()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
DeviceType = this.DeviceType,
|
||||
Tag = this.Tag,
|
||||
FixedImage = this.FixedImage,
|
||||
Mandatory = this.Mandatory,
|
||||
Interface = this.Interface,
|
||||
Instances = this.Instances,
|
||||
Extensions = this.Extensions,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Device, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Device
|
||||
Device newOther = other as Device;
|
||||
|
||||
// If the Device information matches
|
||||
bool match = (DeviceType == newOther.DeviceType
|
||||
&& Tag == newOther.Tag
|
||||
&& FixedImage == newOther.FixedImage
|
||||
&& Mandatory == newOther.Mandatory
|
||||
&& Interface == newOther.Interface);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the instances match
|
||||
if (InstancesSpecified)
|
||||
{
|
||||
foreach (Instance instance in Instances)
|
||||
{
|
||||
match &= newOther.Instances.Contains(instance);
|
||||
}
|
||||
}
|
||||
|
||||
// If the extensions match
|
||||
if (ExtensionsSpecified)
|
||||
{
|
||||
foreach (Extension extension in Extensions)
|
||||
{
|
||||
match &= newOther.Extensions.Contains(extension);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on device type
|
||||
if (filter.DatItem_DeviceType.MatchesPositive(DeviceType.NULL, DeviceType) == false)
|
||||
return false;
|
||||
if (filter.DatItem_DeviceType.MatchesNegative(DeviceType.NULL, DeviceType) == true)
|
||||
return false;
|
||||
|
||||
// Filter on tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
||||
return false;
|
||||
|
||||
// Filter on fixed image
|
||||
if (!filter.PassStringFilter(filter.DatItem_FixedImage, FixedImage))
|
||||
return false;
|
||||
|
||||
// Filter on mandatory
|
||||
if (!filter.PassLongFilter(filter.DatItem_Mandatory, Mandatory))
|
||||
return false;
|
||||
|
||||
// Filter on interface
|
||||
if (!filter.PassStringFilter(filter.DatItem_Interface, Interface))
|
||||
return false;
|
||||
|
||||
// Filter on individual instances
|
||||
if (InstancesSpecified)
|
||||
{
|
||||
foreach (Instance instance in Instances)
|
||||
{
|
||||
if (!instance.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter on individual extensions
|
||||
if (ExtensionsSpecified)
|
||||
{
|
||||
foreach (Extension extension in Extensions)
|
||||
{
|
||||
if (!extension.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_DeviceType))
|
||||
DeviceType = DeviceType.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_FixedImage))
|
||||
FixedImage = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Mandatory))
|
||||
Mandatory = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Interface))
|
||||
Interface = null;
|
||||
|
||||
if (InstancesSpecified)
|
||||
{
|
||||
foreach (Instance instance in Instances)
|
||||
{
|
||||
instance.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
|
||||
if (ExtensionsSpecified)
|
||||
{
|
||||
foreach (Extension extension in Extensions)
|
||||
{
|
||||
extension.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Device to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Device)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Device newItem = item as Device;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_DeviceType))
|
||||
DeviceType = newItem.DeviceType;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = newItem.Tag;
|
||||
|
||||
if (fields.Contains(Field.DatItem_FixedImage))
|
||||
FixedImage = newItem.FixedImage;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Mandatory))
|
||||
Mandatory = newItem.Mandatory;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Interface))
|
||||
Interface = newItem.Interface;
|
||||
|
||||
// DatItem_Instance_* doesn't make sense here
|
||||
// since not every instance under the other item
|
||||
// can replace every instance under this item
|
||||
|
||||
// DatItem_Extension_* doesn't make sense here
|
||||
// since not every extension under the other item
|
||||
// can replace every extension under this item
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
205
SabreTools.DatItems/DeviceReference.cs
Normal file
205
SabreTools.DatItems/DeviceReference.cs
Normal file
@@ -0,0 +1,205 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which Device Reference(s) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("device_ref"), XmlRoot("device_ref")]
|
||||
public class DeviceReference : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle DeviceReference-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty DeviceReference object
|
||||
/// </summary>
|
||||
public DeviceReference()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.DeviceReference;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new DeviceReference()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a device reference, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a device reference
|
||||
DeviceReference newOther = other as DeviceReference;
|
||||
|
||||
// If the device reference information matches
|
||||
return (Name == newOther.Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a DeviceReference to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.DeviceReference)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
DeviceReference newItem = item as DeviceReference;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
489
SabreTools.DatItems/DipSwitch.cs
Normal file
489
SabreTools.DatItems/DipSwitch.cs
Normal file
@@ -0,0 +1,489 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which DIP Switch(es) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("dipswitch"), XmlRoot("dipswitch")]
|
||||
public class DipSwitch : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
#region Common
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tag associated with the dipswitch
|
||||
/// </summary>
|
||||
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("tag")]
|
||||
public string Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Mask associated with the dipswitch
|
||||
/// </summary>
|
||||
[JsonProperty("mask", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("mask")]
|
||||
public string Mask { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Conditions associated with the dipswitch
|
||||
/// </summary>
|
||||
[JsonProperty("conditions", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("conditions")]
|
||||
public List<Condition> Conditions { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ConditionsSpecified { get { return Conditions != null && Conditions.Count > 0; } }
|
||||
|
||||
/// <summary>
|
||||
/// Locations associated with the dipswitch
|
||||
/// </summary>
|
||||
[JsonProperty("locations", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("locations")]
|
||||
public List<Location> Locations { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool LocationsSpecified { get { return Locations != null && Locations.Count > 0; } }
|
||||
|
||||
/// <summary>
|
||||
/// Settings associated with the dipswitch
|
||||
/// </summary>
|
||||
[JsonProperty("values", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("values")]
|
||||
public List<Setting> Values { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ValuesSpecified { get { return Values != null && Values.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
/// <summary>
|
||||
/// Original hardware part associated with the item
|
||||
/// </summary>
|
||||
[JsonProperty("part", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("part")]
|
||||
public Part Part { get; set; } = null;
|
||||
|
||||
[JsonIgnore]
|
||||
public bool PartSpecified
|
||||
{
|
||||
get
|
||||
{
|
||||
return Part != null
|
||||
&& (!string.IsNullOrEmpty(Part.Name)
|
||||
|| !string.IsNullOrEmpty(Part.Interface));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion // Fields
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle DipSwitch-specific fields
|
||||
|
||||
#region Common
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
||||
Tag = mappings[Field.DatItem_Tag];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Mask))
|
||||
Mask = mappings[Field.DatItem_Mask];
|
||||
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
condition.SetFields(mappings, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (LocationsSpecified)
|
||||
{
|
||||
foreach (Location location in Locations)
|
||||
{
|
||||
location.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
|
||||
if (ValuesSpecified)
|
||||
{
|
||||
foreach (Setting value in Values)
|
||||
{
|
||||
value.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
// Handle Part-specific fields
|
||||
if (Part == null)
|
||||
Part = new Part();
|
||||
|
||||
Part.SetFields(mappings);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty DipSwitch object
|
||||
/// </summary>
|
||||
public DipSwitch()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.DipSwitch;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new DipSwitch()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Tag = this.Tag,
|
||||
Mask = this.Mask,
|
||||
Conditions = this.Conditions,
|
||||
Locations = this.Locations,
|
||||
Values = this.Values,
|
||||
|
||||
Part = this.Part,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a DipSwitch, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a DipSwitch
|
||||
DipSwitch newOther = other as DipSwitch;
|
||||
|
||||
// If the DipSwitch information matches
|
||||
bool match = (Name == newOther.Name
|
||||
&& Tag == newOther.Tag
|
||||
&& Mask == newOther.Mask);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the part matches
|
||||
if (PartSpecified)
|
||||
match &= (Part == newOther.Part);
|
||||
|
||||
// If the conditions match
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
match &= newOther.Conditions.Contains(condition);
|
||||
}
|
||||
}
|
||||
|
||||
// If the locations match
|
||||
if (LocationsSpecified)
|
||||
{
|
||||
foreach (Location location in Locations)
|
||||
{
|
||||
match &= newOther.Locations.Contains(location);
|
||||
}
|
||||
}
|
||||
|
||||
// If the values match
|
||||
if (ValuesSpecified)
|
||||
{
|
||||
foreach (Setting value in Values)
|
||||
{
|
||||
match &= newOther.Values.Contains(value);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
#region Common
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
||||
return false;
|
||||
|
||||
// Filter on mask
|
||||
if (!filter.PassStringFilter(filter.DatItem_Mask, Mask))
|
||||
return false;
|
||||
|
||||
// Filter on individual conditions
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
if (!condition.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter on individual locations
|
||||
if (LocationsSpecified)
|
||||
{
|
||||
foreach (Location location in Locations)
|
||||
{
|
||||
if (!location.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter on individual conditions
|
||||
if (ValuesSpecified)
|
||||
{
|
||||
foreach (Setting value in Values)
|
||||
{
|
||||
if (!value.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
// Filter on Part
|
||||
if (PartSpecified)
|
||||
{
|
||||
if (!Part.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
|
||||
#region Common
|
||||
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Mask))
|
||||
Mask = null;
|
||||
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
condition.RemoveFields(fields, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (LocationsSpecified)
|
||||
{
|
||||
foreach (Location location in Locations)
|
||||
{
|
||||
location.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
|
||||
if (ValuesSpecified)
|
||||
{
|
||||
foreach (Setting value in Values)
|
||||
{
|
||||
value.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
if (PartSpecified)
|
||||
Part.RemoveFields(fields);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a DipSwitch to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.DipSwitch)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
DipSwitch newItem = item as DipSwitch;
|
||||
|
||||
// Replace the fields
|
||||
|
||||
#region Common
|
||||
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = newItem.Tag;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Mask))
|
||||
Mask = newItem.Mask;
|
||||
|
||||
// DatItem_Condition_* doesn't make sense here
|
||||
// since not every condition under the other item
|
||||
// can replace every condition under this item
|
||||
|
||||
// DatItem_Location_* doesn't make sense here
|
||||
// since not every location under the other item
|
||||
// can replace every location under this item
|
||||
|
||||
// DatItem_Setting_* doesn't make sense here
|
||||
// since not every value under the other item
|
||||
// can replace every value under this item
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
if (PartSpecified && newItem.PartSpecified)
|
||||
Part.ReplaceFields(newItem.Part, fields);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
698
SabreTools.DatItems/Disk.cs
Normal file
698
SabreTools.DatItems/Disk.cs
Normal file
@@ -0,0 +1,698 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.FileTypes;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents Compressed Hunks of Data (CHD) formatted disks which use internal hashes
|
||||
/// </summary>
|
||||
[JsonObject("disk"), XmlRoot("disk")]
|
||||
public class Disk : DatItem
|
||||
{
|
||||
#region Private instance variables
|
||||
|
||||
private byte[] _md5; // 16 bytes
|
||||
private byte[] _sha1; // 20 bytes
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
#region Common
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Data MD5 hash
|
||||
/// </summary>
|
||||
[JsonProperty("md5", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("md5")]
|
||||
public string MD5
|
||||
{
|
||||
get { return _md5.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_md5); }
|
||||
set { _md5 = Utilities.StringToByteArray(Sanitizer.CleanMD5(value)); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data SHA-1 hash
|
||||
/// </summary>
|
||||
[JsonProperty("sha1", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("sha1")]
|
||||
public string SHA1
|
||||
{
|
||||
get { return _sha1.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha1); }
|
||||
set { _sha1 = Utilities.StringToByteArray(Sanitizer.CleanSHA1(value)); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disk name to merge from parent
|
||||
/// </summary>
|
||||
[JsonProperty("merge", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("merge")]
|
||||
public string MergeTag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Disk region
|
||||
/// </summary>
|
||||
[JsonProperty("region", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("region")]
|
||||
public string Region { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Disk index
|
||||
/// </summary>
|
||||
[JsonProperty("index", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("index")]
|
||||
public string Index { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Disk writable flag
|
||||
/// </summary>
|
||||
[JsonProperty("writable", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("writable")]
|
||||
public bool? Writable { get; set; } = null;
|
||||
|
||||
[JsonIgnore]
|
||||
public bool WritableSpecified { get { return Writable != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Disk dump status
|
||||
/// </summary>
|
||||
[JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("status")]
|
||||
public ItemStatus ItemStatus { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ItemStatusSpecified { get { return ItemStatus != ItemStatus.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the disk is optional in the set
|
||||
/// </summary>
|
||||
[JsonProperty("optional", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("optional")]
|
||||
public bool? Optional { get; set; } = null;
|
||||
|
||||
[JsonIgnore]
|
||||
public bool OptionalSpecified { get { return Optional != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
/// <summary>
|
||||
/// Disk area information
|
||||
/// </summary>
|
||||
[JsonProperty("diskarea", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("diskarea")]
|
||||
public DiskArea DiskArea { get; set; } = null;
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DiskAreaSpecified
|
||||
{
|
||||
get
|
||||
{
|
||||
return DiskArea != null
|
||||
&& !string.IsNullOrEmpty(DiskArea.Name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Original hardware part associated with the item
|
||||
/// </summary>
|
||||
[JsonProperty("part", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("part")]
|
||||
public Part Part { get; set; } = null;
|
||||
|
||||
[JsonIgnore]
|
||||
public bool PartSpecified
|
||||
{
|
||||
get
|
||||
{
|
||||
return Part != null
|
||||
&& (!string.IsNullOrEmpty(Part.Name)
|
||||
|| !string.IsNullOrEmpty(Part.Interface));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion // Fields
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Disk-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_MD5))
|
||||
MD5 = mappings[Field.DatItem_MD5];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_SHA1))
|
||||
SHA1 = mappings[Field.DatItem_SHA1];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Merge))
|
||||
MergeTag = mappings[Field.DatItem_Merge];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Region))
|
||||
Region = mappings[Field.DatItem_Region];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Index))
|
||||
Index = mappings[Field.DatItem_Index];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Writable))
|
||||
Writable = mappings[Field.DatItem_Writable].AsYesNo();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Status))
|
||||
ItemStatus = mappings[Field.DatItem_Status].AsItemStatus();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Optional))
|
||||
Optional = mappings[Field.DatItem_Optional].AsYesNo();
|
||||
|
||||
// Handle DiskArea-specific fields
|
||||
if (DiskArea == null)
|
||||
DiskArea = new DiskArea();
|
||||
|
||||
DiskArea.SetFields(mappings);
|
||||
|
||||
// Handle Part-specific fields
|
||||
if (Part == null)
|
||||
Part = new Part();
|
||||
|
||||
Part.SetFields(mappings);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Disk object
|
||||
/// </summary>
|
||||
public Disk()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Disk;
|
||||
DupeType = 0x00;
|
||||
ItemStatus = ItemStatus.None;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a Disk object from a BaseFile
|
||||
/// </summary>
|
||||
/// <param name="baseFile"></param>
|
||||
public Disk(BaseFile baseFile)
|
||||
{
|
||||
Name = baseFile.Filename;
|
||||
_md5 = baseFile.MD5;
|
||||
_sha1 = baseFile.SHA1;
|
||||
|
||||
ItemType = ItemType.Disk;
|
||||
DupeType = 0x00;
|
||||
ItemStatus = ItemStatus.None;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Disk()
|
||||
{
|
||||
Name = this.Name,
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
_md5 = this._md5,
|
||||
_sha1 = this._sha1,
|
||||
MergeTag = this.MergeTag,
|
||||
Region = this.Region,
|
||||
Index = this.Index,
|
||||
Writable = this.Writable,
|
||||
ItemStatus = this.ItemStatus,
|
||||
Optional = this.Optional,
|
||||
|
||||
DiskArea = this.DiskArea,
|
||||
Part = this.Part,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Disk object to a BaseFile
|
||||
/// </summary>
|
||||
public BaseFile ConvertToBaseFile()
|
||||
{
|
||||
return new BaseFile()
|
||||
{
|
||||
Filename = this.Name,
|
||||
Parent = this.Machine?.Name,
|
||||
MD5 = this._md5,
|
||||
SHA1 = this._sha1,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a disk to the closest Rom approximation
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Rom ConvertToRom()
|
||||
{
|
||||
var rom = new Rom()
|
||||
{
|
||||
Name = this.Name + ".chd",
|
||||
ItemType = ItemType.Rom,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
MergeTag = this.MergeTag,
|
||||
Region = this.Region,
|
||||
ItemStatus = this.ItemStatus,
|
||||
Optional = this.Optional,
|
||||
|
||||
MD5 = this.MD5,
|
||||
SHA1 = this.SHA1,
|
||||
|
||||
DataArea = new DataArea { Name = this.DiskArea.Name },
|
||||
Part = this.Part,
|
||||
};
|
||||
|
||||
return rom;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
bool dupefound = false;
|
||||
|
||||
// If we don't have a rom, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return dupefound;
|
||||
|
||||
// Otherwise, treat it as a Disk
|
||||
Disk newOther = other as Disk;
|
||||
|
||||
// If all hashes are empty but they're both nodump and the names match, then they're dupes
|
||||
if ((ItemStatus == ItemStatus.Nodump && newOther.ItemStatus == ItemStatus.Nodump)
|
||||
&& Name == newOther.Name
|
||||
&& !HasHashes() && !newOther.HasHashes())
|
||||
{
|
||||
dupefound = true;
|
||||
}
|
||||
|
||||
// Otherwise if we get a partial match
|
||||
else if (HashMatch(newOther))
|
||||
{
|
||||
dupefound = true;
|
||||
}
|
||||
|
||||
return dupefound;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill any missing size and hash information from another Disk
|
||||
/// </summary>
|
||||
/// <param name="other">Disk to fill information from</param>
|
||||
public void FillMissingInformation(Disk other)
|
||||
{
|
||||
if (_md5.IsNullOrEmpty() && !other._md5.IsNullOrEmpty())
|
||||
_md5 = other._md5;
|
||||
|
||||
if (_sha1.IsNullOrEmpty() && !other._sha1.IsNullOrEmpty())
|
||||
_sha1 = other._sha1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get unique duplicate suffix on name collision
|
||||
/// </summary>
|
||||
/// <returns>String representing the suffix</returns>
|
||||
public string GetDuplicateSuffix()
|
||||
{
|
||||
if (!_md5.IsNullOrEmpty())
|
||||
return $"_{MD5}";
|
||||
else if (!_sha1.IsNullOrEmpty())
|
||||
return $"_{SHA1}";
|
||||
else
|
||||
return "_1";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if there are no, non-empty hashes in common with another Disk
|
||||
/// </summary>
|
||||
/// <param name="other">Disk to compare against</param>
|
||||
/// <returns>True if at least one hash is not mutually exclusive, false otherwise</returns>
|
||||
private bool HasCommonHash(Disk other)
|
||||
{
|
||||
return !(_md5.IsNullOrEmpty() ^ other._md5.IsNullOrEmpty())
|
||||
|| !(_sha1.IsNullOrEmpty() ^ other._sha1.IsNullOrEmpty());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the Disk contains any hashes
|
||||
/// </summary>
|
||||
/// <returns>True if any hash exists, false otherwise</returns>
|
||||
private bool HasHashes()
|
||||
{
|
||||
return !_md5.IsNullOrEmpty()
|
||||
|| !_sha1.IsNullOrEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if any hashes are common with another Disk
|
||||
/// </summary>
|
||||
/// <param name="other">Disk to compare against</param>
|
||||
/// <returns>True if any hashes are in common, false otherwise</returns>
|
||||
private bool HashMatch(Disk other)
|
||||
{
|
||||
// If either have no hashes, we return false, otherwise this would be a false positive
|
||||
if (!HasHashes() || !other.HasHashes())
|
||||
return false;
|
||||
|
||||
// If neither have hashes in common, we return false, otherwise this would be a false positive
|
||||
if (!HasCommonHash(other))
|
||||
return false;
|
||||
|
||||
// Return if all hashes match according to merge rules
|
||||
return ConditionalHashEquals(_md5, other._md5)
|
||||
&& ConditionalHashEquals(_sha1, other._sha1);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
#region Common
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on MD5
|
||||
if (!filter.PassStringFilter(filter.DatItem_MD5, MD5))
|
||||
return false;
|
||||
|
||||
// Filter on SHA-1
|
||||
if (!filter.PassStringFilter(filter.DatItem_SHA1, SHA1))
|
||||
return false;
|
||||
|
||||
// Filter on merge tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Merge, MergeTag))
|
||||
return false;
|
||||
|
||||
// Filter on region
|
||||
if (!filter.PassStringFilter(filter.DatItem_Region, Region))
|
||||
return false;
|
||||
|
||||
// Filter on index
|
||||
if (!filter.PassStringFilter(filter.DatItem_Index, Index))
|
||||
return false;
|
||||
|
||||
// Filter on writable
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Writable, Writable))
|
||||
return false;
|
||||
|
||||
// Filter on status
|
||||
if (filter.DatItem_Status.MatchesPositive(ItemStatus.NULL, ItemStatus) == false)
|
||||
return false;
|
||||
if (filter.DatItem_Status.MatchesNegative(ItemStatus.NULL, ItemStatus) == true)
|
||||
return false;
|
||||
|
||||
// Filter on optional
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Optional, Optional))
|
||||
return false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
// Filter on DiskArea
|
||||
if (DiskAreaSpecified)
|
||||
{
|
||||
if (!DiskArea.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Filter on Part
|
||||
if (PartSpecified)
|
||||
{
|
||||
if (!Part.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
|
||||
#region Common
|
||||
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_MD5))
|
||||
MD5 = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SHA1))
|
||||
SHA1 = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Merge))
|
||||
MergeTag = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Region))
|
||||
Region = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Index))
|
||||
Index = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Writable))
|
||||
Writable = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Status))
|
||||
ItemStatus = ItemStatus.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Optional))
|
||||
Optional = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
if (DiskAreaSpecified)
|
||||
DiskArea.RemoveFields(fields);
|
||||
|
||||
if (PartSpecified)
|
||||
Part.RemoveFields(fields);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Get the dictionary key that should be used for a given item and bucketing type
|
||||
/// </summary>
|
||||
/// <param name="bucketedBy">Field enum representing what key to get</param>
|
||||
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
|
||||
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
||||
/// <returns>String representing the key to be used for the DatItem</returns>
|
||||
public override string GetKey(Field bucketedBy, bool lower = true, bool norename = true)
|
||||
{
|
||||
// Set the output key as the default blank string
|
||||
string key = string.Empty;
|
||||
|
||||
// Now determine what the key should be based on the bucketedBy value
|
||||
switch (bucketedBy)
|
||||
{
|
||||
case Field.DatItem_MD5:
|
||||
key = MD5;
|
||||
break;
|
||||
|
||||
case Field.DatItem_SHA1:
|
||||
key = SHA1;
|
||||
break;
|
||||
|
||||
// Let the base handle generic stuff
|
||||
default:
|
||||
return base.GetKey(bucketedBy, lower, norename);
|
||||
}
|
||||
|
||||
// Double and triple check the key for corner cases
|
||||
if (key == null)
|
||||
key = string.Empty;
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Disk to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Disk)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Disk newItem = item as Disk;
|
||||
|
||||
// Replace the fields
|
||||
|
||||
#region Common
|
||||
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_MD5))
|
||||
{
|
||||
if (string.IsNullOrEmpty(MD5) && !string.IsNullOrEmpty(newItem.MD5))
|
||||
MD5 = newItem.MD5;
|
||||
}
|
||||
|
||||
if (fields.Contains(Field.DatItem_SHA1))
|
||||
{
|
||||
if (string.IsNullOrEmpty(SHA1) && !string.IsNullOrEmpty(newItem.SHA1))
|
||||
SHA1 = newItem.SHA1;
|
||||
}
|
||||
|
||||
if (fields.Contains(Field.DatItem_Merge))
|
||||
MergeTag = newItem.MergeTag;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Region))
|
||||
Region = newItem.Region;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Index))
|
||||
Index = newItem.Index;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Writable))
|
||||
Writable = newItem.Writable;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Status))
|
||||
ItemStatus = newItem.ItemStatus;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Optional))
|
||||
Optional = newItem.Optional;
|
||||
|
||||
#endregion
|
||||
|
||||
#region SoftwareList
|
||||
|
||||
if (DiskAreaSpecified && newItem.DiskAreaSpecified)
|
||||
DiskArea.ReplaceFields(newItem.DiskArea, fields);
|
||||
|
||||
if (PartSpecified && newItem.PartSpecified)
|
||||
Part.ReplaceFields(newItem.Part, fields);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
207
SabreTools.DatItems/DiskArea.cs
Normal file
207
SabreTools.DatItems/DiskArea.cs
Normal file
@@ -0,0 +1,207 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// SoftwareList diskarea information
|
||||
/// </summary>
|
||||
/// <remarks>One DiskArea can contain multiple Disk items</remarks>
|
||||
[JsonObject("diskarea"), XmlRoot("diskarea")]
|
||||
public class DiskArea : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle DiskArea-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_AreaName))
|
||||
Name = mappings[Field.DatItem_AreaName];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty DiskArea object
|
||||
/// </summary>
|
||||
public DiskArea()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.DiskArea;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new DiskArea()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a DiskArea, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a DiskArea
|
||||
DiskArea newOther = other as DiskArea;
|
||||
|
||||
// If the DiskArea information matches
|
||||
return (Name == newOther.Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on area name
|
||||
if (!filter.PassStringFilter(filter.DatItem_AreaName, Name))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_AreaName))
|
||||
Name = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a DiskArea to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.DiskArea)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
DiskArea newItem = item as DiskArea;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_AreaName))
|
||||
Name = newItem.Name;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
490
SabreTools.DatItems/Display.cs
Normal file
490
SabreTools.DatItems/Display.cs
Normal file
@@ -0,0 +1,490 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents one machine display
|
||||
/// </summary>
|
||||
[JsonObject("display"), XmlRoot("display")]
|
||||
public class Display : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Display tag
|
||||
/// </summary>
|
||||
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("tag")]
|
||||
public string Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Display type
|
||||
/// </summary>
|
||||
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("type")]
|
||||
public DisplayType DisplayType { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DisplayTypeSpecified { get { return DisplayType != DisplayType.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Display rotation
|
||||
/// </summary>
|
||||
[JsonProperty("rotate", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("rotate")]
|
||||
public long? Rotate { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool RotateSpecified { get { return Rotate != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if display is flipped in the X-coordinates
|
||||
/// </summary>
|
||||
[JsonProperty("flipx", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("flipx")]
|
||||
public bool? FlipX { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool FlipXSpecified { get { return FlipX != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Display width
|
||||
/// </summary>
|
||||
[JsonProperty("width", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("width")]
|
||||
public long? Width { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool WidthSpecified { get { return Width != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Display height
|
||||
/// </summary>
|
||||
[JsonProperty("height", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("height")]
|
||||
public long? Height { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool HeightSpecified { get { return Height != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Refresh rate
|
||||
/// </summary>
|
||||
[JsonProperty("refresh", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("refresh")]
|
||||
public double? Refresh { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool RefreshSpecified { get { return Refresh != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Pixel clock timer
|
||||
/// </summary>
|
||||
[JsonProperty("pixclock", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("pixclock")]
|
||||
public long? PixClock { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool PixClockSpecified { get { return PixClock != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Total horizontal lines
|
||||
/// </summary>
|
||||
[JsonProperty("htotal", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("htotal")]
|
||||
public long? HTotal { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool HTotalSpecified { get { return HTotal != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Horizontal blank end
|
||||
/// </summary>
|
||||
[JsonProperty("hbend", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("hbend")]
|
||||
public long? HBEnd { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool HBEndSpecified { get { return HBEnd != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Horizontal blank start
|
||||
/// </summary>
|
||||
[JsonProperty("hbstart", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("hbstart")]
|
||||
public long? HBStart { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool HBStartSpecified { get { return HBStart != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Total vertical lines
|
||||
/// </summary>
|
||||
[JsonProperty("vtotal", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("vtotal")]
|
||||
public long? VTotal { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool VTotalSpecified { get { return VTotal != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Vertical blank end
|
||||
/// </summary>
|
||||
[JsonProperty("vbend", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("vbend")]
|
||||
public long? VBEnd { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool VBEndSpecified { get { return VBEnd != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Vertical blank start
|
||||
/// </summary>
|
||||
[JsonProperty("vbstart", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("vbstart")]
|
||||
public long? VBStart { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool VBStartSpecified { get { return VBStart != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Display-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
||||
Tag = mappings[Field.DatItem_Tag];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_DisplayType))
|
||||
DisplayType = mappings[Field.DatItem_DisplayType].AsDisplayType();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Rotate))
|
||||
Rotate = Sanitizer.CleanLong(mappings[Field.DatItem_Rotate]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_FlipX))
|
||||
FlipX = mappings[Field.DatItem_FlipX].AsYesNo();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Width))
|
||||
Width = Sanitizer.CleanLong(mappings[Field.DatItem_Width]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Height))
|
||||
Height = Sanitizer.CleanLong(mappings[Field.DatItem_Height]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Refresh))
|
||||
{
|
||||
if (Double.TryParse(mappings[Field.DatItem_Refresh], out double refresh))
|
||||
Refresh = refresh;
|
||||
}
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_PixClock))
|
||||
PixClock = Sanitizer.CleanLong(mappings[Field.DatItem_PixClock]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_HTotal))
|
||||
HTotal = Sanitizer.CleanLong(mappings[Field.DatItem_HTotal]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_HBEnd))
|
||||
HBEnd = Sanitizer.CleanLong(mappings[Field.DatItem_HBEnd]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_HBStart))
|
||||
HBStart = Sanitizer.CleanLong(mappings[Field.DatItem_HBStart]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_VTotal))
|
||||
VTotal = Sanitizer.CleanLong(mappings[Field.DatItem_VTotal]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_VBEnd))
|
||||
VBEnd = Sanitizer.CleanLong(mappings[Field.DatItem_VBEnd]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_VBStart))
|
||||
VBStart = Sanitizer.CleanLong(mappings[Field.DatItem_VBStart]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Display object
|
||||
/// </summary>
|
||||
public Display()
|
||||
{
|
||||
ItemType = ItemType.Display;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Display()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Tag = this.Tag,
|
||||
DisplayType = this.DisplayType,
|
||||
Rotate = this.Rotate,
|
||||
FlipX = this.FlipX,
|
||||
Width = this.Width,
|
||||
Height = this.Height,
|
||||
Refresh = this.Refresh,
|
||||
PixClock = this.PixClock,
|
||||
HTotal = this.HTotal,
|
||||
HBEnd = this.HBEnd,
|
||||
HBStart = this.HBStart,
|
||||
VTotal = this.VTotal,
|
||||
VBEnd = this.VBEnd,
|
||||
VBStart = this.VBStart,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Display, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Display
|
||||
Display newOther = other as Display;
|
||||
|
||||
// If the Display information matches
|
||||
return (Tag == newOther.Tag
|
||||
&& DisplayType == newOther.DisplayType
|
||||
&& Rotate == newOther.Rotate
|
||||
&& FlipX == newOther.FlipX
|
||||
&& Width == newOther.Width
|
||||
&& Height == newOther.Height
|
||||
&& Refresh == newOther.Refresh
|
||||
&& PixClock == newOther.PixClock
|
||||
&& HTotal == newOther.HTotal
|
||||
&& HBEnd == newOther.HBEnd
|
||||
&& HBStart == newOther.HBStart
|
||||
&& VTotal == newOther.VTotal
|
||||
&& VBEnd == newOther.VBEnd
|
||||
&& VBStart == newOther.VBStart);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
||||
return false;
|
||||
|
||||
// Filter on display type
|
||||
if (filter.DatItem_DisplayType.MatchesPositive(DisplayType.NULL, DisplayType) == false)
|
||||
return false;
|
||||
if (filter.DatItem_DisplayType.MatchesNegative(DisplayType.NULL, DisplayType) == true)
|
||||
return false;
|
||||
|
||||
// Filter on rotation
|
||||
if (!filter.PassLongFilter(filter.DatItem_Rotate, Rotate))
|
||||
return false;
|
||||
|
||||
// Filter on flipx
|
||||
if (!filter.PassBoolFilter(filter.DatItem_FlipX, FlipX))
|
||||
return false;
|
||||
|
||||
// Filter on width
|
||||
if (!filter.PassLongFilter(filter.DatItem_Width, Width))
|
||||
return false;
|
||||
|
||||
// Filter on height
|
||||
if (!filter.PassLongFilter(filter.DatItem_Height, Height))
|
||||
return false;
|
||||
|
||||
// Filter on refresh
|
||||
if (!filter.PassDoubleFilter(filter.DatItem_Refresh, Refresh))
|
||||
return false;
|
||||
|
||||
// Filter on pixclock
|
||||
if (!filter.PassLongFilter(filter.DatItem_PixClock, PixClock))
|
||||
return false;
|
||||
|
||||
// Filter on htotal
|
||||
if (!filter.PassLongFilter(filter.DatItem_HTotal, HTotal))
|
||||
return false;
|
||||
|
||||
// Filter on hbend
|
||||
if (!filter.PassLongFilter(filter.DatItem_HBEnd, HBEnd))
|
||||
return false;
|
||||
|
||||
// Filter on hbstart
|
||||
if (!filter.PassLongFilter(filter.DatItem_HBStart, HBStart))
|
||||
return false;
|
||||
|
||||
// Filter on vtotal
|
||||
if (!filter.PassLongFilter(filter.DatItem_VTotal, VTotal))
|
||||
return false;
|
||||
|
||||
// Filter on vbend
|
||||
if (!filter.PassLongFilter(filter.DatItem_VBEnd, VBEnd))
|
||||
return false;
|
||||
|
||||
// Filter on vbstart
|
||||
if (!filter.PassLongFilter(filter.DatItem_VBStart, VBStart))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_DisplayType))
|
||||
DisplayType = DisplayType.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Rotate))
|
||||
Rotate = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_FlipX))
|
||||
FlipX = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Width))
|
||||
Width = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Height))
|
||||
Height = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Refresh))
|
||||
Refresh = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_PixClock))
|
||||
PixClock = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_HTotal))
|
||||
HTotal = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_HBEnd))
|
||||
HBEnd = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_HBStart))
|
||||
HBStart = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_VTotal))
|
||||
VTotal = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_VBEnd))
|
||||
VBEnd = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_VBStart))
|
||||
VBStart = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Display to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Display)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Display newItem = item as Display;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = newItem.Tag;
|
||||
|
||||
if (fields.Contains(Field.DatItem_DisplayType))
|
||||
DisplayType = newItem.DisplayType;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Rotate))
|
||||
Rotate = newItem.Rotate;
|
||||
|
||||
if (fields.Contains(Field.DatItem_FlipX))
|
||||
FlipX = newItem.FlipX;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Width))
|
||||
Width = newItem.Width;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Height))
|
||||
Height = newItem.Height;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Refresh))
|
||||
Refresh = newItem.Refresh;
|
||||
|
||||
if (fields.Contains(Field.DatItem_PixClock))
|
||||
PixClock = newItem.PixClock;
|
||||
|
||||
if (fields.Contains(Field.DatItem_HTotal))
|
||||
HTotal = newItem.HTotal;
|
||||
|
||||
if (fields.Contains(Field.DatItem_HBEnd))
|
||||
HBEnd = newItem.HBEnd;
|
||||
|
||||
if (fields.Contains(Field.DatItem_HBStart))
|
||||
HBStart = newItem.HBStart;
|
||||
|
||||
if (fields.Contains(Field.DatItem_VTotal))
|
||||
VTotal = newItem.VTotal;
|
||||
|
||||
if (fields.Contains(Field.DatItem_VBEnd))
|
||||
VBEnd = newItem.VBEnd;
|
||||
|
||||
if (fields.Contains(Field.DatItem_VBStart))
|
||||
VBStart = newItem.VBStart;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
248
SabreTools.DatItems/Driver.cs
Normal file
248
SabreTools.DatItems/Driver.cs
Normal file
@@ -0,0 +1,248 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the a driver of the machine
|
||||
/// </summary>
|
||||
[JsonObject("driver"), XmlRoot("driver")]
|
||||
public class Driver : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Overall driver status
|
||||
/// </summary>
|
||||
[JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("status")]
|
||||
public SupportStatus Status { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool StatusSpecified { get { return Status != SupportStatus.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Driver emulation status
|
||||
/// </summary>
|
||||
[JsonProperty("emulation", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("emulation")]
|
||||
public SupportStatus Emulation { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool EmulationSpecified { get { return Emulation != SupportStatus.NULL; ; } }
|
||||
|
||||
/// <summary>
|
||||
/// Cocktail orientation status
|
||||
/// </summary>
|
||||
[JsonProperty("cocktail", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("cocktail")]
|
||||
public SupportStatus Cocktail { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool CocktailSpecified { get { return Cocktail != SupportStatus.NULL; ; } }
|
||||
|
||||
/// <summary>
|
||||
/// Save state support status
|
||||
/// </summary>
|
||||
[JsonProperty("savestate", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("savestate")]
|
||||
public Supported SaveState { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool SaveStateSpecified { get { return SaveState != Supported.NULL; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Feature-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_SupportStatus))
|
||||
Status = mappings[Field.DatItem_SupportStatus].AsSupportStatus();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_EmulationStatus))
|
||||
Emulation = mappings[Field.DatItem_EmulationStatus].AsSupportStatus();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_CocktailStatus))
|
||||
Cocktail = mappings[Field.DatItem_CocktailStatus].AsSupportStatus();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_SaveStateStatus))
|
||||
SaveState = mappings[Field.DatItem_SaveStateStatus].AsSupported();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Driver object
|
||||
/// </summary>
|
||||
public Driver()
|
||||
{
|
||||
ItemType = ItemType.Driver;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Driver()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Status = this.Status,
|
||||
Emulation = this.Emulation,
|
||||
Cocktail = this.Cocktail,
|
||||
SaveState = this.SaveState,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Driver, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Driver
|
||||
Driver newOther = other as Driver;
|
||||
|
||||
// If the Feature information matches
|
||||
return (Status == newOther.Status
|
||||
&& Emulation == newOther.Emulation
|
||||
&& Cocktail == newOther.Cocktail
|
||||
&& SaveState == newOther.SaveState);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on status
|
||||
if (filter.DatItem_SupportStatus.MatchesPositive(SupportStatus.NULL, Status) == false)
|
||||
return false;
|
||||
if (filter.DatItem_SupportStatus.MatchesNegative(SupportStatus.NULL, Status) == true)
|
||||
return false;
|
||||
|
||||
// Filter on emulation
|
||||
if (filter.DatItem_EmulationStatus.MatchesPositive(SupportStatus.NULL, Emulation) == false)
|
||||
return false;
|
||||
if (filter.DatItem_EmulationStatus.MatchesNegative(SupportStatus.NULL, Emulation) == true)
|
||||
return false;
|
||||
|
||||
// Filter on cocktail
|
||||
if (filter.DatItem_CocktailStatus.MatchesPositive(SupportStatus.NULL, Cocktail) == false)
|
||||
return false;
|
||||
if (filter.DatItem_CocktailStatus.MatchesNegative(SupportStatus.NULL, Cocktail) == true)
|
||||
return false;
|
||||
|
||||
// Filter on savestate
|
||||
if (filter.DatItem_SaveStateStatus.MatchesPositive(Supported.NULL, SaveState) == false)
|
||||
return false;
|
||||
if (filter.DatItem_SaveStateStatus.MatchesNegative(Supported.NULL, SaveState) == true)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_SupportStatus))
|
||||
Status = SupportStatus.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_EmulationStatus))
|
||||
Emulation = SupportStatus.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_CocktailStatus))
|
||||
Cocktail = SupportStatus.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SaveStateStatus))
|
||||
SaveState = Supported.NULL;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Driver to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Driver)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Driver newItem = item as Driver;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_SupportStatus))
|
||||
Status = newItem.Status;
|
||||
|
||||
if (fields.Contains(Field.DatItem_EmulationStatus))
|
||||
Emulation = newItem.Emulation;
|
||||
|
||||
if (fields.Contains(Field.DatItem_CocktailStatus))
|
||||
Cocktail = newItem.Cocktail;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SaveStateStatus))
|
||||
SaveState = newItem.SaveState;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
205
SabreTools.DatItems/Extension.cs
Normal file
205
SabreTools.DatItems/Extension.cs
Normal file
@@ -0,0 +1,205 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a matchable extension
|
||||
/// </summary>
|
||||
[JsonObject("extension"), XmlRoot("extension")]
|
||||
public class Extension : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Sample-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Extension_Name))
|
||||
Name = mappings[Field.DatItem_Extension_Name];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Extension object
|
||||
/// </summary>
|
||||
public Extension()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Extension;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Extension()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Extension, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Extension
|
||||
Extension newOther = other as Extension;
|
||||
|
||||
// If the Extension information matches
|
||||
return (Name == newOther.Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Extension_Name, Name))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Extension_Name))
|
||||
Name = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Extension to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Extension)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Extension newItem = item as Extension;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Extension_Name))
|
||||
Name = newItem.Name;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
218
SabreTools.DatItems/Feature.cs
Normal file
218
SabreTools.DatItems/Feature.cs
Normal file
@@ -0,0 +1,218 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the a feature of the machine
|
||||
/// </summary>
|
||||
[JsonObject("feature"), XmlRoot("feature")]
|
||||
public class Feature : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Type of feature
|
||||
/// </summary>
|
||||
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("type")]
|
||||
public FeatureType Type { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool TypeSpecified { get { return Type != FeatureType.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Emulation status
|
||||
/// </summary>
|
||||
[JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("status")]
|
||||
public FeatureStatus Status { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool StatusSpecified { get { return Status != FeatureStatus.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Overall status
|
||||
/// </summary>
|
||||
[JsonProperty("overall", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("overall")]
|
||||
public FeatureStatus Overall { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool OverallSpecified { get { return Overall != FeatureStatus.NULL; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Feature-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_FeatureType))
|
||||
Type = mappings[Field.DatItem_FeatureType].AsFeatureType();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_FeatureStatus))
|
||||
Status = mappings[Field.DatItem_FeatureStatus].AsFeatureStatus();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_FeatureOverall))
|
||||
Overall = mappings[Field.DatItem_FeatureOverall].AsFeatureStatus();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Feature object
|
||||
/// </summary>
|
||||
public Feature()
|
||||
{
|
||||
ItemType = ItemType.Feature;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Feature()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Type = this.Type,
|
||||
Status = this.Status,
|
||||
Overall = this.Overall,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Feature, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Feature
|
||||
Feature newOther = other as Feature;
|
||||
|
||||
// If the Feature information matches
|
||||
return (Type == newOther.Type && Status == newOther.Status && Overall == newOther.Overall);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on type
|
||||
if (filter.DatItem_FeatureType.MatchesPositive(FeatureType.NULL, Type) == false)
|
||||
return false;
|
||||
if (filter.DatItem_FeatureType.MatchesNegative(FeatureType.NULL, Type) == true)
|
||||
return false;
|
||||
|
||||
// Filter on status
|
||||
if (filter.DatItem_FeatureStatus.MatchesPositive(FeatureStatus.NULL, Status) == false)
|
||||
return false;
|
||||
if (filter.DatItem_FeatureStatus.MatchesNegative(FeatureStatus.NULL, Status) == true)
|
||||
return false;
|
||||
|
||||
// Filter on overall
|
||||
if (filter.DatItem_FeatureOverall.MatchesPositive(FeatureStatus.NULL, Overall) == false)
|
||||
return false;
|
||||
if (filter.DatItem_FeatureOverall.MatchesNegative(FeatureStatus.NULL, Overall) == true)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_FeatureType))
|
||||
Type = FeatureType.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_FeatureStatus))
|
||||
Status = FeatureStatus.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_FeatureOverall))
|
||||
Overall = FeatureStatus.NULL;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Feature to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Feature)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Feature newItem = item as Feature;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_FeatureType))
|
||||
Type = newItem.Type;
|
||||
|
||||
if (fields.Contains(Field.DatItem_FeatureStatus))
|
||||
Status = newItem.Status;
|
||||
|
||||
if (fields.Contains(Field.DatItem_FeatureOverall))
|
||||
Overall = newItem.Overall;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
226
SabreTools.DatItems/Info.cs
Normal file
226
SabreTools.DatItems/Info.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents special information about a machine
|
||||
/// </summary>
|
||||
[JsonObject("info"), XmlRoot("info")]
|
||||
public class Info : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Information value
|
||||
/// </summary>
|
||||
[JsonProperty("value")]
|
||||
[XmlElement("value")]
|
||||
public string Value { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Info-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Value))
|
||||
Value = mappings[Field.DatItem_Value];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Info object
|
||||
/// </summary>
|
||||
public Info()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Info;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Info()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Value = this.Value,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a sample, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Info
|
||||
Info newOther = other as Info;
|
||||
|
||||
// If the archive information matches
|
||||
return (Name == newOther.Name && Value == newOther.Value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on info value
|
||||
if (!filter.PassStringFilter(filter.DatItem_Value, Value))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Value))
|
||||
Value = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Info to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Info)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Info newItem = item as Info;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Value))
|
||||
Value = newItem.Value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
290
SabreTools.DatItems/Input.cs
Normal file
290
SabreTools.DatItems/Input.cs
Normal file
@@ -0,0 +1,290 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents one ListXML input
|
||||
/// </summary>
|
||||
[JsonObject("input"), XmlRoot("input")]
|
||||
public class Input : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Input service ID
|
||||
/// </summary>
|
||||
[JsonProperty("service", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("service")]
|
||||
public bool? Service { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ServiceSpecified { get { return Service != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Determins if this has a tilt sensor
|
||||
/// </summary>
|
||||
[JsonProperty("tilt", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("tilt")]
|
||||
public bool? Tilt { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool TiltSpecified { get { return Tilt != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Number of players on the input
|
||||
/// </summary>
|
||||
[JsonProperty("players", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("players")]
|
||||
public long? Players { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool PlayersSpecified { get { return Players != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Number of coins required
|
||||
/// </summary>
|
||||
[JsonProperty("coins", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("coins")]
|
||||
public long? Coins { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool CoinsSpecified { get { return Coins != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Set of controls for the input
|
||||
/// </summary>
|
||||
[JsonProperty("controls", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("controls")]
|
||||
public List<Control> Controls { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ControlsSpecified { get { return Controls != null && Controls.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Input-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Service))
|
||||
Service = mappings[Field.DatItem_Service].AsYesNo();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Tilt))
|
||||
Tilt = mappings[Field.DatItem_Tilt].AsYesNo();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Players))
|
||||
Players = Sanitizer.CleanLong(mappings[Field.DatItem_Players]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Coins))
|
||||
Coins = Sanitizer.CleanLong(mappings[Field.DatItem_Coins]);
|
||||
|
||||
if (ControlsSpecified)
|
||||
{
|
||||
foreach (Control control in Controls)
|
||||
{
|
||||
control.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Input object
|
||||
/// </summary>
|
||||
public Input()
|
||||
{
|
||||
ItemType = ItemType.Input;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Input()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Service = this.Service,
|
||||
Tilt = this.Tilt,
|
||||
Players = this.Players,
|
||||
Coins = this.Coins,
|
||||
Controls = this.Controls,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Input, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Input
|
||||
Input newOther = other as Input;
|
||||
|
||||
// If the Input information matches
|
||||
bool match = (Service == newOther.Service
|
||||
&& Tilt == newOther.Tilt
|
||||
&& Players == newOther.Players
|
||||
&& Coins == newOther.Coins);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the controls match
|
||||
if (ControlsSpecified)
|
||||
{
|
||||
foreach (Control control in Controls)
|
||||
{
|
||||
match &= newOther.Controls.Contains(control);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on service
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Service, Service))
|
||||
return false;
|
||||
|
||||
// Filter on tilt
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Tilt, Tilt))
|
||||
return false;
|
||||
|
||||
// Filter on players
|
||||
if (!filter.PassLongFilter(filter.DatItem_Players, Players))
|
||||
return false;
|
||||
|
||||
// Filter on coins
|
||||
if (!filter.PassLongFilter(filter.DatItem_Coins, Coins))
|
||||
return false;
|
||||
|
||||
// Filter on individual controls
|
||||
if (ControlsSpecified)
|
||||
{
|
||||
foreach (Control control in Controls)
|
||||
{
|
||||
if (!control.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Service))
|
||||
Service = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tilt))
|
||||
Tilt = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Players))
|
||||
Players = 0;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Coins))
|
||||
Coins = null;
|
||||
|
||||
if (ControlsSpecified)
|
||||
{
|
||||
foreach (Control control in Controls)
|
||||
{
|
||||
control.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Input to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Input)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Input newItem = item as Input;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Service))
|
||||
Service = newItem.Service;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Tilt))
|
||||
Tilt = newItem.Tilt;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Players))
|
||||
Players = newItem.Players;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Coins))
|
||||
Coins = newItem.Coins;
|
||||
|
||||
// DatItem_Control_* doesn't make sense here
|
||||
// since not every control under the other item
|
||||
// can replace every control under this item
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
226
SabreTools.DatItems/Instance.cs
Normal file
226
SabreTools.DatItems/Instance.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single instance of another item
|
||||
/// </summary>
|
||||
[JsonObject("instance"), XmlRoot("instance")]
|
||||
public class Instance : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Short name for the instance
|
||||
/// </summary>
|
||||
[JsonProperty("briefname", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("briefname")]
|
||||
public string BriefName { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Instance-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Instance_Name))
|
||||
Name = mappings[Field.DatItem_Instance_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Instance_BriefName))
|
||||
BriefName = mappings[Field.DatItem_Instance_BriefName];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Instance object
|
||||
/// </summary>
|
||||
public Instance()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Instance;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Instance()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
BriefName = this.BriefName,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Instance, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Instance
|
||||
Instance newOther = other as Instance;
|
||||
|
||||
// If the Instance information matches
|
||||
return (Name == newOther.Name && BriefName == newOther.BriefName);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Instance_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on brief name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Instance_BriefName, BriefName))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Instance_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Instance_BriefName))
|
||||
BriefName = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Instance to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Instance)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Instance newItem = item as Instance;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Instance_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Instance_BriefName))
|
||||
BriefName = newItem.BriefName;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
255
SabreTools.DatItems/Location.cs
Normal file
255
SabreTools.DatItems/Location.cs
Normal file
@@ -0,0 +1,255 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents one conflocation or diplocation
|
||||
/// </summary>
|
||||
[JsonObject("location"), XmlRoot("location")]
|
||||
public class Location : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Location name
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Location ID
|
||||
/// </summary>
|
||||
[JsonProperty("number", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("number")]
|
||||
public long? Number { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool NumberSpecified { get { return Number != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if location is inverted or not
|
||||
/// </summary>
|
||||
[JsonProperty("inverted", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("inverted")]
|
||||
public bool? Inverted { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool InvertedSpecified { get { return Inverted != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Location-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Location_Name))
|
||||
Name = mappings[Field.DatItem_Location_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Location_Number))
|
||||
Number = Sanitizer.CleanLong(mappings[Field.DatItem_Location_Number]);
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Location_Inverted))
|
||||
Inverted = mappings[Field.DatItem_Location_Inverted].AsYesNo();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Location object
|
||||
/// </summary>
|
||||
public Location()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Location;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Location()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Number = this.Number,
|
||||
Inverted = this.Inverted,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Location, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Location
|
||||
Location newOther = other as Location;
|
||||
|
||||
// If the Location information matches
|
||||
return (Name == newOther.Name
|
||||
&& Number == newOther.Number
|
||||
&& Inverted == newOther.Inverted);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Location_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on number
|
||||
if (!filter.PassLongFilter(filter.DatItem_Location_Number, Number))
|
||||
return false;
|
||||
|
||||
// Filter on inverted
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Location_Inverted, Inverted))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Location_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Location_Number))
|
||||
Number = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Location_Inverted))
|
||||
Inverted = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Location to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Location)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Location newItem = item as Location;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Location_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Location_Number))
|
||||
Number = newItem.Number;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Location_Inverted))
|
||||
Inverted = newItem.Inverted;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
1047
SabreTools.DatItems/Machine.cs
Normal file
1047
SabreTools.DatItems/Machine.cs
Normal file
File diff suppressed because it is too large
Load Diff
517
SabreTools.DatItems/Media.cs
Normal file
517
SabreTools.DatItems/Media.cs
Normal file
@@ -0,0 +1,517 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.FileTypes;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents Aaruformat images which use internal hashes
|
||||
/// </summary>
|
||||
[JsonObject("media"), XmlRoot("media")]
|
||||
public class Media : DatItem
|
||||
{
|
||||
#region Private instance variables
|
||||
|
||||
private byte[] _md5; // 16 bytes
|
||||
private byte[] _sha1; // 20 bytes
|
||||
private byte[] _sha256; // 32 bytes
|
||||
private byte[] _spamsum; // variable bytes
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Data MD5 hash
|
||||
/// </summary>
|
||||
[JsonProperty("md5", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("md5")]
|
||||
public string MD5
|
||||
{
|
||||
get { return _md5.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_md5); }
|
||||
set { _md5 = Utilities.StringToByteArray(Sanitizer.CleanMD5(value)); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data SHA-1 hash
|
||||
/// </summary>
|
||||
[JsonProperty("sha1", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("sha1")]
|
||||
public string SHA1
|
||||
{
|
||||
get { return _sha1.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha1); }
|
||||
set { _sha1 = Utilities.StringToByteArray(Sanitizer.CleanSHA1(value)); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data SHA-256 hash
|
||||
/// </summary>
|
||||
[JsonProperty("sha256", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("sha256")]
|
||||
public string SHA256
|
||||
{
|
||||
get { return _sha256.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha256); }
|
||||
set { _sha256 = Utilities.StringToByteArray(Sanitizer.CleanSHA256(value)); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// File SpamSum fuzzy hash
|
||||
/// </summary>
|
||||
[JsonProperty("spamsum", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("spamsum")]
|
||||
public string SpamSum
|
||||
{
|
||||
get { return _spamsum.IsNullOrEmpty() ? null : Encoding.UTF8.GetString(_spamsum); }
|
||||
set { _spamsum = Encoding.UTF8.GetBytes(value ?? string.Empty); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Media-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_MD5))
|
||||
MD5 = mappings[Field.DatItem_MD5];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_SHA1))
|
||||
SHA1 = mappings[Field.DatItem_SHA1];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_SHA256))
|
||||
SHA256 = mappings[Field.DatItem_SHA256];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_SpamSum))
|
||||
SpamSum = mappings[Field.DatItem_SpamSum];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Media object
|
||||
/// </summary>
|
||||
public Media()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Media;
|
||||
DupeType = 0x00;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a Media object from a BaseFile
|
||||
/// </summary>
|
||||
/// <param name="baseFile"></param>
|
||||
public Media(BaseFile baseFile)
|
||||
{
|
||||
Name = baseFile.Filename;
|
||||
_md5 = baseFile.MD5;
|
||||
_sha1 = baseFile.SHA1;
|
||||
_sha256 = baseFile.SHA256;
|
||||
_spamsum = baseFile.SpamSum;
|
||||
|
||||
ItemType = ItemType.Media;
|
||||
DupeType = 0x00;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Media()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
_md5 = this._md5,
|
||||
_sha1 = this._sha1,
|
||||
_sha256 = this._sha256,
|
||||
_spamsum = this._spamsum,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Media object to a BaseFile
|
||||
/// </summary>
|
||||
public BaseFile ConvertToBaseFile()
|
||||
{
|
||||
return new BaseFile()
|
||||
{
|
||||
Filename = this.Name,
|
||||
Parent = this.Machine?.Name,
|
||||
MD5 = this._md5,
|
||||
SHA1 = this._sha1,
|
||||
SHA256 = this._sha256,
|
||||
SpamSum = this._spamsum,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a media to the closest Rom approximation
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Rom ConvertToRom()
|
||||
{
|
||||
var rom = new Rom()
|
||||
{
|
||||
ItemType = ItemType.Rom,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name + ".aif",
|
||||
MD5 = this.MD5,
|
||||
SHA1 = this.SHA1,
|
||||
SHA256 = this.SHA256,
|
||||
SpamSum = this.SpamSum,
|
||||
};
|
||||
|
||||
return rom;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
bool dupefound = false;
|
||||
|
||||
// If we don't have a Media, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return dupefound;
|
||||
|
||||
// Otherwise, treat it as a Media
|
||||
Media newOther = other as Media;
|
||||
|
||||
// If we get a partial match
|
||||
if (HashMatch(newOther))
|
||||
dupefound = true;
|
||||
|
||||
return dupefound;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill any missing size and hash information from another Media
|
||||
/// </summary>
|
||||
/// <param name="other">Media to fill information from</param>
|
||||
public void FillMissingInformation(Media other)
|
||||
{
|
||||
if (_md5.IsNullOrEmpty() && !other._md5.IsNullOrEmpty())
|
||||
_md5 = other._md5;
|
||||
|
||||
if (_sha1.IsNullOrEmpty() && !other._sha1.IsNullOrEmpty())
|
||||
_sha1 = other._sha1;
|
||||
|
||||
if (_sha256.IsNullOrEmpty() && !other._sha256.IsNullOrEmpty())
|
||||
_sha256 = other._sha256;
|
||||
|
||||
if (_spamsum.IsNullOrEmpty() && !other._spamsum.IsNullOrEmpty())
|
||||
_spamsum = other._spamsum;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get unique duplicate suffix on name collision
|
||||
/// </summary>
|
||||
/// <returns>String representing the suffix</returns>
|
||||
public string GetDuplicateSuffix()
|
||||
{
|
||||
if (!_md5.IsNullOrEmpty())
|
||||
return $"_{MD5}";
|
||||
else if (!_sha1.IsNullOrEmpty())
|
||||
return $"_{SHA1}";
|
||||
else if (!_sha256.IsNullOrEmpty())
|
||||
return $"_{SHA256}";
|
||||
else if (!_spamsum.IsNullOrEmpty())
|
||||
return $"_{SpamSum}";
|
||||
else
|
||||
return "_1";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if there are no, non-empty hashes in common with another Media
|
||||
/// </summary>
|
||||
/// <param name="other">Media to compare against</param>
|
||||
/// <returns>True if at least one hash is not mutually exclusive, false otherwise</returns>
|
||||
private bool HasCommonHash(Media other)
|
||||
{
|
||||
return !(_md5.IsNullOrEmpty() ^ other._md5.IsNullOrEmpty())
|
||||
|| !(_sha1.IsNullOrEmpty() ^ other._sha1.IsNullOrEmpty())
|
||||
|| !(_sha256.IsNullOrEmpty() ^ other._sha256.IsNullOrEmpty())
|
||||
|| !(_spamsum.IsNullOrEmpty() ^ other._spamsum.IsNullOrEmpty());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the Media contains any hashes
|
||||
/// </summary>
|
||||
/// <returns>True if any hash exists, false otherwise</returns>
|
||||
private bool HasHashes()
|
||||
{
|
||||
return !_md5.IsNullOrEmpty()
|
||||
|| !_sha1.IsNullOrEmpty()
|
||||
|| !_sha256.IsNullOrEmpty()
|
||||
|| !_spamsum.IsNullOrEmpty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if any hashes are common with another Media
|
||||
/// </summary>
|
||||
/// <param name="other">Media to compare against</param>
|
||||
/// <returns>True if any hashes are in common, false otherwise</returns>
|
||||
private bool HashMatch(Media other)
|
||||
{
|
||||
// If either have no hashes, we return false, otherwise this would be a false positive
|
||||
if (!HasHashes() || !other.HasHashes())
|
||||
return false;
|
||||
|
||||
// If neither have hashes in common, we return false, otherwise this would be a false positive
|
||||
if (!HasCommonHash(other))
|
||||
return false;
|
||||
|
||||
// Return if all hashes match according to merge rules
|
||||
return ConditionalHashEquals(_md5, other._md5)
|
||||
&& ConditionalHashEquals(_sha1, other._sha1)
|
||||
&& ConditionalHashEquals(_spamsum, other._spamsum);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on MD5
|
||||
if (!filter.PassStringFilter(filter.DatItem_MD5, MD5))
|
||||
return false;
|
||||
|
||||
// Filter on SHA-1
|
||||
if (!filter.PassStringFilter(filter.DatItem_SHA1, SHA1))
|
||||
return false;
|
||||
|
||||
// Filter on SHA-256
|
||||
if (!filter.PassStringFilter(filter.DatItem_SHA256, SHA256))
|
||||
return false;
|
||||
|
||||
// Filter on SpamSum
|
||||
if (!filter.PassStringFilter(filter.DatItem_SpamSum, SpamSum))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_MD5))
|
||||
MD5 = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SHA1))
|
||||
SHA1 = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SHA256))
|
||||
SHA256 = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SpamSum))
|
||||
SpamSum = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Get the dictionary key that should be used for a given item and bucketing type
|
||||
/// </summary>
|
||||
/// <param name="bucketedBy">Field enum representing what key to get</param>
|
||||
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
|
||||
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
||||
/// <returns>String representing the key to be used for the DatItem</returns>
|
||||
public override string GetKey(Field bucketedBy, bool lower = true, bool norename = true)
|
||||
{
|
||||
// Set the output key as the default blank string
|
||||
string key = string.Empty;
|
||||
|
||||
// Now determine what the key should be based on the bucketedBy value
|
||||
switch (bucketedBy)
|
||||
{
|
||||
case Field.DatItem_MD5:
|
||||
key = MD5;
|
||||
break;
|
||||
|
||||
case Field.DatItem_SHA1:
|
||||
key = SHA1;
|
||||
break;
|
||||
|
||||
case Field.DatItem_SHA256:
|
||||
key = SHA256;
|
||||
break;
|
||||
|
||||
case Field.DatItem_SpamSum:
|
||||
key = SpamSum;
|
||||
break;
|
||||
|
||||
// Let the base handle generic stuff
|
||||
default:
|
||||
return base.GetKey(bucketedBy, lower, norename);
|
||||
}
|
||||
|
||||
// Double and triple check the key for corner cases
|
||||
if (key == null)
|
||||
key = string.Empty;
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Media to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Media)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Media newItem = item as Media;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_MD5))
|
||||
{
|
||||
if (string.IsNullOrEmpty(MD5) && !string.IsNullOrEmpty(newItem.MD5))
|
||||
MD5 = newItem.MD5;
|
||||
}
|
||||
|
||||
if (fields.Contains(Field.DatItem_SHA1))
|
||||
{
|
||||
if (string.IsNullOrEmpty(SHA1) && !string.IsNullOrEmpty(newItem.SHA1))
|
||||
SHA1 = newItem.SHA1;
|
||||
}
|
||||
|
||||
if (fields.Contains(Field.DatItem_SHA256))
|
||||
{
|
||||
if (string.IsNullOrEmpty(SHA256) && !string.IsNullOrEmpty(newItem.SHA256))
|
||||
SHA256 = newItem.SHA256;
|
||||
}
|
||||
|
||||
if (fields.Contains(Field.DatItem_SpamSum))
|
||||
{
|
||||
if (string.IsNullOrEmpty(SpamSum) && !string.IsNullOrEmpty(newItem.SpamSum))
|
||||
SpamSum = newItem.SpamSum;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
275
SabreTools.DatItems/Part.cs
Normal file
275
SabreTools.DatItems/Part.cs
Normal file
@@ -0,0 +1,275 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// SoftwareList part information
|
||||
/// </summary>
|
||||
/// <remarks>One Part can contain multiple PartFeature, DataArea, DiskArea, and DipSwitch items</remarks>
|
||||
[JsonObject("part"), XmlRoot("part")]
|
||||
public class Part : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonProperty("interface")]
|
||||
[XmlElement("interface")]
|
||||
public string Interface { get; set; }
|
||||
|
||||
[JsonProperty("features", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("features")]
|
||||
public List<PartFeature> Features { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool FeaturesSpecified { get { return Features != null && Features.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Part-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Part_Name))
|
||||
Name = mappings[Field.DatItem_Part_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Part_Interface))
|
||||
Interface = mappings[Field.DatItem_Part_Interface];
|
||||
|
||||
// Handle Feature-specific fields
|
||||
if (FeaturesSpecified)
|
||||
{
|
||||
foreach (PartFeature partFeature in Features)
|
||||
{
|
||||
partFeature.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Part object
|
||||
/// </summary>
|
||||
public Part()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Part;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Part()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Interface = this.Interface,
|
||||
Features = this.Features,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Part, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Part
|
||||
Part newOther = other as Part;
|
||||
|
||||
// If the Part information matches
|
||||
bool match = (Name == newOther.Name
|
||||
&& Interface == newOther.Interface);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the features match
|
||||
if (FeaturesSpecified)
|
||||
{
|
||||
foreach (PartFeature partFeature in Features)
|
||||
{
|
||||
match &= newOther.Features.Contains(partFeature);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on part name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Part_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on part interface
|
||||
if (!filter.PassStringFilter(filter.DatItem_Part_Interface, Interface))
|
||||
return false;
|
||||
|
||||
// Filter on features
|
||||
if (FeaturesSpecified)
|
||||
{
|
||||
foreach (PartFeature partFeature in Features)
|
||||
{
|
||||
if (!partFeature.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Part_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Part_Interface))
|
||||
Interface = null;
|
||||
|
||||
if (FeaturesSpecified)
|
||||
{
|
||||
foreach (PartFeature partFeature in Features)
|
||||
{
|
||||
partFeature.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Part to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Part)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Part newItem = item as Part;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Part_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Part_Interface))
|
||||
Interface = newItem.Interface;
|
||||
|
||||
// DatItem_Part_Feature_* doesn't make sense here
|
||||
// since not every part feature under the other item
|
||||
// can replace every part feature under this item
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
226
SabreTools.DatItems/PartFeature.cs
Normal file
226
SabreTools.DatItems/PartFeature.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents one part feature object
|
||||
/// </summary>
|
||||
[JsonObject("part_feature"), XmlRoot("part_feature")]
|
||||
public class PartFeature : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// PartFeature value
|
||||
/// </summary>
|
||||
[JsonProperty("value")]
|
||||
[XmlElement("value")]
|
||||
public string Value { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle PartFeature-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Part_Feature_Name))
|
||||
Name = mappings[Field.DatItem_Part_Feature_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Part_Feature_Value))
|
||||
Value = mappings[Field.DatItem_Part_Feature_Value];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty PartFeature object
|
||||
/// </summary>
|
||||
public PartFeature()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.PartFeature;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new PartFeature()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Value = this.Value,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a sample, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a PartFeature
|
||||
PartFeature newOther = other as PartFeature;
|
||||
|
||||
// If the archive information matches
|
||||
return (Name == newOther.Name && Value == newOther.Value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Part_Feature_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on value
|
||||
if (!filter.PassStringFilter(filter.DatItem_Part_Feature_Value, Value))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Part_Feature_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Part_Feature_Value))
|
||||
Value = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a PartFeature to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.PartFeature)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
PartFeature newItem = item as PartFeature;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Part_Feature_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Part_Feature_Value))
|
||||
Value = newItem.Value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
211
SabreTools.DatItems/Port.cs
Normal file
211
SabreTools.DatItems/Port.cs
Normal file
@@ -0,0 +1,211 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single port on a machine
|
||||
/// </summary>
|
||||
[JsonObject("port"), XmlRoot("port")]
|
||||
public class Port : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Tag for the port
|
||||
/// </summary>
|
||||
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("tag")]
|
||||
public string Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// List of analogs on the port
|
||||
/// </summary>
|
||||
[JsonProperty("analogs", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("analogs")]
|
||||
public List<Analog> Analogs { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool AnalogsSpecified { get { return Analogs != null && Analogs.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Port-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
||||
Tag = mappings[Field.DatItem_Tag];
|
||||
|
||||
if (AnalogsSpecified)
|
||||
{
|
||||
foreach (Analog analog in Analogs)
|
||||
{
|
||||
analog.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Port object
|
||||
/// </summary>
|
||||
public Port()
|
||||
{
|
||||
ItemType = ItemType.Port;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Port()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Tag = this.Tag,
|
||||
Analogs = this.Analogs,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Port, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Port
|
||||
Port newOther = other as Port;
|
||||
|
||||
// If the Port information matches
|
||||
bool match = (Tag == newOther.Tag);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the analogs match
|
||||
if (AnalogsSpecified)
|
||||
{
|
||||
foreach (Analog analog in Analogs)
|
||||
{
|
||||
match &= newOther.Analogs.Contains(analog);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on tag
|
||||
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
||||
return false;
|
||||
|
||||
// Filter on individual analogs
|
||||
if (AnalogsSpecified)
|
||||
{
|
||||
foreach (Analog analog in Analogs)
|
||||
{
|
||||
if (!analog.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Tag))
|
||||
Tag = null;
|
||||
|
||||
if (AnalogsSpecified)
|
||||
{
|
||||
foreach (Analog analog in Analogs)
|
||||
{
|
||||
analog.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Port to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Port)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Port newItem = item as Port;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Tag = newItem.Tag;
|
||||
|
||||
// DatItem_Analog_* doesn't make sense here
|
||||
// since not every analog under the other item
|
||||
// can replace every analog under this item
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
250
SabreTools.DatItems/RamOption.cs
Normal file
250
SabreTools.DatItems/RamOption.cs
Normal file
@@ -0,0 +1,250 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which RAM option(s) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("ramoption"), XmlRoot("ramoption")]
|
||||
public class RamOption : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determine whether the RamOption is default
|
||||
/// </summary>
|
||||
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("default")]
|
||||
public bool? Default { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DefaultSpecified { get { return Default != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// Determines the content of the RamOption
|
||||
/// </summary>
|
||||
[JsonProperty("content", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("content")]
|
||||
public string Content { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle BiosSet-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Default))
|
||||
Default = mappings[Field.DatItem_Default].AsYesNo();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Content))
|
||||
Content = mappings[Field.DatItem_Content];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty RamOption object
|
||||
/// </summary>
|
||||
public RamOption()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.RamOption;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new RamOption()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Default = this.Default,
|
||||
Content = this.Content,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a RamOption, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a RamOption
|
||||
RamOption newOther = other as RamOption;
|
||||
|
||||
// If the BiosSet information matches
|
||||
return (Name == newOther.Name && Default == newOther.Default && Content == newOther.Content);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on default
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Default, Default))
|
||||
return false;
|
||||
|
||||
// Filter on content
|
||||
if (!filter.PassStringFilter(filter.DatItem_Content, Content))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Default))
|
||||
Default = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Content))
|
||||
Content = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a RamOption to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.RamOption)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
RamOption newItem = item as RamOption;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Default))
|
||||
Default = newItem.Default;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Content))
|
||||
Content = newItem.Content;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
300
SabreTools.DatItems/Release.cs
Normal file
300
SabreTools.DatItems/Release.cs
Normal file
@@ -0,0 +1,300 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents release information about a set
|
||||
/// </summary>
|
||||
[JsonObject("release"), XmlRoot("release")]
|
||||
public class Release : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Release region(s)
|
||||
/// </summary>
|
||||
[JsonProperty("region", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("region")]
|
||||
public string Region { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Release language(s)
|
||||
/// </summary>
|
||||
[JsonProperty("language", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("language")]
|
||||
public string Language { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Date of release
|
||||
/// </summary>
|
||||
[JsonProperty("date", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("date")]
|
||||
public string Date { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default release, if applicable
|
||||
/// </summary>
|
||||
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("default")]
|
||||
public bool? Default { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DefaultSpecified { get { return Default != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Release-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Region))
|
||||
Region = mappings[Field.DatItem_Region];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Language))
|
||||
Language = mappings[Field.DatItem_Language];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Date))
|
||||
Date = mappings[Field.DatItem_Date];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Default))
|
||||
Default = mappings[Field.DatItem_Default].AsYesNo();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Release object
|
||||
/// </summary>
|
||||
public Release()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Release;
|
||||
Region = string.Empty;
|
||||
Language = string.Empty;
|
||||
Date = string.Empty;
|
||||
Default = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Release()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Region = this.Region,
|
||||
Language = this.Language,
|
||||
Date = this.Date,
|
||||
Default = this.Default,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a release return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Release
|
||||
Release newOther = other as Release;
|
||||
|
||||
// If the archive information matches
|
||||
return (Name == newOther.Name
|
||||
&& Region == newOther.Region
|
||||
&& Language == newOther.Language
|
||||
&& Date == newOther.Date
|
||||
&& Default == newOther.Default);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on region
|
||||
if (!filter.PassStringFilter(filter.DatItem_Region, Region))
|
||||
return false;
|
||||
|
||||
// Filter on language
|
||||
if (!filter.PassStringFilter(filter.DatItem_Language, Language))
|
||||
return false;
|
||||
|
||||
// Filter on date
|
||||
if (!filter.PassStringFilter(filter.DatItem_Date, Date))
|
||||
return false;
|
||||
|
||||
// Filter on default
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Default, Default))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Region))
|
||||
Region = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Language))
|
||||
Language = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Date))
|
||||
Date = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Default))
|
||||
Default = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Release to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Release)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Release newItem = item as Release;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Region))
|
||||
Region = newItem.Region;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Language))
|
||||
Language = newItem.Language;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Date))
|
||||
Date = newItem.Date;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Default))
|
||||
Default = newItem.Default;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
1329
SabreTools.DatItems/Rom.cs
Normal file
1329
SabreTools.DatItems/Rom.cs
Normal file
File diff suppressed because it is too large
Load Diff
25
SabreTools.DatItems/SabreTools.DatItems.csproj
Normal file
25
SabreTools.DatItems/SabreTools.DatItems.csproj
Normal file
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net48;netcoreapp3.1;net5.0</TargetFrameworks>
|
||||
<RuntimeIdentifiers>win10-x64;win7-x86</RuntimeIdentifiers>
|
||||
<Configurations>Debug;Release</Configurations>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)'=='net48'">
|
||||
<DefineConstants>NET_FRAMEWORK</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
|
||||
<ProjectReference Include="..\SabreTools.FileTypes\SabreTools.FileTypes.csproj" />
|
||||
<ProjectReference Include="..\SabreTools.Filtering\SabreTools.Filtering.csproj" />
|
||||
<ProjectReference Include="..\SabreTools.Logging\SabreTools.Logging.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
205
SabreTools.DatItems/Sample.cs
Normal file
205
SabreTools.DatItems/Sample.cs
Normal file
@@ -0,0 +1,205 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a (usually WAV-formatted) sample to be included for use in the set
|
||||
/// </summary>
|
||||
[JsonObject("sample"), XmlRoot("sample")]
|
||||
public class Sample : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Sample-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Sample object
|
||||
/// </summary>
|
||||
public Sample()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Sample;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Sample()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a sample, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Sample
|
||||
Sample newOther = other as Sample;
|
||||
|
||||
// If the archive information matches
|
||||
return (Name == newOther.Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Sample to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Sample)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Sample newItem = item as Sample;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
306
SabreTools.DatItems/Setting.cs
Normal file
306
SabreTools.DatItems/Setting.cs
Normal file
@@ -0,0 +1,306 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents one ListXML confsetting or dipvalue
|
||||
/// </summary>
|
||||
[JsonObject("setting"), XmlRoot("setting")]
|
||||
public class Setting : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Setting name
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Setting value
|
||||
/// </summary>
|
||||
[JsonProperty("value", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("value")]
|
||||
public string Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the setting is default or not
|
||||
/// </summary>
|
||||
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("default")]
|
||||
public bool? Default { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DefaultSpecified { get { return Default != null; } }
|
||||
|
||||
/// <summary>
|
||||
/// List of conditions on the setting
|
||||
/// </summary>
|
||||
[JsonProperty("conditions", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("conditions")]
|
||||
public List<Condition> Conditions { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ConditionsSpecified { get { return Conditions != null && Conditions.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Setting-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Setting_Name))
|
||||
Name = mappings[Field.DatItem_Setting_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Setting_Value))
|
||||
Value = mappings[Field.DatItem_Setting_Value];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Setting_Default))
|
||||
Default = mappings[Field.DatItem_Setting_Default].AsYesNo();
|
||||
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
condition.SetFields(mappings, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Setting object
|
||||
/// </summary>
|
||||
public Setting()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Setting;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Setting()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Value = this.Value,
|
||||
Default = this.Default,
|
||||
Conditions = this.Conditions,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Setting, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Setting
|
||||
Setting newOther = other as Setting;
|
||||
|
||||
// If the Setting information matches
|
||||
bool match = (Name == newOther.Name
|
||||
&& Value == newOther.Value
|
||||
&& Default == newOther.Default);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the conditions match
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
match &= newOther.Conditions.Contains(condition);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Setting_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on value
|
||||
if (!filter.PassStringFilter(filter.DatItem_Setting_Value, Value))
|
||||
return false;
|
||||
|
||||
// Filter on default
|
||||
if (!filter.PassBoolFilter(filter.DatItem_Setting_Default, Default))
|
||||
return false;
|
||||
|
||||
// Filter on individual conditions
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
if (!condition.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Setting_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Setting_Value))
|
||||
Value = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Setting_Default))
|
||||
Default = null;
|
||||
|
||||
if (ConditionsSpecified)
|
||||
{
|
||||
foreach (Condition condition in Conditions)
|
||||
{
|
||||
condition.RemoveFields(fields, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Setting to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Setting)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Setting newItem = item as Setting;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Setting_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Setting_Value))
|
||||
Value = newItem.Value;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Setting_Default))
|
||||
Default = newItem.Default;
|
||||
|
||||
// DatItem_Condition_* doesn't make sense here
|
||||
// since not every condition under the other item
|
||||
// can replace every condition under this item
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
226
SabreTools.DatItems/SharedFeature.cs
Normal file
226
SabreTools.DatItems/SharedFeature.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents one shared feature object
|
||||
/// </summary>
|
||||
[JsonObject("sharedfeat"), XmlRoot("sharedfeat")]
|
||||
public class SharedFeature : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// SharedFeature value
|
||||
/// </summary>
|
||||
[JsonProperty("value")]
|
||||
[XmlElement("value")]
|
||||
public string Value { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle SharedFeature-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Value))
|
||||
Value = mappings[Field.DatItem_Value];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty SharedFeature object
|
||||
/// </summary>
|
||||
public SharedFeature()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.SharedFeature;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new SharedFeature()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Value = this.Value,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a sample, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a SharedFeature
|
||||
SharedFeature newOther = other as SharedFeature;
|
||||
|
||||
// If the archive information matches
|
||||
return (Name == newOther.Name && Value == newOther.Value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on value
|
||||
if (!filter.PassStringFilter(filter.DatItem_Value, Value))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Value))
|
||||
Value = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a SharedFeature to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.SharedFeature)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
SharedFeature newItem = item as SharedFeature;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Value))
|
||||
Value = newItem.Value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
259
SabreTools.DatItems/Slot.cs
Normal file
259
SabreTools.DatItems/Slot.cs
Normal file
@@ -0,0 +1,259 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which Slot(s) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("slot"), XmlRoot("slot")]
|
||||
public class Slot : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Slot options associated with the slot
|
||||
/// </summary>
|
||||
[JsonProperty("slotoptions", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("slotoptions")]
|
||||
public List<SlotOption> SlotOptions { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool SlotOptionsSpecified { get { return SlotOptions != null && SlotOptions.Count > 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Slot-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (SlotOptionsSpecified)
|
||||
{
|
||||
foreach (SlotOption slotOption in SlotOptions)
|
||||
{
|
||||
slotOption.SetFields(mappings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Slot object
|
||||
/// </summary>
|
||||
public Slot()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.Slot;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Slot()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
SlotOptions = this.SlotOptions,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Slot, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Slot
|
||||
Slot newOther = other as Slot;
|
||||
|
||||
// If the Slot information matches
|
||||
bool match = (Name == newOther.Name);
|
||||
if (!match)
|
||||
return match;
|
||||
|
||||
// If the slot options match
|
||||
if (SlotOptionsSpecified)
|
||||
{
|
||||
foreach (SlotOption slotOption in SlotOptions)
|
||||
{
|
||||
match &= newOther.SlotOptions.Contains(slotOption);
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on individual slot options
|
||||
if (SlotOptionsSpecified)
|
||||
{
|
||||
foreach (SlotOption slotOption in SlotOptions)
|
||||
{
|
||||
if (!slotOption.PassesFilter(filter, true))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (SlotOptionsSpecified)
|
||||
{
|
||||
foreach (SlotOption slotOption in SlotOptions)
|
||||
{
|
||||
slotOption.RemoveFields(fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Slot to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Slot)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Slot newItem = item as Slot;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
// DatItem_SlotOption_* doesn't make sense here
|
||||
// since not every slot option under the other item
|
||||
// can replace every slot option under this item
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
252
SabreTools.DatItems/SlotOption.cs
Normal file
252
SabreTools.DatItems/SlotOption.cs
Normal file
@@ -0,0 +1,252 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents one ListXML slotoption
|
||||
/// </summary>
|
||||
[JsonObject("slotoption"), XmlRoot("slotoption")]
|
||||
public class SlotOption : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Slot option name
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Referenced device name
|
||||
/// </summary>
|
||||
[JsonProperty("devname")]
|
||||
[XmlElement("devname")]
|
||||
public string DeviceName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines if this slot option is default or not
|
||||
/// </summary>
|
||||
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("default")]
|
||||
public bool? Default { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool DefaultSpecified { get { return Default != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle SlotOption-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_SlotOption_Name))
|
||||
Name = mappings[Field.DatItem_SlotOption_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_SlotOption_DeviceName))
|
||||
DeviceName = mappings[Field.DatItem_SlotOption_DeviceName];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_SlotOption_Default))
|
||||
Default = mappings[Field.DatItem_SlotOption_Default].AsYesNo();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty SlotOption object
|
||||
/// </summary>
|
||||
public SlotOption()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.SlotOption;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new SlotOption()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
DeviceName = this.DeviceName,
|
||||
Default = this.Default,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a SlotOption, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a SlotOption
|
||||
SlotOption newOther = other as SlotOption;
|
||||
|
||||
// If the SlotOption information matches
|
||||
return (Name == newOther.Name
|
||||
&& DeviceName == newOther.DeviceName
|
||||
&& Default == newOther.Default);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_SlotOption_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on device name
|
||||
if (!filter.PassStringFilter(filter.DatItem_SlotOption_DeviceName, DeviceName))
|
||||
return false;
|
||||
|
||||
// Filter on default
|
||||
if (!filter.PassBoolFilter(filter.DatItem_SlotOption_Default, Default))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_SlotOption_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SlotOption_DeviceName))
|
||||
DeviceName = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SlotOption_Default))
|
||||
Default = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a SlotOption to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.SlotOption)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
SlotOption newItem = item as SlotOption;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_SlotOption_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SlotOption_DeviceName))
|
||||
DeviceName = newItem.DeviceName;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SlotOption_Default))
|
||||
Default = newItem.Default;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
256
SabreTools.DatItems/SoftwareList.cs
Normal file
256
SabreTools.DatItems/SoftwareList.cs
Normal file
@@ -0,0 +1,256 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents which SoftwareList(s) is associated with a set
|
||||
/// </summary>
|
||||
[JsonObject("softwarelist"), XmlRoot("softwarelist")]
|
||||
public class SoftwareList : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item
|
||||
/// </summary>
|
||||
[JsonProperty("name")]
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Status of the softare list according to the machine
|
||||
/// </summary>
|
||||
[JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
[XmlElement("status")]
|
||||
public SoftwareListStatus Status { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool StatusSpecified { get { return Status != SoftwareListStatus.NULL; } }
|
||||
|
||||
/// <summary>
|
||||
/// Filter to apply to the software list
|
||||
/// </summary>
|
||||
[JsonProperty("filter", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("filter")]
|
||||
public string Filter { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for a DatItem
|
||||
/// </summary>
|
||||
/// <returns>Name if available, null otherwise</returns>
|
||||
public override string GetName()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle SoftwareList-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Name))
|
||||
Name = mappings[Field.DatItem_Name];
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_SoftwareListStatus))
|
||||
Status = mappings[Field.DatItem_SoftwareListStatus].AsSoftwareListStatus();
|
||||
|
||||
if (mappings.Keys.Contains(Field.DatItem_Filter))
|
||||
Filter = mappings[Field.DatItem_Filter];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty SoftwareList object
|
||||
/// </summary>
|
||||
public SoftwareList()
|
||||
{
|
||||
Name = string.Empty;
|
||||
ItemType = ItemType.SoftwareList;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new SoftwareList()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Name = this.Name,
|
||||
Status = this.Status,
|
||||
Filter = this.Filter,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a sample, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a SoftwareList
|
||||
SoftwareList newOther = other as SoftwareList;
|
||||
|
||||
// If the SoftwareList information matches
|
||||
return (Name == newOther.Name
|
||||
&& Status == newOther.Status
|
||||
&& Filter == newOther.Filter);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Clean a DatItem according to the cleaner
|
||||
/// </summary>
|
||||
/// <param name="cleaner">Cleaner to implement</param>
|
||||
public override void Clean(Cleaner cleaner)
|
||||
{
|
||||
// Clean common items first
|
||||
base.Clean(cleaner);
|
||||
|
||||
// If we're stripping unicode characters, strip item name
|
||||
if (cleaner?.RemoveUnicode == true)
|
||||
Name = Sanitizer.RemoveUnicodeCharacters(Name);
|
||||
|
||||
// If we are in NTFS trim mode, trim the game name
|
||||
if (cleaner?.Trim == true)
|
||||
{
|
||||
// Windows max name length is 260
|
||||
int usableLength = 260 - Machine.Name.Length - (cleaner.Root?.Length ?? 0);
|
||||
if (Name.Length > usableLength)
|
||||
{
|
||||
string ext = Path.GetExtension(Name);
|
||||
Name = Name.Substring(0, usableLength - ext.Length);
|
||||
Name += ext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on item name
|
||||
if (!filter.PassStringFilter(filter.DatItem_Name, Name))
|
||||
return false;
|
||||
|
||||
// Filter on status
|
||||
if (filter.DatItem_SoftwareListStatus.MatchesPositive(SoftwareListStatus.NULL, Status) == false)
|
||||
return false;
|
||||
if (filter.DatItem_SoftwareListStatus.MatchesNegative(SoftwareListStatus.NULL, Status) == true)
|
||||
return false;
|
||||
|
||||
// Filter on filter
|
||||
if (!filter.PassStringFilter(filter.DatItem_Filter, Filter))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = null;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SoftwareListStatus))
|
||||
Status = SoftwareListStatus.NULL;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Filter))
|
||||
Filter = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set internal names to match One Rom Per Game (ORPG) logic
|
||||
/// </summary>
|
||||
public override void SetOneRomPerGame()
|
||||
{
|
||||
string[] splitname = Name.Split('.');
|
||||
Machine.Name += $"/{string.Join(".", splitname.Take(splitname.Length > 1 ? splitname.Length - 1 : 1))}";
|
||||
Name = Path.GetFileName(Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a SoftwareList to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.SoftwareList)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
SoftwareList newItem = item as SoftwareList;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Name))
|
||||
Name = newItem.Name;
|
||||
|
||||
if (fields.Contains(Field.DatItem_SoftwareListStatus))
|
||||
Status = newItem.Status;
|
||||
|
||||
if (fields.Contains(Field.DatItem_Filter))
|
||||
Filter = newItem.Filter;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
161
SabreTools.DatItems/Sound.cs
Normal file
161
SabreTools.DatItems/Sound.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Filtering;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the sound output for a machine
|
||||
/// </summary>
|
||||
[JsonObject("sound"), XmlRoot("sound")]
|
||||
public class Sound : DatItem
|
||||
{
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Number of speakers or channels
|
||||
/// </summary>
|
||||
[JsonProperty("channels", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
[XmlElement("channels")]
|
||||
public long? Channels { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ChannelsSpecified { get { return Channels != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Set fields with given values
|
||||
/// </summary>
|
||||
/// <param name="mappings">Mappings dictionary</param>
|
||||
public override void SetFields(Dictionary<Field, string> mappings)
|
||||
{
|
||||
// Set base fields
|
||||
base.SetFields(mappings);
|
||||
|
||||
// Handle Sound-specific fields
|
||||
if (mappings.Keys.Contains(Field.DatItem_Channels))
|
||||
Channels = Sanitizer.CleanLong(mappings[Field.DatItem_Channels]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a default, empty Sound object
|
||||
/// </summary>
|
||||
public Sound()
|
||||
{
|
||||
ItemType = ItemType.Sound;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cloning Methods
|
||||
|
||||
public override object Clone()
|
||||
{
|
||||
return new Sound()
|
||||
{
|
||||
ItemType = this.ItemType,
|
||||
DupeType = this.DupeType,
|
||||
|
||||
Machine = this.Machine.Clone() as Machine,
|
||||
Source = this.Source.Clone() as Source,
|
||||
Remove = this.Remove,
|
||||
|
||||
Channels = this.Channels,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparision Methods
|
||||
|
||||
public override bool Equals(DatItem other)
|
||||
{
|
||||
// If we don't have a Sound, return false
|
||||
if (ItemType != other.ItemType)
|
||||
return false;
|
||||
|
||||
// Otherwise, treat it as a Sound
|
||||
Sound newOther = other as Sound;
|
||||
|
||||
// If the Sound information matches
|
||||
return (Channels == newOther.Channels);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Filtering
|
||||
|
||||
/// <summary>
|
||||
/// Check to see if a DatItem passes the filter
|
||||
/// </summary>
|
||||
/// <param name="filter">Filter to check against</param>
|
||||
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
||||
/// <returns>True if the item passed the filter, false otherwise</returns>
|
||||
public override bool PassesFilter(Filter filter, bool sub = false)
|
||||
{
|
||||
// Check common fields first
|
||||
if (!base.PassesFilter(filter, sub))
|
||||
return false;
|
||||
|
||||
// Filter on channels
|
||||
if (!filter.PassLongFilter(filter.DatItem_Channels, Channels))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove fields from the DatItem
|
||||
/// </summary>
|
||||
/// <param name="fields">List of Fields to remove</param>
|
||||
public override void RemoveFields(List<Field> fields)
|
||||
{
|
||||
// Remove common fields first
|
||||
base.RemoveFields(fields);
|
||||
|
||||
// Remove the fields
|
||||
if (fields.Contains(Field.DatItem_Channels))
|
||||
Channels = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Replace fields from another item
|
||||
/// </summary>
|
||||
/// <param name="item">DatItem to pull new information from</param>
|
||||
/// <param name="fields">List of Fields representing what should be updated</param>
|
||||
public override void ReplaceFields(DatItem item, List<Field> fields)
|
||||
{
|
||||
// Replace common fields first
|
||||
base.ReplaceFields(item, fields);
|
||||
|
||||
// If we don't have a Sound to replace from, ignore specific fields
|
||||
if (item.ItemType != ItemType.Sound)
|
||||
return;
|
||||
|
||||
// Cast for easier access
|
||||
Sound newItem = item as Sound;
|
||||
|
||||
// Replace the fields
|
||||
if (fields.Contains(Field.DatItem_Channels))
|
||||
Channels = newItem.Channels;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
43
SabreTools.DatItems/Source.cs
Normal file
43
SabreTools.DatItems/Source.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
/// <summary>
|
||||
/// Source information wrapper
|
||||
/// </summary>
|
||||
public class Source : ICloneable
|
||||
{
|
||||
/// <summary>
|
||||
/// Source index
|
||||
/// </summary>
|
||||
public int Index { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Source name
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="id">Source ID, default 0</param>
|
||||
/// <param name="source">Source name, default null</param>
|
||||
public Source(int id = 0, string source = null)
|
||||
{
|
||||
Index = id;
|
||||
Name = source;
|
||||
}
|
||||
|
||||
#region Cloning
|
||||
|
||||
/// <summary>
|
||||
/// Clone the current object
|
||||
/// </summary>
|
||||
public object Clone()
|
||||
{
|
||||
return new Source(Index, Name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user