forked from SteffanDonal/BeatSaber-SaberTailor
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Plugin.cs
120 lines (102 loc) · 3.44 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using IPA;
using IPA.Config;
using IPA.Loader;
using SaberTailor.HarmonyPatches;
using SaberTailor.Settings;
using SaberTailor.Settings.UI;
using SaberTailor.Tweaks;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using IPALogger = IPA.Logging.Logger;
namespace SaberTailor
{
public static class BuildInfo
{
public const string Name = "SaberTailor";
public const string Version = "3.5.0";
}
[Plugin(RuntimeOptions.DynamicInit)]
public class Plugin
{
public static string PluginName => BuildInfo.Name;
public static Hive.Versioning.Version PluginVersion { get; private set; } = new Hive.Versioning.Version("0.0.0"); // Default
public static bool IsBSMLAvailable => CheckPluginAvailable("BeatSaberMarkupLanguage", new Hive.Versioning.Version("1.5.0"));
public static bool IsBSUtilsAvailable => CheckPluginAvailable("BS Utils", new Hive.Versioning.Version("1.10.0"));
[Init]
public void Init(IPALogger logger, PluginMetadata metadata)
{
Logger.log = logger;
Configuration.Init();
if (metadata?.HVersion != null)
{
PluginVersion = metadata.HVersion;
}
}
[OnEnable]
public void OnEnable() => Load();
[OnDisable]
public void OnDisable() => Unload();
public void OnActiveSceneChanged(Scene prevScene, Scene nextScene)
{
if (nextScene.name == "GameCore")
{
if (Configuration.Scale.TweakEnabled)
{
new GameObject(PluginName).AddComponent<SaberLength>();
}
}
}
private void Load()
{
Configuration.Load();
Settings.Utilities.ProfileManager.LoadProfiles();
AddEvents();
if (IsBSMLAvailable)
{
AddMenu();
}
else
{
Logger.log.Debug("BSML is missing. Skipping setting up BSML settings menu...");
}
Logger.log.Info($"{PluginName} v.{PluginVersion} has started.");
}
private void Unload()
{
SaberTailorPatches.RemoveHarmonyPatches();
Configuration.Save();
RemoveEvents();
if (IsBSMLAvailable)
{
RemoveMenu();
}
}
private void AddEvents()
{
RemoveEvents();
SceneManager.activeSceneChanged += OnActiveSceneChanged;
}
private void RemoveEvents()
{
SceneManager.activeSceneChanged -= OnActiveSceneChanged;
}
private void AddMenu()
{
BeatSaberMarkupLanguage.Settings.BSMLSettings.instance.AddSettingsMenu("SaberTailor", "SaberTailor.Settings.UI.Views.mainsettings.bsml", MainSettings.instance);
}
private void RemoveMenu()
{
BeatSaberMarkupLanguage.Settings.BSMLSettings.instance.RemoveSettingsMenu(MainSettings.instance);
}
private static bool CheckPluginAvailable(string pluginName, Hive.Versioning.Version minVersion)
{
PluginMetadata pluginMetadata = IPA.Loader.PluginManager.GetPluginFromId(pluginName);
if (pluginMetadata == null)
{
return false;
}
return pluginMetadata.HVersion >= minVersion;
}
}
}