Allow specifiying overrides for components when spawning an entity. (#4068)

* Oh goddamnit this is a refactor now.

* aaaaaaaaaaa

---------

Co-authored-by: moonheart08 <moonheart08@users.noreply.github.com>
This commit is contained in:
Moony
2023-05-19 15:38:45 -05:00
committed by GitHub
parent 57ddf81fc4
commit 06f5c78152
6 changed files with 58 additions and 33 deletions

View File

@@ -75,9 +75,9 @@ namespace Robust.Server.GameObjects
StartEntity(entity);
}
private protected override EntityUid CreateEntity(string? prototypeName, EntityUid uid = default)
private protected override EntityUid CreateEntity(string? prototypeName, EntityUid uid = default, IEntityLoadContext? context = null)
{
var entity = base.CreateEntity(prototypeName, uid);
var entity = base.CreateEntity(prototypeName, uid, context);
if (!string.IsNullOrWhiteSpace(prototypeName))
{

View File

@@ -289,21 +289,21 @@ namespace Robust.Shared.GameObjects
#region Entity Management
public EntityUid CreateEntityUninitialized(string? prototypeName, EntityUid euid)
public EntityUid CreateEntityUninitialized(string? prototypeName, EntityUid euid, ComponentRegistry? overrides = null)
{
return CreateEntity(prototypeName, euid);
return CreateEntity(prototypeName, euid, overrides);
}
/// <inheritdoc />
public virtual EntityUid CreateEntityUninitialized(string? prototypeName)
public virtual EntityUid CreateEntityUninitialized(string? prototypeName, ComponentRegistry? overrides = null)
{
return CreateEntity(prototypeName);
return CreateEntity(prototypeName, default, overrides);
}
/// <inheritdoc />
public virtual EntityUid CreateEntityUninitialized(string? prototypeName, EntityCoordinates coordinates)
public virtual EntityUid CreateEntityUninitialized(string? prototypeName, EntityCoordinates coordinates, ComponentRegistry? overrides = null)
{
var newEntity = CreateEntity(prototypeName);
var newEntity = CreateEntity(prototypeName, default, overrides);
if (coordinates.IsValid(this))
{
@@ -314,9 +314,9 @@ namespace Robust.Shared.GameObjects
}
/// <inheritdoc />
public virtual EntityUid CreateEntityUninitialized(string? prototypeName, MapCoordinates coordinates)
public virtual EntityUid CreateEntityUninitialized(string? prototypeName, MapCoordinates coordinates, ComponentRegistry? overrides = null)
{
var newEntity = CreateEntity(prototypeName);
var newEntity = CreateEntity(prototypeName, default, overrides);
var transform = GetComponent<TransformComponent>(newEntity);
if (coordinates.MapId == MapId.Nullspace)
@@ -347,7 +347,7 @@ namespace Robust.Shared.GameObjects
}
/// <inheritdoc />
public virtual EntityUid SpawnEntity(string? protoName, EntityCoordinates coordinates)
public virtual EntityUid SpawnEntity(string? protoName, EntityCoordinates coordinates, ComponentRegistry? overrides = null)
{
if (!coordinates.IsValid(this))
throw new InvalidOperationException($"Tried to spawn entity {protoName} on invalid coordinates {coordinates}.");
@@ -358,9 +358,9 @@ namespace Robust.Shared.GameObjects
}
/// <inheritdoc />
public virtual EntityUid SpawnEntity(string? protoName, MapCoordinates coordinates)
public virtual EntityUid SpawnEntity(string? protoName, MapCoordinates coordinates, ComponentRegistry? overrides = null)
{
var entity = CreateEntityUninitialized(protoName, coordinates);
var entity = CreateEntityUninitialized(protoName, coordinates, overrides);
InitializeAndStartEntity(entity, coordinates.MapId);
return entity;
}
@@ -680,7 +680,7 @@ namespace Robust.Shared.GameObjects
/// <summary>
/// Allocates an entity and loads components but does not do initialization.
/// </summary>
private protected virtual EntityUid CreateEntity(string? prototypeName, EntityUid uid = default)
private protected virtual EntityUid CreateEntity(string? prototypeName, EntityUid uid = default, IEntityLoadContext? context = null)
{
if (prototypeName == null)
return AllocEntity(out _, uid);
@@ -688,7 +688,7 @@ namespace Robust.Shared.GameObjects
var entity = AllocEntity(prototypeName, out var metadata, uid);
try
{
EntityPrototype.LoadEntity(metadata.EntityPrototype, entity, ComponentFactory, this, _serManager, null);
EntityPrototype.LoadEntity(metadata.EntityPrototype, entity, ComponentFactory, this, _serManager, context);
return entity;
}
catch (Exception e)

View File

@@ -4,6 +4,7 @@ using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using Prometheus;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
namespace Robust.Shared.GameObjects
@@ -56,13 +57,13 @@ namespace Robust.Shared.GameObjects
event Action<EntityUid>? EntityDeleted;
event Action<EntityUid>? EntityDirtied; // only raised after initialization
EntityUid CreateEntityUninitialized(string? prototypeName, EntityUid euid);
EntityUid CreateEntityUninitialized(string? prototypeName, EntityUid euid, ComponentRegistry? overrides = null);
EntityUid CreateEntityUninitialized(string? prototypeName);
EntityUid CreateEntityUninitialized(string? prototypeName, ComponentRegistry? overrides = null);
EntityUid CreateEntityUninitialized(string? prototypeName, EntityCoordinates coordinates);
EntityUid CreateEntityUninitialized(string? prototypeName, EntityCoordinates coordinates, ComponentRegistry? overrides = null);
EntityUid CreateEntityUninitialized(string? prototypeName, MapCoordinates coordinates);
EntityUid CreateEntityUninitialized(string? prototypeName, MapCoordinates coordinates, ComponentRegistry? overrides = null);
void InitializeAndStartEntity(EntityUid entity, MapId? mapId = null);
@@ -76,7 +77,7 @@ namespace Robust.Shared.GameObjects
/// <param name="protoName">The prototype to clone. If this is null, the entity won't have a prototype.</param>
/// <param name="coordinates"></param>
/// <returns>Newly created entity.</returns>
EntityUid SpawnEntity(string? protoName, EntityCoordinates coordinates);
EntityUid SpawnEntity(string? protoName, EntityCoordinates coordinates, ComponentRegistry? overrides = null);
/// <summary>
/// Spawns an entity at a specific position
@@ -84,7 +85,7 @@ namespace Robust.Shared.GameObjects
/// <param name="protoName"></param>
/// <param name="coordinates"></param>
/// <returns></returns>
EntityUid SpawnEntity(string? protoName, MapCoordinates coordinates);
EntityUid SpawnEntity(string? protoName, MapCoordinates coordinates, ComponentRegistry? overrides = null);
/// <summary>
/// How many entities are currently active.

View File

@@ -4,6 +4,7 @@ using System.Diagnostics.CodeAnalysis;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
@@ -227,7 +228,7 @@ namespace Robust.Shared.Prototypes
}
}
private static void EnsureCompExistsAndDeserialize(EntityUid entity,
public static void EnsureCompExistsAndDeserialize(EntityUid entity,
IComponentFactory factory,
IEntityManager entityManager,
ISerializationManager serManager,
@@ -261,17 +262,6 @@ namespace Robust.Shared.Prototypes
return $"EntityPrototype({ID})";
}
public sealed class ComponentRegistry : Dictionary<string, ComponentRegistryEntry>
{
public ComponentRegistry()
{
}
public ComponentRegistry(Dictionary<string, ComponentRegistryEntry> components) : base(components)
{
}
}
[DataRecord]
public record ComponentRegistryEntry(IComponent Component, MappingDataNode Mapping);
@@ -389,4 +379,36 @@ namespace Robust.Shared.Prototypes
}
}*/
}
public sealed class ComponentRegistry : Dictionary<string, EntityPrototype.ComponentRegistryEntry>, IEntityLoadContext, ISerializationContext
{
public ComponentRegistry()
{
}
public ComponentRegistry(Dictionary<string, EntityPrototype.ComponentRegistryEntry> components) : base(components)
{
}
public bool TryGetComponent(string componentName, [NotNullWhen(true)] out IComponent? component)
{
var success = TryGetValue(componentName, out var comp);
component = comp?.Component;
return success;
}
public IEnumerable<string> GetExtraComponentTypes()
{
return Keys;
}
public bool ShouldSkipComponent(string compName)
{
return false; //Registries cannot represent the "remove this component" state.
}
public SerializationManager.SerializerProvider SerializerProvider { get; } = new();
public bool WritingReadingPrototypes { get; } = true;
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.Markdown;

View File

@@ -2,6 +2,7 @@ using System.IO;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Markdown;
using Robust.Shared.Serialization.Markdown.Mapping;