ResaveCommand.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Shared.Administration;
  4. using Robust.Shared.Console;
  5. using Robust.Shared.ContentPack;
  6. using Robust.Shared.EntitySerialization;
  7. using Robust.Shared.EntitySerialization.Components;
  8. using Robust.Shared.EntitySerialization.Systems;
  9. using Robust.Shared.Utility;
  10. namespace Content.Server.Maps;
  11. /// <summary>
  12. /// Loads every map and resaves it into the data folder.
  13. /// </summary>
  14. [AdminCommand(AdminFlags.Host)]
  15. public sealed class ResaveCommand : LocalizedCommands
  16. {
  17. [Dependency] private readonly IEntityManager _entManager = default!;
  18. [Dependency] private readonly IResourceManager _res = default!;
  19. [Dependency] private readonly ILogManager _log = default!;
  20. public override string Command => "resave";
  21. public override void Execute(IConsoleShell shell, string argStr, string[] args)
  22. {
  23. var loader = _entManager.System<MapLoaderSystem>();
  24. var opts = MapLoadOptions.Default with
  25. {
  26. DeserializationOptions = DeserializationOptions.Default with
  27. {
  28. StoreYamlUids = true,
  29. LogOrphanedGrids = false
  30. }
  31. };
  32. var log = _log.GetSawmill(Command);
  33. var files = _res.ContentFindFiles(new ResPath("/Maps/")).ToList();
  34. for (var i = 0; i < files.Count; i++)
  35. {
  36. var fn = files[i];
  37. log.Info($"Re-saving file {i}/{files.Count} : {fn}");
  38. if (!loader.TryLoadGeneric(fn, out var result, opts))
  39. continue;
  40. if (result.Maps.Count != 1)
  41. {
  42. shell.WriteError(
  43. $"Multi-map or multi-grid files like {fn} are not yet supported by the {Command} command");
  44. loader.Delete(result);
  45. continue;
  46. }
  47. var map = result.Maps.First();
  48. // Process deferred component removals.
  49. _entManager.CullRemovedComponents();
  50. if (_entManager.HasComponent<LoadedMapComponent>(map))
  51. {
  52. loader.TrySaveMap(map.Comp.MapId, fn);
  53. }
  54. else if (result.Grids.Count == 1)
  55. {
  56. loader.TrySaveGrid(result.Grids.First(), fn);
  57. }
  58. else
  59. {
  60. shell.WriteError($"Failed to resave {fn}");
  61. }
  62. loader.Delete(result);
  63. }
  64. shell.WriteLine($"Resaved all maps");
  65. }
  66. }