aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/source/Irrlicht/CWADReader.cpp
blob: 9945fc87c20664ad884f75f497e4de606b49de52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (C) 2002-2012 Thomas Alten
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
// Code contributed by skreamz

#include "IrrCompileConfig.h"

#ifdef __IRR_COMPILE_WITH_WAD_ARCHIVE_LOADER_

#include "CWADReader.h"
#include "os.h"
#include "coreutil.h"

namespace irr
{
namespace io
{

//! Constructor
CArchiveLoaderWAD::CArchiveLoaderWAD( io::IFileSystem* fs)
: FileSystem(fs)
{
	#ifdef _DEBUG
	setDebugName("CArchiveLoaderWAD");
	#endif
}


//! returns true if the file maybe is able to be loaded by this class
bool CArchiveLoaderWAD::isALoadableFileFormat(const io::path& filename) const
{
	return core::hasFileExtension ( filename, "wad" );
}


//! Creates an archive from the filename
/** \param file File handle to check.
\return Pointer to newly created archive, or 0 upon error. */
IFileArchive* CArchiveLoaderWAD::createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const
{
	IFileArchive *archive = 0;
	io::IReadFile* file = FileSystem->createAndOpenFile(filename);

	if (file)
	{
		archive = createArchive ( file, ignoreCase, ignorePaths );
		file->drop ();
	}
	
	return archive;
}

//! creates/loads an archive from the file.
//! \return Pointer to the created archive. Returns 0 if loading failed.
IFileArchive* CArchiveLoaderWAD::createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const
{
	IFileArchive *archive = 0;
	if ( file )
	{
		file->seek ( 0 );
		archive = new CWADReader(file, ignoreCase, ignorePaths);
	}
	return archive;
}


//! Check if the file might be loaded by this class
/** Check might look into the file.
\param file File handle to check.
\return True if file seems to be loadable. */
bool CArchiveLoaderWAD::isALoadableFileFormat(io::IReadFile* file) const
{
	SWADFileHeader header;
	memset(&header, 0, sizeof(header));

	file->read( &header.tag, 4 );

	return !strncmp ( header.tag, "WAD2", 4 ) || !strncmp ( header.tag, "WAD3", 4 );
}

//! Check to see if the loader can create archives of this type.
bool CArchiveLoaderWAD::isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const
{
	return fileType == EFAT_WAD;
}

void createDir ( const c8 *full );


/*!
	WAD Reader
*/
CWADReader::CWADReader(IReadFile* file, bool ignoreCase, bool ignorePaths)
: CFileList((file ? file->getFileName() : io::path("")), ignoreCase, ignorePaths), File(file)
{
	#ifdef _DEBUG
	setDebugName("CWADReader");
	#endif

	if (File)
	{
		File->grab();

		Base = File->getFileName();
		Base.replace ( '\\', '/' );

		// scan local headers
		scanLocalHeader();

		sort();
	}

#if 0
	for ( u32 i = 0; i < FileList.size(); ++i )
	{
		SWADFileEntry &e = FileList[i];
		char buf[128];
		snprintf ( buf, 128, "c:\\h2\\%s", e.wadFileName.c_str() );

		createDir ( buf );
		FILE * f = fopen ( buf, "wb" );
		if ( 0 == f )
			continue;

		u8 * mem = new u8 [ e.header.disksize ];
		File->seek ( e.header.filepos );
		File->read ( mem, e.header.disksize );
		fwrite ( mem, e.header.disksize, 1, f );
		delete [] mem;
		fclose ( f );
	}
#endif

}


CWADReader::~CWADReader()
{
	if (File)
		File->drop();
}


//! return the id of the file Archive
const io::path& CWADReader::getArchiveName () const
{ 
	return Base;
}

const IFileList* CWADReader::getFileList() const
{
	return this;
}


//! scans for a local header, returns false if there is no more local file header.
bool CWADReader::scanLocalHeader()
{
	SWADFileEntryOriginal entry;
	SWADFileEntry save;

	memset(&Header, 0, sizeof(SWADFileHeader));
	File->read(&Header, sizeof(SWADFileHeader));

	if ( 0 == strncmp ( Header.tag, "WAD2", 4 ) )
		WadType = WAD_FORMAT_QUAKE2;
	else
	if ( 0 == strncmp ( Header.tag, "WAD3", 4 ) )
		WadType = WAD_FORMAT_HALFLIFE;
	else
		WadType = WAD_FORMAT_UNKNOWN;

	if ( WadType == WAD_FORMAT_UNKNOWN )
		return false;

#ifdef __BIG_ENDIAN__
	Header.numlumps = os::Byteswap::byteswap(Header.numlumps);
	Header.infotableofs = os::Byteswap::byteswap(Header.infotableofs);
#endif

	File->seek ( Header.infotableofs );

	c8 buf[16];
	for ( u32 i = 0; i < Header.numlumps; ++i )
	{
		// read entry
		File->read(&entry, sizeof ( SWADFileEntryOriginal ));
		entry.name[ sizeof ( entry.name ) - 1 ] = 0;

		save.header = entry;
		save.wadFileName = "/";
		save.wadFileName += entry.name;

		if ( WadType == WAD_FORMAT_HALFLIFE )
		{
			// don't know about the types! i'm guessing
			switch ( entry.type )
			{
				case WAD_TYP_MIPTEX_HALFLIFE:
					save.wadFileName += ".wal2";
					break;
				default:
					snprintf ( buf, 16, ".%02d", entry.type );
					save.wadFileName += buf;
					break;
			}
		}
		else
		if ( WadType == WAD_FORMAT_QUAKE2 )
		{
			switch ( entry.type )
			{
				case WAD_TYP_MIPTEX: save.wadFileName += ".miptex"; break;
				case WAD_TYP_SOUND: save.wadFileName += ".sound"; break;
				case WAD_TYP_PALETTE: save.wadFileName += ".palette"; break;
				case WAD_TYP_QTEX: save.wadFileName += ".qtex"; break;
				case WAD_TYP_QPIC: save.wadFileName += ".qpic"; break;
				case WAD_TYP_FONT: save.wadFileName += ".font"; break;
				default:
					snprintf ( buf, 16, ".%02d", entry.type );
					save.wadFileName += buf;
					break;
			}
		}

		// add file to list
		addItem(save.wadFileName,save.header.filepos, save.header.disksize, false );
		//FileInfo.push_back(save);
	}
	return true;
}


//! opens a file by file name
IReadFile* CWADReader::createAndOpenFile(const io::path& filename)
{
	s32 index = findFile(filename, false);

	if (index != -1)
		return createAndOpenFile(index);

	return 0;
}


//! opens a file by index
IReadFile* CWADReader::createAndOpenFile(u32 index)
{
	if (index >= Files.size() )
		return 0;

	const SFileListEntry &entry = Files[index];
	return createLimitReadFile( entry.FullName, File, entry.Offset, entry.Size );
}



} // end namespace io
} // end namespace irr


#endif // __IRR_COMPILE_WITH_WAD_ARCHIVE_LOADER_