public void AddResource(ResourceEntry Entry, int Priority)
        {
            if (Entry.Key != Key)
            {
                throw new ArgumentException();
            }

            if (List.ContainsKey(Priority))
            {
                Duplicates.Add(Entry);
            }
            else
            {
                List.Add(Priority, Entry);
            }
   
        }
        public void AddResource(ResourceEntry Resource)
        {
            if (IsEncrypted != Resource.IsEncrypted)
                throw new InvalidDataException("Encryption State Mismatch");

            byte[] buffer = Resource.ReadRaw();

            long ChunkOffset = DataStore.Length;
            int ChunkLength = buffer.Length;
            int Length = (int)Resource.Length;

            DataStore.Position = DataStore.Length;
            DataStore.Write(buffer, 0, ChunkLength);
            DataStore.Flush();

            ResourceEntry Entry = new ResourceEntry(DataStore, Resource.Key, ChunkOffset, ChunkLength, Length, Resource.IsCompressed, Resource.IsEncrypted);
            Resources.Add(Entry);
            IsModified = true;
        }
        private void Import()
        {
            BinaryReader Reader;
            byte[] buffer;            

            // Start by getting the Header :  0x60 bytes at the beginning of the file
            DataStore.Seek (0,SeekOrigin .Begin);

            buffer = new byte[0x60];
            DataStore.Read (buffer,0,0x60);

            
            // Check the Magic
            string Magic = Encoding.ASCII.GetString(buffer, 0, 4);

            if ((Magic != "DBPF") & (Magic != "DBPP"))
            {
                throw new InvalidDataException("Unknown Magic Number: " + Magic);
            }

            if (Magic == "DBPP")
            {
                _IsEncrypted = true;
                throw new InvalidDataException("DBPP Not Supported");
            }

            Reader = new BinaryReader(new MemoryStream(buffer, false));
            Reader.BaseStream.Position = 4;
            
            // Next verify the Major, Minor Versions
            // Major, Minor == 2,0

            uint Major = Reader.ReadUInt32();
            uint Minor = Reader.ReadUInt32();
            uint UMajor = Reader.ReadUInt32();
            uint UMinor = Reader.ReadUInt32();
            uint Flags = Reader.ReadUInt32();
            uint CDate = Reader.ReadUInt32();
            uint MDate = Reader.ReadUInt32();
            uint IMajor = Reader.ReadUInt32();
            uint IndexCount = Reader.ReadUInt32();
            uint IOffset = Reader.ReadUInt32();
            uint IndexLength = Reader.ReadUInt32();
            uint HCount = Reader.ReadUInt32();
            uint HOffset = Reader.ReadUInt32();
            uint HLength = Reader.ReadUInt32();
            uint IMinor = Reader.ReadUInt32();
            uint IndexOffset = Reader.ReadUInt32();

            // Verify Header Values
            if ((Major != 2) | (Minor != 0))
            {
                log.Warn(string.Format("Unknown DBPF Version {0}.{1}, Expected 2.0 -- {2}", Major, Minor,this.FileName));
                throw new InvalidDataException(string.Format("Unknown File Version: {0}.{1}, Expected 2.0", Major, Minor));
            }

            if (((IMajor != 0) & (IMajor != 7)) | (IMinor != 3))
            {
                log.Warn(string.Format("Unknown Index Version {0}.{1}, Expected 0.3 -- {2}", IMajor, IMinor,this.FileName));
                //throw new InvalidDataException(string.Format("Unknown Index Version: {0}.{1}, Expected 0.3", IMajor, IMinor));
            }

            if ((UMajor != 0) | (UMinor != 0) | (Flags != 0) | (CDate != 0) | (MDate != 0) | (HCount != 0) | (HLength != 0))
            {
                log.Warn(string.Format("Unused Header Value not set to 0 -- {0}", this.FileName));
            //    throw new InvalidDataException("Unused header value not 0");
            }

            if ((IOffset != 0) & (IOffset != IndexOffset))
            {
                log.Warn(string.Format("Header Offset mismatch -- Not loadable by Game! -- {0}", this.FileName));
            }
            
            // Now we need to get a new buffer, BinaryReader, etc and read in the Index
            buffer = new byte[IndexLength];
            DataStore.Position = IndexOffset;

            DataStore.Read(buffer, 0, (int)IndexLength);
            Reader = new BinaryReader(new MemoryStream(buffer, false));

            // Vars to hold the info
            UInt32 Type =0;
            UInt32 Group=0;
            UInt64 Instance=0;

            TGI_Key Key;
            ResourceEntry Entry;

            UInt32 ILow = 0xffffffffu;
            UInt32 IHigh = 0xffffffffu;

            UInt32 ChunkOffset;
            UInt32 ChunkLength;
            UInt32 ResourceLength;
            UInt16 ResourceCompression;
            UInt16 ResourceFlag;

            // if we don't have any index entries, just exit
            if (IndexCount == 0)
            {
                log.Info("Empty Package -- No Index Entries");
                return;
            }

            // Now we get the index type
            uint IndexBits = Reader.ReadUInt32();

            // Test Package Index Type :::
            //
            int CommonCount = 0;
            if ((IndexBits & 0x0001) == 0x0001)
            {
                CommonCount += 4;
            }
            if ((IndexBits & 0x0002) == 0x0002)
            {
                CommonCount += 4;
            }
            if ((IndexBits & 0x0004) == 0x0004)
            {
                CommonCount += 4;
            }
            if ((IndexBits & 0x0008) == 0x0008)
            {
                CommonCount += 4;
            }

            

            int BlockLength = CommonCount + 4;
            BlockLength += (32 - CommonCount) * (int)IndexCount;

            if (BlockLength != IndexLength)
            {
                log.Warn(string.Format("DBPF Format Error, IndexType vs IndexSize mismatch: Actual Size {0}, Calculated Size {1}, IndexType {2} -- {3}", IndexLength, BlockLength,IndexBits,this.FileName));
                throw new InvalidDataException("DBPF Format Error, IndexType vs IndexSize mismatch");          
            }



            if ((IndexBits | 0x000f) != 0x000f )
            {
                log.Fatal("Import not implemented");
                throw new NotImplementedException();           
            }

            if ((IndexBits & 0x00000001) == 0x00000001)
            {
                Type = Reader.ReadUInt32();
            }

            if ((IndexBits & 0x00000002) == 0x00000002)
            {
                Group = Reader.ReadUInt32();
            }

            if ((IndexBits & 0x00000004) == 0x00000004)
            {
                ILow = Reader.ReadUInt32();
            }

            if ((IndexBits & 0x00000008) == 0x00000008)
            {
                IHigh = Reader.ReadUInt32();             


            }

            // now we cycle through each of the entries
            for (int i = 0; i < IndexCount; i++)
            {
                // Get the Variable Portion
                if ((IndexBits & 0x00000001) == 0x00000000)
                {
                    Type = Reader.ReadUInt32();
                }

                if ((IndexBits & 0x00000002) == 0x00000000)
                {
                    Group = Reader.ReadUInt32();
                }

                if ((IndexBits & 0x00000004) == 0x00000000)
                {
                    ILow = Reader.ReadUInt32();
                }
                if ((IndexBits & 0x000000008) == 0x00000000)
                {
                    IHigh = Reader.ReadUInt32();
                }

                Instance = (UInt64)IHigh | ((UInt64)ILow << 32);

                // Get the static part
                ChunkOffset = Reader.ReadUInt32();
                ChunkLength = Reader.ReadUInt32() & 0x7fffffff;
                ResourceLength = Reader.ReadUInt32();
                ResourceCompression = Reader.ReadUInt16();
                ResourceFlag = Reader.ReadUInt16();

                // Build the Key and the Entry
                Key = new TGI_Key(Type, Group, Instance);

                Entry = new ResourceEntry(DataStore, Key, (long)ChunkOffset, (int)ChunkLength, (int)ResourceLength, ResourceCompression == 0xffff, IsEncrypted);

                Resources.Add(Entry);
            }
        }