2020-12-10 11:28:11 -08:00
using System ;
using System.Collections.Generic ;
2024-03-05 03:04:47 -05:00
#if NET40_OR_GREATER | | NETCOREAPP
2020-12-10 11:28:11 -08:00
using System.Threading.Tasks ;
2024-03-05 03:04:47 -05:00
#endif
2020-12-10 23:24:09 -08:00
using SabreTools.DatFiles ;
2020-12-14 15:43:01 -08:00
using SabreTools.DatItems ;
2024-04-24 13:45:38 -04:00
using SabreTools.IO.Extensions ;
2024-10-24 00:36:44 -04:00
using SabreTools.IO.Logging ;
2020-12-11 10:10:56 -08:00
using SabreTools.Reports ;
2020-12-10 11:28:11 -08:00
2020-12-10 23:24:09 -08:00
namespace SabreTools.DatTools
2020-12-10 11:28:11 -08:00
{
2020-12-21 11:38:56 -08:00
/// <summary>
/// Helper methods for writing from DatFiles
/// </summary>
2020-12-10 14:03:07 -08:00
public class Writer
2020-12-10 11:28:11 -08:00
{
2020-12-10 14:03:07 -08:00
#region Logging
/// <summary>
/// Logging object
/// </summary>
2025-01-08 16:59:44 -05:00
private static readonly Logger _staticLogger = new ( ) ;
2020-12-10 14:03:07 -08:00
#endregion
2020-12-10 11:28:11 -08:00
/// <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>
2025-01-12 20:49:22 -05:00
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public static bool Write ( DatFile datFile , string? outDir )
= > Write ( datFile , outDir , overwrite : true , throwOnError : false ) ;
/// <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, false if they should be renamed instead</param>
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public static bool Write ( DatFile datFile , string? outDir , bool overwrite )
= > Write ( datFile , outDir , overwrite , throwOnError : false ) ;
/// <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, false if they should be renamed instead</param>
2020-12-10 11:28:11 -08:00
/// <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>
2025-01-12 20:49:22 -05:00
public static bool Write ( DatFile datFile , string? outDir , bool overwrite , bool throwOnError )
2020-12-10 11:28:11 -08:00
{
// If we have nothing writable, abort
if ( ! HasWritable ( datFile ) )
{
2025-01-08 16:59:44 -05:00
_staticLogger . User ( "There were no items to write out!" ) ;
2020-12-10 11:28:11 -08:00
return false ;
}
// Ensure the output directory is set and created
2024-03-12 16:47:21 -04:00
outDir = outDir . Ensure ( create : true ) ;
2020-12-10 11:28:11 -08:00
2023-04-19 16:39:58 -04:00
InternalStopwatch watch = new ( $"Writing out internal dat to '{outDir}'" ) ;
2021-02-02 14:09:49 -08:00
2020-12-10 11:28:11 -08:00
// If the DAT has no output format, default to XML
2024-03-10 21:54:07 -04:00
if ( datFile . Header . GetFieldValue < DatFormat > ( DatHeader . DatFormatKey ) = = 0 )
2020-12-10 11:28:11 -08:00
{
2025-01-08 16:59:44 -05:00
_staticLogger . Verbose ( "No DAT format defined, defaulting to XML" ) ;
2024-03-10 21:54:07 -04:00
datFile . Header . SetFieldValue < DatFormat > ( DatHeader . DatFormatKey , DatFormat . Logiqx ) ;
2020-12-10 11:28:11 -08:00
}
// Make sure that the three essential fields are filled in
2020-12-10 11:58:46 -08:00
EnsureHeaderFields ( datFile ) ;
2020-12-10 11:28:11 -08:00
// Bucket roms by game name, if not already
2025-01-12 23:15:30 -05:00
datFile . BucketBy ( ItemKey . Machine , DedupeType . None ) ;
2020-12-10 11:28:11 -08:00
// Output the number of items we're going to be writing
2025-01-12 23:15:30 -05:00
_staticLogger . User ( $"A total of {datFile.DatStatistics.TotalCount - datFile.DatStatistics.RemovedCount} items will be written out to '{datFile.Header.GetStringFieldValue(DatHeader.FileNameKey)}'" ) ;
2020-12-10 11:28:11 -08:00
// Get the outfile names
2024-03-06 00:53:32 -05:00
Dictionary < DatFormat , string > outfiles = datFile . Header . CreateOutFileNames ( outDir ! , overwrite ) ;
2020-12-10 11:28:11 -08:00
try
{
// Write out all required formats
2024-02-28 22:54:56 -05:00
#if NET452_OR_GREATER | | NETCOREAPP
2024-10-24 05:58:03 -04:00
Parallel . ForEach ( outfiles . Keys , Core . Globals . ParallelOptions , datFormat = >
2024-02-28 22:54:56 -05:00
#elif NET40_OR_GREATER
Parallel . ForEach ( outfiles . Keys , datFormat = >
#else
foreach ( var datFormat in outfiles . Keys )
#endif
2020-12-10 11:28:11 -08:00
{
string outfile = outfiles [ datFormat ] ;
try
{
2025-01-12 20:49:22 -05:00
DatFile writingDatFile = DatFileTool . CreateDatFile ( datFormat , datFile ) ;
writingDatFile . WriteToFile ( outfile , ignoreblanks : true , throwOnError ) ;
2020-12-10 11:28:11 -08:00
}
2021-01-12 15:54:14 -08:00
catch ( Exception ex ) when ( ! throwOnError )
2020-12-10 11:28:11 -08:00
{
2025-01-08 16:59:44 -05:00
_staticLogger . Error ( ex , $"Datfile '{outfile}' could not be written out" ) ;
2020-12-10 11:28:11 -08:00
}
2024-02-28 21:59:13 -05:00
#if NET40_OR_GREATER | | NETCOREAPP
2020-12-10 11:28:11 -08:00
} ) ;
2024-02-28 21:59:13 -05:00
#else
}
#endif
2020-12-10 11:28:11 -08:00
}
2021-01-12 15:54:14 -08:00
catch ( Exception ex ) when ( ! throwOnError )
2020-12-10 11:28:11 -08:00
{
2025-01-08 16:59:44 -05:00
_staticLogger . Error ( ex ) ;
2020-12-10 11:28:11 -08:00
return false ;
}
2021-02-02 14:09:49 -08:00
finally
{
watch . Stop ( ) ;
}
2020-12-10 11:28:11 -08:00
return true ;
}
2020-12-10 15:52:31 -08:00
/// <summary>
/// Write the stats out to console for the current DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to write from</param>
public static void WriteStatsToConsole ( DatFile datFile )
{
2025-01-12 23:15:30 -05:00
long diskCount = datFile . DatStatistics . GetItemCount ( ItemType . Disk ) ;
long mediaCount = datFile . DatStatistics . GetItemCount ( ItemType . Media ) ;
long romCount = datFile . DatStatistics . GetItemCount ( ItemType . Rom ) ;
2024-03-04 22:52:03 -05:00
if ( diskCount + mediaCount + romCount = = 0 )
2020-12-10 15:52:31 -08:00
datFile . Items . RecalculateStats ( ) ;
2025-01-12 23:15:30 -05:00
datFile . BucketBy ( ItemKey . Machine , DedupeType . None , norename : true ) ;
2024-03-19 23:15:58 -04:00
2025-01-12 23:15:30 -05:00
datFile . DatStatistics . DisplayName = datFile . Header . GetStringFieldValue ( DatHeader . FileNameKey ) ;
datFile . DatStatistics . MachineCount = datFile . Items . Keys . Count ;
2024-03-19 23:15:58 -04:00
2024-12-06 23:16:09 -05:00
List < DatStatistics > statsList =
[
2025-01-12 23:15:30 -05:00
datFile . DatStatistics ,
2024-12-06 23:16:09 -05:00
] ;
2021-02-18 11:13:11 -08:00
var consoleOutput = BaseReport . Create ( StatReportFormat . None , statsList ) ;
2024-02-28 19:19:50 -05:00
consoleOutput ! . WriteToFile ( null , true , true ) ;
2020-12-10 15:52:31 -08:00
}
2020-12-10 11:28:11 -08:00
/// <summary>
/// Ensure that FileName, Name, and Description are filled with some value
/// </summary>
/// <param name="datFile">Current DatFile object to write from</param>
2020-12-10 11:58:46 -08:00
private static void EnsureHeaderFields ( DatFile datFile )
2020-12-10 11:28:11 -08:00
{
// Empty FileName
2024-03-11 15:46:44 -04:00
if ( string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( DatHeader . FileNameKey ) ) )
2020-12-10 11:28:11 -08:00
{
2024-03-11 15:46:44 -04:00
if ( string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) & & string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) )
2024-03-10 04:10:37 -04:00
{
2024-07-17 15:46:42 -04:00
datFile . Header . SetFieldValue < string? > ( DatHeader . FileNameKey , "Default" ) ;
2024-03-10 04:10:37 -04:00
datFile . Header . SetFieldValue < string? > ( Models . Metadata . Header . NameKey , "Default" ) ;
datFile . Header . SetFieldValue < string? > ( Models . Metadata . Header . DescriptionKey , "Default" ) ;
}
2020-12-10 11:28:11 -08:00
2024-03-11 15:46:44 -04:00
else if ( string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) & & ! string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) )
2024-03-10 04:10:37 -04:00
{
2024-07-17 15:46:42 -04:00
datFile . Header . SetFieldValue < string? > ( DatHeader . FileNameKey , datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) ;
2024-03-11 15:46:44 -04:00
datFile . Header . SetFieldValue < string? > ( Models . Metadata . Header . NameKey , datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) ;
2024-03-10 04:10:37 -04:00
}
2020-12-10 11:28:11 -08:00
2024-03-11 15:46:44 -04:00
else if ( ! string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) & & string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) )
2024-03-10 04:10:37 -04:00
{
2024-03-11 15:46:44 -04:00
datFile . Header . SetFieldValue < string? > ( DatHeader . FileNameKey , datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) ;
datFile . Header . SetFieldValue < string? > ( Models . Metadata . Header . DescriptionKey , datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) ;
2024-03-10 04:10:37 -04:00
}
2020-12-10 11:28:11 -08:00
2024-03-11 15:46:44 -04:00
else if ( ! string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) & & ! string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) )
2024-03-10 04:10:37 -04:00
{
2024-03-11 15:46:44 -04:00
datFile . Header . SetFieldValue < string? > ( DatHeader . FileNameKey , datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) ;
2024-03-10 04:10:37 -04:00
}
2020-12-10 11:28:11 -08:00
}
// Filled FileName
else
{
2024-03-11 15:46:44 -04:00
if ( string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) & & string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) )
2024-03-10 04:10:37 -04:00
{
2024-03-11 15:46:44 -04:00
datFile . Header . SetFieldValue < string? > ( Models . Metadata . Header . NameKey , datFile . Header . GetStringFieldValue ( DatHeader . FileNameKey ) ) ;
datFile . Header . SetFieldValue < string? > ( Models . Metadata . Header . DescriptionKey , datFile . Header . GetStringFieldValue ( DatHeader . FileNameKey ) ) ;
2024-03-10 04:10:37 -04:00
}
2020-12-10 11:28:11 -08:00
2024-03-11 15:46:44 -04:00
else if ( string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) & & ! string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) )
2024-03-10 04:10:37 -04:00
{
2024-03-11 15:46:44 -04:00
datFile . Header . SetFieldValue < string? > ( Models . Metadata . Header . NameKey , datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) ;
2024-03-10 04:10:37 -04:00
}
2020-12-10 11:28:11 -08:00
2024-03-11 15:46:44 -04:00
else if ( ! string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) & & string . IsNullOrEmpty ( datFile . Header . GetStringFieldValue ( Models . Metadata . Header . DescriptionKey ) ) )
2024-03-10 04:10:37 -04:00
{
2024-03-11 15:46:44 -04:00
datFile . Header . SetFieldValue < string? > ( Models . Metadata . Header . DescriptionKey , datFile . Header . GetStringFieldValue ( Models . Metadata . Header . NameKey ) ) ;
2024-03-10 04:10:37 -04:00
}
2020-12-10 11:28:11 -08:00
}
}
/// <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>
2020-12-10 11:58:46 -08:00
private static bool HasWritable ( DatFile datFile )
2020-12-10 11:28:11 -08:00
{
// Force a statistics recheck, just in case
datFile . Items . RecalculateStats ( ) ;
2024-03-19 23:15:58 -04:00
datFile . ItemsDB . RecalculateStats ( ) ;
2020-12-10 11:28:11 -08:00
// If there's nothing there, abort
2025-01-12 23:15:30 -05:00
if ( datFile . DatStatistics . TotalCount = = 0 )
2020-12-10 11:28:11 -08:00
return false ;
2024-07-03 10:48:13 -04:00
// if (datFile.ItemsDB.DatStatistics.TotalCount == 0)
// return false;
2020-12-10 11:28:11 -08:00
// If every item is removed, abort
2025-01-12 23:15:30 -05:00
if ( datFile . DatStatistics . TotalCount = = datFile . DatStatistics . RemovedCount )
2020-12-10 11:28:11 -08:00
return false ;
2024-07-03 10:48:13 -04:00
// if (datFile.ItemsDB.DatStatistics.TotalCount == datFile.ItemsDB.DatStatistics.RemovedCount)
// return false;
2020-12-10 11:28:11 -08:00
return true ;
2024-02-28 22:54:56 -05:00
}
2020-12-10 11:28:11 -08:00
}
}