Ejemplo n.º 1
0
        public MapInfo(ByteReader reader)
        {
            MapName = reader.String(12, '\0');
            MapName = MapName.ToLowerInvariant();
            Xs      = reader.UInt16();
            Ys      = reader.UInt16();
            int len = reader.Int32();

            Data = reader.Bytes(len);
        }
Ejemplo n.º 2
0
        private static List <WadEntry> ReadEntriesOrThrow(byte[] wadData)
        {
            ByteReader reader = ByteReader.From(ByteOrder.Little, wadData);

            // The header must exist fully or else we can't read it.
            if (!reader.HasRemaining(12))
            {
                throw new Exception("Cannot read Wad");
            }

            WadResourceNamespaceTracker namespaceTracker = new WadResourceNamespaceTracker();
            List <WadEntry>             entries          = new List <WadEntry>();

            WadHeader header = ReadHeader(reader);

            reader.Offset = header.DirectoryTableOffset;

            for (int i = 0; i < header.EntryCount; i++)
            {
                WadDirectoryEntry dirEntry = ReadWadEntry(reader);

                ResourceNamespace resourceNamespace = namespaceTracker.Update(dirEntry);
                byte[]            data = reader.Bytes(dirEntry.Size, dirEntry.Offset);

                // Unfortunately binary readers can silently fail and just
                // consume the remaining data. We want to let the caller
                // know the wad is in fact corrupt if it can't read enough.
                if (data.Length != dirEntry.Size)
                {
                    throw new Exception("Malformed wad entry length");
                }

                WadEntry entry = new WadEntry(dirEntry.Name, resourceNamespace, data);
                entries.Add(entry);
            }

            return(entries);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a palette image from a column-based image.
        /// </summary>
        /// <param name="data">The data for the image.</param>
        /// <param name="resourceNamespace">The namespace this image belongs
        /// to.</param>
        /// <returns>The palette image for the data, or null if the image is
        /// corrupt and would not make a valid column image.</returns>
        public static Optional <PaletteImage> FromColumn(byte[] data, ResourceNamespace resourceNamespace)
        {
            // OPTIMIZE: Short circuit if it's likely not a column image.
            // OPTIMIZE: Write posts horizontally, then rotate for speed?
            try
            {
                ByteReader reader = ByteReader.From(ByteOrder.Little, data);
                int        width  = reader.Short();
                int        height = reader.Short();
                Vec2I      offset = (reader.Short(), reader.Short());

                if (offset.X < 0 || offset.Y < 0)
                {
                    return(Empty);
                }

                int[] offsets = new int[width];
                for (int i = 0; i < width; i++)
                {
                    offsets[i] = reader.Int();
                }

                short[] indices = Arrays.Create(width * height, TransparentIndex);

                for (int col = 0; col < width; col++)
                {
                    reader.Offset = offsets[col];

                    while (true)
                    {
                        int rowStart = reader.Byte();
                        if (rowStart == 0xFF)
                        {
                            break;
                        }

                        int indicesCount = reader.Byte();
                        reader.Skip(1);
                        byte[] paletteIndices = reader.Bytes(indicesCount);
                        reader.Skip(1);

                        int indicesOffset = (rowStart * width) + col;
                        for (int i = 0; i < paletteIndices.Length; i++)
                        {
                            indices[indicesOffset] = paletteIndices[i];
                            indicesOffset         += width;
                        }
                    }
                }

                return(new PaletteImage(width, height, indices)
                {
                    Namespace = resourceNamespace,
                    Offset = offset
                });
            }
            catch
            {
                return(Empty);
            }
        }