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"
35 #include "string/string.h"
39 class WadArchive : public Archive {
42 wad_record_t(unsigned int position, unsigned int stream_size, unsigned int file_size)
43 : m_position(position), m_stream_size(stream_size), m_file_size(file_size)
46 unsigned int m_position;
47 unsigned int m_stream_size;
48 unsigned int m_file_size;
57 typedef std::map<CopiedString, wad_record_t, StringLessNoCase> files_t;
60 FileInputStream m_wadfile;
62 EWadVersion wad_version(const char *identification)
64 if (strncmp(identification, "WAD2", 4) == 0) {
67 if (strncmp(identification, "WAD3", 4) == 0) {
73 const char *type_for_version(EWadVersion version)
86 int miptex_type_for_version(EWadVersion version)
100 WadArchive(const char *name)
101 : m_name(name), m_wadfile(name)
103 if (!m_wadfile.failed()) {
105 istream_read_wadinfo(m_wadfile, wadinfo);
107 EWadVersion version = wad_version(wadinfo.identification);
108 int miptexType = miptex_type_for_version(version);
110 if (version != eNotValid) {
111 m_wadfile.seek(wadinfo.infotableofs);
113 for (int i = 0; i < wadinfo.numlumps; ++i) {
116 istream_read_lumpinfo(m_wadfile, lumpinfo);
117 if (lumpinfo.type == miptexType) {
118 strcpy(buffer, "textures/");
119 strcat(buffer, lumpinfo.name);
120 strcat(buffer, type_for_version(version));
121 m_files.insert(files_t::value_type(buffer, wad_record_t(lumpinfo.filepos, lumpinfo.disksize,
134 ArchiveFile *openFile(const char *name)
136 files_t::iterator i = m_files.find(name);
137 if (i != m_files.end()) {
138 return StoredArchiveFile::create(name, m_name.c_str(), i->second.m_position, i->second.m_stream_size,
139 i->second.m_file_size);
144 virtual ArchiveTextFile *openTextFile(const char *name)
146 files_t::iterator i = m_files.find(name);
147 if (i != m_files.end()) {
148 return StoredArchiveTextFile::create(name, m_name.c_str(), i->second.m_position, i->second.m_stream_size);
153 bool containsFile(const char *name)
155 return m_files.find(name) != m_files.end();
158 void forEachFile(VisitorFunc visitor, const char *root)
160 if (root[0] == '\0') {
161 if (visitor.directory("textures/", 1)) {
164 } else if (strcmp(root, "textures/") != 0) {
168 for (files_t::iterator i = m_files.begin(); i != m_files.end(); ++i) {
169 visitor.file(i->first.c_str());
175 Archive *OpenArchive(const char *name)
177 return new WadArchive(name);
184 class TestVisitor : public Archive::IVisitor
187 void visit( const char* name ){
194 Archive* archive = OpenArchive( "" );
198 Archive* archive = OpenArchive( "NONEXISTANTFILE" );
202 Archive* archive = OpenArchive( "c:/quake/id1/quake101.wad" );
203 ArchiveFile* file = archive->openFile( "textures/sky1.mip" );
205 unsigned char* buffer = new unsigned char[file->size()];
206 file->getInputStream().read( (InputStream::byte_type*)buffer, file->size() );
211 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 1 ), "" );
212 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 0 ), "" );
213 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 0 ), "textures/" );
214 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 1 ), "textures/" );