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.

140 lines
5.4 KiB

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