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.

144 lines
5.5 KiB

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