Files
SabreTools/SabreTools.DatItems/DataArea.cs

254 lines
7.6 KiB
C#
Raw Normal View History

2020-09-04 14:10:35 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-09-07 22:00:02 -07:00
using System.Xml.Serialization;
2020-09-04 14:10:35 -07:00
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2020-12-08 13:48:57 -08:00
using SabreTools.Filtering;
2020-09-04 14:10:35 -07:00
using Newtonsoft.Json;
2020-12-08 15:15:41 -08:00
namespace SabreTools.DatItems
2020-09-04 14:10:35 -07:00
{
/// <summary>
/// SoftwareList dataarea information
/// </summary>
/// <remarks>One DataArea can contain multiple Rom items</remarks>
2020-09-08 10:12:41 -07:00
[JsonObject("dataarea"), XmlRoot("dataarea")]
2020-09-04 14:10:35 -07:00
public class DataArea : DatItem
{
#region Fields
/// <summary>
/// Name of the item
/// </summary>
[JsonProperty("name", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("name")]
2020-09-04 14:10:35 -07:00
public string Name { get; set; }
/// <summary>
/// Total size of the area
/// </summary>
[JsonProperty("size", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("size")]
2020-09-04 14:10:35 -07:00
public long? Size { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool SizeSpecified { get { return Size != null; } }
2020-09-04 14:10:35 -07:00
/// <summary>
/// Word width for the area
/// </summary>
[JsonProperty("width", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("width")]
2020-09-04 14:10:35 -07:00
public long? Width { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool WidthSpecified { get { return Width != null; } }
2020-09-04 14:10:35 -07:00
/// <summary>
/// Byte endianness of the area
/// </summary>
[JsonProperty("endianness", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("endianness")]
2020-09-04 14:10:35 -07:00
public Endianness Endianness { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool EndiannessSpecified { get { return Endianness != Endianness.NULL; } }
2020-09-04 14:10:35 -07:00
#endregion
#region Accessors
/// <inheritdoc/>
2020-09-04 14:10:35 -07:00
public override string GetName()
{
return Name;
}
/// <inheritdoc/>
public override void SetName(string name)
{
Name = name;
}
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void SetFields(
Dictionary<DatItemField, string> datItemMappings,
Dictionary<MachineField, string> machineMappings)
2020-09-04 14:10:35 -07:00
{
// Set base fields
2020-12-13 13:22:06 -08:00
base.SetFields(datItemMappings, machineMappings);
2020-09-04 14:10:35 -07:00
// Handle DataArea-specific fields
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.AreaName))
Name = datItemMappings[DatItemField.AreaName];
2020-09-04 14:10:35 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.AreaSize))
Size = Utilities.CleanLong(datItemMappings[DatItemField.AreaSize]);
2020-09-04 14:10:35 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.AreaWidth))
Width = Utilities.CleanLong(datItemMappings[DatItemField.AreaWidth]);
2020-09-04 14:10:35 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.AreaEndianness))
Endianness = datItemMappings[DatItemField.AreaEndianness].AsEndianness();
2020-09-04 14:10:35 -07:00
}
#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
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override bool PassesFilter(Cleaner cleaner, bool sub = false)
2020-09-30 13:25:40 -07:00
{
// Check common fields first
2020-12-13 13:22:06 -08:00
if (!base.PassesFilter(cleaner, sub))
return false;
2020-09-04 14:10:35 -07:00
// Filter on area name
2020-12-13 13:22:06 -08:00
if (!Filter.PassStringFilter(cleaner.DatItemFilter.AreaName, Name))
2020-09-04 14:10:35 -07:00
return false;
// Filter on area size
2020-12-13 13:22:06 -08:00
if (!Filter.PassLongFilter(cleaner.DatItemFilter.AreaSize, Size))
2020-09-04 14:10:35 -07:00
return false;
2020-09-08 10:56:37 -07:00
// Filter on area width
2020-12-13 13:22:06 -08:00
if (!Filter.PassLongFilter(cleaner.DatItemFilter.AreaWidth, Width))
2020-09-04 14:10:35 -07:00
return false;
// Filter on area endianness
2020-12-13 13:22:06 -08:00
if (cleaner.DatItemFilter.AreaEndianness.MatchesPositive(Endianness.NULL, Endianness) == false)
2020-09-04 14:10:35 -07:00
return false;
2020-12-13 13:22:06 -08:00
if (cleaner.DatItemFilter.AreaEndianness.MatchesNegative(Endianness.NULL, Endianness) == true)
2020-09-04 14:10:35 -07:00
return false;
return true;
}
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void RemoveFields(
List<DatItemField> datItemFields,
List<MachineField> machineFields)
2020-09-04 14:10:35 -07:00
{
// Remove common fields first
2020-12-13 13:22:06 -08:00
base.RemoveFields(datItemFields, machineFields);
2020-09-04 14:10:35 -07:00
// Remove the fields
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.AreaName))
2020-09-04 14:10:35 -07:00
Name = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.AreaSize))
2020-09-04 14:10:35 -07:00
Size = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.AreaWidth))
2020-09-04 14:10:35 -07:00
Width = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.AreaEndianness))
2020-09-04 14:10:35 -07:00
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
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void ReplaceFields(
DatItem item,
List<DatItemField> datItemFields,
List<MachineField> machineFields)
2020-09-04 14:10:35 -07:00
{
// Replace common fields first
2020-12-13 13:22:06 -08:00
base.ReplaceFields(item, datItemFields, machineFields);
2020-09-04 14:10:35 -07:00
// 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
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.AreaName))
2020-09-04 14:10:35 -07:00
Name = newItem.Name;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.AreaSize))
2020-09-04 14:10:35 -07:00
Size = newItem.Size;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.AreaWidth))
2020-09-04 14:10:35 -07:00
Width = newItem.Width;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.AreaEndianness))
2020-09-04 14:10:35 -07:00
Endianness = newItem.Endianness;
}
#endregion
}
}