using System; using System.Xml.Serialization; using Newtonsoft.Json; using SabreTools.Core; using SabreTools.Core.Filter; using SabreTools.Core.Tools; namespace SabreTools.DatFiles { /// /// Represents all possible DAT header information /// [JsonObject("header"), XmlRoot("header")] public sealed class DatHeader : ModelBackedItem, ICloneable { #region Constants /// /// Read or write format /// public const string DatFormatKey = "DATFORMAT"; /// /// External name of the DAT /// public const string FileNameKey = "FILENAME"; #endregion #region Fields [JsonIgnore] public bool CanOpenSpecified { get { var canOpen = GetStringArrayFieldValue(Models.Metadata.Header.CanOpenKey); return canOpen != null && canOpen.Length > 0; } } [JsonIgnore] public bool ImagesSpecified { get { return GetFieldValue(Models.Metadata.Header.ImagesKey) != null; } } [JsonIgnore] public bool InfosSpecified { get { return GetFieldValue(Models.Metadata.Header.InfosKey) != null; } } [JsonIgnore] public bool NewDatSpecified { get { return GetFieldValue(Models.Metadata.Header.NewDatKey) != null; } } [JsonIgnore] public bool SearchSpecified { get { return GetFieldValue(Models.Metadata.Header.SearchKey) != null; } } #endregion #region Constructors public DatHeader() { } public DatHeader(Models.Metadata.Header header) { // Get all fields to automatically copy without processing var nonItemFields = TypeHelper.GetConstants(typeof(Models.Metadata.Header)); if (nonItemFields == null) return; // Populate the internal machine from non-filter fields _internal = new Models.Metadata.Header(); foreach (string fieldName in nonItemFields) { if (header.ContainsKey(fieldName)) _internal[fieldName] = header[fieldName]; } // Get all fields specific to the DatFiles implementation var nonStandardFields = TypeHelper.GetConstants(typeof(DatHeader)); if (nonStandardFields == null) return; // Populate the internal machine from filter fields foreach (string fieldName in nonStandardFields) { if (header.ContainsKey(fieldName)) _internal[fieldName] = header[fieldName]; } } #endregion #region Cloning Methods /// /// Clone the current header /// public object Clone() => new DatHeader(GetInternalClone()); /// /// Clone just the format from the current header /// public DatHeader CloneFormat() { var header = new DatHeader(); header.SetFieldValue(DatHeader.DatFormatKey, GetFieldValue(DatHeader.DatFormatKey)); return header; } /// /// Get a clone of the current internal model /// public Models.Metadata.Header GetInternalClone() { var header = (_internal.Clone() as Models.Metadata.Header)!; // Remove fields with default values if (header.ReadString(Models.Metadata.Header.ForceMergingKey).AsEnumValue() == MergingFlag.None) header.Remove(Models.Metadata.Header.ForceMergingKey); if (header.ReadString(Models.Metadata.Header.ForceNodumpKey).AsEnumValue() == NodumpFlag.None) header.Remove(Models.Metadata.Header.ForceNodumpKey); if (header.ReadString(Models.Metadata.Header.ForcePackingKey).AsEnumValue() == PackingFlag.None) header.Remove(Models.Metadata.Header.ForcePackingKey); // Convert subheader values if (CanOpenSpecified) header[Models.Metadata.Header.CanOpenKey] = new Models.OfflineList.CanOpen { Extension = GetStringArrayFieldValue(Models.Metadata.Header.CanOpenKey) }; if (ImagesSpecified) header[Models.Metadata.Header.ImagesKey] = GetFieldValue(Models.Metadata.Header.ImagesKey); if (InfosSpecified) header[Models.Metadata.Header.InfosKey] = GetFieldValue(Models.Metadata.Header.InfosKey); if (NewDatSpecified) header[Models.Metadata.Header.NewDatKey] = GetFieldValue(Models.Metadata.Header.NewDatKey); if (SearchSpecified) header[Models.Metadata.Header.SearchKey] = GetFieldValue(Models.Metadata.Header.SearchKey); return header; } #endregion #region Comparision Methods /// public override bool Equals(ModelBackedItem? other) { // If other is null if (other == null) return false; // If the type is mismatched if (other is not DatHeader otherItem) return false; // Compare internal models return _internal.EqualTo(otherItem._internal); } /// public override bool Equals(ModelBackedItem? other) { // If other is null if (other == null) return false; // If the type is mismatched if (other is not DatHeader otherItem) return false; // Compare internal models return _internal.EqualTo(otherItem._internal); } #endregion #region Manipulation /// /// Runs a filter and determines if it passes or not /// /// Filter runner to use for checking /// True if the Machine passes the filter, false otherwise public bool PassesFilter(FilterRunner filterRunner) => filterRunner.Run(_internal); #endregion } }