1
0

LoadMapRuleSystem.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Linq;
  2. using Content.Server.GameTicking.Rules.Components;
  3. using Content.Server.GridPreloader;
  4. using Content.Server.StationEvents.Events;
  5. using Content.Shared.GameTicking.Components;
  6. using Robust.Server.GameObjects;
  7. using Robust.Shared.EntitySerialization;
  8. using Robust.Shared.EntitySerialization.Systems;
  9. using Robust.Shared.Map;
  10. using Robust.Shared.Prototypes;
  11. using Robust.Shared.Utility;
  12. namespace Content.Server.GameTicking.Rules;
  13. public sealed class LoadMapRuleSystem : StationEventSystem<LoadMapRuleComponent>
  14. {
  15. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  16. [Dependency] private readonly MapSystem _map = default!;
  17. [Dependency] private readonly MapLoaderSystem _mapLoader = default!;
  18. [Dependency] private readonly TransformSystem _transform = default!;
  19. [Dependency] private readonly GridPreloaderSystem _gridPreloader = default!;
  20. protected override void Added(EntityUid uid, LoadMapRuleComponent comp, GameRuleComponent rule, GameRuleAddedEvent args)
  21. {
  22. if (comp.PreloadedGrid != null && !_gridPreloader.PreloadingEnabled)
  23. {
  24. // Preloading will never work if it's disabled, duh
  25. Log.Debug($"Immediately ending {ToPrettyString(uid):rule} as preloading grids is disabled by cvar.");
  26. ForceEndSelf(uid, rule);
  27. return;
  28. }
  29. MapId mapId;
  30. IReadOnlyList<EntityUid> grids;
  31. if (comp.GameMap != null)
  32. {
  33. // Component has one of three modes, only one of the three fields should ever be populated.
  34. DebugTools.AssertNull(comp.MapPath);
  35. DebugTools.AssertNull(comp.GridPath);
  36. DebugTools.AssertNull(comp.PreloadedGrid);
  37. var gameMap = _prototypeManager.Index(comp.GameMap.Value);
  38. grids = GameTicker.LoadGameMap(gameMap, out mapId, null);
  39. Log.Info($"Created map {mapId} for {ToPrettyString(uid):rule}");
  40. }
  41. else if (comp.MapPath is {} path)
  42. {
  43. DebugTools.AssertNull(comp.GridPath);
  44. DebugTools.AssertNull(comp.PreloadedGrid);
  45. var opts = DeserializationOptions.Default with {InitializeMaps = true};
  46. if (!_mapLoader.TryLoadMap(path, out var map, out var gridSet, opts))
  47. {
  48. Log.Error($"Failed to load map from {path}!");
  49. ForceEndSelf(uid, rule);
  50. return;
  51. }
  52. grids = gridSet.Select( x => x.Owner).ToList();
  53. mapId = map.Value.Comp.MapId;
  54. }
  55. else if (comp.GridPath is { } gPath)
  56. {
  57. DebugTools.AssertNull(comp.PreloadedGrid);
  58. // I fucking love it when "map paths" choses to ar
  59. _map.CreateMap(out mapId);
  60. var opts = DeserializationOptions.Default with {InitializeMaps = true};
  61. if (!_mapLoader.TryLoadGrid(mapId, gPath, out var grid, opts))
  62. {
  63. Log.Error($"Failed to load grid from {gPath}!");
  64. ForceEndSelf(uid, rule);
  65. return;
  66. }
  67. grids = new List<EntityUid> {grid.Value.Owner};
  68. }
  69. else if (comp.PreloadedGrid is {} preloaded)
  70. {
  71. // TODO: If there are no preloaded grids left, any rule announcements will still go off!
  72. if (!_gridPreloader.TryGetPreloadedGrid(preloaded, out var loadedShuttle))
  73. {
  74. Log.Error($"Failed to get a preloaded grid with {preloaded}!");
  75. ForceEndSelf(uid, rule);
  76. return;
  77. }
  78. var mapUid = _map.CreateMap(out mapId, runMapInit: false);
  79. _transform.SetParent(loadedShuttle.Value, mapUid);
  80. grids = new List<EntityUid>() { loadedShuttle.Value };
  81. _map.InitializeMap(mapUid);
  82. }
  83. else
  84. {
  85. Log.Error($"No valid map prototype or map path associated with the rule {ToPrettyString(uid)}");
  86. ForceEndSelf(uid, rule);
  87. return;
  88. }
  89. var ev = new RuleLoadedGridsEvent(mapId, grids);
  90. RaiseLocalEvent(uid, ref ev);
  91. base.Added(uid, comp, rule, args);
  92. }
  93. }