2020-07-31 23:17:12 -07:00
using System.Collections.Generic ;
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-12-07 15:08:57 -08:00
using SabreTools.IO ;
2020-12-08 11:09:05 -08:00
using SabreTools.Logging ;
2020-07-31 23:17:12 -07:00
namespace SabreTools.Features
{
internal class Verify : BaseFeature
{
public const string Value = "Verify" ;
public Verify ( )
{
Name = Value ;
Flags = new List < string > ( ) { "-ve" , "--verify" } ;
Description = "Verify a folder against DATs" ;
2020-12-07 13:57:26 -08:00
_featureType = ParameterType . Flag ;
2020-07-31 23:17:12 -07:00
LongDescription = "When used, this will use an input DAT or set of DATs to blindly check against an input folder. The base of the folder is considered the base for the combined DATs and games are either the directories or archives within. This will only do a direct verification of the items within and will create a fixdat afterwards for missing files." ;
Features = new Dictionary < string , Feature > ( ) ;
AddFeature ( DatListInput ) ;
AddFeature ( DepotFlag ) ;
2020-08-18 23:39:13 -07:00
this [ DepotFlag ] . AddFeature ( DepotDepthInt32Input ) ;
2020-07-31 23:17:12 -07:00
AddFeature ( TempStringInput ) ;
AddFeature ( OutputDirStringInput ) ;
AddFeature ( HashOnlyFlag ) ;
AddFeature ( QuickFlag ) ;
AddFeature ( HeaderStringInput ) ;
2020-08-27 16:57:22 -07:00
AddFeature ( AaruFormatsAsFilesFlag ) ;
2020-07-31 23:17:12 -07:00
AddFeature ( ChdsAsFilesFlag ) ;
AddFeature ( IndividualFlag ) ;
AddInternalSplitFeatures ( ) ;
2020-08-21 10:38:42 -07:00
AddFeature ( ExtraIniListInput ) ;
2020-07-31 23:17:12 -07:00
AddFilteringFeatures ( ) ;
}
public override void ProcessFeatures ( Dictionary < string , Feature > features )
{
base . ProcessFeatures ( features ) ;
// Get a list of files from the input datfiles
var datfiles = GetList ( features , DatListValue ) ;
var datfilePaths = DirectoryExtensions . GetFilesOnly ( datfiles ) ;
// Get feature flags
2020-09-18 00:45:08 -07:00
TreatAsFile asFiles = GetTreatAsFiles ( features ) ;
2020-07-31 23:17:12 -07:00
bool hashOnly = GetBoolean ( features , HashOnlyValue ) ;
bool quickScan = GetBoolean ( features , QuickValue ) ;
2020-08-28 13:54:53 -07:00
var splitType = GetSplitType ( features ) ;
2020-07-31 23:17:12 -07:00
// If we are in individual mode, process each DAT on their own
if ( GetBoolean ( features , IndividualValue ) )
{
foreach ( ParentablePath datfile in datfilePaths )
{
2020-08-28 13:59:42 -07:00
// Parse in from the file
2020-07-31 23:17:12 -07:00
DatFile datdata = DatFile . Create ( ) ;
2020-12-10 13:53:34 -08:00
Parser . ParseInto ( datdata , datfile , int . MaxValue , keep : true ) ;
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 , true ) ;
DatTool . ApplyFilter ( datdata , Filter ) ;
DatTool . ApplyCleaning ( datdata , Cleaner ) ;
2020-08-02 12:55:39 -07:00
2020-08-20 11:23:48 -07:00
// Set depot information
datdata . Header . InputDepot = Header . InputDepot . Clone ( ) as DepotInformation ;
2020-08-02 12:55:39 -07:00
// If we have overridden the header skipper, set it now
2020-08-02 12:54:27 -07:00
if ( ! string . IsNullOrEmpty ( Header . HeaderSkipper ) )
datdata . Header . HeaderSkipper = Header . HeaderSkipper ;
2020-07-31 23:17:12 -07:00
// If we have the depot flag, respect it
2020-08-24 11:56:49 -07:00
if ( Header . InputDepot ? . IsActive ? ? false )
2020-09-18 11:40:21 -07:00
{
2020-12-10 13:30:08 -08:00
Verification . VerifyDepot ( datdata , Inputs ) ;
2020-09-18 11:40:21 -07:00
}
2020-07-31 23:17:12 -07:00
else
2020-09-18 11:40:21 -07:00
{
// Loop through and add the inputs to check against
2020-10-07 15:42:30 -07:00
logger . User ( "Processing files:\n" ) ;
2020-09-18 11:40:21 -07:00
foreach ( string input in Inputs )
{
2020-12-10 13:30:08 -08:00
DirFromDat . PopulateFromDir ( datdata , input , asFiles : asFiles , hashes : quickScan ? Hash . CRC : Hash . Standard ) ;
2020-09-18 11:40:21 -07:00
}
2020-12-10 13:30:08 -08:00
Verification . VerifyGeneric ( datdata , hashOnly ) ;
2020-09-18 11:40:21 -07:00
}
2020-08-27 22:53:21 -07:00
// Now write out if there are any items left
2020-09-18 10:42:06 -07:00
datdata . WriteStatsToConsole ( ) ;
2020-12-10 11:58:46 -08:00
DatTool . Write ( datdata , OutputDir ) ;
2020-07-31 23:17:12 -07:00
}
}
// Otherwise, process all DATs into the same output
else
{
InternalStopwatch watch = new InternalStopwatch ( "Populating internal DAT" ) ;
// Add all of the input DATs into one huge internal DAT
DatFile datdata = DatFile . Create ( ) ;
foreach ( ParentablePath datfile in datfilePaths )
{
2020-12-10 13:53:34 -08:00
Parser . ParseInto ( datdata , datfile , int . MaxValue , keep : true ) ;
2020-07-31 23:17:12 -07:00
}
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 , true ) ;
DatTool . ApplyFilter ( datdata , Filter ) ;
DatTool . ApplyCleaning ( datdata , Cleaner ) ;
2020-08-28 13:59:42 -07:00
2020-08-20 11:23:48 -07:00
// Set depot information
datdata . Header . InputDepot = Header . InputDepot . Clone ( ) as DepotInformation ;
2020-08-02 12:55:39 -07:00
// If we have overridden the header skipper, set it now
if ( ! string . IsNullOrEmpty ( Header . HeaderSkipper ) )
datdata . Header . HeaderSkipper = Header . HeaderSkipper ;
2020-07-31 23:17:12 -07:00
watch . Stop ( ) ;
// If we have the depot flag, respect it
2020-08-27 22:56:11 -07:00
if ( Header . InputDepot ? . IsActive ? ? false )
2020-09-18 11:40:21 -07:00
{
2020-12-10 13:30:08 -08:00
Verification . VerifyDepot ( datdata , Inputs ) ;
2020-09-18 11:40:21 -07:00
}
2020-07-31 23:17:12 -07:00
else
2020-09-18 11:40:21 -07:00
{
// Loop through and add the inputs to check against
2020-10-07 15:42:30 -07:00
logger . User ( "Processing files:\n" ) ;
2020-09-18 11:40:21 -07:00
foreach ( string input in Inputs )
{
2020-12-10 13:30:08 -08:00
DirFromDat . PopulateFromDir ( datdata , input , asFiles : asFiles , hashes : quickScan ? Hash . CRC : Hash . Standard ) ;
2020-09-18 11:40:21 -07:00
}
2020-12-10 13:30:08 -08:00
Verification . VerifyGeneric ( datdata , hashOnly ) ;
2020-09-18 11:40:21 -07:00
}
2020-08-27 22:53:21 -07:00
// Now write out if there are any items left
2020-09-18 10:42:06 -07:00
datdata . WriteStatsToConsole ( ) ;
2020-12-10 11:58:46 -08:00
DatTool . Write ( datdata , OutputDir ) ;
2020-07-31 23:17:12 -07:00
}
}
}
}