@@ -17,88 +17,9 @@ public static class GameFileParser
1717 /// <param name="onLog">Callback for logging messages.</param>
1818 /// <param name="token">Cancellation token to cancel the operation.</param>
1919 /// <returns>A list of file paths referenced by the CUE sheet.</returns>
20- public static async Task < List < string > > GetReferencedFilesFromCueAsync ( string cuePath , Action < string > onLog , CancellationToken token )
20+ public static Task < List < string > > GetReferencedFilesFromCueAsync ( string cuePath , Action < string > onLog , CancellationToken token )
2121 {
22- var referencedFiles = new List < string > ( ) ;
23- var cueDir = Path . GetDirectoryName ( cuePath ) ?? string . Empty ;
24- try
25- {
26- var lines = await File . ReadAllLinesAsync ( cuePath , token ) ;
27- token . ThrowIfCancellationRequested ( ) ;
28- foreach ( var line in lines )
29- {
30- var trimmedLine = line . Trim ( ) ;
31- if ( ! trimmedLine . StartsWith ( "FILE " , StringComparison . OrdinalIgnoreCase ) )
32- {
33- continue ;
34- }
35-
36- string fileName ;
37- var firstQuote = trimmedLine . IndexOf ( '"' ) ;
38- var lastQuote = trimmedLine . LastIndexOf ( '"' ) ;
39-
40- if ( firstQuote != - 1 && lastQuote > firstQuote )
41- {
42- fileName = trimmedLine . Substring ( firstQuote + 1 , lastQuote - firstQuote - 1 ) ;
43- }
44- else
45- {
46- // Unquoted fallback: split with limit to preserve filename+spaces+type
47- var parts = trimmedLine . Split ( Separator , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
48- if ( parts . Length < 2 )
49- {
50- continue ;
51- }
52-
53- // parts[1] is now "filename TYPE" — strip the trailing file type keyword
54- var rest = parts [ 1 ] . TrimEnd ( ) ;
55- var lastSpace = rest . LastIndexOf ( ' ' ) ;
56- if ( lastSpace > 0 )
57- {
58- // Known CUE file type keywords that follow the filename
59- var afterFilename = rest [ ( lastSpace + 1 ) ..] ;
60- if ( afterFilename . Equals ( "BINARY" , StringComparison . OrdinalIgnoreCase ) ||
61- afterFilename . Equals ( "WAVE" , StringComparison . OrdinalIgnoreCase ) ||
62- afterFilename . Equals ( "MP3" , StringComparison . OrdinalIgnoreCase ) ||
63- afterFilename . Equals ( "AIFF" , StringComparison . OrdinalIgnoreCase ) ||
64- afterFilename . Equals ( "MOTOROLA" , StringComparison . OrdinalIgnoreCase ) ||
65- afterFilename . Equals ( "AUDIO" , StringComparison . OrdinalIgnoreCase ) )
66- {
67- fileName = rest [ ..lastSpace ] ;
68- }
69- else
70- {
71- fileName = rest ;
72- }
73- }
74- else
75- {
76- fileName = rest ;
77- }
78- }
79-
80- referencedFiles . Add ( Path . Combine ( cueDir , fileName ) ) ;
81- }
82- }
83- catch ( OperationCanceledException )
84- {
85- throw ;
86- }
87- catch ( Exception ex )
88- {
89- onLog ( $ "[WARNING] Could not parse CUE file: { Path . GetFileName ( cuePath ) } . Error: { ex . Message } ") ;
90-
91- try
92- {
93- _ = App . SharedBugReportService ? . SendBugReportAsync ( $ "Error parsing CUE file: { Path . GetFileName ( cuePath ) } ", ex ) ;
94- }
95- catch
96- {
97- // Silently fail to avoid cascading errors
98- }
99- }
100-
101- return referencedFiles ;
22+ return ParseFileReferenceLinesAsync ( cuePath , onLog , "CUE" , token ) ;
10223 }
10324
10425 /// <summary>
@@ -186,13 +107,19 @@ public static async Task<List<string>> GetReferencedFilesFromGdiAsync(string gdi
186107 /// <param name="onLog">Callback for logging messages.</param>
187108 /// <param name="token">Cancellation token to cancel the operation.</param>
188109 /// <returns>A list of file paths referenced by the TOC file.</returns>
189- public static async Task < List < string > > GetReferencedFilesFromTocAsync ( string tocPath , Action < string > onLog , CancellationToken token )
110+ public static Task < List < string > > GetReferencedFilesFromTocAsync ( string tocPath , Action < string > onLog , CancellationToken token )
111+ {
112+ return ParseFileReferenceLinesAsync ( tocPath , onLog , "TOC" , token ) ;
113+ }
114+
115+ private static async Task < List < string > > ParseFileReferenceLinesAsync (
116+ string filePath , Action < string > onLog , string fileType , CancellationToken token )
190117 {
191118 var referencedFiles = new List < string > ( ) ;
192- var tocDir = Path . GetDirectoryName ( tocPath ) ?? string . Empty ;
119+ var directory = Path . GetDirectoryName ( filePath ) ?? string . Empty ;
193120 try
194121 {
195- var lines = await File . ReadAllLinesAsync ( tocPath , token ) ;
122+ var lines = await File . ReadAllLinesAsync ( filePath , token ) ;
196123 token . ThrowIfCancellationRequested ( ) ;
197124 foreach ( var line in lines )
198125 {
@@ -212,19 +139,16 @@ public static async Task<List<string>> GetReferencedFilesFromTocAsync(string toc
212139 }
213140 else
214141 {
215- // Unquoted fallback: split with limit to preserve filename+spaces+type
216142 var parts = trimmedLine . Split ( Separator , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
217143 if ( parts . Length < 2 )
218144 {
219145 continue ;
220146 }
221147
222- // parts[1] is now "filename TYPE" — strip the trailing file type keyword
223148 var rest = parts [ 1 ] . TrimEnd ( ) ;
224149 var lastSpace = rest . LastIndexOf ( ' ' ) ;
225150 if ( lastSpace > 0 )
226151 {
227- // Known TOC/CUE file type keywords that follow the filename
228152 var afterFilename = rest [ ( lastSpace + 1 ) ..] ;
229153 if ( afterFilename . Equals ( "BINARY" , StringComparison . OrdinalIgnoreCase ) ||
230154 afterFilename . Equals ( "WAVE" , StringComparison . OrdinalIgnoreCase ) ||
@@ -246,7 +170,7 @@ public static async Task<List<string>> GetReferencedFilesFromTocAsync(string toc
246170 }
247171 }
248172
249- referencedFiles . Add ( Path . Combine ( tocDir , fileName ) ) ;
173+ referencedFiles . Add ( Path . Combine ( directory , fileName ) ) ;
250174 }
251175 }
252176 catch ( OperationCanceledException )
@@ -255,11 +179,11 @@ public static async Task<List<string>> GetReferencedFilesFromTocAsync(string toc
255179 }
256180 catch ( Exception ex )
257181 {
258- onLog ( $ "[WARNING] Could not parse TOC file: { Path . GetFileName ( tocPath ) } . Error: { ex . Message } ") ;
182+ onLog ( $ "[WARNING] Could not parse { fileType } file: { Path . GetFileName ( filePath ) } . Error: { ex . Message } ") ;
259183
260184 try
261185 {
262- _ = App . SharedBugReportService ? . SendBugReportAsync ( $ "Error parsing TOC file: { Path . GetFileName ( tocPath ) } ", ex ) ;
186+ _ = App . SharedBugReportService ? . SendBugReportAsync ( $ "Error parsing { fileType } file: { Path . GetFileName ( filePath ) } ", ex ) ;
263187 }
264188 catch
265189 {
0 commit comments