From 6fb228ddd18e09817bb733d258e13f79c41923b7 Mon Sep 17 00:00:00 2001 From: penev92 Date: Tue, 4 Jan 2022 23:58:55 +0200 Subject: [PATCH] Fixed Mp3Loader and OggLoader not resetting stream position --- OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs | 28 +++++++++++++------- OpenRA.Mods.Common/AudioLoaders/OggLoader.cs | 16 ++++++++--- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs b/OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs index 0df368f9a4..afb1271139 100644 --- a/OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs @@ -49,19 +49,27 @@ namespace OpenRA.Mods.Common.AudioLoaders public Mp3Format(Stream stream) { - mp3 = new MP3Stream(stream); - this.stream = stream; - - // Make a first guess based on the file size and bitrate - // This should be fine for constant bitrate files - LengthInSeconds = mp3.Length * 8f / (2f * Channels * SampleRate); - + var startPosition = stream.Position; try { - // Attempt to parse a more accurate length from the file metadata; - LengthInSeconds = (float)new TagLib.Mpeg.AudioFile(new StreamAbstraction(stream)).Properties.Duration.TotalSeconds; + mp3 = new MP3Stream(stream); + this.stream = stream; + + // Make a first guess based on the file size and bitrate + // This should be fine for constant bitrate files + LengthInSeconds = mp3.Length * 8f / (2f * Channels * SampleRate); + + try + { + // Attempt to parse a more accurate length from the file metadata; + LengthInSeconds = (float)new TagLib.Mpeg.AudioFile(new StreamAbstraction(stream)).Properties.Duration.TotalSeconds; + } + catch { } + } + finally + { + stream.Position = startPosition; } - catch { } } Stream Clone(Mp3Format cloneFrom) diff --git a/OpenRA.Mods.Common/AudioLoaders/OggLoader.cs b/OpenRA.Mods.Common/AudioLoaders/OggLoader.cs index c4c11fe84a..27375694c4 100644 --- a/OpenRA.Mods.Common/AudioLoaders/OggLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/OggLoader.cs @@ -49,15 +49,23 @@ namespace OpenRA.Mods.Common.AudioLoaders public OggFormat(Stream stream) { - this.stream = stream; - reader = new VorbisReader(stream); - LengthInSeconds = (float) reader.TotalTime.TotalSeconds; + var startPosition = stream.Position; + try + { + this.stream = stream; + reader = new VorbisReader(stream, false); + LengthInSeconds = (float)reader.TotalTime.TotalSeconds; + } + finally + { + stream.Position = startPosition; + } } OggFormat(OggFormat cloneFrom) { stream = SegmentStream.CreateWithoutOwningStream(cloneFrom.stream, 0, (int)cloneFrom.stream.Length); - reader = new VorbisReader(stream) + reader = new VorbisReader(stream, false) { // Tell NVorbis to clip samples so we don't have to range-check during reading. ClipSamples = true