Name is not guaranteed

This commit is contained in:
Matt Nadareski
2020-09-02 12:19:12 -07:00
parent c77bcca9ad
commit bd92f8993a
20 changed files with 1397 additions and 164 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SabreTools.Library.Filtering;
@@ -15,6 +16,12 @@ namespace SabreTools.Library.DatItems
{
#region Fields
/// <summary>
/// Name of the item
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }
/// <summary>
/// Description of the BIOS
/// </summary>
@@ -31,6 +38,15 @@ namespace SabreTools.Library.DatItems
#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>
@@ -41,6 +57,9 @@ namespace SabreTools.Library.DatItems
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();
@@ -121,6 +140,33 @@ namespace SabreTools.Library.DatItems
#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>
@@ -132,6 +178,12 @@ namespace SabreTools.Library.DatItems
if (!base.PassesFilter(filter))
return false;
// Filter on item name
if (filter.DatItem_Name.MatchesPositiveSet(Name) == false)
return false;
if (filter.DatItem_Name.MatchesNegativeSet(Name) == true)
return false;
// Filter on description
if (filter.DatItem_Description.MatchesPositiveSet(Description) == false)
return false;
@@ -155,6 +207,9 @@ namespace SabreTools.Library.DatItems
base.RemoveFields(fields);
// Remove the fields
if (fields.Contains(Field.DatItem_Name))
Name = null;
if (fields.Contains(Field.DatItem_Description))
Description = null;
@@ -162,6 +217,16 @@ namespace SabreTools.Library.DatItems
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
@@ -184,6 +249,9 @@ namespace SabreTools.Library.DatItems
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;