Files
SabreTools/SabreTools.DatItems/Machine.cs

124 lines
4.0 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;
using SabreTools.Core.Filter;
2024-03-12 23:38:06 -04:00
using SabreTools.Core.Tools;
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")]
2024-03-12 22:26:54 -04:00
public sealed class Machine : ModelBackedItem<Models.Metadata.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
2024-03-12 11:53:58 -04:00
#region Constructors
2020-09-07 22:00:02 -07:00
2024-03-19 15:38:30 -04:00
public Machine()
{
_internal = new Models.Metadata.Machine();
}
2020-08-20 22:42:04 -07:00
2024-03-12 11:53:58 -04: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;
2020-08-20 22:42:04 -07:00
2024-03-12 11:53:58 -04:00
// Populate the internal machine from non-filter fields
_internal = new Models.Metadata.Machine();
2024-03-12 11:53:58 -04:00
foreach (string fieldName in nonItemFields)
{
if (machine.ContainsKey(fieldName))
_internal[fieldName] = machine[fieldName];
2024-03-12 11:53:58 -04:00
}
}
2024-03-09 23:43:43 -05:00
#endregion
#region Accessors
2024-03-11 11:07:21 -04:00
/// <summary>
/// Get a clone of the current internal model
/// </summary>
public Models.Metadata.Machine GetInternalClone() => (_internal.Clone() as Models.Metadata.Machine)!;
2024-03-11 11:07:21 -04:00
2020-09-07 14:47:27 -07: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()
{
_internal = this._internal.Clone() as Models.Metadata.Machine ?? [],
};
}
#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(_internal);
2024-03-05 02:56:50 -05:00
/// <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(_internal, 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?._internal, _internal, fieldName);
2024-03-05 20:07:38 -05:00
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(_internal, fieldName, value);
2024-03-05 02:20:12 -05:00
2024-03-05 01:42:42 -05:00
#endregion
}
}