- moved the file lookup functions to utilities.

# Conflicts:
#	src/common/utility/findfile.cpp
This commit is contained in:
Christoph Oelckers 2020-04-11 18:08:20 +02:00
parent 5dd89541fe
commit 0e7480b4ed
7 changed files with 288 additions and 273 deletions

View file

@ -34,10 +34,13 @@
#include "findfile.h"
#include "zstring.h"
#include "cmdlib.h"
#include "printf.h"
#include "configfile.h"
#ifndef _WIN32
#include <unistd.h>
#include <fnmatch.h>
#include "cmdlib.h"
@ -126,7 +129,8 @@ int I_FindAttr(findstate_t *const fileinfo)
#else
#include <windows.h>
#include <direct.h>
//==========================================================================
//
// I_FindFirst
@ -186,3 +190,242 @@ const char *I_FindName(findstate_t *fileinfo)
}
#endif
//==========================================================================
//
// D_AddFile
//
//==========================================================================
bool D_AddFile(TArray<FString>& wadfiles, const char* file, bool check, int position, FConfigFile* config)
{
if (file == nullptr || *file == '\0')
{
return false;
}
if (check && !DirEntryExists(file))
{
const char* f = BaseFileSearch(file, ".wad", false, config);
if (f == nullptr)
{
Printf("Can't find '%s'\n", file);
return false;
}
file = f;
}
FString f = file;
FixPathSeperator(f);
if (position == -1) wadfiles.Push(f);
else wadfiles.Insert(position, f);
return true;
}
//==========================================================================
//
// D_AddWildFile
//
//==========================================================================
void D_AddWildFile(TArray<FString>& wadfiles, const char* value, const char *extension, FConfigFile* config)
{
if (value == nullptr || *value == '\0')
{
return;
}
const char* wadfile = BaseFileSearch(value, extension, false, config);
if (wadfile != nullptr)
{
D_AddFile(wadfiles, wadfile, true, -1, config);
}
else
{ // Try pattern matching
findstate_t findstate;
char path[ZPATH_MAX];
char* sep;
void* handle = I_FindFirst(value, &findstate);
strcpy(path, value);
sep = strrchr(path, '/');
if (sep == nullptr)
{
sep = strrchr(path, '\\');
#ifdef _WIN32
if (sep == nullptr && path[1] == ':')
{
sep = path + 1;
}
#endif
}
if (handle != ((void*)-1))
{
do
{
if (!(I_FindAttr(&findstate) & FA_DIREC))
{
if (sep == nullptr)
{
D_AddFile(wadfiles, I_FindName(&findstate), true, -1, config);
}
else
{
strcpy(sep + 1, I_FindName(&findstate));
D_AddFile(wadfiles, path, true, -1, config);
}
}
} while (I_FindNext(handle, &findstate) == 0);
}
I_FindClose(handle);
}
}
//==========================================================================
//
// D_AddConfigWads
//
// Adds all files in the specified config file section.
//
//==========================================================================
void D_AddConfigFiles(TArray<FString>& wadfiles, const char* section, const char* extension, FConfigFile *config)
{
if (config && config->SetSection(section))
{
const char* key;
const char* value;
FConfigFile::Position pos;
while (config->NextInSection(key, value))
{
if (stricmp(key, "Path") == 0)
{
// D_AddWildFile resets config's position, so remember it
config->GetPosition(pos);
D_AddWildFile(wadfiles, ExpandEnvVars(value), extension, config);
// Reset config's position to get next wad
config->SetPosition(pos);
}
}
}
}
//==========================================================================
//
// D_AddDirectory
//
// Add all .wad files in a directory. Does not descend into subdirectories.
//
//==========================================================================
void D_AddDirectory(TArray<FString>& wadfiles, const char* dir, const char *filespec, FConfigFile* config)
{
char curdir[ZPATH_MAX];
if (getcwd(curdir, ZPATH_MAX))
{
char skindir[ZPATH_MAX];
findstate_t findstate;
void* handle;
size_t stuffstart;
stuffstart = strlen(dir);
memcpy(skindir, dir, stuffstart * sizeof(*dir));
skindir[stuffstart] = 0;
if (skindir[stuffstart - 1] == '/')
{
skindir[--stuffstart] = 0;
}
if (!chdir(skindir))
{
skindir[stuffstart++] = '/';
if ((handle = I_FindFirst(filespec, &findstate)) != (void*)-1)
{
do
{
if (!(I_FindAttr(&findstate) & FA_DIREC))
{
strcpy(skindir + stuffstart, I_FindName(&findstate));
D_AddFile(wadfiles, skindir, true, -1, config);
}
} while (I_FindNext(handle, &findstate) == 0);
I_FindClose(handle);
}
}
chdir(curdir);
}
}
//==========================================================================
//
// BaseFileSearch
//
// If a file does not exist at <file>, looks for it in the directories
// specified in the config file. Returns the path to the file, if found,
// or nullptr if it could not be found.
//
//==========================================================================
const char* BaseFileSearch(const char* file, const char* ext, bool lookfirstinprogdir, FConfigFile* config)
{
static char wad[ZPATH_MAX];
if (file == nullptr || *file == '\0')
{
return nullptr;
}
if (lookfirstinprogdir)
{
mysnprintf(wad, countof(wad), "%s%s%s", progdir.GetChars(), progdir.Back() == '/' ? "" : "/", file);
if (DirEntryExists(wad))
{
return wad;
}
}
if (DirEntryExists(file))
{
mysnprintf(wad, countof(wad), "%s", file);
return wad;
}
if (config != nullptr && config->SetSection("FileSearch.Directories"))
{
const char* key;
const char* value;
while (config->NextInSection(key, value))
{
if (stricmp(key, "Path") == 0)
{
FString dir;
dir = NicePath(value);
if (dir.IsNotEmpty())
{
mysnprintf(wad, countof(wad), "%s%s%s", dir.GetChars(), dir.Back() == '/' ? "" : "/", file);
if (DirEntryExists(wad))
{
return wad;
}
}
}
}
}
// Retry, this time with a default extension
if (ext != nullptr)
{
FString tmp = file;
DefaultExtension(tmp, ext);
return BaseFileSearch(tmp, nullptr, lookfirstinprogdir, config);
}
return nullptr;
}

View file

@ -4,6 +4,11 @@
#include <stdint.h>
#include "zstring.h"
enum
{
ZPATH_MAX = 260
};
#ifndef _WIN32
#include <dirent.h>
@ -54,7 +59,7 @@ private:
FileTime Times[3];
uint32_t Size[2];
uint32_t Reserved[2];
wchar_t Name[260];
wchar_t Name[ZPATH_MAX];
wchar_t AltName[14];
};
WinData FindData;
@ -84,3 +89,11 @@ inline int I_FindAttr(findstate_t *fileinfo)
void *I_FindFirst (const char *filespec, findstate_t *fileinfo);
int I_FindNext (void *handle, findstate_t *fileinfo);
int I_FindClose (void *handle);
class FConfigFile;
bool D_AddFile(TArray<FString>& wadfiles, const char* file, bool check, int position, FConfigFile* config);
void D_AddWildFile(TArray<FString>& wadfiles, const char* value, const char *extension, FConfigFile* config);
void D_AddConfigFiles(TArray<FString>& wadfiles, const char* section, const char* extension, FConfigFile* config);
void D_AddDirectory(TArray<FString>& wadfiles, const char* dir, const char *filespec, FConfigFile* config);
const char* BaseFileSearch(const char* file, const char* ext, bool lookfirstinprogdir, FConfigFile* config);

View file

@ -49,6 +49,7 @@
#include "printf.h"
#include "c_cvars.h"
#include "c_buttons.h"
#include "findfile.h"
// Todo: Get rid of
#include "d_net.h"
@ -1019,11 +1020,11 @@ void FExecList::ExecCommands() const
}
}
void FExecList::AddPullins(TArray<FString> &wads) const
void FExecList::AddPullins(TArray<FString> &wads, FConfigFile *config) const
{
for (unsigned i = 0; i < Pullins.Size(); ++i)
{
D_AddFile(wads, Pullins[i]);
D_AddFile(wads, Pullins[i], true, -1, config);
}
}

View file

@ -52,7 +52,7 @@ struct FExecList
void AddCommand(const char *cmd, const char *file = nullptr);
void ExecCommands() const;
void AddPullins(TArray<FString> &wads) const;
void AddPullins(TArray<FString> &wads, FConfigFile *config) const;
};
extern bool ParsingKeyConf, UnsafeExecutionContext;

View file

@ -51,8 +51,6 @@
CVAR (Bool, queryiwad, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
CVAR (String, defaultiwad, "", CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
const char* BaseFileSearch(const char* file, const char* ext, bool lookfirstinprogdir);
//==========================================================================
//
// Parses IWAD definitions
@ -725,11 +723,11 @@ int FIWadManager::IdentifyVersion (TArray<FString> &wadfiles, const char *iwad,
// zdoom.pk3 must always be the first file loaded and the IWAD second.
wadfiles.Clear();
D_AddFile (wadfiles, zdoom_wad);
D_AddFile (wadfiles, zdoom_wad, true, -1, GameConfig);
// [SP] Load non-free assets if available. This must be done before the IWAD.
int iwadnum;
if (D_AddFile(wadfiles, optional_wad))
if (D_AddFile(wadfiles, optional_wad, true, -1, GameConfig))
iwadnum = 2;
else
iwadnum = 1;
@ -737,10 +735,10 @@ int FIWadManager::IdentifyVersion (TArray<FString> &wadfiles, const char *iwad,
fileSystem.SetIwadNum(iwadnum);
if (picks[pick].mRequiredPath.IsNotEmpty())
{
D_AddFile (wadfiles, picks[pick].mRequiredPath);
D_AddFile (wadfiles, picks[pick].mRequiredPath, true, -1, GameConfig);
iwadnum++;
}
D_AddFile (wadfiles, picks[pick].mFullPath);
D_AddFile (wadfiles, picks[pick].mFullPath, true, -1, GameConfig);
fileSystem.SetMaxIwadNum(iwadnum);
auto info = mIWadInfos[picks[pick].mInfoIndex];
@ -761,12 +759,12 @@ int FIWadManager::IdentifyVersion (TArray<FString> &wadfiles, const char *iwad,
path = FString(picks[pick].mFullPath.GetChars(), lastslash + 1);
}
path += info.Load[i];
D_AddFile(wadfiles, path);
D_AddFile(wadfiles, path, true, -1, GameConfig);
}
else
{
auto wad = BaseFileSearch(info.Load[i].GetChars() + 1, NULL, true);
if (wad) D_AddFile(wadfiles, wad);
auto wad = BaseFileSearch(info.Load[i].GetChars() + 1, NULL, true, GameConfig);
if (wad) D_AddFile(wadfiles, wad, true, -1, GameConfig);
}
}

View file

@ -140,7 +140,6 @@ bool D_CheckNetGame ();
void D_ProcessEvents ();
void G_BuildTiccmd (ticcmd_t* cmd);
void D_DoAdvanceDemo ();
void D_AddWildFile (TArray<FString> &wadfiles, const char *pattern);
void D_LoadWadSettings ();
void ParseGLDefs();
void DrawFullscreenSubtitle(const char *text);
@ -151,7 +150,6 @@ void I_UpdateWindowTitle();
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
void D_DoomLoop ();
const char *BaseFileSearch (const char *file, const char *ext, bool lookfirstinprogdir=false);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
@ -1664,243 +1662,6 @@ void ParseCVarInfo()
}
}
//==========================================================================
//
// D_AddFile
//
//==========================================================================
bool D_AddFile (TArray<FString> &wadfiles, const char *file, bool check, int position)
{
if (file == NULL || *file == '\0')
{
return false;
}
if (check && !DirEntryExists (file))
{
const char *f = BaseFileSearch (file, ".wad");
if (f == NULL)
{
Printf ("Can't find '%s'\n", file);
return false;
}
file = f;
}
FString f = file;
FixPathSeperator(f);
if (position == -1) wadfiles.Push(f);
else wadfiles.Insert(position, f);
return true;
}
//==========================================================================
//
// D_AddWildFile
//
//==========================================================================
void D_AddWildFile (TArray<FString> &wadfiles, const char *value)
{
if (value == NULL || *value == '\0')
{
return;
}
const char *wadfile = BaseFileSearch (value, ".wad");
if (wadfile != NULL)
{
D_AddFile (wadfiles, wadfile);
}
else
{ // Try pattern matching
findstate_t findstate;
char path[PATH_MAX];
char *sep;
void *handle = I_FindFirst (value, &findstate);
strcpy (path, value);
sep = strrchr (path, '/');
if (sep == NULL)
{
sep = strrchr (path, '\\');
#ifdef _WIN32
if (sep == NULL && path[1] == ':')
{
sep = path + 1;
}
#endif
}
if (handle != ((void *)-1))
{
do
{
if (!(I_FindAttr(&findstate) & FA_DIREC))
{
if (sep == NULL)
{
D_AddFile (wadfiles, I_FindName (&findstate));
}
else
{
strcpy (sep+1, I_FindName (&findstate));
D_AddFile (wadfiles, path);
}
}
} while (I_FindNext (handle, &findstate) == 0);
}
I_FindClose (handle);
}
}
//==========================================================================
//
// D_AddConfigWads
//
// Adds all files in the specified config file section.
//
//==========================================================================
void D_AddConfigWads (TArray<FString> &wadfiles, const char *section)
{
if (GameConfig->SetSection (section))
{
const char *key;
const char *value;
FConfigFile::Position pos;
while (GameConfig->NextInSection (key, value))
{
if (stricmp (key, "Path") == 0)
{
// D_AddWildFile resets GameConfig's position, so remember it
GameConfig->GetPosition (pos);
D_AddWildFile (wadfiles, ExpandEnvVars(value));
// Reset GameConfig's position to get next wad
GameConfig->SetPosition (pos);
}
}
}
}
//==========================================================================
//
// D_AddDirectory
//
// Add all .wad files in a directory. Does not descend into subdirectories.
//
//==========================================================================
static void D_AddDirectory (TArray<FString> &wadfiles, const char *dir)
{
char curdir[PATH_MAX];
if (getcwd (curdir, PATH_MAX))
{
char skindir[PATH_MAX];
findstate_t findstate;
void *handle;
size_t stuffstart;
stuffstart = strlen (dir);
memcpy (skindir, dir, stuffstart*sizeof(*dir));
skindir[stuffstart] = 0;
if (skindir[stuffstart-1] == '/')
{
skindir[--stuffstart] = 0;
}
if (!chdir (skindir))
{
skindir[stuffstart++] = '/';
if ((handle = I_FindFirst ("*.wad", &findstate)) != (void *)-1)
{
do
{
if (!(I_FindAttr (&findstate) & FA_DIREC))
{
strcpy (skindir + stuffstart, I_FindName (&findstate));
D_AddFile (wadfiles, skindir);
}
} while (I_FindNext (handle, &findstate) == 0);
I_FindClose (handle);
}
}
chdir (curdir);
}
}
//==========================================================================
//
// BaseFileSearch
//
// If a file does not exist at <file>, looks for it in the directories
// specified in the config file. Returns the path to the file, if found,
// or NULL if it could not be found.
//
//==========================================================================
const char *BaseFileSearch (const char *file, const char *ext, bool lookfirstinprogdir)
{
static char wad[PATH_MAX];
if (file == NULL || *file == '\0')
{
return NULL;
}
if (lookfirstinprogdir)
{
mysnprintf (wad, countof(wad), "%s%s%s", progdir.GetChars(), progdir.Back() == '/' ? "" : "/", file);
if (DirEntryExists (wad))
{
return wad;
}
}
if (DirEntryExists (file))
{
mysnprintf (wad, countof(wad), "%s", file);
return wad;
}
if (GameConfig != NULL && GameConfig->SetSection ("FileSearch.Directories"))
{
const char *key;
const char *value;
while (GameConfig->NextInSection (key, value))
{
if (stricmp (key, "Path") == 0)
{
FString dir;
dir = NicePath(value);
if (dir.IsNotEmpty())
{
mysnprintf (wad, countof(wad), "%s%s%s", dir.GetChars(), dir.Back() == '/' ? "" : "/", file);
if (DirEntryExists (wad))
{
return wad;
}
}
}
}
}
// Retry, this time with a default extension
if (ext != NULL)
{
FString tmp = file;
DefaultExtension (tmp, ext);
return BaseFileSearch (tmp, NULL);
}
return NULL;
}
//==========================================================================
//
// ConsiderPatches
@ -1918,8 +1679,8 @@ bool ConsiderPatches (const char *arg)
argc = Args->CheckParmList(arg, &args);
for (i = 0; i < argc; ++i)
{
if ( (f = BaseFileSearch(args[i], ".deh")) ||
(f = BaseFileSearch(args[i], ".bex")) )
if ( (f = BaseFileSearch(args[i], ".deh", false, GameConfig)) ||
(f = BaseFileSearch(args[i], ".bex", false, GameConfig)) )
{
D_LoadDehFile(f);
}
@ -1950,7 +1711,7 @@ static void GetCmdLineFiles(TArray<FString> &wadfiles)
argc = Args->CheckParmList("-file", &args);
for (i = 0; i < argc; ++i)
{
D_AddWildFile(wadfiles, args[i]);
D_AddWildFile(wadfiles, args[i], ".wad", GameConfig);
}
}
@ -2003,11 +1764,11 @@ static FString ParseGameInfo(TArray<FString> &pwads, const char *fn, const char
}
if (!FileExists(checkpath))
{
pos += D_AddFile(pwads, sc.String, true, pos);
pos += D_AddFile(pwads, sc.String, true, pos, GameConfig);
}
else
{
pos += D_AddFile(pwads, checkpath, true, pos);
pos += D_AddFile(pwads, checkpath, true, pos, GameConfig);
}
}
while (sc.CheckToken(','));
@ -2179,15 +1940,15 @@ static void AddAutoloadFiles(const char *autoname)
{
if (DoomStartupInfo.LoadLights == 1 || (DoomStartupInfo.LoadLights != 0 && autoloadlights))
{
const char *lightswad = BaseFileSearch ("lights.pk3", NULL);
const char *lightswad = BaseFileSearch ("lights.pk3", NULL, false, GameConfig);
if (lightswad)
D_AddFile (allwads, lightswad);
D_AddFile (allwads, lightswad, true, -1, GameConfig);
}
if (DoomStartupInfo.LoadBrightmaps == 1 || (DoomStartupInfo.LoadBrightmaps != 0 && autoloadbrightmaps))
{
const char *bmwad = BaseFileSearch ("brightmaps.pk3", NULL);
const char *bmwad = BaseFileSearch ("brightmaps.pk3", NULL, false, GameConfig);
if (bmwad)
D_AddFile (allwads, bmwad);
D_AddFile (allwads, bmwad, true, -1, GameConfig);
}
}
@ -2200,9 +1961,9 @@ static void AddAutoloadFiles(const char *autoname)
// voices. I never got around to writing the utility to do it, though.
// And I probably never will now. But I know at least one person uses
// it for something else, so this gets to stay here.
const char *wad = BaseFileSearch ("zvox.wad", NULL);
const char *wad = BaseFileSearch ("zvox.wad", NULL, false, GameConfig);
if (wad)
D_AddFile (allwads, wad);
D_AddFile (allwads, wad, true, -1, GameConfig);
// [RH] Add any .wad files in the skins directory
#ifdef __unix__
@ -2211,15 +1972,15 @@ static void AddAutoloadFiles(const char *autoname)
file = progdir;
#endif
file += "skins";
D_AddDirectory (allwads, file);
D_AddDirectory (allwads, file, "*.wad", GameConfig);
#ifdef __unix__
file = NicePath("$HOME/" GAME_DIR "/skins");
D_AddDirectory (allwads, file);
D_AddDirectory (allwads, file, "*.wad", GameConfig);
#endif
// Add common (global) wads
D_AddConfigWads (allwads, "Global.Autoload");
D_AddConfigFiles(allwads, "Global.Autoload", "*.wad", GameConfig);
long len;
int lastpos = -1;
@ -2227,7 +1988,7 @@ static void AddAutoloadFiles(const char *autoname)
while ((len = LumpFilterIWAD.IndexOf('.', lastpos+1)) > 0)
{
file = LumpFilterIWAD.Left(len) + ".Autoload";
D_AddConfigWads(allwads, file);
D_AddConfigFiles(allwads, file, "*.wad", GameConfig);
lastpos = len;
}
}
@ -2811,14 +2572,14 @@ static int D_DoomMain_Internal (void)
// [RH] Make sure zdoom.pk3 is always loaded,
// as it contains magic stuff we need.
wad = BaseFileSearch (BASEWAD, NULL, true);
wad = BaseFileSearch (BASEWAD, NULL, true, GameConfig);
if (wad == NULL)
{
I_FatalError ("Cannot find " BASEWAD);
}
FString basewad = wad;
FString optionalwad = BaseFileSearch(OPTIONALWAD, NULL, true);
FString optionalwad = BaseFileSearch(OPTIONALWAD, NULL, true, GameConfig);
iwad_man = new FIWadManager(basewad, optionalwad);
@ -2898,7 +2659,7 @@ static int D_DoomMain_Internal (void)
CopyFiles(allwads, pwads);
if (exec != NULL)
{
exec->AddPullins(allwads);
exec->AddPullins(allwads, GameConfig);
}
// Since this function will never leave we must delete this array here manually.

View file

@ -58,7 +58,6 @@ void D_PageTicker (void);
void D_PageDrawer (void);
void D_AdvanceDemo (void);
void D_StartTitle (void);
bool D_AddFile (TArray<FString> &wadfiles, const char *file, bool check = true, int position = -1);
// [RH] Set this to something to draw an icon during the next screen refresh.