diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index fb38df15..411b1c31 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -3405,25 +3405,8 @@ namespace SabreTools.Library.DatFiles /// True if the DAT was written correctly, false otherwise public bool Write(string outDir, bool norename = true, bool stats = false, bool ignoreblanks = false, bool overwrite = true) { - // Force a statistics recheck, just in case - Items.RecalculateStats(); - - // If there's nothing there, abort - if (Items.TotalCount == 0) - { - Globals.Logger.User("There were no items to write out!"); - return false; - } - - // Get a count of all removed items - long removed = 0; - foreach (string key in Items.Keys) - { - removed += Items[key].Count(i => i.Remove); - } - - // If every item is removed, abort - if (Items.TotalCount == removed) + // If we have nothing writable, abort + if (!HasWritable()) { Globals.Logger.User("There were no items to write out!"); return false; @@ -3440,38 +3423,7 @@ namespace SabreTools.Library.DatFiles } // Make sure that the three essential fields are filled in - if (string.IsNullOrWhiteSpace(Header.FileName) && string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) - { - Header.FileName = Header.Name = Header.Description = "Default"; - } - else if (string.IsNullOrWhiteSpace(Header.FileName) && string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) - { - Header.FileName = Header.Name = Header.Description; - } - else if (string.IsNullOrWhiteSpace(Header.FileName) && !string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) - { - Header.FileName = Header.Description = Header.Name; - } - else if (string.IsNullOrWhiteSpace(Header.FileName) && !string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) - { - Header.FileName = Header.Description; - } - else if (!string.IsNullOrWhiteSpace(Header.FileName) && string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) - { - Header.Name = Header.Description = Header.FileName; - } - else if (!string.IsNullOrWhiteSpace(Header.FileName) && string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) - { - Header.Name = Header.Description; - } - else if (!string.IsNullOrWhiteSpace(Header.FileName) && !string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) - { - Header.Description = Header.Name; - } - else if (!string.IsNullOrWhiteSpace(Header.FileName) && !string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) - { - // Nothing is needed - } + EnsureHeaderFields(); // Output initial statistics, for kicks if (stats) @@ -3765,6 +3717,61 @@ namespace SabreTools.Library.DatFiles return false; } + /// + /// Ensure that FileName, Name, and Description are filled with some value + /// + private void EnsureHeaderFields() + { + // Empty FileName + if (string.IsNullOrWhiteSpace(Header.FileName)) + { + if (string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) + Header.FileName = Header.Name = Header.Description = "Default"; + + else if (string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) + Header.FileName = Header.Name = Header.Description; + + else if (!string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) + Header.FileName = Header.Description = Header.Name; + + else if (!string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) + Header.FileName = Header.Description; + } + + // Filled FileName + else + { + if (string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) + Header.Name = Header.Description = Header.FileName; + + else if (string.IsNullOrWhiteSpace(Header.Name) && !string.IsNullOrWhiteSpace(Header.Description)) + Header.Name = Header.Description; + + else if (!string.IsNullOrWhiteSpace(Header.Name) && string.IsNullOrWhiteSpace(Header.Description)) + Header.Description = Header.Name; + } + } + + /// + /// Get if the DatFile has any writable items + /// + /// True if there are any writable items, false otherwise + private bool HasWritable() + { + // Force a statistics recheck, just in case + Items.RecalculateStats(); + + // If there's nothing there, abort + if (Items.TotalCount == 0) + return false; + + // If every item is removed, abort + if (Items.TotalCount == Items.RemovedCount) + return false; + + return true; + } + #endregion } }