|
| 1 | +using JetBrains.Annotations; |
| 2 | +using Robust.Shared.Prototypes; |
| 3 | + |
| 4 | +namespace Content.Shared._Emberfall.EntityDataOverride; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Handles applying name and description overrides from EntityDataOverridePrototypes. |
| 8 | +/// </summary> |
| 9 | +public sealed class EntityDataOverrideSystem : EntitySystem |
| 10 | +{ |
| 11 | + [Dependency] private readonly IPrototypeManager _prototype = default!; |
| 12 | + [Dependency] private readonly MetaDataSystem _metaData = default!; |
| 13 | + |
| 14 | + private EntityQuery<MetaDataComponent> _metaQuery; |
| 15 | + private readonly Dictionary<string, EntityDataOverridePrototype> _overrideCache = new(); |
| 16 | + |
| 17 | + public override void Initialize() |
| 18 | + { |
| 19 | + base.Initialize(); |
| 20 | + |
| 21 | + _metaQuery = GetEntityQuery<MetaDataComponent>(); |
| 22 | + SubscribeLocalEvent<MetaDataComponent, MapInitEvent>(OnMapInit); |
| 23 | + SubscribeLocalEvent<MetaDataComponent, ComponentStartup>(OnStartup); |
| 24 | + |
| 25 | + // Cache our overrides for faster lookup |
| 26 | + _prototype.PrototypesReloaded += OnPrototypesReloaded; |
| 27 | + CacheOverrides(); |
| 28 | + } |
| 29 | + |
| 30 | + private void OnStartup(Entity<MetaDataComponent> ent, ref ComponentStartup args) |
| 31 | + { |
| 32 | + // Apply override for newly spawned entities |
| 33 | + TryApplyOverride(ent); |
| 34 | + } |
| 35 | + |
| 36 | + private void OnMapInit(Entity<MetaDataComponent> ent, ref MapInitEvent ev) |
| 37 | + { |
| 38 | + // Apply overrides for map-spawned entities |
| 39 | + TryApplyOverride(ent); |
| 40 | + } |
| 41 | + |
| 42 | + private void OnPrototypesReloaded(PrototypesReloadedEventArgs obj) |
| 43 | + { |
| 44 | + if (!obj.ByType.ContainsKey(typeof(EntityDataOverridePrototype))) |
| 45 | + return; |
| 46 | + |
| 47 | + // Recache and reapply overrides |
| 48 | + _overrideCache.Clear(); |
| 49 | + CacheOverrides(); |
| 50 | + |
| 51 | + var query = AllEntityQuery<MetaDataComponent>(); |
| 52 | + while (query.MoveNext(out var uid, out var meta)) |
| 53 | + { |
| 54 | + TryApplyOverride((uid, meta)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private void CacheOverrides() |
| 59 | + { |
| 60 | + foreach (var proto in _prototype.EnumeratePrototypes<EntityDataOverridePrototype>()) |
| 61 | + { |
| 62 | + _overrideCache[proto.ID] = proto; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Attempts to apply an entity data override to the given entity if one exists. |
| 68 | + /// </summary> |
| 69 | + [PublicAPI] |
| 70 | + public void TryApplyOverride(Entity<MetaDataComponent> ent) |
| 71 | + { |
| 72 | + var meta = ent.Comp; |
| 73 | + if (meta.EntityPrototype == null) |
| 74 | + return; |
| 75 | + |
| 76 | + // Check if we have an override for this entity's prototype |
| 77 | + if (!_overrideCache.TryGetValue(meta.EntityPrototype.ID, out var override_)) // funny |
| 78 | + return; |
| 79 | + |
| 80 | + // Only apply non-null overrides |
| 81 | + if (override_.Name != null) |
| 82 | + _metaData.SetEntityName(ent, override_.Name, meta); |
| 83 | + |
| 84 | + if (override_.Description != null) |
| 85 | + _metaData.SetEntityDescription(ent, override_.Description, meta); |
| 86 | + } |
| 87 | +} |
0 commit comments