-
Notifications
You must be signed in to change notification settings - Fork 716
Expand file tree
/
Copy pathLocalizationManager.Entity.cs
More file actions
176 lines (151 loc) · 6.78 KB
/
Copy pathLocalizationManager.Entity.cs
File metadata and controls
176 lines (151 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Linguini.Bundle;
using Linguini.Bundle.Errors;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Localization;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Robust.Shared.Localization
{
internal abstract partial class LocalizationManager
{
// Concurrent dict so that multiple threads "reading" .Name won't cause a concurrent write issue
// when the cache gets populated.
private readonly ConcurrentDictionary<string, EntityLocData> _entityCache = new();
private void FlushEntityCache()
{
_logSawmill.Debug("Flushing entity localization cache.");
_entityCache.Clear();
}
private bool TryGetEntityLocAttrib(EntityUid entity, string attribute, [NotNullWhen(true)] out string? value)
{
if (_entMan.TryGetComponent(entity, out GrammarComponent? grammar) &&
grammar.Attributes.TryGetValue(attribute, out value))
{
return true;
}
if (_entMan.GetComponent<MetaDataComponent>(entity).EntityPrototype is not {} prototype)
{
value = null;
return false;
}
var data = GetEntityData(prototype.ID);
return data.Attributes.TryGetValue(attribute, out value);
}
// Flush caches conservatively on prototype/localization changes.
private void OnPrototypesReloaded(PrototypesReloadedEventArgs args)
{
if (args.WasModified<EntityPrototype>())
FlushEntityCache();
}
private EntityLocData CalcEntityLoc(string prototypeId)
{
string? name = null;
string? desc = null;
string? suffix = null;
Dictionary<string, string>? attributes = null;
// A child can have a locale entry just to override its suffix, while
// still inheriting the description from a parent. Hold these errors
// until we know no parent supplied a description either.
List<(string locId, List<FluentError> errs)>? deferredDescErrors = null;
foreach (var (id, prototype) in _prototype.EnumerateAllParents<EntityPrototype>(prototypeId, true))
{
var locId = prototype?.CustomLocalizationID ?? $"ent-{id}";
if (TryGetMessage(locId, out var bundle, out var msg))
{
// Localization override exists.
var msgAttrs = msg.Attributes;
if (name == null && msg.Value != null)
{
// Only set name if the value isn't empty.
// So that you can override *only* a desc/suffix.
var locName = bundle.FormatPattern(msg.Value, null, out var fmtErr);
WriteWarningForErrs(fmtErr, locId);
if (!string.IsNullOrEmpty(locName))
name = locName;
}
if (msgAttrs.Count != 0)
{
foreach (var (attrId, pattern) in msg.Attributes)
{
var attrib = attrId.ToString();
if (attrib.Equals("desc")
|| attrib.Equals("suffix"))
continue;
attributes ??= new Dictionary<string, string>();
if (!attributes.ContainsKey(attrib))
{
var value = bundle.FormatPattern(pattern, null, out var errors);
WriteWarningForErrs(errors, locId);
attributes[attrib] = value;
}
}
if (desc == null
&& !bundle.TryGetMessage(locId, "desc", null, out var err1, out desc))
{
desc = null;
// Defer: a parent prototype may still provide .desc.
deferredDescErrors ??= new List<(string, List<FluentError>)>();
deferredDescErrors.Add((locId, err1.ToList()));
}
if (suffix == null
&& !bundle.TryGetMessage(locId, "suffix", null, out var err, out suffix))
{
suffix = null;
}
}
}
if (prototype == null)
continue;
name ??= prototype.SetName;
desc ??= prototype.SetDesc;
suffix ??= prototype.SetSuffix;
if (prototype.LocProperties.Count != 0)
{
foreach (var (attrib, value) in prototype.LocProperties)
{
attributes ??= new Dictionary<string, string>();
if (!attributes.ContainsKey(attrib))
{
attributes[attrib] = value;
}
}
}
}
// Plenty of prototypes have no description at all, such as spawners,
// markers, and debug/admin entities. Keep the missing .desc warning
// visible for debugging, but don't treat it as a real localization issue.
if (desc == null && deferredDescErrors != null)
{
foreach (var (locId, errs) in deferredDescErrors)
{
foreach (var err in errs)
{
_logSawmill.Debug("Missing .desc for `{locId}`\n{e1}", locId, err);
}
}
}
// Attempt to infer suffix from entity categories
if (suffix == null)
{
var suffixes = _prototype.Index<EntityPrototype>(prototypeId).Categories
.Where(x => x.Suffix != null)
.Select(x => GetString(x.Suffix!));
suffix = string.Join(", ", suffixes);
}
return new EntityLocData(
name ?? prototypeId,
desc ?? "",
suffix,
attributes?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty);
}
public EntityLocData GetEntityData(string prototypeId)
{
return _entityCache.GetOrAdd(prototypeId, (id, t) => t.CalcEntityLoc(id), this);
}
}
}