using IPA.Config;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using UnityEngine;
#if NET3
using Path = Net3_Proxy.Path;
#endif
namespace IPA.Utilities
{
///
/// Provides some basic utility methods and properties of Beat Saber
///
public static class UnityGame
{
private static AlmostVersion _gameVersion;
///
/// Provides the current game version.
///
/// the SemVer version of the game
public static AlmostVersion GameVersion => _gameVersion;// ?? (_gameVersion = new AlmostVersion(ApplicationVersionProxy));
internal static void SetEarlyGameVersion(AlmostVersion ver)
{
_gameVersion = ver;
Logging.Logger.log.Debug($"GameVersion set early to {ver}");
}
private static string ApplicationVersionProxy => Application.version;
internal static void EnsureRuntimeGameVersion()
{
try
{
var rtVer = new AlmostVersion(ApplicationVersionProxy);
if (!rtVer.Equals(_gameVersion)) // this actually uses stricter equality than == for AlmostVersion
{
Logging.Logger.log.Warn($"Early version {_gameVersion} parsed from game files doesn't match runtime version {rtVer}!");
_gameVersion = rtVer;
}
}
catch (MissingMethodException e)
{
Logging.Logger.log.Error("Application.version was not found! Cannot check early parsed version");
if (SelfConfig.Debug_.ShowHandledErrorStackTraces_)
Logging.Logger.log.Error(e);
var st = new StackTrace();
Logging.Logger.log.Notice($"{st}");
}
}
internal static bool IsGameVersionBoundary { get; private set; }
internal static AlmostVersion OldVersion { get; private set; }
internal static void CheckGameVersionBoundary()
{
var gameVer = GameVersion;
var lastVerS = SelfConfig.LastGameVersion_;
OldVersion = lastVerS != null ? new AlmostVersion(lastVerS, gameVer) : null;
IsGameVersionBoundary = OldVersion != null && gameVer != OldVersion;
SelfConfig.Instance.LastGameVersion = gameVer.ToString();
}
private static Thread mainThread;
///
/// Checks if the currently running code is running on the Unity main thread.
///
/// if the curent thread is the Unity main thread, otherwise
public static bool OnMainThread => Thread.CurrentThread.ManagedThreadId == mainThread?.ManagedThreadId;
internal static void SetMainThread()
=> mainThread = Thread.CurrentThread;
///
/// The different types of releases of the game.
///
public enum Release
{
///
/// Indicates a Steam release.
///
Steam,
///
/// Indicates a non-Steam release.
///
Other
}
private static Release? _releaseCache;
///
/// Gets the type of this installation of Beat Saber
///
///
/// This only gives a
///
/// the type of release this is
public static Release ReleaseType => (_releaseCache ?? (_releaseCache = CheckIsSteam() ? Release.Steam : Release.Other)).Value;
private static string _installRoot;
///
/// Gets the path to the game's install directory.
///
/// the path of the game install directory
public static string InstallPath
{
get
{
if (_installRoot == null)
_installRoot = Path.GetFullPath(
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", ".."));
return _installRoot;
}
}
///
/// The path to the `Libs` folder. Use only if necessary.
///
/// the path to the library directory
public static string LibraryPath => Path.Combine(InstallPath, "Libs");
///
/// The path to the `Libs\Native` folder. Use only if necessary.
///
/// the path to the native library directory
public static string NativeLibraryPath => Path.Combine(LibraryPath, "Native");
///
/// The directory to load plugins from.
///
/// the path to the plugin directory
public static string PluginsPath => Path.Combine(InstallPath, "Plugins");
///
/// The path to the `UserData` folder.
///
/// the path to the user data directory
public static string UserDataPath => Path.Combine(InstallPath, "UserData");
private static bool CheckIsSteam()
{
var installDirInfo = new DirectoryInfo(InstallPath);
return installDirInfo.Parent?.Name == "common"
&& installDirInfo.Parent?.Parent?.Name == "steamapps";
}
}
}