-
Notifications
You must be signed in to change notification settings - Fork 716
Expand file tree
/
Copy pathEntityPrototype.cs
More file actions
443 lines (377 loc) · 16.5 KB
/
Copy pathEntityPrototype.cs
File metadata and controls
443 lines (377 loc) · 16.5 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using Robust.Shared.EntitySerialization;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Array;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.Prototypes
{
/// <summary>
/// Prototype that represents game entities.
/// </summary>
[Prototype(-1)]
public sealed partial class EntityPrototype : IPrototype, IInheritingPrototype, ISerializationHooks
{
private ILocalizationManager _loc = default!;
private static readonly Dictionary<string, string> LocPropertiesDefault = new();
// LOCALIZATION NOTE:
// Localization-related properties in here are manually localized in LocalizationManager.
// As such, they should NOT be inherited to avoid confusing the system.
private const int DEFAULT_RANGE = 200;
[DataField("loc")]
[NeverPushInheritance]
private Dictionary<string, string>? _locPropertiesSet;
/// <summary>
/// The "in code name" of the object. Must be unique.
/// </summary>
[ViewVariables]
[IdDataFieldAttribute]
public string ID { get; private set; } = default!;
/// <summary>
/// The name set on this level of the prototype. This does NOT handle localization or inheritance.
/// You probably want <see cref="Name"/> instead.
/// </summary>
/// <seealso cref="Name"/>
[DataField("name")]
[NeverPushInheritance]
public string? SetName { get; private set; }
[DataField("description")]
[NeverPushInheritance]
public string? SetDesc { get; private set; }
[DataField("suffix")]
[NeverPushInheritance]
public string? SetSuffix { get; private set; }
[DataField("categories"), Access(typeof(PrototypeManager))]
[NeverPushInheritance]
internal HashSet<ProtoId<EntityCategoryPrototype>>? CategoriesInternal;
/// <summary>
/// What categories this prototype belongs to. This includes categories inherited from parents and categories
/// that were automatically inferred from the prototype's components.
/// </summary>
[ViewVariables]
public IReadOnlySet<EntityCategoryPrototype> Categories { get; internal set; } = new HashSet<EntityCategoryPrototype>();
[ViewVariables]
public IReadOnlyDictionary<string, string> LocProperties => _locPropertiesSet ?? LocPropertiesDefault;
/// <summary>
/// The "in game name" of the object. What is displayed to most players.
/// </summary>
[ViewVariables]
public string Name => _loc.GetEntityData(ID).Name;
/// <summary>
/// The description of the object that shows upon using examine
/// </summary>
[ViewVariables]
public string Description => _loc.GetEntityData(ID).Desc;
/// <summary>
/// Optional suffix to display in development menus like the entity spawn panel,
/// to provide additional info without ruining the Name property itself.
/// </summary>
[ViewVariables]
public string? EditorSuffix => _loc.GetEntityData(ID).Suffix;
/// <summary>
/// Fluent messageId used to lookup the entity's name and localization attributes.
/// </summary>
[DataField("localizationId")]
public string? CustomLocalizationID { get; private set; }
/// <summary>
/// If true, this object should not show up in the entity spawn panel.
/// </summary>
[Access(typeof(PrototypeManager))]
public bool HideSpawnMenu { get; internal set; }
[DataField("placement")]
private EntityPlacementProperties PlacementProperties = new();
/// <summary>
/// The different mounting points on walls. (If any).
/// </summary>
[ViewVariables]
public List<int>? MountingPoints => PlacementProperties.MountingPoints;
/// <summary>
/// The Placement mode used for client-initiated placement. This is used for admin and editor placement. The serverside version controls what type the server assigns in normal gameplay.
/// </summary>
[ViewVariables]
public string PlacementMode => PlacementProperties.PlacementMode;
/// <summary>
/// The Range this entity can be placed from. This is only used serverside since the server handles normal gameplay. The client uses unlimited range since it handles things like admin spawning and editing.
/// </summary>
[ViewVariables]
public int PlacementRange => PlacementProperties.PlacementRange;
/// <summary>
/// Offset that is added to the position when placing. (if any). Client only.
/// </summary>
[ViewVariables]
public Vector2i PlacementOffset => PlacementProperties.PlacementOffset;
/// <summary>
/// True if this entity will be saved by the map loader.
/// </summary>
[DataField("save")]
public bool MapSavable { get; set; } = true;
/// <summary>
/// The prototype we inherit from.
/// </summary>
[ViewVariables]
[ParentDataFieldAttribute(typeof(AbstractPrototypeIdArraySerializer<EntityPrototype>))]
public string[]? Parents { get; private set; }
[ViewVariables]
[NeverPushInheritance]
[AbstractDataField]
public bool Abstract { get; private set; }
/// <summary>
/// A dictionary mapping the component type list to the YAML mapping containing their settings.
/// </summary>
[DataField]
[AlwaysPushInheritance]
public ComponentRegistry Components = new();
public EntityPrototype()
{
// Everybody gets a transform component!
Components.Add("Transform", new ComponentRegistryEntry(new TransformComponent()));
// And a metadata component too!
Components.Add("MetaData", new ComponentRegistryEntry(new MetaDataComponent()));
}
void ISerializationHooks.AfterDeserialization()
{
_loc = IoCManager.Resolve<ILocalizationManager>();
}
[Obsolete("Pass in IComponentFactory")]
public bool TryGetComponent<T>([NotNullWhen(true)] out T? component) where T : IComponent, new()
=> TryGetComponent(out component, IoCManager.Resolve<IComponentFactory>());
[Pure]
[Obsolete("TryComp is shorter, use it instead")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetComponent<T>([NotNullWhen(true)] out T? component, IComponentFactory factory) where T : IComponent, new()
=> TryComp(out component, factory);
[Obsolete("Use TryComp with a CompName instead")]
public bool TryGetComponent<T>(string name, [NotNullWhen(true)] out T? component) where T : IComponent, new()
{
// please do not use this method ever ^_^
var factory = IoCManager.Resolve<IComponentFactory>();
return TryComp(factory.CompName<T>(), out component);
}
/// <summary>
/// Tries to get and cast a component from this prototype with <typeparamref name="T"/>.
/// Returns false if the component is missing or is not assignable to <typeparamref name="T"/>.
/// </summary>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryComp<T>([NotNullWhen(true)] out T? component, IComponentFactory factory) where T : IComponent, new()
=> TryComp(factory.CompName<T>(), out component);
/// <summary>
/// Tries to get and cast a component from this prototype with a <see cref="CompName"/> associated with it.
/// Returns false if the component is missing or is not assignable to <typeparamref name="T"/>.
/// </summary>
[Pure]
public bool TryComp<T>(CompName name, [NotNullWhen(true)] out T? component) where T : IComponent, new()
{
if (!Components.TryGetValue(name.Name, out var componentUnCast))
{
component = default;
return false;
}
// TODO: should this throw? might break some crazy shitcode though
if (componentUnCast.Component is not T cast)
{
component = default;
return false;
}
component = cast;
return true;
}
/// <summary>
/// Returns true if this prototype contains a <typeparamref name="T"/>.
/// </summary>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasComp<T>(IComponentFactory factory) where T : IComponent, new()
=> HasComp(factory.CompName<T>());
/// <summary>
/// Returns true if this prototype contains a component with a given type.
/// It is a programmer error if the name does not belong to a component.
/// This is not caught by a debug assert, so only use it with types from a
/// component registry, <c>typeof(SomeComponent)</c>, etc.
/// </summary>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasComp(Type type, IComponentFactory factory)
=> HasComp(factory.CompName(type));
/// <summary>
/// Returns true if this prototype contains a component with a given <see cref="CompName"/>.
/// </summary>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasComp(CompName name)
=> Components.ContainsKey(name.Name);
internal static void LoadEntity(
Entity<MetaDataComponent> ent,
IComponentFactory factory,
IEntityManager entityManager,
ISerializationManager serManager,
IEntityLoadContext? context) //yeah officer this method right here
{
var (entity, meta) = ent;
var prototype = meta.EntityPrototype;
var ctx = context as ISerializationContext;
if (prototype != null)
{
foreach (var (name, entry) in prototype.Components)
{
if (context != null && context.ShouldSkipComponent(name))
continue;
var fullData = context != null && context.TryGetComponent(name, out var data) ? data : entry.Component;
var compReg = factory.GetRegistration(name);
EnsureCompExistsAndDeserialize(entity, compReg, factory, entityManager, serManager, name, fullData, ctx);
if (!entry.Component.NetSyncEnabled && compReg.NetID is {} netId)
meta.NetComponents.Remove(netId);
}
}
if (context != null)
{
foreach (var name in context.GetExtraComponentTypes())
{
if (prototype != null && prototype.Components.ContainsKey(name))
{
// This component also exists in the prototype.
// This means that the previous step already caught both the prototype data AND map data.
// Meaning that re-running EnsureCompExistsAndDeserialize would wipe prototype data.
continue;
}
if (!context.TryGetComponent(name, out var data))
{
throw new InvalidOperationException(
$"{nameof(IEntityLoadContext)} provided component name {name} but refused to provide data");
}
var compReg = factory.GetRegistration(name);
EnsureCompExistsAndDeserialize(entity, compReg, factory, entityManager, serManager, name, data, ctx);
}
}
}
public static void EnsureCompExistsAndDeserialize(EntityUid entity,
ComponentRegistration compReg,
IComponentFactory factory,
IEntityManager entityManager,
ISerializationManager serManager,
string compName,
IComponent data,
ISerializationContext? context)
{
if (!entityManager.TryGetComponent(entity, compReg.Idx, out var component))
{
var newComponent = factory.GetComponent(compName);
entityManager.AddComponent(entity, newComponent);
component = newComponent;
}
if (context is not EntityDeserializer map)
{
serManager.CopyTo(data, ref component, context, notNullableOverride: true);
return;
}
map.CurrentComponent = compName;
serManager.CopyTo(data, ref component, context, notNullableOverride: true);
map.CurrentComponent = null;
}
public override string ToString()
{
return $"EntityPrototype({ID})";
}
public sealed class ComponentRegistryEntry
{
public IComponent Component { get; }
public ComponentRegistryEntry(IComponent component)
{
Component = component;
}
}
[DataDefinition]
public sealed partial class EntityPlacementProperties
{
public bool PlacementOverriden { get; private set; }
public bool SnapOverriden { get; private set; }
private string _placementMode = "PlaceFree";
private Vector2i _placementOffset;
[DataField("mode")]
public string PlacementMode
{
get => _placementMode;
set
{
PlacementOverriden = true;
_placementMode = value;
}
}
[DataField("offset")]
public Vector2i PlacementOffset
{
get => _placementOffset;
set
{
PlacementOverriden = true;
_placementOffset = value;
}
}
[DataField("nodes")] public List<int>? MountingPoints;
[DataField("range")] public int PlacementRange = DEFAULT_RANGE;
private HashSet<string> _snapFlags = new();
[DataField("snap")]
public HashSet<string> SnapFlags
{
get => _snapFlags;
set
{
SnapOverriden = true;
_snapFlags = value;
}
}
}
}
public sealed class ComponentRegistry : Dictionary<string, EntityPrototype.ComponentRegistryEntry>, IEntityLoadContext
{
public ComponentRegistry()
{
}
public ComponentRegistry(Dictionary<string, EntityPrototype.ComponentRegistryEntry> components) : base(components)
{
}
/// <inheritdoc />
public bool TryGetComponent(string componentName, [NotNullWhen(true)] out IComponent? component)
{
var success = TryGetValue(componentName, out var comp);
component = comp?.Component;
return success;
}
/// <inheritdoc />
public bool TryGetComponent<TComponent>(
IComponentFactory componentFactory,
[NotNullWhen(true)] out TComponent? component
) where TComponent : class, IComponent, new()
{
component = null;
var componentName = componentFactory.GetComponentName<TComponent>();
if (TryGetComponent(componentName, out var foundComponent))
{
component = (TComponent)foundComponent;
return true;
}
return false;
}
/// <inheritdoc />
public IEnumerable<string> GetExtraComponentTypes()
{
return Keys;
}
/// <inheritdoc />
public bool ShouldSkipComponent(string compName)
{
return false; //Registries cannot represent the "remove this component" state.
}
}
}