Exemplo n.º 1
0
        private void LoadLumpAt(string path, int index)
        {
            string       filename = Path.GetFileName(path);
            BinaryReader br;

            if (filename.Length > 12)
            {
                //trim the filename
                filename = filename.Remove(12);
            }
            br = new BinaryReader(File.Open(path, FileMode.Open));
            int size = (int)br.BaseStream.Length;

            byte[]  data    = br.ReadBytes((int)br.BaseStream.Length);
            HOGLump newLump = new HOGLump(filename, size, -1);

            newLump.Data = data;
            newLump.Type = HOGLump.IdentifyLump(newLump.Name, newLump.Data);
            //datafile.AddLump(newLump);

            ListViewItem lumpElement = new ListViewItem(newLump.Name);

            lumpElement.SubItems.Add(newLump.Size.ToString());
            lumpElement.SubItems.Add(newLump.Type.ToString());
            if (index != -1)
            {
                listView1.Items.Insert(index, lumpElement);
                datafile.AddLumpAt(newLump, index);
            }
            else
            {
                listView1.Items.Add(lumpElement);
                datafile.AddLump(newLump);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the data of a HOG file from a stream.
        /// </summary>
        /// <param name="stream">The stream to read the HOG file from.</param>
        public void Read(Stream stream)
        {
            lock (hogFileLock)
                lock (lumpLock)
                    lock (lumpNameLock)
                    {
                        BinaryReader br = new BinaryReader(stream);
                        fileStream = br;
                        lumps.Clear();

                        char[] header = new char[3];
                        header[0] = (char)br.ReadByte();
                        header[1] = (char)br.ReadByte();
                        header[2] = (char)br.ReadByte();

                        var headerString = new string(header);
                        switch (headerString)
                        {
                        case "DHF":
                            Format = HOGFormat.Standard;
                            break;

                        case "D2X":
                            Format = HOGFormat.D2X_XL;
                            break;

                        default:
                            throw new InvalidDataException($"Unrecognized HOG header \"{headerString}\"");
                        }

                        try
                        {
                            while (true)
                            {
                                char[] filenamedata = new char[13];
                                bool   hashitnull   = false;
                                for (int x = 0; x < 13; x++)
                                {
                                    char c = (char)br.ReadByte();
                                    if (c == 0)
                                    {
                                        hashitnull = true;
                                    }
                                    if (!hashitnull)
                                    {
                                        filenamedata[x] = c;
                                    }
                                }
                                string filename = new string(filenamedata);
                                filename = filename.Trim(' ', '\0');
                                int filesize = br.ReadInt32();
                                if (Format == HOGFormat.D2X_XL && filesize < 0)
                                {
                                    // D2X-XL format encodes "extended" lump headers with negative file sizes
                                    filesize = -filesize;

                                    string longFilename = Encoding.ASCII.GetString(br.ReadBytes(256));
                                    if (longFilename.Contains("\0"))
                                    {
                                        longFilename = longFilename.Remove(longFilename.IndexOf('\0'));
                                    }
                                    longFilename = longFilename.Trim(' ');
                                    // No real reason to use short filename in this instance; just replace it
                                    filename = longFilename;
                                }
                                int offset = (int)br.BaseStream.Position;
                                br.BaseStream.Seek(filesize, SeekOrigin.Current); //I hate hog files. Wads are cooler..

                                HOGLump lump = new HOGLump(filename, filesize, offset);
                                lumps.Add(lump);
                            }
                        }
                        catch (EndOfStreamException)
                        {
                            //we got all the files
                            //heh
                            //i love hog
                            byte[] data;
                            for (int i = 0; i < NumLumps; i++)
                            {
                                data          = GetLumpData(i);
                                lumps[i].Type = HOGLump.IdentifyLump(lumps[i].Name, data);
                            }
                        }
                    }
        }