1- using Sprache ;
1+ using Serilog ;
2+ using Sprache ;
23using System ;
34using System . Collections . Generic ;
5+ using System . Data ;
46using System . Diagnostics ;
57using System . IO ;
68using System . Linq ;
79using System . Text ;
810using System . Threading . Tasks ;
9- using System . Data ;
1011using TruckLib . HashFs ;
11- using static Extractor . PathUtils ;
1212using static Extractor . ConsoleUtils ;
13+ using static Extractor . PathUtils ;
1314using static Extractor . TextUtils ;
1415
1516namespace Extractor . Deep
@@ -41,8 +42,11 @@ public class HashFsDeepExtractor : HashFsExtractor
4142
4243 private bool hasSearchedForPaths ;
4344
45+ private readonly ILogger logger ;
46+
4447 public HashFsDeepExtractor ( string scsPath , Options opt ) : base ( scsPath , opt )
4548 {
49+ logger = Log . ForContext < HashFsDeepExtractor > ( ) ;
4650 }
4751
4852 /// <inheritdoc/>
@@ -58,6 +62,7 @@ public void Extract(IList<string> foundFiles, string outputRoot, bool ignoreMiss
5862 bool startPathsSet = ! opt . StartPaths . SequenceEqual ( [ "/" ] ) ;
5963
6064 substitutions = DeterminePathSubstitutions ( foundFiles ) ;
65+ LogPathSubstitutions ( ) ;
6166
6267 // Extract regular files
6368 var filteredFoundFiles = foundFiles
@@ -69,6 +74,8 @@ public void Extract(IList<string> foundFiles, string outputRoot, bool ignoreMiss
6974 } )
7075 . Order ( )
7176 . ToArray ( ) ;
77+ logger . Information ( "[{ScsName}] Extracting {NumPaths} files with known paths" ,
78+ ScsName , filteredFoundFiles . Length ) ;
7279 ExtractFiles ( filteredFoundFiles , outputRoot , ignoreMissing ) ;
7380
7481 // Extract decoy files
@@ -81,6 +88,11 @@ public void Extract(IList<string> foundFiles, string outputRoot, bool ignoreMiss
8188 } )
8289 . Order ( )
8390 . ToArray ( ) ;
91+ if ( foundDecoyFiles . Length > 0 )
92+ {
93+ logger . Information ( "[{ScsName}] Extracting {NumDecoyPaths} decoy files" ,
94+ ScsName , foundDecoyFiles . Length ) ;
95+ }
8496 var decoyDestination = Path . Combine ( outputRoot , DecoyDirectory ) ;
8597 foreach ( var decoyFile in foundDecoyFiles )
8698 {
@@ -99,12 +111,15 @@ public void Extract(IList<string> foundFiles, string outputRoot, bool ignoreMiss
99111
100112 public ( HashSet < string > FoundFiles , HashSet < string > ReferencedFiles ) FindPaths ( )
101113 {
114+ logger . Information ( "[{ScsName}] Searching archive contents for paths" , ScsName ) ;
102115 if ( ! hasSearchedForPaths )
103116 {
104117 finder = new HashFsPathFinder ( Reader , opt . AdditionalStartPaths , junkEntries ) ;
105118 finder . Find ( ) ;
106119 hasSearchedForPaths = true ;
107120 }
121+ logger . Information ( "[{ScsName}] Found {NumFoundFiles} paths, {NumReferencedFiles} referenced paths" ,
122+ ScsName , finder . FoundFiles . Count , finder . ReferencedFiles . Count ) ;
108123 return ( finder . FoundFiles , finder . ReferencedFiles ) ;
109124 }
110125
@@ -133,21 +148,31 @@ private void DumpUnrecovered(string destination, IEnumerable<string> foundFiles)
133148 }
134149 junkEntries . TryGetValue ( Reader . HashPath ( f ) , out var retval ) ;
135150 return retval ;
136- } ) ) ;
151+ } ) )
152+ . Except ( junkEntries . Values )
153+ . Except ( maybeJunkEntries . Values )
154+ . ToArray ( ) ;
137155
156+ if ( notRecovered . Length == 0 )
157+ {
158+ return ;
159+ }
160+
161+ logger . Information ( "[{ScsName}] Dumping {NumUnrecovered} files with unknown paths" ,
162+ ScsName , notRecovered . Length ) ;
138163 HashSet < ulong > visitedOffsets = [ ] ;
139164
140165 var outputDir = Path . Combine ( destination , DumpDirectory ) ;
141166
142167 foreach ( var entry in notRecovered )
143168 {
144- if ( junkEntries . ContainsKey ( entry . Hash ) ||
169+ /* if (junkEntries.ContainsKey(entry.Hash) ||
145170 maybeJunkEntries.ContainsKey(entry.Hash))
146171 {
147172 continue;
148- }
173+ }*/
149174
150- var offsetHexStr = entry . Hash . ToString ( "x16" ) ;
175+ var hashHexStr = entry . Hash . ToString ( "x16" ) ;
151176
152177 byte [ ] fileBuffer ;
153178 try
@@ -156,14 +181,15 @@ private void DumpUnrecovered(string destination, IEnumerable<string> foundFiles)
156181 }
157182 catch ( Exception ex )
158183 {
159- Console . WriteLine ( $ "Uanble to dump { offsetHexStr } : { ex . Message } ") ;
184+ Console . WriteLine ( $ "Unable to dump { hashHexStr } : { ex . Message } ") ;
185+ logger . Error ( ex , "[{ScsName}] Unable to dump {HashStr} ({Offset})" , ScsName , hashHexStr , entry . Offset ) ;
160186 numFailed ++ ;
161187 continue ;
162188 }
163189
164190 var fileType = FileTypeHelper . Infer ( fileBuffer ) ;
165191 var extension = FileTypeHelper . FileTypeToExtension ( fileType ) ;
166- var fileName = offsetHexStr + extension ;
192+ var fileName = hashHexStr + extension ;
167193 var outputPath = Path . Combine ( outputDir , fileName ) ;
168194 Console . WriteLine ( $ "Dumping { fileName } ...") ;
169195 if ( ! Overwrite && File . Exists ( outputPath ) )
@@ -172,6 +198,7 @@ private void DumpUnrecovered(string destination, IEnumerable<string> foundFiles)
172198 }
173199 else
174200 {
201+ logger . Verbose ( "[{ScsName}] Dumping {FileName}" , ScsName , fileName ) ;
175202 ExtractToDisk ( entry , $ "/{ DumpDirectory } /{ fileName } ", outputPath ) ;
176203 numDumped ++ ;
177204 }
@@ -180,6 +207,7 @@ private void DumpUnrecovered(string destination, IEnumerable<string> foundFiles)
180207
181208 public override void PrintPaths ( bool includeAll )
182209 {
210+ logger . Information ( "[{ScsName}] Searching archive contents for paths" , ScsName ) ;
183211 var finder = new HashFsPathFinder ( Reader ) ;
184212 finder . Find ( ) ;
185213 var paths = ( includeAll
@@ -194,11 +222,17 @@ public override void PrintExtractionResult()
194222 Console . WriteLine ( $ "{ numExtracted } extracted " +
195223 $ "({ renamedFiles . Count } renamed, { modifiedFiles . Count } modified, { numDumped } dumped), " +
196224 $ "{ numSkipped } skipped, { numJunk } junk, { numFailed } failed") ;
225+ logger . Information ( "[{ScsName}] {NumExtracted} extracted " +
226+ "({NumRenamed} renamed, {NumModified} modified, {NumDumped} dumped), " +
227+ "{NumSkipped} skipped, {NumJunk} junk, {NumFailed} failed" ,
228+ ScsName , numExtracted , renamedFiles . Count , modifiedFiles . Count ,
229+ numDumped , numSkipped , numJunk , numFailed ) ;
197230 PrintRenameSummary ( renamedFiles . Count , modifiedFiles . Count ) ;
198231 }
199232
200233 public override List < Tree . Directory > GetDirectoryTree ( )
201234 {
235+ logger . Information ( "[{ScsName}] Searching archive contents for paths" , ScsName ) ;
202236 var finder = new HashFsPathFinder ( Reader ) ;
203237 finder . Find ( ) ;
204238
0 commit comments