forked from SignatureBeef/Open-Terraria-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchNpcStrikeArgs.Server.cs
More file actions
122 lines (108 loc) · 5.59 KB
/
PatchNpcStrikeArgs.Server.cs
File metadata and controls
122 lines (108 loc) · 5.59 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
/*
Copyright (C) 2020 DeathCradle
This file is part of Open Terraria API v3 (OTAPI)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if !tModLoaderServer_V1_3
using ModFramework;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MonoMod;
using System;
using System.Linq;
/// <summary>
/// @doc Adds a Terraria.Entity entity parameter to Terraria.NPC.StrikeNPC.
/// </summary>
[MonoModIgnore]
partial class NpcStrikeArgs
{
[Modification(ModType.PreMerge, "Patching in entity source for NPC strike")]
static void PatchNpcStrikeArgs(ModFwModder modder)
{
#if TerrariaServer_1450_OrAbove || Terraria__1450_OrAbove || tModLoader_1450_OrAbove
var csr = modder.GetILCursor(() => (new Terraria.NPC()).StrikeNPC(0, 0, 0, false, false, false, 0));
#else
var csr = modder.GetILCursor(() => (new Terraria.NPC()).StrikeNPC(0, 0, 0, false, false, false));
#endif
var redirects = csr.Method.DeclaringType.Methods
.Where(x => (HookEmitter.HookMethodNamePrefix + x.Name) == csr.Method.Name || ("orig_" + x.Name) == csr.Method.Name)
.Select(x => x.GetILCursor())
.ToArray();
foreach (var method in redirects.Append(csr))
{
ParameterDefinition Entity;
method.Method.Parameters.Add(Entity = new ("entity",
ParameterAttributes.HasDefault | ParameterAttributes.Optional,
modder.Module.ImportReference(modder.GetDefinition<Terraria.Entity>())
)
{
Constant = null
});
modder.OnRewritingMethodBody += (MonoModder modder, MethodBody body, Instruction instr, int instri) =>
{
if (instr.Operand is MethodReference methodReference)
{
if (methodReference.DeclaringType.Name == method.Method.DeclaringType.Name
&& methodReference.Name == method.Method.Name)
{
if (methodReference.Parameters.Any(x => x.Name == Entity.Name))
{
return;
}
methodReference.Parameters.Add(Entity);
var methodName = body.Method.DeclaringType.Name + "." + body.Method.Name;
switch (methodName.Replace(HookEmitter.HookMethodNamePrefix, ""))
{
case "MessageBuffer.GetData":
// order matters with this logic...
var hasWhoAmI = instr.Previous.OpCode == OpCodes.Ldfld &&
instr.Previous.Operand is FieldReference fieldReference &&
fieldReference.Name == "whoAmI";
var playerRef = Instruction.Create(OpCodes.Ldsfld, modder.Module.ImportReference(modder.GetFieldDefinition(() => Terraria.Main.player)));
body.GetILProcessor().InsertBefore(instr, playerRef);
body.GetILProcessor().InsertBefore(instr,
new { OpCodes.Ldarg_0 },
new { OpCodes.Ldfld, Operand = modder.Module.ImportReference(modder.GetFieldDefinition(() => (new Terraria.MessageBuffer()).whoAmI)) },
new { OpCodes.Ldelem_Ref }
);
if (hasWhoAmI) { // 145+
// rewire the branching
var brs = instr.Previous(x => x.OpCode == OpCodes.Br_S);
brs.Operand = playerRef;
}
break;
case "NPC.StrikeNPCNoInteraction":
// this is via world, so null is expected.
body.GetILProcessor().InsertBefore(instr,
new { OpCodes.Ldnull }
);
break;
case "Player.ApplyDamageToNPC":
case "Player.ItemCheck_MeleeHitNPCs":
case "Projectile.Damage":
case "Player.ProcessHitAgainstNPC":
case "NPC.StrikeNPC":
case "Projectile.Damage_PVE_Inner":
body.GetILProcessor().InsertBefore(instr,
new { OpCodes.Ldarg_0 }
);
break;
default:
throw new NotImplementedException($"{body.Method.FullName} is not a supported caller for this modification");
}
}
}
};
}
}
}
#endif