1 /*
2  * HLLib
3  * Copyright (C) 2006-2010 Ryan Gregg
4 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later
9  * version.
10  */
11 
12 #ifndef VBSPFILE_H
13 #define VBSPFILE_H
14 
15 #include "stdafx.h"
16 #include "Package.h"
17 
18 #define HL_VBSP_LUMP_COUNT 64
19 
20 namespace HLLib
21 {
22 	class HLLIB_API CVBSPFile : public CPackage
23 	{
24 	private:
25 		#pragma pack(1)
26 
27 		struct VBSPLump
28 		{
29 			hlUInt	uiOffset;
30 			hlUInt	uiLength;
31 			hlUInt	uiVersion;							// Default to zero.
32 			hlChar	lpFourCC[4];						// Default to ( char )0, ( char )0, ( char )0, ( char )0.
33 		};
34 
35 		struct VBSPHeader
36 		{
37 			hlChar		lpSignature[4];					// BSP file signature.
38 			hlInt		iVersion;						// BSP file version.
39 			VBSPLump	lpLumps[HL_VBSP_LUMP_COUNT];	// Lumps.
40 			hlInt		iMapRevision;					// The map's revision (iteration, version) number.
41 		};
42 
43 		struct ZIPEndOfCentralDirectoryRecord
44 		{
45 			hlUInt uiSignature; // 4 bytes (0x06054b50)
46 			hlUInt16 uiNumberOfThisDisk;  // 2 bytes
47 			hlUInt16 uiNumberOfTheDiskWithStartOfCentralDirectory; // 2 bytes
48 			hlUInt16 uiCentralDirectoryEntriesThisDisk;	// 2 bytes
49 			hlUInt16 uiCentralDirectoryEntriesTotal;	// 2 bytes
50 			hlUInt uiCentralDirectorySize; // 4 bytes
51 			hlUInt uiStartOfCentralDirOffset; // 4 bytes
52 			hlUInt16 uiCommentLength; // 2 bytes
53 			// zip file comment follows
54 		};
55 
56 		struct ZIPFileHeader
57 		{
58 			hlUInt uiSignature; //  4 bytes (0x02014b50)
59 			hlUInt16 uiVersionMadeBy; // version made by 2 bytes
60 			hlUInt16 uiVersionNeededToExtract; // version needed to extract 2 bytes
61 			hlUInt16 uiFlags; // general purpose bit flag 2 bytes
62 			hlUInt16 uiCompressionMethod; // compression method 2 bytes
63 			hlUInt16 uiLastModifiedTime; // last mod file time 2 bytes
64 			hlUInt16 uiLastModifiedDate; // last mod file date 2 bytes
65 			hlUInt uiCRC32; // crc-32 4 bytes
66 			hlUInt uiCompressedSize; // compressed size 4 bytes
67 			hlUInt uiUncompressedSize; // uncompressed size 4 bytes
68 			hlUInt16 uiFileNameLength; // file name length 2 bytes
69 			hlUInt16 uiExtraFieldLength; // extra field length 2 bytes
70 			hlUInt16 uiFileCommentLength; // file comment length 2 bytes
71 			hlUInt16 uiDiskNumberStart; // disk number start 2 bytes
72 			hlUInt16 uiInternalFileAttribs; // internal file attributes 2 bytes
73 			hlUInt uiExternalFileAttribs; // external file attributes 4 bytes
74 			hlUInt uiRelativeOffsetOfLocalHeader; // relative offset of local header 4 bytes
75 			// file name (variable size)
76 			// extra field (variable size)
77 			// file comment (variable size)
78 		};
79 
80 		struct ZIPLocalFileHeader
81 		{
82 			hlUInt uiSignature; //local file header signature 4 bytes (0x04034b50)
83 			hlUInt16 uiVersionNeededToExtract; // version needed to extract 2 bytes
84 			hlUInt16 uiFlags; // general purpose bit flag 2 bytes
85 			hlUInt16 uiCompressionMethod; // compression method 2 bytes
86 			hlUInt16 uiLastModifiedTime; // last mod file time 2 bytes
87 			hlUInt16 uiLastModifiedDate; // last mod file date 2 bytes
88 			hlUInt uiCRC32; // crc-32 4 bytes
89 			hlUInt uiCompressedSize; // compressed size 4 bytes
90 			hlUInt uiUncompressedSize; // uncompressed size 4 bytes
91 			hlUInt16 uiFileNameLength; // file name length 2 bytes
92 			hlUInt16 uiExtraFieldLength; // extra field length 2 bytes
93 			// file name (variable size)
94 			// extra field (variable size)
95 			// file data (variable size)
96 		};
97 
98 		struct LMPHeader
99 		{
100 			hlInt	iLumpOffset;
101 			hlInt	iLumpID;
102 			hlInt	iLumpVersion;
103 			hlInt	iLumpLength;
104 			hlInt	iMapRevision;
105 		};
106 
107 		#pragma pack()
108 
109 	private:
110 		static const char *lpAttributeNames[];
111 		static const char *lpItemAttributeNames[];
112 
113 		Mapping::CView *pHeaderView;
114 		Mapping::CView *pFileHeaderView;
115 		Mapping::CView *pEndOfCentralDirectoryRecordView;
116 
117 		const VBSPHeader *pHeader;
118 		const ZIPEndOfCentralDirectoryRecord *pEndOfCentralDirectoryRecord;
119 
120 	public:
121 		CVBSPFile();
122 		virtual ~CVBSPFile();
123 
124 		virtual HLPackageType GetType() const;
125 		virtual const hlChar *GetExtension() const;
126 		virtual const hlChar *GetDescription() const;
127 
128 	protected:
129 		virtual hlBool MapDataStructures();
130 		virtual hlVoid UnmapDataStructures();
131 
132 		virtual CDirectoryFolder *CreateRoot();
133 
134 		virtual hlUInt GetAttributeCountInternal() const;
135 		virtual const hlChar *GetAttributeNameInternal(HLPackageAttribute eAttribute) const;
136 		virtual hlBool GetAttributeInternal(HLPackageAttribute eAttribute, HLAttribute &Attribute) const;
137 
138 		virtual hlUInt GetItemAttributeCountInternal() const;
139 		virtual const hlChar *GetItemAttributeNameInternal(HLPackageAttribute eAttribute) const;
140 		virtual hlBool GetItemAttributeInternal(const CDirectoryItem *pItem, HLPackageAttribute eAttribute, HLAttribute &Attribute) const;
141 
142 		virtual hlBool GetFileExtractableInternal(const CDirectoryFile *pFile, hlBool &bExtractable) const;
143 		virtual hlBool GetFileValidationInternal(const CDirectoryFile *pFile, HLValidation &eValidation) const;
144 		virtual hlBool GetFileSizeInternal(const CDirectoryFile *pFile, hlUInt &uiSize) const;
145 		virtual hlBool GetFileSizeOnDiskInternal(const CDirectoryFile *pFile, hlUInt &uiSize) const;
146 
147 		virtual hlBool CreateStreamInternal(const CDirectoryFile *pFile, Streams::IStream *&pStream) const;
148 		virtual hlVoid ReleaseStreamInternal(Streams::IStream &Stream) const;
149 
150 	private:
151 		hlVoid GetFileName(hlChar *lpBuffer, hlUInt uiBufferSize);
152 	};
153 }
154 
155 #endif
156