-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plugin.cs
95 lines (76 loc) · 2.57 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
using BeatSaberMarkupLanguage.Settings;
using HarmonyLib;
using IPA;
using IPA.Config;
using IPA.Config.Stores;
using System;
using System.Reflection;
using UnityEngine.SceneManagement;
using IPALogger = IPA.Logging.Logger;
namespace GottaGoFast {
[Plugin(RuntimeOptions.DynamicInit)]
public class Plugin {
internal static Plugin Instance { get; private set; }
internal static IPALogger Log { get; private set; }
public static Harmony harmony;
static class BsmlWrapper {
static readonly bool hasBsml = IPA.Loader.PluginManager.GetPluginFromId("BeatSaberMarkupLanguage") != null;
public static void SetupUI() {
void wrap() => BeatSaberMarkupLanguage.Util.MainMenuAwaiter.MainMenuInitializing += delegate {
EnableUI();
};
if(hasBsml)
wrap();
}
public static void EnableUI() {
void wrap() => BSMLSettings.Instance.AddSettingsMenu("Gotta Go Fast", "GottaGoFast.Views.settings.bsml", Configuration.PluginConfig.Instance);
if(hasBsml)
wrap();
}
public static void DisableUI() {
void wrap() => BSMLSettings.Instance.RemoveSettingsMenu(Configuration.PluginConfig.Instance);
if(hasBsml)
wrap();
}
}
[Init]
/// <summary>
/// Called when the plugin is first loaded by IPA (either when the game starts or when the plugin is enabled if it starts disabled).
/// [Init] methods that use a Constructor or called before regular methods like InitWithConfig.
/// Only use [Init] with one Constructor.
/// </summary>
public void Init(IPALogger logger, Config conf) {
Instance = this;
Log = logger;
Log.Info("Gotta Go Fast initialized.");
Configuration.PluginConfig.Instance = conf.Generated<Configuration.PluginConfig>();
harmony = new Harmony("Kinsi55.BeatSaber.GottaGoFast");
}
#region Disableable
[OnEnable]
public void OnEnable() {
SceneManager.activeSceneChanged += OnActiveSceneChanged;
harmony.PatchAll(Assembly.GetExecutingAssembly());
BsmlWrapper.SetupUI();
}
[OnDisable]
public void OnDisable() {
SceneManager.activeSceneChanged -= OnActiveSceneChanged;
harmony.UnpatchSelf();
BsmlWrapper.DisableUI();
}
#endregion
internal static Exception PatchFailed(MethodBase method, Exception ex) {
if(method != null && ex != null)
Plugin.Log.Warn(string.Format("Patching {0} {1} failed: {2}", method.ReflectedType, method.Name, ex));
return null;
}
public static Scene currentScene;
public void OnActiveSceneChanged(Scene oldScene, Scene newScene) {
#if DEBUG
Log.Warn($"{oldScene.name} -> {newScene.name}");
#endif
currentScene = newScene;
}
}
}