From b74016bba4a680c277284be4a5dda1fd17a273ae Mon Sep 17 00:00:00 2001 From: PNWParksFan Date: Sun, 27 Oct 2019 23:15:56 -0700 Subject: [PATCH 1/3] Added auto-detection of GTA V folder from registry --- Utils/GTAFolder.cs | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Utils/GTAFolder.cs b/Utils/GTAFolder.cs index 2cc77e7..45a5ecb 100644 --- a/Utils/GTAFolder.cs +++ b/Utils/GTAFolder.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using System.IO; using System.Windows.Forms; using CodeWalker.Properties; +using Microsoft.Win32; namespace CodeWalker { @@ -52,6 +53,13 @@ namespace CodeWalker string origFolder = CurrentGTAFolder; string folder = CurrentGTAFolder; SelectFolderForm f = new SelectFolderForm(); + + string autoFolder = AutoDetectFolder(); + if (autoFolder != null) + { + f.SelectedFolder = autoFolder; + } + f.ShowDialog(); if(f.Result == DialogResult.OK && Directory.Exists(f.SelectedFolder)) { @@ -95,5 +103,50 @@ namespace CodeWalker public static string GetCurrentGTAFolderWithTrailingSlash() =>CurrentGTAFolder.EndsWith(@"\") ? CurrentGTAFolder : CurrentGTAFolder + @"\"; + public static bool AutoDetectFolder(out string steamPath, out string retailPath) + { + retailPath = null; + steamPath = null; + + RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); + string steamPathValue = baseKey.OpenSubKey(@"Software\Rockstar Games\GTAV")?.GetValue("InstallFolderSteam") as string; + string retailPathValue = baseKey.OpenSubKey(@"Software\Rockstar Games\Grand Theft Auto V")?.GetValue("InstallFolder") as string; + + if(steamPathValue != null) + { + if(steamPathValue.EndsWith("\\GTAV")) + { + steamPathValue = steamPathValue.Substring(0, steamPathValue.LastIndexOf("\\GTAV")); + } + + if(ValidateGTAFolder(steamPathValue)) + { + steamPath = steamPathValue; + } + } + + if(retailPathValue != null && ValidateGTAFolder(retailPathValue)) + { + retailPath = retailPathValue; + } + + return steamPath != null || retailPath != null; + } + + public static string AutoDetectFolder() + { + if(AutoDetectFolder(out string steam, out string retail)) + { + if(steam != null) + { + return steam; + } else if(retail != null) + { + return retail; + } + } + + return null; + } } } From 644733a8d0cf0dcf40807fc8b770bddbbcd1232f Mon Sep 17 00:00:00 2001 From: PNWParksFan Date: Sun, 27 Oct 2019 23:28:15 -0700 Subject: [PATCH 2/3] Added detection of OIV path for increased conversion --- Utils/GTAFolder.cs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/Utils/GTAFolder.cs b/Utils/GTAFolder.cs index 45a5ecb..a9b1c50 100644 --- a/Utils/GTAFolder.cs +++ b/Utils/GTAFolder.cs @@ -103,40 +103,48 @@ namespace CodeWalker public static string GetCurrentGTAFolderWithTrailingSlash() =>CurrentGTAFolder.EndsWith(@"\") ? CurrentGTAFolder : CurrentGTAFolder + @"\"; - public static bool AutoDetectFolder(out string steamPath, out string retailPath) + public static bool AutoDetectFolder(out string steamPath, out string retailPath, out string oivPath) { retailPath = null; steamPath = null; + oivPath = null; - RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); - string steamPathValue = baseKey.OpenSubKey(@"Software\Rockstar Games\GTAV")?.GetValue("InstallFolderSteam") as string; - string retailPathValue = baseKey.OpenSubKey(@"Software\Rockstar Games\Grand Theft Auto V")?.GetValue("InstallFolder") as string; + RegistryKey baseKey32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); + string steamPathValue = baseKey32.OpenSubKey(@"Software\Rockstar Games\GTAV")?.GetValue("InstallFolderSteam") as string; + string retailPathValue = baseKey32.OpenSubKey(@"Software\Rockstar Games\Grand Theft Auto V")?.GetValue("InstallFolder") as string; + string oivPathValue = Registry.CurrentUser.OpenSubKey(@"Software\NewTechnologyStudio\OpenIV.exe\BrowseForFolder")?.GetValue("game_path_Five_pc") as string; - if(steamPathValue != null) + if(steamPathValue?.EndsWith("\\GTAV") == true) { - if(steamPathValue.EndsWith("\\GTAV")) - { - steamPathValue = steamPathValue.Substring(0, steamPathValue.LastIndexOf("\\GTAV")); - } - - if(ValidateGTAFolder(steamPathValue)) - { - steamPath = steamPathValue; - } + steamPathValue = steamPathValue.Substring(0, steamPathValue.LastIndexOf("\\GTAV")); } - if(retailPathValue != null && ValidateGTAFolder(retailPathValue)) + if(ValidateGTAFolder(steamPathValue)) + { + steamPath = steamPathValue; + } + + if(ValidateGTAFolder(retailPathValue)) { retailPath = retailPathValue; } - return steamPath != null || retailPath != null; + if(ValidateGTAFolder(oivPathValue)) + { + oivPath = oivPathValue; + } + + return steamPath != null || retailPath != null || oivPath != null; } public static string AutoDetectFolder() { - if(AutoDetectFolder(out string steam, out string retail)) + if(AutoDetectFolder(out string steam, out string retail, out string oiv)) { + if(oiv != null) + { + return oiv; + } if(steam != null) { return steam; From 07a724ee0f497d4adbfb6464c8afb25843c57e47 Mon Sep 17 00:00:00 2001 From: PNWParksFan Date: Sun, 27 Oct 2019 23:39:25 -0700 Subject: [PATCH 3/3] Changed to more reusable methods --- Utils/GTAFolder.cs | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/Utils/GTAFolder.cs b/Utils/GTAFolder.cs index a9b1c50..ec6a4e9 100644 --- a/Utils/GTAFolder.cs +++ b/Utils/GTAFolder.cs @@ -54,8 +54,8 @@ namespace CodeWalker string folder = CurrentGTAFolder; SelectFolderForm f = new SelectFolderForm(); - string autoFolder = AutoDetectFolder(); - if (autoFolder != null) + string autoFolder = AutoDetectFolder(out string source); + if (autoFolder != null && MessageBox.Show($"Auto-detected game folder \"{autoFolder}\" from {source}.\n\nContinue with auto-detected folder?", "Auto-detected game folder", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { f.SelectedFolder = autoFolder; } @@ -103,11 +103,14 @@ namespace CodeWalker public static string GetCurrentGTAFolderWithTrailingSlash() =>CurrentGTAFolder.EndsWith(@"\") ? CurrentGTAFolder : CurrentGTAFolder + @"\"; - public static bool AutoDetectFolder(out string steamPath, out string retailPath, out string oivPath) + public static bool AutoDetectFolder(out Dictionary matches) { - retailPath = null; - steamPath = null; - oivPath = null; + matches = new Dictionary(); + + if(ValidateGTAFolder(CurrentGTAFolder)) + { + matches.Add("Current CodeWalker Folder", CurrentGTAFolder); + } RegistryKey baseKey32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); string steamPathValue = baseKey32.OpenSubKey(@"Software\Rockstar Games\GTAV")?.GetValue("InstallFolderSteam") as string; @@ -121,40 +124,36 @@ namespace CodeWalker if(ValidateGTAFolder(steamPathValue)) { - steamPath = steamPathValue; + matches.Add("Steam", steamPathValue); } if(ValidateGTAFolder(retailPathValue)) { - retailPath = retailPathValue; + matches.Add("Retail", retailPathValue); } if(ValidateGTAFolder(oivPathValue)) { - oivPath = oivPathValue; + matches.Add("OpenIV", oivPathValue); } - return steamPath != null || retailPath != null || oivPath != null; + return matches.Count > 0; } - public static string AutoDetectFolder() + public static string AutoDetectFolder(out string source) { - if(AutoDetectFolder(out string steam, out string retail, out string oiv)) + source = null; + + if(AutoDetectFolder(out Dictionary matches)) { - if(oiv != null) - { - return oiv; - } - if(steam != null) - { - return steam; - } else if(retail != null) - { - return retail; - } + var match = matches.First(); + source = match.Key; + return match.Value; } return null; } + + public static string AutoDetectFolder() => AutoDetectFolder(out string _); } }