-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
243 lines (202 loc) · 6.76 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using RoR2;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;
using UnityEngine;
using Console = System.Console;
using Version = System.Version;
[assembly: AssemblyVersion(Local.Eclipse.CurseCatcher.Plugin.version)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
namespace Local.Eclipse.CurseCatcher;
[BepInPlugin(identifier, "CurseCatcher", version)]
class Plugin : BaseUnityPlugin
{
public const string identifier = "local.eclipse.cursecatcher", version = "0.3.2";
static bool artifact;
static ConfigEntry<bool> self, friendly, fall, interactable, hazard, log;
protected void Awake()
{
const string general = "General", other = "Other";
self = Config.Bind(
section: general,
key: "Self Damage",
defaultValue: false,
description: "Abilities that cost health to activate will curse the player"
+ " if this setting is enabled.");
friendly = Config.Bind(
section: general,
key: "Friendly Fire",
defaultValue: true,
description: "Effects that damage allies may apply curse when enabled.");
fall = Config.Bind(
section: general,
key: "Fall Damage",
defaultValue: true,
description: "Inflict curse upon taking sufficient fall damage.");
interactable = Config.Bind(
section: general,
key: "Interactable Cost",
defaultValue: true,
description: "Whether the debuff is triggered upon paying health costs.");
hazard = Config.Bind(
section: general,
key: "Neutral Hazards",
defaultValue: true,
description: "If neutral damage, such as stage hazards, can cause curse.");
artifact = Config.Bind(
section: other,
key: "Artifact Enabled",
defaultValue: false,
description: "Used to toggle the above functionality. Selectable in the"
+ " Eclipse lobby, but also applies the modifier to other game modes."
).Value;
log = Config.Bind(
section: other,
key: "Detailed Logging",
defaultValue: false,
description: "For troubleshooting purposes only.");
if ( artifact )
{
Harmony.CreateAndPatchAll(typeof(Artifact));
RoR2Application.onLoad += CheckVersion;
}
else RoR2Application.onLoad += ( ) =>
{
CheckVersion();
Harmony.CreateAndPatchAll(instance.GetType());
};
}
internal static IPatch instance;
internal interface IPatch { public object GetDamageType(DamageInfo info); }
void CheckVersion()
{
instance = Version.TryParse(RoR2Application.GetBuildId(), out Version build)
&& build < new Version(1, 3) ? new Hopoo() : new Gearbox();
}
static IEnumerable<CodeInstruction> InsertCodeInstruction(IEnumerable<CodeInstruction> IL)
{
MethodInfo getSelectedDifficulty = typeof(Run).GetProperty(
nameof(Run.selectedDifficulty)).GetMethod;
bool found = false;
CodeInstruction previousInstruction = null;
Console.WriteLine("Installing hook for 'Eclipse 8' curse...");
foreach ( CodeInstruction instruction in IL )
{
if ( found && instruction.Branches(out _) )
{
found = false;
if ( previousInstruction.LoadsConstant(DifficultyIndex.Eclipse8) )
{
Console.WriteLine("...instruction sequence found.");
yield return new CodeInstruction(OpCodes.Pop);
yield return new CodeInstruction(OpCodes.Ldarg_1);
yield return CodeInstruction.Call(
typeof(Plugin), nameof(Plugin.ApplyCurse));
yield return new CodeInstruction(OpCodes.Brfalse, instruction.operand);
continue;
}
}
else if ( instruction.Calls(getSelectedDifficulty) )
found = true;
previousInstruction = instruction;
yield return instruction;
}
}
public static bool ApplyCurse(DifficultyIndex difficulty, DamageInfo damageInfo)
{
if ( artifact is false && difficulty < DifficultyIndex.Eclipse8 )
return false;
bool curse = true;
if ( log.Value ) PrintInfo(damageInfo);
if ( damageInfo.attacker is null )
{
switch ( instance.GetDamageType(damageInfo) )
{
case DamageType.NonLethal:
case DamageType.NonLethal | DamageType.BypassArmor:
case (uint) DamageTypeExtended.SojournVehicleDamage:
curse = self.Value;
break;
case DamageType enumeration:
if ( enumeration.HasFlag(DamageType.FallDamage) )
curse = fall.Value;
break;
}
}
else if ( damageInfo.attacker )
{
TeamComponent team = damageInfo.attacker.GetComponent<TeamComponent>();
if ( team ) switch ( team.teamIndex )
{
case TeamIndex.Player:
curse = friendly.Value;
break;
case TeamIndex.Neutral:
curse = hazard.Value;
break;
}
else if ( damageInfo.attacker.GetComponent<IInteractable>() is object )
curse = interactable.Value;
}
if ( curse ) return true;
else
{
if ( log.Value ) Console.WriteLine("...preventing curse.");
return false;
}
}
static void PrintInfo(DamageInfo damage)
{
string attacker = getName(damage.attacker),
inflictor = getName(damage.inflictor);
static string getName(GameObject obj)
{
string name;
if ( obj is null ) name = "null";
else if ( obj == null ) name = "destroyed";
else if ( obj.name is null ) name = "unknown";
else name = obj.name;
int start = name.IndexOf('('), count = name.IndexOf(')') - start + 1;
if ( start >= 0 && count >= 0 )
name = name.Remove(start, count);
return name;
}
string info = "attacker: " + attacker;
if ( inflictor != attacker && damage.inflictor is not null )
info += " (" + inflictor + ")";
if ( damage.attacker != null )
{
TeamComponent team = damage.attacker.GetComponent<TeamComponent>();
if ( team != null )
info += ", team: " + team.teamIndex;
}
info += ", type: " + damage.damageType;
Console.WriteLine(info);
}
class Hopoo : IPatch
{
[HarmonyPatch(typeof(HealthComponent), nameof(HealthComponent.TakeDamage))]
[HarmonyTranspiler]
static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> IL)
=> InsertCodeInstruction(IL);
public object GetDamageType(DamageInfo info)
=> typeof(DamageInfo).GetField(nameof(info.damageType)).GetValue(info);
}
class Gearbox : IPatch
{
[HarmonyPatch(typeof(HealthComponent), nameof(HealthComponent.TakeDamageProcess))]
[HarmonyTranspiler]
static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> IL)
=> InsertCodeInstruction(IL);
public object GetDamageType(DamageInfo info)
{
DamageType type = info.damageType.damageType;
if ( type is not DamageType.Generic ) return type;
else return (uint) info.damageType.damageTypeExtended;
}
}
}