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.

168 lines
6.6 KiB

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