From 0c898534250db569cf4635822f6d4dfcb7940dfc Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Wed, 10 Jan 2018 16:51:23 +0000
Subject: [PATCH] created W_IsLumpWad as a shortcut for the "is the lump a WAD"
 checking in the code getting map data lumps

---
 src/p_saveg.c |  6 +-----
 src/p_setup.c |  4 +---
 src/p_spec.c  |  3 +--
 src/w_wad.c   | 13 +++++++++++++
 src/w_wad.h   |  2 ++
 5 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/src/p_saveg.c b/src/p_saveg.c
index 01711a737f..029df08f4b 100644
--- a/src/p_saveg.c
+++ b/src/p_saveg.c
@@ -502,14 +502,11 @@ static void P_NetArchiveWorld(void)
 	maplinedef_t *mld;
 	const sector_t *ss = sectors;
 	UINT8 diff, diff2;
-	char *lumpfullName;
 
 	WRITEUINT32(save_p, ARCHIVEBLOCK_WORLD);
 	put = save_p;
 
-	lumpfullName = (wadfiles[WADFILENUM(lastloadedmaplumpnum)]->lumpinfo + LUMPNUM(lastloadedmaplumpnum))->name2;
-
-	if (!strnicmp(lumpfullName + strlen(lumpfullName) - 4, ".wad", 4)) // welp it's a map wad in a pk3
+	if (W_IsLumpWad(lastloadedmaplumpnum)) // welp it's a map wad in a pk3
 	{ // HACK: Open wad file rather quickly so we can get the data from the relevant lumps
 		UINT8 *wadData = W_CacheLumpNum(lastloadedmaplumpnum, PU_STATIC);
 		filelump_t *fileinfo = (filelump_t *)(wadData + ((wadinfo_t *)wadData)->infotableofs);
@@ -658,7 +655,6 @@ static void P_NetArchiveWorld(void)
 
 	WRITEUINT16(put, 0xffff);
 
-
 	// do lines
 	for (i = 0; i < numlines; i++, mld++, li++)
 	{
diff --git a/src/p_setup.c b/src/p_setup.c
index 497bba4458..a9fc57652f 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -2658,7 +2658,6 @@ boolean P_SetupLevel(boolean skipprecip)
 	// use gamemap to get map number.
 	// 99% of the things already did, so.
 	// Map header should always be in place at this point
-	char *lumpfullName;
 	INT32 i, loadprecip = 1, ranspecialwipe = 0;
 	INT32 loademblems = 1;
 	INT32 fromnetsave = 0;
@@ -2841,8 +2840,7 @@ boolean P_SetupLevel(boolean skipprecip)
 	// As it is implemented right now, we're assuming an uncompressed WAD.
 	// (As in, a normal PWAD, not ZWAD or anything. The lump itself can be compressed.)
 	// We're not accounting for extra lumps and scrambled lump positions. Any additional data will cause an error.
-	lumpfullName = (wadfiles[WADFILENUM(lastloadedmaplumpnum)]->lumpinfo + LUMPNUM(lastloadedmaplumpnum))->name2;
-	if (!strnicmp(lumpfullName + strlen(lumpfullName) - 4, ".wad", 4))
+	if (W_IsLumpWad(lastloadedmaplumpnum))
 	{
 		// Remember that we're assuming that the WAD will have a specific set of lumps in a specific order.
 		UINT8 *wadData = W_CacheLumpNum(lastloadedmaplumpnum, PU_STATIC);
diff --git a/src/p_spec.c b/src/p_spec.c
index 498ebcf7d2..0b005baff8 100644
--- a/src/p_spec.c
+++ b/src/p_spec.c
@@ -6307,9 +6307,8 @@ void P_SpawnSpecials(INT32 fromnetsave)
 				{
 					UINT8 *data;
 					UINT16 b;
-					char *lumpfullName = (wadfiles[WADFILENUM(lastloadedmaplumpnum)]->lumpinfo + LUMPNUM(lastloadedmaplumpnum))->name2;
 
-					if (!strnicmp(lumpfullName + strlen(lumpfullName) - 4, ".wad", 4)) // welp it's a map wad in a pk3
+					if (W_IsLumpWad(lastloadedmaplumpnum)) // welp it's a map wad in a pk3
 					{ // HACK: Open wad file rather quickly so we can get the data from the sidedefs lump
 						UINT8 *wadData = W_CacheLumpNum(lastloadedmaplumpnum, PU_STATIC);
 						filelump_t *fileinfo = (filelump_t *)(wadData + ((wadinfo_t *)wadData)->infotableofs);
diff --git a/src/w_wad.c b/src/w_wad.c
index 0dea2c3525..816bbd44f4 100644
--- a/src/w_wad.c
+++ b/src/w_wad.c
@@ -1051,6 +1051,19 @@ size_t W_LumpLength(lumpnum_t lumpnum)
 	return W_LumpLengthPwad(WADFILENUM(lumpnum),LUMPNUM(lumpnum));
 }
 
+//
+// W_IsLumpWad
+// Is the lump a WAD? (presumably in a PK3)
+//
+boolean W_IsLumpWad(lumpnum_t lumpnum)
+{
+	const char *lumpfullName = (wadfiles[WADFILENUM(lumpnum)]->lumpinfo + LUMPNUM(lumpnum))->name2;
+
+	if (strlen(lumpfullName) < 4)
+		return false; // can't possibly be a wad can it?
+	return !strnicmp(lumpfullName + strlen(lumpfullName) - 4, ".wad", 4);
+}
+
 /* report a zlib or i/o error */
 void zerr(int ret)
 {
diff --git a/src/w_wad.h b/src/w_wad.h
index 26e87f3c2d..ef4213579f 100644
--- a/src/w_wad.h
+++ b/src/w_wad.h
@@ -139,6 +139,8 @@ UINT8 W_LumpExists(const char *name); // Lua uses this.
 size_t W_LumpLengthPwad(UINT16 wad, UINT16 lump);
 size_t W_LumpLength(lumpnum_t lumpnum);
 
+boolean W_IsLumpWad(lumpnum_t lumpnum); // for loading maps from WADs in PK3s
+
 void zerr(int ret); // zlib error checking
 
 size_t W_ReadLumpHeaderPwad(UINT16 wad, UINT16 lump, void *dest, size_t size, size_t offset);
-- 
GitLab