using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace SharpQuake
{
	internal static class wad
	{
		public static byte[] Data
		{
			get
			{
				return wad._Data;
			}
		}

		public static IntPtr DataPointer
		{
			get
			{
				return wad._DataPtr;
			}
		}

		public static void LoadWadFile(string filename)
		{
			wad._Data = common.LoadFile(filename);
			if (wad._Data == null)
			{
				sys.Error("Wad.LoadWadFile: couldn't load {0}", new object[]
				{
					filename
				});
			}
			if (wad._Handle.IsAllocated)
			{
				wad._Handle.Free();
			}
			wad._Handle = GCHandle.Alloc(wad._Data, GCHandleType.Pinned);
			wad._DataPtr = wad._Handle.AddrOfPinnedObject();
			wadinfo_t wadinfo_t = sys.BytesToStructure<wadinfo_t>(wad._Data, 0);
			if (wadinfo_t.identification[0] != 87 || wadinfo_t.identification[1] != 65 || wadinfo_t.identification[2] != 68 || wadinfo_t.identification[3] != 50)
			{
				sys.Error("Wad file {0} doesn't have WAD2 id\n", new object[]
				{
					filename
				});
			}
			int num = common.LittleLong(wadinfo_t.numlumps);
			int num2 = common.LittleLong(wadinfo_t.infotableofs);
			int num3 = Marshal.SizeOf(typeof(lumpinfo_t));
			wad._Lumps = new Dictionary<string, lumpinfo_t>(num);
			for (int i = 0; i < num; i++)
			{
				IntPtr ptr = new IntPtr(wad._DataPtr.ToInt64() + (long)num2 + (long)(i * num3));
				lumpinfo_t lumpinfo_t = (lumpinfo_t)Marshal.PtrToStructure(ptr, typeof(lumpinfo_t));
				lumpinfo_t.filepos = common.LittleLong(lumpinfo_t.filepos);
				lumpinfo_t.size = common.LittleLong(lumpinfo_t.size);
				if (lumpinfo_t.type == 66)
				{
					ptr = new IntPtr(wad._DataPtr.ToInt64() + (long)lumpinfo_t.filepos);
					dqpicheader_t dqpicheader_t = (dqpicheader_t)Marshal.PtrToStructure(ptr, typeof(dqpicheader_t));
					wad.SwapPic(dqpicheader_t);
					Marshal.StructureToPtr<dqpicheader_t>(dqpicheader_t, ptr, true);
				}
				wad._Lumps.Add(Encoding.ASCII.GetString(lumpinfo_t.name).TrimEnd(new char[1]).ToLower(), lumpinfo_t);
			}
		}

		public static lumpinfo_t GetLumpInfo(string name)
		{
			lumpinfo_t result;
			if (wad._Lumps.TryGetValue(name, out result))
			{
				return result;
			}
			sys.Error("W_GetLumpinfo: {0} not found", new object[]
			{
				name
			});
			throw new InvalidOperationException("W_GetLumpinfo: Unreachable code reached!");
		}

		public static int GetLumpNameOffset(string name)
		{
			return wad.GetLumpInfo(name).filepos;
		}

		public static void SwapPic(dqpicheader_t pic)
		{
			pic.width = common.LittleLong(pic.width);
			pic.height = common.LittleLong(pic.height);
		}

		public const int CMP_NONE = 0;

		public const int CMP_LZSS = 1;

		public const int TYP_NONE = 0;

		public const int TYP_LABEL = 1;

		public const int TYP_LUMPY = 64;

		public const int TYP_PALETTE = 64;

		public const int TYP_QTEX = 65;

		public const int TYP_QPIC = 66;

		public const int TYP_SOUND = 67;

		public const int TYP_MIPTEX = 68;

		private static byte[] _Data;

		private static Dictionary<string, lumpinfo_t> _Lumps;

		private static GCHandle _Handle;

		private static IntPtr _DataPtr;
	}
}
