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.

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