Skip to content

Commit

Permalink
Move files enumeration to background worker for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
garoxas committed Mar 9, 2019
1 parent 3b90dac commit 0ce16c5
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 52 deletions.
12 changes: 11 additions & 1 deletion NX_Game_Info/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,17 @@ public string masterkeyString
}
public string filename { get; set; }
public long filesize { get; set; }
public string filesizeString { get { StringBuilder builder = new StringBuilder(20); StrFormatByteSize(filesize, builder, 20); return builder.ToString(); } }
public string filesizeString
{
get
{
#if MACOS
return NSByteCountFormatter.Format(filesize, NSByteCountFormatterCountStyle.File);
#else
StringBuilder builder = new StringBuilder(20); StrFormatByteSize(filesize, builder, 20); return builder.ToString();
#endif
}
}
public TitleType type { get; set; }
public string typeString
{
Expand Down
2 changes: 1 addition & 1 deletion Windows/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private void backgroundWorkerProcess_DoWork(object sender, System.ComponentModel
{
if (argumentPath.Item1 == Worker.Directory && argumentPath.Item2 is string path)
{
List<string> filenames = filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
List<string> filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
filenames.Sort();

Expand Down
146 changes: 96 additions & 50 deletions macOS/MainWindowController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public partial class MainWindowController : NSWindowController
private BackgroundWorker backgroundWorker;
private bool userCancelled;

public enum Worker
{
File,
Directory,
SDCard,
Invalid = -1
}

private List<Title> titles = new List<Title>();

public MainWindowController(IntPtr handle) : base(handle)
Expand Down Expand Up @@ -131,14 +139,7 @@ public void OpenFile(NSMenuItem menuItem)
Window.BeginSheet(sheet, ProgressComplete);
List<string> filenames = openPanel.Urls.Select((arg) => arg.Path).ToList();
filenames.Sort();
Process.log?.WriteLine("{0} files selected", filenames.Count);
title.StringValue = String.Format("Opening {0} files", filenames.Count);
backgroundWorker.RunWorkerAsync(filenames);
backgroundWorker.RunWorkerAsync((Worker.File, openPanel.Urls.Select((arg) => arg.Path).ToList()));
}
});
}
Expand Down Expand Up @@ -181,15 +182,7 @@ public void OpenDirectory(NSMenuItem menuItem)
Window.BeginSheet(sheet, ProgressComplete);
List<string> filenames = Directory.EnumerateFiles(openPanel.Urls.First().Path, "*.*", SearchOption.AllDirectories)
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
filenames.Sort();
Process.log?.WriteLine("{0} files selected", filenames.Count);
title.StringValue = String.Format("Opening {0} files from directory {1}", filenames.Count, openPanel.Urls.First().Path);
backgroundWorker.RunWorkerAsync(filenames);
backgroundWorker.RunWorkerAsync((Worker.Directory, openPanel.Urls.First().Path));
}
});
}
Expand Down Expand Up @@ -263,7 +256,7 @@ public void OpenSDCard(NSMenuItem menuItem)
Window.BeginSheet(sheet, ProgressComplete);
backgroundWorker.RunWorkerAsync(openPanel.Urls.First().Path);
backgroundWorker.RunWorkerAsync((Worker.SDCard, openPanel.Urls.First().Path));
}
});
}
Expand Down Expand Up @@ -337,7 +330,7 @@ public void Export(NSMenuItem menuItem)
title.firmware,
title.masterkeyString,
title.filename,
NSByteCountFormatter.Format(title.filesize, NSByteCountFormatterCountStyle.File),
title.filesizeString,
title.typeString,
title.distribution,
title.structureString,
Expand All @@ -362,45 +355,60 @@ void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)

titles.Clear();

if (e.Argument is List<string> filenames)
if (e.Argument is ValueTuple<Worker, List<string>> argumentFile)
{
int count = filenames.Count, index = 0;

foreach (var filename in filenames)
if (argumentFile.Item1 == Worker.File && argumentFile.Item2 is List<string> filenames)
{
if (worker.CancellationPending) break;
filenames.Sort();

worker.ReportProgress(100 * index++ / count, filename);
Process.log?.WriteLine("{0} files selected", filenames.Count);

worker.ReportProgress(-1, String.Format("Opening {0} files", filenames.Count));

Title title = Process.processFile(filename);
if (title != null)
int count = filenames.Count, index = 0;

foreach (var filename in filenames)
{
titles.Add(title);
if (worker.CancellationPending) break;

worker.ReportProgress(100 * index++ / count, filename);

Title title = Process.processFile(filename);
if (title != null)
{
titles.Add(title);
}
}
}

if (!worker.CancellationPending)
{
worker.ReportProgress(100, "");
}
if (!worker.CancellationPending)
{
worker.ReportProgress(100, "");
}

Process.log?.WriteLine("\n{0} titles processed", titles.Count);
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
}
}
else if (e.Argument is string path)
else if (e.Argument is ValueTuple<Worker, string> argumentPath)
{
List<FsTitle> fsTitles = Process.processSd(path);

if (fsTitles != null)
if (argumentPath.Item1 == Worker.Directory && argumentPath.Item2 is string path)
{
int count = fsTitles.Count, index = 0;
List<string> filenames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
.Where(filename => filename.ToLower().EndsWith(".xci") || filename.ToLower().EndsWith(".nsp") || filename.ToLower().EndsWith(".nro")).ToList();
filenames.Sort();

Process.log?.WriteLine("{0} files selected", filenames.Count);

foreach (var fsTitle in fsTitles)
worker.ReportProgress(-1, String.Format("Opening {0} files from directory {1}", filenames.Count, path));

int count = filenames.Count, index = 0;

foreach (var filename in filenames)
{
if (worker.CancellationPending) break;

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

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

Process.log?.WriteLine("\n{0} titles processed", titles.Count);
}
else
else if (argumentPath.Item1 == Worker.SDCard && argumentPath.Item2 is string pathSd)
{
string error = "SD card \"Contents\" directory could not be found";
Process.log?.WriteLine(error);
List<FsTitle> fsTitles = Process.processSd(pathSd);

if (fsTitles != null)
{
int count = fsTitles.Count, index = 0;

foreach (var fsTitle in fsTitles)
{
if (worker.CancellationPending) break;

worker.ReportProgress(100 * index++ / count, fsTitle.MainNca?.Filename);

Title title = Process.processTitle(fsTitle);
if (title != null)
{
titles.Add(title);
}
}

if (!worker.CancellationPending)
{
worker.ReportProgress(100, "");
}

Process.log?.WriteLine("\n{0} titles processed", titles.Count);
}
else
{
worker.ReportProgress(0, "");

string error = "SD card \"Contents\" directory could not be found";
Process.log?.WriteLine(error);

e.Result = error;
return;
e.Result = error;
return;
}
}
}

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

void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
message.StringValue = e.UserState as string;
progress.DoubleValue = e.ProgressPercentage;
if (e.ProgressPercentage == -1)
{
title.StringValue = e.UserState as string;
}
else
{
message.StringValue = e.UserState as string;
progress.DoubleValue = e.ProgressPercentage;
}
}

void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
Expand Down Expand Up @@ -583,7 +629,7 @@ public override NSView GetViewForItem(NSTableView tableView, NSTableColumn table
textField.StringValue = title.filename ?? "";
break;
case "FileSize":
textField.StringValue = NSByteCountFormatter.Format(title.filesize, NSByteCountFormatterCountStyle.File) ?? "";
textField.StringValue = title.filesizeString ?? "";
break;
case "Type":
textField.StringValue = title.typeString ?? "";
Expand Down

0 comments on commit 0ce16c5

Please sign in to comment.