From 370eff9014b01ab22e032b7514831e40d4e0c9ba Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@zdoom.fake>
Date: Fri, 1 Jan 2010 12:40:47 +0000
Subject: [PATCH] - replaced wadlist_t with an array of FStrings and added a
 list parameter to   everything that eventually calls D_AddFile. Also create
 the list of files   loaded on the command line separately to allow further
 checks on them.

SVN r2072 (trunk)
---
 docs/rh-log.txt    |   3 +
 src/c_dispatch.cpp |   3 +-
 src/d_iwad.cpp     |  14 ++---
 src/d_main.cpp     | 133 +++++++++++++++++++++++++--------------------
 src/d_main.h       |   2 +-
 src/w_wad.cpp      |  12 ++--
 src/w_wad.h        |  12 +---
 7 files changed, 93 insertions(+), 86 deletions(-)

diff --git a/docs/rh-log.txt b/docs/rh-log.txt
index e93b2a877..5326ee3aa 100644
--- a/docs/rh-log.txt
+++ b/docs/rh-log.txt
@@ -1,4 +1,7 @@
 January 1, 2010  (Changes by Graf Zahl)
+- replaced wadlist_t with an array of FStrings and added a list parameter to
+  everything that eventually calls D_AddFile. Also create the list of files
+  loaded on the command line separately to allow further checks on them.
 - fixed: The node builder did not check if all segs could be split properly.
   Also removed some fudging that tried to work around this case but produced
   a broken BSP tree on other maps.
diff --git a/src/c_dispatch.cpp b/src/c_dispatch.cpp
index 6540fc4c9..5ebe450b6 100644
--- a/src/c_dispatch.cpp
+++ b/src/c_dispatch.cpp
@@ -1376,6 +1376,7 @@ void FConsoleAlias::SafeDelete ()
 
 static BYTE PullinBad = 2;
 static const char *PullinFile;
+extern TArray<FString> allwads;
 
 int C_ExecFile (const char *file, bool usePullin)
 {
@@ -1492,7 +1493,7 @@ CCMD (pullin)
 						FixPathSeperator (path);
 					}
 				}
-				D_AddFile (path);
+				D_AddFile (allwads, path);
 				if (path != argv[i])
 				{
 					delete[] path;
diff --git a/src/d_iwad.cpp b/src/d_iwad.cpp
index dae65379d..421a35e4a 100644
--- a/src/d_iwad.cpp
+++ b/src/d_iwad.cpp
@@ -484,7 +484,7 @@ static int CheckIWAD (const char *doomwaddir, WadStuff *wads)
 //
 //==========================================================================
 
-static EIWADType IdentifyVersion (const char *zdoom_wad)
+static EIWADType IdentifyVersion (TArray<FString> &wadfiles, const char *zdoom_wad)
 {
 	WadStuff wads[countof(IWADNames)];
 	size_t foundwads[NUM_IWAD_TYPES] = { 0 };
@@ -630,14 +630,14 @@ static EIWADType IdentifyVersion (const char *zdoom_wad)
 		exit (0);
 
 	// zdoom.pk3 must always be the first file loaded and the IWAD second.
-	D_AddFile (zdoom_wad);
+	D_AddFile (wadfiles, zdoom_wad);
 
 	if (wads[pickwad].Type == IWAD_HexenDK)
 	{ // load hexen.wad before loading hexdd.wad
-		D_AddFile (wads[foundwads[IWAD_Hexen]-1].Path);
+		D_AddFile (wadfiles, wads[foundwads[IWAD_Hexen]-1].Path);
 	}
 
-	D_AddFile (wads[pickwad].Path);
+	D_AddFile (wadfiles, wads[pickwad].Path);
 
 	if (wads[pickwad].Type == IWAD_Strife)
 	{ // Try to load voices.wad along with strife1.wad
@@ -653,16 +653,16 @@ static EIWADType IdentifyVersion (const char *zdoom_wad)
 			path = FString (wads[pickwad].Path.GetChars(), lastslash + 1);
 		}
 		path += "voices.wad";
-		D_AddFile (path);
+		D_AddFile (wadfiles, path);
 	}
 
 	return wads[pickwad].Type;
 }
 
 
-const IWADInfo *D_FindIWAD(const char *basewad)
+const IWADInfo *D_FindIWAD(TArray<FString> &wadfiles, const char *basewad)
 {
-	EIWADType iwadType = IdentifyVersion(basewad);
+	EIWADType iwadType = IdentifyVersion(wadfiles, basewad);
 	gameiwad = iwadType;
 	const IWADInfo *iwad_info = &IWADInfos[iwadType];
 	I_SetIWADInfo(iwad_info);
diff --git a/src/d_main.cpp b/src/d_main.cpp
index 656875941..d59eee1e5 100644
--- a/src/d_main.cpp
+++ b/src/d_main.cpp
@@ -118,7 +118,7 @@ extern void R_ExecuteSetViewSize ();
 extern void G_NewInit ();
 extern void SetupPlayerClasses ();
 extern bool CheckCheatmode ();
-extern const IWADInfo *D_FindIWAD(const char *basewad);
+const IWADInfo *D_FindIWAD(TArray<FString> &wadfiles, const char *basewad);
 
 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
 
@@ -182,7 +182,7 @@ CVAR (Int, wipetype, 1, CVAR_ARCHIVE);
 CVAR (Int, snd_drawoutput, 0, 0);
 
 bool DrawFSHUD;				// [RH] Draw fullscreen HUD?
-wadlist_t *wadfiles;		// [RH] remove limit on # of loaded wads
+TArray<FString> allwads;
 bool devparm;				// started game with -devparm
 const char *D_DrawIcon;	// [RH] Patch name of icon to draw on next refresh
 int NoWipe;				// [RH] Allow wipe? (Needs to be set each time)
@@ -204,7 +204,6 @@ cycle_t FrameCycles;
 
 // PRIVATE DATA DEFINITIONS ------------------------------------------------
 
-static wadlist_t **wadtail = &wadfiles;
 static int demosequence;
 static int pagetic;
 
@@ -1242,7 +1241,7 @@ CCMD (endgame)
 //
 //==========================================================================
 
-void D_AddFile (const char *file, bool check)
+void D_AddFile (TArray<FString> &wadfiles, const char *file, bool check)
 {
 	if (file == NULL)
 	{
@@ -1259,12 +1258,10 @@ void D_AddFile (const char *file, bool check)
 		}
 		file = f;
 	}
-	wadlist_t *wad = (wadlist_t *)M_Malloc (sizeof(*wad) + strlen(file));
 
-	*wadtail = wad;
-	wad->next = NULL;
-	strcpy (wad->name, file);
-	wadtail = &wad->next;
+	FString f = file;
+	FixPathSeperator(f);
+	wadfiles.Push(f);
 }
 
 //==========================================================================
@@ -1273,13 +1270,13 @@ void D_AddFile (const char *file, bool check)
 //
 //==========================================================================
 
-void D_AddWildFile (const char *value)
+void D_AddWildFile (TArray<FString> &wadfiles, const char *value)
 {
 	const char *wadfile = BaseFileSearch (value, ".wad");
 
 	if (wadfile != NULL)
 	{
-		D_AddFile (wadfile);
+		D_AddFile (wadfiles, wadfile);
 	}
 	else
 	{ // Try pattern matching
@@ -1309,12 +1306,12 @@ void D_AddWildFile (const char *value)
 				{
 					if (sep == NULL)
 					{
-						D_AddFile (I_FindName (&findstate));
+						D_AddFile (wadfiles, I_FindName (&findstate));
 					}
 					else
 					{
 						strcpy (sep+1, I_FindName (&findstate));
-						D_AddFile (path);
+						D_AddFile (wadfiles, path);
 					}
 				}
 			} while (I_FindNext (handle, &findstate) == 0);
@@ -1331,7 +1328,7 @@ void D_AddWildFile (const char *value)
 //
 //==========================================================================
 
-void D_AddConfigWads (const char *section)
+void D_AddConfigWads (TArray<FString> &wadfiles, const char *section)
 {
 	if (GameConfig->SetSection (section))
 	{
@@ -1345,7 +1342,7 @@ void D_AddConfigWads (const char *section)
 			{
 				// D_AddWildFile resets GameConfig's position, so remember it
 				GameConfig->GetPosition (pos);
-				D_AddWildFile (value);
+				D_AddWildFile (wadfiles, value);
 				// Reset GameConfig's position to get next wad
 				GameConfig->SetPosition (pos);
 			}
@@ -1361,7 +1358,7 @@ void D_AddConfigWads (const char *section)
 //
 //==========================================================================
 
-static void D_AddDirectory (const char *dir)
+static void D_AddDirectory (TArray<FString> &wadfiles, const char *dir)
 {
 	char curdir[PATH_MAX];
 
@@ -1391,7 +1388,7 @@ static void D_AddDirectory (const char *dir)
 					if (!(I_FindAttr (&findstate) & FA_DIREC))
 					{
 						strcpy (skindir + stuffstart, I_FindName (&findstate));
-						D_AddFile (skindir);
+						D_AddFile (wadfiles, skindir);
 					}
 				} while (I_FindNext (handle, &findstate) == 0);
 				I_FindClose (handle);
@@ -1584,6 +1581,53 @@ void D_MultiExec (DArgs *list, bool usePullin)
 	}
 }
 
+static void GetCmdLineFiles(TArray<FString> &wadfiles)
+{
+	DArgs *files = Args->GatherFiles ("-file", ".wad", true);
+	DArgs *files1 = Args->GatherFiles (NULL, ".zip", false);
+	DArgs *files2 = Args->GatherFiles (NULL, ".pk3", false);
+	DArgs *files3 = Args->GatherFiles (NULL, ".txt", false);
+	if (files->NumArgs() > 0 || files1->NumArgs() > 0 || files2->NumArgs() > 0 || files3->NumArgs() > 0)
+	{
+		// Check for -file in shareware
+		if (gameinfo.flags & GI_SHAREWARE)
+		{
+			I_FatalError ("You cannot -file with the shareware version. Register!");
+		}
+
+		// the files gathered are wadfile/lump names
+		for (int i = 0; i < files->NumArgs(); i++)
+		{
+			D_AddWildFile (wadfiles, files->GetArg (i));
+		}
+		for (int i = 0; i < files1->NumArgs(); i++)
+		{
+			D_AddWildFile (wadfiles, files1->GetArg (i));
+		}
+		for (int i = 0; i < files2->NumArgs(); i++)
+		{
+			D_AddWildFile (wadfiles, files2->GetArg (i));
+		}
+		for (int i = 0; i < files3->NumArgs(); i++)
+		{
+			D_AddWildFile (wadfiles, files3->GetArg (i));
+		}
+	}
+	files->Destroy();
+	files1->Destroy();
+	files2->Destroy();
+	files3->Destroy();
+}
+
+static void CopyFiles(TArray<FString> &to, TArray<FString> &from)
+{
+	unsigned int ndx = to.Reserve(from.Size());
+	for(unsigned i=0;i<from.Size(); i++)
+	{
+		to[ndx+i] = from[i];
+	}
+}
+
 //==========================================================================
 //
 // D_DoomMain
@@ -1596,6 +1640,7 @@ void D_DoomMain (void)
 	char *v;
 	const char *wad;
 	DArgs *execFiles;
+	TArray<FString> pwads;
 
 	// Set the FPU precision to 53 significant bits. This is the default
 	// for Visual C++, but not for GCC, so some slight math variances
@@ -1640,7 +1685,7 @@ void D_DoomMain (void)
 	// Load zdoom.pk3 alone so that we can get access to the internal gameinfos before 
 	// the IWAD is known.
 
-	const IWADInfo *iwad_info = D_FindIWAD(wad);
+	const IWADInfo *iwad_info = D_FindIWAD(allwads, wad);
 	gameinfo.gametype = iwad_info->gametype;
 	gameinfo.flags = iwad_info->flags;
 
@@ -1657,7 +1702,7 @@ void D_DoomMain (void)
 		// it for something else, so this gets to stay here.
 		wad = BaseFileSearch ("zvox.wad", NULL);
 		if (wad)
-			D_AddFile (wad);
+			D_AddFile (allwads, wad);
 	
 		// [RH] Add any .wad files in the skins directory
 #ifdef unix
@@ -1666,27 +1711,27 @@ void D_DoomMain (void)
 		file = progdir;
 #endif
 		file += "skins";
-		D_AddDirectory (file);
+		D_AddDirectory (allwads, file);
 
 #ifdef unix
 		file = NicePath("~/" GAME_DIR "/skins");
-		D_AddDirectory (file);
+		D_AddDirectory (allwads, file);
 #endif	
 
 		// Add common (global) wads
-		D_AddConfigWads ("Global.Autoload");
+		D_AddConfigWads (allwads, "Global.Autoload");
 
 		// Add game-specific wads
 		file = GameNames[gameinfo.gametype];
 		file += ".Autoload";
-		D_AddConfigWads (file);
+		D_AddConfigWads (allwads, file);
 
 		// Add IWAD-specific wads
 		if (iwad_info->Autoname != NULL)
 		{
 			file = iwad_info->Autoname;
 			file += ".Autoload";
-			D_AddConfigWads(file);
+			D_AddConfigWads(allwads, file);
 		}
 	}
 
@@ -1703,43 +1748,13 @@ void D_DoomMain (void)
 
 	C_ExecCmdLineParams ();		// [RH] do all +set commands on the command line
 
-	DArgs *files = Args->GatherFiles ("-file", ".wad", true);
-	DArgs *files1 = Args->GatherFiles (NULL, ".zip", false);
-	DArgs *files2 = Args->GatherFiles (NULL, ".pk3", false);
-	DArgs *files3 = Args->GatherFiles (NULL, ".txt", false);
-	if (files->NumArgs() > 0 || files1->NumArgs() > 0 || files2->NumArgs() > 0 || files3->NumArgs() > 0)
-	{
-		// Check for -file in shareware
-		if (gameinfo.flags & GI_SHAREWARE)
-		{
-			I_FatalError ("You cannot -file with the shareware version. Register!");
-		}
+	GetCmdLineFiles(pwads);
 
-		// the files gathered are wadfile/lump names
-		for (int i = 0; i < files->NumArgs(); i++)
-		{
-			D_AddWildFile (files->GetArg (i));
-		}
-		for (int i = 0; i < files1->NumArgs(); i++)
-		{
-			D_AddWildFile (files1->GetArg (i));
-		}
-		for (int i = 0; i < files2->NumArgs(); i++)
-		{
-			D_AddWildFile (files2->GetArg (i));
-		}
-		for (int i = 0; i < files3->NumArgs(); i++)
-		{
-			D_AddWildFile (files3->GetArg (i));
-		}
-	}
-	files->Destroy();
-	files1->Destroy();
-	files2->Destroy();
-	files3->Destroy();
+	CopyFiles(allwads, pwads);
 
 	Printf ("W_Init: Init WADfiles.\n");
-	Wads.InitMultipleFiles (&wadfiles);
+	Wads.InitMultipleFiles (allwads);
+	allwads.Clear();
 
 	// [RH] Initialize localizable strings.
 	GStrings.LoadStrings (false);
@@ -2043,7 +2058,7 @@ void D_DoomMain (void)
 
 	V_Init2();
 
-	files = Args->GatherFiles ("-playdemo", ".lmp", false);
+	DArgs *files = Args->GatherFiles ("-playdemo", ".lmp", false);
 	if (files->NumArgs() > 0)
 	{
 		singledemo = true;				// quit after one demo
diff --git a/src/d_main.h b/src/d_main.h
index 744e76bcc..3e22acb71 100644
--- a/src/d_main.h
+++ b/src/d_main.h
@@ -49,7 +49,7 @@ void D_PageTicker (void);
 void D_PageDrawer (void);
 void D_AdvanceDemo (void);
 void D_StartTitle (void);
-void D_AddFile (const char *file, bool check = true);
+void D_AddFile (TArray<FString> &wadfiles, const char *file, bool check = true);
 
 
 // [RH] Set this to something to draw an icon during the next screen refresh.
diff --git a/src/w_wad.cpp b/src/w_wad.cpp
index 9c5b525ca..bb8deb384 100644
--- a/src/w_wad.cpp
+++ b/src/w_wad.cpp
@@ -160,7 +160,7 @@ void FWadCollection::DeleteAll ()
 //
 //==========================================================================
 
-void FWadCollection::InitMultipleFiles (wadlist_t **filenames)
+void FWadCollection::InitMultipleFiles (TArray<FString> &filenames)
 {
 	int numfiles;
 
@@ -168,13 +168,10 @@ void FWadCollection::InitMultipleFiles (wadlist_t **filenames)
 	DeleteAll();
 	numfiles = 0;
 
-	while (*filenames)
+	for(unsigned i=0;i<filenames.Size(); i++)
 	{
-		wadlist_t *next = (*filenames)->next;
 		int baselump = NumLumps;
-		AddFile ((*filenames)->name);
-		M_Free (*filenames);
-		*filenames = next;
+		AddFile (filenames[i]);
 	}
 
 	NumLumps = LumpInfo.Size();
@@ -222,7 +219,7 @@ int FWadCollection::AddExternalFile(const char *filename)
 // [RH] Removed reload hack
 //==========================================================================
 
-void FWadCollection::AddFile (char *filename, FileReader *wadinfo)
+void FWadCollection::AddFile (const char *filename, FileReader *wadinfo)
 {
 	int startlump;
 	bool isdir = false;
@@ -252,7 +249,6 @@ void FWadCollection::AddFile (char *filename, FileReader *wadinfo)
 				return;
 			}
 		}
-		FixPathSeperator(filename);
 	}
 
 	Printf (" adding %s", filename);
diff --git a/src/w_wad.h b/src/w_wad.h
index 7adea8ab7..2edd71587 100644
--- a/src/w_wad.h
+++ b/src/w_wad.h
@@ -49,14 +49,6 @@ struct wadlump_t
 #define PWAD_ID		MAKE_ID('P','W','A','D')
 
 
-// [RH] Remove limit on number of WAD files
-struct wadlist_t
-{
-	wadlist_t *next;
-	char name[1];	// +size of string
-};
-extern wadlist_t *wadfiles;
-
 // [RH] Namespaces from BOOM.
 typedef enum {
 	ns_global = 0,
@@ -153,8 +145,8 @@ public:
 	// The wadnum for the IWAD
 	enum { IWAD_FILENUM = 1 };
 
-	void InitMultipleFiles (wadlist_t **filenames);
-	void AddFile (char *filename, FileReader *wadinfo = NULL);
+	void InitMultipleFiles (TArray<FString> &filenames);
+	void AddFile (const char *filename, FileReader *wadinfo = NULL);
 	int CheckIfWadLoaded (const char *name);
 
 	const char *GetWadName (int wadnum) const;