Files
RobustToolbox/Robust.Shared/Console/EntityConsoleHost.cs
83c2a1be11 [Dependency] source generator part 2 (#6550)
* [Dependency] source generator

No more reflection, no more codegen at runtime

Also various changes to Roslyn helpers to make this easier to write.

Requires all types with dependencies to be partial and not have readonly dependency fields. An analyzer enforces this at warning level, the previous injection strategies have remained in the code *for now* as a fallback.

No fallback is available for [field: Dependency] properties, due to a Roslyn bug.

Code Fixes exist. We love Roslyn

* Apply dependencies generator changes to all code

* Release notes

* Preprocessor got hands

* Handle nullable dependencies

These are bad but gotta deal with it.

* Apply suggestions from code review

Co-authored-by: Moony <moony@hellomouse.net>

* Fine, let's not use collection expressions

---------

Co-authored-by: Moony <moony@hellomouse.net>
2026-05-08 12:38:33 +02:00

66 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Reflection;
using Robust.Shared.Utility;
namespace Robust.Shared.Console;
/// <summary>
/// Manages registration for "entity" console commands.
/// </summary>
/// <remarks>
/// See <see cref="LocalizedEntityCommands"/> for details on what "entity" console commands are.
/// </remarks>
internal sealed partial class EntityConsoleHost
{
[Dependency] private IConsoleHost _consoleHost = default!;
[Dependency] private IReflectionManager _reflectionManager = default!;
[Dependency] private IEntitySystemManager _entitySystemManager = default!;
private readonly HashSet<string> _entityCommands = [];
/// <summary>
/// If disabled, don't automatically discover commands via reflection.
/// </summary>
/// <remarks>
/// This gets disabled in certain unit tests.
/// </remarks>
public bool DiscoverCommands { get; set; } = true;
public void Startup()
{
DebugTools.Assert(_entityCommands.Count == 0);
if (!DiscoverCommands)
return;
var deps = ((EntitySystemManager)_entitySystemManager).SystemDependencyCollection;
_consoleHost.BeginRegistrationRegion();
// search for all client commands in all assemblies, and register them
foreach (var type in _reflectionManager.GetAllChildren<IEntityConsoleCommand>())
{
var instance = (IConsoleCommand)Activator.CreateInstance(type)!;
deps.InjectDependencies(instance, oneOff: true);
_entityCommands.Add(instance.Command);
_consoleHost.RegisterCommand(instance);
}
_consoleHost.EndRegistrationRegion();
}
public void Shutdown()
{
foreach (var command in _entityCommands)
{
_consoleHost.UnregisterCommand(command);
}
_entityCommands.Clear();
}
}