2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "idatastream.h"
26 #include "bytestreamutils.h"
28 #include "stream/filestream.h"
32 #include "archivelib.h"
38 #include "string/string.h"
39 #include "fs_filesystem.h"
41 inline void buffer_findreplace(char* buffer, char find, char replace)
43 while(*buffer != '\0')
53 class PakArchive : public Archive
58 PakRecord(unsigned int position, unsigned int stream_size)
59 : m_position(position), m_stream_size(stream_size)
62 unsigned int m_position;
63 unsigned int m_stream_size;
65 typedef GenericFileSystem<PakRecord> PakFileSystem;
66 PakFileSystem m_filesystem;
67 FileInputStream m_pakfile;
72 PakArchive(const char* name)
73 : m_pakfile(name), m_name(name)
75 if(!m_pakfile.failed())
79 m_pakfile.read(reinterpret_cast<FileInputStream::byte_type*>(header.magic), 4);
80 header.diroffset = istream_read_uint32_le(m_pakfile);
81 header.dirsize = istream_read_uint32_le(m_pakfile);
83 if(strncmp (header.magic, "PACK", 4) == 0)
85 m_pakfile.seek(header.diroffset);
87 for(unsigned int i = 0; i < header.dirsize; i += sizeof(pakentry_t))
91 m_pakfile.read(reinterpret_cast<FileInputStream::byte_type*>(entry.filename), 0x38);
92 entry.offset = istream_read_uint32_le(m_pakfile);
93 entry.size = istream_read_uint32_le(m_pakfile);
95 buffer_findreplace(entry.filename, '\\', '/');
97 PakFileSystem::entry_type& file = m_filesystem[entry.filename];
98 if(!file.is_directory())
100 globalOutputStream() << "Warning: pak archive " << makeQuoted(m_name.c_str()) << " contains duplicated file: " << makeQuoted(entry.filename) << "\n";
104 file = new PakRecord(entry.offset, entry.size);
113 for(PakFileSystem::iterator i = m_filesystem.begin(); i != m_filesystem.end(); ++i)
114 delete i->second.file();
121 ArchiveFile* openFile(const char* name)
123 PakFileSystem::iterator i = m_filesystem.find(name);
124 if(i != m_filesystem.end() && !i->second.is_directory())
126 PakRecord* file = i->second.file();
127 return StoredArchiveFile::create(name, m_name.c_str(), file->m_position, file->m_stream_size, file->m_stream_size);
131 virtual ArchiveTextFile* openTextFile(const char* name)
133 PakFileSystem::iterator i = m_filesystem.find(name);
134 if(i != m_filesystem.end() && !i->second.is_directory())
136 PakRecord* file = i->second.file();
137 return StoredArchiveTextFile::create(name, m_name.c_str(), file->m_position, file->m_stream_size);
141 bool containsFile(const char* name)
143 PakFileSystem::iterator i = m_filesystem.find(name);
144 return i != m_filesystem.end() && !i->second.is_directory();
146 void forEachFile(VisitorFunc visitor, const char* root)
148 m_filesystem.traverse(visitor, root);
153 Archive* OpenArchive(const char* name)
155 return new PakArchive(name);
165 Archive* archive = OpenArchive("c:/quake3/baseq3/pak0.pak");
166 ArchiveFile* file = archive->openFile("gfx/palette.lmp");
170 file->getInputStream().read((InputStream::byte_type*)buffer, 1024);
185 class TestVisitor : public Archive::IVisitor
188 void visit(const char* name)
197 Archive* archive = OpenArchive("");
201 Archive* archive = OpenArchive("NONEXISTANTFILE");
205 Archive* archive = OpenArchive("c:/quake/id1/pak0.pak");
206 ArchiveFile* file = archive->openFile("gfx/palette.lmp");
210 file->getInputStream().read((InputStream::byte_type*)buffer, 1024);
214 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 0), "");
215 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFiles, 0), "progs/");
216 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFiles, 0), "maps/");
217 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFiles, 1), "sound/ambience/");
218 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "sound/");
219 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eDirectories, 1), "sound/");
220 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 2), "sound/");
221 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 2), "");