From 900561df25cc279d46b63262008e098caaa30135 Mon Sep 17 00:00:00 2001 From: DEATHB4DEFEAT Date: Fri, 3 Nov 2023 21:18:32 -0700 Subject: [PATCH] add baseAudioParams and ignoreBasePath to announcer prototype and data --- .../Systems/AnnouncerSystem.Announce.cs | 68 +++++++++++-------- .../Prototypes/AnnouncerPrototype.cs | 42 ++++++++++-- 2 files changed, 74 insertions(+), 36 deletions(-) diff --git a/Content.Server/SimpleStation14/Announcements/Systems/AnnouncerSystem.Announce.cs b/Content.Server/SimpleStation14/Announcements/Systems/AnnouncerSystem.Announce.cs index 8e57592763..d062895333 100644 --- a/Content.Server/SimpleStation14/Announcements/Systems/AnnouncerSystem.Announce.cs +++ b/Content.Server/SimpleStation14/Announcements/Systems/AnnouncerSystem.Announce.cs @@ -2,6 +2,7 @@ using Content.Shared.SimpleStation14.Announcements.Prototypes; using Robust.Shared.Audio; using Robust.Shared.Player; +using Robust.Shared.Utility; namespace Content.Server.SimpleStation14.Announcements.Systems; @@ -10,57 +11,66 @@ public sealed partial class AnnouncerSystem /// /// Gets an announcement path from the announcer /// - /// ID of the announcer /// ID of the announcement from the announcer to get information from - private string GetAnnouncementPath(string announcerId, string announcementId) + /// ID of the announcer to use instead of the current one + private string GetAnnouncementPath(string announcementId, string? announcerId = null) { - var announcer = _prototypeManager.EnumeratePrototypes().ToArray().First(a => a.ID == announcerId); + var announcer = announcerId != null ? _prototypeManager.Index(announcerId) : Announcer; + // Get the announcement data from the announcer + // Will be the fallback if the data for the announcementId is not found + // A fallback is REQUIRED // TODO: Make a test to see if there is a fallback on every announcerPrototype var announcementType = announcer.AnnouncementPaths.FirstOrDefault(a => a.ID == announcementId) ?? announcer.AnnouncementPaths.First(a => a.ID.ToLower() == "fallback"); - if (announcementType.Path != null) - return $"{announcer.BasePath}/{announcementType.Path}"; + // This is not possible, throw an error if this happens + DebugTools.Assert(announcementType.Path == null && announcementType.IgnoreBasePath); + + // If the greedy announcementType wants to do the job of announcer, ignore the base path and just return the path + if (announcementType.IgnoreBasePath) + return announcementType.Path!; + // If the announcementType has a collection, get the sound from the collection if (announcementType.Collection != null) return _audioSystem.GetSound(new SoundCollectionSpecifier(announcementType.Collection)); + // If nothing is overriding the base paths, return the base path + the announcement file path return $"{announcer.BasePath}/{announcementType.Path}"; } /// /// Gets audio params from the announcer /// - /// ID of the announcer /// ID of the announcement from the announcer to get information from - private AudioParams? GetAudioParams(string announcerId, string announcementId) + /// ID of the announcer to use instead of the current one + private AudioParams? GetAudioParams(string announcementId, string? announcerId = null) { - var announcer = _prototypeManager.EnumeratePrototypes().ToArray().First(a => a.ID == announcerId); + // Fetch the announcer prototype if a different one is specified + var announcer = announcerId != null && announcerId != Announcer.ID ? _prototypeManager.Index(announcerId) : Announcer; + // Get the announcement data from the announcer + // Will be the fallback if the data for the announcementId is not found var announcementType = announcer.AnnouncementPaths.FirstOrDefault(a => a.ID == announcementId) ?? - announcer.AnnouncementPaths.First(a => a.ID == "fallback"); + announcer.AnnouncementPaths.First(a => a.ID == "fallback"); - return announcementType.AudioParams; + // Return the announcer.BaseAudioParams if the announcementType doesn't have an override + return announcementType.AudioParams ?? announcer.BaseAudioParams ?? null; // For some reason the formatter doesn't warn me about "?? null" being redundant } /// /// Gets an announcement message from the announcer /// - /// ID of the announcer /// ID of the announcement from the announcer to get information from - private string? GetAnnouncementMessage(string announcerId, string announcementId) + private string? GetAnnouncementMessage(string announcementId) { - string? result = null; - - var announcer = _prototypeManager.EnumeratePrototypes().ToArray().First(a => a.ID == announcerId); - + // Get the announcement data from the announcer + // Will be the fallback if the data for the announcementId is not found var announcementType = Announcer.AnnouncementPaths.FirstOrDefault(a => a.ID == announcementId) ?? - Announcer.AnnouncementPaths.First(a => a.ID == "fallback"); + Announcer.AnnouncementPaths.First(a => a.ID == "fallback"); - if (announcementType.MessageOverride != null) - result = Loc.GetString(announcementType.MessageOverride); - - return result; + // Return the announcementType.MessageOverride if it exists, otherwise return null + return announcementType.MessageOverride != null ? Loc.GetString(announcementType.MessageOverride) : null; } + /// /// Sends an announcement audio /// @@ -68,8 +78,8 @@ private string GetAnnouncementPath(string announcerId, string announcementId) /// Who hears the announcement audio public void SendAnnouncementAudio(string announcementId, Filter filter) { - var announcement = GetAnnouncementPath(Announcer.ID, announcementId.ToLower()); - _audioSystem.PlayGlobal(announcement, filter, true, GetAudioParams(Announcer.ID, announcementId.ToLower())); + var announcement = GetAnnouncementPath(announcementId.ToLower()); + _audioSystem.PlayGlobal(announcement, filter, true, GetAudioParams(announcementId.ToLower())); } /// @@ -84,18 +94,16 @@ public void SendAnnouncementMessage(string announcementId, string message, strin { sender ??= Announcer.Name; - var announcementMessage = GetAnnouncementMessage(Announcer.ID, announcementId.ToLower()); - if (announcementMessage != null) + // If the announcement has a message override, use that instead of the message parameter + if (GetAnnouncementMessage(announcementId) is { } announcementMessage) message = announcementMessage; + // If there is a station, send the announcement to the station, otherwise send it to everyone if (station == null) - { _chatSystem.DispatchGlobalAnnouncement(message, sender, false, colorOverride: colorOverride); - } else - { - _chatSystem.DispatchStationAnnouncement(station.Value, message, sender, false, colorOverride: colorOverride); - } + _chatSystem.DispatchStationAnnouncement(station.Value, message, sender, false, + colorOverride: colorOverride); } /// diff --git a/Content.Shared/SimpleStation14/Announcements/Prototypes/AnnouncerPrototype.cs b/Content.Shared/SimpleStation14/Announcements/Prototypes/AnnouncerPrototype.cs index 777fec672f..46c3b72bfe 100644 --- a/Content.Shared/SimpleStation14/Announcements/Prototypes/AnnouncerPrototype.cs +++ b/Content.Shared/SimpleStation14/Announcements/Prototypes/AnnouncerPrototype.cs @@ -12,12 +12,23 @@ public sealed class AnnouncerPrototype : IPrototype [IdDataField] public string ID { get; } = default!; + /// Not localized due to globalization [DataField("name")] public string Name { get; } = default!; + /// + /// A prefix to add to all announcement paths unless told not to by + /// + /// Paths always start in Resources/ [DataField("basePath")] public string BasePath { get; } = default!; + /// + /// Audio parameters to apply to all announcement sounds unless overwritten by + /// + [DataField("baseAudioParams")] + public AudioParams? BaseAudioParams { get; } + [DataField("announcementPaths")] public AnnouncementData[] AnnouncementPaths { get; } = default!; } @@ -29,17 +40,36 @@ public sealed class AnnouncerPrototype : IPrototype public sealed partial class AnnouncementData { [DataField("id")] - public string ID { get; set; } = default!; + public string ID = default!; + + /// + /// If true, the will not be prepended to this announcement's path + /// + [DataField("ignoreBasePath")] + public bool IgnoreBasePath = false; + /// + /// Where to look for the announcement audio file + /// [DataField("path")] - public string? Path { get; set; } + public string? Path; - [DataField("collection")] - public string? Collection { get; set; } + /// + /// Use a soundCollection instead of a single sound + /// + [DataField("collection"), ValidatePrototypeId] + public string? Collection; + /// + /// Overrides the default announcement message for this announcement type + /// [DataField("message")] - public string? MessageOverride { get; set; } + public string? MessageOverride; + /// + /// Audio parameters to apply to this announcement sound + /// Will override + /// [DataField("audioParams")] - public AudioParams? AudioParams { get; set; } + public AudioParams? AudioParams; }