Use ModelBackedItem for header, machine, and datitem

This commit is contained in:
Matt Nadareski
2024-03-12 22:22:12 -04:00
parent 103274505c
commit 7d3cea4a89
3 changed files with 18 additions and 409 deletions

View File

@@ -58,7 +58,7 @@ namespace SabreTools.DatItems
[XmlInclude(typeof(SlotOption))]
[XmlInclude(typeof(SoftwareList))]
[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
@@ -91,12 +91,6 @@ namespace SabreTools.DatItems
/// </summary>
protected abstract ItemType ItemType { get; }
/// <summary>
/// Internal model wrapped by this DatItem
/// </summary>
[JsonIgnore, XmlIgnore]
protected Models.Metadata.DatItem _internal = [];
#endregion
#region Logging
@@ -113,127 +107,6 @@ 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 _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>
/// Gets the name to use for a DatItem
/// </summary>