1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
//------------------------------------------------------------------------
// WAD : Wad file read/write functions.
//------------------------------------------------------------------------
//
// GL-Node Viewer (C) 2004-2007 Andrew Apted
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------
// this includes everything we need
#include "defs.h"
#define DEBUG_DIR 1
#define DEBUG_LUMP 0
#define APPEND_BLKSIZE 256
#define LEVNAME_BUNCH 20
#define ALIGN_LEN(len) ((((len) + 3) / 4) * 4)
// Global variables
wad_c *the_wad;
wad_c *the_gwa;
wad_c::wad_c() :
in_file(NULL), kind(-1),
num_entries(0), dir_start(-1),
dir(), current_level(NULL),
level_names(NULL), num_level_names(0)
{
}
wad_c::~wad_c()
{
if (in_file)
fclose(in_file);
/* free directory entries */
for (lump_c *cur = (lump_c *)dir.pop_front(); cur != NULL;
cur = (lump_c *)dir.pop_front())
{
delete cur;
}
/* free the level names */
if (level_names)
{
for (int i = 0; i < num_level_names; i++)
UtilFree((void *) level_names[i]);
UtilFree(level_names);
}
}
level_c::level_c() :
flags(0), children(),
soft_limit(0), hard_limit(0), v3_switch(0)
{
}
level_c::~level_c()
{
for (lump_c *cur = (lump_c *)children.pop_front(); cur != NULL;
cur = (lump_c *)children.pop_front())
{
delete cur;
}
}
lump_c::lump_c() :
name(NULL), start(-1), length(0), flags(0),
data(NULL), lev_info(NULL)
{
}
lump_c::~lump_c()
{
delete lev_info;
if (data)
UtilFree((void*)data);
if (name)
UtilFree((void*)name);
}
/* ---------------------------------------------------------------- */
#define NUM_LEVEL_LUMPS 12
#define NUM_GL_LUMPS 5
static const char *level_lumps[NUM_LEVEL_LUMPS]=
{
"THINGS", "LINEDEFS", "SIDEDEFS", "VERTEXES", "SEGS",
"SSECTORS", "NODES", "SECTORS", "REJECT", "BLOCKMAP",
"BEHAVIOR", // <-- hexen support
"SCRIPTS" // -JL- Lump with script sources
};
static const char *gl_lumps[NUM_GL_LUMPS]=
{
"GL_VERT", "GL_SEGS", "GL_SSECT", "GL_NODES",
"GL_PVS" // -JL- PVS (Potentially Visible Set) lump
};
//
// CheckMagic
//
static bool CheckMagic(const char type[4])
{
if ((type[0] == 'I' || type[0] == 'P') &&
type[1] == 'W' && type[2] == 'A' && type[3] == 'D')
{
return true;
}
return false;
}
//
// CheckLevelName
//
bool wad_c::CheckLevelName(const char *name)
{
for (int i = 0; i < num_level_names; i++)
{
if (strcmp(level_names[i], name) == 0)
return true;
}
return false;
}
static bool HasGLPrefix(const char *name)
{
return (name[0] == 'G' && name[1] == 'L' && name[2] == '_');
}
///---bool wad_c::CheckLevelNameGL(const char *name)
///---{
///--- if (name[0] != 'G' || name[1] != 'L' || name[2] != '_')
///--- return false;
///---
///--- return CheckLevelName(name+3);
///---}
//
// CheckLevelLumpName
//
// Tests if the entry name is one of the level lumps.
//
static bool CheckLevelLumpName(const char *name)
{
for (int i = 0; i < NUM_LEVEL_LUMPS; i++)
{
if (strcmp(name, level_lumps[i]) == 0)
return true;
}
return false;
}
//
// CheckGLLumpName
//
// Tests if the entry name matches one of the GL lump names.
//
static bool CheckGLLumpName(const char *name)
{
for (int i = 0; i < NUM_GL_LUMPS; i++)
{
if (strcmp(name, gl_lumps[i]) == 0)
return true;
}
return false;
}
//
// NewLevel
//
// Create new level information
//
static level_c *NewLevel(int flags) // FIXME @@@@
{
level_c *cur = new level_c();
cur->flags = flags;
return cur;
}
//
// NewLump
//
// Create new lump. 'name' must be allocated storage.
//
static lump_c *NewLump(const char *name) // FIXME @@@@
{
lump_c *cur = new lump_c();
cur->name = name;
return cur;
}
//
// ReadHeader
//
// Returns true if successful, or FALSE if there was a problem (in
// which case the error message as been setup).
//
bool wad_c::ReadHeader()
{
raw_wad_header_t header;
size_t len = fread(&header, sizeof(header), 1, in_file);
if (len != 1)
{
PrintWarn("Trouble reading wad header: %s\n", strerror(errno));
return false;
}
if (! CheckMagic(header.type))
{
PrintWarn("This file is not a WAD file : bad magic\n");
return false;
}
kind = (header.type[0] == 'I') ? IWAD : PWAD;
num_entries = UINT32(header.num_entries);
dir_start = UINT32(header.dir_start);
return true;
}
//
// ReadDirEntry
//
void wad_c::ReadDirEntry()
{
raw_wad_entry_t entry;
size_t len = fread(&entry, sizeof(entry), 1, in_file);
if (len != 1)
FatalError("Trouble reading wad directory");
lump_c *lump = NewLump(UtilStrNDup(entry.name, 8));
lump->start = UINT32(entry.start);
lump->length = UINT32(entry.length);
#if DEBUG_DIR
PrintDebug("Read dir... %s\n", lump->name);
#endif
dir.push_back(lump);
}
//
// Level name helper
//
void wad_c::AddLevelName(const char *name)
{
if ((num_level_names % LEVNAME_BUNCH) == 0)
{
level_names = (const char **) UtilRealloc((void *)level_names,
(num_level_names + LEVNAME_BUNCH) * sizeof(const char *));
}
level_names[num_level_names++] = UtilStrDup(name);
}
//
// DetermineLevelNames
//
void wad_c::DetermineLevelNames()
{
for (lump_c *L = (lump_c*)dir.begin(); L != NULL; L = L->LumpNext())
{
// check if the next four lumps after the current lump match the
// level-lump or GL-lump names.
int i;
lump_c *N;
int normal_count = 0;
int gl_count = 0;
for (i = 0, N = L->LumpNext();
(i < 4) && (N != NULL);
i++, N = N->LumpNext())
{
if (strcmp(N->name, level_lumps[i]) == 0)
normal_count++;
if (strcmp(N->name, gl_lumps[i]) == 0)
gl_count++;
}
if (normal_count != 4 && gl_count != 4)
continue;
#if DEBUG_DIR
PrintDebug("Found level name: %s\n", L->name);
#endif
// check for invalid name and duplicate levels
if (normal_count == 4 && strlen(L->name) > 5)
PrintWarn("Bad level '%s' in wad (name too long)\n", L->name);
else if (CheckLevelName(L->name))
PrintWarn("Level name '%s' found twice in wad\n", L->name);
else
AddLevelName(L->name);
}
}
//
// ProcessDirEntry
//
void wad_c::ProcessDirEntry(lump_c *lump)
{
// --- LEVEL MARKERS ---
if (CheckLevelName(lump->name))
{
lump->lev_info = NewLevel(0);
current_level = lump;
#if DEBUG_DIR
PrintDebug("Process level... %s\n", lump->name);
#endif
dir.push_back(lump);
return;
}
// --- LEVEL LUMPS ---
if (current_level)
{
if (HasGLPrefix(current_level->name) ?
CheckGLLumpName(lump->name) : CheckLevelLumpName(lump->name))
{
// check for duplicates
if (FindLumpInLevel(lump->name))
{
PrintWarn("Duplicate entry '%s' ignored in %s\n",
lump->name, current_level->name);
delete lump;
return;
}
#if DEBUG_DIR
PrintDebug(" |--- %s\n", lump->name);
#endif
// link it in
current_level->lev_info->children.push_back(lump);
return;
}
// non-level lump -- end previous level and fall through.
current_level = NULL;
}
// --- ORDINARY LUMPS ---
#if DEBUG_DIR
PrintDebug("Process dir... %s\n", lump->name);
#endif
if (CheckLevelLumpName(lump->name))
PrintWarn("Level lump '%s' found outside any level\n", lump->name);
else if (CheckGLLumpName(lump->name))
PrintWarn("GL lump '%s' found outside any level\n", lump->name);
// link it in
dir.push_back(lump);
}
//
// ReadDirectory
//
void wad_c::ReadDirectory()
{
fseek(in_file, dir_start, SEEK_SET);
for (int i = 0; i < num_entries; i++)
{
ReadDirEntry();
}
DetermineLevelNames();
// finally, unlink all lumps and process each one in turn
list_c temp(dir);
dir.clear();
for (lump_c *cur = (lump_c *) temp.pop_front(); cur;
cur = (lump_c *) temp.pop_front())
{
ProcessDirEntry(cur);
}
}
/* ---------------------------------------------------------------- */
//
// CacheLump
//
void wad_c::CacheLump(lump_c *lump)
{
size_t len;
#if DEBUG_LUMP
PrintDebug("Reading... %s (%d)\n", lump->name, lump->length);
#endif
if (lump->length == 0)
return;
lump->data = UtilCalloc(lump->length);
fseek(in_file, lump->start, SEEK_SET);
len = fread(lump->data, lump->length, 1, in_file);
if (len != 1)
{
if (current_level)
PrintWarn("Trouble reading lump '%s' in %s\n",
lump->name, current_level->name);
else
PrintWarn("Trouble reading lump '%s'\n", lump->name);
}
}
//
// FindLevel
//
bool wad_c::FindLevel(const char *map_name)
{
lump_c *L;
for (L = (lump_c*)dir.begin(); L != NULL; L = L->LumpNext())
{
if (! L->lev_info)
continue;
if (L->lev_info->flags & LEVEL_IS_GL) // @@@@
continue;
if (UtilStrCaseCmp(L->name, map_name) == 0)
break;
}
current_level = L;
return (L != NULL);
}
//
// FirstLevelName
//
const char *wad_c::FirstLevelName()
{
lump_c *L;
for (L = (lump_c*)dir.begin(); L != NULL; L = L->LumpNext())
{
if (L->lev_info)
return L->name;
}
return NULL;
}
//
// FindLumpInLevel
//
lump_c *wad_c::FindLumpInLevel(const char *name)
{
SYS_ASSERT(current_level);
for (lump_c *L = (lump_c*)current_level->lev_info->children.begin();
L != NULL;
L = L->LumpNext())
{
if (strcmp(L->name, name) == 0)
return L;
}
return NULL; // not found
}
//
// wad_c::Load
//
wad_c *wad_c::Load(const char *filename)
{
wad_c *wad = new wad_c();
// open input wad file & read header
wad->in_file = fopen(filename, "rb");
if (! wad->in_file)
{
PrintWarn("Cannot open WAD file %s : %s", filename, strerror(errno));
return NULL;
}
if (! wad->ReadHeader())
return NULL;
PrintDebug("Opened %cWAD file : %s\n", (wad->kind == IWAD) ? 'I' : 'P',
filename);
PrintDebug("Reading %d dir entries at 0x%X\n", wad->num_entries,
wad->dir_start);
// read directory
wad->ReadDirectory();
wad->current_level = NULL;
return wad;
}
|