Skip to content

Commit dada75b

Browse files
committed
Add support for specifying multiple game installation registry lookup paths
1 parent 9cb2792 commit dada75b

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

src/TSMapEditor/Constants.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public static class Constants
3030

3131
public static string ExpectedClientExecutableName = "DTA.exe";
3232
public static string GameRegistryInstallPath = "SOFTWARE\\DawnOfTheTiberiumAge";
33-
public static bool InstallPathAtHKLM = false;
3433
public static string OpenFileDialogFilter = "TS maps|*.map|All files|*.*";
3534

3635
public static bool EnableIniInclude = false;
@@ -146,7 +145,6 @@ public static void Init()
146145

147146
ExpectedClientExecutableName = constantsIni.GetStringValue(ConstantsSectionName, nameof(ExpectedClientExecutableName), ExpectedClientExecutableName);
148147
GameRegistryInstallPath = constantsIni.GetStringValue(ConstantsSectionName, nameof(GameRegistryInstallPath), GameRegistryInstallPath);
149-
InstallPathAtHKLM = constantsIni.GetBooleanValue(ConstantsSectionName, nameof(InstallPathAtHKLM), InstallPathAtHKLM);
150148
OpenFileDialogFilter = constantsIni.GetStringValue(ConstantsSectionName, nameof(OpenFileDialogFilter), OpenFileDialogFilter);
151149

152150
EnableIniInclude = constantsIni.GetBooleanValue(ConstantsSectionName, nameof(EnableIniInclude), EnableIniInclude);

src/TSMapEditor/UI/MainMenu.cs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -259,33 +259,61 @@ private void CreateMapWindow_OnCreateNewMap(object sender, CreateNewMapEventArgs
259259

260260
private void ReadGameInstallDirectoryFromRegistry()
261261
{
262-
try
262+
string[] pathsToLookup = Constants.GameRegistryInstallPath.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
263+
264+
if (pathsToLookup.Length == 0)
263265
{
264-
RegistryKey key;
266+
Logger.Log($"No valid paths specified in {nameof(Constants.GameRegistryInstallPath)}. Unable to read game installation path from Windows registry.");
267+
return;
268+
}
265269

266-
if (Constants.InstallPathAtHKLM)
267-
{
268-
key = Registry.LocalMachine.OpenSubKey(Constants.GameRegistryInstallPath);
269-
}
270-
else
270+
try
271+
{
272+
foreach (string registryInstallPath in pathsToLookup)
271273
{
272-
key = Registry.CurrentUser.OpenSubKey(Constants.GameRegistryInstallPath);
273-
}
274+
RegistryKey key;
274275

275-
object value = key.GetValue("InstallPath", string.Empty);
276-
if (!(value is string valueAsString))
277-
{
278-
tbGameDirectory.Text = string.Empty;
279-
}
280-
else
281-
{
282-
if (File.Exists(valueAsString))
283-
tbGameDirectory.Text = Path.GetDirectoryName(valueAsString);
276+
const string hklmIdentifier = "HKLM:";
277+
278+
// By default, try to find the key from the current user's registry.
279+
// Optionally, if the path starts with the HKLM identifier, look for the key in the local machine's registry instead.
280+
if (registryInstallPath.StartsWith(hklmIdentifier))
281+
{
282+
key = Registry.LocalMachine.OpenSubKey(registryInstallPath.Substring(hklmIdentifier.Length));
283+
}
284284
else
285-
tbGameDirectory.Text = valueAsString;
286-
}
285+
{
286+
key = Registry.CurrentUser.OpenSubKey(registryInstallPath);
287+
}
287288

288-
key.Close();
289+
bool isValid = false;
290+
291+
object value = key.GetValue("InstallPath", string.Empty);
292+
if (!(value is string valueAsString))
293+
{
294+
tbGameDirectory.Text = string.Empty;
295+
}
296+
else
297+
{
298+
if (File.Exists(valueAsString))
299+
{
300+
tbGameDirectory.Text = Path.GetDirectoryName(valueAsString);
301+
}
302+
else
303+
{
304+
tbGameDirectory.Text = valueAsString;
305+
}
306+
307+
if (File.Exists(Path.Combine(tbGameDirectory.Text, Constants.ExpectedClientExecutableName)))
308+
isValid = true;
309+
}
310+
311+
key.Close();
312+
313+
// Break when we find the first valid installation path
314+
if (isValid)
315+
break;
316+
}
289317
}
290318
catch (Exception ex)
291319
{

0 commit comments

Comments
 (0)