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:
@@ -7,7 +7,6 @@ using SabreTools.Core;
|
|||||||
using SabreTools.Core.Tools;
|
using SabreTools.Core.Tools;
|
||||||
using SabreTools.DatFiles.Formats;
|
using SabreTools.DatFiles.Formats;
|
||||||
using SabreTools.Filter;
|
using SabreTools.Filter;
|
||||||
using SabreTools.Serialization;
|
|
||||||
|
|
||||||
namespace SabreTools.DatFiles
|
namespace SabreTools.DatFiles
|
||||||
{
|
{
|
||||||
@@ -15,7 +14,7 @@ namespace SabreTools.DatFiles
|
|||||||
/// Represents all possible DAT header information
|
/// Represents all possible DAT header information
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonObject("header"), XmlRoot("header")]
|
[JsonObject("header"), XmlRoot("header")]
|
||||||
public class DatHeader : ICloneable
|
public class DatHeader : ModelBackedItem<Models.Metadata.Header>, ICloneable
|
||||||
{
|
{
|
||||||
#region Constants
|
#region Constants
|
||||||
|
|
||||||
@@ -132,16 +131,8 @@ namespace SabreTools.DatFiles
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Internal Header model
|
|
||||||
/// </summary>
|
|
||||||
[JsonIgnore]
|
|
||||||
private readonly Models.Metadata.Header _header = [];
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Instance Methods
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public DatHeader() { }
|
public DatHeader() { }
|
||||||
@@ -154,11 +145,11 @@ namespace SabreTools.DatFiles
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
// Populate the internal machine from non-filter fields
|
// Populate the internal machine from non-filter fields
|
||||||
_header = [];
|
_internal = new Models.Metadata.Header();
|
||||||
foreach (string fieldName in nonItemFields)
|
foreach (string fieldName in nonItemFields)
|
||||||
{
|
{
|
||||||
if (header.ContainsKey(fieldName))
|
if (header.ContainsKey(fieldName))
|
||||||
_header[fieldName] = header[fieldName];
|
_internal[fieldName] = header[fieldName];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,131 +157,10 @@ namespace SabreTools.DatFiles
|
|||||||
|
|
||||||
#region Accessors
|
#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) || !_header.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Get the value based on the type
|
|
||||||
return _header.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) || !_header.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Get the value based on the type
|
|
||||||
return _header.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) || !_header.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Try to parse directly
|
|
||||||
double? doubleValue = _header.ReadDouble(fieldName!);
|
|
||||||
if (doubleValue != null)
|
|
||||||
return doubleValue;
|
|
||||||
|
|
||||||
// Try to parse from the string
|
|
||||||
string? stringValue = _header.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) || !_header.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Try to parse directly
|
|
||||||
long? longValue = _header.ReadLong(fieldName!);
|
|
||||||
if (longValue != null)
|
|
||||||
return longValue;
|
|
||||||
|
|
||||||
// Try to parse from the string
|
|
||||||
string? stringValue = _header.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) || !_header.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Get the value based on the type
|
|
||||||
return _header.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) || !_header.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Get the value based on the type
|
|
||||||
return _header.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
|
|
||||||
_header[fieldName!] = value;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a clone of the current internal model
|
/// Get a clone of the current internal model
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Models.Metadata.Header GetInternalClone() => (_header.Clone() as Models.Metadata.Header)!;
|
public Models.Metadata.Header GetInternalClone() => (_internal.Clone() as Models.Metadata.Header)!;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -442,7 +312,7 @@ namespace SabreTools.DatFiles
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filterRunner">Filter runner to use for checking</param>
|
/// <param name="filterRunner">Filter runner to use for checking</param>
|
||||||
/// <returns>True if the Machine passes the filter, false otherwise</returns>
|
/// <returns>True if the Machine passes the filter, false otherwise</returns>
|
||||||
public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_header);
|
public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_internal);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Remove a field from the header
|
/// Remove a field from the header
|
||||||
@@ -450,7 +320,7 @@ namespace SabreTools.DatFiles
|
|||||||
/// <param name="fieldName">Field to remove</param>
|
/// <param name="fieldName">Field to remove</param>
|
||||||
/// <returns>True if the removal was successful, false otherwise</returns>
|
/// <returns>True if the removal was successful, false otherwise</returns>
|
||||||
public bool RemoveField(string fieldName)
|
public bool RemoveField(string fieldName)
|
||||||
=> FieldManipulator.RemoveField(_header, fieldName);
|
=> FieldManipulator.RemoveField(_internal, fieldName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Replace a field from another DatHeader
|
/// Replace a field from another DatHeader
|
||||||
@@ -459,7 +329,7 @@ namespace SabreTools.DatFiles
|
|||||||
/// <param name="fieldName">Field to replace</param>
|
/// <param name="fieldName">Field to replace</param>
|
||||||
/// <returns>True if the replacement was successful, false otherwise</returns>
|
/// <returns>True if the replacement was successful, false otherwise</returns>
|
||||||
public bool ReplaceField(DatHeader? other, string? fieldName)
|
public bool ReplaceField(DatHeader? other, string? fieldName)
|
||||||
=> FieldManipulator.ReplaceField(other?._header, _header, fieldName);
|
=> FieldManipulator.ReplaceField(other?._internal, _internal, fieldName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set a field in the header from a mapping string
|
/// Set a field in the header from a mapping string
|
||||||
@@ -469,7 +339,7 @@ namespace SabreTools.DatFiles
|
|||||||
/// <returns>True if the setting was successful, false otherwise</returns>
|
/// <returns>True if the setting was successful, false otherwise</returns>
|
||||||
/// <remarks>This only performs minimal validation before setting</remarks>
|
/// <remarks>This only performs minimal validation before setting</remarks>
|
||||||
public bool SetField(string? fieldName, string value)
|
public bool SetField(string? fieldName, string value)
|
||||||
=> FieldManipulator.SetField(_header, fieldName, value);
|
=> FieldManipulator.SetField(_internal, fieldName, value);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -961,7 +831,5 @@ namespace SabreTools.DatFiles
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#endregion // Instance Methods
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ namespace SabreTools.DatItems
|
|||||||
[XmlInclude(typeof(SlotOption))]
|
[XmlInclude(typeof(SlotOption))]
|
||||||
[XmlInclude(typeof(SoftwareList))]
|
[XmlInclude(typeof(SoftwareList))]
|
||||||
[XmlInclude(typeof(Sound))]
|
[XmlInclude(typeof(Sound))]
|
||||||
public abstract class DatItem : IEquatable<DatItem>, IComparable<DatItem>, ICloneable
|
public abstract class DatItem : ModelBackedItem<Models.Metadata.DatItem>, IEquatable<DatItem>, IComparable<DatItem>, ICloneable
|
||||||
{
|
{
|
||||||
#region Constants
|
#region Constants
|
||||||
|
|
||||||
@@ -91,12 +91,6 @@ namespace SabreTools.DatItems
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected abstract ItemType ItemType { get; }
|
protected abstract ItemType ItemType { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Internal model wrapped by this DatItem
|
|
||||||
/// </summary>
|
|
||||||
[JsonIgnore, XmlIgnore]
|
|
||||||
protected Models.Metadata.DatItem _internal = [];
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Logging
|
#region Logging
|
||||||
@@ -113,127 +107,6 @@ namespace SabreTools.DatItems
|
|||||||
|
|
||||||
#region Accessors
|
#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 _internal.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) || !_internal.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Get the value based on the type
|
|
||||||
return _internal.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) || !_internal.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Try to parse directly
|
|
||||||
double? doubleValue = _internal.ReadDouble(fieldName!);
|
|
||||||
if (doubleValue != null)
|
|
||||||
return doubleValue;
|
|
||||||
|
|
||||||
// Try to parse from the string
|
|
||||||
string? stringValue = _internal.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) || !_internal.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Try to parse directly
|
|
||||||
long? longValue = _internal.ReadLong(fieldName!);
|
|
||||||
if (longValue != null)
|
|
||||||
return longValue;
|
|
||||||
|
|
||||||
// Try to parse from the string
|
|
||||||
string? stringValue = _internal.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) || !_internal.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Get the value based on the type
|
|
||||||
return _internal.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) || !_internal.ContainsKey(fieldName!))
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Get the value based on the type
|
|
||||||
return _internal.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
|
|
||||||
_internal[fieldName!] = value;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the name to use for a DatItem
|
/// Gets the name to use for a DatItem
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using SabreTools.Core;
|
using SabreTools.Core;
|
||||||
using SabreTools.Core.Tools;
|
|
||||||
using SabreTools.Filter;
|
using SabreTools.Filter;
|
||||||
|
|
||||||
namespace SabreTools.DatItems
|
namespace SabreTools.DatItems
|
||||||
@@ -11,7 +10,7 @@ namespace SabreTools.DatItems
|
|||||||
/// Represents the information specific to a set/game/machine
|
/// Represents the information specific to a set/game/machine
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonObject("machine"), XmlRoot("machine")]
|
[JsonObject("machine"), XmlRoot("machine")]
|
||||||
public class Machine : ICloneable
|
public class Machine : ModelBackedItem<Models.Metadata.Machine>, ICloneable
|
||||||
{
|
{
|
||||||
#region Constants
|
#region Constants
|
||||||
|
|
||||||
@@ -32,16 +31,6 @@ namespace SabreTools.DatItems
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Fields
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Internal Machine model
|
|
||||||
/// </summary>
|
|
||||||
[JsonIgnore]
|
|
||||||
private Models.Metadata.Machine _machine = [];
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public Machine() { }
|
public Machine() { }
|
||||||
@@ -54,11 +43,11 @@ namespace SabreTools.DatItems
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
// Populate the internal machine from non-filter fields
|
// Populate the internal machine from non-filter fields
|
||||||
_machine = [];
|
_internal = new Models.Metadata.Machine();
|
||||||
foreach (string fieldName in nonItemFields)
|
foreach (string fieldName in nonItemFields)
|
||||||
{
|
{
|
||||||
if (machine.ContainsKey(fieldName))
|
if (machine.ContainsKey(fieldName))
|
||||||
_machine[fieldName] = machine[fieldName];
|
_internal[fieldName] = machine[fieldName];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,131 +55,10 @@ namespace SabreTools.DatItems
|
|||||||
|
|
||||||
#region Accessors
|
#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>
|
/// <summary>
|
||||||
/// Get a clone of the current internal model
|
/// Get a clone of the current internal model
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Models.Metadata.Machine GetInternalClone() => (_machine.Clone() as Models.Metadata.Machine)!;
|
public Models.Metadata.Machine GetInternalClone() => (_internal.Clone() as Models.Metadata.Machine)!;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -204,7 +72,7 @@ namespace SabreTools.DatItems
|
|||||||
{
|
{
|
||||||
return new Machine()
|
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>
|
/// </summary>
|
||||||
/// <param name="filterRunner">Filter runner to use for checking</param>
|
/// <param name="filterRunner">Filter runner to use for checking</param>
|
||||||
/// <returns>True if the Machine passes the filter, false otherwise</returns>
|
/// <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>
|
/// <summary>
|
||||||
/// Remove a field from the Machine
|
/// Remove a field from the Machine
|
||||||
@@ -225,7 +93,7 @@ namespace SabreTools.DatItems
|
|||||||
/// <param name="fieldName">Field to remove</param>
|
/// <param name="fieldName">Field to remove</param>
|
||||||
/// <returns>True if the removal was successful, false otherwise</returns>
|
/// <returns>True if the removal was successful, false otherwise</returns>
|
||||||
public bool RemoveField(string? fieldName)
|
public bool RemoveField(string? fieldName)
|
||||||
=> FieldManipulator.RemoveField(_machine, fieldName);
|
=> FieldManipulator.RemoveField(_internal, fieldName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Replace a field from another Machine
|
/// Replace a field from another Machine
|
||||||
@@ -234,7 +102,7 @@ namespace SabreTools.DatItems
|
|||||||
/// <param name="fieldName">Field to replace</param>
|
/// <param name="fieldName">Field to replace</param>
|
||||||
/// <returns>True if the replacement was successful, false otherwise</returns>
|
/// <returns>True if the replacement was successful, false otherwise</returns>
|
||||||
public bool ReplaceField(Machine? other, string? fieldName)
|
public bool ReplaceField(Machine? other, string? fieldName)
|
||||||
=> FieldManipulator.ReplaceField(other?._machine, _machine, fieldName);
|
=> FieldManipulator.ReplaceField(other?._internal, _internal, fieldName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set a field in the Machine from a mapping string
|
/// 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>
|
/// <returns>True if the setting was successful, false otherwise</returns>
|
||||||
/// <remarks>This only performs minimal validation before setting</remarks>
|
/// <remarks>This only performs minimal validation before setting</remarks>
|
||||||
public bool SetField(string? fieldName, string value)
|
public bool SetField(string? fieldName, string value)
|
||||||
=> FieldManipulator.SetField(_machine, fieldName, value);
|
=> FieldManipulator.SetField(_internal, fieldName, value);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user