C#:
// Reference: 0Harmony
using HarmonyLib;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Reflection;
using UnityEngine;
using System.Linq;
using Facepunch;
namespace Oxide.Plugins
{
[Info("No Incen AHeli", "ViolationHandler", "0.0.3")]
[Description("Prevents usage of incen rockets in the Attack Helicopters.")]
public class NoIncenAHeli : RustPlugin
{
private Harmony _harmonyInstance;
private void Loaded()
{
_harmonyInstance = new Harmony("com.ViolationHandler" + Name);
_harmonyInstance.Patch(AccessTools.Method(typeof(AttackHelicopterRockets), nameof(AttackHelicopterRockets.ItemFilter)), transpiler: new HarmonyMethod(typeof(NoIncenAttackHeli), nameof(NoIncenAttackHeli.Transpiler)));
}
private void Unload()
{
_harmonyInstance?.UnpatchAll(_harmonyInstance.Id);
}
public class NoIncenAttackHeli
{
internal static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator ilGenerator)
{
List<CodeInstruction> list = instructions.ToList();
Label skip = ilGenerator.DefineLabel();
if (list[0].labels == null) list[0].labels = new List<Label> { skip };
else list[0].labels.Add(skip);
list.InsertRange(0, new List<CodeInstruction>
{
// load Item
new CodeInstruction(OpCodes.Ldarg_1),
// Get ItemDefinition
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(Item), nameof(global::Item.info))),
// Load This
new CodeInstruction(OpCodes.Ldarg_0),
// Load Incendiary Rocket ItemDefinition
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(AttackHelicopterRockets), "incendiaryRocketDef")),
// Check if equal to the item definition from arg_1
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(UnityEngine.Object), nameof(UnityEngine.Object.Equals))),
// If not equal, then go to the next portion of checks in default code.
new CodeInstruction(OpCodes.Brfalse, skip),
// If equal, then return false and don't allow it.
new CodeInstruction(OpCodes.Ldc_I4_0),
new CodeInstruction(OpCodes.Ret)
});
return list;
}
}
}
}