You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.9 KiB

  1. using IPA.Config;
  2. using System;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6. #if NET3
  7. using Path = Net3_Proxy.Path;
  8. #endif
  9. namespace IPA.Utilities
  10. {
  11. /// <summary>
  12. /// Provides some basic utility methods and properties of Beat Saber
  13. /// </summary>
  14. public static class UnityGame
  15. {
  16. private static AlmostVersion _gameVersion;
  17. /// <summary>
  18. /// Provides the current game version.
  19. /// </summary>
  20. /// <value>the SemVer version of the game</value>
  21. public static AlmostVersion GameVersion => _gameVersion ?? (_gameVersion = new AlmostVersion(ApplicationVersionProxy));
  22. internal static void SetEarlyGameVersion(AlmostVersion ver)
  23. {
  24. _gameVersion = ver;
  25. Logging.Logger.log.Debug($"GameVersion set early to {ver}");
  26. }
  27. private static string ApplicationVersionProxy => Application.version;
  28. internal static void EnsureRuntimeGameVersion()
  29. {
  30. try
  31. {
  32. var rtVer = new AlmostVersion(ApplicationVersionProxy);
  33. if (!rtVer.Equals(_gameVersion)) // this actually uses stricter equality than == for AlmostVersion
  34. {
  35. Logging.Logger.log.Warn($"Early version {_gameVersion} parsed from game files doesn't match runtime version {rtVer}!");
  36. _gameVersion = rtVer;
  37. }
  38. }
  39. catch (MissingMethodException e)
  40. {
  41. Logging.Logger.log.Error("Application.version was not found! Cannot check early parsed version");
  42. if (SelfConfig.Debug_.ShowHandledErrorStackTraces_)
  43. Logging.Logger.log.Error(e);
  44. }
  45. }
  46. internal static bool IsGameVersionBoundary { get; private set; }
  47. internal static AlmostVersion OldVersion { get; private set; }
  48. internal static void CheckGameVersionBoundary()
  49. {
  50. var gameVer = GameVersion;
  51. var lastVerS = SelfConfig.LastGameVersion_;
  52. OldVersion = lastVerS != null ? new AlmostVersion(lastVerS, gameVer) : null;
  53. IsGameVersionBoundary = OldVersion != null && gameVer != OldVersion;
  54. SelfConfig.Instance.LastGameVersion = gameVer.ToString();
  55. }
  56. /// <summary>
  57. /// The different types of releases of the game.
  58. /// </summary>
  59. public enum Release
  60. {
  61. /// <summary>
  62. /// Indicates a Steam release.
  63. /// </summary>
  64. Steam,
  65. /// <summary>
  66. /// Indicates a non-Steam release.
  67. /// </summary>
  68. Other
  69. }
  70. private static Release? _releaseCache;
  71. /// <summary>
  72. /// Gets the <see cref="Release"/> type of this installation of Beat Saber
  73. /// </summary>
  74. /// <remarks>
  75. /// This only gives a
  76. /// </remarks>
  77. /// <value>the type of release this is</value>
  78. public static Release ReleaseType => (_releaseCache ?? (_releaseCache = CheckIsSteam() ? Release.Steam : Release.Other)).Value;
  79. private static string _installRoot;
  80. /// <summary>
  81. /// Gets the path to the Beat Saber install directory.
  82. /// </summary>
  83. /// <value>the path of the game install directory</value>
  84. public static string InstallPath
  85. {
  86. get
  87. {
  88. if (_installRoot == null)
  89. _installRoot = Path.GetFullPath(
  90. Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", ".."));
  91. return _installRoot;
  92. }
  93. }
  94. /// <summary>
  95. /// The path to the `Libs` folder. Use only if necessary.
  96. /// </summary>
  97. /// <value>the path to the library directory</value>
  98. public static string LibraryPath => Path.Combine(InstallPath, "Libs");
  99. /// <summary>
  100. /// The path to the `Libs\Native` folder. Use only if necessary.
  101. /// </summary>
  102. /// <value>the path to the native library directory</value>
  103. public static string NativeLibraryPath => Path.Combine(LibraryPath, "Native");
  104. /// <summary>
  105. /// The directory to load plugins from.
  106. /// </summary>
  107. /// <value>the path to the plugin directory</value>
  108. public static string PluginsPath => Path.Combine(InstallPath, "Plugins");
  109. /// <summary>
  110. /// The path to the `UserData` folder.
  111. /// </summary>
  112. /// <value>the path to the user data directory</value>
  113. public static string UserDataPath => Path.Combine(InstallPath, "UserData");
  114. private static bool CheckIsSteam()
  115. {
  116. var installDirInfo = new DirectoryInfo(InstallPath);
  117. return installDirInfo.Parent?.Name == "common"
  118. && installDirInfo.Parent?.Parent?.Name == "steamapps";
  119. }
  120. }
  121. }