-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlamethrower.cs
More file actions
62 lines (54 loc) · 2.25 KB
/
Flamethrower.cs
File metadata and controls
62 lines (54 loc) · 2.25 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
using HarmonyLib;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
namespace DarkwoodCustomizer;
[HarmonyPatch]
internal static class FlamethrowerPatch
{
[HarmonyPatch(typeof(Flame), "onCollideWith")]
[HarmonyPrefix]
// ReSharper disable once InconsistentNaming
private static void Prefix_FlameContact(Flame __instance)
{
if (!Plugin.ItemsModification.Value) return;
if (Player.Instance.currentItem == null || Player.Instance.currentItem.type != "flamethrower") return;
if (!Plugin.CustomItems.TryGetValue("flamethrower", out var itemData) || itemData is not JObject data) return;
var contactDmgToken = data["flamethrowerContactDamage"];
if (contactDmgToken != null)
{
AccessTools.Field(typeof(Flame), "contactDamage").SetValue(__instance, contactDmgToken.Value<int>());
}
}
[HarmonyPatch]
internal static class BurnTickPatch
{
[HarmonyTargetMethod]
private static MethodBase TargetMethod()
{
// This finds the hidden inner class created by the yield return in burnTick
return AccessTools.EnumeratorMoveNext(AccessTools.Method(typeof(Burn), "burnTick"));
}
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
var codes = new List<CodeInstruction>(instructions);
for (var i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Callvirt && codes[i].operand is MethodInfo { Name: "getHit" })
{
codes.Insert(i - 1, new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(FlamethrowerPatch), nameof(GetCustomBurnDamage))));
}
}
return codes;
}
}
public static float GetCustomBurnDamage(float originalModifier)
{
if (!Plugin.ItemsModification.Value || !Plugin.CustomItems.TryGetValue("flamethrower", out var itemData) ||
itemData is not JObject data) return originalModifier;
var burnDmgToken = data["flamethrowerBurnDamage"];
return burnDmgToken?.Value<float>() ?? originalModifier;
}
}