3 using Microsoft.Xna.Framework;
5 using System.Collections.Generic;
6 using System.Globalization;
32 if (!freqMultAttr.Contains(
','))
34 if (
float.TryParse(freqMultAttr, NumberStyles.Any, CultureInfo.InvariantCulture, out
float freqMult))
41 var freqMult = XMLExtensions.ParseVector2(freqMultAttr,
false);
42 if (freqMult.Y >= 0.25f)
49 DebugConsole.ThrowError($
"Loaded frequency range exceeds max value: {FrequencyMultiplierRange} (original string was \"{freqMultAttr}\")",
59 private static readonly List<RoundSound> roundSounds =
new List<RoundSound>();
60 private static readonly Dictionary<string, RoundSound> roundSoundByPath =
new Dictionary<string, RoundSound>();
68 string errorMsg =
"Error when loading round sound (" + element +
") - file path not set";
69 DebugConsole.ThrowError(errorMsg,
71 GameAnalyticsManager.AddErrorEventOnce(
"RoundSound.LoadRoundSound:FilePathEmpty" + element.ToString(), GameAnalyticsManager.ErrorSeverity.Error, errorMsg +
"\n" + Environment.StackTrace.CleanupStackTrace());
75 Sound? existingSound =
null;
76 if (roundSoundByPath.TryGetValue(filename.FullPath, out
RoundSound? rs))
78 if (rs.Sound is { Disposed: false })
80 existingSound = rs.
Sound;
84 roundSoundByPath.Remove(filename.FullPath);
88 if (existingSound is
null)
93 if (existingSound ==
null) {
return null; }
95 catch (System.IO.FileNotFoundException e)
97 string errorMsg =
"Failed to load sound file \"" + filename +
"\" (file not found).";
98 DebugConsole.ThrowError(errorMsg, e,
100 if (!ContentPackageManager.ModsEnabled)
102 GameAnalyticsManager.AddErrorEventOnce(
"RoundSound.LoadRoundSound:FileNotFound" + filename, GameAnalyticsManager.ErrorSeverity.Error, errorMsg +
"\n" + Environment.StackTrace.CleanupStackTrace());
106 catch (System.IO.InvalidDataException e)
108 string errorMsg =
"Failed to load sound file \"" + filename +
"\" (invalid data).";
109 DebugConsole.ThrowError(errorMsg, e,
111 GameAnalyticsManager.AddErrorEventOnce(
"RoundSound.LoadRoundSound:InvalidData" + filename, GameAnalyticsManager.ErrorSeverity.Error, errorMsg +
"\n" + Environment.StackTrace.CleanupStackTrace());
117 if (filename is not
null && !newSound.
Stream)
119 roundSoundByPath.TryAdd(filename.FullPath, newSound);
121 roundSounds.Add(newSound);
127 Sound? existingSound = roundSounds.Find(s => s.Filename == roundSound.
Filename && s.Stream == roundSound.
Stream && s.Sound is { Disposed: false })?.
Sound;
128 if (existingSound ==
null)
134 catch (System.IO.FileNotFoundException e)
136 string errorMsg =
"Failed to load sound file \"" + roundSound.
Filename +
"\".";
137 DebugConsole.ThrowError(errorMsg, e,
139 GameAnalyticsManager.AddErrorEventOnce(
"RoundSound.LoadRoundSound:FileNotFound" + roundSound.
Filename, GameAnalyticsManager.ErrorSeverity.Error, errorMsg +
"\n" + Environment.StackTrace.CleanupStackTrace());
143 roundSound.
Sound = existingSound;
148 foreach (var roundSound
in roundSounds)
150 roundSound.Sound?.Dispose();
153 roundSoundByPath.Clear();
string? GetAttributeString(string key, string? def)
float GetAttributeFloat(string key, float def)
ContentPackage? ContentPackage
ContentPath? GetAttributeContentPath(string key)
bool GetAttributeBool(string key, bool def)
static Sounds.SoundManager SoundManager
static ? RoundSound Load(ContentXElement element, bool stream=false)
static void Reload(RoundSound roundSound)
readonly Vector2 FrequencyMultiplierRange
static void RemoveAllRoundSounds()
float GetRandomFrequencyMultiplier()
readonly? string Filename
readonly bool IgnoreMuffling
Sound(SoundManager owner, string filename, bool stream, bool streamsReliably, ContentXElement xElement=null, bool getFullPath=true)
readonly ContentXElement XElement
Sound LoadSound(string filename, bool stream=false)