Skip to content

Commit 0ce16c5

Browse files
committed
Move files enumeration to background worker for macOS
1 parent 3b90dac commit 0ce16c5

File tree

3 files changed

+108
-52
lines changed

3 files changed

+108
-52
lines changed

NX_Game_Info/Common.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,17 @@ public string masterkeyString
142142
}
143143
public string filename { get; set; }
144144
public long filesize { get; set; }
145-
public string filesizeString { get { StringBuilder builder = new StringBuilder(20); StrFormatByteSize(filesize, builder, 20); return builder.ToString(); } }
145+
public string filesizeString
146+
{
147+
get
148+
{
149+
#if MACOS
150+
return NSByteCountFormatter.Format(filesize, NSByteCountFormatterCountStyle.File);
151+
#else
152+
StringBuilder builder = new StringBuilder(20); StrFormatByteSize(filesize, builder, 20); return builder.ToString();
153+
#endif
154+
}
155+
}
146156
public TitleType type { get; set; }
147157
public string typeString
148158
{

Windows/Main.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ private void backgroundWorkerProcess_DoWork(object sender, System.ComponentModel
317317
{
318318
if (argumentPath.Item1 == Worker.Directory && argumentPath.Item2 is string path)
319319
{
320-
List<string> filenames = filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
320+
List<string> filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
321321
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
322322
filenames.Sort();
323323

macOS/MainWindowController.cs

+96-50
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public partial class MainWindowController : NSWindowController
2323
private BackgroundWorker backgroundWorker;
2424
private bool userCancelled;
2525

26+
public enum Worker
27+
{
28+
File,
29+
Directory,
30+
SDCard,
31+
Invalid = -1
32+
}
33+
2634
private List<Title> titles = new List<Title>();
2735

2836
public MainWindowController(IntPtr handle) : base(handle)
@@ -131,14 +139,7 @@ public void OpenFile(NSMenuItem menuItem)
131139

132140
Window.BeginSheet(sheet, ProgressComplete);
133141

134-
List<string> filenames = openPanel.Urls.Select((arg) => arg.Path).ToList();
135-
filenames.Sort();
136-
137-
Process.log?.WriteLine("{0} files selected", filenames.Count);
138-
139-
title.StringValue = String.Format("Opening {0} files", filenames.Count);
140-
141-
backgroundWorker.RunWorkerAsync(filenames);
142+
backgroundWorker.RunWorkerAsync((Worker.File, openPanel.Urls.Select((arg) => arg.Path).ToList()));
142143
}
143144
});
144145
}
@@ -181,15 +182,7 @@ public void OpenDirectory(NSMenuItem menuItem)
181182

182183
Window.BeginSheet(sheet, ProgressComplete);
183184

184-
List<string> filenames = Directory.EnumerateFiles(openPanel.Urls.First().Path, "*.*", SearchOption.AllDirectories)
185-
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
186-
filenames.Sort();
187-
188-
Process.log?.WriteLine("{0} files selected", filenames.Count);
189-
190-
title.StringValue = String.Format("Opening {0} files from directory {1}", filenames.Count, openPanel.Urls.First().Path);
191-
192-
backgroundWorker.RunWorkerAsync(filenames);
185+
backgroundWorker.RunWorkerAsync((Worker.Directory, openPanel.Urls.First().Path));
193186
}
194187
});
195188
}
@@ -263,7 +256,7 @@ public void OpenSDCard(NSMenuItem menuItem)
263256

264257
Window.BeginSheet(sheet, ProgressComplete);
265258

266-
backgroundWorker.RunWorkerAsync(openPanel.Urls.First().Path);
259+
backgroundWorker.RunWorkerAsync((Worker.SDCard, openPanel.Urls.First().Path));
267260
}
268261
});
269262
}
@@ -337,7 +330,7 @@ public void Export(NSMenuItem menuItem)
337330
title.firmware,
338331
title.masterkeyString,
339332
title.filename,
340-
NSByteCountFormatter.Format(title.filesize, NSByteCountFormatterCountStyle.File),
333+
title.filesizeString,
341334
title.typeString,
342335
title.distribution,
343336
title.structureString,
@@ -362,45 +355,60 @@ void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
362355

363356
titles.Clear();
364357

365-
if (e.Argument is List<string> filenames)
358+
if (e.Argument is ValueTuple<Worker, List<string>> argumentFile)
366359
{
367-
int count = filenames.Count, index = 0;
368-
369-
foreach (var filename in filenames)
360+
if (argumentFile.Item1 == Worker.File && argumentFile.Item2 is List<string> filenames)
370361
{
371-
if (worker.CancellationPending) break;
362+
filenames.Sort();
372363

373-
worker.ReportProgress(100 * index++ / count, filename);
364+
Process.log?.WriteLine("{0} files selected", filenames.Count);
365+
366+
worker.ReportProgress(-1, String.Format("Opening {0} files", filenames.Count));
374367

375-
Title title = Process.processFile(filename);
376-
if (title != null)
368+
int count = filenames.Count, index = 0;
369+
370+
foreach (var filename in filenames)
377371
{
378-
titles.Add(title);
372+
if (worker.CancellationPending) break;
373+
374+
worker.ReportProgress(100 * index++ / count, filename);
375+
376+
Title title = Process.processFile(filename);
377+
if (title != null)
378+
{
379+
titles.Add(title);
380+
}
379381
}
380-
}
381382

382-
if (!worker.CancellationPending)
383-
{
384-
worker.ReportProgress(100, "");
385-
}
383+
if (!worker.CancellationPending)
384+
{
385+
worker.ReportProgress(100, "");
386+
}
386387

387-
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
388+
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
389+
}
388390
}
389-
else if (e.Argument is string path)
391+
else if (e.Argument is ValueTuple<Worker, string> argumentPath)
390392
{
391-
List<FsTitle> fsTitles = Process.processSd(path);
392-
393-
if (fsTitles != null)
393+
if (argumentPath.Item1 == Worker.Directory && argumentPath.Item2 is string path)
394394
{
395-
int count = fsTitles.Count, index = 0;
395+
List<string> filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
396+
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
397+
filenames.Sort();
398+
399+
Process.log?.WriteLine("{0} files selected", filenames.Count);
396400

397-
foreach (var fsTitle in fsTitles)
401+
worker.ReportProgress(-1, String.Format("Opening {0} files from directory {1}", filenames.Count, path));
402+
403+
int count = filenames.Count, index = 0;
404+
405+
foreach (var filename in filenames)
398406
{
399407
if (worker.CancellationPending) break;
400408

401-
worker.ReportProgress(100 * index++ / count, fsTitle.MainNca?.Filename);
409+
worker.ReportProgress(100 * index++ / count, filename);
402410

403-
Title title = Process.processTitle(fsTitle);
411+
Title title = Process.processFile(filename);
404412
if (title != null)
405413
{
406414
titles.Add(title);
@@ -414,13 +422,44 @@ void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
414422

415423
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
416424
}
417-
else
425+
else if (argumentPath.Item1 == Worker.SDCard && argumentPath.Item2 is string pathSd)
418426
{
419-
string error = "SD card \"Contents\" directory could not be found";
420-
Process.log?.WriteLine(error);
427+
List<FsTitle> fsTitles = Process.processSd(pathSd);
428+
429+
if (fsTitles != null)
430+
{
431+
int count = fsTitles.Count, index = 0;
432+
433+
foreach (var fsTitle in fsTitles)
434+
{
435+
if (worker.CancellationPending) break;
436+
437+
worker.ReportProgress(100 * index++ / count, fsTitle.MainNca?.Filename);
438+
439+
Title title = Process.processTitle(fsTitle);
440+
if (title != null)
441+
{
442+
titles.Add(title);
443+
}
444+
}
445+
446+
if (!worker.CancellationPending)
447+
{
448+
worker.ReportProgress(100, "");
449+
}
450+
451+
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
452+
}
453+
else
454+
{
455+
worker.ReportProgress(0, "");
456+
457+
string error = "SD card \"Contents\" directory could not be found";
458+
Process.log?.WriteLine(error);
421459

422-
e.Result = error;
423-
return;
460+
e.Result = error;
461+
return;
462+
}
424463
}
425464
}
426465

@@ -429,8 +468,15 @@ void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
429468

430469
void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
431470
{
432-
message.StringValue = e.UserState as string;
433-
progress.DoubleValue = e.ProgressPercentage;
471+
if (e.ProgressPercentage == -1)
472+
{
473+
title.StringValue = e.UserState as string;
474+
}
475+
else
476+
{
477+
message.StringValue = e.UserState as string;
478+
progress.DoubleValue = e.ProgressPercentage;
479+
}
434480
}
435481

436482
void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
@@ -583,7 +629,7 @@ public override NSView GetViewForItem(NSTableView tableView, NSTableColumn table
583629
textField.StringValue = title.filename ?? "";
584630
break;
585631
case "FileSize":
586-
textField.StringValue = NSByteCountFormatter.Format(title.filesize, NSByteCountFormatterCountStyle.File) ?? "";
632+
textField.StringValue = title.filesizeString ?? "";
587633
break;
588634
case "Type":
589635
textField.StringValue = title.typeString ?? "";

0 commit comments

Comments
 (0)