2024-03-02 23:40:04 +03:00
|
|
|
using System.Linq;
|
|
|
|
|
using Content.Server.GameTicking;
|
2023-11-27 22:12:34 +11:00
|
|
|
using Content.Server.GameTicking.Events;
|
2022-08-12 12:53:28 +10:00
|
|
|
using Content.Shared.Audio;
|
2024-03-02 23:40:04 +03:00
|
|
|
using Content.Shared.Audio.Events;
|
2025-06-24 21:22:28 -05:00
|
|
|
using Content.Shared.CCVar;
|
2023-12-11 21:26:55 +11:00
|
|
|
using Content.Shared.GameTicking;
|
2023-11-27 22:12:34 +11:00
|
|
|
using Robust.Server.Audio;
|
|
|
|
|
using Robust.Shared.Audio;
|
2025-06-24 21:22:28 -05:00
|
|
|
using Robust.Shared.Configuration;
|
2023-11-27 22:12:34 +11:00
|
|
|
using Robust.Shared.Prototypes;
|
2024-03-02 23:40:04 +03:00
|
|
|
using Robust.Shared.Random;
|
2022-08-12 12:53:28 +10:00
|
|
|
|
2025-06-24 21:22:28 -05:00
|
|
|
|
2022-08-12 12:53:28 +10:00
|
|
|
namespace Content.Server.Audio;
|
|
|
|
|
|
|
|
|
|
public sealed class ContentAudioSystem : SharedContentAudioSystem
|
|
|
|
|
{
|
2024-03-02 23:40:04 +03:00
|
|
|
[ValidatePrototypeId<SoundCollectionPrototype>]
|
2024-04-20 11:51:04 +03:00
|
|
|
private const string LobbyMusicCollection = "CP14LobbyMusic";
|
2024-03-02 23:40:04 +03:00
|
|
|
|
2023-11-27 22:12:34 +11:00
|
|
|
[Dependency] private readonly AudioSystem _serverAudio = default!;
|
2024-03-02 23:40:04 +03:00
|
|
|
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
|
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
2025-06-24 21:22:28 -05:00
|
|
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
2024-03-02 23:40:04 +03:00
|
|
|
|
2025-06-24 21:22:28 -05:00
|
|
|
private SoundCollectionPrototype? _lobbyMusicCollection = default!;
|
2024-03-02 23:40:04 +03:00
|
|
|
private string[]? _lobbyPlaylist;
|
2022-08-12 12:53:28 +10:00
|
|
|
|
2023-11-27 22:12:34 +11:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2024-03-02 23:40:04 +03:00
|
|
|
|
2025-06-24 21:22:28 -05:00
|
|
|
//changes the music collection and reshuffles the playlist to update the lobby music
|
|
|
|
|
Subs.CVar(
|
|
|
|
|
_cfg,
|
|
|
|
|
CCVars.LobbyMusicCollection,
|
|
|
|
|
x =>
|
|
|
|
|
{
|
|
|
|
|
//Checks to see if the sound collection exists. If it does change it if not defaults to null
|
|
|
|
|
// as the new _lobbyMusicCollection meaning it wont play anything in the lobby.
|
|
|
|
|
if(_prototypeManager.TryIndex<SoundCollectionPrototype>(x, out var outputSoundCollection))
|
|
|
|
|
{
|
|
|
|
|
_lobbyMusicCollection = outputSoundCollection;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"Invalid Lobby Music sound collection specified: {x}");
|
|
|
|
|
_lobbyMusicCollection = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_lobbyPlaylist = ShuffleLobbyPlaylist();
|
|
|
|
|
},
|
|
|
|
|
true);
|
2024-03-02 23:40:04 +03:00
|
|
|
|
|
|
|
|
SubscribeLocalEvent<RoundEndMessageEvent>(OnRoundEnd);
|
|
|
|
|
SubscribeLocalEvent<PlayerJoinedLobbyEvent>(OnPlayerJoinedLobby);
|
2023-12-11 21:26:55 +11:00
|
|
|
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundCleanup);
|
2023-11-27 22:12:34 +11:00
|
|
|
SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);
|
2023-12-22 09:13:45 -05:00
|
|
|
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnProtoReload);
|
2023-11-27 22:12:34 +11:00
|
|
|
}
|
|
|
|
|
|
2023-12-11 21:26:55 +11:00
|
|
|
private void OnRoundCleanup(RoundRestartCleanupEvent ev)
|
|
|
|
|
{
|
|
|
|
|
SilenceAudio();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-27 22:12:34 +11:00
|
|
|
private void OnProtoReload(PrototypesReloadedEventArgs obj)
|
|
|
|
|
{
|
2023-12-22 09:13:45 -05:00
|
|
|
if (obj.WasModified<AudioPresetPrototype>())
|
|
|
|
|
_serverAudio.ReloadPresets();
|
2023-11-27 22:12:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnRoundStart(RoundStartingEvent ev)
|
|
|
|
|
{
|
|
|
|
|
// On cleanup all entities get purged so need to ensure audio presets are still loaded
|
|
|
|
|
// yeah it's whacky af.
|
|
|
|
|
_serverAudio.ReloadPresets();
|
|
|
|
|
}
|
2024-03-02 23:40:04 +03:00
|
|
|
|
|
|
|
|
private void OnPlayerJoinedLobby(PlayerJoinedLobbyEvent ev)
|
|
|
|
|
{
|
|
|
|
|
if (_lobbyPlaylist != null)
|
|
|
|
|
{
|
|
|
|
|
var session = ev.PlayerSession;
|
|
|
|
|
RaiseNetworkEvent(new LobbyPlaylistChangedEvent(_lobbyPlaylist), session);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnRoundEnd(RoundEndMessageEvent ev)
|
|
|
|
|
{
|
|
|
|
|
// The lobby song is set here instead of in RestartRound,
|
|
|
|
|
// because ShowRoundEndScoreboard triggers the start of the music playing
|
|
|
|
|
// at the end of a round, and this needs to be set before RestartRound
|
|
|
|
|
// in order for the lobby song status display to be accurate.
|
|
|
|
|
_lobbyPlaylist = ShuffleLobbyPlaylist();
|
|
|
|
|
RaiseNetworkEvent(new LobbyPlaylistChangedEvent(_lobbyPlaylist));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string[] ShuffleLobbyPlaylist()
|
|
|
|
|
{
|
2025-06-24 21:22:28 -05:00
|
|
|
if (_lobbyMusicCollection == null)
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-02 23:40:04 +03:00
|
|
|
var playlist = _lobbyMusicCollection.PickFiles
|
|
|
|
|
.Select(x => x.ToString())
|
|
|
|
|
.ToArray();
|
2025-06-24 21:22:28 -05:00
|
|
|
_robustRandom.Shuffle(playlist);
|
2024-03-02 23:40:04 +03:00
|
|
|
|
2025-06-24 21:22:28 -05:00
|
|
|
return playlist;
|
2024-03-02 23:40:04 +03:00
|
|
|
}
|
2022-08-12 12:53:28 +10:00
|
|
|
}
|