ContentAudioSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Linq;
  2. using Content.Server.GameTicking;
  3. using Content.Server.GameTicking.Events;
  4. using Content.Shared.Audio;
  5. using Content.Shared.Audio.Events;
  6. using Content.Shared.GameTicking;
  7. using Robust.Server.Audio;
  8. using Robust.Shared.Audio;
  9. using Robust.Shared.Prototypes;
  10. using Robust.Shared.Random;
  11. namespace Content.Server.Audio;
  12. public sealed class ContentAudioSystem : SharedContentAudioSystem
  13. {
  14. [ValidatePrototypeId<SoundCollectionPrototype>]
  15. private const string LobbyMusicCollection = "LobbyMusic";
  16. [Dependency] private readonly AudioSystem _serverAudio = default!;
  17. [Dependency] private readonly IRobustRandom _robustRandom = default!;
  18. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  19. private SoundCollectionPrototype _lobbyMusicCollection = default!;
  20. private string[]? _lobbyPlaylist;
  21. public override void Initialize()
  22. {
  23. base.Initialize();
  24. _lobbyMusicCollection = _prototypeManager.Index<SoundCollectionPrototype>(LobbyMusicCollection);
  25. _lobbyPlaylist = ShuffleLobbyPlaylist();
  26. SubscribeLocalEvent<RoundEndMessageEvent>(OnRoundEnd);
  27. SubscribeLocalEvent<PlayerJoinedLobbyEvent>(OnPlayerJoinedLobby);
  28. SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundCleanup);
  29. SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);
  30. SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnProtoReload);
  31. }
  32. private void OnRoundCleanup(RoundRestartCleanupEvent ev)
  33. {
  34. SilenceAudio();
  35. }
  36. private void OnProtoReload(PrototypesReloadedEventArgs obj)
  37. {
  38. if (obj.WasModified<AudioPresetPrototype>())
  39. _serverAudio.ReloadPresets();
  40. }
  41. private void OnRoundStart(RoundStartingEvent ev)
  42. {
  43. // On cleanup all entities get purged so need to ensure audio presets are still loaded
  44. // yeah it's whacky af.
  45. _serverAudio.ReloadPresets();
  46. }
  47. private void OnPlayerJoinedLobby(PlayerJoinedLobbyEvent ev)
  48. {
  49. if (_lobbyPlaylist != null)
  50. {
  51. var session = ev.PlayerSession;
  52. RaiseNetworkEvent(new LobbyPlaylistChangedEvent(_lobbyPlaylist), session);
  53. }
  54. }
  55. private void OnRoundEnd(RoundEndMessageEvent ev)
  56. {
  57. // The lobby song is set here instead of in RestartRound,
  58. // because ShowRoundEndScoreboard triggers the start of the music playing
  59. // at the end of a round, and this needs to be set before RestartRound
  60. // in order for the lobby song status display to be accurate.
  61. _lobbyPlaylist = ShuffleLobbyPlaylist();
  62. RaiseNetworkEvent(new LobbyPlaylistChangedEvent(_lobbyPlaylist));
  63. }
  64. private string[] ShuffleLobbyPlaylist()
  65. {
  66. var playlist = _lobbyMusicCollection.PickFiles
  67. .Select(x => x.ToString())
  68. .ToArray();
  69. _robustRandom.Shuffle(playlist);
  70. return playlist;
  71. }
  72. }