refactor config handling

This commit is contained in:
2025-07-14 08:11:10 +02:00
parent d53d71c6d9
commit ccaf44d50b
6 changed files with 56 additions and 88 deletions
+26 -52
View File
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Remoting.Messaging;
using CustomPlayerEffects;
using HintServiceMeow.Core.Models.Hints;
using Interactables.Interobjects.DoorUtils;
@@ -53,45 +54,6 @@ public sealed class CustomClasses : Plugin
public const ushort BroadcastDuration = 10;
/// <summary>
/// Configuration for the Janitor class.
/// </summary>
public JanitorConfig JanitorConfig { get; private set; } = new();
/// <summary>
/// Configuration for the Research Subject class.
/// </summary>
public ResearchSubjectConfig ResearchSubjectConfig { get; private set; } = new();
/// <summary>
/// Configuration for the Head Guard class.
/// </summary>
public HeadGuardConfig HeadGuardConfig { get; private set; } = new();
/// <summary>
/// Configuration for the Medic class.
/// </summary>
public MedicConfig MedicConfig { get; private set; } = new();
/// <summary>
/// Configuration for the Gambler class.
/// </summary>
public GamblerConfig GamblerConfig { get; private set; } = new();
/// <summary>
/// Configuration for the ShadowStepper class.
/// </summary>
public ShadowStepperConfig ShadowStepperConfig { get; private set; } = new();
public MtfDemolitionistConfig MtfDemolitionistConfig { get; private set; } = new();
public ScoutConfig ScoutConfig { get; private set; } = new();
public ExplosiveMasterConfig ExplosiveMasterConfig { get; private set; } = new();
public FlashMasterConfig FlashMasterConfig { get; private set; } = new();
public SerpentsHandConfig SerpentsHandConfig { get; private set; } = new();
public NegromancerConfig NegromancerConfig { get; private set; } = new();
public NegromancerShadowConfig NegromancerShadowConfig { get; private set; } = new();
public BloodFueledConfig BloodFueledConfig { get; private set; } = new();
internal readonly Dictionary<Player, Hint> Hints = new();
public static CustomClasses Instance { get; private set; }
@@ -223,9 +185,9 @@ public sealed class CustomClasses : Plugin
{
SerpentsHandManager.SpawnSerpentWave();
ClassManager.TryHandleSpawn(spectator, ScoutConfig, typeof(ScoutConfig), () =>
ClassManager.TryHandleSpawn(spectator, ClassManager.GetConfig<ScoutConfig>(), typeof(ScoutConfig), () =>
{
if (!ClassManager.ForceSpawn(spectator, SerpentsHandConfig, typeof(SerpentsHandConfig), PreSpawn))
if (!ClassManager.ForceSpawn(spectator, ClassManager.GetConfig<SerpentsHandConfig>(), typeof(SerpentsHandConfig), PreSpawn))
Logger.Error("Serpents Hand didn't spawn");
return;
@@ -239,7 +201,7 @@ public sealed class CustomClasses : Plugin
}
}
if (ClassManager.TryHandleSpawn(spectator, ScoutConfig, typeof(ScoutConfig), () =>
if (ClassManager.TryHandleSpawn(spectator, ClassManager.GetConfig<ScoutConfig>(), typeof(ScoutConfig), () =>
{
spectator.SetRole(ev.Wave.Faction == Faction.FoundationStaff ? RoleTypeId.NtfPrivate : RoleTypeId.ChaosConscript, RoleChangeReason.Respawn, RoleSpawnFlags.UseSpawnpoint);
})) return;
@@ -259,7 +221,7 @@ public sealed class CustomClasses : Plugin
ev.IsAllowed = false;
foreach (var evSpawningPlayer in ev.SpawningPlayers)
{
if (!ClassManager.ForceSpawn(evSpawningPlayer, SerpentsHandConfig, typeof(SerpentsHandConfig), PreSpawn))
if (!ClassManager.ForceSpawn(evSpawningPlayer, ClassManager.GetConfig<SerpentsHandConfig>(), typeof(SerpentsHandConfig), PreSpawn))
Logger.Error("Serpents Hand didn't spawn");
continue;
@@ -280,16 +242,10 @@ public sealed class CustomClasses : Plugin
{
ev.Player.CustomInfo = "";
if (ClassManager.TryHandleSpawn(ev.Player, JanitorConfig, typeof(JanitorConfig), null)) return;
if (ClassManager.TryHandleSpawn(ev.Player, ResearchSubjectConfig, typeof(ResearchSubjectConfig), null)) return;
if (ClassManager.TryHandleSpawn(ev.Player, HeadGuardConfig, typeof(HeadGuardConfig), null)) return;
if (ClassManager.TryHandleSpawn(ev.Player, MedicConfig, typeof(MedicConfig), null)) return;
if (ClassManager.TryHandleSpawn(ev.Player, GamblerConfig, typeof(GamblerConfig), null)) return;
if (ClassManager.TryHandleSpawn(ev.Player, ShadowStepperConfig, typeof(ShadowStepperConfig), null)) return;
if (ClassManager.TryHandleSpawn(ev.Player, MtfDemolitionistConfig, typeof(MtfDemolitionistConfig), null))
if (ClassManager.Configs.Any(classManagerConfig => ClassManager.TryHandleSpawn(ev.Player, classManagerConfig.Value, classManagerConfig.Key, null)))
{
return;
if (ClassManager.TryHandleSpawn(ev.Player, ExplosiveMasterConfig, typeof(ExplosiveMasterConfig), null)) return;
if (ClassManager.TryHandleSpawn(ev.Player, BloodFueledConfig, typeof(BloodFueledConfig), null)) return;
}
}
private static void OnScp914ProcessingPickup(Scp914ProcessingPickupEventArgs ev)
@@ -330,6 +286,7 @@ public class CustomClassManager
private readonly object _lock = new();
private readonly Random _random = new();
private readonly Dictionary<Type, SpawnState> _spawnStates = new();
public Dictionary<Type, CustomClassConfig> Configs { get; } = new();
/// <summary>
/// Initializes a new instance of the <see cref="CustomClassManager"/> class and registers all handlers.
@@ -378,13 +335,30 @@ public class CustomClassManager
/// <param name="spawnState">Optional custom spawn state</param>
private void RegisterHandler<T>(ICustomClassHandler handler, [CanBeNull] SpawnState spawnState = null) where T : CustomClassConfig
{
T config;
try
{
config = Activator.CreateInstance<T>();
}
catch (Exception ex)
{
Logger.Error($"Failed to create config instance for {typeof(T).Name}: {ex.Message}");
return;
}
lock (_lock)
{
_spawnStates[typeof(T)] = spawnState ?? new SpawnState();
_handlers[typeof(T)] = handler;
Configs[typeof(T)] = config;
}
}
public T GetConfig<T>() where T : CustomClassConfig
{
return (T)Configs[typeof(T)];
}
/// <summary>
/// Resets all spawn states for a new round.
/// </summary>