From c79987aae273b0473afa357cd5b447dcd0ecb1ab Mon Sep 17 00:00:00 2001 From: Zingabopp Date: Sat, 4 Jan 2020 03:21:23 -0600 Subject: [PATCH] Updated for Beat Saber 1.6.0 --- BailOutMode/BailOutController.cs | 70 ++-- BailOutMode/BailOutMode.csproj | 98 +++++- BailOutMode/Config.cs | 275 ++++++++++++++++ .../GameEnergyCounterAddEnergy.cs | 19 +- .../Harmony_Patches/HandleSongDidFinish.cs | 23 +- BailOutMode/Logger.cs | 99 +----- BailOutMode/Plugin.cs | 298 ++---------------- BailOutMode/Properties/AssemblyInfo.cs | 2 +- BailOutMode/UI/BailOutModeUI.cs | 65 ---- BailOutMode/UI/Settings.bsml | 9 + BailOutMode/manifest.json | 10 +- 11 files changed, 455 insertions(+), 513 deletions(-) create mode 100644 BailOutMode/Config.cs delete mode 100644 BailOutMode/UI/BailOutModeUI.cs create mode 100644 BailOutMode/UI/Settings.bsml diff --git a/BailOutMode/BailOutController.cs b/BailOutMode/BailOutController.cs index 554f7f3..a278a2f 100644 --- a/BailOutMode/BailOutController.cs +++ b/BailOutMode/BailOutController.cs @@ -11,7 +11,7 @@ namespace BailOutMode class BailOutController : MonoBehaviour { #region "Fields with get/setters" - private static BailOutController _instance; + public static BailOutController instance { get; private set; } private LevelFailedTextEffect _levelFailedEffect; private StandardLevelGameplayManager _gameManager; private GameEnergyCounter _energyCounter; @@ -20,13 +20,13 @@ class BailOutController : MonoBehaviour #endregion #region "Fields" public bool isHiding = false; - public static int numFails = 0; + public int numFails = 0; public bool IsEnabled { get { - return Plugin.IsEnabled && (!isCampaign); + return Config.instance.IsEnabled && (!isCampaign); } } @@ -79,7 +79,7 @@ private static PlayerSpecificSettings PlayerSettings { _playerSettings = BS_Utils.Plugin.LevelData?.GameplayCoreSceneSetupData?.playerSpecificSettings; if (_playerSettings == null) - Logger.Warning($"Unable to find PlayerSettings"); + Logger.log.Warn($"Unable to find PlayerSettings"); } return _playerSettings; } @@ -95,26 +95,15 @@ private static float PlayerHeight } else { - Logger.Warning("Unable to find PlayerSettings, using 1.8 for player height"); + Logger.log.Warn("Unable to find PlayerSettings, using 1.8 for player height"); return 1.8f; } } } - public static BailOutController Instance - { - get - { - if (_instance == null) - { - Logger.Debug("BailOutController instance is null, creating new one"); - _instance = new GameObject("BailOutController").AddComponent(); - } - return _instance; - } - } + public static bool InstanceExists { - get { return _instance != null; } + get { return instance != null; } } #endregion @@ -122,7 +111,7 @@ public static bool InstanceExists public void Awake() { //Logger.Trace("BailOutController Awake()"); - _instance = this; + instance = this; isHiding = false; } @@ -142,7 +131,7 @@ private IEnumerator Initialize() //Logger.Trace("Checking for Campaign mode"); if (GameObject.FindObjectsOfType().Count() > 0) { - Logger.Info("Campaign mode detected, BailOutMode unavailable."); + Logger.log.Info("Campaign mode detected, BailOutMode unavailable."); isCampaign = true; } else @@ -151,7 +140,7 @@ private IEnumerator Initialize() } if ((GameManager != null) && (EnergyCounter != null) && IsEnabled) { - Logger.Info("BailOutMode enabled"); + Logger.log.Info("BailOutMode enabled"); //Logger.Trace("Removing HandleGameEnergyDidReach0"); EnergyCounter.gameEnergyDidReach0Event -= GameManager.HandleGameEnergyDidReach0; } @@ -159,7 +148,8 @@ private IEnumerator Initialize() private void OnDestroy() { - Logger.Debug("Destroying BailOutController"); + Logger.log.Debug("Destroying BailOutController"); + instance = null; } public void ShowLevelFailed() @@ -167,47 +157,48 @@ public void ShowLevelFailed() //Logger.Trace("BailOutController ShowLevelFailed()"); //BS_Utils.Gameplay.ScoreSubmission.DisableSubmission(Plugin.PluginName); Don't need this here UpdateFailText($"Bailed Out {numFails} time{(numFails != 1 ? "s" : "")}"); - if (!isHiding && Plugin.ShowFailEffect) + if (!isHiding && Config.instance.ShowFailEffect) { try { - if (!Plugin.RepeatFailEffect && numFails > 1) + if (!Config.instance.RepeatFailEffect && numFails > 1) return; // Don't want to repeatedly show fail effect, stop here. //Logger.Debug("Showing fail effect"); LevelFailedEffect.ShowEffect(); - if (Plugin.FailEffectDuration > 0) + if (Config.instance.FailEffectDuration > 0) StartCoroutine(hideLevelFailed()); else isHiding = true; // Fail text never hides, so don't try to keep showing it } catch (Exception ex) { - Logger.Exception("Exception trying to show the fail Effect", ex); + Logger.log.Error($"Exception trying to show the fail Effect: {ex.Message}"); + Logger.log.Debug(ex); } } } - + private WaitForSeconds failDurationWait = new WaitForSeconds(Config.instance.FailEffectDuration); public IEnumerator hideLevelFailed() { - Logger.Trace("BailOutController hideLevelFailed() CoRoutine"); + Logger.log.Trace("BailOutController hideLevelFailed() CoRoutine"); if (!isHiding) { - Logger.Trace($"BailOutController, will hide LevelFailedEffect after {Plugin.FailEffectDuration}s"); + Logger.log.Trace($"BailOutController, will hide LevelFailedEffect after {Config.instance.FailEffectDuration}s"); isHiding = true; - yield return new WaitForSeconds(Plugin.FailEffectDuration); - Logger.Trace($"BailOutController, hiding LevelFailedEffect"); + yield return failDurationWait; + Logger.log.Trace($"BailOutController, hiding LevelFailedEffect"); LevelFailedEffect.gameObject.SetActive(false); isHiding = false; } else - Logger.Trace("BailOutController, skipping hideLevel because isHiding is true"); + Logger.log.Trace("BailOutController, skipping hideLevel because isHiding is true"); yield break; } public static void FacePosition(Transform obj, Vector3 targetPos) { - var rotAngle = Quaternion.LookRotation(StringToVector3(Plugin.CounterTextPosition) - targetPos); + var rotAngle = Quaternion.LookRotation(StringToVector3(Config.instance.CounterTextPosition) - targetPos); obj.rotation = rotAngle; } @@ -233,11 +224,11 @@ public TextMeshProUGUI CreateText(Canvas parent, string text, Vector2 anchoredPo var font = Instantiate(Resources.FindObjectsOfTypeAll().First(t => t.name == "Teko-Medium SDF No Glow")); if (font == null) { - Logger.Error("Could not locate font asset, unable to display text"); + Logger.log.Error("Could not locate font asset, unable to display text"); return null; } textMesh.font = font; - textMesh.fontSize = Plugin.CounterTextSize; + textMesh.fontSize = Config.instance.CounterTextSize; textMesh.rectTransform.SetParent(parent.transform as RectTransform, false); textMesh.text = text; textMesh.color = Color.white; @@ -254,7 +245,7 @@ public TextMeshProUGUI CreateText(Canvas parent, string text, Vector2 anchoredPo public static void CenterTextMesh(TextMeshProUGUI text) { text.ForceMeshUpdate(); - var pos = StringToVector3(Plugin.CounterTextPosition); + var pos = StringToVector3(Config.instance.CounterTextPosition); pos.x = pos.x - (text.renderedWidth * text.gameObject.transform.localScale.x) / 2; pos.y = pos.y + (text.renderedHeight * text.gameObject.transform.localScale.y); FacePosition(text.gameObject.transform, new Vector3(0, PlayerHeight, 0)); @@ -265,8 +256,8 @@ public static void CenterTextMesh(TextMeshProUGUI text) public void UpdateFailText(string text) { FailText.text = text; - if (Plugin.DynamicSettings) - FailText.fontSize = Plugin.CounterTextSize; + if (Config.instance.DynamicSettings) + FailText.fontSize = Config.instance.CounterTextSize; CenterTextMesh(FailText); } @@ -284,7 +275,8 @@ public static Vector3 StringToVector3(string vStr) } catch (Exception ex) { - Logger.Exception($"Cannot convert value of {vStr} to a Vector. Needs to be in the format #,#,#", ex); + Logger.log.Error($"Cannot convert value of {vStr} to a Vector. Needs to be in the format #,#,#: {ex.Message}"); + Logger.log.Debug(ex); return new Vector3(DefaultSettings.CounterTextPosition.x, DefaultSettings.CounterTextPosition.y, DefaultSettings.CounterTextPosition.z); } } diff --git a/BailOutMode/BailOutMode.csproj b/BailOutMode/BailOutMode.csproj index 6b7977a..de355ae 100644 --- a/BailOutMode/BailOutMode.csproj +++ b/BailOutMode/BailOutMode.csproj @@ -9,6 +9,7 @@ Properties BailOutMode BailOutMode + 1.4.0 v4.7.2 512 @@ -33,24 +34,34 @@ false - - .\References\Managed\0Harmony.dll - - - .\References\Managed\Assembly-CSharp.dll + + False + $(BeatSaberDir)\Libs\0Harmony.1.2.0.1.dll False References\Plugins\BeatSaberCustomUI.dll + + False + $(BeatSaberDir)\Plugins\BSML.dll + References\Plugins\BS_Utils.dll + + False + $(BeatSaberDir)\Beat Saber_Data\Managed\HMLib.dll + + + False + $(BeatSaberDir)\Beat Saber_Data\Managed\HMUI.dll + References\Managed\IllusionInjector.dll - References\Managed\IllusionPlugin.dll + IllusionPlugin.dll References\Managed\INIFileParser.dll @@ -61,6 +72,10 @@ References\Managed\IPA.Loader.dll + + False + $(BeatSaberDir)\Beat Saber_Data\Managed\MainAssembly.dll + @@ -94,8 +109,13 @@ .\References\Managed\UnityEngine.VRModule.dll False + + False + $(BeatSaberDir)\Beat Saber_Data\Managed\Zenject.dll + + @@ -103,15 +123,77 @@ - + + + + + + + + + + + + + + + $(FullCommitHash.Substring(0, 8)) + + + + + + + + + + + + + + + + + + + - copy /Y "$(TargetFileName)" "$(ProjectDir)\References\Plugins" + + \ No newline at end of file diff --git a/BailOutMode/Config.cs b/BailOutMode/Config.cs new file mode 100644 index 0000000..3f787f4 --- /dev/null +++ b/BailOutMode/Config.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BeatSaberMarkupLanguage; +using BeatSaberMarkupLanguage.Attributes; + +namespace BailOutMode +{ + public struct DefaultSettings + { + public const bool IsEnabled = true; + public const bool ShowFailEffect = true; + public const bool RepeatFailEffect = false; + public const bool DynamicSettings = false; + public const int EnergyResetAmount = 50; + public const float CounterTextSize = 15f; + public const int FailEffectDuration = 3; + + public struct CounterTextPosition + { + public const float x = 0; + public const float y = 0; + public const float z = 2f; + public static string AsString() + { + return $"{x},{y},{z}"; + } + } + + } + public class Config + { + private static Config _instance; + internal static Config instance + { + get + { + if (_instance == null) + _instance = new Config(); + return _instance; + } + } + internal BS_Utils.Utilities.Config config; + + private bool _isEnabled = DefaultSettings.IsEnabled; + private bool _showFailEffect = DefaultSettings.ShowFailEffect; + private bool _repeatFailEffect = DefaultSettings.RepeatFailEffect; + private bool _dynamicSettings = DefaultSettings.DynamicSettings; + private float _counterTextSize = DefaultSettings.CounterTextSize; + private int _failEffectDuration = DefaultSettings.FailEffectDuration; + private int _energyReset = DefaultSettings.EnergyResetAmount; + private string _counterPosition = DefaultSettings.CounterTextPosition.AsString(); + + private const string KeyBailOutMode = "BailOutModeEnabled"; + private const string KeyShowFailEffect = "ShowFailEffect"; + private const string KeyRepeatFailEffect = "RepeatFailEffect"; + private const string KeyFailEffectDuration = "FailEffectDuration"; + private const string KeyEnergyResetAmount = "EnergyResetAmount"; + private const string KeyCounterTextPosition = "FailCounterPosition"; + private const string KeyCounterTextSize = "FailCounterTextSize"; + private const string KeyDynamicSettings = "DynamicSettings"; + + public const int nrgResetMin = 30; + public const int nrgResetMax = 100; + + [UIValue("IsEnabled")] + public bool IsEnabled + { + get + { + return _isEnabled; + } + set + { + config.SetBool(Plugin.PluginName, KeyBailOutMode, value); + _isEnabled = value; + } + + } + + [UIValue("ShowFailEffect")] + public bool ShowFailEffect + { + get + { + return _showFailEffect; + } + set + { + config.SetBool(Plugin.PluginName, KeyShowFailEffect, value); + _showFailEffect = value; + } + + } + + [UIValue("RepeatFailEffect")] + public bool RepeatFailEffect + { + get + { + return _repeatFailEffect; + } + set + { + config.SetBool(Plugin.PluginName, KeyRepeatFailEffect, value); + _repeatFailEffect = value; + } + + } + + [UIValue("FailEffectDuration")] + public int FailEffectDuration + { + get + { + return _failEffectDuration; + } + set + { + config.SetInt(Plugin.PluginName, KeyFailEffectDuration, value); + _failEffectDuration = value; + } + } + + [UIValue("EnergyResetAmount")] + public int EnergyResetAmount + { + get { return _energyReset; } + set + { + if ((value >= nrgResetMin)) + { + if (value <= nrgResetMax) + _energyReset = value; + else + _energyReset = nrgResetMax; + + } + config.SetInt(Plugin.PluginName, KeyEnergyResetAmount, _energyReset); + } + + } + + [UIValue("CounterTextPosition")] + public string CounterTextPosition + { + get + { + if (DynamicSettings) + { + _counterPosition = config.GetString(Plugin.PluginName, KeyCounterTextPosition, DefaultSettings.CounterTextPosition.AsString()); + } + return _counterPosition; + } + set + { + if (_counterPosition == value) + return; + _counterPosition = value; + config.SetString(Plugin.PluginName, KeyCounterTextPosition, value); + } + } + + [UIValue("CounterTextSize")] + public float CounterTextSize + { + get + { + if (DynamicSettings) + { + float val = config.GetFloat(Plugin.PluginName, KeyCounterTextSize, DefaultSettings.CounterTextSize); + if (val <= 0) + val = DefaultSettings.CounterTextSize; + _counterTextSize = val; + } + return _counterTextSize; + } + set + { + if (_counterTextSize == value) + return; + var oldValue = _counterTextSize; + if (value > 0) + _counterTextSize = value; + else + { + Logger.log.Error($"Invalid {KeyCounterTextSize}: {value}, must be > 0."); + _counterTextSize = DefaultSettings.CounterTextSize; + } + if (_counterTextSize == oldValue) + return; + config.SetFloat(Plugin.PluginName, KeyCounterTextSize, _counterTextSize); + } + } + + [UIValue("DynamicSettings")] + public bool DynamicSettings + { + get { return _dynamicSettings; } + set { _dynamicSettings = value; } + } + + internal void CheckForUserDataFolder() + { + /* + string userDataPath = Environment.CurrentDirectory + "/UserData"; + if (!Directory.Exists(userDataPath)) + { + Directory.CreateDirectory(userDataPath); + } + */ + if ("".Equals(config.GetString(Plugin.PluginName, KeyBailOutMode, ""))) + { + config.SetBool(Plugin.PluginName, KeyBailOutMode, DefaultSettings.IsEnabled); + } + else + IsEnabled = config.GetBool(Plugin.PluginName, KeyBailOutMode, DefaultSettings.IsEnabled); + + if ("".Equals(config.GetString(Plugin.PluginName, KeyShowFailEffect, ""))) + { + config.SetBool(Plugin.PluginName, KeyShowFailEffect, DefaultSettings.ShowFailEffect); + } + else + ShowFailEffect = config.GetBool(Plugin.PluginName, KeyShowFailEffect, DefaultSettings.ShowFailEffect); + + if ("".Equals(config.GetString(Plugin.PluginName, KeyRepeatFailEffect, ""))) + { + config.SetBool(Plugin.PluginName, KeyRepeatFailEffect, DefaultSettings.RepeatFailEffect); + } + else + RepeatFailEffect = config.GetBool(Plugin.PluginName, KeyRepeatFailEffect, DefaultSettings.RepeatFailEffect); + + if ("".Equals(config.GetString(Plugin.PluginName, KeyFailEffectDuration, ""))) + { + config.SetInt(Plugin.PluginName, KeyFailEffectDuration, DefaultSettings.FailEffectDuration); + } + else + FailEffectDuration = config.GetInt(Plugin.PluginName, KeyFailEffectDuration, DefaultSettings.FailEffectDuration); + + if ("".Equals(config.GetString(Plugin.PluginName, KeyEnergyResetAmount, ""))) + { + config.SetInt(Plugin.PluginName, KeyEnergyResetAmount, DefaultSettings.EnergyResetAmount); + } + else + EnergyResetAmount = config.GetInt(Plugin.PluginName, KeyEnergyResetAmount, DefaultSettings.EnergyResetAmount); + + if ("".Equals(config.GetString(Plugin.PluginName, KeyCounterTextPosition, ""))) + { + config.SetString(Plugin.PluginName, KeyCounterTextPosition, DefaultSettings.CounterTextPosition.AsString()); + } + else + CounterTextPosition = config.GetString(Plugin.PluginName, KeyCounterTextPosition, DefaultSettings.CounterTextPosition.AsString()); + + if ("".Equals(config.GetString(Plugin.PluginName, KeyCounterTextSize, ""))) + { + config.SetFloat(Plugin.PluginName, KeyCounterTextSize, DefaultSettings.CounterTextSize); + } + else + CounterTextSize = config.GetFloat(Plugin.PluginName, KeyCounterTextSize, DefaultSettings.CounterTextSize); + + if ("".Equals(config.GetString(Plugin.PluginName, KeyDynamicSettings, ""))) + { + config.SetBool(Plugin.PluginName, KeyDynamicSettings, DefaultSettings.DynamicSettings); + } + else + DynamicSettings = config.GetBool(Plugin.PluginName, KeyDynamicSettings, DefaultSettings.DynamicSettings); + + Logger.log.Debug(string.Format("Settings:\n IsEnabled={0}\n ShowFailEffect={1}\n FailEffectDuration={2}\n EnergyResetAmount={3}\n CounterPosition={4}\n CounterTextSize={5}\n DynamicSettings={6}", + IsEnabled, ShowFailEffect, FailEffectDuration, EnergyResetAmount, CounterTextPosition, CounterTextSize, DynamicSettings)); + } + + } +} diff --git a/BailOutMode/Harmony_Patches/GameEnergyCounterAddEnergy.cs b/BailOutMode/Harmony_Patches/GameEnergyCounterAddEnergy.cs index c5b9238..8fd977a 100644 --- a/BailOutMode/Harmony_Patches/GameEnergyCounterAddEnergy.cs +++ b/BailOutMode/Harmony_Patches/GameEnergyCounterAddEnergy.cs @@ -20,8 +20,9 @@ static bool Prefix(GameEnergyCounter __instance, ref float value) { //Logger.Trace("In GameEnergyCounter.AddEnergy()"); bool enabled = false; - if (BailOutController.Instance != null) - enabled = BailOutController.Instance.IsEnabled; + if (BailOutController.instance == null) + return true; + enabled = BailOutController.instance.IsEnabled; if (enabled && value < 0f) { //Logger.Trace("Negative energy change detected: {0}", value); @@ -29,18 +30,18 @@ static bool Prefix(GameEnergyCounter __instance, ref float value) { //Logger.Debug("Fail detected. Current Energy: {0}, Energy Change: {1}", __instance.energy, value); if (BS_Utils.Gameplay.ScoreSubmission.Disabled == false - ||BailOutController.numFails == 0) + || BailOutController.instance.numFails == 0) { - Logger.Info("First fail detected, disabling score submission"); - if (BailOutController.numFails > 0) - Logger.Error($"Fail Counter is {BailOutController.numFails}, but score submission wasn't disabled. This should not happen"); + Logger.log.Info("First fail detected, disabling score submission"); + if (BailOutController.instance.numFails > 0) + Logger.log.Error($"Fail Counter is {BailOutController.instance.numFails}, but score submission wasn't disabled. This should not happen"); BS_Utils.Gameplay.ScoreSubmission.DisableSubmission(Plugin.PluginName); } - BailOutController.numFails++; + BailOutController.instance.numFails++; //Logger.Debug($"{__instance.energy} + {value} puts us <= 0"); - value = (Plugin.EnergyResetAmount / 100f) - __instance.energy; + value = (Config.instance.EnergyResetAmount / 100f) - __instance.energy; //Logger.Debug("Changing value to {0} to raise energy to {1}", value, Plugin.EnergyResetAmount); - BailOutController.Instance.ShowLevelFailed(); + BailOutController.instance.ShowLevelFailed(); } } return true; diff --git a/BailOutMode/Harmony_Patches/HandleSongDidFinish.cs b/BailOutMode/Harmony_Patches/HandleSongDidFinish.cs index 208131b..edd4b7d 100644 --- a/BailOutMode/Harmony_Patches/HandleSongDidFinish.cs +++ b/BailOutMode/Harmony_Patches/HandleSongDidFinish.cs @@ -15,26 +15,29 @@ namespace BailOutMode.Harmony_Patches new Type[] { })] class StandardLevelGameplayManagerHandleSongDidFinish { - static void Prefix(StandardLevelGameplayManager __instance, ref StandardLevelGameplayManager.GameState ____gameState, ref Signal ____levelFailedSignal) + static bool Prefix(StandardLevelGameplayManager __instance, ref StandardLevelGameplayManager.GameState ____gameState) { //Logger.Trace("In StandardLevelGameplayManager.HandleSongDidFinish()"); - try { + try + { bool enabled = false; - if (BailOutController.Instance != null) - enabled = BailOutController.Instance.IsEnabled; - if (enabled && BailOutController.numFails > 0) + if (BailOutController.instance == null) + return true; + enabled = BailOutController.instance.IsEnabled; + if (enabled && BailOutController.instance.numFails > 0) { - //Logger.Trace("Fail detected in BailOutController, setting state to failed"); - ____gameState = StandardLevelGameplayManager.GameState.Failed; - ____levelFailedSignal.Raise(); + Logger.log.Debug("Fail detected in BailOutController, setting state to failed"); + __instance.HandleGameEnergyDidReach0(); + return false; } } catch (Exception ex) { - Logger.Exception("Error in StandardLevelGameplayManagerHandleSongDidFinish", ex); + Logger.log.Error($"Error in StandardLevelGameplayManagerHandleSongDidFinish: {ex.Message}"); + Logger.log.Debug(ex); } - return; + return true; } } } diff --git a/BailOutMode/Logger.cs b/BailOutMode/Logger.cs index 4ddc78a..164b59b 100644 --- a/BailOutMode/Logger.cs +++ b/BailOutMode/Logger.cs @@ -6,103 +6,8 @@ namespace BailOutMode { - public enum LogLevel + internal static class Logger { - Trace, - Debug, - Info, - Warn, - Error - } - - public static class Logger - { - private static readonly string LoggerName = Plugin.PluginName; - public static LogLevel LogLevel = LogLevel.Info; - private static readonly ConsoleColor DefaultFgColor = ConsoleColor.Gray; - public static IPA.Logging.Logger ipaLogger; - /* IPA Log Levels by severity - * None, Debug, Info, Notice, Warning, Error, Critical - */ - - private static void ResetForegroundColor() - { - Console.ForegroundColor = DefaultFgColor; - } - - public static void Trace(string format, params object[] args) - { - if (LogLevel > LogLevel.Trace) - { - return; - } - /* - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Trace] " + String.Format(format, args)); - ResetForegroundColor(); - */ - IPA.Logging.StandardLogger.PrintFilter = IPA.Logging.Logger.LogLevel.All; - ipaLogger.Debug(String.Format(format, args)); - } - - public static void Debug(string format, params object[] args) - { - if (LogLevel > LogLevel.Debug) - { - IPA.Logging.StandardLogger.PrintFilter = IPA.Logging.Logger.LogLevel.InfoUp; - ipaLogger.Debug(String.Format(format, args)); - return; - } - - //Console.ForegroundColor = ConsoleColor.Magenta; - //Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Debug] " + String.Format(format, args)); - //ResetForegroundColor(); - IPA.Logging.StandardLogger.PrintFilter = IPA.Logging.Logger.LogLevel.All; - ipaLogger.Debug(String.Format(format, args)); - //IPA.Logging.StandardLogger.PrintFilter = prevLevel; - } - - public static void Info(string format, params object[] args) - { - if (LogLevel > LogLevel.Info) - { - return; - } - - //Console.ForegroundColor = ConsoleColor.Green; - //Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Info] " + String.Format(format, args)); - //ResetForegroundColor(); - ipaLogger.Info(String.Format(format, args)); - } - - public static void Warning(string format, params object[] args) - { - if (LogLevel > LogLevel.Warn) - { - return; - } - - //Console.ForegroundColor = ConsoleColor.Blue; - //Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Warning] " + String.Format(format, args)); - //ResetForegroundColor(); - ipaLogger.Warn(String.Format(format, args)); - } - - public static void Error(string format, params object[] args) - { - //Console.ForegroundColor = ConsoleColor.Yellow; - //Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Error] " + String.Format(format, args)); - //ResetForegroundColor(); - ipaLogger.Error(String.Format(format, args)); - } - - public static void Exception(string message, Exception e) - { - //Console.ForegroundColor = ConsoleColor.Red; - //Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + "] " + String.Format("{0}-{1}-{2}\n{3}", message, e.GetType().FullName, e.Message, e.StackTrace)); - //ResetForegroundColor(); - ipaLogger.Critical(message); - ipaLogger.Critical(e); - } + internal static IPA.Logging.Logger log; } } diff --git a/BailOutMode/Plugin.cs b/BailOutMode/Plugin.cs index e149b80..08462a8 100644 --- a/BailOutMode/Plugin.cs +++ b/BailOutMode/Plugin.cs @@ -14,55 +14,11 @@ namespace BailOutMode { - public struct DefaultSettings - { - public const bool IsEnabled = true; - public const bool ShowFailEffect = true; - public const bool RepeatFailEffect = false; - public const bool DynamicSettings = false; - public const int EnergyResetAmount = 50; - public const float CounterTextSize = 15f; - public const int FailEffectDuration = 3; - public struct CounterTextPosition - { - public const float x = 0; - public const float y = 0; - public const float z = 2f; - public static string AsString() - { - return $"{x},{y},{z}"; - } - } - - } public class Plugin : IBeatSaberPlugin { public static string PluginName = "BailOutMode"; - private static bool _isEnabled = DefaultSettings.IsEnabled; - private static bool _showFailEffect = DefaultSettings.ShowFailEffect; - private static bool _repeatFailEffect = DefaultSettings.RepeatFailEffect; - private static bool _dynamicSettings = DefaultSettings.DynamicSettings; - private static float _counterTextSize = DefaultSettings.CounterTextSize; - private static int _failEffectDuration = DefaultSettings.FailEffectDuration; - private static int _energyReset = DefaultSettings.EnergyResetAmount; - private static string _counterPosition = DefaultSettings.CounterTextPosition.AsString(); - public static BS_Utils.Utilities.Config config; - - private const string KeyBailOutMode = "BailOutModeEnabled"; - private const string KeyShowFailEffect = "ShowFailEffect"; - private const string KeyRepeatFailEffect = "RepeatFailEffect"; - private const string KeyFailEffectDuration = "FailEffectDuration"; - private const string KeyEnergyResetAmount = "EnergyResetAmount"; - private const string KeyCounterTextPosition = "FailCounterPosition"; - private const string KeyCounterTextSize = "FailCounterTextSize"; - private const string KeyDynamicSettings = "DynamicSettings"; - public const int nrgResetMin = 30; - public const int nrgResetMax = 100; - bool bsUtilsExists; - bool customUIExists; - private GameScenesManager _scenesManager; public GameScenesManager _gameScenesManager { @@ -78,31 +34,17 @@ public GameScenesManager _gameScenesManager public void Init(IPA.Logging.Logger logger) { - Logger.LogLevel = LogLevel.Info; - IPA.Logging.StandardLogger.PrintFilter = IPA.Logging.Logger.LogLevel.All; - Logger.ipaLogger = logger; + Logger.log = logger; } public void OnApplicationStart() { - customUIExists = IPA.Loader.PluginManager.AllPlugins.FirstOrDefault(c => c.Metadata.Name == "Custom UI") != null; - bsUtilsExists = IPA.Loader.PluginManager.AllPlugins.FirstOrDefault(c => c.Metadata.Name == "BS_Utils") != null; - - if (!bsUtilsExists) + if (Config.instance.config == null) { - Logger.Error($"Missing critical dependency: Beat Saber Utils, unable to start"); - return; + Config.instance.config = new BS_Utils.Utilities.Config("BailOutMode"); } - if (!customUIExists) - { - Logger.Warning($"Missing dependency: Beat Saber CustomUI, settings will not be available in game."); - } - if(config == null) - { - config = new BS_Utils.Utilities.Config("BailOutMode"); - } - CheckForUserDataFolder(); - + Config.instance.CheckForUserDataFolder(); + BS_Utils.Utilities.BSEvents.menuSceneLoadedFresh += MenuLoadedFresh; try { var harmony = HarmonyInstance.Create("com.github.zingabopp.bailoutmode"); @@ -110,14 +52,13 @@ public void OnApplicationStart() } catch (Exception ex) { - Logger.Exception("This plugin requires Harmony. Make sure you " + - "installed the plugin properly, as the Harmony DLL should have been installed with it.", ex); + Logger.log.Error($"Error applying Harmony patches: {ex.Message}"); + Logger.log.Debug(ex.ToString()); } } - - private void OnSceneTransitionFinish() + private void OnSceneTransitionFinish(ScenesTransitionSetupDataSO arg1, Zenject.DiContainer arg2) { //Logger.Debug("OnSceneTransitionFinished: Creating new BailOutController"); new GameObject("BailOutController").AddComponent(); @@ -131,23 +72,25 @@ public void OnApplicationQuit() public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) { - //Create GameplayOptions/SettingsUI if using either - if (customUIExists && scene.name == "MenuCore") - UI.BailOutModeUI.CreateUI(); + } + + public void MenuLoadedFresh() + { + BeatSaberMarkupLanguage.Settings.BSMLSettings.instance.AddSettingsMenu(PluginName, "BailOutMode.UI.Settings.bsml", Config.instance); } public void OnSceneUnloaded(Scene scene) { - + } public void OnActiveSceneChanged(Scene oldScene, Scene newScene) { - Logger.Trace($"In scene {newScene.name} from {oldScene.name}"); - if (BailOutController.InstanceExists) + Logger.log.Debug($"In scene {newScene.name} from {oldScene.name}"); + if (BailOutController.instance != null) { - GameObject.Destroy(BailOutController.Instance); - Logger.Debug("Found controller onActiveSceneChanged, destroyed it"); + GameObject.Destroy(BailOutController.instance); + Logger.log.Debug("Found controller onActiveSceneChanged, destroyed it"); } if (newScene.name == "MenuCore") { @@ -161,7 +104,6 @@ public void OnActiveSceneChanged(Scene oldScene, Scene newScene) { //Code to execute when entering actual gameplay _gameScenesManager.transitionDidFinishEvent += OnSceneTransitionFinish; - BailOutController.numFails = 0; } } @@ -189,210 +131,6 @@ public void OnFixedUpdate() { } #endregion - - public static bool IsEnabled - { - get - { - return _isEnabled; - } - set - { - config.SetBool(Plugin.PluginName, KeyBailOutMode, value); - _isEnabled = value; - } - - } - - public static bool ShowFailEffect - { - get - { - return _showFailEffect; - } - set - { - config.SetBool(Plugin.PluginName, KeyShowFailEffect, value); - _showFailEffect = value; - } - - } - - public static bool RepeatFailEffect - { - get - { - return _repeatFailEffect; - } - set - { - config.SetBool(Plugin.PluginName, KeyRepeatFailEffect, value); - _repeatFailEffect = value; - } - - } - - public static int FailEffectDuration - { - get - { - return _failEffectDuration; - } - set - { - config.SetInt(Plugin.PluginName, KeyFailEffectDuration, value); - _failEffectDuration = value; - } - } - - public static int EnergyResetAmount - { - get { return _energyReset; } - set - { - if ((value > nrgResetMin)) - { - if (value <= nrgResetMax) - _energyReset = GetMultipleOfTen(value); - else - _energyReset = nrgResetMax; - - } - config.SetInt(Plugin.PluginName, KeyEnergyResetAmount, _energyReset); - } - - } - - public static string CounterTextPosition - { - get - { - if(DynamicSettings) - { - _counterPosition = config.GetString(PluginName, KeyCounterTextPosition, DefaultSettings.CounterTextPosition.AsString()); - } - return _counterPosition; - } - set { _counterPosition = value; } - } - - public static float CounterTextSize - { - get - { - if (DynamicSettings) - { - float val = config.GetFloat(Plugin.PluginName, Plugin.KeyCounterTextSize, DefaultSettings.CounterTextSize); - if (val <= 0) - val = DefaultSettings.CounterTextSize; - _counterTextSize = val; - } - return _counterTextSize; - } - set - { - if(value > 0) - _counterTextSize = value; - else - { - Logger.Error($"Invalid {KeyCounterTextSize}: {value}, must be > 0."); - _counterTextSize = DefaultSettings.CounterTextSize; - } - } - } - - public static bool DynamicSettings - { - get { return _dynamicSettings; } - set { _dynamicSettings = value; } - } - - - - private static int GetMultipleOfTen(int value) - { - int remainder = value % 10; - if (remainder == 0) - return value; - else - { - if ((10 - remainder) > 5) - return value - remainder; - else - return value + (10 - remainder); - } - } - - private void CheckForUserDataFolder() - { - /* - string userDataPath = Environment.CurrentDirectory + "/UserData"; - if (!Directory.Exists(userDataPath)) - { - Directory.CreateDirectory(userDataPath); - } - */ - if ("".Equals(config.GetString(Plugin.PluginName, Plugin.KeyBailOutMode, ""))) - { - config.SetBool(Plugin.PluginName, Plugin.KeyBailOutMode, DefaultSettings.IsEnabled); - } - else - IsEnabled = config.GetBool(Plugin.PluginName, Plugin.KeyBailOutMode, DefaultSettings.IsEnabled); - - if ("".Equals(config.GetString(Plugin.PluginName, Plugin.KeyShowFailEffect, ""))) - { - config.SetBool(Plugin.PluginName, Plugin.KeyShowFailEffect, DefaultSettings.ShowFailEffect); - } - else - ShowFailEffect = config.GetBool(Plugin.PluginName, Plugin.KeyShowFailEffect, DefaultSettings.ShowFailEffect); - - if ("".Equals(config.GetString(Plugin.PluginName, Plugin.KeyRepeatFailEffect, ""))) - { - config.SetBool(Plugin.PluginName, Plugin.KeyRepeatFailEffect, DefaultSettings.RepeatFailEffect); - } - else - RepeatFailEffect = config.GetBool(Plugin.PluginName, Plugin.KeyRepeatFailEffect, DefaultSettings.RepeatFailEffect); - - if ("".Equals(config.GetString(Plugin.PluginName, Plugin.KeyFailEffectDuration, ""))) - { - config.SetInt(Plugin.PluginName, Plugin.KeyFailEffectDuration, DefaultSettings.FailEffectDuration); - } - else - FailEffectDuration = config.GetInt(Plugin.PluginName, Plugin.KeyFailEffectDuration, DefaultSettings.FailEffectDuration); - - if ("".Equals(config.GetString(Plugin.PluginName, Plugin.KeyEnergyResetAmount, ""))) - { - config.SetInt(Plugin.PluginName, Plugin.KeyEnergyResetAmount, DefaultSettings.EnergyResetAmount); - } - else - EnergyResetAmount = config.GetInt(Plugin.PluginName, Plugin.KeyEnergyResetAmount, DefaultSettings.EnergyResetAmount); - - if ("".Equals(config.GetString(Plugin.PluginName, Plugin.KeyCounterTextPosition, ""))) - { - config.SetString(Plugin.PluginName, Plugin.KeyCounterTextPosition, DefaultSettings.CounterTextPosition.AsString()); - } - else - CounterTextPosition = config.GetString(Plugin.PluginName, Plugin.KeyCounterTextPosition, DefaultSettings.CounterTextPosition.AsString()); - - if ("".Equals(config.GetString(Plugin.PluginName, Plugin.KeyCounterTextSize, ""))) - { - config.SetFloat(Plugin.PluginName, Plugin.KeyCounterTextSize, DefaultSettings.CounterTextSize); - } - else - CounterTextSize = config.GetFloat(Plugin.PluginName, Plugin.KeyCounterTextSize, DefaultSettings.CounterTextSize); - - if ("".Equals(config.GetString(Plugin.PluginName, Plugin.KeyDynamicSettings, ""))) - { - config.SetBool(Plugin.PluginName, Plugin.KeyDynamicSettings, DefaultSettings.DynamicSettings); - } - else - DynamicSettings = config.GetBool(Plugin.PluginName, Plugin.KeyDynamicSettings, DefaultSettings.DynamicSettings); - - Logger.Debug("Settings:\n IsEnabled={0}\n ShowFailEffect={1}\n FailEffectDuration={2}\n EnergyResetAmount={3}\n CounterPosition={4}\n CounterTextSize={5}\n DynamicSettings={6}", - IsEnabled, ShowFailEffect, FailEffectDuration, EnergyResetAmount, CounterTextPosition, CounterTextSize, DynamicSettings); - } - - } diff --git a/BailOutMode/Properties/AssemblyInfo.cs b/BailOutMode/Properties/AssemblyInfo.cs index e691d36..a38b28f 100644 --- a/BailOutMode/Properties/AssemblyInfo.cs +++ b/BailOutMode/Properties/AssemblyInfo.cs @@ -32,4 +32,4 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.3")] +[assembly: AssemblyVersion("1.5.0")] diff --git a/BailOutMode/UI/BailOutModeUI.cs b/BailOutMode/UI/BailOutModeUI.cs deleted file mode 100644 index 1e56c25..0000000 --- a/BailOutMode/UI/BailOutModeUI.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using CustomUI.GameplaySettings; -using CustomUI.Settings; - -namespace BailOutMode.UI -{ - class BailOutModeUI - { - public static void CreateUI() - { - CreateSettingsUI(); - } - - public static void CreateSettingsUI() - { - //This will create a menu tab in the settings menu for your plugin - var pluginSettingsSubmenu = SettingsUI.CreateSubMenu("BailOut Mode"); - Logger.Debug("Setting up UI:\n IsEnabled={0}\n ShowFailEffect={1}\n RepeatedFailEffect={2}\n FailEffectDuration={3}\n EnergyResetAmount={4}", - Plugin.IsEnabled, Plugin.ShowFailEffect, Plugin.RepeatFailEffect, Plugin.FailEffectDuration, Plugin.EnergyResetAmount); - var enableToggle = pluginSettingsSubmenu.AddBool("Enable", "Keep playing songs if you fail, won't post scores if you do"); - enableToggle.GetValue += delegate { return Plugin.IsEnabled; }; - enableToggle.SetValue += delegate (bool value) { - Plugin.IsEnabled = value; - }; - var failEffectToggle = pluginSettingsSubmenu.AddBool("Show Fail Effect", "Show the fail text effect when you would've failed"); - failEffectToggle.GetValue += delegate { return Plugin.ShowFailEffect; }; - failEffectToggle.SetValue += delegate (bool value) { - Plugin.ShowFailEffect = value; - }; - - int durationMax = (Plugin.FailEffectDuration <= 10) ? 10 : Plugin.FailEffectDuration; // If duration setting is more than 10, increase the max to match - var failEffectDurationOption = pluginSettingsSubmenu.AddInt("Fail Effect Duration", "How long the fail text effect lingers, 0 to show it forever", 0, durationMax, 1); - failEffectDurationOption.GetValue += delegate { return Plugin.FailEffectDuration; }; - failEffectDurationOption.SetValue += delegate (int value) { - Plugin.FailEffectDuration = value; - }; - - - var nrgResetAmountOption = pluginSettingsSubmenu.AddInt("Energy Reset Amount", "How much energy you get back after failing", Plugin.nrgResetMin, Plugin.nrgResetMax, 10); - nrgResetAmountOption.GetValue += delegate { return Plugin.EnergyResetAmount; }; - nrgResetAmountOption.SetValue += delegate (int value) { - Plugin.EnergyResetAmount = value; - }; - - - } - - public static void CreateGameplayOptionsUI() - { - - //Example submenu option - var pluginSubmenu = GameplaySettingsUI.CreateSubmenuOption(GameplaySettingsPanels.ModifiersLeft, "Plugin Name", "MainMenu", "pluginMenu1", "You can keep all your plugin's gameplay options nested within this one button"); - - //Example Toggle Option within a submenu - var exampleOption = GameplaySettingsUI.CreateToggleOption(GameplaySettingsPanels.ModifiersLeft, "Example Toggle", "pluginMenu1", "Put a toggle for a setting you want easily accessible in game here."); - exampleOption.GetValue = /* Fetch the initial value for the option here*/ false; - exampleOption.OnToggle += (value) => { /* You can execute whatever you want to occur when the value is toggled here, usually that would include updating wherever the value is pulled from */}; - exampleOption.AddConflict("Conflicting Option Name"); //You can add conflicts with other gameplay options settings here, preventing both from being active at the same time, including that of other mods - } - } -} diff --git a/BailOutMode/UI/Settings.bsml b/BailOutMode/UI/Settings.bsml new file mode 100644 index 0000000..cb64a98 --- /dev/null +++ b/BailOutMode/UI/Settings.bsml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/BailOutMode/manifest.json b/BailOutMode/manifest.json index 5fcd067..2e4d41d 100644 --- a/BailOutMode/manifest.json +++ b/BailOutMode/manifest.json @@ -3,11 +3,13 @@ "name": "BailOutMode", "id": "bailoutmode", "description": "Allows you to continue playing songs without posting a score if you fail, but allows you to post scores if you don't.", - "version": "1.3.3", - "gameVersion": "1.1.0", + "version": "1.5.0", + "gameVersion": "1.6.0", "dependsOn": { - "BSIPA": "^3.12.22", - "CustomUI": "^1.5.11" + "BSIPA": "^3.13.4", + "BS Utils": "^1.4.0", + "Ini Parser": "^2.5.5", + "BeatSaberMarkupLanguage": "^1.1.1" }, "author": "Zingabopp" } \ No newline at end of file