mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Use ModelBackedItem for header, machine, and datitem
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
using System.Xml.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.Filter;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
@@ -11,7 +10,7 @@ namespace SabreTools.DatItems
|
||||
/// Represents the information specific to a set/game/machine
|
||||
/// </summary>
|
||||
[JsonObject("machine"), XmlRoot("machine")]
|
||||
public class Machine : ICloneable
|
||||
public class Machine : ModelBackedItem<Models.Metadata.Machine>, ICloneable
|
||||
{
|
||||
#region Constants
|
||||
|
||||
@@ -32,16 +31,6 @@ namespace SabreTools.DatItems
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
/// <summary>
|
||||
/// Internal Machine model
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
private Models.Metadata.Machine _machine = [];
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public Machine() { }
|
||||
@@ -54,11 +43,11 @@ namespace SabreTools.DatItems
|
||||
return;
|
||||
|
||||
// Populate the internal machine from non-filter fields
|
||||
_machine = [];
|
||||
_internal = new Models.Metadata.Machine();
|
||||
foreach (string fieldName in nonItemFields)
|
||||
{
|
||||
if (machine.ContainsKey(fieldName))
|
||||
_machine[fieldName] = machine[fieldName];
|
||||
_internal[fieldName] = machine[fieldName];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,131 +55,10 @@ namespace SabreTools.DatItems
|
||||
|
||||
#region Accessors
|
||||
|
||||
/// <summary>
|
||||
/// Get the value from a field based on the type provided
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
// Invalid field cannot be processed
|
||||
if (string.IsNullOrEmpty(fieldName))
|
||||
return default;
|
||||
|
||||
// Get the value based on the type
|
||||
return _machine.Read<T>(fieldName!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the value from a field based on the type provided
|
||||
/// </summary>
|
||||
/// <param name="fieldName">Field to retrieve</param>
|
||||
/// <returns>Value from the field, if possible</returns>
|
||||
public bool? GetBoolFieldValue(string? fieldName)
|
||||
{
|
||||
// Invalid field cannot be processed
|
||||
if (string.IsNullOrEmpty(fieldName) || !_machine.ContainsKey(fieldName!))
|
||||
return default;
|
||||
|
||||
// Get the value based on the type
|
||||
return _machine.ReadBool(fieldName!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the value from a field based on the type provided
|
||||
/// </summary>
|
||||
/// <param name="fieldName">Field to retrieve</param>
|
||||
/// <returns>Value from the field, if possible</returns>
|
||||
public double? GetDoubleFieldValue(string? fieldName)
|
||||
{
|
||||
// Invalid field cannot be processed
|
||||
if (string.IsNullOrEmpty(fieldName) || !_machine.ContainsKey(fieldName!))
|
||||
return default;
|
||||
|
||||
// Try to parse directly
|
||||
double? doubleValue = _machine.ReadDouble(fieldName!);
|
||||
if (doubleValue != null)
|
||||
return doubleValue;
|
||||
|
||||
// Try to parse from the string
|
||||
string? stringValue = _machine.ReadString(fieldName!);
|
||||
return NumberHelper.ConvertToDouble(stringValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the value from a field based on the type provided
|
||||
/// </summary>
|
||||
/// <param name="fieldName">Field to retrieve</param>
|
||||
/// <returns>Value from the field, if possible</returns>
|
||||
public long? GetInt64FieldValue(string? fieldName)
|
||||
{
|
||||
// Invalid field cannot be processed
|
||||
if (string.IsNullOrEmpty(fieldName) || !_machine.ContainsKey(fieldName!))
|
||||
return default;
|
||||
|
||||
// Try to parse directly
|
||||
long? longValue = _machine.ReadLong(fieldName!);
|
||||
if (longValue != null)
|
||||
return longValue;
|
||||
|
||||
// Try to parse from the string
|
||||
string? stringValue = _machine.ReadString(fieldName!);
|
||||
return NumberHelper.ConvertToInt64(stringValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the value from a field based on the type provided
|
||||
/// </summary>
|
||||
/// <param name="fieldName">Field to retrieve</param>
|
||||
/// <returns>Value from the field, if possible</returns>
|
||||
public string? GetStringFieldValue(string? fieldName)
|
||||
{
|
||||
// Invalid field cannot be processed
|
||||
if (string.IsNullOrEmpty(fieldName) || !_machine.ContainsKey(fieldName!))
|
||||
return default;
|
||||
|
||||
// Get the value based on the type
|
||||
return _machine.ReadString(fieldName!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the value from a field based on the type provided
|
||||
/// </summary>
|
||||
/// <param name="fieldName">Field to retrieve</param>
|
||||
/// <returns>Value from the field, if possible</returns>
|
||||
public string[]? GetStringArrayFieldValue(string? fieldName)
|
||||
{
|
||||
// Invalid field cannot be processed
|
||||
if (string.IsNullOrEmpty(fieldName) || !_machine.ContainsKey(fieldName!))
|
||||
return default;
|
||||
|
||||
// Get the value based on the type
|
||||
return _machine.ReadStringArray(fieldName!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the value from a field based on the type provided
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
// Invalid field cannot be processed
|
||||
if (string.IsNullOrEmpty(fieldName))
|
||||
return false;
|
||||
|
||||
// Set the value based on the type
|
||||
_machine[fieldName!] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a clone of the current internal model
|
||||
/// </summary>
|
||||
public Models.Metadata.Machine GetInternalClone() => (_machine.Clone() as Models.Metadata.Machine)!;
|
||||
public Models.Metadata.Machine GetInternalClone() => (_internal.Clone() as Models.Metadata.Machine)!;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -204,7 +72,7 @@ namespace SabreTools.DatItems
|
||||
{
|
||||
return new Machine()
|
||||
{
|
||||
_machine = this._machine.Clone() as Models.Metadata.Machine ?? [],
|
||||
_internal = this._internal.Clone() as Models.Metadata.Machine ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -217,7 +85,7 @@ namespace SabreTools.DatItems
|
||||
/// </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);
|
||||
public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_internal);
|
||||
|
||||
/// <summary>
|
||||
/// Remove a field from the Machine
|
||||
@@ -225,7 +93,7 @@ namespace SabreTools.DatItems
|
||||
/// <param name="fieldName">Field to remove</param>
|
||||
/// <returns>True if the removal was successful, false otherwise</returns>
|
||||
public bool RemoveField(string? fieldName)
|
||||
=> FieldManipulator.RemoveField(_machine, fieldName);
|
||||
=> FieldManipulator.RemoveField(_internal, fieldName);
|
||||
|
||||
/// <summary>
|
||||
/// Replace a field from another Machine
|
||||
@@ -234,7 +102,7 @@ namespace SabreTools.DatItems
|
||||
/// <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);
|
||||
=> FieldManipulator.ReplaceField(other?._internal, _internal, fieldName);
|
||||
|
||||
/// <summary>
|
||||
/// Set a field in the Machine from a mapping string
|
||||
@@ -244,7 +112,7 @@ namespace SabreTools.DatItems
|
||||
/// <returns>True if the setting was successful, false otherwise</returns>
|
||||
/// <remarks>This only performs minimal validation before setting</remarks>
|
||||
public bool SetField(string? fieldName, string value)
|
||||
=> FieldManipulator.SetField(_machine, fieldName, value);
|
||||
=> FieldManipulator.SetField(_internal, fieldName, value);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user