Exemplo n.º 1
0
        /// <summary>
        /// Creates instances from a stream.
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="stream">The stream.</param>
        /// <param name="bufferSize">The custom buffer size to use to read from <paramref name="stream" />.</param>
        /// <returns>The list of lazy loaded instances.</returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="stream" /> is not readable.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="stream" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="bufferSize" /> is less than 1.
        /// </exception>
        public static IEnumerable <IWADFile> FromStream(Stream stream, WADFormat format = WADFormat.Default, int?bufferSize = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (stream.CanRead == false)
            {
                throw new ArgumentException("stream");
            }

            if (bufferSize < 1)
            {
                throw new ArgumentOutOfRangeException("bufferSize", bufferSize,
                                                      "Is less than 1!");
            }

            bool hasNext;

            do
            {
                hasNext = false;

                byte[] buffer;

                buffer = new byte[4];
                if (stream.Read(buffer, 0, buffer.Length) != buffer.Length)
                {
                    continue;
                }

                var header = Encoding.ASCII.GetString(buffer).ToUpper().Trim();

                IWADFile result = null;

                var wadStream = new MemoryStream();
                try
                {
                    Action copyToWadStream = () =>
                    {
                        if (!bufferSize.HasValue)
                        {
                            stream.CopyTo(wadStream);
                        }
                        else
                        {
                            stream.CopyTo(wadStream, bufferSize.Value);
                        }

                        wadStream.Position = 0;
                    };

                    switch (header)
                    {
                    case "IWAD":
                        copyToWadStream();

                        result = new IWAD(format, wadStream, true);
                        break;

                    case "PWAD":
                        copyToWadStream();

                        result = new PWAD(format, wadStream, true);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    wadStream.Dispose();

                    throw ex;
                }

                if (result != null)
                {
                    hasNext = true;

                    yield return(result);
                }
            }while (hasNext);
        }
Exemplo n.º 2
0
        public void FileSignatures_TestPWADFile()
        {
            PWAD testFile = new PWAD(Path.Combine(resourceDir, "FileSignature_Emulation_PWADFile.wad"));

            Assert.AreEqual(true, testFile.EntireFileIsValid);
        }