my own uncrustify run
[xonotic/netradiant.git] / plugins / archivepak / archive.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "archive.h"
23
24 #include "idatastream.h"
25 #include "cmdlib.h"
26 #include "bytestreamutils.h"
27 #include <algorithm>
28 #include "stream/filestream.h"
29
30 #include "iarchive.h"
31
32 #include "archivelib.h"
33
34
35 #include "plugin.h"
36
37 #include <map>
38 #include "string/string.h"
39 #include "fs_filesystem.h"
40
41 inline void buffer_findreplace( char* buffer, char find, char replace ){
42         while ( *buffer != '\0' )
43         {
44                 if ( *buffer == find ) {
45                         *buffer = replace;
46                 }
47                 ++buffer;
48         }
49 }
50
51 #include "pak.h"
52
53 class PakArchive : public Archive
54 {
55 class PakRecord
56 {
57 public:
58 PakRecord( unsigned int position, unsigned int stream_size )
59         : m_position( position ), m_stream_size( stream_size ){
60 }
61 unsigned int m_position;
62 unsigned int m_stream_size;
63 };
64 typedef GenericFileSystem<PakRecord> PakFileSystem;
65 PakFileSystem m_filesystem;
66 FileInputStream m_pakfile;
67 CopiedString m_name;
68
69 public:
70
71 PakArchive( const char* name )
72         : m_pakfile( name ), m_name( name ){
73         if ( !m_pakfile.failed() ) {
74                 pakheader_t header;
75
76                 m_pakfile.read( reinterpret_cast<FileInputStream::byte_type*>( header.magic ), 4 );
77                 header.diroffset = istream_read_uint32_le( m_pakfile );
78                 header.dirsize = istream_read_uint32_le( m_pakfile );
79
80                 if ( strncmp( header.magic, "PACK", 4 ) == 0 ) {
81                         m_pakfile.seek( header.diroffset );
82
83                         for ( unsigned int i = 0; i < header.dirsize; i += sizeof( pakentry_t ) )
84                         {
85                                 pakentry_t entry;
86
87                                 m_pakfile.read( reinterpret_cast<FileInputStream::byte_type*>( entry.filename ), 0x38 );
88                                 entry.offset = istream_read_uint32_le( m_pakfile );
89                                 entry.size = istream_read_uint32_le( m_pakfile );
90
91                                 buffer_findreplace( entry.filename, '\\', '/' );
92
93                                 PakFileSystem::entry_type& file = m_filesystem[entry.filename];
94                                 if ( !file.is_directory() ) {
95                                         globalOutputStream() << "Warning: pak archive " << makeQuoted( m_name.c_str() ) << " contains duplicated file: " << makeQuoted( entry.filename ) << "\n";
96                                 }
97                                 else
98                                 {
99                                         file = new PakRecord( entry.offset, entry.size );
100                                 }
101                         }
102                 }
103         }
104 }
105
106 ~PakArchive(){
107         for ( PakFileSystem::iterator i = m_filesystem.begin(); i != m_filesystem.end(); ++i )
108                 delete i->second.file();
109 }
110
111 void release(){
112         delete this;
113 }
114 ArchiveFile* openFile( const char* name ){
115         PakFileSystem::iterator i = m_filesystem.find( name );
116         if ( i != m_filesystem.end() && !i->second.is_directory() ) {
117                 PakRecord* file = i->second.file();
118                 return StoredArchiveFile::create( name, m_name.c_str(), file->m_position, file->m_stream_size, file->m_stream_size );
119         }
120         return 0;
121 }
122 virtual ArchiveTextFile* openTextFile( const char* name ){
123         PakFileSystem::iterator i = m_filesystem.find( name );
124         if ( i != m_filesystem.end() && !i->second.is_directory() ) {
125                 PakRecord* file = i->second.file();
126                 return StoredArchiveTextFile::create( name, m_name.c_str(), file->m_position, file->m_stream_size );
127         }
128         return 0;
129 }
130 bool containsFile( const char* name ){
131         PakFileSystem::iterator i = m_filesystem.find( name );
132         return i != m_filesystem.end() && !i->second.is_directory();
133 }
134 void forEachFile( VisitorFunc visitor, const char* root ){
135         m_filesystem.traverse( visitor, root );
136 }
137 };
138
139
140 Archive* OpenArchive( const char* name ){
141         return new PakArchive( name );
142 }
143
144 #if 0
145
146 class TestArchive
147 {
148 public:
149 TestArchive(){
150         Archive* archive = OpenArchive( "c:/quake3/baseq3/pak0.pak" );
151         ArchiveFile* file = archive->openFile( "gfx/palette.lmp" );
152         if ( file != 0 ) {
153                 char buffer[1024];
154                 file->getInputStream().read( (InputStream::byte_type*)buffer, 1024 );
155                 file->release();
156         }
157         archive->release();
158 }
159 };
160
161 TestArchive g_test;
162
163 #endif
164
165 #if 0
166
167 class TestArchive
168 {
169 class TestVisitor : public Archive::IVisitor
170 {
171 public:
172 void visit( const char* name ){
173         int bleh = 0;
174 }
175 };
176 public:
177 TestArchive(){
178         {
179                 Archive* archive = OpenArchive( "" );
180                 archive->release();
181         }
182         {
183                 Archive* archive = OpenArchive( "NONEXISTANTFILE" );
184                 archive->release();
185         }
186         {
187                 Archive* archive = OpenArchive( "c:/quake/id1/pak0.pak" );
188                 ArchiveFile* file = archive->openFile( "gfx/palette.lmp" );
189                 if ( file != 0 ) {
190                         char buffer[1024];
191                         file->getInputStream().read( (InputStream::byte_type*)buffer, 1024 );
192                         file->release();
193                 }
194                 TestVisitor visitor;
195                 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 0 ), "" );
196                 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFiles, 0 ), "progs/" );
197                 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFiles, 0 ), "maps/" );
198                 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFiles, 1 ), "sound/ambience/" );
199                 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 1 ), "sound/" );
200                 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eDirectories, 1 ), "sound/" );
201                 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 2 ), "sound/" );
202                 archive->forEachFile( Archive::VisitorFunc( &visitor, Archive::eFilesAndDirectories, 2 ), "" );
203                 archive->release();
204         }
205 }
206 };
207
208 TestArchive g_test;
209
210 #endif