Skip to content

Commit 97aec7f

Browse files
committed
Fix crash on Open SD Card when Contents directory not found
1 parent 6a29b3d commit 97aec7f

File tree

3 files changed

+100
-49
lines changed

3 files changed

+100
-49
lines changed

NX_Game_Info/Process.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public static bool initialize(out List<string> messages)
6565
log?.WriteLine(" - {0} ({1}exists)", console_keys, File.Exists(console_keys) ? "" : "not ");
6666

6767
keyset = ExternalKeys.ReadKeyFile(prod_keys, title_keys, console_keys);
68+
69+
log?.WriteLine("Found {0} title keys", keyset?.TitleKeys?.Count);
6870
}
6971
catch { }
7072

@@ -130,9 +132,11 @@ public static Title processFile(string filename)
130132
{
131133
log?.WriteLine("Missing {0}: {1}", ex.Type == KeyType.Title ? "Title Key" : "Key", ex.Name.Replace("key_area_key_application", "master_key"));
132134
}
133-
catch (NullReferenceException)
135+
catch (NullReferenceException ex)
134136
{
135-
log?.WriteLine("File {0} has failed to process", filename);
137+
log?.WriteLine(ex.StackTrace);
138+
139+
log?.WriteLine("\nFile {0} has failed to process", filename);
136140
}
137141

138142
return null;

Windows/Main.cs

+53-32
Original file line numberDiff line numberDiff line change
@@ -225,27 +225,41 @@ private void backgroundWorkerProcess_DoWork(object sender, System.ComponentModel
225225
else if (e.Argument is string sdpath)
226226
{
227227
List<LibHac.Title> sdtitles = Process.processSd(sdpath);
228-
int count = sdtitles.Count, index = 0;
229228

230-
foreach (var sdtitle in sdtitles)
229+
if (sdtitles != null)
231230
{
232-
if (worker.CancellationPending) break;
231+
int count = sdtitles.Count, index = 0;
233232

234-
worker.ReportProgress(100 * index++ / count, sdtitle.MainNca?.Filename);
233+
foreach (var sdtitle in sdtitles)
234+
{
235+
if (worker.CancellationPending) break;
235236

236-
Title title = Process.processTitle(sdtitle);
237-
if (title != null)
237+
worker.ReportProgress(100 * index++ / count, sdtitle.MainNca?.Filename);
238+
239+
Title title = Process.processTitle(sdtitle);
240+
if (title != null)
241+
{
242+
titles.Add(title);
243+
}
244+
}
245+
246+
if (!worker.CancellationPending)
238247
{
239-
titles.Add(title);
248+
worker.ReportProgress(100, "");
240249
}
241-
}
242250

243-
if (!worker.CancellationPending)
244-
{
245-
worker.ReportProgress(100, "");
251+
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
246252
}
253+
else
254+
{
255+
worker.ReportProgress(0, "");
247256

248-
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
257+
string error = "SD card \"Contents\" directory could not be found";
258+
Process.log?.WriteLine(error);
259+
260+
e.Result = error;
261+
return;
262+
}
249263
}
250264

251265
e.Result = titles;
@@ -264,33 +278,40 @@ private void backgroundWorkerProcess_ProgressChanged(object sender, ProgressChan
264278

265279
private void backgroundWorkerProcess_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
266280
{
267-
List<Title> titles = (List<Title>)e.Result;
268-
269-
objectListView.SetObjects(titles);
270-
271-
foreach (OLVListItem listItem in objectListView.Items)
281+
if (e.Result is List<Title> titles)
272282
{
273-
Title title = listItem.RowObject as Title;
283+
objectListView.SetObjects(titles);
274284

275-
if (title.signature != true)
285+
foreach (OLVListItem listItem in objectListView.Items)
276286
{
277-
listItem.BackColor = Color.WhiteSmoke;
278-
}
287+
Title title = listItem.RowObject as Title;
279288

280-
if (title.permission == Title.Permission.Dangerous)
281-
{
282-
listItem.ForeColor = Color.DarkRed;
283-
}
284-
else if (title.permission == Title.Permission.Unsafe)
285-
{
286-
listItem.ForeColor = Color.Indigo;
289+
if (title.signature != true)
290+
{
291+
listItem.BackColor = Color.WhiteSmoke;
292+
}
293+
294+
if (title.permission == Title.Permission.Dangerous)
295+
{
296+
listItem.ForeColor = Color.DarkRed;
297+
}
298+
else if (title.permission == Title.Permission.Unsafe)
299+
{
300+
listItem.ForeColor = Color.Indigo;
301+
}
287302
}
288-
}
289303

290-
toolStripStatusLabel.Text = String.Format("{0} files", titles.Count);
304+
toolStripStatusLabel.Text = String.Format("{0} files", titles.Count);
291305

292-
progressDialog.StopProgressDialog();
293-
Activate();
306+
progressDialog.StopProgressDialog();
307+
Activate();
308+
}
309+
else if (e.Result is string error)
310+
{
311+
progressDialog.StopProgressDialog();
312+
313+
MessageBox.Show(String.Format("{0}.", error), Application.ProductName);
314+
}
294315
}
295316
}
296317

macOS/MainWindowController.cs

+41-15
Original file line numberDiff line numberDiff line change
@@ -312,27 +312,39 @@ void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
312312
else if (e.Argument is string sdpath)
313313
{
314314
List<LibHac.Title> sdtitles = Process.processSd(sdpath);
315-
int count = sdtitles.Count, index = 0;
316315

317-
foreach (var sdtitle in sdtitles)
316+
if (sdtitles != null)
318317
{
319-
if (worker.CancellationPending) break;
318+
int count = sdtitles.Count, index = 0;
319+
320+
foreach (var sdtitle in sdtitles)
321+
{
322+
if (worker.CancellationPending) break;
320323

321-
worker.ReportProgress(100 * index++ / count, sdtitle.MainNca?.Filename);
324+
worker.ReportProgress(100 * index++ / count, sdtitle.MainNca?.Filename);
322325

323-
Title title = Process.processTitle(sdtitle);
324-
if (title != null)
326+
Title title = Process.processTitle(sdtitle);
327+
if (title != null)
328+
{
329+
titles.Add(title);
330+
}
331+
}
332+
333+
if (!worker.CancellationPending)
325334
{
326-
titles.Add(title);
335+
worker.ReportProgress(100, "");
327336
}
328-
}
329337

330-
if (!worker.CancellationPending)
331-
{
332-
worker.ReportProgress(100, "");
338+
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
333339
}
340+
else
341+
{
342+
string error = "SD card \"Contents\" directory could not be found";
343+
Process.log?.WriteLine(error);
334344

335-
Process.log?.WriteLine("\n{0} titles processed", titles.Count);
345+
e.Result = error;
346+
return;
347+
}
336348
}
337349

338350
e.Result = titles;
@@ -346,11 +358,25 @@ void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
346358

347359
void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
348360
{
349-
tableViewDataSource.Titles.AddRange((List<Title>)e.Result);
361+
if (e.Result is List<Title> titles)
362+
{
363+
tableViewDataSource.Titles.AddRange(titles);
364+
365+
tableView.ReloadData();
350366

351-
tableView.ReloadData();
367+
Window.EndSheet(sheet);
368+
}
369+
else if (e.Result is string error)
370+
{
371+
Window.EndSheet(sheet);
352372

353-
Window.EndSheet(sheet);
373+
var alert = new NSAlert()
374+
{
375+
InformativeText = String.Format("{0}.", error),
376+
MessageText = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleExecutable").ToString(),
377+
};
378+
alert.RunModal();
379+
}
354380
}
355381

356382
[Export("cancelProgress:")]

0 commit comments

Comments
 (0)