/// <summary> /// Returns an entry in this archive as a Stream instance. /// Throws a DBPFException if entry was not found. /// </summary> /// <param name="ID">ID of the entry to grab from archive.</param> /// <returns>The entry's data as a Stream instance.</returns> public Stream GrabEntry(UniqueFileID ID) { m_FinishedReading.WaitOne(); if (!ContainsEntry(ID)) { throw new DBPFException("Couldn't find entry - DBPFArchive.cs!"); } DBPFEntry Entry = m_Entries[ID]; m_Reader.Seek(Entry.FileOffset); MemoryStream Data = new MemoryStream(m_Reader.ReadBytes((int)Entry.FileSize)); Data.Position = 0; return(Data); }
/// <summary> /// Reads all entries in this archive into memory. /// </summary> /// <param name="ThrowException">Wether or not to throw an exception if the archive was not a DBPF. If false, function will return.</param> public bool ReadArchive(bool ThrowException) { m_FinishedReading.Reset(); if (m_Reader == null) { try { m_Reader = new FileReader(m_Path, false); } //This will be thrown because of file access privileges or because an archive is being tentatively opened twice. catch (Exception) { if (ThrowException) { throw; } else { return(false); } } } lock (m_Reader) { ASCIIEncoding Enc = new ASCIIEncoding(); string MagicNumber = Enc.GetString(m_Reader.ReadBytes(4)); if (!MagicNumber.Equals("DBPF", StringComparison.InvariantCultureIgnoreCase)) { if (ThrowException) { throw new DBPFException("MagicNumber was wrong - DBPFArchive.cs!"); } else { m_Reader.Close(); return(false); } } m_Reader.ReadUInt32(); //MajorVersion m_Reader.ReadUInt32(); //MinorVersion m_Reader.ReadBytes(12); //Reserved. m_Reader.ReadBytes(4); //Date created. m_Reader.ReadBytes(4); //Date modified. m_Reader.ReadUInt32(); //Index major version. IndexEntryCount = m_Reader.ReadUInt32(); IndexOffset = m_Reader.ReadUInt32(); IndexSize = m_Reader.ReadUInt32(); m_Reader.Seek(IndexOffset); for (int i = 0; i < IndexEntryCount; i++) { DBPFEntry Entry = new DBPFEntry(); Entry.TypeID = m_Reader.ReadUInt32(); Entry.GroupID = m_Reader.ReadUInt32(); Entry.InstanceID = m_Reader.ReadUInt32(); Entry.FileOffset = m_Reader.ReadUInt32(); Entry.FileSize = m_Reader.ReadUInt32(); UniqueFileID ID = new UniqueFileID(Entry.TypeID, Entry.InstanceID, Entry.GroupID); Entry.EntryID = ID; m_Entries.Add(ID, Entry); } } m_FinishedReading.Set(); return(true); }
/// <summary> /// Reads all entries in this archive into memory. /// </summary> /// <param name="ThrowException">Wether or not to throw an exception if the archive was not a DBPF. If false, function will return.</param> public bool ReadArchive(bool ThrowException) { m_FinishedReading.Reset(); if (m_Reader == null) { try { m_Reader = new FileReader(m_Path, false); } //This will be thrown because of file access privileges or because an archive is being tentatively opened twice. catch (Exception) { if (ThrowException) throw; else return false; } } lock (m_Reader) { ASCIIEncoding Enc = new ASCIIEncoding(); string MagicNumber = Enc.GetString(m_Reader.ReadBytes(4)); if (!MagicNumber.Equals("DBPF", StringComparison.InvariantCultureIgnoreCase)) { if (ThrowException) throw new DBPFException("MagicNumber was wrong - DBPFArchive.cs!"); else { m_Reader.Close(); return false; } } m_Reader.ReadUInt32(); //MajorVersion m_Reader.ReadUInt32(); //MinorVersion m_Reader.ReadBytes(12); //Reserved. m_Reader.ReadBytes(4); //Date created. m_Reader.ReadBytes(4); //Date modified. m_Reader.ReadUInt32(); //Index major version. IndexEntryCount = m_Reader.ReadUInt32(); IndexOffset = m_Reader.ReadUInt32(); IndexSize = m_Reader.ReadUInt32(); m_Reader.Seek(IndexOffset); for(int i = 0; i < IndexEntryCount; i++) { DBPFEntry Entry = new DBPFEntry(); Entry.TypeID = m_Reader.ReadUInt32(); Entry.GroupID = m_Reader.ReadUInt32(); Entry.InstanceID = m_Reader.ReadUInt32(); Entry.FileOffset = m_Reader.ReadUInt32(); Entry.FileSize = m_Reader.ReadUInt32(); UniqueFileID ID = new UniqueFileID(Entry.TypeID, Entry.InstanceID, Entry.GroupID); Entry.EntryID = ID; m_Entries.Add(ID, Entry); } } m_FinishedReading.Set(); return true; }