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.

This commit is contained in:
Joonas Rikkonen
2018-02-24 19:41:28 +02:00
parent 313fb35b4a
commit 127cc28af3
3 changed files with 38 additions and 29 deletions
@@ -92,10 +92,19 @@ namespace Barotrauma
{ {
OverrideMusicType = null; OverrideMusicType = null;
XDocument doc = XMLExtensions.TryLoadXml("Content/Sounds/sounds.xml"); List<string> soundFiles = GameMain.Config.SelectedContentPackage.GetFilesOfType(ContentType.Sounds);
if (doc == null) yield return CoroutineStatus.Failure;
SoundCount = 16 + doc.Root.Elements().Count(); List<XElement> soundElements = new List<XElement>();
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 = Sound.Load("Content/Sounds/startDrone.ogg", false);
startDrone.Play(); startDrone.Play();
@@ -113,61 +122,59 @@ namespace Barotrauma
flowSounds[2] = Sound.Load("Content/Sounds/Water/FlowLarge.ogg", false); flowSounds[2] = Sound.Load("Content/Sounds/Water/FlowLarge.ogg", false);
yield return CoroutineStatus.Running; 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; yield return CoroutineStatus.Running;
} }
var xMusic = doc.Root.Elements("music").ToList(); var musicElements = soundElements.FindAll(e => e.Name.ToString().ToLowerInvariant() == "music");
if (xMusic.Any()) musicClips = new BackgroundMusic[musicElements.Count];
int i = 0;
foreach (XElement element in musicElements)
{ {
musicClips = new BackgroundMusic[xMusic.Count]; string file = element.GetAttributeString("file", "");
int i = 0; string type = element.GetAttributeString("type", "").ToLowerInvariant();
foreach (XElement element in xMusic) 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<KeyValuePair<string, Sound>> miscSoundList = new List<KeyValuePair<string, Sound>>(); List<KeyValuePair<string, Sound>> miscSoundList = new List<KeyValuePair<string, Sound>>();
damageSounds = new List<DamageSound>(); damageSounds = new List<DamageSound>();
foreach (XElement subElement in doc.Root.Elements()) foreach (XElement soundElement in soundElements)
{ {
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
switch (subElement.Name.ToString().ToLowerInvariant()) switch (soundElement.Name.ToString().ToLowerInvariant())
{ {
case "music": case "music":
continue; continue;
case "damagesound": case "damagesound":
Sound damageSound = Sound.Load(subElement.GetAttributeString("file", ""), false); Sound damageSound = Sound.Load(soundElement.GetAttributeString("file", ""), false);
if (damageSound == null) continue; if (damageSound == null) continue;
string damageSoundType = subElement.GetAttributeString("damagesoundtype", "None"); string damageSoundType = soundElement.GetAttributeString("damagesoundtype", "None");
damageSounds.Add(new DamageSound( damageSounds.Add(new DamageSound(
damageSound, damageSound,
subElement.GetAttributeVector2("damagerange", new Vector2(0.0f, 100.0f)), soundElement.GetAttributeVector2("damagerange", new Vector2(0.0f, 100.0f)),
damageSoundType, damageSoundType,
subElement.GetAttributeString("requiredtag", ""))); soundElement.GetAttributeString("requiredtag", "")));
break; break;
default: default:
Sound sound = Sound.Load(subElement.GetAttributeString("file", ""), false); Sound sound = Sound.Load(soundElement.GetAttributeString("file", ""), false);
if (sound != null) if (sound != null)
{ {
miscSoundList.Add(new KeyValuePair<string, Sound>(subElement.Name.ToString().ToLowerInvariant(), sound)); miscSoundList.Add(new KeyValuePair<string, Sound>(soundElement.Name.ToString().ToLowerInvariant(), sound));
} }
break; break;
@@ -263,7 +270,7 @@ namespace Barotrauma
public static Sound GetSound(string soundTag) public static Sound GetSound(string soundTag)
{ {
var matchingSounds = miscSounds[soundTag].ToList(); 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)]; return matchingSounds[Rand.Int(matchingSounds.Count)];
} }
@@ -52,5 +52,6 @@
<LevelGenerationParameters file="Content/Map/LevelGenerationParameters.xml" /> <LevelGenerationParameters file="Content/Map/LevelGenerationParameters.xml" />
<Missions file="Content/Missions.xml" /> <Missions file="Content/Missions.xml" />
<Jobs file="Content/Jobs.xml" /> <Jobs file="Content/Jobs.xml" />
<Sounds file="Content/Sounds/sounds.xml" />
<Executable file="Barotrauma.exe" /> <Executable file="Barotrauma.exe" />
</contentpackage> </contentpackage>
@@ -18,7 +18,8 @@ namespace Barotrauma
LevelGenerationParameters, LevelGenerationParameters,
RandomEvents, RandomEvents,
Missions, Missions,
BackgroundCreaturePrefabs, BackgroundSpritePrefabs BackgroundCreaturePrefabs, BackgroundSpritePrefabs,
Sounds
} }
public class ContentPackage public class ContentPackage