Skip to content

Commit

Permalink
installer: rename legacy registry key
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 committed Jul 12, 2024
1 parent 6bf950e commit 33db968
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
4 changes: 1 addition & 3 deletions tools/installer/Installer/Installers/InstallExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Installer.Models;
using Microsoft.Win32;
using System;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -66,8 +65,7 @@ protected async Task CopyOriginalGameFiles(string sourceDirectory, string target

protected static async Task CopyTR1XFiles(string targetDirectory, IProgress<InstallProgress> progress)
{
using var key = Registry.CurrentUser.CreateSubKey(@"Software\Tomb1Main");
key?.SetValue("InstallPath", targetDirectory);
InstallUtils.StoreInstallationPath(targetDirectory);

progress.Report(new InstallProgress
{
Expand Down
52 changes: 49 additions & 3 deletions tools/installer/Installer/Installers/InstallUtils.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using Installer.Utils;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Installer.Utils;
using System.Threading.Tasks;

namespace Installer.Installers;

public static class InstallUtils
{
private static readonly string _legacyStorageKey = @"Software\Tomb1Main";
private static readonly string _registryStorageKey = @"Software\TR1X";

public static async Task CopyDirectoryTree(
string sourceDirectory,
string targetDirectory,
Expand Down Expand Up @@ -185,4 +189,46 @@ var shortcutPath in Directory.EnumerateFiles(
}
}
}

public static void StoreInstallationPath(string installPath)
{
RenameLegacyStorage();
using var key = Registry.CurrentUser.CreateSubKey(_registryStorageKey);
key?.SetValue("InstallPath", installPath);
}

public static string? GetPreviousInstallationPath()
{
RenameLegacyStorage();
using var key = Registry.CurrentUser.OpenSubKey(_registryStorageKey);
return key?.GetValue("InstallPath")?.ToString();
}

private static void RenameLegacyStorage()
{
using var legacyKey = Registry.CurrentUser.OpenSubKey(_legacyStorageKey);
if (legacyKey is null)
{
return;
}

using var currentKey = Registry.CurrentUser.OpenSubKey(_registryStorageKey);
if (currentKey is not null)
{
return;
}

using var destinationKey = Registry.CurrentUser.CreateSubKey(_registryStorageKey);
foreach (string valueName in legacyKey.GetValueNames())
{
object? objValue = legacyKey.GetValue(valueName);
if (objValue is not null)
{
RegistryValueKind valueKind = legacyKey.GetValueKind(valueName);
destinationKey.SetValue(valueName, objValue, valueKind);
}
}

Registry.CurrentUser.DeleteSubKey(_legacyStorageKey);
}
}
11 changes: 2 additions & 9 deletions tools/installer/Installer/Installers/TR1XInstallSource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -13,7 +12,7 @@ public override IEnumerable<string> DirectoriesToTry
{
get
{
var previousPath = GetPreviousInstallationPath();
var previousPath = InstallUtils.GetPreviousInstallationPath();
if (previousPath is not null)
{
yield return previousPath;
Expand All @@ -30,7 +29,7 @@ public override string SuggestedInstallationDirectory
{
get
{
return GetPreviousInstallationPath() ?? base.SuggestedInstallationDirectory;
return InstallUtils.GetPreviousInstallationPath() ?? base.SuggestedInstallationDirectory;
}
}

Expand Down Expand Up @@ -67,10 +66,4 @@ public override bool IsGameFound(string sourceDirectory)
{
return File.Exists(Path.Combine(sourceDirectory, "TR1X.exe")) || File.Exists(Path.Combine(sourceDirectory, "Tomb1Main.exe"));
}

private static string? GetPreviousInstallationPath()
{
using var key = Registry.CurrentUser.OpenSubKey(@"Software\Tomb1Main");
return key?.GetValue("InstallPath")?.ToString();
}
}

0 comments on commit 33db968

Please sign in to comment.