Fix MappingCommand grid loading (#35233)

This commit is contained in:
Leon Friedrich
2025-02-18 23:25:14 +11:00
committed by GitHub
parent 6bb19d8f1d
commit e09ef5aa6f
3 changed files with 102 additions and 40 deletions

View File

@@ -9,6 +9,7 @@ using Robust.Shared.ContentPack;
using Robust.Shared.EntitySerialization;
using Robust.Shared.EntitySerialization.Systems;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Utility;
namespace Content.Server.Mapping
@@ -35,6 +36,8 @@ namespace Content.Server.Mapping
var opts = CompletionHelper.UserFilePath(args[1], res.UserData)
.Concat(CompletionHelper.ContentFilePath(args[1], res));
return CompletionResult.FromHintOptions(opts, Loc.GetString("cmd-hint-mapping-path"));
case 3:
return CompletionResult.FromHintOptions(["false", "true"], Loc.GetString("cmd-mapping-hint-grid"));
}
return CompletionResult.Empty;
}
@@ -47,7 +50,7 @@ namespace Content.Server.Mapping
return;
}
if (args.Length > 2)
if (args.Length > 3)
{
shell.WriteLine(Help);
return;
@@ -57,12 +60,20 @@ namespace Content.Server.Mapping
shell.WriteLine(Loc.GetString("cmd-mapping-warning"));
#endif
// For backwards compatibility, isGrid is optional and we allow mappers to try load grids without explicitly
// specifying that they are loading a grid. Currently content is not allowed to override a map's MapId, so
// without engine changes this needs to be done by brute force by just trying to load it as a map first.
// This can result in errors being logged if the file is actually a grid, but the command should still work.
// yipeeee
bool? isGrid = args.Length < 3 ? null : bool.Parse(args[2]);
MapId mapId;
string? toLoad = null;
var mapSys = _entities.System<SharedMapSystem>();
Entity<MapGridComponent>? grid = null;
// Get the map ID to use
if (args.Length is 1 or 2)
if (args.Length > 0)
{
if (!int.TryParse(args[0], out var intMapId))
{
@@ -79,7 +90,7 @@ namespace Content.Server.Mapping
return;
}
if (_map.MapExists(mapId))
if (mapSys.MapExists(mapId))
{
shell.WriteError(Loc.GetString("cmd-mapping-exists", ("mapId", mapId)));
return;
@@ -93,12 +104,43 @@ namespace Content.Server.Mapping
else
{
var path = new ResPath(args[1]);
toLoad = path.FilenameWithoutExtension;
var opts = new DeserializationOptions {StoreYamlUids = true};
_entities.System<MapLoaderSystem>().TryLoadMapWithId(mapId, path, out _, out _, opts);
var loader = _entities.System<MapLoaderSystem>();
if (isGrid == true)
{
mapSys.CreateMap(mapId, runMapInit: false);
if (!loader.TryLoadGrid(mapId, path, out grid, opts))
{
shell.WriteError(Loc.GetString("cmd-mapping-error"));
mapSys.DeleteMap(mapId);
return;
}
}
else if (!loader.TryLoadMapWithId(mapId, path, out _, out _, opts))
{
if (isGrid == false)
{
shell.WriteError(Loc.GetString("cmd-mapping-error"));
return;
}
// isGrid was not specified and loading it as a map failed, so we fall back to trying to load
// the file as a grid
shell.WriteLine(Loc.GetString("cmd-mapping-try-grid"));
mapSys.CreateMap(mapId, runMapInit: false);
if (!loader.TryLoadGrid(mapId, path, out grid, opts))
{
shell.WriteError(Loc.GetString("cmd-mapping-error"));
mapSys.DeleteMap(mapId);
return;
}
}
}
// was the map actually created or did it fail somehow?
if (!_map.MapExists(mapId))
if (!mapSys.MapExists(mapId))
{
shell.WriteError(Loc.GetString("cmd-mapping-error"));
return;
@@ -120,16 +162,22 @@ namespace Content.Server.Mapping
shell.ExecuteCommand("changecvar events.enabled false");
shell.ExecuteCommand("changecvar shuttle.auto_call_time 0");
if (_cfg.GetCVar(CCVars.AutosaveEnabled))
shell.ExecuteCommand($"toggleautosave {mapId} {toLoad ?? "NEWMAP"}");
var auto = _entities.System<MappingSystem>();
if (grid != null)
auto.ToggleAutosave(grid.Value.Owner, toLoad ?? "NEWGRID");
else
auto.ToggleAutosave(mapId, toLoad ?? "NEWMAP");
shell.ExecuteCommand($"tp 0 0 {mapId}");
shell.RemoteExecuteCommand("mappingclientsidesetup");
_map.SetMapPaused(mapId, true);
DebugTools.Assert(mapSys.IsPaused(mapId));
if (args.Length == 2)
shell.WriteLine(Loc.GetString("cmd-mapping-success-load",("mapId",mapId),("path", args[1])));
else
if (args.Length != 2)
shell.WriteLine(Loc.GetString("cmd-mapping-success", ("mapId", mapId)));
else if (grid == null)
shell.WriteLine(Loc.GetString("cmd-mapping-success-load", ("mapId", mapId), ("path", args[1])));
else
shell.WriteLine(Loc.GetString("cmd-mapping-success-load-grid", ("mapId", mapId), ("path", args[1])));
}
}
}