Files
RobustToolbox/Robust.Shared/Console/Commands/MapCommands.cs
f2625bf5c5 Removes IMapManager (#6584)
* Move MapManager queries to SharedMapSystem
Moves all variants of FindGridsIntersecting/TryFindGridAt to SharedMapSystem
Hollows out the MapManager methods and converts them into relays to SharedMapSystem

* Move CreateGrid to SharedMapSystem
Moves the functionality for CreateGrid and its variants to SharedMapSystem
Hollows out the MapManager methods and converts them into relays for the SharedMapSystem methods
Obsoletes them too
Also moves over the GetAllMapGrids and GetAllGrids methods

* Move RaiseOnTileChanged to SharedMapSystem
Also moves the SuppressOnTileChanged flag member to SharedMapSystem
Hollows out and obsoletes the MapManager versions

* Move default value constants to SharedMapSystem

* Converts map pausing events into LocalizedEntityCommands

* Move MapManager related delegates/structs to SharedMapSystem
Moves the GridCreationOptions struct to the same namespace as SharedMapSystem and converts it into a record struct
Move the GridCallback delegates to the same namespace as SharedMapSystem

* Move CullDeletionHistory to SharedMapSystem
Well, that was less painful than I thought it would be

* Actually obsolete the NetworkedMapManager method

* Rename file

* Doc comments for new SharedMapSystem methods

* Doc comment

* Doc comments

* Fix access

* Purge all references to IMapManagerInternal

* Cull easy IMapManager references

* The rest

* Purge IMapManager

* Private internal FindGridsIntersecting method

* please die

* 41

* notes

---------

Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com>
2026-07-01 19:32:17 -07:00

219 lines
6.9 KiB
C#

using System.Globalization;
using System.Linq;
using System.Text;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
namespace Robust.Shared.Console.Commands;
sealed partial class AddMapCommand : LocalizedEntityCommands
{
[Dependency] private SharedMapSystem _mapSystem = default!;
public override string Command => "addmap";
public override bool RequireServerOrSingleplayer => true;
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length < 1)
return;
var mapId = new MapId(int.Parse(args[0]));
if (!_mapSystem.MapExists(mapId))
{
var init = args.Length < 2 || !bool.Parse(args[1]);
EntityManager.System<SharedMapSystem>().CreateMap(mapId, runMapInit: init);
shell.WriteLine($"Map with ID {mapId} created.");
return;
}
shell.WriteError($"Map with ID {mapId} already exists!");
}
public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
switch (args.Length)
{
case 1:
var mapId = _mapSystem.GetNextMapId();
return CompletionResult.FromHintOptions([ new CompletionOption($"{mapId}")], LocalizationManager.GetString("generic-mapid"));
case 2:
return CompletionResult.FromHint(LocalizationManager.GetString("cmd-addmap-hint-2"));
default:
return CompletionResult.Empty;
}
}
}
sealed partial class RemoveMapCommand : LocalizedEntityCommands
{
[Dependency] private IEntitySystemManager _systems = default!;
public override string Command => "rmmap";
public override bool RequireServerOrSingleplayer => true;
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
{
shell.WriteError("Wrong number of args.");
return;
}
var mapId = new MapId(int.Parse(args[0]));
var mapSystem = _systems.GetEntitySystem<SharedMapSystem>();
if (!mapSystem.MapExists(mapId))
{
shell.WriteError($"Map {mapId.Value} does not exist.");
return;
}
mapSystem.DeleteMap(mapId);
shell.WriteLine($"Map {mapId.Value} was removed.");
}
public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
if (args.Length != 1)
return CompletionResult.Empty;
return CompletionResult.FromHintOptions(CompletionHelper.MapIds(args[0], entManager: EntityManager), LocalizationManager.GetString("generic-map"));
}
}
sealed class RemoveGridCommand : LocalizedEntityCommands
{
public override string Command => "rmgrid";
public override bool RequireServerOrSingleplayer => true;
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
{
shell.WriteError("Wrong number of args.");
return;
}
var gridIdNet = NetEntity.Parse(args[0]);
if (!EntityManager.TryGetEntity(gridIdNet, out var gridId) || !EntityManager.HasComponent<MapGridComponent>(gridId))
{
shell.WriteError($"Grid {gridId} does not exist.");
return;
}
EntityManager.DeleteEntity(gridId);
shell.WriteLine($"Grid {gridId} was removed.");
}
public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
if (args.Length != 1)
return CompletionResult.Empty;
return CompletionResult.FromHintOptions(CompletionHelper.Components<MapGridComponent>(args[0], entManager: EntityManager), LocalizationManager.GetString("generic-grid"));
}
}
internal sealed partial class RunMapInitCommand : LocalizedEntityCommands
{
[Dependency] private SharedMapSystem _mapSystem = default!;
public override string Command => "mapinit";
public override bool RequireServerOrSingleplayer => true;
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
{
shell.WriteError("Wrong number of args.");
return;
}
var arg = args[0];
var mapId = new MapId(int.Parse(arg, CultureInfo.InvariantCulture));
if (!_mapSystem.MapExists(mapId))
{
shell.WriteError("Map does not exist!");
return;
}
if (_mapSystem.IsInitialized(mapId))
{
shell.WriteError("Map is already initialized!");
return;
}
_mapSystem.InitializeMap(mapId);
}
}
internal sealed partial class ListMapsCommand : LocalizedEntityCommands
{
[Dependency] private IEntityManager _entManager = default!;
[Dependency] private SharedMapSystem _mapSystem = default!;
public override string Command => "lsmap";
// PVS prevents the player from knowing about all maps.
public override bool RequireServerOrSingleplayer => true;
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
var msg = new StringBuilder();
foreach (var mapId in _mapSystem.GetAllMapIds().OrderBy(id => id.Value))
{
if (!_mapSystem.TryGetMap(mapId, out var mapUid))
continue;
msg.AppendFormat("{0}: {1}, init: {2}, paused: {3}, nent: {4}, grids: {5}\n",
mapId,
_entManager.GetComponent<MetaDataComponent>(mapUid.Value).EntityName,
_mapSystem.IsInitialized(mapUid),
_mapSystem.IsPaused(mapId),
_entManager.GetNetEntity(mapUid),
string.Join(",", _mapSystem.GetAllGrids(mapId).Select(grid => grid.Owner)));
}
// Trim the newline
shell.WriteLine(msg.ToString()[..^1]);
}
}
internal sealed partial class ListGridsCommand : LocalizedEntityCommands
{
[Dependency]
private SharedTransformSystem _transformSystem = default!;
public override string Command => "lsgrid";
// PVS prevents the player from knowing about all maps.
public override bool RequireServerOrSingleplayer => true;
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
var msg = new StringBuilder();
var xformQuery = EntityManager.GetEntityQuery<TransformComponent>();
var grids = EntityManager.AllComponentsList<MapGridComponent>();
grids.Sort((x, y) => x.Uid.CompareTo(y.Uid));
foreach (var (uid, _) in grids)
{
var xform = xformQuery.GetComponent(uid);
var worldPos = _transformSystem.GetWorldPosition(xform);
msg.AppendFormat("{0}: map: {1}, ent: {2}, pos: {3:0.0},{4:0.0} \n",
uid, xform.MapID, uid, worldPos.X, worldPos.Y);
}
shell.WriteLine(msg.ToString()[..^1]);
}
}