C# example to load Doom WAD file

using System;
using System.IO;

namespace waddy
{
    class Program
    {
        static string GetString(byte[] arr)
        {
            return System.Text.ASCIIEncoding.ASCII.GetString(arr);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            if (File.Exists("DOOM.WAD"))
            {
                using (BinaryReader reader = new BinaryReader(File.Open("DOOM.WAD", FileMode.Open)))
                {
                    string name = GetString(reader.ReadBytes(4));
                    int numberOfLumps = reader.ReadInt32();
                    int folderPosition = reader.ReadInt32();

                    reader.BaseStream.Seek(folderPosition, SeekOrigin.Begin);

                    Console.WriteLine(name);
                    Console.WriteLine("Contains " + numberOfLumps + " lumps");
                    int count = 0;

                    while (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        
                        reader.BaseStream.Seek(folderPosition + (16 * count), SeekOrigin.Begin);
                        int filePos = reader.ReadInt32();
                        int fileSize = reader.ReadInt32();
                        string lumpName = GetString(reader.ReadBytes(8));

                        //Console.WriteLine("Next lump pos  is " + filePos);
                        //Console.WriteLine("Next lump size is " + fileSize);
                        Console.WriteLine("Next lump name is " + lumpName);

                        //reader.BaseStream.Seek(filePos + 12, SeekOrigin.Begin);

                        count++;
                    }
                    
                }
            }

        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s