1/*
2Copyright (C) 1996-1997 Id Software, Inc.
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19*/
20// wad.c
21
22#include "quakedef.h"
23
24int			wad_numlumps;
25lumpinfo_t	*wad_lumps;
26byte		*wad_base;
27
28void SwapPic (qpic_t *pic);
29
30/*
31==================
32W_CleanupName
33
34Lowercases name and pads with spaces and a terminating 0 to the length of
35lumpinfo_t->name.
36Used so lumpname lookups can proceed rapidly by comparing 4 chars at a time
37Space padding is so names can be printed nicely in tables.
38Can safely be performed in place.
39==================
40*/
41void W_CleanupName (const char *in, char *out)
42{
43	int		i;
44	int		c;
45
46	for (i=0 ; i<16 ; i++ )
47	{
48		c = in[i];
49		if (!c)
50			break;
51
52		if (c >= 'A' && c <= 'Z')
53			c += ('a' - 'A');
54		out[i] = c;
55	}
56
57	for ( ; i< 16 ; i++ )
58		out[i] = 0;
59}
60
61
62
63/*
64====================
65W_LoadWadFile
66====================
67*/
68void W_LoadWadFile (const char *filename)
69{
70	lumpinfo_t		*lump_p;
71	wadinfo_t		*header;
72	unsigned		i;
73	int				infotableofs;
74
75	wad_base = COM_LoadHunkFile (filename);
76	if (!wad_base)
77		Sys_Error ("W_LoadWadFile: couldn't load %s", filename);
78
79	header = (wadinfo_t *)wad_base;
80
81	if (header->identification[0] != 'W'
82	|| header->identification[1] != 'A'
83	|| header->identification[2] != 'D'
84	|| header->identification[3] != '2')
85		Sys_Error ("Wad file %s doesn't have WAD2 id\n",filename);
86
87	wad_numlumps = LittleLong(header->numlumps);
88	infotableofs = LittleLong(header->infotableofs);
89	wad_lumps = (lumpinfo_t *)(wad_base + infotableofs);
90
91	for (i=0, lump_p = wad_lumps ; i< (unsigned) wad_numlumps ; i++,lump_p++)
92	{
93		lump_p->filepos = LittleLong(lump_p->filepos);
94		lump_p->size = LittleLong(lump_p->size);
95		W_CleanupName (lump_p->name, lump_p->name);
96		if (lump_p->type == TYP_QPIC)
97			SwapPic ( (qpic_t *)(wad_base + lump_p->filepos));
98	}
99}
100
101
102/*
103=============
104W_GetLumpinfo
105=============
106*/
107lumpinfo_t	*W_GetLumpinfo (const char *name)
108{
109	int		i;
110	lumpinfo_t	*lump_p;
111	char	clean[16];
112
113	W_CleanupName (name, clean);
114
115	for (lump_p=wad_lumps, i=0 ; i<wad_numlumps ; i++,lump_p++)
116	{
117		if (!strcmp(clean, lump_p->name))
118			return lump_p;
119	}
120
121	Sys_Error ("W_GetLumpinfo: %s not found", name);
122	return NULL;
123}
124
125void *W_GetLumpName (const char *name)
126{
127	lumpinfo_t	*lump;
128
129	lump = W_GetLumpinfo (name);
130
131	return (void *)(wad_base + lump->filepos);
132}
133
134void *W_GetLumpNum (int num)
135{
136	lumpinfo_t	*lump;
137
138	if (num < 0 || num > wad_numlumps)
139		Sys_Error ("W_GetLumpNum: bad number: %i", num);
140
141	lump = wad_lumps + num;
142
143	return (void *)(wad_base + lump->filepos);
144}
145
146/*
147=============================================================================
148
149automatic byte swapping
150
151=============================================================================
152*/
153
154void SwapPic (qpic_t *pic)
155{
156	pic->width = LittleLong(pic->width);
157	pic->height = LittleLong(pic->height);
158}
159