From 7d3cea4a89b88e45bf8c091c5a1f4838fc2f5c57 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 12 Mar 2024 22:22:12 -0400 Subject: [PATCH] Use ModelBackedItem for header, machine, and datitem --- SabreTools.DatFiles/DatHeader.cs | 148 ++---------------------------- SabreTools.DatItems/DatItem.cs | 129 +------------------------- SabreTools.DatItems/Machine.cs | 150 ++----------------------------- 3 files changed, 18 insertions(+), 409 deletions(-) diff --git a/SabreTools.DatFiles/DatHeader.cs b/SabreTools.DatFiles/DatHeader.cs index 4dcd8723..2734dd92 100644 --- a/SabreTools.DatFiles/DatHeader.cs +++ b/SabreTools.DatFiles/DatHeader.cs @@ -7,7 +7,6 @@ using SabreTools.Core; using SabreTools.Core.Tools; using SabreTools.DatFiles.Formats; using SabreTools.Filter; -using SabreTools.Serialization; namespace SabreTools.DatFiles { @@ -15,7 +14,7 @@ namespace SabreTools.DatFiles /// Represents all possible DAT header information /// [JsonObject("header"), XmlRoot("header")] - public class DatHeader : ICloneable + public class DatHeader : ModelBackedItem, ICloneable { #region Constants @@ -132,16 +131,8 @@ namespace SabreTools.DatFiles } } - /// - /// Internal Header model - /// - [JsonIgnore] - private readonly Models.Metadata.Header _header = []; - #endregion - #region Instance Methods - #region Constructors public DatHeader() { } @@ -154,11 +145,11 @@ namespace SabreTools.DatFiles return; // Populate the internal machine from non-filter fields - _header = []; + _internal = new Models.Metadata.Header(); foreach (string fieldName in nonItemFields) { if (header.ContainsKey(fieldName)) - _header[fieldName] = header[fieldName]; + _internal[fieldName] = header[fieldName]; } } @@ -166,131 +157,10 @@ namespace SabreTools.DatFiles #region Accessors - /// - /// Get the value from a field based on the type provided - /// - /// Type of the value to get from the internal model - /// Field to retrieve - /// Value from the field, if possible - public T? GetFieldValue(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(fieldName!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Set the value from a field based on the type provided - /// - /// Type of the value to set in the internal model - /// Field to set - /// Value to set - /// True if the value was set, false otherwise - public bool SetFieldValue(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; - } - /// /// Get a clone of the current internal model /// - public Models.Metadata.Header GetInternalClone() => (_header.Clone() as Models.Metadata.Header)!; + public Models.Metadata.Header GetInternalClone() => (_internal.Clone() as Models.Metadata.Header)!; #endregion @@ -442,7 +312,7 @@ namespace SabreTools.DatFiles /// /// Filter runner to use for checking /// True if the Machine passes the filter, false otherwise - public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_header); + public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_internal); /// /// Remove a field from the header @@ -450,7 +320,7 @@ namespace SabreTools.DatFiles /// Field to remove /// True if the removal was successful, false otherwise public bool RemoveField(string fieldName) - => FieldManipulator.RemoveField(_header, fieldName); + => FieldManipulator.RemoveField(_internal, fieldName); /// /// Replace a field from another DatHeader @@ -459,7 +329,7 @@ namespace SabreTools.DatFiles /// Field to replace /// True if the replacement was successful, false otherwise public bool ReplaceField(DatHeader? other, string? fieldName) - => FieldManipulator.ReplaceField(other?._header, _header, fieldName); + => FieldManipulator.ReplaceField(other?._internal, _internal, fieldName); /// /// Set a field in the header from a mapping string @@ -469,7 +339,7 @@ namespace SabreTools.DatFiles /// True if the setting was successful, false otherwise /// This only performs minimal validation before setting public bool SetField(string? fieldName, string value) - => FieldManipulator.SetField(_header, fieldName, value); + => FieldManipulator.SetField(_internal, fieldName, value); #endregion @@ -961,7 +831,5 @@ namespace SabreTools.DatFiles } #endregion - - #endregion // Instance Methods } } diff --git a/SabreTools.DatItems/DatItem.cs b/SabreTools.DatItems/DatItem.cs index d7ca9b04..e816377d 100644 --- a/SabreTools.DatItems/DatItem.cs +++ b/SabreTools.DatItems/DatItem.cs @@ -58,7 +58,7 @@ namespace SabreTools.DatItems [XmlInclude(typeof(SlotOption))] [XmlInclude(typeof(SoftwareList))] [XmlInclude(typeof(Sound))] - public abstract class DatItem : IEquatable, IComparable, ICloneable + public abstract class DatItem : ModelBackedItem, IEquatable, IComparable, ICloneable { #region Constants @@ -91,12 +91,6 @@ namespace SabreTools.DatItems /// protected abstract ItemType ItemType { get; } - /// - /// Internal model wrapped by this DatItem - /// - [JsonIgnore, XmlIgnore] - protected Models.Metadata.DatItem _internal = []; - #endregion #region Logging @@ -113,127 +107,6 @@ namespace SabreTools.DatItems #region Accessors - /// - /// Get the value from a field based on the type provided - /// - /// Type of the value to get from the internal model - /// Field to retrieve - /// Value from the field, if possible - public T? GetFieldValue(string? fieldName) - { - // Invalid field cannot be processed - if (string.IsNullOrEmpty(fieldName)) - return default; - - // Get the value based on the type - return _internal.Read(fieldName!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Set the value from a field based on the type provided - /// - /// Type of the value to set in the internal model - /// Field to set - /// Value to set - /// True if the value was set, false otherwise - public bool SetFieldValue(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; - } - /// /// Gets the name to use for a DatItem /// diff --git a/SabreTools.DatItems/Machine.cs b/SabreTools.DatItems/Machine.cs index f296ca20..0c371b97 100644 --- a/SabreTools.DatItems/Machine.cs +++ b/SabreTools.DatItems/Machine.cs @@ -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 /// [JsonObject("machine"), XmlRoot("machine")] - public class Machine : ICloneable + public class Machine : ModelBackedItem, ICloneable { #region Constants @@ -32,16 +31,6 @@ namespace SabreTools.DatItems #endregion - #region Fields - - /// - /// Internal Machine model - /// - [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 - /// - /// Get the value from a field based on the type provided - /// - /// Type of the value to get from the internal model - /// Field to retrieve - /// Value from the field, if possible - public T? GetFieldValue(string? fieldName) - { - // Invalid field cannot be processed - if (string.IsNullOrEmpty(fieldName)) - return default; - - // Get the value based on the type - return _machine.Read(fieldName!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Get the value from a field based on the type provided - /// - /// Field to retrieve - /// Value from the field, if possible - 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!); - } - - /// - /// Set the value from a field based on the type provided - /// - /// Type of the value to set in the internal model - /// Field to set - /// Value to set - /// True if the value was set, false otherwise - public bool SetFieldValue(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; - } - /// /// Get a clone of the current internal model /// - 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 /// /// Filter runner to use for checking /// True if the Machine passes the filter, false otherwise - public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_machine); + public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_internal); /// /// Remove a field from the Machine @@ -225,7 +93,7 @@ namespace SabreTools.DatItems /// Field to remove /// True if the removal was successful, false otherwise public bool RemoveField(string? fieldName) - => FieldManipulator.RemoveField(_machine, fieldName); + => FieldManipulator.RemoveField(_internal, fieldName); /// /// Replace a field from another Machine @@ -234,7 +102,7 @@ namespace SabreTools.DatItems /// Field to replace /// True if the replacement was successful, false otherwise public bool ReplaceField(Machine? other, string? fieldName) - => FieldManipulator.ReplaceField(other?._machine, _machine, fieldName); + => FieldManipulator.ReplaceField(other?._internal, _internal, fieldName); /// /// Set a field in the Machine from a mapping string @@ -244,7 +112,7 @@ namespace SabreTools.DatItems /// True if the setting was successful, false otherwise /// This only performs minimal validation before setting public bool SetField(string? fieldName, string value) - => FieldManipulator.SetField(_machine, fieldName, value); + => FieldManipulator.SetField(_internal, fieldName, value); #endregion }