Exemplo n.º 1
0
        /// <summary>
        /// Creates a new <see cref="PakFile"/> instance and loads the package from the source stream.
        /// </summary>
        /// <param name="stream">The source stream that contains the package data.</param>
        /// <exception cref="PakException">
        /// Thrown when the specified <paramref name="stream"/> could not be loaded.
        /// </exception>
        /// <remarks>
        /// The source stream must support the ability to read and seek.
        /// </remarks>
        public PakFile(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream must be able to read", "stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream must be able to seek", "stream");
            }

            this.stream  = stream;
            this.map     = new HashMap <string, PakEntry>();
            this.entries = new ArrayList <PakEntry>();

            PakEntry   entry;
            DataStream dataStream = new DataStream(stream);

            while (stream.Position < stream.Length)
            {
                try
                {
                    entry = PakEntry.Deserialize(dataStream);
                }
                catch (Exception e)
                {
                    throw new PakException(e.Message);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>Returns a stream that provides access to the package entry data.</summary>
        /// <param name="name">The name of the package entry.</param>
        /// <returns>Returns a stream that provides access to the package entry data.</returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the specified <paramref name="name"/> could not be found.
        /// </exception>
        public SubStream GetEntryStream(string name)
        {
            if (!HasEntry(name))
            {
                throw new ArgumentException("Could not find entry", "name");
            }

            PakEntry entry = GetEntry(name);

            return(new SubStream(stream, entry.Offset, entry.Size));
        }
Exemplo n.º 3
0
        /// <summary>Returns a list of all entries found within the <paramref name="stream"/>.</summary>
        /// <param name="stream">The source stream that contains the package data.</param>
        /// <exception cref="PakException">
        /// Thrown when the specified <paramref name="stream"/> could not be loaded.
        /// </exception>
        /// <remarks>
        /// The source stream must support the ability to read and seek.
        /// </remarks>
        /// <returns>
        /// Returns a list of all entries found within the <paramref name="stream"/>.
        /// </returns>
        public static IList <PakEntry> GetEntries(Stream stream)
        {
            IList <PakEntry> entries    = new ArrayList <PakEntry>();
            DataStream       dataStream = new DataStream(stream);

            while (stream.Position < stream.Length)
            {
                try
                {
                    entries.Add(PakEntry.Deserialize(dataStream));
                }
                catch (Exception e)
                {
                    throw new PakException(e.Message);
                }
            }

            return(entries);
        }