2017-01-27 16:42:24 -08:00
|
|
|
|
using System;
|
2020-09-07 14:47:27 -07:00
|
|
|
|
using System.Xml.Serialization;
|
2020-08-21 17:27:11 -07:00
|
|
|
|
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;
|
2017-01-08 22:48:19 -08:00
|
|
|
|
|
2020-12-08 15:15:41 -08:00
|
|
|
|
namespace SabreTools.DatItems
|
2016-10-24 12:58:57 -07:00
|
|
|
|
{
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents the information specific to a set/game/machine
|
|
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonObject("machine"), XmlRoot("machine")]
|
2019-01-08 12:11:55 -08:00
|
|
|
|
public class Machine : ICloneable
|
|
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
|
#region Constants
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Trurip/EmuArc Machine developer
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public const string DeveloperKey = "DEVELOPER";
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Trurip/EmuArc Game genre
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public const string GenreKey = "GENRE";
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Trurip/EmuArc Title ID
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public const string TitleIDKey = "TITLEID";
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2020-08-20 13:17:14 -07:00
|
|
|
|
#region Fields
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? RelatedTo { get; set; } = null;
|
2020-08-20 22:42:04 -07:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2020-08-21 17:27:11 -07:00
|
|
|
|
/// <summary>
|
2024-03-09 23:43:43 -05:00
|
|
|
|
/// Internal Machine model
|
2020-08-21 17:27:11 -07:00
|
|
|
|
/// </summary>
|
2024-03-09 23:43:43 -05:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
|
private Models.Metadata.Machine _machine = [];
|
2020-08-21 17:27:11 -07:00
|
|
|
|
|
2024-03-09 23:43:43 -05:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Accessors
|
2020-08-21 17:27:11 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-09 23:43:43 -05:00
|
|
|
|
/// Get the value from a field based on the type provided
|
2020-08-21 17:27:11 -07:00
|
|
|
|
/// </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;
|
2020-08-21 17:27:11 -07:00
|
|
|
|
|
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
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
2024-03-10 21:03:53 -04:00
|
|
|
|
public Machine() { }
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2024-03-09 21:34:26 -05:00
|
|
|
|
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];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
#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()
|
|
|
|
|
|
{
|
2020-08-20 16:49:21 -07:00
|
|
|
|
#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
|
|
|
|
|
2020-08-20 16:49:21 -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
|
2019-01-08 12:11:55 -08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
2016-10-24 12:58:57 -07:00
|
|
|
|
}
|