| 1 |
//----------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright 2017 Christoph Oelckers |
| 4 |
// |
| 5 |
// This program is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 2 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// This program is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with this program. If not, see http://www.gnu.org/licenses/ |
| 17 |
// |
| 18 |
//----------------------------------------------------------------------------- |
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <ctype.h> |
| 23 |
#include <assert.h> |
| 24 |
#include "umapinfo.h" |
| 25 |
#include "scanner.h" |
| 26 |
|
| 27 |
extern "C" |
| 28 |
{ |
| 29 |
#include "m_misc.h" |
| 30 |
#include "g_game.h" |
| 31 |
#include "doomdef.h" |
| 32 |
#include "doomstat.h" |
| 33 |
|
| 34 |
#include "dsda/episode.h" |
| 35 |
#include "dsda/name.h" |
| 36 |
|
| 37 |
MapList Maps; |
| 38 |
} |
| 39 |
|
| 40 |
// ----------------------------------------------- |
| 41 |
// |
| 42 |
// |
| 43 |
// ----------------------------------------------- |
| 44 |
|
| 45 |
static void FreeMap(MapEntry *mape) |
| 46 |
{ |
| 47 |
if (mape->mapname) Z_Free(mape->mapname); |
| 48 |
if (mape->levelname) Z_Free(mape->levelname); |
| 49 |
if (mape->label) Z_Free(mape->label); |
| 50 |
if (mape->author) Z_Free(mape->author); |
| 51 |
if (mape->intertext) Z_Free(mape->intertext); |
| 52 |
if (mape->intertextsecret) Z_Free(mape->intertextsecret); |
| 53 |
if (mape->properties) Z_Free(mape->properties); |
| 54 |
if (mape->bossactions) Z_Free(mape->bossactions); |
| 55 |
mape->propertycount = 0; |
| 56 |
mape->mapname = NULL; |
| 57 |
mape->properties = NULL; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
void FreeMapList() |
| 62 |
{ |
| 63 |
unsigned i; |
| 64 |
|
| 65 |
for(i = 0; i < Maps.mapcount; i++) |
| 66 |
{ |
| 67 |
FreeMap(&Maps.maps[i]); |
| 68 |
} |
| 69 |
Z_Free(Maps.maps); |
| 70 |
Maps.maps = NULL; |
| 71 |
Maps.mapcount = 0; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
void ReplaceString(char **pptr, const char *newstring) |
| 76 |
{ |
| 77 |
if (*pptr != NULL) Z_Free(*pptr); |
| 78 |
*pptr = Z_Strdup(newstring); |
| 79 |
} |
| 80 |
|
| 81 |
// ----------------------------------------------- |
| 82 |
// |
| 83 |
// Parses a set of string and concatenates them |
| 84 |
// |
| 85 |
// ----------------------------------------------- |
| 86 |
|
| 87 |
static char *ParseMultiString(Scanner &scanner, int error) |
| 88 |
{ |
| 89 |
char *build = NULL; |
| 90 |
|
| 91 |
if (scanner.CheckToken(TK_Identifier)) |
| 92 |
{ |
| 93 |
if (scanner.StringMatch("clear")) |
| 94 |
{ |
| 95 |
return Z_Strdup("-"); // this was explicitly deleted to override the default. |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
scanner.ErrorF("Either 'clear' or string constant expected"); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
do |
| 104 |
{ |
| 105 |
scanner.MustGetToken(TK_StringConst); |
| 106 |
if (build == NULL) build = Z_Strdup(scanner.string); |
| 107 |
else |
| 108 |
{ |
| 109 |
size_t newlen = strlen(build) + strlen(scanner.string) + 2; // strlen for both the existing text and the new line, plus room for one \n and one \0 |
| 110 |
build = (char*)Z_Realloc(build, newlen); // Prepare the destination memory for the below strcats |
| 111 |
strcat(build, "\n"); // Replace the existing text's \0 terminator with a \n |
| 112 |
strcat(build, scanner.string); // Concatenate the new line onto the existing text |
| 113 |
} |
| 114 |
} while (scanner.CheckToken(',')); |
| 115 |
return build; |
| 116 |
} |
| 117 |
|
| 118 |
// ----------------------------------------------- |
| 119 |
// |
| 120 |
// Parses a lump name. The buffer must be at least 9 characters. |
| 121 |
// |
| 122 |
// ----------------------------------------------- |
| 123 |
|
| 124 |
static int ParseLumpName(Scanner &scanner, char *buffer) |
| 125 |
{ |
| 126 |
scanner.MustGetToken(TK_StringConst); |
| 127 |
if (strlen(scanner.string) > 8) |
| 128 |
{ |
| 129 |
scanner.ErrorF("String too long. Maximum size is 8 characters."); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
strncpy(buffer, scanner.string, 8); |
| 133 |
buffer[8] = 0; |
| 134 |
M_Strupr(buffer); |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
// ----------------------------------------------- |
| 139 |
// |
| 140 |
// Parses a standard property that is already known |
| 141 |
// These do not get stored in the property list |
| 142 |
// but in dedicated struct member variables. |
| 143 |
// |
| 144 |
// ----------------------------------------------- |
| 145 |
|
| 146 |
static int ParseStandardProperty(Scanner &scanner, MapEntry *mape) |
| 147 |
{ |
| 148 |
// find the next line with content. |
| 149 |
// this line is no property. |
| 150 |
|
| 151 |
scanner.MustGetToken(TK_Identifier); |
| 152 |
char *pname = Z_Strdup(scanner.string); |
| 153 |
scanner.MustGetToken('='); |
| 154 |
|
| 155 |
if (!stricmp(pname, "levelname")) |
| 156 |
{ |
| 157 |
scanner.MustGetToken(TK_StringConst); |
| 158 |
ReplaceString(&mape->levelname, scanner.string); |
| 159 |
} |
| 160 |
else if (!stricmp(pname, "label")) |
| 161 |
{ |
| 162 |
if (scanner.CheckToken(TK_Identifier)) |
| 163 |
{ |
| 164 |
if (scanner.StringMatch("clear")) ReplaceString(&mape->label, "-"); |
| 165 |
else |
| 166 |
{ |
| 167 |
scanner.ErrorF("Either 'clear' or string constant expected"); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
scanner.MustGetToken(TK_StringConst); |
| 174 |
ReplaceString(&mape->label, scanner.string); |
| 175 |
} |
| 176 |
} |
| 177 |
else if (!stricmp(pname, "author")) |
| 178 |
{ |
| 179 |
scanner.MustGetToken(TK_StringConst); |
| 180 |
ReplaceString(&mape->author, scanner.string); |
| 181 |
} |
| 182 |
else if (!stricmp(pname, "next")) |
| 183 |
{ |
| 184 |
ParseLumpName(scanner, mape->nextmap); |
| 185 |
if (!G_ValidateMapName(mape->nextmap, NULL, NULL)) |
| 186 |
{ |
| 187 |
scanner.ErrorF("Invalid map name %s.", mape->nextmap); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
} |
| 191 |
else if (!stricmp(pname, "nextsecret")) |
| 192 |
{ |
| 193 |
ParseLumpName(scanner, mape->nextsecret); |
| 194 |
if (!G_ValidateMapName(mape->nextsecret, NULL, NULL)) |
| 195 |
{ |
| 196 |
scanner.ErrorF("Invalid map name %s", mape->nextsecret); |
| 197 |
return 0; |
| 198 |
} |
| 199 |
} |
| 200 |
else if (!stricmp(pname, "levelpic")) |
| 201 |
{ |
| 202 |
ParseLumpName(scanner, mape->levelpic); |
| 203 |
} |
| 204 |
else if (!stricmp(pname, "skytexture")) |
| 205 |
{ |
| 206 |
ParseLumpName(scanner, mape->skytexture); |
| 207 |
} |
| 208 |
else if (!stricmp(pname, "music")) |
| 209 |
{ |
| 210 |
ParseLumpName(scanner, mape->music); |
| 211 |
} |
| 212 |
else if (!stricmp(pname, "endpic")) |
| 213 |
{ |
| 214 |
ParseLumpName(scanner, mape->endpic); |
| 215 |
} |
| 216 |
else if (!stricmp(pname, "endcast")) |
| 217 |
{ |
| 218 |
scanner.MustGetToken(TK_BoolConst); |
| 219 |
if (scanner.boolean) strcpy(mape->endpic, "$CAST"); |
| 220 |
else strcpy(mape->endpic, "-"); |
| 221 |
} |
| 222 |
else if (!stricmp(pname, "endbunny")) |
| 223 |
{ |
| 224 |
scanner.MustGetToken(TK_BoolConst); |
| 225 |
if (scanner.boolean) strcpy(mape->endpic, "$BUNNY"); |
| 226 |
else strcpy(mape->endpic, "-"); |
| 227 |
} |
| 228 |
else if (!stricmp(pname, "endgame")) |
| 229 |
{ |
| 230 |
scanner.MustGetToken(TK_BoolConst); |
| 231 |
if (scanner.boolean) strcpy(mape->endpic, "!"); |
| 232 |
else strcpy(mape->endpic, "-"); |
| 233 |
} |
| 234 |
else if (!stricmp(pname, "exitpic")) |
| 235 |
{ |
| 236 |
ParseLumpName(scanner, mape->exitpic); |
| 237 |
} |
| 238 |
else if (!stricmp(pname, "enterpic")) |
| 239 |
{ |
| 240 |
ParseLumpName(scanner, mape->enterpic); |
| 241 |
} |
| 242 |
else if (!stricmp(pname, "nointermission")) |
| 243 |
{ |
| 244 |
scanner.MustGetToken(TK_BoolConst); |
| 245 |
mape->nointermission = scanner.boolean; |
| 246 |
} |
| 247 |
else if (!stricmp(pname, "partime")) |
| 248 |
{ |
| 249 |
scanner.MustGetInteger(); |
| 250 |
mape->partime = TICRATE * scanner.number; |
| 251 |
} |
| 252 |
else if (!stricmp(pname, "intertext")) |
| 253 |
{ |
| 254 |
char *lname = ParseMultiString(scanner, 1); |
| 255 |
if (!lname) return 0; |
| 256 |
if (mape->intertext != NULL) Z_Free(mape->intertext); |
| 257 |
mape->intertext = lname; |
| 258 |
} |
| 259 |
else if (!stricmp(pname, "intertextsecret")) |
| 260 |
{ |
| 261 |
char *lname = ParseMultiString(scanner, 1); |
| 262 |
if (!lname) return 0; |
| 263 |
if (mape->intertextsecret != NULL) Z_Free(mape->intertextsecret); |
| 264 |
mape->intertextsecret = lname; |
| 265 |
} |
| 266 |
else if (!stricmp(pname, "interbackdrop")) |
| 267 |
{ |
| 268 |
ParseLumpName(scanner, mape->interbackdrop); |
| 269 |
} |
| 270 |
else if (!stricmp(pname, "intermusic")) |
| 271 |
{ |
| 272 |
ParseLumpName(scanner, mape->intermusic); |
| 273 |
} |
| 274 |
else if (!stricmp(pname, "episode")) |
| 275 |
{ |
| 276 |
if (scanner.CheckToken(TK_Identifier)) |
| 277 |
{ |
| 278 |
if (scanner.StringMatch("clear")) dsda_ClearEpisodes(); |
| 279 |
else |
| 280 |
{ |
| 281 |
scanner.ErrorF("Either 'clear' or string constant expected"); |
| 282 |
return 0; |
| 283 |
} |
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
char lumpname[9] = {0}; |
| 288 |
char *alttext = NULL; |
| 289 |
char key = 0; |
| 290 |
|
| 291 |
ParseLumpName(scanner, lumpname); |
| 292 |
if (scanner.CheckToken(',')) |
| 293 |
{ |
| 294 |
scanner.MustGetToken(TK_StringConst); |
| 295 |
alttext = Z_Strdup(scanner.string); |
| 296 |
if (scanner.CheckToken(',')) |
| 297 |
{ |
| 298 |
scanner.MustGetToken(TK_StringConst); |
| 299 |
key = tolower(scanner.string[0]); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
dsda_AddEpisode(mape->mapname, alttext, lumpname, key, false); |
| 304 |
|
| 305 |
if (alttext) Z_Free(alttext); |
| 306 |
} |
| 307 |
} |
| 308 |
else if (!stricmp(pname, "bossaction")) |
| 309 |
{ |
| 310 |
scanner.MustGetToken(TK_Identifier); |
| 311 |
int special, tag; |
| 312 |
if (scanner.StringMatch("clear")) |
| 313 |
{ |
| 314 |
// mark level free of boss actions |
| 315 |
special = tag = -1; |
| 316 |
if (mape->bossactions) Z_Free(mape->bossactions); |
| 317 |
mape->bossactions = NULL; |
| 318 |
mape->numbossactions = -1; |
| 319 |
} |
| 320 |
else |
| 321 |
{ |
| 322 |
int i; |
| 323 |
|
| 324 |
i = dsda_ActorNameToType(scanner.string); |
| 325 |
|
| 326 |
if (i == NAME_NOT_FOUND) |
| 327 |
{ |
| 328 |
scanner.ErrorF("Unknown thing type %s", scanner.string); |
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
scanner.MustGetToken(','); |
| 333 |
scanner.MustGetInteger(); |
| 334 |
special = scanner.number; |
| 335 |
scanner.MustGetToken(','); |
| 336 |
scanner.MustGetInteger(); |
| 337 |
tag = scanner.number; |
| 338 |
// allow no 0-tag specials here, unless a level exit. |
| 339 |
if (tag != 0 || special == 11 || special == 51 || special == 52 || special == 124) |
| 340 |
{ |
| 341 |
if (mape->numbossactions == -1) mape->numbossactions = 1; |
| 342 |
else mape->numbossactions++; |
| 343 |
mape->bossactions = (struct BossAction *)Z_Realloc(mape->bossactions, sizeof(struct BossAction) * mape->numbossactions); |
| 344 |
mape->bossactions[mape->numbossactions - 1].type = i; |
| 345 |
mape->bossactions[mape->numbossactions - 1].special = special; |
| 346 |
mape->bossactions[mape->numbossactions - 1].tag = tag; |
| 347 |
} |
| 348 |
|
| 349 |
} |
| 350 |
} |
| 351 |
else |
| 352 |
{ |
| 353 |
do |
| 354 |
{ |
| 355 |
if (!scanner.CheckFloat()) scanner.GetNextToken(); |
| 356 |
if (scanner.token > TK_BoolConst) |
| 357 |
{ |
| 358 |
scanner.Error(TK_Identifier); |
| 359 |
} |
| 360 |
|
| 361 |
} while (scanner.CheckToken(',')); |
| 362 |
} |
| 363 |
Z_Free(pname); |
| 364 |
return 1; |
| 365 |
} |
| 366 |
|
| 367 |
// ----------------------------------------------- |
| 368 |
// |
| 369 |
// Parses a complete map entry |
| 370 |
// |
| 371 |
// ----------------------------------------------- |
| 372 |
|
| 373 |
static int ParseMapEntry(Scanner &scanner, MapEntry *val) |
| 374 |
{ |
| 375 |
val->mapname = NULL; |
| 376 |
val->propertycount = 0; |
| 377 |
val->properties = NULL; |
| 378 |
|
| 379 |
scanner.MustGetIdentifier("map"); |
| 380 |
scanner.MustGetToken(TK_Identifier); |
| 381 |
if (!G_ValidateMapName(scanner.string, NULL, NULL)) |
| 382 |
{ |
| 383 |
scanner.ErrorF("Invalid map name %s", scanner.string); |
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
ReplaceString(&val->mapname, scanner.string); |
| 388 |
scanner.MustGetToken('{'); |
| 389 |
while(!scanner.CheckToken('}')) |
| 390 |
{ |
| 391 |
ParseStandardProperty(scanner, val); |
| 392 |
} |
| 393 |
return 1; |
| 394 |
} |
| 395 |
|
| 396 |
// ----------------------------------------------- |
| 397 |
// |
| 398 |
// Parses a complete UMAPINFO lump |
| 399 |
// |
| 400 |
// ----------------------------------------------- |
| 401 |
|
| 402 |
int ParseUMapInfo(const unsigned char *buffer, size_t length, umapinfo_errorfunc err) |
| 403 |
{ |
| 404 |
Scanner scanner((const char*)buffer, length); |
| 405 |
unsigned int i; |
| 406 |
|
| 407 |
scanner.SetErrorCallback(err); |
| 408 |
|
| 409 |
|
| 410 |
while (scanner.TokensLeft()) |
| 411 |
{ |
| 412 |
MapEntry parsed = { 0 }; |
| 413 |
ParseMapEntry(scanner, &parsed); |
| 414 |
|
| 415 |
// Set default level progression here to simplify the checks elsewhere. Doing this lets us skip all normal code for this if nothing has been defined. |
| 416 |
if (!parsed.nextmap[0] && !parsed.endpic[0]) |
| 417 |
{ |
| 418 |
if (!stricmp(parsed.mapname, "MAP30")) strcpy(parsed.endpic, "$CAST"); |
| 419 |
else if (!stricmp(parsed.mapname, "E1M8")) strcpy(parsed.endpic, gamemode == retail? "CREDIT" : "HELP2"); |
| 420 |
else if (!stricmp(parsed.mapname, "E2M8")) strcpy(parsed.endpic, "VICTORY2"); |
| 421 |
else if (!stricmp(parsed.mapname, "E3M8")) strcpy(parsed.endpic, "$BUNNY"); |
| 422 |
else if (!stricmp(parsed.mapname, "E4M8")) strcpy(parsed.endpic, "ENDPIC"); |
| 423 |
else if (gamemission == tc_chex && !stricmp(parsed.mapname, "E1M5")) strcpy(parsed.endpic, "CREDIT"); |
| 424 |
else |
| 425 |
{ |
| 426 |
int ep, map; |
| 427 |
G_ValidateMapName(parsed.mapname, &ep, &map); |
| 428 |
map++; |
| 429 |
snprintf(parsed.nextmap, sizeof(parsed.nextmap), "%s", VANILLA_MAP_LUMP_NAME(ep, map)); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
// Does this property already exist? If yes, replace it. |
| 434 |
for(i = 0; i < Maps.mapcount; i++) |
| 435 |
{ |
| 436 |
if (!strcmp(parsed.mapname, Maps.maps[i].mapname)) |
| 437 |
{ |
| 438 |
FreeMap(&Maps.maps[i]); |
| 439 |
Maps.maps[i] = parsed; |
| 440 |
break; |
| 441 |
} |
| 442 |
} |
| 443 |
// Not found so create a new one. |
| 444 |
if (i == Maps.mapcount) |
| 445 |
{ |
| 446 |
Maps.mapcount++; |
| 447 |
Maps.maps = (MapEntry*)Z_Realloc(Maps.maps, sizeof(MapEntry)*Maps.mapcount); |
| 448 |
Maps.maps[Maps.mapcount-1] = parsed; |
| 449 |
} |
| 450 |
|
| 451 |
} |
| 452 |
return 1; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
MapProperty *FindProperty(MapEntry *map, const char *name) |
| 457 |
{ |
| 458 |
return NULL; |
| 459 |
} |