Skip to content

Commit

Permalink
Implementing SupplyLocker
Browse files Browse the repository at this point in the history
* I literally copy the generator transpiler

* Its works i test it.

* Give me my exiled contributor role.
  • Loading branch information
SrLicht committed Aug 26, 2024
1 parent 7cf5ee0 commit 30e3cee
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
9 changes: 1 addition & 8 deletions EXILED/Exiled.API/Features/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ namespace Exiled.API.Features
/// </summary>
public static class Map
{
/// <summary>
/// A list of <see cref="Locker"/>s on the map.
/// </summary>
internal static readonly List<Locker> LockersValue = new(35);

/// <summary>
/// A list of <see cref="PocketDimensionTeleport"/>s on the map.
/// </summary>
Expand Down Expand Up @@ -86,7 +81,7 @@ DecontaminationController.Singleton.NetworkDecontaminationOverride is Decontamin
/// <summary>
/// Gets all <see cref="Locker"/> objects.
/// </summary>
public static ReadOnlyCollection<Locker> Lockers { get; } = LockersValue.AsReadOnly();
public static ReadOnlyCollection<Locker> Lockers { get; } = SupplyLocker.LockerToSupplyLocker.Keys.ToList().AsReadOnly();

/// <summary>
/// Gets all <see cref="AdminToy"/> objects.
Expand Down Expand Up @@ -389,8 +384,6 @@ internal static void ClearCache()
{
Item.BaseToItem.Clear();

LockersValue.RemoveAll(locker => locker == null);

Ragdoll.BasicRagdollToRagdoll.Clear();

Items.Firearm.ItemTypeToFirearmInstance.Clear();
Expand Down
4 changes: 2 additions & 2 deletions EXILED/Exiled.API/Features/Spawn/LockerSpawnPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class LockerSpawnPoint : SpawnPoint
/// <summary>
/// Gets or sets the offset position within the locker where the spawn point is located, relative to the locker's origin.
/// </summary>
public Vector3? Offset { get; set; }
public Vector3 Offset { get; set; } = Vector3.zero;

/// <inheritdoc/>
public override float Chance { get; set; }
Expand All @@ -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.");
}
Expand Down
4 changes: 2 additions & 2 deletions EXILED/Exiled.API/Features/Spawn/RoomSpawnPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RoomSpawnPoint : SpawnPoint
/// <summary>
/// Gets or sets the offset position within the room where the spawn point is located, relative to the room's origin.
/// </summary>
public Vector3? Offset { get; set; }
public Vector3 Offset { get; set; } = Vector3.zero;

/// <inheritdoc/>
public override float Chance { get; set; }
Expand All @@ -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.");
}
Expand Down
20 changes: 20 additions & 0 deletions EXILED/Exiled.API/Features/SupplyLocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -195,5 +196,24 @@ public void AddItem(ItemPickup item)
/// </summary>
/// <param name="type">The type of item to be added.</param>
public void AddItem(ItemType type) => AddItem(ItemPickup.Create(type, default, default));

/// <summary>
/// Clears the cached lockers in the <see cref="LockerToSupplyLocker"/> dictionary that have become invalid.
/// This method identifies and removes all entries where either the key (a <see cref="Locker"/> instance)
/// or the value (a <see cref="SupplyLocker"/> instance) is null, ensuring that only valid references
/// are kept in the cache.
/// </summary>
internal static void ClearCache()
{
List<Locker> keysToRemove = LockerToSupplyLocker
.Where(kv => kv.Key == null || kv.Value == null)
.Select(kv => kv.Key)
.ToList();

foreach (Locker key in keysToRemove)
{
LockerToSupplyLocker.Remove(key);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------
// <copyright file="RoundHandler.cs" company="Exiled Team">
// <copyright file="MapHandler.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
Expand All @@ -8,17 +8,21 @@
namespace Exiled.CustomItems.Events
{
using Exiled.CustomItems.API.Features;
using MEC;

/// <summary>
/// Event Handlers for the CustomItem API.
/// </summary>
internal sealed class RoundHandler
internal sealed class MapHandler
{
/// <inheritdoc cref="Exiled.Events.Handlers.Map.Generated"/>
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();
});
}
}
}
1 change: 1 addition & 0 deletions EXILED/Exiled.Events/Handlers/Internal/MapGenerated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions EXILED/Exiled.Events/Patches/Generic/LockerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.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<Locker>), nameof(List<Locker>.Add), new[] { typeof(Locker) })),
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(SupplyLocker))[0]),
new(OpCodes.Pop),
});

for (int z = 0; z < newInstructions.Count; z++)
Expand Down

0 comments on commit 30e3cee

Please sign in to comment.