Skip to content

Commit 52f79eb

Browse files
committed
just works?
1 parent cafa30f commit 52f79eb

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Robust.Shared.Prototypes;
2+
3+
namespace Content.Shared._Emberfall.EntityDataOverride;
4+
5+
/// <summary>
6+
/// Prototype for overriding entity name and description without modifying the original entity prototype.
7+
/// </summary>
8+
[Prototype]
9+
public sealed class EntityDataOverridePrototype : IPrototype
10+
{
11+
/// <inheritdoc/>
12+
/// <remarks>Must match the entity ID you're trying to override</remarks>
13+
[IdDataField]
14+
public string ID { get; } = default!;
15+
16+
/// <summary>
17+
/// The custom name to override the entity's default name.
18+
/// </summary>
19+
[DataField]
20+
public string? Name { get; }
21+
22+
/// <summary>
23+
/// The custom description to override the entity's default description.
24+
/// </summary>
25+
[DataField]
26+
public string? Description { get; }
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)