Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions SecretAPI/Features/PrefabManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace SecretAPI.Features
{
using System;
using System.Linq;
using Interactables.Interobjects;
using MapGeneration;
Expand All @@ -14,11 +15,6 @@ public static class PrefabManager
private const string HczBulkDoorName = "HCZ BulkDoor";
private const string EzDoorName = "EZ BreakableDoor";

private static BasicDoor? lczDoor;
private static BasicDoor? hczDoor;
private static BasicDoor? hczBulkDoor;
private static BasicDoor? ezDoor;

/// <summary>
/// Gets the <see cref="ReferenceHub"/> prefab.
/// </summary>
Expand All @@ -27,24 +23,25 @@ public static class PrefabManager
/// <summary>
/// Gets the <see cref="BasicDoor"/> found in <see cref="FacilityZone.LightContainment"/>.
/// </summary>
public static BasicDoor LczDoorPrefab => lczDoor ??= GetDoor(LczDoorName);
public static BasicDoor LczDoorPrefab => field ??= GetDoor(LczDoorName);

/// <summary>
/// Gets the <see cref="BasicDoor"/> found in <see cref="FacilityZone.HeavyContainment"/>.
/// </summary>
public static BasicDoor HczDoorPrefab => hczDoor ??= GetDoor(HczDoorName);
public static BasicDoor HczDoorPrefab => field ??= GetDoor(HczDoorName);

/// <summary>
/// Gets the <see cref="BasicDoor"/> found in <see cref="FacilityZone.HeavyContainment"/>.
/// </summary>
public static BasicDoor HczBulkDoorPrefab => hczBulkDoor ??= GetDoor(HczBulkDoorName);
public static BasicDoor HczBulkDoorPrefab => field ??= GetDoor(HczBulkDoorName);

/// <summary>
/// Gets the <see cref="BasicDoor"/> found in <see cref="FacilityZone.Entrance"/>.
/// </summary>
public static BasicDoor EzDoorPrefab => ezDoor ??= GetDoor(EzDoorName);
public static BasicDoor EzDoorPrefab => field ??= GetDoor(EzDoorName);

private static BasicDoor GetDoor(string name)
=> PrefabStore<BasicDoor>.AllComponentPrefabs.FirstOrDefault(d => d.name == name)!;
=> PrefabStore<BasicDoor>.AllComponentPrefabs.FirstOrDefault(d => d.name == name)
?? throw new InvalidOperationException($"[PrefabManager] Failed to get door named {name} | Report this as a bug!");
}
}
19 changes: 8 additions & 11 deletions SecretAPI/Features/PrefabStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@
public static class PrefabStore<TPrefab>
where TPrefab : NetworkBehaviour
{
private static TPrefab[]? collection;
private static TPrefab? savedPrefab;

/// <summary>
/// Gets the first prefab found of the specified type.
/// </summary>
public static TPrefab Prefab
{
get
{
if (savedPrefab)
return savedPrefab;
if (field)
return field;

if (typeof(TPrefab) == typeof(ReferenceHub))
return savedPrefab = NetworkManager.singleton.playerPrefab.GetComponent<TPrefab>();
return field = NetworkManager.singleton.playerPrefab.GetComponent<TPrefab>();

return savedPrefab = AllComponentPrefabs.FirstOrDefault()!;
return field = AllComponentPrefabs.FirstOrDefault()!;
}
}

Expand All @@ -43,8 +40,8 @@ public static TPrefab[] AllComponentPrefabs
{
get
{
if (collection != null)
return collection;
if (field != null)
return field;

List<TPrefab> allPrefabs = ListPool<TPrefab>.Shared.Rent();

Expand All @@ -54,10 +51,10 @@ public static TPrefab[] AllComponentPrefabs
allPrefabs.Add(prefab);
}

collection = allPrefabs.ToArray();
field = allPrefabs.ToArray();
ListPool<TPrefab>.Shared.Return(allPrefabs);

return collection;
return field;
}
}
}
Expand Down