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.

160 lines
6.0 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
  29. {
  30. get
  31. {
  32. try
  33. {
  34. return Application.version;
  35. }
  36. catch(MissingMemberException ex)
  37. {
  38. Logging.Logger.log.Error($"Tried to grab 'Application.version' too early, it's probably broken now.");
  39. Logging.Logger.log.Debug(ex);
  40. }
  41. catch (Exception ex)
  42. {
  43. Logging.Logger.log.Error($"Error getting Application.version: {ex.Message}");
  44. Logging.Logger.log.Debug(ex);
  45. }
  46. return string.Empty;
  47. }
  48. }
  49. internal static void EnsureRuntimeGameVersion()
  50. {
  51. try
  52. {
  53. var rtVer = new AlmostVersion(ApplicationVersionProxy);
  54. if (!rtVer.Equals(_gameVersion)) // this actually uses stricter equality than == for AlmostVersion
  55. {
  56. Logging.Logger.log.Warn($"Early version {_gameVersion} parsed from game files doesn't match runtime version {rtVer}!");
  57. _gameVersion = rtVer;
  58. }
  59. }
  60. catch (MissingMethodException e)
  61. {
  62. Logging.Logger.log.Error("Application.version was not found! Cannot check early parsed version");
  63. if (SelfConfig.Debug_.ShowHandledErrorStackTraces_)
  64. Logging.Logger.log.Error(e);
  65. }
  66. }
  67. internal static bool IsGameVersionBoundary { get; private set; }
  68. internal static AlmostVersion OldVersion { get; private set; }
  69. internal static void CheckGameVersionBoundary()
  70. {
  71. var gameVer = GameVersion;
  72. var lastVerS = SelfConfig.LastGameVersion_;
  73. OldVersion = lastVerS != null ? new AlmostVersion(lastVerS, gameVer) : null;
  74. IsGameVersionBoundary = OldVersion != null && gameVer != OldVersion;
  75. SelfConfig.Instance.LastGameVersion = gameVer.ToString();
  76. }
  77. private static Thread mainThread;
  78. /// <summary>
  79. /// Checks if the currently running code is running on the Unity main thread.
  80. /// </summary>
  81. /// <value><see langword="true"/> if the curent thread is the Unity main thread, <see langword="false"/> otherwise</value>
  82. public static bool OnMainThread => Thread.CurrentThread.ManagedThreadId == mainThread?.ManagedThreadId;
  83. internal static void SetMainThread()
  84. => mainThread = Thread.CurrentThread;
  85. /// <summary>
  86. /// The different types of releases of the game.
  87. /// </summary>
  88. public enum Release
  89. {
  90. /// <summary>
  91. /// Indicates a Steam release.
  92. /// </summary>
  93. Steam,
  94. /// <summary>
  95. /// Indicates a non-Steam release.
  96. /// </summary>
  97. Other
  98. }
  99. private static Release? _releaseCache;
  100. /// <summary>
  101. /// Gets the <see cref="Release"/> type of this installation of Beat Saber
  102. /// </summary>
  103. /// <remarks>
  104. /// This only gives a
  105. /// </remarks>
  106. /// <value>the type of release this is</value>
  107. public static Release ReleaseType => (_releaseCache ?? (_releaseCache = CheckIsSteam() ? Release.Steam : Release.Other)).Value;
  108. private static string _installRoot;
  109. /// <summary>
  110. /// Gets the path to the game's install directory.
  111. /// </summary>
  112. /// <value>the path of the game install directory</value>
  113. public static string InstallPath
  114. {
  115. get
  116. {
  117. if (_installRoot == null)
  118. _installRoot = Path.GetFullPath(
  119. Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", ".."));
  120. return _installRoot;
  121. }
  122. }
  123. /// <summary>
  124. /// The path to the `Libs` folder. Use only if necessary.
  125. /// </summary>
  126. /// <value>the path to the library directory</value>
  127. public static string LibraryPath => Path.Combine(InstallPath, "Libs");
  128. /// <summary>
  129. /// The path to the `Libs\Native` folder. Use only if necessary.
  130. /// </summary>
  131. /// <value>the path to the native library directory</value>
  132. public static string NativeLibraryPath => Path.Combine(LibraryPath, "Native");
  133. /// <summary>
  134. /// The directory to load plugins from.
  135. /// </summary>
  136. /// <value>the path to the plugin directory</value>
  137. public static string PluginsPath => Path.Combine(InstallPath, "Plugins");
  138. /// <summary>
  139. /// The path to the `UserData` folder.
  140. /// </summary>
  141. /// <value>the path to the user data directory</value>
  142. public static string UserDataPath => Path.Combine(InstallPath, "UserData");
  143. private static bool CheckIsSteam()
  144. {
  145. var installDirInfo = new DirectoryInfo(InstallPath);
  146. return installDirInfo.Parent?.Name == "common"
  147. && installDirInfo.Parent?.Parent?.Name == "steamapps";
  148. }
  149. }
  150. }