Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix drop boss bag hook not working properly #131

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions OTAPI.Patcher/Targets/PCServerTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public void Patch()
mm.Module.GetType("Terraria.NPC").CreateHooks(mm);
mm.Module.GetType("Terraria.WorldGen").CreateHooks(mm);
mm.Module.GetType("Terraria.Chat.ChatHelper").CreateHooks(mm);
mm.Module.GetType("Terraria.GameContent.ItemDropRules.CommonCode").CreateHooks(mm);
mm.Module.GetType("Terraria.IO.WorldFile").CreateHooks(mm);
mm.Module.GetType("Terraria.Net.NetManager").CreateHooks(mm);
mm.Module.GetType("Terraria.Projectile").CreateHooks(mm);
Expand Down
145 changes: 0 additions & 145 deletions OTAPI.Scripts/Mods/HookNpcBossBag.Server.cs

This file was deleted.

112 changes: 112 additions & 0 deletions OTAPI.Scripts/Patches/PatchNpcBossBag.Server.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
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/>.
*/
#pragma warning disable CS8321 // Local function is declared but never used
#pragma warning disable CS0436 // Type conflicts with imported type

using System;
using ModFramework;
using MonoMod.Cil;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System.Linq;
using MonoMod;

/// <summary>
/// @doc Creates Hooks.NPC.BossBag. Allows plugins to customize bosses loot as well as distributing it.
/// </summary>

[MonoModIgnore]
partial class NpcBossBag
{
[Modification(ModType.PreMerge, "Hooking NPC Boss Bag")]
static void HookNpcBossBag(ModFwModder modder)
{
foreach (var csr in new[] {
modder.GetILCursor(() => (new Terraria.NPC()).DropItemInstanced(default, default, 0, 0, false)),
modder.GetILCursor(() => Terraria.GameContent.ItemDropRules.CommonCode.DropItemLocalPerClientAndSetNPCMoneyTo0(default, default, default, default))
})
{
csr.GotoNext(MoveType.After,
i => i.OpCode == OpCodes.Ldsfld &&
i.Operand is FieldReference fieldReference &&
fieldReference.Name == "netMode" &&
fieldReference.DeclaringType.FullName == "Terraria.Main",
i => i.OpCode == OpCodes.Ldc_I4_2,
i => i.OpCode == OpCodes.Bne_Un
);

csr.Emit(OpCodes.Ldarg_0);
if (csr.Method.Name == "DropItemLocalPerClientAndSetNPCMoneyTo0")
{
csr.Emit(OpCodes.Ldarg_1);
csr.Emit(OpCodes.Ldarg_2);
csr.Emit(OpCodes.Ldarg_3);
}
else
{
csr.Emit(OpCodes.Ldarg_3);
csr.Emit(OpCodes.Ldarg, 4);
csr.Emit(OpCodes.Ldarg, 5);
}
csr.EmitDelegate(OTAPI.Hooks.NPC.InvokeBossBag);
csr.Emit(OpCodes.Nop);
csr.Emit(OpCodes.Nop);

csr.Previous.Previous.OpCode = OpCodes.Brtrue;
csr.Previous.Previous.Operand = csr.Next;

var targetBranch = csr.Method.Body.Instructions.Reverse().Where(x => x.OpCode == OpCodes.Ldarg_0).FirstOrDefault();

csr.Previous.OpCode = OpCodes.Br;
csr.Previous.Operand = targetBranch;
}
}
}

namespace OTAPI
{
public static partial class Hooks
{
public static partial class NPC
{
public class BossBagEventArgs : EventArgs
{
public HookResult? Result { get; set; }
public Terraria.NPC NPC { get; set; }
public int ItemID { get; set; }
public int Stack { get; set; }
public bool InteractionRequired { get; set; }
}
public static event EventHandler<BossBagEventArgs> BossBag;

public static bool InvokeBossBag(Terraria.NPC npc, int itemid, int stack, bool interactionRequired)
{
var args = new BossBagEventArgs()
{
NPC = npc,
ItemID = itemid,
Stack = stack,
InteractionRequired = interactionRequired
};
BossBag?.Invoke(null, args);
return args.Result != HookResult.Cancel;
}
}
}
}
50 changes: 25 additions & 25 deletions OTAPI.Scripts/Patches/PatchNpcStrikeArgs.Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

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 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
{
//static ParameterDefinition Entity { get; set; }
//static MethodDefinition StrikeNPC { get; set; }

[Modification(ModType.PreMerge, "Patching in entity source for NPC strike")]
static void PatchNpcStrikeArgs(ModFwModder modder)
{
using System.Linq;
/// <summary>
/// @doc Adds a Terraria.Entity entity parameter to Terraria.NPC.StrikeNPC.
/// </summary>
[MonoModIgnore]
partial class NpcStrikeArgs
{
//static ParameterDefinition Entity { get; set; }
//static MethodDefinition StrikeNPC { get; set; }
[Modification(ModType.PreMerge, "Patching in entity source for NPC strike")]
static void PatchNpcStrikeArgs(ModFwModder modder)
{
var csr = modder.GetILCursor(() => (new Terraria.NPC()).StrikeNPC(0, 0, 0, false, false, false));
var redirects = csr.Method.DeclaringType.Methods
.Where(x => (HookEmitter.HookMethodNamePrefix + x.Name) == csr.Method.Name || ("orig_" + x.Name) == csr.Method.Name)
Expand All @@ -45,16 +45,16 @@ static void PatchNpcStrikeArgs(ModFwModder modder)
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>())
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) =>
modder.OnRewritingMethodBody += (MonoModder modder, MethodBody body, Instruction instr, int instri) =>
{
if (instr.Operand is MethodReference methodReference)
{
Expand Down Expand Up @@ -102,7 +102,7 @@ static void PatchNpcStrikeArgs(ModFwModder modder)
}
}
};
}
}
}
}
}
#endif