-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvincibilityMonitorPlugin.cs
More file actions
182 lines (149 loc) · 5.9 KB
/
InvincibilityMonitorPlugin.cs
File metadata and controls
182 lines (149 loc) · 5.9 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using MonoDetour;
using Silksong.InvincibilityMonitor.Conditions;
using Silksong.ModMenu.Elements;
using Silksong.ModMenu.Models;
using Silksong.ModMenu.Plugin;
using Silksong.ModMenu.Screens;
using UnityEngine;
namespace Silksong.InvincibilityMonitor;
[BepInDependency("org.silksong-modding.fsmutil")]
[BepInDependency("org.silksong-modding.prepatcher")]
[BepInAutoPlugin(id: "io.github.invincibilitymonitor")]
public partial class InvincibilityMonitorPlugin : BaseUnityPlugin, IModMenuCustomMenu
{
private ConfigEntry<bool>? pluginEnabledConfig;
internal bool PluginEnabled => pluginEnabledConfig?.Value ?? false;
internal event Action<bool>? OnPluginEnabledChanged;
private ConfigEntry<float>? gracePeriodConfig;
private readonly List<string> conditionNames = [];
private readonly List<bool> activeConditions = [];
private int numActiveConditions = 0;
private void Awake()
{
MonoDetourManager.InvokeHookInitializers(System.Reflection.Assembly.GetExecutingAssembly());
pluginEnabledConfig = Config.Bind(
configDefinition: new("Main", "Enabled"),
false,
configDescription: new("Whether to apply any invincibility rules at all.")
);
pluginEnabledConfig.SettingChanged += (_, args) =>
{
SettingChangedEventArgs typedArgs = (SettingChangedEventArgs)args;
OnPluginEnabledChanged?.Invoke((bool)typedArgs.ChangedSetting.BoxedValue);
};
gracePeriodConfig = Config.Bind(
configDefinition: new("Main", "Grace Period"),
0.2f,
new(
"Time (seconds) for invincibility to wear off.",
tags: [(ConfigEntryFactory.MenuElementGenerator)CreateGracePeriodElement]
)
);
foreach (var condition in InvincibilityCondition.CreateAllConditions(this))
{
int index = activeConditions.Count;
activeConditions.Add(false);
conditionNames.Add(condition.Key);
void OnChange(bool value)
{
bool prev = activeConditions[index];
if (prev == value)
return;
activeConditions[index] = value;
if (value)
++numActiveConditions;
else
--numActiveConditions;
}
OnChange(condition.IsEnabledAndActive);
condition.OnEnabledAndActiveChanged += OnChange;
}
if (Chainloader.PluginInfos.ContainsKey("io.github.hk-speedrunning.debugmod"))
HookDebugMod();
Logger.LogInfo($"Plugin {Name} ({Id}) has loaded!");
}
public LocalizedText ModMenuName() => "Invincibility Monitor";
public AbstractMenuScreen BuildCustomMenu()
{
SimpleMenuScreen screen = new("Invincibility Monitor");
PaginatedMenuScreenBuilder conditionsBuilder = new("Invincibility Conditions");
ConfigEntryFactory factory = new();
foreach (var (configDefinition, configBase) in Config)
{
if (!factory.GenerateMenuElement(configBase, out var menuElement))
continue;
if (configDefinition.Section == InvincibilityCondition.SECTION)
conditionsBuilder.Add(menuElement);
else
screen.Add(menuElement);
}
TextButton subMenu = new("Conditions");
var conditions = conditionsBuilder.Build();
subMenu.OnSubmit += () => MenuScreenNavigation.Show(conditions);
screen.Add(subMenu);
return screen;
}
private static bool CreateGracePeriodElement(ConfigEntryBase entry, out MenuElement menuElement)
{
ListSliderModel<float> model = new([
0,
0.1f,
0.2f,
0.3f,
0.4f,
0.5f,
0.6f,
0.7f,
0.8f,
0.9f,
1.0f,
])
{
DisplayFn = (idx, _) => $"{(idx / 10.0f):0.0}",
};
SliderElement<float> sliderElement = new("Grace Period", model);
sliderElement.SynchronizeRawWith(entry);
menuElement = sliderElement;
return true;
}
private void HookDebugMod() =>
DebugMod.DebugMod.AddTextToInfoPanel(
"Invincible",
() => IsCurrentlyInvincible ? "Yes" : "No"
);
private void OnEnable() =>
PrepatcherPlugin.PlayerDataVariableEvents<bool>.OnGetVariable += OverrideIsInvincible;
private void OnDisable()
{
invincibilityCooldown = 0f;
PrepatcherPlugin.PlayerDataVariableEvents<bool>.OnGetVariable -= OverrideIsInvincible;
}
private bool HasInvincibilityCondition => PluginEnabled && numActiveConditions > 0;
private bool IsCurrentlyInvincible => HasInvincibilityCondition || invincibilityCooldown > 0f;
private float invincibilityCooldown = 0f; // Cooldown before invincibility goes away.
private void UpdateInvincibility()
{
if (HasInvincibilityCondition)
invincibilityCooldown = gracePeriodConfig?.Value ?? 0;
else if (invincibilityCooldown > 0)
{
invincibilityCooldown -= Time.deltaTime;
if (invincibilityCooldown < 0)
invincibilityCooldown = 0;
}
}
internal event Action? OnUpdate;
private void InvokeCallbacks() => OnUpdate?.Invoke();
private void Update()
{
UpdateInvincibility();
InvokeCallbacks();
}
private bool OverrideIsInvincible(PlayerData playerData, string name, bool orig) =>
orig || (name == nameof(PlayerData.isInvincible) && IsCurrentlyInvincible);
}