From 080e01fc7f0267fff00509dd91ed39b46dd7d2eb Mon Sep 17 00:00:00 2001 From: Num1ock Date: Tue, 18 Feb 2025 22:56:00 +0400 Subject: [PATCH 1/4] feat: Spawning NPCs via RA adds them to the `Npc.List` --- ...RemoteAdminNpcCommandAddToDictionaryFix.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs diff --git a/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs b/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs new file mode 100644 index 000000000..6d8c07be6 --- /dev/null +++ b/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs @@ -0,0 +1,72 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Fixes +{ + using System.Collections.Generic; + using System.Reflection; + using System.Reflection.Emit; + + using CommandSystem.Commands.RemoteAdmin.Dummies; + using Exiled.API.Features; + using Exiled.API.Features.Pools; + using GameCore; + using HarmonyLib; + using UnityEngine; + + using static HarmonyLib.AccessTools; + + /// + /// Fix to add > created via RA to the >. + /// + [HarmonyPatch(typeof(SpawnDummyCommand), nameof(SpawnDummyCommand.Execute))] + internal class RemoteAdminNpcCommandAddToDictionaryFix + { + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + MethodBase method = Method(typeof(DummyUtils), nameof(DummyUtils.SpawnDummy)); + + // call ReferenceHub GameCore.DummyUtils::SpawnDummy(string) + int index = newInstructions.FindIndex(instruction => + instruction.operand == (object)method) + 1; + + LocalBuilder npc = generator.DeclareLocal(typeof(Npc)); + + // pop + newInstructions.RemoveAt(index); + + newInstructions.InsertRange( + index, + new CodeInstruction[] + { + // Npc::.ctor(ReferenceHub) + new(OpCodes.Newobj, Constructor(typeof(Npc), new[] { typeof(ReferenceHub) })), + new(OpCodes.Stloc_S, npc.LocalIndex), + + // Player.Dictionary::get_Dictionary + new(OpCodes.Call, PropertyGetter(typeof(Player), nameof(Player.Dictionary))), + new(OpCodes.Ldloc_S, npc.LocalIndex), + + // Player::GameObject_getGameObject + new(OpCodes.Callvirt, PropertyGetter(typeof(Player), nameof(Player.GameObject))), + new(OpCodes.Ldloc_S, npc.LocalIndex), + + // Player.Dictionary.Add(GameObject, ReferenceHub) + new(OpCodes.Callvirt, Method(typeof(Dictionary), nameof(Dictionary.Add))), + }); + + for (int i = 0; i < newInstructions.Count; i++) + { + yield return newInstructions[i]; + } + + ListPool.Pool.Return(newInstructions); + } + } +} From 5a9f7aa8be87ff1e96b90cc17143663257937468 Mon Sep 17 00:00:00 2001 From: Num1ock <71600441+Num10ck@users.noreply.github.com> Date: Wed, 19 Feb 2025 07:56:19 +0400 Subject: [PATCH 2/4] Update RemoteAdminNpcCommandAddToDictionaryFix.cs RemoteAdminNpcCommandAddToDictionaryFix is now static. --- .../Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs b/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs index 6d8c07be6..eb37f73a2 100644 --- a/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs +++ b/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs @@ -24,7 +24,7 @@ namespace Exiled.Events.Patches.Fixes /// Fix to add > created via RA to the >. /// [HarmonyPatch(typeof(SpawnDummyCommand), nameof(SpawnDummyCommand.Execute))] - internal class RemoteAdminNpcCommandAddToDictionaryFix + internal static class RemoteAdminNpcCommandAddToDictionaryFix { private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) { @@ -49,11 +49,11 @@ private static IEnumerable Transpiler(IEnumerable Date: Wed, 19 Feb 2025 15:14:58 +0400 Subject: [PATCH 3/4] Code style changes. Co-authored-by: Yamato <66829532+louis1706@users.noreply.github.com> --- .../Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs b/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs index eb37f73a2..7c403bc31 100644 --- a/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs +++ b/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs @@ -62,9 +62,7 @@ private static IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions); } From 655c66d61ee56aaf69be608396df89e2ec14ffbe Mon Sep 17 00:00:00 2001 From: Num1ock <71600441+Num10ck@users.noreply.github.com> Date: Wed, 19 Feb 2025 15:20:59 +0400 Subject: [PATCH 4/4] Documentation fix. --- .../Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs b/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs index 7c403bc31..cb7b867f7 100644 --- a/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs +++ b/EXILED/Exiled.Events/Patches/Fixes/RemoteAdminNpcCommandAddToDictionaryFix.cs @@ -21,7 +21,7 @@ namespace Exiled.Events.Patches.Fixes using static HarmonyLib.AccessTools; /// - /// Fix to add > created via RA to the >. + /// Fix to add created via RA to the . /// [HarmonyPatch(typeof(SpawnDummyCommand), nameof(SpawnDummyCommand.Execute))] internal static class RemoteAdminNpcCommandAddToDictionaryFix