From 127cc28af34d198adf73f8060299be148665fa4d Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Sat, 24 Feb 2018 19:41:28 +0200 Subject: [PATCH] SoundPlayer.GetSound doesn't attempt to load the sound if a sound with a matching tag is not found, because otherwise configuring limb hit sounds and such with the file path instead of the tag will cause a new sound instance to be created every time the sound is played (and these instances are never freed). + Sound config files can be now configured in content packages. --- .../Source/Sounds/SoundPlayer.cs | 63 ++++++++++--------- .../Data/ContentPackages/Vanilla 0.8.xml | 1 + .../BarotraumaShared/Source/ContentPackage.cs | 3 +- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs b/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs index 2b2649893..012f986db 100644 --- a/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs +++ b/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs @@ -92,10 +92,19 @@ namespace Barotrauma { OverrideMusicType = null; - XDocument doc = XMLExtensions.TryLoadXml("Content/Sounds/sounds.xml"); - if (doc == null) yield return CoroutineStatus.Failure; + List soundFiles = GameMain.Config.SelectedContentPackage.GetFilesOfType(ContentType.Sounds); - SoundCount = 16 + doc.Root.Elements().Count(); + List soundElements = new List(); + foreach (string soundFile in soundFiles) + { + XDocument doc = XMLExtensions.TryLoadXml(soundFile); + if (doc != null && doc.Root != null) + { + soundElements.AddRange(doc.Root.Elements()); + } + } + + SoundCount = 16 + soundElements.Count(); startDrone = Sound.Load("Content/Sounds/startDrone.ogg", false); startDrone.Play(); @@ -113,61 +122,59 @@ namespace Barotrauma flowSounds[2] = Sound.Load("Content/Sounds/Water/FlowLarge.ogg", false); yield return CoroutineStatus.Running; - for (int i = 0; i < 10; i++ ) + for (int j = 0; j < 10; j++) { - SplashSounds[i] = Sound.Load("Content/Sounds/Water/Splash"+(i)+".ogg", false); + SplashSounds[j] = Sound.Load("Content/Sounds/Water/Splash" + j + ".ogg", false); yield return CoroutineStatus.Running; } - var xMusic = doc.Root.Elements("music").ToList(); - - if (xMusic.Any()) + var musicElements = soundElements.FindAll(e => e.Name.ToString().ToLowerInvariant() == "music"); + + musicClips = new BackgroundMusic[musicElements.Count]; + int i = 0; + foreach (XElement element in musicElements) { - musicClips = new BackgroundMusic[xMusic.Count]; - int i = 0; - foreach (XElement element in xMusic) - { - string file = element.GetAttributeString("file", ""); - string type = element.GetAttributeString("type", "").ToLowerInvariant(); - Vector2 priority = element.GetAttributeVector2("priorityrange", new Vector2(0.0f, 100.0f)); + string file = element.GetAttributeString("file", ""); + string type = element.GetAttributeString("type", "").ToLowerInvariant(); + Vector2 priority = element.GetAttributeVector2("priorityrange", new Vector2(0.0f, 100.0f)); - musicClips[i] = new BackgroundMusic(file, type, priority); + musicClips[i] = new BackgroundMusic(file, type, priority); - yield return CoroutineStatus.Running; + yield return CoroutineStatus.Running; - i++; - } + i++; } + List> miscSoundList = new List>(); damageSounds = new List(); - foreach (XElement subElement in doc.Root.Elements()) + foreach (XElement soundElement in soundElements) { yield return CoroutineStatus.Running; - switch (subElement.Name.ToString().ToLowerInvariant()) + switch (soundElement.Name.ToString().ToLowerInvariant()) { case "music": continue; case "damagesound": - Sound damageSound = Sound.Load(subElement.GetAttributeString("file", ""), false); + Sound damageSound = Sound.Load(soundElement.GetAttributeString("file", ""), false); if (damageSound == null) continue; - string damageSoundType = subElement.GetAttributeString("damagesoundtype", "None"); + string damageSoundType = soundElement.GetAttributeString("damagesoundtype", "None"); damageSounds.Add(new DamageSound( damageSound, - subElement.GetAttributeVector2("damagerange", new Vector2(0.0f, 100.0f)), + soundElement.GetAttributeVector2("damagerange", new Vector2(0.0f, 100.0f)), damageSoundType, - subElement.GetAttributeString("requiredtag", ""))); + soundElement.GetAttributeString("requiredtag", ""))); break; default: - Sound sound = Sound.Load(subElement.GetAttributeString("file", ""), false); + Sound sound = Sound.Load(soundElement.GetAttributeString("file", ""), false); if (sound != null) { - miscSoundList.Add(new KeyValuePair(subElement.Name.ToString().ToLowerInvariant(), sound)); + miscSoundList.Add(new KeyValuePair(soundElement.Name.ToString().ToLowerInvariant(), sound)); } break; @@ -263,7 +270,7 @@ namespace Barotrauma public static Sound GetSound(string soundTag) { var matchingSounds = miscSounds[soundTag].ToList(); - if (matchingSounds.Count == 0) return Sound.Load(soundTag); + if (matchingSounds.Count == 0) return null; return matchingSounds[Rand.Int(matchingSounds.Count)]; } diff --git a/Barotrauma/BarotraumaShared/Data/ContentPackages/Vanilla 0.8.xml b/Barotrauma/BarotraumaShared/Data/ContentPackages/Vanilla 0.8.xml index 0bf2cb51c..47757fe05 100644 --- a/Barotrauma/BarotraumaShared/Data/ContentPackages/Vanilla 0.8.xml +++ b/Barotrauma/BarotraumaShared/Data/ContentPackages/Vanilla 0.8.xml @@ -52,5 +52,6 @@ + diff --git a/Barotrauma/BarotraumaShared/Source/ContentPackage.cs b/Barotrauma/BarotraumaShared/Source/ContentPackage.cs index 1583a4cf3..7297ffc9d 100644 --- a/Barotrauma/BarotraumaShared/Source/ContentPackage.cs +++ b/Barotrauma/BarotraumaShared/Source/ContentPackage.cs @@ -18,7 +18,8 @@ namespace Barotrauma LevelGenerationParameters, RandomEvents, Missions, - BackgroundCreaturePrefabs, BackgroundSpritePrefabs + BackgroundCreaturePrefabs, BackgroundSpritePrefabs, + Sounds } public class ContentPackage