2020-07-31 23:17:12 -07:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2020-12-08 13:23:59 -08:00
using SabreTools.Core ;
2020-12-08 16:37:08 -08:00
using SabreTools.DatFiles ;
2020-12-07 13:57:26 -08:00
using SabreTools.Help ;
2020-07-31 23:17:12 -07:00
namespace SabreTools.Features
{
internal class DatFromDir : BaseFeature
{
public const string Value = "DATFromDir" ;
public DatFromDir ( )
{
Name = Value ;
Flags = new List < string > ( ) { "-d" , "--d2d" , "--dfd" } ;
Description = "Create DAT(s) from an input directory" ;
2020-12-07 13:57:26 -08:00
_featureType = ParameterType . Flag ;
2020-07-31 23:17:12 -07:00
LongDescription = "Create a DAT file from an input directory or set of files. By default, this will output a DAT named based on the input directory and the current date. It will also treat all archives as possible games and add all three hashes (CRC, MD5, SHA-1) for each file." ;
2020-12-07 13:57:26 -08:00
Features = new Dictionary < string , Help . Feature > ( ) ;
2020-07-31 23:17:12 -07:00
// Hash Features
AddFeature ( SkipMd5Flag ) ;
#if NET_FRAMEWORK
AddFeature ( SkipRipeMd160Flag ) ;
#endif
AddFeature ( SkipSha1Flag ) ;
AddFeature ( SkipSha256Flag ) ;
AddFeature ( SkipSha384Flag ) ;
AddFeature ( SkipSha512Flag ) ;
2020-09-04 15:02:15 -07:00
AddFeature ( SkipSpamSumFlag ) ;
2020-07-31 23:17:12 -07:00
AddFeature ( NoAutomaticDateFlag ) ;
2020-08-27 16:57:22 -07:00
AddFeature ( AaruFormatsAsFilesFlag ) ;
2020-07-31 23:17:12 -07:00
AddFeature ( ArchivesAsFilesFlag ) ;
2020-08-27 16:57:22 -07:00
AddFeature ( ChdsAsFilesFlag ) ;
2020-07-31 23:17:12 -07:00
AddFeature ( OutputTypeListInput ) ;
this [ OutputTypeListInput ] . AddFeature ( DeprecatedFlag ) ;
AddFeature ( RombaFlag ) ;
2020-08-18 23:39:13 -07:00
this [ RombaFlag ] . AddFeature ( RombaDepthInt32Input ) ;
2020-07-31 23:17:12 -07:00
AddFeature ( SkipArchivesFlag ) ;
AddFeature ( SkipFilesFlag ) ;
AddHeaderFeatures ( ) ;
AddFeature ( AddBlankFilesFlag ) ;
AddFeature ( AddDateFlag ) ;
AddFeature ( HeaderStringInput ) ;
2020-08-21 10:38:42 -07:00
AddFeature ( ExtraIniListInput ) ;
2020-07-31 23:17:12 -07:00
AddFilteringFeatures ( ) ;
AddFeature ( TempStringInput ) ;
AddFeature ( OutputDirStringInput ) ;
AddFeature ( ThreadsInt32Input ) ;
}
2020-12-07 13:57:26 -08:00
public override void ProcessFeatures ( Dictionary < string , Help . Feature > features )
2020-07-31 23:17:12 -07:00
{
base . ProcessFeatures ( features ) ;
// Get feature flags
bool addBlankFiles = GetBoolean ( features , AddBlankFilesValue ) ;
bool addFileDates = GetBoolean ( features , AddDateValue ) ;
2020-09-18 00:45:08 -07:00
TreatAsFile asFiles = GetTreatAsFiles ( features ) ;
2020-07-31 23:17:12 -07:00
bool noAutomaticDate = GetBoolean ( features , NoAutomaticDateValue ) ;
2020-10-05 17:43:44 -07:00
var includeInScan = GetIncludeInScan ( features ) ;
2020-07-31 23:17:12 -07:00
var skipFileType = GetSkipFileType ( features ) ;
2020-08-28 13:54:53 -07:00
var splitType = GetSplitType ( features ) ;
2020-07-31 23:17:12 -07:00
2020-09-18 11:26:50 -07:00
// Apply the specialized field removals to the cleaner
2020-09-18 10:21:04 -07:00
if ( Cleaner . ExcludeFields = = null )
Cleaner . ExcludeFields = new List < Field > ( ) ;
2020-09-18 11:26:50 -07:00
if ( ! addFileDates )
Cleaner . ExcludeFields . Add ( Field . DatItem_Date ) ;
2020-09-18 10:21:04 -07:00
2020-07-31 23:17:12 -07:00
// Create a new DATFromDir object and process the inputs
DatFile basedat = DatFile . Create ( Header ) ;
basedat . Header . Date = DateTime . Now . ToString ( "yyyy-MM-dd" ) ;
// For each input directory, create a DAT
foreach ( string path in Inputs )
{
if ( Directory . Exists ( path ) | | File . Exists ( path ) )
{
// Clone the base Dat for information
DatFile datdata = DatFile . Create ( basedat . Header ) ;
2020-08-27 20:56:50 -07:00
// Get the base path and fill the header, if needed
2020-07-31 23:17:12 -07:00
string basePath = Path . GetFullPath ( path ) ;
2020-08-27 20:56:50 -07:00
datdata . FillHeaderFromPath ( basePath , noAutomaticDate ) ;
// Now populate from the path
2020-12-10 13:30:08 -08:00
bool success = DirFromDat . PopulateFromDir (
2020-12-10 10:39:39 -08:00
datdata ,
2020-07-31 23:17:12 -07:00
basePath ,
2020-08-02 13:23:47 -07:00
asFiles ,
2020-07-31 23:17:12 -07:00
skipFileType ,
addBlankFiles ,
2020-10-05 17:43:44 -07:00
hashes : includeInScan ) ;
2020-07-31 23:17:12 -07:00
if ( success )
{
2020-08-28 13:59:42 -07:00
// Perform additional processing steps
2020-12-10 13:13:54 -08:00
DatTool . ApplyExtras ( datdata , Extras ) ;
DatTool . ApplySplitting ( datdata , splitType , false ) ;
DatTool . ApplyFilter ( datdata , Filter ) ;
DatTool . ApplyCleaning ( datdata , Cleaner ) ;
2020-08-28 13:59:42 -07:00
// Write out the file
2020-12-10 11:58:46 -08:00
DatTool . Write ( datdata , OutputDir ) ;
2020-07-31 23:17:12 -07:00
}
else
{
Console . WriteLine ( ) ;
OutputRecursive ( 0 ) ;
}
}
}
}
}
}