using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; using HarmonyLib; using NitroxClient.Communication.Abstract; using NitroxClient.GameLogic; using NitroxModel.DataStructures; using NitroxModel.Helper; using NitroxModel.Packets; using UnityEngine; namespace NitroxPatcher.Patches.Dynamic; /// /// Syncs item deletion in trash can (under certain simulation ownership conditions, see ) /// public sealed partial class Trashcan_Update_Patch : NitroxPatch, IDynamicPatch { internal static readonly MethodInfo TARGET_METHOD = Reflect.Method((Trashcan t) => t.Update()); /* * if (this.storageContainer.container.RemoveItem(item, true)) * { * BroadcastDeletion(this, item.gameObject); <------- [INSERTED LINE] * UnityEngine.Object.Destroy(item.gameObject); * } */ public static IEnumerable Transpiler(IEnumerable instructions) { return new CodeMatcher(instructions).End() .Insert([ new CodeInstruction(OpCodes.Ldarg_0), new CodeInstruction(OpCodes.Ldloc_1), new CodeInstruction(OpCodes.Callvirt, Reflect.Property((Component t) => t.gameObject).GetGetMethod()), new CodeInstruction(OpCodes.Call, Reflect.Method(() => BroadcastDeletion(default, default))) ]) .InstructionEnumeration(); } /// /// Players only get simulation ownership on the base and on some objects, trashcan is not one of them /// So we are left with two cases:
/// - if the trashcan is under a SubRoot we check simulation ownership onto it
/// - else, we check simulation ownership on the trashcan and we hope at least someone has it ///
public static void BroadcastDeletion(Trashcan trashcan, GameObject gameObject) { if (!trashcan.TryGetNitroxId(out NitroxId trashcanId) || !gameObject.TryGetNitroxId(out NitroxId objectId)) { return; } if (trashcan.transform.parent && trashcan.transform.parent.GetComponent()) { if (trashcan.transform.parent.TryGetNitroxId(out NitroxId subRootId) && Resolve().HasAnyLockType(subRootId)) { Resolve().Send(new EntityDestroyed(objectId)); } return; } if (Resolve().HasAnyLockType(trashcanId)) { Resolve().Send(new EntityDestroyed(objectId)); } } }