mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Move writing to new class
This commit is contained in:
@@ -55,7 +55,7 @@ in -old DAT file. Ignores those entries in -old that are not in -new.";
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the DatTool for parsing
|
// Get the DatTool for opeations
|
||||||
DatTool dt = new DatTool();
|
DatTool dt = new DatTool();
|
||||||
|
|
||||||
// Create the encapsulating datfile
|
// Create the encapsulating datfile
|
||||||
@@ -67,7 +67,7 @@ in -old DAT file. Ignores those entries in -old that are not in -new.";
|
|||||||
// Diff against the new datfile
|
// Diff against the new datfile
|
||||||
DatFile intDat = dt.CreateAndParse(newdat);
|
DatFile intDat = dt.CreateAndParse(newdat);
|
||||||
datfile.DiffAgainst(intDat, false);
|
datfile.DiffAgainst(intDat, false);
|
||||||
intDat.Write(outdat);
|
dt.Write(intDat, outdat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ namespace RombaSharp.Features
|
|||||||
datfile.Header.Description = description;
|
datfile.Header.Description = description;
|
||||||
dt.PopulateFromDir(datfile, source, asFiles: TreatAsFile.NonArchive);
|
dt.PopulateFromDir(datfile, source, asFiles: TreatAsFile.NonArchive);
|
||||||
datfile.ApplyCleaning(new Cleaner() { ExcludeFields = Hash.DeepHashes.AsFields() });
|
datfile.ApplyCleaning(new Cleaner() { ExcludeFields = Hash.DeepHashes.AsFields() });
|
||||||
datfile.Write(outdat);
|
dt.Write(datfile, outdat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ namespace RombaSharp.Features
|
|||||||
// Diff against the new datfile
|
// Diff against the new datfile
|
||||||
DatFile intDat = dt.CreateAndParse(newdat);
|
DatFile intDat = dt.CreateAndParse(newdat);
|
||||||
datfile.DiffAgainst(intDat, false);
|
datfile.DiffAgainst(intDat, false);
|
||||||
intDat.Write(outdat);
|
dt.Write(intDat, outdat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -299,7 +299,8 @@ namespace SabreTools.DatFiles
|
|||||||
newDatFile.Header.Type = null;
|
newDatFile.Header.Type = null;
|
||||||
|
|
||||||
// Write out the temporary DAT to the proper directory
|
// Write out the temporary DAT to the proper directory
|
||||||
newDatFile.Write(outDir);
|
DatTool dt = new DatTool();
|
||||||
|
dt.Write(newDatFile, outDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,439 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
using SabreTools.Core;
|
|
||||||
using SabreTools.DatFiles.Reports;
|
|
||||||
using SabreTools.DatItems;
|
|
||||||
using SabreTools.IO;
|
|
||||||
|
|
||||||
// This file represents all methods related to writing to a file
|
|
||||||
namespace SabreTools.DatFiles
|
|
||||||
{
|
|
||||||
public abstract partial class DatFile
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Create and open an output file for writing direct from a dictionary
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="outDir">Set the output directory (current directory on null)</param>
|
|
||||||
/// <param name="overwrite">True if files should be overwritten (default), false if they should be renamed instead</param>
|
|
||||||
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
|
||||||
/// <param name="quotes">True if quotes are assumed in supported types (default), false otherwise</param>
|
|
||||||
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
|
||||||
/// <returns>True if the DAT was written correctly, false otherwise</returns>
|
|
||||||
public bool Write(
|
|
||||||
string outDir,
|
|
||||||
bool overwrite = true,
|
|
||||||
bool ignoreblanks = false,
|
|
||||||
bool quotes = true,
|
|
||||||
bool throwOnError = false)
|
|
||||||
{
|
|
||||||
// If we have nothing writable, abort
|
|
||||||
if (!HasWritable())
|
|
||||||
{
|
|
||||||
logger.User("There were no items to write out!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the output directory is set and created
|
|
||||||
outDir = DirectoryExtensions.Ensure(outDir, create: true);
|
|
||||||
|
|
||||||
// If the DAT has no output format, default to XML
|
|
||||||
if (Header.DatFormat == 0)
|
|
||||||
{
|
|
||||||
logger.Verbose("No DAT format defined, defaulting to XML");
|
|
||||||
Header.DatFormat = DatFormat.Logiqx;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure that the three essential fields are filled in
|
|
||||||
EnsureHeaderFields();
|
|
||||||
|
|
||||||
// Bucket roms by game name, if not already
|
|
||||||
Items.BucketBy(Field.Machine_Name, DedupeType.None);
|
|
||||||
|
|
||||||
// Output the number of items we're going to be writing
|
|
||||||
logger.User($"A total of {Items.TotalCount - Items.RemovedCount} items will be written out to '{Header.FileName}'");
|
|
||||||
|
|
||||||
// Get the outfile names
|
|
||||||
Dictionary<DatFormat, string> outfiles = Header.CreateOutFileNames(outDir, overwrite);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Write out all required formats
|
|
||||||
Parallel.ForEach(outfiles.Keys, Globals.ParallelOptions, datFormat =>
|
|
||||||
{
|
|
||||||
string outfile = outfiles[datFormat];
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Create(datFormat, this, quotes)?.WriteToFile(outfile, ignoreblanks, throwOnError);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.Error(ex, $"Datfile {outfile} could not be written out");
|
|
||||||
if (throwOnError) throw ex;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.Error(ex);
|
|
||||||
if (throwOnError) throw ex;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Write the stats out to console for the current DatFile
|
|
||||||
/// </summary>
|
|
||||||
public void WriteStatsToConsole()
|
|
||||||
{
|
|
||||||
if (Items.RomCount + Items.DiskCount == 0)
|
|
||||||
Items.RecalculateStats();
|
|
||||||
|
|
||||||
Items.BucketBy(Field.Machine_Name, DedupeType.None, norename: true);
|
|
||||||
|
|
||||||
var consoleOutput = BaseReport.Create(StatReportFormat.None, null, true, true);
|
|
||||||
consoleOutput.ReplaceStatistics(Header.FileName, Items.Keys.Count(), Items);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create and open an output file for writing direct from a dictionary
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="outfile">Name of the file to write to</param>
|
|
||||||
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
|
||||||
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
|
||||||
/// <returns>True if the DAT was written correctly, false otherwise</returns>
|
|
||||||
public abstract bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create a prefix or postfix from inputs
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">DatItem to create a prefix/postfix for</param>
|
|
||||||
/// <param name="prefix">True for prefix, false for postfix</param>
|
|
||||||
/// <returns>Sanitized string representing the postfix or prefix</returns>
|
|
||||||
protected string CreatePrefixPostfix(DatItem item, bool prefix)
|
|
||||||
{
|
|
||||||
// Initialize strings
|
|
||||||
string fix = string.Empty,
|
|
||||||
game = item.Machine.Name,
|
|
||||||
name = item.GetName() ?? item.ItemType.ToString(),
|
|
||||||
crc = string.Empty,
|
|
||||||
md5 = string.Empty,
|
|
||||||
ripemd160 = string.Empty,
|
|
||||||
sha1 = string.Empty,
|
|
||||||
sha256 = string.Empty,
|
|
||||||
sha384 = string.Empty,
|
|
||||||
sha512 = string.Empty,
|
|
||||||
size = string.Empty,
|
|
||||||
spamsum = string.Empty;
|
|
||||||
|
|
||||||
// If we have a prefix
|
|
||||||
if (prefix)
|
|
||||||
fix = Header.Prefix + (Header.Quotes ? "\"" : string.Empty);
|
|
||||||
|
|
||||||
// If we have a postfix
|
|
||||||
else
|
|
||||||
fix = (Header.Quotes ? "\"" : string.Empty) + Header.Postfix;
|
|
||||||
|
|
||||||
// Ensure we have the proper values for replacement
|
|
||||||
if (item.ItemType == ItemType.Disk)
|
|
||||||
{
|
|
||||||
md5 = (item as Disk).MD5 ?? string.Empty;
|
|
||||||
sha1 = (item as Disk).SHA1 ?? string.Empty;
|
|
||||||
}
|
|
||||||
else if (item.ItemType == ItemType.Media)
|
|
||||||
{
|
|
||||||
md5 = (item as Media).MD5 ?? string.Empty;
|
|
||||||
sha1 = (item as Media).SHA1 ?? string.Empty;
|
|
||||||
sha256 = (item as Media).SHA256 ?? string.Empty;
|
|
||||||
spamsum = (item as Media).SpamSum ?? string.Empty;
|
|
||||||
}
|
|
||||||
else if (item.ItemType == ItemType.Rom)
|
|
||||||
{
|
|
||||||
crc = (item as Rom).CRC ?? string.Empty;
|
|
||||||
md5 = (item as Rom).MD5 ?? string.Empty;
|
|
||||||
#if NET_FRAMEWORK
|
|
||||||
ripemd160 = (item as Rom).RIPEMD160 ?? string.Empty;
|
|
||||||
#endif
|
|
||||||
sha1 = (item as Rom).SHA1 ?? string.Empty;
|
|
||||||
sha256 = (item as Rom).SHA256 ?? string.Empty;
|
|
||||||
sha384 = (item as Rom).SHA384 ?? string.Empty;
|
|
||||||
sha512 = (item as Rom).SHA512 ?? string.Empty;
|
|
||||||
size = (item as Rom).Size?.ToString() ?? string.Empty;
|
|
||||||
spamsum = (item as Rom).SpamSum ?? string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now do bulk replacement where possible
|
|
||||||
fix = fix
|
|
||||||
.Replace("%game%", game)
|
|
||||||
.Replace("%machine%", game)
|
|
||||||
.Replace("%name%", name)
|
|
||||||
.Replace("%manufacturer%", item.Machine.Manufacturer ?? string.Empty)
|
|
||||||
.Replace("%publisher%", item.Machine.Publisher ?? string.Empty)
|
|
||||||
.Replace("%category%", item.Machine.Category ?? string.Empty)
|
|
||||||
.Replace("%crc%", crc)
|
|
||||||
.Replace("%md5%", md5)
|
|
||||||
.Replace("%ripemd160%", ripemd160)
|
|
||||||
.Replace("%sha1%", sha1)
|
|
||||||
.Replace("%sha256%", sha256)
|
|
||||||
.Replace("%sha384%", sha384)
|
|
||||||
.Replace("%sha512%", sha512)
|
|
||||||
.Replace("%size%", size)
|
|
||||||
.Replace("%spamsum%", spamsum);
|
|
||||||
|
|
||||||
// TODO: Add GameName logic here too?
|
|
||||||
// TODO: Figure out what I meant by the above ^
|
|
||||||
|
|
||||||
return fix;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Process an item and correctly set the item name
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">DatItem to update</param>
|
|
||||||
/// <param name="forceRemoveQuotes">True if the Quotes flag should be ignored, false otherwise</param>
|
|
||||||
/// <param name="forceRomName">True if the UseRomName should be always on (default), false otherwise</param>
|
|
||||||
protected void ProcessItemName(DatItem item, bool forceRemoveQuotes, bool forceRomName = true)
|
|
||||||
{
|
|
||||||
string name = item.GetName() ?? string.Empty;
|
|
||||||
|
|
||||||
// Backup relevant values and set new ones accordingly
|
|
||||||
bool quotesBackup = Header.Quotes;
|
|
||||||
bool useRomNameBackup = Header.UseRomName;
|
|
||||||
if (forceRemoveQuotes)
|
|
||||||
Header.Quotes = false;
|
|
||||||
|
|
||||||
if (forceRomName)
|
|
||||||
Header.UseRomName = true;
|
|
||||||
|
|
||||||
// Create the proper Prefix and Postfix
|
|
||||||
string pre = CreatePrefixPostfix(item, true);
|
|
||||||
string post = CreatePrefixPostfix(item, false);
|
|
||||||
|
|
||||||
// If we're in Depot mode, take care of that instead
|
|
||||||
if (Header.OutputDepot?.IsActive == true)
|
|
||||||
{
|
|
||||||
if (item.ItemType == ItemType.Disk)
|
|
||||||
{
|
|
||||||
Disk disk = item as Disk;
|
|
||||||
|
|
||||||
// We can only write out if there's a SHA-1
|
|
||||||
if (!string.IsNullOrWhiteSpace(disk.SHA1))
|
|
||||||
{
|
|
||||||
name = PathExtensions.GetDepotPath(disk.SHA1, Header.OutputDepot.Depth).Replace('\\', '/');
|
|
||||||
item.SetFields(new Dictionary<Field, string> { [Field.DatItem_Name] = $"{pre}{name}{post}" } );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (item.ItemType == ItemType.Media)
|
|
||||||
{
|
|
||||||
Media media = item as Media;
|
|
||||||
|
|
||||||
// We can only write out if there's a SHA-1
|
|
||||||
if (!string.IsNullOrWhiteSpace(media.SHA1))
|
|
||||||
{
|
|
||||||
name = PathExtensions.GetDepotPath(media.SHA1, Header.OutputDepot.Depth).Replace('\\', '/');
|
|
||||||
item.SetFields(new Dictionary<Field, string> { [Field.DatItem_Name] = $"{pre}{name}{post}" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (item.ItemType == ItemType.Rom)
|
|
||||||
{
|
|
||||||
Rom rom = item as Rom;
|
|
||||||
|
|
||||||
// We can only write out if there's a SHA-1
|
|
||||||
if (!string.IsNullOrWhiteSpace(rom.SHA1))
|
|
||||||
{
|
|
||||||
name = PathExtensions.GetDepotPath(rom.SHA1, Header.OutputDepot.Depth).Replace('\\', '/');
|
|
||||||
item.SetFields(new Dictionary<Field, string> { [Field.DatItem_Name] = $"{pre}{name}{post}" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(Header.ReplaceExtension) || Header.RemoveExtension)
|
|
||||||
{
|
|
||||||
if (Header.RemoveExtension)
|
|
||||||
Header.ReplaceExtension = string.Empty;
|
|
||||||
|
|
||||||
string dir = Path.GetDirectoryName(name);
|
|
||||||
dir = dir.TrimStart(Path.DirectorySeparatorChar);
|
|
||||||
name = Path.Combine(dir, Path.GetFileNameWithoutExtension(name) + Header.ReplaceExtension);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(Header.AddExtension))
|
|
||||||
name += Header.AddExtension;
|
|
||||||
|
|
||||||
if (Header.UseRomName && Header.GameName)
|
|
||||||
name = Path.Combine(item.Machine.Name, name);
|
|
||||||
|
|
||||||
// Now assign back the item name
|
|
||||||
item.SetFields(new Dictionary<Field, string> { [Field.DatItem_Name] = pre + name + post });
|
|
||||||
|
|
||||||
// Restore all relevant values
|
|
||||||
if (forceRemoveQuotes)
|
|
||||||
Header.Quotes = quotesBackup;
|
|
||||||
|
|
||||||
if (forceRomName)
|
|
||||||
Header.UseRomName = useRomNameBackup;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Process any DatItems that are "null", usually created from directory population
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="datItem">DatItem to check for "null" status</param>
|
|
||||||
/// <returns>Cleaned DatItem</returns>
|
|
||||||
protected DatItem ProcessNullifiedItem(DatItem datItem)
|
|
||||||
{
|
|
||||||
// If we don't have a Rom, we can ignore it
|
|
||||||
if (datItem.ItemType != ItemType.Rom)
|
|
||||||
return datItem;
|
|
||||||
|
|
||||||
// Cast for easier parsing
|
|
||||||
Rom rom = datItem as Rom;
|
|
||||||
|
|
||||||
// If the Rom has "null" characteristics, ensure all fields
|
|
||||||
if (rom.Size == null && rom.CRC == "null")
|
|
||||||
{
|
|
||||||
logger.Verbose($"Empty folder found: {datItem.Machine.Name}");
|
|
||||||
|
|
||||||
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
|
|
||||||
rom.Size = Constants.SizeZero;
|
|
||||||
rom.CRC = rom.CRC == "null" ? Constants.CRCZero : null;
|
|
||||||
rom.MD5 = rom.MD5 == "null" ? Constants.MD5Zero : null;
|
|
||||||
#if NET_FRAMEWORK
|
|
||||||
rom.RIPEMD160 = rom.RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
|
|
||||||
#endif
|
|
||||||
rom.SHA1 = rom.SHA1 == "null" ? Constants.SHA1Zero : null;
|
|
||||||
rom.SHA256 = rom.SHA256 == "null" ? Constants.SHA256Zero : null;
|
|
||||||
rom.SHA384 = rom.SHA384 == "null" ? Constants.SHA384Zero : null;
|
|
||||||
rom.SHA512 = rom.SHA512 == "null" ? Constants.SHA512Zero : null;
|
|
||||||
rom.SpamSum = rom.SpamSum == "null" ? Constants.SpamSumZero : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return rom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get supported types for write
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>List of supported types for writing</returns>
|
|
||||||
protected virtual ItemType[] GetSupportedTypes()
|
|
||||||
{
|
|
||||||
return Enum.GetValues(typeof(ItemType)) as ItemType[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get if a machine contains any writable items
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="datItems">DatItems to check</param>
|
|
||||||
/// <returns>True if the machine contains at least one writable item, false otherwise</returns>
|
|
||||||
/// <remarks>Empty machines are kept with this</remarks>
|
|
||||||
protected bool ContainsWritable(List<DatItem> datItems)
|
|
||||||
{
|
|
||||||
// Empty machines are considered writable
|
|
||||||
if (datItems == null || datItems.Count == 0)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
foreach (DatItem datItem in datItems)
|
|
||||||
{
|
|
||||||
if (GetSupportedTypes().Contains(datItem.ItemType))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get if an item should be ignored on write
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="datItem">DatItem to check</param>
|
|
||||||
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
|
|
||||||
/// <returns>True if the item should be skipped on write, false otherwise</returns>
|
|
||||||
protected bool ShouldIgnore(DatItem datItem, bool ignoreBlanks)
|
|
||||||
{
|
|
||||||
// If the item is supposed to be removed, we ignore
|
|
||||||
if (datItem.Remove)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// If we have the Blank dat item, we ignore
|
|
||||||
if (datItem.ItemType == ItemType.Blank)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// If we're ignoring blanks and we have a Rom
|
|
||||||
if (ignoreBlanks && datItem.ItemType == ItemType.Rom)
|
|
||||||
{
|
|
||||||
Rom rom = datItem as Rom;
|
|
||||||
|
|
||||||
// If we have a 0-size or blank rom, then we ignore
|
|
||||||
if (rom.Size == 0 || rom.Size == null)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have an item type not in the list of supported values
|
|
||||||
if (!GetSupportedTypes().Contains(datItem.ItemType))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ensure that FileName, Name, and Description are filled with some value
|
|
||||||
/// </summary>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get if the DatFile has any writable items
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>True if there are any writable items, false otherwise</returns>
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
using SabreTools.Core;
|
using SabreTools.Core;
|
||||||
using SabreTools.DatFiles.Formats;
|
using SabreTools.DatFiles.Formats;
|
||||||
|
using SabreTools.DatFiles.Reports;
|
||||||
using SabreTools.DatItems;
|
using SabreTools.DatItems;
|
||||||
|
using SabreTools.IO;
|
||||||
using SabreTools.Logging;
|
using SabreTools.Logging;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
@@ -86,7 +89,7 @@ namespace SabreTools.DatFiles
|
|||||||
return new ClrMamePro(baseDat, quotes);
|
return new ClrMamePro(baseDat, quotes);
|
||||||
|
|
||||||
case DatFormat.CSV:
|
case DatFormat.CSV:
|
||||||
return new SeparatedValue(baseDat, ',');
|
return new Formats.SeparatedValue(baseDat, ',');
|
||||||
|
|
||||||
case DatFormat.DOSCenter:
|
case DatFormat.DOSCenter:
|
||||||
return new DosCenter(baseDat);
|
return new DosCenter(baseDat);
|
||||||
@@ -154,10 +157,10 @@ namespace SabreTools.DatFiles
|
|||||||
return new Formats.SoftwareList(baseDat);
|
return new Formats.SoftwareList(baseDat);
|
||||||
|
|
||||||
case DatFormat.SSV:
|
case DatFormat.SSV:
|
||||||
return new SeparatedValue(baseDat, ';');
|
return new Formats.SeparatedValue(baseDat, ';');
|
||||||
|
|
||||||
case DatFormat.TSV:
|
case DatFormat.TSV:
|
||||||
return new SeparatedValue(baseDat, '\t');
|
return new Formats.SeparatedValue(baseDat, '\t');
|
||||||
|
|
||||||
// We use new-style Logiqx as a backup for generic DatFile
|
// We use new-style Logiqx as a backup for generic DatFile
|
||||||
case null:
|
case null:
|
||||||
@@ -375,5 +378,303 @@ namespace SabreTools.DatFiles
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Writing
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Write the stats out to console for the current DatFile
|
||||||
|
/// </summary>
|
||||||
|
public void WriteStatsToConsole()
|
||||||
|
{
|
||||||
|
if (Items.RomCount + Items.DiskCount == 0)
|
||||||
|
Items.RecalculateStats();
|
||||||
|
|
||||||
|
Items.BucketBy(Field.Machine_Name, DedupeType.None, norename: true);
|
||||||
|
|
||||||
|
var consoleOutput = BaseReport.Create(StatReportFormat.None, null, true, true);
|
||||||
|
consoleOutput.ReplaceStatistics(Header.FileName, Items.Keys.Count(), Items);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create and open an output file for writing direct from a dictionary
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="outfile">Name of the file to write to</param>
|
||||||
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
||||||
|
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
||||||
|
/// <returns>True if the DAT was written correctly, false otherwise</returns>
|
||||||
|
public abstract bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a prefix or postfix from inputs
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">DatItem to create a prefix/postfix for</param>
|
||||||
|
/// <param name="prefix">True for prefix, false for postfix</param>
|
||||||
|
/// <returns>Sanitized string representing the postfix or prefix</returns>
|
||||||
|
protected string CreatePrefixPostfix(DatItem item, bool prefix)
|
||||||
|
{
|
||||||
|
// Initialize strings
|
||||||
|
string fix = string.Empty,
|
||||||
|
game = item.Machine.Name,
|
||||||
|
name = item.GetName() ?? item.ItemType.ToString(),
|
||||||
|
crc = string.Empty,
|
||||||
|
md5 = string.Empty,
|
||||||
|
ripemd160 = string.Empty,
|
||||||
|
sha1 = string.Empty,
|
||||||
|
sha256 = string.Empty,
|
||||||
|
sha384 = string.Empty,
|
||||||
|
sha512 = string.Empty,
|
||||||
|
size = string.Empty,
|
||||||
|
spamsum = string.Empty;
|
||||||
|
|
||||||
|
// If we have a prefix
|
||||||
|
if (prefix)
|
||||||
|
fix = Header.Prefix + (Header.Quotes ? "\"" : string.Empty);
|
||||||
|
|
||||||
|
// If we have a postfix
|
||||||
|
else
|
||||||
|
fix = (Header.Quotes ? "\"" : string.Empty) + Header.Postfix;
|
||||||
|
|
||||||
|
// Ensure we have the proper values for replacement
|
||||||
|
if (item.ItemType == ItemType.Disk)
|
||||||
|
{
|
||||||
|
md5 = (item as Disk).MD5 ?? string.Empty;
|
||||||
|
sha1 = (item as Disk).SHA1 ?? string.Empty;
|
||||||
|
}
|
||||||
|
else if (item.ItemType == ItemType.Media)
|
||||||
|
{
|
||||||
|
md5 = (item as Media).MD5 ?? string.Empty;
|
||||||
|
sha1 = (item as Media).SHA1 ?? string.Empty;
|
||||||
|
sha256 = (item as Media).SHA256 ?? string.Empty;
|
||||||
|
spamsum = (item as Media).SpamSum ?? string.Empty;
|
||||||
|
}
|
||||||
|
else if (item.ItemType == ItemType.Rom)
|
||||||
|
{
|
||||||
|
crc = (item as Rom).CRC ?? string.Empty;
|
||||||
|
md5 = (item as Rom).MD5 ?? string.Empty;
|
||||||
|
#if NET_FRAMEWORK
|
||||||
|
ripemd160 = (item as Rom).RIPEMD160 ?? string.Empty;
|
||||||
|
#endif
|
||||||
|
sha1 = (item as Rom).SHA1 ?? string.Empty;
|
||||||
|
sha256 = (item as Rom).SHA256 ?? string.Empty;
|
||||||
|
sha384 = (item as Rom).SHA384 ?? string.Empty;
|
||||||
|
sha512 = (item as Rom).SHA512 ?? string.Empty;
|
||||||
|
size = (item as Rom).Size?.ToString() ?? string.Empty;
|
||||||
|
spamsum = (item as Rom).SpamSum ?? string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now do bulk replacement where possible
|
||||||
|
fix = fix
|
||||||
|
.Replace("%game%", game)
|
||||||
|
.Replace("%machine%", game)
|
||||||
|
.Replace("%name%", name)
|
||||||
|
.Replace("%manufacturer%", item.Machine.Manufacturer ?? string.Empty)
|
||||||
|
.Replace("%publisher%", item.Machine.Publisher ?? string.Empty)
|
||||||
|
.Replace("%category%", item.Machine.Category ?? string.Empty)
|
||||||
|
.Replace("%crc%", crc)
|
||||||
|
.Replace("%md5%", md5)
|
||||||
|
.Replace("%ripemd160%", ripemd160)
|
||||||
|
.Replace("%sha1%", sha1)
|
||||||
|
.Replace("%sha256%", sha256)
|
||||||
|
.Replace("%sha384%", sha384)
|
||||||
|
.Replace("%sha512%", sha512)
|
||||||
|
.Replace("%size%", size)
|
||||||
|
.Replace("%spamsum%", spamsum);
|
||||||
|
|
||||||
|
// TODO: Add GameName logic here too?
|
||||||
|
// TODO: Figure out what I meant by the above ^
|
||||||
|
|
||||||
|
return fix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Process an item and correctly set the item name
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">DatItem to update</param>
|
||||||
|
/// <param name="forceRemoveQuotes">True if the Quotes flag should be ignored, false otherwise</param>
|
||||||
|
/// <param name="forceRomName">True if the UseRomName should be always on (default), false otherwise</param>
|
||||||
|
protected void ProcessItemName(DatItem item, bool forceRemoveQuotes, bool forceRomName = true)
|
||||||
|
{
|
||||||
|
string name = item.GetName() ?? string.Empty;
|
||||||
|
|
||||||
|
// Backup relevant values and set new ones accordingly
|
||||||
|
bool quotesBackup = Header.Quotes;
|
||||||
|
bool useRomNameBackup = Header.UseRomName;
|
||||||
|
if (forceRemoveQuotes)
|
||||||
|
Header.Quotes = false;
|
||||||
|
|
||||||
|
if (forceRomName)
|
||||||
|
Header.UseRomName = true;
|
||||||
|
|
||||||
|
// Create the proper Prefix and Postfix
|
||||||
|
string pre = CreatePrefixPostfix(item, true);
|
||||||
|
string post = CreatePrefixPostfix(item, false);
|
||||||
|
|
||||||
|
// If we're in Depot mode, take care of that instead
|
||||||
|
if (Header.OutputDepot?.IsActive == true)
|
||||||
|
{
|
||||||
|
if (item.ItemType == ItemType.Disk)
|
||||||
|
{
|
||||||
|
Disk disk = item as Disk;
|
||||||
|
|
||||||
|
// We can only write out if there's a SHA-1
|
||||||
|
if (!string.IsNullOrWhiteSpace(disk.SHA1))
|
||||||
|
{
|
||||||
|
name = PathExtensions.GetDepotPath(disk.SHA1, Header.OutputDepot.Depth).Replace('\\', '/');
|
||||||
|
item.SetFields(new Dictionary<Field, string> { [Field.DatItem_Name] = $"{pre}{name}{post}" } );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (item.ItemType == ItemType.Media)
|
||||||
|
{
|
||||||
|
Media media = item as Media;
|
||||||
|
|
||||||
|
// We can only write out if there's a SHA-1
|
||||||
|
if (!string.IsNullOrWhiteSpace(media.SHA1))
|
||||||
|
{
|
||||||
|
name = PathExtensions.GetDepotPath(media.SHA1, Header.OutputDepot.Depth).Replace('\\', '/');
|
||||||
|
item.SetFields(new Dictionary<Field, string> { [Field.DatItem_Name] = $"{pre}{name}{post}" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (item.ItemType == ItemType.Rom)
|
||||||
|
{
|
||||||
|
Rom rom = item as Rom;
|
||||||
|
|
||||||
|
// We can only write out if there's a SHA-1
|
||||||
|
if (!string.IsNullOrWhiteSpace(rom.SHA1))
|
||||||
|
{
|
||||||
|
name = PathExtensions.GetDepotPath(rom.SHA1, Header.OutputDepot.Depth).Replace('\\', '/');
|
||||||
|
item.SetFields(new Dictionary<Field, string> { [Field.DatItem_Name] = $"{pre}{name}{post}" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(Header.ReplaceExtension) || Header.RemoveExtension)
|
||||||
|
{
|
||||||
|
if (Header.RemoveExtension)
|
||||||
|
Header.ReplaceExtension = string.Empty;
|
||||||
|
|
||||||
|
string dir = Path.GetDirectoryName(name);
|
||||||
|
dir = dir.TrimStart(Path.DirectorySeparatorChar);
|
||||||
|
name = Path.Combine(dir, Path.GetFileNameWithoutExtension(name) + Header.ReplaceExtension);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(Header.AddExtension))
|
||||||
|
name += Header.AddExtension;
|
||||||
|
|
||||||
|
if (Header.UseRomName && Header.GameName)
|
||||||
|
name = Path.Combine(item.Machine.Name, name);
|
||||||
|
|
||||||
|
// Now assign back the item name
|
||||||
|
item.SetFields(new Dictionary<Field, string> { [Field.DatItem_Name] = pre + name + post });
|
||||||
|
|
||||||
|
// Restore all relevant values
|
||||||
|
if (forceRemoveQuotes)
|
||||||
|
Header.Quotes = quotesBackup;
|
||||||
|
|
||||||
|
if (forceRomName)
|
||||||
|
Header.UseRomName = useRomNameBackup;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Process any DatItems that are "null", usually created from directory population
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datItem">DatItem to check for "null" status</param>
|
||||||
|
/// <returns>Cleaned DatItem</returns>
|
||||||
|
protected DatItem ProcessNullifiedItem(DatItem datItem)
|
||||||
|
{
|
||||||
|
// If we don't have a Rom, we can ignore it
|
||||||
|
if (datItem.ItemType != ItemType.Rom)
|
||||||
|
return datItem;
|
||||||
|
|
||||||
|
// Cast for easier parsing
|
||||||
|
Rom rom = datItem as Rom;
|
||||||
|
|
||||||
|
// If the Rom has "null" characteristics, ensure all fields
|
||||||
|
if (rom.Size == null && rom.CRC == "null")
|
||||||
|
{
|
||||||
|
logger.Verbose($"Empty folder found: {datItem.Machine.Name}");
|
||||||
|
|
||||||
|
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
|
||||||
|
rom.Size = Constants.SizeZero;
|
||||||
|
rom.CRC = rom.CRC == "null" ? Constants.CRCZero : null;
|
||||||
|
rom.MD5 = rom.MD5 == "null" ? Constants.MD5Zero : null;
|
||||||
|
#if NET_FRAMEWORK
|
||||||
|
rom.RIPEMD160 = rom.RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
|
||||||
|
#endif
|
||||||
|
rom.SHA1 = rom.SHA1 == "null" ? Constants.SHA1Zero : null;
|
||||||
|
rom.SHA256 = rom.SHA256 == "null" ? Constants.SHA256Zero : null;
|
||||||
|
rom.SHA384 = rom.SHA384 == "null" ? Constants.SHA384Zero : null;
|
||||||
|
rom.SHA512 = rom.SHA512 == "null" ? Constants.SHA512Zero : null;
|
||||||
|
rom.SpamSum = rom.SpamSum == "null" ? Constants.SpamSumZero : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rom;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get supported types for write
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>List of supported types for writing</returns>
|
||||||
|
protected virtual ItemType[] GetSupportedTypes()
|
||||||
|
{
|
||||||
|
return Enum.GetValues(typeof(ItemType)) as ItemType[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get if a machine contains any writable items
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datItems">DatItems to check</param>
|
||||||
|
/// <returns>True if the machine contains at least one writable item, false otherwise</returns>
|
||||||
|
/// <remarks>Empty machines are kept with this</remarks>
|
||||||
|
protected bool ContainsWritable(List<DatItem> datItems)
|
||||||
|
{
|
||||||
|
// Empty machines are considered writable
|
||||||
|
if (datItems == null || datItems.Count == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
foreach (DatItem datItem in datItems)
|
||||||
|
{
|
||||||
|
if (GetSupportedTypes().Contains(datItem.ItemType))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get if an item should be ignored on write
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datItem">DatItem to check</param>
|
||||||
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
|
||||||
|
/// <returns>True if the item should be skipped on write, false otherwise</returns>
|
||||||
|
protected bool ShouldIgnore(DatItem datItem, bool ignoreBlanks)
|
||||||
|
{
|
||||||
|
// If the item is supposed to be removed, we ignore
|
||||||
|
if (datItem.Remove)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// If we have the Blank dat item, we ignore
|
||||||
|
if (datItem.ItemType == ItemType.Blank)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// If we're ignoring blanks and we have a Rom
|
||||||
|
if (ignoreBlanks && datItem.ItemType == ItemType.Rom)
|
||||||
|
{
|
||||||
|
Rom rom = datItem as Rom;
|
||||||
|
|
||||||
|
// If we have a 0-size or blank rom, then we ignore
|
||||||
|
if (rom.Size == 0 || rom.Size == null)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have an item type not in the list of supported values
|
||||||
|
if (!GetSupportedTypes().Contains(datItem.ItemType))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
using SabreTools.Core;
|
using SabreTools.Core;
|
||||||
|
using SabreTools.DatFiles.Reports;
|
||||||
using SabreTools.DatItems;
|
using SabreTools.DatItems;
|
||||||
using SabreTools.FileTypes;
|
using SabreTools.FileTypes;
|
||||||
using SabreTools.IO;
|
using SabreTools.IO;
|
||||||
|
|||||||
146
SabreTools.DatFiles/DatTool.Writing.cs
Normal file
146
SabreTools.DatFiles/DatTool.Writing.cs
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using SabreTools.Core;
|
||||||
|
using SabreTools.IO;
|
||||||
|
|
||||||
|
// This file represents all methods related to writing to a file
|
||||||
|
namespace SabreTools.DatFiles
|
||||||
|
{
|
||||||
|
// TODO: Re-evaluate if these should be made static instead of instanced
|
||||||
|
public partial class DatTool
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Create and open an output file for writing direct from a dictionary
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datFile">Current DatFile object to write from</param>
|
||||||
|
/// <param name="outDir">Set the output directory (current directory on null)</param>
|
||||||
|
/// <param name="overwrite">True if files should be overwritten (default), false if they should be renamed instead</param>
|
||||||
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
||||||
|
/// <param name="quotes">True if quotes are assumed in supported types (default), false otherwise</param>
|
||||||
|
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
||||||
|
/// <returns>True if the DAT was written correctly, false otherwise</returns>
|
||||||
|
public bool Write(
|
||||||
|
DatFile datFile,
|
||||||
|
string outDir,
|
||||||
|
bool overwrite = true,
|
||||||
|
bool ignoreblanks = false,
|
||||||
|
bool quotes = true,
|
||||||
|
bool throwOnError = false)
|
||||||
|
{
|
||||||
|
// If we have nothing writable, abort
|
||||||
|
if (!HasWritable(datFile))
|
||||||
|
{
|
||||||
|
logger.User("There were no items to write out!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the output directory is set and created
|
||||||
|
outDir = DirectoryExtensions.Ensure(outDir, create: true);
|
||||||
|
|
||||||
|
// If the DAT has no output format, default to XML
|
||||||
|
if (datFile.Header.DatFormat == 0)
|
||||||
|
{
|
||||||
|
logger.Verbose("No DAT format defined, defaulting to XML");
|
||||||
|
datFile.Header.DatFormat = DatFormat.Logiqx;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure that the three essential fields are filled in
|
||||||
|
EnsureHeaderFields();
|
||||||
|
|
||||||
|
// Bucket roms by game name, if not already
|
||||||
|
datFile.Items.BucketBy(Field.Machine_Name, DedupeType.None);
|
||||||
|
|
||||||
|
// Output the number of items we're going to be writing
|
||||||
|
logger.User($"A total of {datFile.Items.TotalCount - datFile.Items.RemovedCount} items will be written out to '{datFile.Header.FileName}'");
|
||||||
|
|
||||||
|
// Get the outfile names
|
||||||
|
Dictionary<DatFormat, string> outfiles = datFile.Header.CreateOutFileNames(outDir, overwrite);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Write out all required formats
|
||||||
|
Parallel.ForEach(outfiles.Keys, Globals.ParallelOptions, datFormat =>
|
||||||
|
{
|
||||||
|
string outfile = outfiles[datFormat];
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DatFile.Create(datFormat, datFile, quotes)?.WriteToFile(outfile, ignoreblanks, throwOnError);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex, $"Datfile {outfile} could not be written out");
|
||||||
|
if (throwOnError) throw ex;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex);
|
||||||
|
if (throwOnError) throw ex;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensure that FileName, Name, and Description are filled with some value
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datFile">Current DatFile object to write from</param>
|
||||||
|
private void EnsureHeaderFields(DatFile datFile)
|
||||||
|
{
|
||||||
|
// Empty FileName
|
||||||
|
if (string.IsNullOrWhiteSpace(datFile.Header.FileName))
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(datFile.Header.Name) && string.IsNullOrWhiteSpace(datFile.Header.Description))
|
||||||
|
datFile.Header.FileName = datFile.Header.Name = datFile.Header.Description = "Default";
|
||||||
|
|
||||||
|
else if (string.IsNullOrWhiteSpace(datFile.Header.Name) && !string.IsNullOrWhiteSpace(datFile.Header.Description))
|
||||||
|
datFile.Header.FileName = datFile.Header.Name = datFile.Header.Description;
|
||||||
|
|
||||||
|
else if (!string.IsNullOrWhiteSpace(datFile.Header.Name) && string.IsNullOrWhiteSpace(datFile.Header.Description))
|
||||||
|
datFile.Header.FileName = datFile.Header.Description = datFile.Header.Name;
|
||||||
|
|
||||||
|
else if (!string.IsNullOrWhiteSpace(datFile.Header.Name) && !string.IsNullOrWhiteSpace(datFile.Header.Description))
|
||||||
|
datFile.Header.FileName = datFile.Header.Description;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filled FileName
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(datFile.Header.Name) && string.IsNullOrWhiteSpace(datFile.Header.Description))
|
||||||
|
datFile.Header.Name = datFile.Header.Description = datFile.Header.FileName;
|
||||||
|
|
||||||
|
else if (string.IsNullOrWhiteSpace(datFile.Header.Name) && !string.IsNullOrWhiteSpace(datFile.Header.Description))
|
||||||
|
datFile.Header.Name = datFile.Header.Description;
|
||||||
|
|
||||||
|
else if (!string.IsNullOrWhiteSpace(datFile.Header.Name) && string.IsNullOrWhiteSpace(datFile.Header.Description))
|
||||||
|
datFile.Header.Description = datFile.Header.Name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get if the DatFile has any writable items
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="datFile">Current DatFile object to write from</param>
|
||||||
|
/// <returns>True if there are any writable items, false otherwise</returns>
|
||||||
|
private bool HasWritable(DatFile datFile)
|
||||||
|
{
|
||||||
|
// Force a statistics recheck, just in case
|
||||||
|
datFile.Items.RecalculateStats();
|
||||||
|
|
||||||
|
// If there's nothing there, abort
|
||||||
|
if (datFile.Items.TotalCount == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// If every item is removed, abort
|
||||||
|
if (datFile.Items.TotalCount == datFile.Items.RemovedCount)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -406,7 +406,7 @@ Reset the internal state: reset();";
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write out the dat with the current state
|
// Write out the dat with the current state
|
||||||
datFile.Write(outputDirectory, overwrite: overwrite.Value);
|
dt.Write(datFile, outputDirectory, overwrite: overwrite.Value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Reset the internal state
|
// Reset the internal state
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ namespace SabreTools.Features
|
|||||||
datdata.ApplyCleaning(Cleaner);
|
datdata.ApplyCleaning(Cleaner);
|
||||||
|
|
||||||
// Write out the file
|
// Write out the file
|
||||||
datdata.Write(OutputDir);
|
dt.Write(datdata, OutputDir);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ namespace SabreTools.Features
|
|||||||
datdata.Header.Name = $"fixDAT_{Header.Name}";
|
datdata.Header.Name = $"fixDAT_{Header.Name}";
|
||||||
datdata.Header.Description = $"fixDAT_{Header.Description}";
|
datdata.Header.Description = $"fixDAT_{Header.Description}";
|
||||||
datdata.Items.ClearMarked();
|
datdata.Items.ClearMarked();
|
||||||
datdata.Write(OutputDir);
|
dt.Write(datdata, OutputDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -158,7 +158,7 @@ namespace SabreTools.Features
|
|||||||
datdata.Header.Name = $"fixDAT_{Header.Name}";
|
datdata.Header.Name = $"fixDAT_{Header.Name}";
|
||||||
datdata.Header.Description = $"fixDAT_{Header.Description}";
|
datdata.Header.Description = $"fixDAT_{Header.Description}";
|
||||||
datdata.Items.ClearMarked();
|
datdata.Items.ClearMarked();
|
||||||
datdata.Write(OutputDir);
|
dt.Write(datdata, OutputDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ namespace SabreTools.Features
|
|||||||
InternalStopwatch watch = new InternalStopwatch("Outputting extension-split DATs");
|
InternalStopwatch watch = new InternalStopwatch("Outputting extension-split DATs");
|
||||||
|
|
||||||
// Output both possible DatFiles
|
// Output both possible DatFiles
|
||||||
extADat.Write(OutputDir);
|
dt.Write(extADat, OutputDir);
|
||||||
extBDat.Write(OutputDir);
|
dt.Write(extBDat, OutputDir);
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ namespace SabreTools.Features
|
|||||||
// Loop through each type DatFile
|
// Loop through each type DatFile
|
||||||
Parallel.ForEach(typeDats.Keys, Globals.ParallelOptions, itemType =>
|
Parallel.ForEach(typeDats.Keys, Globals.ParallelOptions, itemType =>
|
||||||
{
|
{
|
||||||
typeDats[itemType].Write(OutputDir);
|
dt.Write(typeDats[itemType], OutputDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
@@ -111,8 +111,8 @@ namespace SabreTools.Features
|
|||||||
InternalStopwatch watch = new InternalStopwatch("Outputting size-split DATs");
|
InternalStopwatch watch = new InternalStopwatch("Outputting size-split DATs");
|
||||||
|
|
||||||
// Output both possible DatFiles
|
// Output both possible DatFiles
|
||||||
lessThan.Write(OutputDir);
|
dt.Write(lessThan, OutputDir);
|
||||||
greaterThan.Write(OutputDir);
|
dt.Write(greaterThan, OutputDir);
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
}
|
}
|
||||||
@@ -127,7 +127,7 @@ namespace SabreTools.Features
|
|||||||
// Loop through each type DatFile
|
// Loop through each type DatFile
|
||||||
Parallel.ForEach(typeDats.Keys, Globals.ParallelOptions, itemType =>
|
Parallel.ForEach(typeDats.Keys, Globals.ParallelOptions, itemType =>
|
||||||
{
|
{
|
||||||
typeDats[itemType].Write(OutputDir);
|
dt.Write(typeDats[itemType], OutputDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ namespace SabreTools.Features
|
|||||||
string realOutDir = inputPath.GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
string realOutDir = inputPath.GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
||||||
|
|
||||||
// Try to output the file, overwriting only if it's not in the current directory
|
// Try to output the file, overwriting only if it's not in the current directory
|
||||||
datFile.Write(realOutDir, overwrite: GetBoolean(features, InplaceValue));
|
dt.Write(datFile, realOutDir, overwrite: GetBoolean(features, InplaceValue));
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -219,7 +219,7 @@ namespace SabreTools.Features
|
|||||||
DatFile dupeData = userInputDat.DiffDuplicates(inputPaths);
|
DatFile dupeData = userInputDat.DiffDuplicates(inputPaths);
|
||||||
|
|
||||||
InternalStopwatch watch = new InternalStopwatch("Outputting duplicate DAT");
|
InternalStopwatch watch = new InternalStopwatch("Outputting duplicate DAT");
|
||||||
dupeData.Write(OutputDir, overwrite: false);
|
dt.Write(dupeData, OutputDir, overwrite: false);
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +229,7 @@ namespace SabreTools.Features
|
|||||||
DatFile outerDiffData = userInputDat.DiffNoDuplicates(inputPaths);
|
DatFile outerDiffData = userInputDat.DiffNoDuplicates(inputPaths);
|
||||||
|
|
||||||
InternalStopwatch watch = new InternalStopwatch("Outputting no duplicate DAT");
|
InternalStopwatch watch = new InternalStopwatch("Outputting no duplicate DAT");
|
||||||
outerDiffData.Write(OutputDir, overwrite: false);
|
dt.Write(outerDiffData, OutputDir, overwrite: false);
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ namespace SabreTools.Features
|
|||||||
string path = inputPaths[j].GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
string path = inputPaths[j].GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
||||||
|
|
||||||
// Try to output the file
|
// Try to output the file
|
||||||
datFiles[j].Write(path, overwrite: GetBoolean(features, InplaceValue));
|
dt.Write(datFiles[j], path, overwrite: GetBoolean(features, InplaceValue));
|
||||||
});
|
});
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
@@ -283,7 +283,7 @@ namespace SabreTools.Features
|
|||||||
string path = inputPaths[j].GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
string path = inputPaths[j].GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
||||||
|
|
||||||
// Try to output the file
|
// Try to output the file
|
||||||
datFiles[j].Write(path, overwrite: GetBoolean(features, InplaceValue));
|
dt.Write(datFiles[j], path, overwrite: GetBoolean(features, InplaceValue));
|
||||||
});
|
});
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
@@ -310,7 +310,7 @@ namespace SabreTools.Features
|
|||||||
|
|
||||||
// Finally output the diffed DatFile
|
// Finally output the diffed DatFile
|
||||||
string interOutDir = inputPath.GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
string interOutDir = inputPath.GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
||||||
repDat.Write(interOutDir, overwrite: GetBoolean(features, InplaceValue));
|
dt.Write(repDat, interOutDir, overwrite: GetBoolean(features, InplaceValue));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,7 +335,7 @@ namespace SabreTools.Features
|
|||||||
|
|
||||||
// Finally output the replaced DatFile
|
// Finally output the replaced DatFile
|
||||||
string interOutDir = inputPath.GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
string interOutDir = inputPath.GetOutputPath(OutputDir, GetBoolean(features, InplaceValue));
|
||||||
repDat.Write(interOutDir, overwrite: GetBoolean(features, InplaceValue));
|
dt.Write(repDat, interOutDir, overwrite: GetBoolean(features, InplaceValue));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -347,7 +347,7 @@ namespace SabreTools.Features
|
|||||||
if (string.Equals(userInputDat.Header.Type, "SuperDAT", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(userInputDat.Header.Type, "SuperDAT", StringComparison.OrdinalIgnoreCase))
|
||||||
userInputDat.ApplySuperDAT(inputPaths);
|
userInputDat.ApplySuperDAT(inputPaths);
|
||||||
|
|
||||||
userInputDat.Write(OutputDir);
|
dt.Write(userInputDat, OutputDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ namespace SabreTools.Features
|
|||||||
|
|
||||||
// Now write out if there are any items left
|
// Now write out if there are any items left
|
||||||
datdata.WriteStatsToConsole();
|
datdata.WriteStatsToConsole();
|
||||||
datdata.Write(OutputDir);
|
dt.Write(datdata, OutputDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Otherwise, process all DATs into the same output
|
// Otherwise, process all DATs into the same output
|
||||||
@@ -144,7 +144,7 @@ namespace SabreTools.Features
|
|||||||
|
|
||||||
// Now write out if there are any items left
|
// Now write out if there are any items left
|
||||||
datdata.WriteStatsToConsole();
|
datdata.WriteStatsToConsole();
|
||||||
datdata.Write(OutputDir);
|
dt.Write(datdata, OutputDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user