1 /*
2 ** file_pak.cpp
3 **
4 **---------------------------------------------------------------------------
5 ** Copyright 2009 Christoph Oelckers
6 ** All rights reserved.
7 **
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions
10 ** are met:
11 **
12 ** 1. Redistributions of source code must retain the above copyright
13 **    notice, this list of conditions and the following disclaimer.
14 ** 2. Redistributions in binary form must reproduce the above copyright
15 **    notice, this list of conditions and the following disclaimer in the
16 **    documentation and/or other materials provided with the distribution.
17 ** 3. The name of the author may not be used to endorse or promote products
18 **    derived from this software without specific prior written permission.
19 **
20 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 **---------------------------------------------------------------------------
31 **
32 **
33 */
34 
35 #include "resourcefile.h"
36 #include "w_wad.h"
37 
38 //==========================================================================
39 //
40 //
41 //
42 //==========================================================================
43 
44 struct dpackfile_t
45 {
46 	char	name[56];
47 	int		filepos, filelen;
48 } ;
49 
50 struct dpackheader_t
51 {
52 	int		ident;		// == IDPAKHEADER
53 	int		dirofs;
54 	int		dirlen;
55 } ;
56 
57 
58 //==========================================================================
59 //
60 // Wad file
61 //
62 //==========================================================================
63 
64 class FPakFile : public FUncompressedFile
65 {
66 public:
67 	FPakFile(const char * filename, FileReader *file);
68 	bool Open(bool quiet);
69 };
70 
71 
72 //==========================================================================
73 //
74 // FWadFile::FWadFile
75 //
76 // Initializes a WAD file
77 //
78 //==========================================================================
79 
FPakFile(const char * filename,FileReader * file)80 FPakFile::FPakFile(const char *filename, FileReader *file) : FUncompressedFile(filename, file)
81 {
82 	Lumps = NULL;
83 }
84 
85 //==========================================================================
86 //
87 // Open it
88 //
89 //==========================================================================
90 
Open(bool quiet)91 bool FPakFile::Open(bool quiet)
92 {
93 	dpackheader_t header;
94 
95 	Reader->Read(&header, sizeof(header));
96 	NumLumps = LittleLong(header.dirlen) / sizeof(dpackfile_t);
97 	header.dirofs = LittleLong(header.dirofs);
98 
99 	dpackfile_t *fileinfo = new dpackfile_t[NumLumps];
100 	Reader->Seek (header.dirofs, SEEK_SET);
101 	Reader->Read (fileinfo, NumLumps * sizeof(dpackfile_t));
102 
103 	Lumps = new FUncompressedLump[NumLumps];
104 
105 	if (!quiet) Printf(", %d lumps\n", NumLumps);
106 
107 	for(DWORD i = 0; i < NumLumps; i++)
108 	{
109 		Lumps[i].LumpNameSetup(fileinfo[i].name);
110 		Lumps[i].Owner = this;
111 		Lumps[i].Position = LittleLong(fileinfo[i].filepos);
112 		Lumps[i].LumpSize = LittleLong(fileinfo[i].filelen);
113 		Lumps[i].CheckEmbedded();
114 	}
115 
116 	delete [] fileinfo;
117 	return true;
118 }
119 
120 
121 //==========================================================================
122 //
123 // File open
124 //
125 //==========================================================================
126 
CheckPak(const char * filename,FileReader * file,bool quiet)127 FResourceFile *CheckPak(const char *filename, FileReader *file, bool quiet)
128 {
129 	char head[4];
130 
131 	if (file->GetLength() >= 12)
132 	{
133 		file->Seek(0, SEEK_SET);
134 		file->Read(&head, 4);
135 		file->Seek(0, SEEK_SET);
136 		if (!memcmp(head, "PACK", 4))
137 		{
138 			FResourceFile *rf = new FPakFile(filename, file);
139 			if (rf->Open(quiet)) return rf;
140 			rf->Reader = NULL; // to avoid destruction of reader
141 			delete rf;
142 		}
143 	}
144 	return NULL;
145 }
146 
147