Files
SabreTools/SabreTools.DatItems/Machine.cs

249 lines
8.3 KiB
C#
Raw Normal View History

2017-01-27 16:42:24 -08:00
using System;
2020-09-07 14:47:27 -07:00
using System.Xml.Serialization;
using Newtonsoft.Json;
2023-08-14 15:12:26 -04:00
using SabreTools.Core;
2024-03-05 01:42:42 -05:00
using SabreTools.Filter;
2020-12-08 15:15:41 -08:00
namespace SabreTools.DatItems
{
/// <summary>
/// Represents the information specific to a set/game/machine
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("machine"), XmlRoot("machine")]
public class Machine : ICloneable
{
2020-08-20 13:17:14 -07:00
#region Fields
2020-08-24 22:25:47 -07:00
// TODO: Should this be a separate object for TruRip?
2020-09-07 22:00:02 -07:00
#region Logiqx EmuArc
2020-08-20 22:42:04 -07:00
/// <summary>
/// Title ID
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("titleid", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("titleid")]
public string? TitleID { get; set; } = null;
2020-08-20 22:42:04 -07:00
/// <summary>
/// Machine developer
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("developer", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("developer")]
public string? Developer { get; set; } = null;
2020-08-20 22:42:04 -07:00
/// <summary>
/// Game genre
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("genre", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("genre")]
public string? Genre { get; set; } = null;
2020-08-20 22:42:04 -07:00
/// <summary>
/// Game subgenre
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("subgenre", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("subgenre")]
public string? Subgenre { get; set; } = null;
2020-08-20 22:42:04 -07:00
/// <summary>
/// Game ratings
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("ratings", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("ratings")]
public string? Ratings { get; set; } = null;
2020-08-20 22:42:04 -07:00
/// <summary>
/// Game score
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("score", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("score")]
public string? Score { get; set; } = null;
2020-08-20 22:42:04 -07:00
/// <summary>
/// Is the machine enabled
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("enabled", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("enabled")]
public string? Enabled { get; set; } = null; // bool?
2020-08-20 22:42:04 -07:00
/// <summary>
/// Does the game have a CRC check
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("hascrc", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("hascrc")]
2020-08-25 22:13:49 -07:00
public bool? Crc { get; set; } = null;
2020-08-20 22:42:04 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool CrcSpecified { get { return Crc != null; } }
2020-08-20 22:42:04 -07:00
/// <summary>
/// Machine relations
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("relatedto", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("relatedto")]
public string? RelatedTo { get; set; } = null;
2020-08-20 22:42:04 -07:00
#endregion
/// <summary>
2024-03-09 23:43:43 -05:00
/// Internal Machine model
/// </summary>
2024-03-09 23:43:43 -05:00
[JsonIgnore]
private Models.Metadata.Machine _machine = [];
2024-03-09 23:43:43 -05:00
#endregion
#region Accessors
/// <summary>
2024-03-09 23:43:43 -05:00
/// Get the value from a field based on the type provided
/// </summary>
2024-03-09 23:43:43 -05:00
/// <typeparam name="T">Type of the value to get from the internal model</typeparam>
/// <param name="fieldName">Field to retrieve</param>
/// <returns>Value from the field, if possible</returns>
public T? GetFieldValue<T>(string? fieldName)
2023-08-14 15:12:26 -04:00
{
2024-03-09 23:43:43 -05:00
// Invalid field cannot be processed
if (string.IsNullOrEmpty(fieldName))
return default;
2024-03-09 23:43:43 -05:00
// Get the value based on the type
return _machine.Read<T>(fieldName!);
}
2020-08-20 14:36:36 -07:00
/// <summary>
2024-03-09 23:43:43 -05:00
/// Set the value from a field based on the type provided
2020-08-20 14:36:36 -07:00
/// </summary>
2024-03-09 23:43:43 -05:00
/// <typeparam name="T">Type of the value to set in the internal model</typeparam>
/// <param name="fieldName">Field to set</param>
/// <param name="value">Value to set</param>
/// <returns>True if the value was set, false otherwise</returns>
public bool SetFieldValue<T>(string? fieldName, T? value)
2023-08-14 15:12:26 -04:00
{
2024-03-09 23:43:43 -05:00
// Invalid field cannot be processed
if (string.IsNullOrEmpty(fieldName))
return false;
2020-08-20 14:36:36 -07:00
2024-03-09 23:43:43 -05:00
// Set the value based on the type
_machine[fieldName!] = value;
return true;
}
2020-09-07 14:47:27 -07:00
#endregion
#region Constructors
/// <summary>
/// Create a new Machine object
/// </summary>
public Machine()
{
}
2024-03-09 21:34:26 -05:00
/// <summary>
/// Create a new Machine object from an existing metadata model
/// </summary>
/// <param name="machine">Machine metadata model</param>
public Machine(Models.Metadata.Machine machine)
{
// Get all fields to automatically copy without processing
var nonItemFields = TypeHelper.GetConstants(typeof(Models.Metadata.Machine));
if (nonItemFields == null)
return;
// Populate the internal machine from non-filter fields
_machine = [];
foreach (string fieldName in nonItemFields)
{
if (machine.ContainsKey(fieldName))
_machine[fieldName] = machine[fieldName];
}
}
/// <summary>
/// Create a new Machine object with the included information
/// </summary>
/// <param name="name">Name of the machine</param>
/// <param name="description">Description of the machine</param>
public Machine(string name, string description)
{
2024-03-09 23:43:43 -05:00
SetFieldValue<string?>(Models.Metadata.Machine.NameKey, name);
SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, description);
}
#endregion
#region Cloning methods
/// <summary>
/// Create a clone of the current machine
/// </summary>
/// <returns>New machine with the same values as the current one</returns>
public object Clone()
{
return new Machine()
{
#region Common
2024-02-28 19:19:50 -05:00
_machine = this._machine.Clone() as Models.Metadata.Machine ?? [],
2020-08-20 14:36:36 -07:00
#endregion
2020-08-20 22:42:04 -07:00
#region Logiqx EmuArc
TitleID = this.TitleID,
Developer = this.Developer,
Genre = this.Genre,
Subgenre = this.Subgenre,
Ratings = this.Ratings,
Score = this.Score,
Enabled = this.Enabled,
2020-08-25 22:13:49 -07:00
Crc = this.Crc,
2020-08-20 22:42:04 -07:00
RelatedTo = this.RelatedTo,
#endregion
};
}
#endregion
2024-03-05 01:42:42 -05:00
#region Manipulation
/// <summary>
2024-03-05 02:56:50 -05:00
/// Runs a filter and determines if it passes or not
/// </summary>
/// <param name="filterRunner">Filter runner to use for checking</param>
/// <returns>True if the Machine passes the filter, false otherwise</returns>
public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_machine);
/// <summary>
2024-03-05 01:42:42 -05:00
/// Remove a field from the Machine
/// </summary>
2024-03-05 16:37:52 -05:00
/// <param name="fieldName">Field to remove</param>
2024-03-05 01:42:42 -05:00
/// <returns>True if the removal was successful, false otherwise</returns>
2024-03-05 16:37:52 -05:00
public bool RemoveField(string? fieldName)
=> FieldManipulator.RemoveField(_machine, fieldName);
2024-03-05 01:42:42 -05:00
2024-03-05 20:07:38 -05:00
/// <summary>
/// Replace a field from another Machine
/// </summary>
/// <param name="other">Machine to replace field from</param>
/// <param name="fieldName">Field to replace</param>
/// <returns>True if the replacement was successful, false otherwise</returns>
public bool ReplaceField(Machine? other, string? fieldName)
=> FieldManipulator.ReplaceField(other?._machine, _machine, fieldName);
2024-03-05 02:20:12 -05:00
/// <summary>
/// Set a field in the Machine from a mapping string
/// </summary>
2024-03-05 17:17:40 -05:00
/// <param name="fieldName">Field to set</param>
2024-03-05 02:20:12 -05:00
/// <param name="value">String representing the value to set</param>
/// <returns>True if the setting was successful, false otherwise</returns>
/// <remarks>This only performs minimal validation before setting</remarks>
2024-03-05 17:17:40 -05:00
public bool SetField(string? fieldName, string value)
=> FieldManipulator.SetField(_machine, fieldName, value);
2024-03-05 02:20:12 -05:00
2024-03-05 01:42:42 -05:00
#endregion
}
}