From d1e24ba5c247bb9cfdeca7251bf5f8951c927576 Mon Sep 17 00:00:00 2001
From: mageven <62494521+mageven@users.noreply.github.com>
Date: Tue, 26 Jan 2021 23:15:07 +0530
Subject: [PATCH] Initial Setup: Reload keys before verifying firmware (#1955)

* Initial Setup: Reload keys before verifying firmware

Also, display the NoKeys dialog if keyset is empty when verifying
firmware.

* LoadApplications: Remove the lone debug log and print the error directly
---
 Ryujinx.HLE/FileSystem/Content/ContentManager.cs | 9 +++++++++
 Ryujinx/Ui/App/ApplicationLibrary.cs             | 3 +--
 Ryujinx/Ui/MainWindow.cs                         | 5 +++++
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/Ryujinx.HLE/FileSystem/Content/ContentManager.cs b/Ryujinx.HLE/FileSystem/Content/ContentManager.cs
index 4c4f3c8676..1630835d2c 100644
--- a/Ryujinx.HLE/FileSystem/Content/ContentManager.cs
+++ b/Ryujinx.HLE/FileSystem/Content/ContentManager.cs
@@ -653,6 +653,15 @@ namespace Ryujinx.HLE.FileSystem.Content
 
         public SystemVersion VerifyFirmwarePackage(string firmwarePackage)
         {
+            _virtualFileSystem.Reload();
+
+            // LibHac.NcaHeader's DecryptHeader doesn't check if HeaderKey is empty and throws InvalidDataException instead
+            // So, we check it early for a better user experience.
+            if (_virtualFileSystem.KeySet.HeaderKey.IsEmpty())
+            {
+                throw new MissingKeyException("HeaderKey is empty. Cannot decrypt NCA headers.");
+            }
+
             Dictionary<ulong, List<(NcaContentType type, string path)>> updateNcas = new Dictionary<ulong, List<(NcaContentType, string)>>();
 
             if (Directory.Exists(firmwarePackage))
diff --git a/Ryujinx/Ui/App/ApplicationLibrary.cs b/Ryujinx/Ui/App/ApplicationLibrary.cs
index fb0e066490..dcf49204b3 100644
--- a/Ryujinx/Ui/App/ApplicationLibrary.cs
+++ b/Ryujinx/Ui/App/ApplicationLibrary.cs
@@ -298,8 +298,7 @@ namespace Ryujinx.Ui.App
                             }
                             catch (Exception exception)
                             {
-                                Logger.Warning?.Print(LogClass.Application, $"The file encountered was not of a valid type. Errored File: {applicationPath}");
-                                Logger.Debug?.Print(LogClass.Application, exception.ToString());
+                                Logger.Warning?.Print(LogClass.Application, $"The file encountered was not of a valid type. File: '{applicationPath}' Error: {exception}");
 
                                 numApplicationsFound--;
                                 _loadingError = true;
diff --git a/Ryujinx/Ui/MainWindow.cs b/Ryujinx/Ui/MainWindow.cs
index 3f2c2fb8a5..7697376bd5 100644
--- a/Ryujinx/Ui/MainWindow.cs
+++ b/Ryujinx/Ui/MainWindow.cs
@@ -1035,6 +1035,11 @@ namespace Ryujinx.Ui
                         thread.Start();
                     }
                 }
+                catch (LibHac.MissingKeyException ex)
+                {
+                    Logger.Error?.Print(LogClass.Application, ex.ToString());
+                    UserErrorDialog.CreateUserErrorDialog(UserError.NoKeys);
+                }
                 catch (Exception ex)
                 {
                     GtkDialog.CreateErrorDialog(ex.Message);