mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Raise ECS event on prototype-reload (#4724)
* Raise system event on prototype-reload * Oops
This commit is contained in:
@@ -122,14 +122,13 @@ public sealed partial class SpriteSystem
|
||||
return GetFallbackState();
|
||||
}
|
||||
|
||||
private void OnPrototypesReloaded(PrototypesReloadedEventArgs protoReloaded)
|
||||
private void OnPrototypesReloaded(PrototypesReloadedEventArgs args)
|
||||
{
|
||||
// Check if any EntityPrototype has been changed.
|
||||
if (!protoReloaded.ByType.TryGetValue(typeof(EntityPrototype), out var changedSet))
|
||||
if (!args.TryGetModified<EntityPrototype>(out var modified))
|
||||
return;
|
||||
|
||||
// Remove all changed prototypes from the cache, if they're there.
|
||||
foreach (var (prototype, _) in changedSet.Modified)
|
||||
foreach (var prototype in modified)
|
||||
{
|
||||
// Let's be lazy and not regenerate them until something needs them again.
|
||||
_cachedPrototypeIcons.Remove(prototype);
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace Robust.Client.GameObjects
|
||||
|
||||
UpdatesAfter.Add(typeof(SpriteTreeSystem));
|
||||
|
||||
_proto.PrototypesReloaded += OnPrototypesReloaded;
|
||||
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReloaded);
|
||||
SubscribeLocalEvent<SpriteComponent, SpriteUpdateInertEvent>(QueueUpdateInert);
|
||||
SubscribeLocalEvent<SpriteComponent, ComponentInit>(OnInit);
|
||||
|
||||
@@ -75,7 +75,6 @@ namespace Robust.Client.GameObjects
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_proto.PrototypesReloaded -= OnPrototypesReloaded;
|
||||
_cfg.UnsubValueChanged(CVars.RenderSpriteDirectionBias, OnBiasChanged);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,14 +16,7 @@ internal sealed class PrototypeReloadSystem : EntitySystem
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
_prototypes.PrototypesReloaded += OnPrototypesReloaded;
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
|
||||
_prototypes.PrototypesReloaded -= OnPrototypesReloaded;
|
||||
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReloaded);
|
||||
}
|
||||
|
||||
private void OnPrototypesReloaded(PrototypesReloadedEventArgs eventArgs)
|
||||
|
||||
@@ -44,10 +44,8 @@ namespace Robust.Shared.Localization
|
||||
// Flush caches conservatively on prototype/localization changes.
|
||||
private void OnPrototypesReloaded(PrototypesReloadedEventArgs args)
|
||||
{
|
||||
if (!args.ByType.ContainsKey(typeof(EntityPrototype)))
|
||||
return;
|
||||
|
||||
FlushEntityCache();
|
||||
if (args.WasModified<EntityPrototype>())
|
||||
FlushEntityCache();
|
||||
}
|
||||
|
||||
private EntityLocData CalcEntityLoc(string prototypeId)
|
||||
|
||||
@@ -357,9 +357,40 @@ internal interface IPrototypeManagerInternal : IPrototypeManager
|
||||
event Action<DataNodeDocument>? LoadedData;
|
||||
}
|
||||
|
||||
public sealed record PrototypesReloadedEventArgs(
|
||||
/// <summary>
|
||||
/// This is event contains information about prototypes that have been modified. It is broadcast as a system event,
|
||||
/// whenever <see cref="IPrototypeManager.PrototypesReloaded"/> gets invoked.
|
||||
/// </summary>
|
||||
public sealed record PrototypesReloadedEventArgs(HashSet<Type> Modified,
|
||||
IReadOnlyDictionary<Type, PrototypesReloadedEventArgs.PrototypeChangeSet> ByType,
|
||||
IReadOnlyDictionary<Type, HashSet<string>>? Removed = null)
|
||||
{
|
||||
public sealed record PrototypeChangeSet(IReadOnlyDictionary<string, IPrototype> Modified);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given prototype kind was modified at all. This includes both changes and removals.
|
||||
/// </summary>
|
||||
public bool WasModified<T>() where T : IPrototype
|
||||
{
|
||||
return Modified.Contains(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a set of all modified prototype instances of a given kind. This includes both changes and removals.
|
||||
/// </summary>
|
||||
public bool TryGetModified<T>([NotNullWhen(true)] out HashSet<string>? modified) where T : IPrototype
|
||||
{
|
||||
modified = null;
|
||||
if (!WasModified<T>())
|
||||
return false;
|
||||
|
||||
modified = new();
|
||||
if (ByType.TryGetValue(typeof(T), out var mod))
|
||||
modified.UnionWith(mod.Modified.Keys);
|
||||
|
||||
if (Removed != null && Removed.TryGetValue(typeof(T), out var rem))
|
||||
modified.UnionWith(rem);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ namespace Robust.Shared.Prototypes
|
||||
[Dependency] private readonly ILogManager _logManager = default!;
|
||||
[Dependency] private readonly ILocalizationManager _locMan = default!;
|
||||
[Dependency] private readonly IComponentFactory _factory = default!;
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
|
||||
private readonly Dictionary<string, Dictionary<string, MappingDataNode>> _prototypeDataCache = new();
|
||||
private EntityDiffContext _context = new();
|
||||
@@ -354,15 +355,20 @@ namespace Robust.Shared.Prototypes
|
||||
#endif
|
||||
|
||||
//todo paul i hate it but i am not opening that can of worms in this refactor
|
||||
PrototypesReloaded?.Invoke(
|
||||
new PrototypesReloadedEventArgs(
|
||||
modified
|
||||
.ToDictionary(
|
||||
g => g.Key,
|
||||
g => new PrototypesReloadedEventArgs.PrototypeChangeSet(
|
||||
g.Value.Where(x => _kinds[g.Key].Instances.ContainsKey(x))
|
||||
.ToDictionary(a => a, a => _kinds[g.Key].Instances[a]))),
|
||||
removed));
|
||||
var byType = modified
|
||||
.ToDictionary(
|
||||
g => g.Key,
|
||||
g => new PrototypesReloadedEventArgs.PrototypeChangeSet(
|
||||
g.Value.Where(x => _kinds[g.Key].Instances.ContainsKey(x))
|
||||
.ToDictionary(a => a, a => _kinds[g.Key].Instances[a])));
|
||||
|
||||
var modifiedTypes = new HashSet<Type>(byType.Keys);
|
||||
if (removed != null)
|
||||
modifiedTypes.UnionWith(removed.Keys);
|
||||
|
||||
var ev = new PrototypesReloadedEventArgs(modifiedTypes, byType, removed);
|
||||
PrototypesReloaded?.Invoke(ev);
|
||||
_entMan.EventBus.RaiseEvent(EventSource.Local, ev);
|
||||
}
|
||||
|
||||
private void Freeze(HashSet<KindData> kinds)
|
||||
|
||||
Reference in New Issue
Block a user