2020-04-11 21:50:43 +00:00
|
|
|
/*
|
|
|
|
** findfile.cpp
|
|
|
|
** Warpper around the native directory scanning API
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 1998-2016 Randy Heit
|
|
|
|
** Copyright 2005-2020 Christoph Oelckers
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "findfile.h"
|
|
|
|
#include "zstring.h"
|
2020-04-11 22:07:52 +00:00
|
|
|
#include "cmdlib.h"
|
|
|
|
#include "printf.h"
|
|
|
|
#include "configfile.h"
|
2022-08-26 16:28:22 +00:00
|
|
|
#include "i_system.h"
|
2023-08-19 14:57:37 +00:00
|
|
|
#include "fs_findfile.h"
|
2020-04-11 21:50:43 +00:00
|
|
|
|
2023-08-23 18:36:19 +00:00
|
|
|
#ifdef __unix__
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif // __unix__
|
2020-04-11 22:07:52 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// D_AddFile
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2023-08-20 00:15:57 +00:00
|
|
|
bool D_AddFile(std::vector<std::string>& wadfiles, const char* file, bool check, int position, FConfigFile* config)
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
|
|
|
if (file == nullptr || *file == '\0')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-12 18:01:40 +00:00
|
|
|
#ifdef __unix__
|
2020-10-14 19:50:41 +00:00
|
|
|
// Confirm file exists in filesystem.
|
|
|
|
struct stat info;
|
|
|
|
bool found = stat(file, &info) == 0;
|
2021-02-26 12:28:02 +00:00
|
|
|
FString fullpath = file;
|
2020-10-14 19:50:41 +00:00
|
|
|
if (!found)
|
2020-10-12 18:01:40 +00:00
|
|
|
{
|
2020-10-14 19:50:41 +00:00
|
|
|
// File not found, so split file into path and filename so we can enumerate the path for the file.
|
|
|
|
auto lastindex = fullpath.LastIndexOf("/");
|
|
|
|
FString basepath = fullpath.Left(lastindex);
|
|
|
|
FString filename = fullpath.Right(fullpath.Len() - lastindex - 1);
|
|
|
|
|
|
|
|
// Proceed only if locating a file (i.e. `file` isn't a path to just a directory.)
|
|
|
|
if (filename.IsNotEmpty())
|
2020-10-12 18:01:40 +00:00
|
|
|
{
|
2020-10-14 19:50:41 +00:00
|
|
|
DIR *d;
|
|
|
|
struct dirent *dir;
|
|
|
|
d = opendir(basepath.GetChars());
|
|
|
|
if (d)
|
2020-10-14 09:18:53 +00:00
|
|
|
{
|
2020-10-14 19:50:41 +00:00
|
|
|
while ((dir = readdir(d)) != NULL)
|
|
|
|
{
|
|
|
|
if (filename.CompareNoCase(dir->d_name) == 0)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
filename = dir->d_name;
|
|
|
|
fullpath = basepath << "/" << filename;
|
|
|
|
file = fullpath.GetChars();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
if (!found)
|
2020-10-14 09:18:53 +00:00
|
|
|
{
|
2020-10-14 19:50:41 +00:00
|
|
|
Printf("Can't find file '%s' in '%s'\n", filename.GetChars(), basepath.GetChars());
|
|
|
|
return false;
|
2020-10-14 09:18:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-14 19:50:41 +00:00
|
|
|
else
|
2020-10-14 09:18:53 +00:00
|
|
|
{
|
2020-10-14 19:50:41 +00:00
|
|
|
Printf("Can't open directory '%s'\n", basepath.GetChars());
|
2020-10-14 09:18:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 18:01:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-04-11 22:07:52 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-20 00:15:57 +00:00
|
|
|
std::string f = file;
|
|
|
|
for (auto& c : f) if (c == '\\') c = '/';
|
|
|
|
if (position == -1) wadfiles.push_back(f);
|
|
|
|
else wadfiles.insert(wadfiles.begin() + position, f);
|
2020-04-11 22:07:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// D_AddWildFile
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2023-08-20 00:15:57 +00:00
|
|
|
void D_AddWildFile(std::vector<std::string>& wadfiles, const char* value, const char *extension, FConfigFile* config)
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
|
|
|
if (value == nullptr || *value == '\0')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char* wadfile = BaseFileSearch(value, extension, false, config);
|
|
|
|
|
|
|
|
if (wadfile != nullptr)
|
|
|
|
{
|
|
|
|
D_AddFile(wadfiles, wadfile, true, -1, config);
|
|
|
|
}
|
2023-08-19 14:57:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Try pattern matching
|
2023-08-23 18:36:19 +00:00
|
|
|
FileSys::FileList list;
|
|
|
|
auto path = ExtractFilePath(value);
|
|
|
|
auto name = ExtractFileBase(value, true);
|
|
|
|
if (path.IsEmpty()) path = ".";
|
2023-10-07 16:44:31 +00:00
|
|
|
if (FileSys::ScanDirectory(list, path.GetChars(), name.GetChars(), true))
|
2023-08-19 14:57:37 +00:00
|
|
|
{
|
|
|
|
for(auto& entry : list)
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
2023-08-19 14:57:37 +00:00
|
|
|
D_AddFile(wadfiles, entry.FilePath.c_str(), true, -1, config);
|
2020-04-11 22:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// D_AddConfigWads
|
|
|
|
//
|
|
|
|
// Adds all files in the specified config file section.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2023-08-20 00:15:57 +00:00
|
|
|
void D_AddConfigFiles(std::vector<std::string>& wadfiles, const char* section, const char* extension, FConfigFile *config)
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
|
|
|
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);
|
2023-10-07 16:44:31 +00:00
|
|
|
D_AddWildFile(wadfiles, ExpandEnvVars(value).GetChars(), extension, config);
|
2020-04-11 22:07:52 +00:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2023-08-20 00:15:57 +00:00
|
|
|
void D_AddDirectory(std::vector<std::string>& wadfiles, const char* dir, const char *filespec, FConfigFile* config)
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
2023-08-23 18:36:19 +00:00
|
|
|
FileSys::FileList list;
|
|
|
|
if (FileSys::ScanDirectory(list, dir, "*.wad", true))
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
2023-08-19 14:57:37 +00:00
|
|
|
for (auto& entry : list)
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
2023-08-19 14:57:37 +00:00
|
|
|
if (!entry.isDirectory)
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
2023-08-19 14:57:37 +00:00
|
|
|
D_AddFile(wadfiles, entry.FilePath.c_str(), true, -1, config);
|
2020-04-11 22:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2022-08-26 16:28:22 +00:00
|
|
|
static FString BFSwad; // outside the function to evade C++'s insane rules for constructing static variables inside functions.
|
|
|
|
|
2020-04-11 22:07:52 +00:00
|
|
|
const char* BaseFileSearch(const char* file, const char* ext, bool lookfirstinprogdir, FConfigFile* config)
|
|
|
|
{
|
|
|
|
if (file == nullptr || *file == '\0')
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (lookfirstinprogdir)
|
|
|
|
{
|
2022-08-26 16:28:22 +00:00
|
|
|
BFSwad.Format("%s%s%s", progdir.GetChars(), progdir.Back() == '/' ? "" : "/", file);
|
2023-10-07 16:44:31 +00:00
|
|
|
if (DirEntryExists(BFSwad.GetChars()))
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
2022-08-26 16:28:22 +00:00
|
|
|
return BFSwad.GetChars();
|
2020-04-11 22:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DirEntryExists(file))
|
|
|
|
{
|
2022-08-26 16:28:22 +00:00
|
|
|
BFSwad.Format("%s", file);
|
|
|
|
return BFSwad.GetChars();
|
2020-04-11 22:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2022-08-26 16:28:22 +00:00
|
|
|
BFSwad.Format("%s%s%s", dir.GetChars(), dir.Back() == '/' ? "" : "/", file);
|
2023-10-07 16:44:31 +00:00
|
|
|
if (DirEntryExists(BFSwad.GetChars()))
|
2020-04-11 22:07:52 +00:00
|
|
|
{
|
2022-08-26 16:28:22 +00:00
|
|
|
return BFSwad.GetChars();
|
2020-04-11 22:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry, this time with a default extension
|
|
|
|
if (ext != nullptr)
|
|
|
|
{
|
|
|
|
FString tmp = file;
|
|
|
|
DefaultExtension(tmp, ext);
|
2023-10-07 16:44:31 +00:00
|
|
|
return BaseFileSearch(tmp.GetChars(), nullptr, lookfirstinprogdir, config);
|
2020-04-11 22:07:52 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|