* Use new Subs.CVar helper Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe. This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown. * Fix a bunch of warnings * More warning fixes * Use new DateTime serializer to get rid of ISerializationHooks in changelog code. * Get rid of some more ISerializationHooks for enums * And a little more * Apply suggestions from code review Co-authored-by: 0x6273 <0x40@keemail.me> --------- Co-authored-by: 0x6273 <0x40@keemail.me>
118 lines
3.4 KiB
C#
118 lines
3.4 KiB
C#
using Content.Shared.Audio;
|
|
using Content.Shared.CCVar;
|
|
using Content.Shared.GameTicking;
|
|
using Robust.Shared.Audio;
|
|
using Robust.Shared.Audio.Systems;
|
|
using Robust.Shared.Configuration;
|
|
using Robust.Shared.Player;
|
|
|
|
namespace Content.Client.Audio;
|
|
|
|
public sealed class ClientGlobalSoundSystem : SharedGlobalSoundSystem
|
|
{
|
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
|
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
|
|
|
// Admin music
|
|
private bool _adminAudioEnabled = true;
|
|
private List<EntityUid?> _adminAudio = new(1);
|
|
|
|
// Event sounds (e.g. nuke timer)
|
|
private bool _eventAudioEnabled = true;
|
|
private Dictionary<StationEventMusicType, EntityUid?> _eventAudio = new(1);
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
|
|
SubscribeNetworkEvent<AdminSoundEvent>(PlayAdminSound);
|
|
Subs.CVar(_cfg, CCVars.AdminSoundsEnabled, ToggleAdminSound, true);
|
|
|
|
SubscribeNetworkEvent<StationEventMusicEvent>(PlayStationEventMusic);
|
|
SubscribeNetworkEvent<StopStationEventMusic>(StopStationEventMusic);
|
|
Subs.CVar(_cfg, CCVars.EventMusicEnabled, ToggleStationEventMusic, true);
|
|
|
|
SubscribeNetworkEvent<GameGlobalSoundEvent>(PlayGameSound);
|
|
}
|
|
|
|
private void OnRoundRestart(RoundRestartCleanupEvent ev)
|
|
{
|
|
ClearAudio();
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
base.Shutdown();
|
|
ClearAudio();
|
|
}
|
|
|
|
private void ClearAudio()
|
|
{
|
|
foreach (var stream in _adminAudio)
|
|
{
|
|
_audio.Stop(stream);
|
|
}
|
|
_adminAudio.Clear();
|
|
|
|
foreach (var stream in _eventAudio.Values)
|
|
{
|
|
_audio.Stop(stream);
|
|
}
|
|
|
|
_eventAudio.Clear();
|
|
}
|
|
|
|
private void PlayAdminSound(AdminSoundEvent soundEvent)
|
|
{
|
|
if(!_adminAudioEnabled) return;
|
|
|
|
var stream = _audio.PlayGlobal(soundEvent.Filename, Filter.Local(), false, soundEvent.AudioParams);
|
|
_adminAudio.Add(stream.Value.Entity);
|
|
}
|
|
|
|
private void PlayStationEventMusic(StationEventMusicEvent soundEvent)
|
|
{
|
|
// Either the cvar is disabled or it's already playing
|
|
if(!_eventAudioEnabled || _eventAudio.ContainsKey(soundEvent.Type)) return;
|
|
|
|
var stream = _audio.PlayGlobal(soundEvent.Filename, Filter.Local(), false, soundEvent.AudioParams);
|
|
_eventAudio.Add(soundEvent.Type, stream.Value.Entity);
|
|
}
|
|
|
|
private void PlayGameSound(GameGlobalSoundEvent soundEvent)
|
|
{
|
|
_audio.PlayGlobal(soundEvent.Filename, Filter.Local(), false, soundEvent.AudioParams);
|
|
}
|
|
|
|
private void StopStationEventMusic(StopStationEventMusic soundEvent)
|
|
{
|
|
if (!_eventAudio.TryGetValue(soundEvent.Type, out var stream))
|
|
return;
|
|
|
|
_audio.Stop(stream);
|
|
_eventAudio.Remove(soundEvent.Type);
|
|
}
|
|
|
|
private void ToggleAdminSound(bool enabled)
|
|
{
|
|
_adminAudioEnabled = enabled;
|
|
if (_adminAudioEnabled) return;
|
|
foreach (var stream in _adminAudio)
|
|
{
|
|
_audio.Stop(stream);
|
|
}
|
|
_adminAudio.Clear();
|
|
}
|
|
|
|
private void ToggleStationEventMusic(bool enabled)
|
|
{
|
|
_eventAudioEnabled = enabled;
|
|
if (_eventAudioEnabled) return;
|
|
foreach (var stream in _eventAudio)
|
|
{
|
|
_audio.Stop(stream.Value);
|
|
}
|
|
_eventAudio.Clear();
|
|
}
|
|
}
|