Files
crystall-punk-14/Content.Server/Maps/ResaveCommand.cs

82 lines
2.4 KiB
C#
Raw Normal View History

2023-05-02 11:38:01 +10:00
using System.Linq;
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Robust.Shared.ContentPack;
2024-12-22 15:13:10 +13:00
using Robust.Shared.EntitySerialization;
using Robust.Shared.EntitySerialization.Components;
using Robust.Shared.EntitySerialization.Systems;
2023-05-02 11:38:01 +10:00
using Robust.Shared.Utility;
namespace Content.Server.Maps;
/// <summary>
/// Loads every map and resaves it into the data folder.
/// </summary>
[AdminCommand(AdminFlags.Host)]
2023-05-02 11:38:01 +10:00
public sealed class ResaveCommand : LocalizedCommands
{
[Dependency] private readonly IEntityManager _entManager = default!;
[Dependency] private readonly IResourceManager _res = default!;
2024-12-22 15:13:10 +13:00
[Dependency] private readonly ILogManager _log = default!;
2023-05-02 11:38:01 +10:00
public override string Command => "resave";
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
var loader = _entManager.System<MapLoaderSystem>();
2024-12-22 15:13:10 +13:00
var opts = MapLoadOptions.Default with
2023-05-02 11:38:01 +10:00
{
2024-12-22 15:13:10 +13:00
DeserializationOptions = DeserializationOptions.Default with
{
StoreYamlUids = true,
LogOrphanedGrids = false
}
};
var log = _log.GetSawmill(Command);
var files = _res.ContentFindFiles(new ResPath("/Maps/")).ToList();
for (var i = 0; i < files.Count; i++)
{
var fn = files[i];
log.Info($"Re-saving file {i}/{files.Count} : {fn}");
2024-12-24 21:00:53 +13:00
if (!loader.TryLoadGeneric(fn, out var result, opts))
2024-12-22 15:13:10 +13:00
continue;
if (result.Maps.Count != 1)
2023-05-02 11:38:01 +10:00
{
2024-12-22 15:13:10 +13:00
shell.WriteError(
$"Multi-map or multi-grid files like {fn} are not yet supported by the {Command} command");
loader.Delete(result);
continue;
}
var map = result.Maps.First();
2023-05-02 11:38:01 +10:00
// Process deferred component removals.
_entManager.CullRemovedComponents();
2024-12-22 15:13:10 +13:00
if (_entManager.HasComponent<LoadedMapComponent>(map))
2023-05-02 11:38:01 +10:00
{
2024-12-24 18:57:52 +13:00
loader.TrySaveMap(map.Comp.MapId, fn);
2023-05-02 11:38:01 +10:00
}
2024-12-22 15:13:10 +13:00
else if (result.Grids.Count == 1)
2023-05-02 11:38:01 +10:00
{
2024-12-24 18:57:52 +13:00
loader.TrySaveGrid(result.Grids.First(), fn);
2024-12-22 15:13:10 +13:00
}
else
{
shell.WriteError($"Failed to resave {fn}");
2023-05-02 11:38:01 +10:00
}
2024-12-22 15:13:10 +13:00
loader.Delete(result);
2023-05-02 11:38:01 +10:00
}
2024-12-22 15:13:10 +13:00
shell.WriteLine($"Resaved all maps");
2023-05-02 11:38:01 +10:00
}
}