Use ECS prototype-reload events (#22613)

* Use ECS prototype-reload events

* better constructors

* Maybe this fixes tests?
This commit is contained in:
Leon Friedrich
2023-12-22 09:13:45 -05:00
committed by GitHub
parent 053c1e877f
commit b6bd82caa6
23 changed files with 135 additions and 242 deletions

View File

@@ -23,15 +23,7 @@ public sealed class AlertLevelSystem : EntitySystem
public override void Initialize()
{
SubscribeLocalEvent<StationInitializedEvent>(OnStationInitialize);
_prototypeManager.PrototypesReloaded += OnPrototypeReload;
}
public override void Shutdown()
{
base.Shutdown();
_prototypeManager.PrototypesReloaded -= OnPrototypeReload;
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypeReload);
}
public override void Update(float time)

View File

@@ -3,7 +3,6 @@ using Content.Shared.Audio;
using Content.Shared.GameTicking;
using Robust.Server.Audio;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Components;
using Robust.Shared.Prototypes;
namespace Content.Server.Audio;
@@ -11,14 +10,13 @@ namespace Content.Server.Audio;
public sealed class ContentAudioSystem : SharedContentAudioSystem
{
[Dependency] private readonly AudioSystem _serverAudio = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundCleanup);
SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);
_protoManager.PrototypesReloaded += OnProtoReload;
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnProtoReload);
}
private void OnRoundCleanup(RoundRestartCleanupEvent ev)
@@ -28,16 +26,8 @@ public sealed class ContentAudioSystem : SharedContentAudioSystem
private void OnProtoReload(PrototypesReloadedEventArgs obj)
{
if (!obj.ByType.ContainsKey(typeof(AudioPresetPrototype)))
return;
_serverAudio.ReloadPresets();
}
public override void Shutdown()
{
base.Shutdown();
_protoManager.PrototypesReloaded -= OnProtoReload;
if (obj.WasModified<AudioPresetPrototype>())
_serverAudio.ReloadPresets();
}
private void OnRoundStart(RoundStartingEvent ev)

View File

@@ -1,3 +1,4 @@
using System.Collections.Frozen;
using Content.Shared.Chat.Prototypes;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
@@ -7,44 +8,36 @@ namespace Content.Server.Chat.Systems;
// emotes using emote prototype
public partial class ChatSystem
{
private readonly Dictionary<string, EmotePrototype> _wordEmoteDict = new();
private FrozenDictionary<string, EmotePrototype> _wordEmoteDict = FrozenDictionary<string, EmotePrototype>.Empty;
private void InitializeEmotes()
protected override void OnPrototypeReload(PrototypesReloadedEventArgs obj)
{
_prototypeManager.PrototypesReloaded += OnPrototypeReloadEmotes;
CacheEmotes();
}
private void ShutdownEmotes()
{
_prototypeManager.PrototypesReloaded -= OnPrototypeReloadEmotes;
}
private void OnPrototypeReloadEmotes(PrototypesReloadedEventArgs obj)
{
CacheEmotes();
base.OnPrototypeReload(obj);
if (obj.WasModified<EmotePrototype>())
CacheEmotes();
}
private void CacheEmotes()
{
_wordEmoteDict.Clear();
var dict = new Dictionary<string, EmotePrototype>();
var emotes = _prototypeManager.EnumeratePrototypes<EmotePrototype>();
foreach (var emote in emotes)
{
foreach (var word in emote.ChatTriggers)
{
var lowerWord = word.ToLower();
if (_wordEmoteDict.ContainsKey(lowerWord))
if (dict.TryGetValue(lowerWord, out var value))
{
var existingId = _wordEmoteDict[lowerWord].ID;
var errMsg = $"Duplicate of emote word {lowerWord} in emotes {emote.ID} and {existingId}";
Logger.Error(errMsg);
var errMsg = $"Duplicate of emote word {lowerWord} in emotes {emote.ID} and {value.ID}";
Log.Error(errMsg);
continue;
}
_wordEmoteDict.Add(lowerWord, emote);
dict.Add(lowerWord, emote);
}
}
_wordEmoteDict = dict.ToFrozenDictionary();
}
/// <summary>

View File

@@ -69,7 +69,7 @@ public sealed partial class ChatSystem : SharedChatSystem
public override void Initialize()
{
base.Initialize();
InitializeEmotes();
CacheEmotes();
_configurationManager.OnValueChanged(CCVars.LoocEnabled, OnLoocEnabledChanged, true);
_configurationManager.OnValueChanged(CCVars.DeadLoocEnabled, OnDeadLoocEnabledChanged, true);
_configurationManager.OnValueChanged(CCVars.CritLoocEnabled, OnCritLoocEnabledChanged, true);
@@ -80,7 +80,6 @@ public sealed partial class ChatSystem : SharedChatSystem
public override void Shutdown()
{
base.Shutdown();
ShutdownEmotes();
_configurationManager.UnsubValueChanged(CCVars.LoocEnabled, OnLoocEnabledChanged);
_configurationManager.UnsubValueChanged(CCVars.DeadLoocEnabled, OnDeadLoocEnabledChanged);
_configurationManager.UnsubValueChanged(CCVars.CritLoocEnabled, OnCritLoocEnabledChanged);
@@ -736,7 +735,7 @@ public sealed partial class ChatSystem : SharedChatSystem
return ev.Message;
}
public bool CheckIgnoreSpeechBlocker(EntityUid sender, bool ignoreBlocker)
{
if (ignoreBlocker)

View File

@@ -17,8 +17,7 @@ public sealed class ChemistryGuideDataSystem : SharedChemistryGuideDataSystem
{
base.Initialize();
PrototypeManager.PrototypesReloaded += PrototypeManagerReload;
SubscribeLocalEvent<PrototypesReloadedEventArgs>(PrototypeManagerReload);
_player.PlayerStatusChanged += OnPlayerStatusChanged;
InitializeServerRegistry();

View File

@@ -31,7 +31,7 @@ public sealed class RandomGiftSystem : EntitySystem
/// <inheritdoc/>
public override void Initialize()
{
_prototype.PrototypesReloaded += OnPrototypesReloaded;
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReloaded);
SubscribeLocalEvent<RandomGiftComponent, MapInitEvent>(OnGiftMapInit);
SubscribeLocalEvent<RandomGiftComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<RandomGiftComponent, ExaminedEvent>(OnExamined);
@@ -80,7 +80,8 @@ public sealed class RandomGiftSystem : EntitySystem
private void OnPrototypesReloaded(PrototypesReloadedEventArgs obj)
{
BuildIndex();
if (obj.WasModified<EntityPrototype>())
BuildIndex();
}
private void BuildIndex()

View File

@@ -10,7 +10,6 @@ using Content.Shared.Administration;
using Content.Shared.Mobs;
using Content.Shared.NPC;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
@@ -38,8 +37,7 @@ public sealed class HTNSystem : EntitySystem
SubscribeLocalEvent<HTNComponent, PlayerDetachedEvent>(_npc.OnPlayerNPCDetach);
SubscribeLocalEvent<HTNComponent, ComponentShutdown>(OnHTNShutdown);
SubscribeNetworkEvent<RequestHTNMessage>(OnHTNMessage);
_prototypeManager.PrototypesReloaded += OnPrototypeLoad;
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypeLoad);
OnLoad();
}
@@ -57,12 +55,6 @@ public sealed class HTNSystem : EntitySystem
_subscribers.Remove(args.SenderSession);
}
public override void Shutdown()
{
base.Shutdown();
_prototypeManager.PrototypesReloaded -= OnPrototypeLoad;
}
private void OnLoad()
{
// Clear all NPCs in case they're hanging onto stale tasks

View File

@@ -1,3 +1,4 @@
using System.Collections.Frozen;
using System.Linq;
using Content.Server.NPC.Components;
using JetBrains.Annotations;
@@ -18,31 +19,23 @@ public sealed partial class NpcFactionSystem : EntitySystem
/// <summary>
/// To avoid prototype mutability we store an intermediary data class that gets used instead.
/// </summary>
private Dictionary<string, FactionData> _factions = new();
private FrozenDictionary<string, FactionData> _factions = FrozenDictionary<string, FactionData>.Empty;
public override void Initialize()
{
base.Initialize();
_sawmill = Logger.GetSawmill("faction");
SubscribeLocalEvent<NpcFactionMemberComponent, ComponentStartup>(OnFactionStartup);
_protoManager.PrototypesReloaded += OnProtoReload;
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnProtoReload);
InitializeException();
RefreshFactions();
}
public override void Shutdown()
{
base.Shutdown();
_protoManager.PrototypesReloaded -= OnProtoReload;
}
private void OnProtoReload(PrototypesReloadedEventArgs obj)
{
if (!obj.ByType.ContainsKey(typeof(NpcFactionPrototype)))
return;
RefreshFactions();
if (obj.WasModified<NpcFactionPrototype>())
RefreshFactions();
}
private void OnFactionStartup(EntityUid uid, NpcFactionMemberComponent memberComponent, ComponentStartup args)
@@ -237,16 +230,15 @@ public sealed partial class NpcFactionSystem : EntitySystem
private void RefreshFactions()
{
_factions.Clear();
foreach (var faction in _protoManager.EnumeratePrototypes<NpcFactionPrototype>())
{
_factions[faction.ID] = new FactionData()
_factions = _protoManager.EnumeratePrototypes<NpcFactionPrototype>().ToFrozenDictionary(
faction => faction.ID,
faction => new FactionData
{
Friendly = faction.Friendly.ToHashSet(),
Hostile = faction.Hostile.ToHashSet(),
};
}
Hostile = faction.Hostile.ToHashSet()
});
foreach (var comp in EntityQuery<NpcFactionMemberComponent>(true))
{

View File

@@ -28,9 +28,9 @@ public sealed class NameIdentifierSystem : EntitySystem
SubscribeLocalEvent<NameIdentifierComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<NameIdentifierComponent, ComponentShutdown>(OnComponentShutdown);
SubscribeLocalEvent<RoundRestartCleanupEvent>(CleanupIds);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnReloadPrototypes);
InitialSetupPrototypes();
_prototypeManager.PrototypesReloaded += OnReloadPrototypes;
}
private void OnComponentShutdown(EntityUid uid, NameIdentifierComponent component, ComponentShutdown args)
@@ -46,13 +46,6 @@ public sealed class NameIdentifierSystem : EntitySystem
}
}
public override void Shutdown()
{
base.Shutdown();
_prototypeManager.PrototypesReloaded -= OnReloadPrototypes;
}
/// <summary>
/// Generates a new unique name/suffix for a given entity and adds it to <see cref="CurrentIds"/>
/// but does not set the entity's name.

View File

@@ -85,14 +85,13 @@ public sealed partial class BiomeSystem : SharedBiomeSystem
SubscribeLocalEvent<ShuttleFlattenEvent>(OnShuttleFlatten);
_configManager.OnValueChanged(CVars.NetMaxUpdateRange, SetLoadRange, true);
InitializeCommands();
ProtoManager.PrototypesReloaded += ProtoReload;
SubscribeLocalEvent<PrototypesReloadedEventArgs>(ProtoReload);
}
public override void Shutdown()
{
base.Shutdown();
_configManager.UnsubValueChanged(CVars.NetMaxUpdateRange, SetLoadRange);
ProtoManager.PrototypesReloaded -= ProtoReload;
}
private void ProtoReload(PrototypesReloadedEventArgs obj)

View File

@@ -48,7 +48,7 @@ public sealed partial class DungeonSystem : SharedDungeonSystem
_console.RegisterCommand("dungen", Loc.GetString("cmd-dungen-desc"), Loc.GetString("cmd-dungen-help"), GenerateDungeon, CompletionCallback);
_console.RegisterCommand("dungen_preset_vis", Loc.GetString("cmd-dungen_preset_vis-desc"), Loc.GetString("cmd-dungen_preset_vis-help"), DungeonPresetVis, PresetCallback);
_console.RegisterCommand("dungen_pack_vis", Loc.GetString("cmd-dungen_pack_vis-desc"), Loc.GetString("cmd-dungen_pack_vis-help"), DungeonPackVis, PackCallback);
_prototype.PrototypesReloaded += PrototypeReload;
SubscribeLocalEvent<PrototypesReloadedEventArgs>(PrototypeReload);
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundCleanup);
SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);
}
@@ -91,8 +91,6 @@ public sealed partial class DungeonSystem : SharedDungeonSystem
public override void Shutdown()
{
base.Shutdown();
_prototype.PrototypesReloaded -= PrototypeReload;
foreach (var token in _dungeonJobs.Values)
{
token.Cancel();

View File

@@ -30,6 +30,7 @@ public sealed class SpreaderSystem : EntitySystem
/// <summary>
/// Remaining number of updates per grid & prototype.
/// </summary>
// TODO PERFORMANCE Assign each prototype to an index and convert dictionary to array
private Dictionary<EntityUid, Dictionary<string, int>> _gridUpdates = new();
public const float SpreadCooldownSeconds = 1;
@@ -42,24 +43,16 @@ public sealed class SpreaderSystem : EntitySystem
{
SubscribeLocalEvent<AirtightChanged>(OnAirtightChanged);
SubscribeLocalEvent<GridInitializeEvent>(OnGridInit);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypeReload);
SubscribeLocalEvent<EdgeSpreaderComponent, EntityTerminatingEvent>(OnTerminating);
SetupPrototypes();
_prototype.PrototypesReloaded += OnPrototypeReload;
}
public override void Shutdown()
{
base.Shutdown();
_prototype.PrototypesReloaded -= OnPrototypeReload;
}
private void OnPrototypeReload(PrototypesReloadedEventArgs obj)
{
if (!obj.ByType.ContainsKey(typeof(EdgeSpreaderPrototype)))
return;
SetupPrototypes();
if (obj.WasModified<EdgeSpreaderPrototype>())
SetupPrototypes();
}
private void SetupPrototypes()