WorldgenConfigSystem.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Content.Server.Administration;
  2. using Content.Server.GameTicking;
  3. using Content.Server.GameTicking.Events;
  4. using Content.Server.Worldgen.Components;
  5. using Content.Server.Worldgen.Prototypes;
  6. using Content.Shared.Administration;
  7. using Content.Shared.CCVar;
  8. using Robust.Shared.Configuration;
  9. using Robust.Shared.Console;
  10. using Robust.Shared.Map;
  11. using Robust.Shared.Prototypes;
  12. using Robust.Shared.Serialization.Manager;
  13. using Robust.Shared.Utility;
  14. namespace Content.Server.Worldgen.Systems;
  15. /// <summary>
  16. /// This handles configuring world generation during round start.
  17. /// </summary>
  18. public sealed class WorldgenConfigSystem : EntitySystem
  19. {
  20. [Dependency] private readonly GameTicker _gameTicker = default!;
  21. [Dependency] private readonly IConfigurationManager _cfg = default!;
  22. [Dependency] private readonly IConsoleHost _conHost = default!;
  23. [Dependency] private readonly IMapManager _map = default!;
  24. [Dependency] private readonly IPrototypeManager _proto = default!;
  25. [Dependency] private readonly ISerializationManager _ser = default!;
  26. private bool _enabled;
  27. private string _worldgenConfig = default!;
  28. /// <inheritdoc />
  29. public override void Initialize()
  30. {
  31. SubscribeLocalEvent<RoundStartingEvent>(OnLoadingMaps);
  32. _conHost.RegisterCommand("applyworldgenconfig", Loc.GetString("cmd-applyworldgenconfig-description"), Loc.GetString("cmd-applyworldgenconfig-help"), ApplyWorldgenConfigCommand);
  33. Subs.CVar(_cfg, CCVars.WorldgenEnabled, b => _enabled = b, true);
  34. Subs.CVar(_cfg, CCVars.WorldgenConfig, s => _worldgenConfig = s, true);
  35. }
  36. [AdminCommand(AdminFlags.Mapping)]
  37. private void ApplyWorldgenConfigCommand(IConsoleShell shell, string argstr, string[] args)
  38. {
  39. if (args.Length != 2)
  40. {
  41. shell.WriteError(Loc.GetString("shell-wrong-arguments-number-need-specific", ("properAmount", 2), ("currentAmount", args.Length)));
  42. return;
  43. }
  44. if (!int.TryParse(args[0], out var mapInt) || !_map.MapExists(new MapId(mapInt)))
  45. {
  46. shell.WriteError(Loc.GetString("shell-invalid-map-id"));
  47. return;
  48. }
  49. var map = _map.GetMapEntityId(new MapId(mapInt));
  50. if (!_proto.TryIndex<WorldgenConfigPrototype>(args[1], out var proto))
  51. {
  52. shell.WriteError(Loc.GetString("shell-argument-must-be-prototype", ("index", 2), ("prototypeName", "cmd-applyworldgenconfig-prototype")));
  53. return;
  54. }
  55. proto.Apply(map, _ser, EntityManager);
  56. shell.WriteLine(Loc.GetString("cmd-applyworldgenconfig-success"));
  57. }
  58. /// <summary>
  59. /// Applies the world config to the default map if enabled.
  60. /// </summary>
  61. private void OnLoadingMaps(RoundStartingEvent ev)
  62. {
  63. if (_enabled == false)
  64. return;
  65. var target = _map.GetMapEntityId(_gameTicker.DefaultMap);
  66. Log.Debug($"Trying to configure {_gameTicker.DefaultMap}, aka {ToPrettyString(target)} aka {target}");
  67. var cfg = _proto.Index<WorldgenConfigPrototype>(_worldgenConfig);
  68. cfg.Apply(target, _ser, EntityManager); // Apply the config to the map.
  69. DebugTools.Assert(HasComp<WorldControllerComponent>(target));
  70. }
  71. }