1 /*
2 ** file_rtl.cpp
3 **
4 **---------------------------------------------------------------------------
5 ** Copyright 2013 Braden Obrzut
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 "doomerrors.h"
36 #include "wl_def.h"
37 #include "resourcefile.h"
38 #include "w_wad.h"
39 #include "m_swap.h"
40 #include "zstring.h"
41 #include "wolfmapcommon.h"
42 
43 #pragma pack(1)
44 struct RtlMapHeader
45 {
46 public:
47 	DWORD usedFlag;
48 	DWORD crc; // Useless for us, this is just a random value to check maps in multiplayer
49 	DWORD rlewTag;
50 	DWORD mapSpecials;
51 	DWORD planeOffset[PLANES];
52 	DWORD planeLength[PLANES];
53 	char name[24];
54 };
55 #pragma pack()
56 
57 class FRtlFile : public FResourceFile
58 {
59 	public:
60 		FRtlFile(const char* filename, FileReader *file);
61 		~FRtlFile();
62 
63 		FResourceLump *GetLump(int lump);
64 		bool Open(bool quiet);
65 
66 	private:
67 		FMapLump*	Lumps;
68 };
69 
FRtlFile(const char * filename,FileReader * file)70 FRtlFile::FRtlFile(const char* filename, FileReader *file) : FResourceFile(filename, file), Lumps(NULL)
71 {
72 }
73 
~FRtlFile()74 FRtlFile::~FRtlFile()
75 {
76 	if(Lumps != NULL)
77 		delete[] Lumps;
78 }
79 
GetLump(int lump)80 FResourceLump *FRtlFile::GetLump(int lump)
81 {
82 	return &Lumps[lump];
83 }
84 
Open(bool quiet)85 bool FRtlFile::Open(bool quiet)
86 {
87 	RtlMapHeader header[100];
88 
89 	Reader->Seek(8, SEEK_SET);
90 	Reader->Read(&header, sizeof(RtlMapHeader)*100);
91 
92 	// We allocate 2 lumps per map so...
93 	static const unsigned int NUM_MAP_LUMPS = 2;
94 	NumLumps = 0;
95 	for(unsigned int i = 0;i < 100;++i)
96 	{
97 		if(header[i].usedFlag)
98 			NumLumps += NUM_MAP_LUMPS;
99 	}
100 
101 	Lumps = new FMapLump[NumLumps];
102 	// Preserve map position in the MAPxy notation
103 	for(unsigned int i = 0;i < 100;++i)
104 	{
105 		if(!header[i].usedFlag)
106 			continue;
107 
108 		// Map marker
109 		FMapLump &markerLump = Lumps[i*NUM_MAP_LUMPS];
110 		char lumpname[6];
111 		sprintf(lumpname, "MAP%02d", i != 99 ? i+1 : 0);
112 		markerLump.Owner = this;
113 		markerLump.LumpNameSetup(lumpname);
114 		markerLump.Namespace = ns_global;
115 		markerLump.LumpSize = 0;
116 
117 		// Make the data lump
118 		FMapLump &dataLump = Lumps[i*NUM_MAP_LUMPS+1];
119 		dataLump.Owner = this;
120 		dataLump.LumpNameSetup("PLANES");
121 		dataLump.Namespace = ns_global;
122 		for(unsigned int j = 0;j < PLANES;j++)
123 		{
124 			dataLump.Header.PlaneOffset[j] = LittleLong(header[i].planeOffset[j]);
125 			dataLump.Header.PlaneLength[j] = LittleLong(header[i].planeLength[j]);
126 		}
127 		dataLump.rlewTag = LittleLong(header[i].rlewTag);
128 		dataLump.carmackCompressed = false;
129 		dataLump.rtlMap = true;
130 		dataLump.Header.Width = 128;
131 		dataLump.Header.Height = 128;
132 		memcpy(dataLump.Header.Name, header[i].name, 24);
133 		dataLump.LumpSize += dataLump.Header.Width*dataLump.Header.Height*RTLCONVERTEDPLANES*2;
134 	}
135 	if(!quiet) Printf(", %d lumps\n", NumLumps);
136 	return true;
137 }
138 
CheckRtl(const char * filename,FileReader * file,bool quiet)139 FResourceFile *CheckRtl(const char *filename, FileReader *file, bool quiet)
140 {
141 	char head[4];
142 	DWORD version;
143 
144 	if(file->GetLength() >= static_cast<signed>(8+sizeof(RtlMapHeader)*100))
145 	{
146 		file->Seek(0, SEEK_SET);
147 		file->Read(&head, 4);
148 		file->Read(&version, 4);
149 		file->Seek(0, SEEK_SET);
150 		if((!memcmp(head, "RTL", 4) || !memcmp(head, "RTC", 4)) && LittleLong(version) == 0x0101)
151 		{
152 			FResourceFile *rf = new FRtlFile(filename, file);
153 			if(rf->Open(quiet)) return rf;
154 			rf->Reader = NULL; // to avoid destruction of reader
155 			delete rf;
156 		}
157 	}
158 	return NULL;
159 }
160