diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 402f4abc06..e0ac621eb0 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -161,6 +161,7 @@ + diff --git a/OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs b/OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs new file mode 100644 index 0000000000..f53450a694 --- /dev/null +++ b/OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs @@ -0,0 +1,89 @@ +#region Copyright & License Information +/* + * Copyright 2007-2017 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.IO; + +namespace OpenRA.Primitives +{ + /// + /// Provides a read-only buffering layer so data can be streamed from sources where reading arbitrary amounts of + /// data is difficult. + /// + public abstract class ReadOnlyAdapterStream : Stream + { + readonly Queue data = new Queue(1024); + readonly Stream baseStream; + + protected ReadOnlyAdapterStream(Stream stream) + { + if (stream == null) + throw new ArgumentNullException("stream"); + if (!stream.CanRead) + throw new ArgumentException("stream must be readable.", "stream"); + + baseStream = stream; + } + + public sealed override bool CanSeek { get { return false; } } + public sealed override bool CanRead { get { return true; } } + public sealed override bool CanWrite { get { return false; } } + + public sealed override long Length { get { throw new NotSupportedException(); } } + public sealed override long Position + { + get { throw new NotSupportedException(); } + set { throw new NotSupportedException(); } + } + + public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } + public sealed override void SetLength(long value) { throw new NotSupportedException(); } + public sealed override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); } + public sealed override void Flush() { throw new NotSupportedException(); } + + public sealed override int Read(byte[] buffer, int offset, int count) + { + var copied = 0; + ConsumeData(buffer, offset, count, ref copied); + + var finished = false; + while (copied < count && !finished) + { + finished = BufferData(baseStream, data); + ConsumeData(buffer, offset, count, ref copied); + } + + return copied; + } + + /// + /// Reads data into a buffer, which will be used to satisfy calls. + /// + /// The source stream from which bytes should be read. + /// The queue where bytes should be enqueued. Do not dequeue from this buffer. + /// Return true if all data has been read; otherwise, false. + protected abstract bool BufferData(Stream baseStream, Queue data); + + void ConsumeData(byte[] buffer, int offset, int count, ref int copied) + { + while (copied < count && data.Count > 0) + buffer[offset + copied++] = data.Dequeue(); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + baseStream.Dispose(); + base.Dispose(disposing); + } + } +} diff --git a/OpenRA.Game/Primitives/SegmentStream.cs b/OpenRA.Game/Primitives/SegmentStream.cs index 3e4e21bbad..7192dbad71 100644 --- a/OpenRA.Game/Primitives/SegmentStream.cs +++ b/OpenRA.Game/Primitives/SegmentStream.cs @@ -21,6 +21,15 @@ namespace OpenRA.Primitives public readonly long BaseOffset; public readonly long BaseCount; + /// + /// Creates a new that wraps a subset of the source stream. This takes ownership of + /// the source stream. The is dependent on the source and changes its underlying + /// position. + /// + /// The source stream, of which only a segment should be exposed. Ownership is transferred + /// to the . + /// The offset at which the segment starts. + /// The length of the segment. public SegmentStream(Stream stream, long offset, long count) { if (stream == null) @@ -90,7 +99,7 @@ namespace OpenRA.Primitives base.Dispose(disposing); } - public static long GetOverallNestedOffset(Stream stream, out Stream overallBaseStream) + static long GetOverallNestedOffset(Stream stream, out Stream overallBaseStream) { var offset = 0L; overallBaseStream = stream; @@ -99,5 +108,34 @@ namespace OpenRA.Primitives offset += segmentStream.BaseOffset + GetOverallNestedOffset(segmentStream.BaseStream, out overallBaseStream); return offset; } + + /// + /// Creates a new that wraps a subset of the source stream without taking ownership of it, + /// allowing it to be reused by the caller. The is independent of the source stream and + /// won't affect its position. + /// + /// The source stream, of which only a segment should be exposed. Ownership is retained by + /// the caller. + /// The offset at which the segment starts. + /// The length of the segment. + public static Stream CreateWithoutOwningStream(Stream stream, long offset, int count) + { + Stream parentStream; + var nestedOffset = offset + GetOverallNestedOffset(stream, out parentStream); + + // Special case FileStream - instead of creating an in-memory copy, + // just reference the portion of the on-disk file that we need to save memory. + // We use GetType instead of 'is' here since we can't handle any derived classes of FileStream. + if (parentStream.GetType() == typeof(FileStream)) + { + var path = ((FileStream)parentStream).Name; + return new SegmentStream(File.OpenRead(path), nestedOffset, count); + } + + // For all other streams, create a copy in memory. + // This uses more memory but is the only way in general to ensure the returned streams won't clash. + stream.Seek(offset, SeekOrigin.Begin); + return new MemoryStream(stream.ReadBytes(count)); + } } } diff --git a/OpenRA.Game/StreamExts.cs b/OpenRA.Game/StreamExts.cs index 77a696944e..779c26f330 100644 --- a/OpenRA.Game/StreamExts.cs +++ b/OpenRA.Game/StreamExts.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; namespace OpenRA @@ -117,7 +118,17 @@ namespace OpenRA public static byte[] ReadAllBytes(this Stream s) { using (s) - return s.ReadBytes((int)(s.Length - s.Position)); + { + if (s.CanSeek) + return s.ReadBytes((int)(s.Length - s.Position)); + + var bytes = new List(); + var buffer = new byte[1024]; + int count; + while ((count = s.Read(buffer, 0, buffer.Length)) > 0) + bytes.AddRange(buffer.Take(count)); + return bytes.ToArray(); + } } public static void Write(this Stream s, byte[] data) diff --git a/OpenRA.Mods.Cnc/FileSystem/MixFile.cs b/OpenRA.Mods.Cnc/FileSystem/MixFile.cs index d8ed30bd3f..3fab1ccb94 100644 --- a/OpenRA.Mods.Cnc/FileSystem/MixFile.cs +++ b/OpenRA.Mods.Cnc/FileSystem/MixFile.cs @@ -183,23 +183,7 @@ namespace OpenRA.Mods.Cnc.FileSystem public Stream GetContent(PackageEntry entry) { - Stream parentStream; - var baseOffset = dataStart + entry.Offset; - var nestedOffset = baseOffset + SegmentStream.GetOverallNestedOffset(s, out parentStream); - - // Special case FileStream - instead of creating an in-memory copy, - // just reference the portion of the on-disk file that we need to save memory. - // We use GetType instead of 'is' here since we can't handle any derived classes of FileStream. - if (parentStream.GetType() == typeof(FileStream)) - { - var path = ((FileStream)parentStream).Name; - return new SegmentStream(File.OpenRead(path), nestedOffset, entry.Length); - } - - // For all other streams, create a copy in memory. - // This uses more memory but is the only way in general to ensure the returned streams won't clash. - s.Seek(baseOffset, SeekOrigin.Begin); - return new MemoryStream(s.ReadBytes((int)entry.Length)); + return SegmentStream.CreateWithoutOwningStream(s, dataStart + entry.Offset, (int)entry.Length); } public Stream GetStream(string filename) diff --git a/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs b/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs index bd5828bd7a..29fcb91158 100644 --- a/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs +++ b/OpenRA.Mods.Common/AudioLoaders/AudLoader.cs @@ -56,34 +56,20 @@ namespace OpenRA.Mods.Common.AudioLoaders public int Channels { get { return 1; } } public int SampleBits { get { return 16; } } public int SampleRate { get { return sampleRate; } } - public float LengthInSeconds { get { return AudReader.SoundLength(stream); } } - public Stream GetPCMInputStream() { return new MemoryStream(rawData.Value); } - public void Dispose() { stream.Dispose(); } + public float LengthInSeconds { get { return AudReader.SoundLength(sourceStream); } } + public Stream GetPCMInputStream() { return audStreamFactory(); } + public void Dispose() { sourceStream.Dispose(); } - int sampleRate; - Lazy rawData; - - Stream stream; + readonly Stream sourceStream; + readonly Func audStreamFactory; + readonly int sampleRate; public AudFormat(Stream stream) { - this.stream = stream; + sourceStream = stream; - var position = stream.Position; - rawData = Exts.Lazy(() => - { - try - { - byte[] data; - if (!AudReader.LoadSound(stream, out data, out sampleRate)) - throw new InvalidDataException(); - return data; - } - finally - { - stream.Position = position; - } - }); + if (!AudReader.LoadSound(stream, out audStreamFactory, out sampleRate)) + throw new InvalidDataException(); } } } diff --git a/OpenRA.Mods.Common/FileFormats/AudReader.cs b/OpenRA.Mods.Common/FileFormats/AudReader.cs index 43bb81f031..5f98e5bed0 100644 --- a/OpenRA.Mods.Common/FileFormats/AudReader.cs +++ b/OpenRA.Mods.Common/FileFormats/AudReader.cs @@ -10,7 +10,9 @@ #endregion using System; +using System.Collections.Generic; using System.IO; +using OpenRA.Primitives; namespace OpenRA.Mods.Common.FileFormats { @@ -44,7 +46,7 @@ namespace OpenRA.Mods.Common.FileFormats } } - public class AudReader + public static class AudReader { static readonly int[] IndexAdjust = { -1, -1, -1, -1, 2, 4, 6, 8 }; static readonly int[] StepTable = @@ -116,55 +118,87 @@ namespace OpenRA.Mods.Common.FileFormats var samples = outputSize; if (0 != (flags & SoundFlags.Stereo)) samples /= 2; if (0 != (flags & SoundFlags._16Bit)) samples /= 2; - return samples / sampleRate; + return (float)samples / sampleRate; } - public static bool LoadSound(Stream s, out byte[] rawData, out int sampleRate) + public static bool LoadSound(Stream s, out Func result, out int sampleRate) { - rawData = null; - - sampleRate = s.ReadUInt16(); - var dataSize = s.ReadInt32(); - var outputSize = s.ReadInt32(); - - var readFlag = s.ReadByte(); - if (!Enum.IsDefined(typeof(SoundFlags), readFlag)) - return false; - - var readFormat = s.ReadByte(); - if (!Enum.IsDefined(typeof(SoundFormat), readFormat)) - return false; - - var output = new byte[outputSize]; - var offset = 0; - var index = 0; - var currentSample = 0; - - while (dataSize > 0) + result = null; + var startPosition = s.Position; + try { - var chunk = Chunk.Read(s); + sampleRate = s.ReadUInt16(); + var dataSize = s.ReadInt32(); + var outputSize = s.ReadInt32(); + + var readFlag = s.ReadByte(); + if (!Enum.IsDefined(typeof(SoundFlags), readFlag)) + return false; + + var readFormat = s.ReadByte(); + if (!Enum.IsDefined(typeof(SoundFormat), readFormat)) + return false; + + var offsetPosition = s.Position; + + result = () => + { + var audioStream = SegmentStream.CreateWithoutOwningStream(s, offsetPosition, (int)(s.Length - offsetPosition)); + return new AudStream(audioStream, outputSize, dataSize); + }; + } + finally + { + s.Position = startPosition; + } + + return true; + } + + sealed class AudStream : ReadOnlyAdapterStream + { + readonly int outputSize; + int dataSize; + + int currentSample; + int baseOffset; + int index; + + public AudStream(Stream stream, int outputSize, int dataSize) : base(stream) + { + this.outputSize = outputSize; + this.dataSize = dataSize; + } + + protected override bool BufferData(Stream baseStream, Queue data) + { + if (dataSize <= 0) + return true; + + var chunk = Chunk.Read(baseStream); for (var n = 0; n < chunk.CompressedSize; n++) { - var b = s.ReadUInt8(); + var b = baseStream.ReadUInt8(); var t = DecodeSample(b, ref index, ref currentSample); - output[offset++] = (byte)t; - output[offset++] = (byte)(t >> 8); + data.Enqueue((byte)t); + data.Enqueue((byte)(t >> 8)); + baseOffset += 2; - if (offset < outputSize) + if (baseOffset < outputSize) { /* possible that only half of the final byte is used! */ t = DecodeSample((byte)(b >> 4), ref index, ref currentSample); - output[offset++] = (byte)t; - output[offset++] = (byte)(t >> 8); + data.Enqueue((byte)t); + data.Enqueue((byte)(t >> 8)); + baseOffset += 2; } } dataSize -= 8 + chunk.CompressedSize; - } - rawData = output; - return true; + return dataSize <= 0; + } } } }