Use ECS prototype-reload events (#22613)
* Use ECS prototype-reload events * better constructors * Maybe this fixes tests?
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user