]> git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/bsploader.cpp
gcc: appease the hardening warnings
[xonotic/netradiant.git] / contrib / bobtoolz / bsploader.cpp
1 #include <globaldefs.h>
2 #include <cassert>
3 #include "bsploader.h"
4 #include "dialogs/dialogs-gtk.h"
5 #include "cmdlib.h"
6
7 int numnodes;
8 int numplanes;
9 int numleafs;
10 int numleafsurfaces;
11 int numVisBytes;
12 int numDrawVerts;
13 int numDrawSurfaces;
14 int numbrushes;
15 int numbrushsides;
16 int numleafbrushes;
17
18 byte *visBytes = NULL;
19 dnode_t *dnodes = NULL;
20 dplane_t *dplanes = NULL;
21 dleaf_t *dleafs = NULL;
22 qdrawVert_t *drawVerts = NULL;
23 dsurface_t *drawSurfaces = NULL;
24 int *dleafsurfaces = NULL;
25 dbrush_t *dbrushes = NULL;
26 dbrushside_t *dbrushsides = NULL;
27 int *dleafbrushes = NULL;
28
29 const int BSP_IDENT = (('P' << 24) + ('S' << 16) + ('B' << 8) + 'I');
30 const int Q3_BSP_VERSION = 46;
31 const int WOLF_BSP_VERSION = 47;
32
33 /*
34    ================
35    FileLength
36    ================
37  */
38 int FileLength(FILE *f)
39 {
40     int pos;
41     int end;
42
43     pos = ftell(f);
44     fseek(f, 0, SEEK_END);
45     end = ftell(f);
46     fseek(f, pos, SEEK_SET);
47
48     return end;
49 }
50
51 /*
52    ==============
53    LoadFile
54    ==============
55  */
56 bool LoadFile(const char *filename, byte **bufferptr)
57 {
58     FILE *f;
59     int length;
60     byte *buffer;
61
62     f = fopen(filename, "rb");
63     if (!f) {
64         return false;
65     }
66
67     length = FileLength(f);
68     buffer = new byte[length + 1];
69     buffer[length] = 0;
70     assert(fread(buffer, 1, length, f));
71     fclose(f);
72
73     *bufferptr = buffer;
74     return true;
75 }
76
77 int LittleLong(int l)
78 {
79     if (GDEF_ARCH_ENDIAN_BIG) {
80         std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(int));
81     }
82     return l;
83 }
84
85 float LittleFloat(float l)
86 {
87     if (GDEF_ARCH_ENDIAN_BIG) {
88         std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(float));
89     }
90     return l;
91 }
92
93 /*
94    =============
95    SwapBlock
96
97    If all values are 32 bits, this can be used to swap everything
98    =============
99  */
100 void SwapBlock(int *block, int sizeOfBlock)
101 {
102     int i;
103
104     sizeOfBlock >>= 2;
105     for (i = 0; i < sizeOfBlock; i++) {
106         block[i] = LittleLong(block[i]);
107     }
108 }
109
110 /*
111    =============
112    SwapBSPFile
113
114    Byte swaps all data in a bsp file.
115    =============
116  */
117 void SwapBSPFile(void)
118 {
119     int i;
120
121     // models
122 //      SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
123
124     // shaders (don't swap the name)
125 //      for ( i = 0 ; i < numShaders ; i++ ) {
126 //              dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
127 //              dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
128 //      }
129
130     // planes
131     SwapBlock((int *) dplanes, numplanes * sizeof(dplanes[0]));
132
133     // nodes
134     SwapBlock((int *) dnodes, numnodes * sizeof(dnodes[0]));
135
136     // leafs
137     SwapBlock((int *) dleafs, numleafs * sizeof(dleafs[0]));
138
139     // leaffaces
140     SwapBlock((int *) dleafsurfaces, numleafsurfaces * sizeof(dleafsurfaces[0]));
141
142     // leafbrushes
143     SwapBlock((int *) dleafbrushes, numleafbrushes * sizeof(dleafbrushes[0]));
144
145     // brushes
146     SwapBlock((int *) dbrushes, numbrushes * sizeof(dbrushes[0]));
147
148     // brushsides
149     SwapBlock((int *) dbrushsides, numbrushsides * sizeof(dbrushsides[0]));
150
151     // vis
152     ((int *) &visBytes)[0] = LittleLong(((int *) &visBytes)[0]);
153     ((int *) &visBytes)[1] = LittleLong(((int *) &visBytes)[1]);
154
155     // drawverts (don't swap colors )
156     for (i = 0; i < numDrawVerts; i++) {
157         drawVerts[i].lightmap[0] = LittleFloat(drawVerts[i].lightmap[0]);
158         drawVerts[i].lightmap[1] = LittleFloat(drawVerts[i].lightmap[1]);
159         drawVerts[i].st[0] = LittleFloat(drawVerts[i].st[0]);
160         drawVerts[i].st[1] = LittleFloat(drawVerts[i].st[1]);
161         drawVerts[i].xyz[0] = LittleFloat(drawVerts[i].xyz[0]);
162         drawVerts[i].xyz[1] = LittleFloat(drawVerts[i].xyz[1]);
163         drawVerts[i].xyz[2] = LittleFloat(drawVerts[i].xyz[2]);
164         drawVerts[i].normal[0] = LittleFloat(drawVerts[i].normal[0]);
165         drawVerts[i].normal[1] = LittleFloat(drawVerts[i].normal[1]);
166         drawVerts[i].normal[2] = LittleFloat(drawVerts[i].normal[2]);
167     }
168
169     // drawindexes
170 //      SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
171
172     // drawsurfs
173     SwapBlock((int *) drawSurfaces, numDrawSurfaces * sizeof(drawSurfaces[0]));
174
175     // fogs
176 //      for ( i = 0 ; i < numFogs ; i++ ) {
177 //              dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
178 //              dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
179 //      }
180 }
181
182 /*
183    =============
184    CopyLump
185    =============
186  */
187 int CopyLump(dheader_t *header, int lump, void **dest, int size)
188 {
189     int length, ofs;
190
191     length = header->lumps[lump].filelen;
192     ofs = header->lumps[lump].fileofs;
193
194     if (length == 0) {
195         return 0;
196     }
197
198     *dest = new byte[length];
199     memcpy(*dest, (byte *) header + ofs, length);
200
201     return length / size;
202 }
203
204 /*
205    =============
206    LoadBSPFile
207    =============
208  */
209 bool LoadBSPFile(const char *filename)
210 {
211     dheader_t *header;
212
213     // load the file header
214     if (!LoadFile(filename, (byte **) &header)) {
215         return false;
216     }
217
218     // swap the header
219     SwapBlock((int *) header, sizeof(*header));
220
221     if (header->ident != BSP_IDENT) {
222         DoMessageBox("Cant find a valid IBSP file", "Error", eMB_OK);
223         return false;
224     }
225     if ((header->version != Q3_BSP_VERSION) &&
226         (header->version != WOLF_BSP_VERSION)) {
227         DoMessageBox("File is incorrect version", "Error", eMB_OK);
228         return false;
229     }
230
231     numbrushsides = CopyLump(header, LUMP_BRUSHES, (void **) &dbrushsides, sizeof(dbrushside_t));
232     numbrushes = CopyLump(header, LUMP_BRUSHES, (void **) &dbrushes, sizeof(dbrush_t));
233     numplanes = CopyLump(header, LUMP_PLANES, (void **) &dplanes, sizeof(dplane_t));
234     numleafs = CopyLump(header, LUMP_LEAFS, (void **) &dleafs, sizeof(dleaf_t));
235     numnodes = CopyLump(header, LUMP_NODES, (void **) &dnodes, sizeof(dnode_t));
236     numDrawVerts = CopyLump(header, LUMP_DRAWVERTS, (void **) &drawVerts, sizeof(qdrawVert_t));
237     numDrawSurfaces = CopyLump(header, LUMP_SURFACES, (void **) &drawSurfaces, sizeof(dsurface_t));
238     numleafsurfaces = CopyLump(header, LUMP_LEAFSURFACES, (void **) &dleafsurfaces, sizeof(int));
239     numVisBytes = CopyLump(header, LUMP_VISIBILITY, (void **) &visBytes, 1);
240     numleafbrushes = CopyLump(header, LUMP_LEAFBRUSHES, (void **) &dleafbrushes, sizeof(int));
241
242     delete header;      // everything has been copied out
243
244     // swap everything
245     SwapBSPFile();
246
247     return true;
248 }
249
250 void FreeBSPData()
251 {
252     if (visBytes) {
253         delete visBytes;
254     }
255     if (dnodes) {
256         delete dnodes;
257     }
258     if (dplanes) {
259         delete dplanes;
260     }
261     if (dleafs) {
262         delete dleafs;
263     }
264     if (drawVerts) {
265         delete drawVerts;
266     }
267     if (drawSurfaces) {
268         delete drawSurfaces;
269     }
270     if (dleafsurfaces) {
271         delete dleafsurfaces;
272     }
273     if (dleafbrushes) {
274         delete dleafbrushes;
275     }
276     if (dbrushes) {
277         delete dbrushes;
278     }
279     if (dbrushsides) {
280         delete dbrushsides;
281     }
282 }