From 4e482e53a29a88de76ee5157163173f7913fee21 Mon Sep 17 00:00:00 2001 From: trawzified Date: Mon, 15 Jun 2020 20:03:50 +0200 Subject: [PATCH 1/2] Improve metric key storage --- Wabbajack.Common/Metrics.cs | 57 +++++++++++++++++++++++++++++-------- Wabbajack.Common/Utils.cs | 16 +++++++++++ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/Wabbajack.Common/Metrics.cs b/Wabbajack.Common/Metrics.cs index 737a2e6b..1e65806d 100644 --- a/Wabbajack.Common/Metrics.cs +++ b/Wabbajack.Common/Metrics.cs @@ -3,8 +3,10 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using Microsoft.Win32; using Wabbajack.Common.Exceptions; namespace Wabbajack.Common @@ -21,20 +23,51 @@ namespace Wabbajack.Common using var _ = await _creationLock.WaitAsync(); if (!Utils.HaveEncryptedJson(Consts.MetricsKeyHeader)) { - var key = Utils.MakeRandomKey(); - await key.ToEcryptedJson(Consts.MetricsKeyHeader); - return key; + if (!Utils.HaveRegKeyMetricsKey()) + { + // When there's no file or regkey + var key = Utils.MakeRandomKey(); + await key.ToEcryptedJson(Consts.MetricsKeyHeader); + using (RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!) + { + regKey.SetValue("x-metrics-key", key); + } + return key; + } + else + { + // When there's a file but no regkey + Utils.Log("user has reg key but no file"); + // If there is no file but a registry key, create the file and transfer the data from the registry key + using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!) + { + string key = (string)regKey.GetValue(Consts.MetricsKeyHeader)!; + await key.ToEcryptedJson(Consts.MetricsKeyHeader); + return key; + } + } } - - try + else { - return await Utils.FromEncryptedJson(Consts.MetricsKeyHeader); - } - catch (Exception) - { - var key = Utils.MakeRandomKey(); - await key.ToEcryptedJson(Consts.MetricsKeyHeader); - return key; + if (Utils.HaveRegKeyMetricsKey()) + { + // When there's a file and a regkey + using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!) + { + return (string)regKey.GetValue(Consts.MetricsKeyHeader)!; + } + } + else + { + Utils.Log("user has file but no regkey"); + // If there's a regkey and a file, return regkey + using (RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!) + { + string key = await Utils.FromEncryptedJson(Consts.MetricsKeyHeader)!; + regKey.SetValue("x-metrics-key", key); + return key; + } + } } } /// diff --git a/Wabbajack.Common/Utils.cs b/Wabbajack.Common/Utils.cs index ccd0585e..a7dd2819 100644 --- a/Wabbajack.Common/Utils.cs +++ b/Wabbajack.Common/Utils.cs @@ -21,6 +21,7 @@ using ICSharpCode.SharpZipLib.BZip2; using IniParser; using IniParser.Model.Configuration; using IniParser.Parser; +using Microsoft.Win32; using Newtonsoft.Json; using ReactiveUI; using RocksDbSharp; @@ -970,6 +971,21 @@ namespace Wabbajack.Common return Consts.LocalAppDataPath.Combine(key).IsFile; } + public static bool HaveRegKey() + { + return Registry.CurrentUser.OpenSubKey(@"Software\Wabbajack") != null; + } + + public static bool HaveRegKeyMetricsKey() + { + if (HaveRegKey()) + { + return Registry.CurrentUser.OpenSubKey(@"Software\Wabbajack")!.GetValueNames().Contains(Consts.MetricsKeyHeader); + } + return false; + } + + public static IObservable<(FileEventType, FileSystemEventArgs)> AppLocalEvents { get; } public static IObservable HaveEncryptedJsonObservable(string key) From 4987bf02e1a9d67e33099f2206debcac23a21bdd Mon Sep 17 00:00:00 2001 From: trawzified Date: Mon, 15 Jun 2020 20:06:13 +0200 Subject: [PATCH 2/2] Remove redundant logs --- Wabbajack.Common/Metrics.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Wabbajack.Common/Metrics.cs b/Wabbajack.Common/Metrics.cs index 1e65806d..e97e8fdf 100644 --- a/Wabbajack.Common/Metrics.cs +++ b/Wabbajack.Common/Metrics.cs @@ -36,8 +36,6 @@ namespace Wabbajack.Common } else { - // When there's a file but no regkey - Utils.Log("user has reg key but no file"); // If there is no file but a registry key, create the file and transfer the data from the registry key using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!) { @@ -59,7 +57,6 @@ namespace Wabbajack.Common } else { - Utils.Log("user has file but no regkey"); // If there's a regkey and a file, return regkey using (RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!) {