WindowsGameLocationProvider.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Microsoft.Win32;
  2. using System.IO;
  3. namespace OpenVIII
  4. {
  5. public sealed class WindowsGameLocationProvider : IGameLocationProvider
  6. {
  7. /// <summary>
  8. /// Steam 2013
  9. /// </summary>
  10. private const string SteamRegistyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 39150";
  11. /// <summary>
  12. /// Steam 2013
  13. /// </summary>
  14. private const string SteamGamePathTag = @"InstallLocation";
  15. /// <summary>
  16. /// Supplied from LordUrQuan
  17. /// </summary>
  18. private const string CD2000RegistyPath = @"SOFTWARE\Wow6432Node\Square Soft, Inc\FINAL FANTASY VIII\1.00";
  19. /// <summary>
  20. /// Supplied from LordUrQuan
  21. /// </summary>
  22. private const string CD2000GamePathTag = @"AppPath";
  23. /// <summary>
  24. /// ReMaster
  25. /// </summary>
  26. private const string SteamRERegistyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 1026680";
  27. /// <summary>
  28. /// ReMaster
  29. /// </summary>
  30. private const string SteamREGamePathTag = @"InstallLocation";
  31. public GameLocation GetGameLocation()
  32. {
  33. if (_hardcoded.FindGameLocation(out GameLocation gameLocation))
  34. return gameLocation;
  35. foreach (RegistryView registryView in new RegistryView[] { RegistryView.Registry32, RegistryView.Registry64 })
  36. {
  37. GameLocation r;
  38. if ((r = Check(SteamRegistyPath, SteamGamePathTag)) == null &&
  39. (r = Check(CD2000RegistyPath, CD2000RegistyPath)) == null &&
  40. (r = Check(SteamRERegistyPath, SteamREGamePathTag)) == null)
  41. continue;
  42. else
  43. return r;
  44. GameLocation Check(string path, string tag)
  45. {
  46. using (RegistryKey localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
  47. using (RegistryKey registryKey = localMachine.OpenSubKey(path))
  48. {
  49. if (registryKey != null)
  50. {
  51. string installLocation = (string)registryKey.GetValue(tag);
  52. string dataPath = installLocation;
  53. if (Directory.Exists(dataPath))
  54. return new GameLocation(dataPath);
  55. }
  56. }
  57. return null;
  58. }
  59. }
  60. throw new DirectoryNotFoundException($"Cannot find game directory." +
  61. $"Add your own path to the {nameof(WindowsGameLocationProvider)} type or fix a registration in the registry:" +
  62. $"{SteamRegistyPath}.{SteamGamePathTag}");
  63. }
  64. private readonly HardcodedGameLocationProvider _hardcoded = new HardcodedGameLocationProvider(new[]
  65. {
  66. @"C:\Program Files (x86)\Steam\steamapps\common\FINAL FANTASY VIII",//Data\lang-en
  67. @"D:\SteamLibrary\steamapps\common\FINAL FANTASY VIII",
  68. @"D:\Steam\steamapps\common\FINAL FANTASY VIII",
  69. });
  70. }
  71. }