Skip to content

Commit

Permalink
Better handling of broken settings files (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
Klocman authored Aug 21, 2024
1 parent 1628a88 commit 25ce789
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions source/BulkCrapUninstaller/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal static void PrepareSettings()
if (dir.Name.StartsWith("win-x") && dir.Parent != null) dir = dir.Parent;
var settingsDir = dir.FullName;
PortableSettingsProvider.PortableSettingsProvider.AppSettingsPathOverride = settingsDir;
ConfigFileFullname = Path.Combine(settingsDir, @"BCUninstaller.settings");
ConfigFileFullname = Path.Combine(settingsDir, "BCUninstaller.settings");

var settingsXmlDocument = XDocument.Parse(File.ReadAllText(ConfigFileFullname));
if (settingsXmlDocument.Root == null)
Expand All @@ -127,23 +127,33 @@ internal static void PrepareSettings()

if (!string.IsNullOrWhiteSpace(result.Value) && new Version(result.Value) < AssemblyVersion)
IsAfterUpgrade = true;

// One extra check to make sure loading and using the settings doesn't throw
// Initializes the Default settings object (unless it has been accessed before, which it shouldn't have)
Settings.Default.Reload();
Settings.Default.AdvancedSimulate = Settings.Default.AdvancedSimulate;
}
catch
catch (Exception ex)
{
DeleteConfigFile();
Console.WriteLine("Failed to load settings from the config file: " + ex);

File.Delete(ConfigFileFullname);
Settings.Default.Reload();
}

// Initializes the settings object (unless it has been accessed before, which it shouldnt have)
// Ensure the user ID is valid
if (Settings.Default.MiscUserId == 0)
Settings.Default.MiscUserId = GetUniqueUserId();

if (IsAfterUpgrade)
ClearCaches(false);
}

/// <summary>
/// Get an ID that is unlikely to be duplicate but that should always return the same on current pc
/// </summary>
private static ulong GetUniqueUserId()
{
// Get an ID that is unlikely to be duplicate but that should always return the same on current pc
var windowsIdentity = WindowsIdentity.GetCurrent();

string networkIdentity;
Expand All @@ -162,11 +172,6 @@ private static ulong GetUniqueUserId()
return UninstallerRatingManager.Utils.StableHash(idStr);
}

private static void DeleteConfigFile()
{
File.Delete(ConfigFileFullname);
}

/// <summary>
/// TODO: A bit wonky, needs to be remade
/// </summary>
Expand Down

0 comments on commit 25ce789

Please sign in to comment.