From 30e3cee3c66bcd812e10b86a72abffd1d45bffc8 Mon Sep 17 00:00:00 2001 From: SrLicht Date: Mon, 26 Aug 2024 18:50:01 -0300 Subject: [PATCH] Implementing SupplyLocker * I literally copy the generator transpiler * Its works i test it. * Give me my exiled contributor role. --- EXILED/Exiled.API/Features/Map.cs | 9 +-------- .../Features/Spawn/LockerSpawnPoint.cs | 4 ++-- .../Features/Spawn/RoomSpawnPoint.cs | 4 ++-- EXILED/Exiled.API/Features/SupplyLocker.cs | 20 +++++++++++++++++++ .../Events/{RoundHandler.cs => MapHandler.cs} | 12 +++++++---- .../Handlers/Internal/MapGenerated.cs | 1 + .../Patches/Generic/LockerList.cs | 6 +++--- 7 files changed, 37 insertions(+), 19 deletions(-) rename EXILED/Exiled.CustomItems/Events/{RoundHandler.cs => MapHandler.cs} (60%) diff --git a/EXILED/Exiled.API/Features/Map.cs b/EXILED/Exiled.API/Features/Map.cs index 2b3c4cdb0..05e5007a4 100644 --- a/EXILED/Exiled.API/Features/Map.cs +++ b/EXILED/Exiled.API/Features/Map.cs @@ -42,11 +42,6 @@ namespace Exiled.API.Features /// public static class Map { - /// - /// A list of s on the map. - /// - internal static readonly List LockersValue = new(35); - /// /// A list of s on the map. /// @@ -86,7 +81,7 @@ DecontaminationController.Singleton.NetworkDecontaminationOverride is Decontamin /// /// Gets all objects. /// - public static ReadOnlyCollection Lockers { get; } = LockersValue.AsReadOnly(); + public static ReadOnlyCollection Lockers { get; } = SupplyLocker.LockerToSupplyLocker.Keys.ToList().AsReadOnly(); /// /// Gets all objects. @@ -389,8 +384,6 @@ internal static void ClearCache() { Item.BaseToItem.Clear(); - LockersValue.RemoveAll(locker => locker == null); - Ragdoll.BasicRagdollToRagdoll.Clear(); Items.Firearm.ItemTypeToFirearmInstance.Clear(); diff --git a/EXILED/Exiled.API/Features/Spawn/LockerSpawnPoint.cs b/EXILED/Exiled.API/Features/Spawn/LockerSpawnPoint.cs index 216e19ca1..155be3a7b 100644 --- a/EXILED/Exiled.API/Features/Spawn/LockerSpawnPoint.cs +++ b/EXILED/Exiled.API/Features/Spawn/LockerSpawnPoint.cs @@ -32,7 +32,7 @@ public class LockerSpawnPoint : SpawnPoint /// /// Gets or sets the offset position within the locker where the spawn point is located, relative to the locker's origin. /// - public Vector3? Offset { get; set; } + public Vector3 Offset { get; set; } = Vector3.zero; /// public override float Chance { get; set; } @@ -58,7 +58,7 @@ public override Vector3 Position return foundLocker.RandomChamberPosition; // Otherwise, use the Offset if provided, or the locker's position. - return Offset.HasValue ? foundLocker.Transform.TransformPoint(Offset.Value) : foundLocker.Position; + return Offset != Vector3.zero ? foundLocker.Transform.TransformPoint(Offset) : foundLocker.Position; } set => throw new InvalidOperationException("The position of this type of SpawnPoint cannot be changed."); } diff --git a/EXILED/Exiled.API/Features/Spawn/RoomSpawnPoint.cs b/EXILED/Exiled.API/Features/Spawn/RoomSpawnPoint.cs index 21f50fb9b..92e24e712 100644 --- a/EXILED/Exiled.API/Features/Spawn/RoomSpawnPoint.cs +++ b/EXILED/Exiled.API/Features/Spawn/RoomSpawnPoint.cs @@ -27,7 +27,7 @@ public class RoomSpawnPoint : SpawnPoint /// /// Gets or sets the offset position within the room where the spawn point is located, relative to the room's origin. /// - public Vector3? Offset { get; set; } + public Vector3 Offset { get; set; } = Vector3.zero; /// public override float Chance { get; set; } @@ -48,7 +48,7 @@ public override Vector3 Position { Room roomInstance = Features.Room.Get(Room) ?? throw new InvalidOperationException("The room instance could not be found."); - return Offset.HasValue ? roomInstance.transform.TransformPoint(Offset.Value) : roomInstance.Position; + return Offset != Vector3.zero ? roomInstance.transform.TransformPoint(Offset) : roomInstance.Position; } set => throw new InvalidOperationException("The position of this type of SpawnPoint cannot be changed."); } diff --git a/EXILED/Exiled.API/Features/SupplyLocker.cs b/EXILED/Exiled.API/Features/SupplyLocker.cs index a776f47fa..157c45ee9 100644 --- a/EXILED/Exiled.API/Features/SupplyLocker.cs +++ b/EXILED/Exiled.API/Features/SupplyLocker.cs @@ -9,6 +9,7 @@ namespace Exiled.API.Features using System; using System.Collections.Generic; using System.Linq; + using System.Reflection; using Exiled.API.Enums; using Exiled.API.Extensions; @@ -195,5 +196,24 @@ public void AddItem(ItemPickup item) /// /// The type of item to be added. public void AddItem(ItemType type) => AddItem(ItemPickup.Create(type, default, default)); + + /// + /// Clears the cached lockers in the dictionary that have become invalid. + /// This method identifies and removes all entries where either the key (a instance) + /// or the value (a instance) is null, ensuring that only valid references + /// are kept in the cache. + /// + internal static void ClearCache() + { + List keysToRemove = LockerToSupplyLocker + .Where(kv => kv.Key == null || kv.Value == null) + .Select(kv => kv.Key) + .ToList(); + + foreach (Locker key in keysToRemove) + { + LockerToSupplyLocker.Remove(key); + } + } } } diff --git a/EXILED/Exiled.CustomItems/Events/RoundHandler.cs b/EXILED/Exiled.CustomItems/Events/MapHandler.cs similarity index 60% rename from EXILED/Exiled.CustomItems/Events/RoundHandler.cs rename to EXILED/Exiled.CustomItems/Events/MapHandler.cs index 65e505c7a..8d9ca2708 100644 --- a/EXILED/Exiled.CustomItems/Events/RoundHandler.cs +++ b/EXILED/Exiled.CustomItems/Events/MapHandler.cs @@ -1,5 +1,5 @@ // ----------------------------------------------------------------------- -// +// // Copyright (c) Exiled Team. All rights reserved. // Licensed under the CC BY-SA 3.0 license. // @@ -8,17 +8,21 @@ namespace Exiled.CustomItems.Events { using Exiled.CustomItems.API.Features; + using MEC; /// /// Event Handlers for the CustomItem API. /// - internal sealed class RoundHandler + internal sealed class MapHandler { /// public void OnMapGenerated() { - foreach (CustomItem customItem in CustomItem.Registered) - customItem?.SpawnAll(); + Timing.CallDelayed(1, () => // Delay its necessary for the spawnpoints of lockers and rooms to be generated. + { + foreach (CustomItem customItem in CustomItem.Registered) + customItem?.SpawnAll(); + }); } } } \ No newline at end of file diff --git a/EXILED/Exiled.Events/Handlers/Internal/MapGenerated.cs b/EXILED/Exiled.Events/Handlers/Internal/MapGenerated.cs index f75f2e91f..1bbcc5ddb 100644 --- a/EXILED/Exiled.Events/Handlers/Internal/MapGenerated.cs +++ b/EXILED/Exiled.Events/Handlers/Internal/MapGenerated.cs @@ -47,6 +47,7 @@ public static void OnMapGenerated() { Map.ClearCache(); PrefabHelper.LoadPrefabs(); + SupplyLocker.ClearCache(); // TODO: Fix For (https://git.scpslgame.com/northwood-qa/scpsl-bug-reporting/-/issues/377) PlayerRoles.RoleAssign.HumanSpawner.Handlers[PlayerRoles.Team.ChaosInsurgency] = new PlayerRoles.RoleAssign.OneRoleHumanSpawner(PlayerRoles.RoleTypeId.ChaosConscript); diff --git a/EXILED/Exiled.Events/Patches/Generic/LockerList.cs b/EXILED/Exiled.Events/Patches/Generic/LockerList.cs index 26b0732ab..a4491f5c3 100644 --- a/EXILED/Exiled.Events/Patches/Generic/LockerList.cs +++ b/EXILED/Exiled.Events/Patches/Generic/LockerList.cs @@ -29,14 +29,14 @@ private static IEnumerable Transpiler(IEnumerable newInstructions = ListPool.Pool.Get(codeInstructions); - // Map.LockersValue.Add(this); + // new SupplyLocker(this) newInstructions.InsertRange( 0, new CodeInstruction[] { - new(OpCodes.Ldsfld, Field(typeof(Map), nameof(Map.LockersValue))), new(OpCodes.Ldarg_0), - new(OpCodes.Callvirt, Method(typeof(List), nameof(List.Add), new[] { typeof(Locker) })), + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(SupplyLocker))[0]), + new(OpCodes.Pop), }); for (int z = 0; z < newInstructions.Count; z++)