Rewrite FileSearch and improve ScanDirectoryTree.

- FileSearch is now just one function, and it converts the original glob
  into a regex on all platforms rather than relying on native Windows
  pattern matching on there and a complete hack elsewhere.  It now
  supports recursion out of the box rather than manually expanding
  into a full list of directories in multiple call sites.

  - This adds a GCC >= 4.9 dependency due to older versions having
  outright broken <regex>.  MSVC is fine with it.

- ScanDirectoryTree returns the parent entry rather than filling parts
  of it in via reference.  The count is now stored in the entry like it
  was for subdirectories.

- .glsl file search is now done with DoFileSearch.

- IOCTLV_READ_DIR now uses ScanDirectoryTree directly and sorts the
  results after replacements for better determinism.
This commit is contained in:
comex 2014-11-15 15:46:40 -05:00
parent 6ff3fcee59
commit a225426510
17 changed files with 156 additions and 348 deletions

View File

@ -2,101 +2,60 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#if !__clang__ && __GNUC__ == 4 && __GNUC_MINOR__ < 9
#error <regex> is broken in GCC < 4.9; please upgrade
#endif
#include <algorithm> #include <algorithm>
#include <cstring> #include <functional>
#include <regex>
#include "Common/CommonPaths.h" #include "Common/CommonPaths.h"
#include "Common/FileSearch.h" #include "Common/FileSearch.h"
#include "Common/StringUtil.h" #include "Common/FileUtil.h"
#ifndef _WIN32 static std::vector<std::string> FileSearchWithTest(const std::vector<std::string>& directories, bool recursive, std::function<bool(const File::FSTEntry &)> callback)
#include <dirent.h>
#else
#include <windows.h>
#endif
CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, const CFileSearch::XStringVector& _rDirectories)
{ {
// Reverse the loop order for speed? std::vector<std::string> result;
for (auto& _rSearchString : _rSearchStrings) for (const std::string& directory : directories)
{ {
for (auto& _rDirectory : _rDirectories) File::FSTEntry top = File::ScanDirectoryTree(directory, recursive);
{
FindFiles(_rSearchString, _rDirectory); std::function<void(File::FSTEntry&)> DoEntry;
} DoEntry = [&](File::FSTEntry& entry) {
if (callback(entry))
result.push_back(entry.physicalName);
for (auto& child : entry.children)
DoEntry(child);
};
DoEntry(top);
} }
// remove duplicates
std::sort(result.begin(), result.end());
result.erase(std::unique(result.begin(), result.end()), result.end());
return result;
} }
std::vector<std::string> DoFileSearch(const std::vector<std::string>& globs, const std::vector<std::string>& directories, bool recursive)
void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath)
{ {
std::string GCMSearchPath; std::string regex_str = "^(";
BuildCompleteFilename(GCMSearchPath, _strPath, _searchString); for (const auto& str : globs)
#ifdef _WIN32
WIN32_FIND_DATA findData;
HANDLE FindFirst = FindFirstFile(UTF8ToTStr(GCMSearchPath).c_str(), &findData);
if (FindFirst != INVALID_HANDLE_VALUE)
{ {
bool bkeepLooping = true; if (regex_str.size() != 2)
regex_str += "|";
while (bkeepLooping) // convert glob to regex
{ regex_str += std::regex_replace(std::regex_replace(str, std::regex("\\."), "\\."), std::regex("\\*"), ".*");
if (findData.cFileName[0] != '.')
{
std::string strFilename;
BuildCompleteFilename(strFilename, _strPath, TStrToUTF8(findData.cFileName));
m_FileNames.push_back(strFilename);
}
bkeepLooping = FindNextFile(FindFirst, &findData) ? true : false;
}
} }
FindClose(FindFirst); regex_str += ")$";
std::regex regex(regex_str);
return FileSearchWithTest(directories, recursive, [&](const File::FSTEntry& entry) {
#else return std::regex_match(entry.virtualName, regex);
// TODO: super lame/broken });
auto end_match(_searchString);
// assuming we have a "*.blah"-like pattern
if (!end_match.empty() && end_match[0] == '*')
end_match.erase(0, 1);
// ugly
if (end_match == ".*")
end_match.clear();
DIR* dir = opendir(_strPath.c_str());
if (!dir)
return;
while (auto const dp = readdir(dir))
{
std::string found(dp->d_name);
if ((found != ".") && (found != "..") &&
(found.size() >= end_match.size()) &&
std::equal(end_match.rbegin(), end_match.rend(), found.rbegin()))
{
std::string full_name;
if (_strPath.c_str()[_strPath.size() - 1] == DIR_SEP_CHR)
full_name = _strPath + found;
else
full_name = _strPath + DIR_SEP + found;
m_FileNames.push_back(full_name);
}
}
closedir(dir);
#endif
} }
const CFileSearch::XStringVector& CFileSearch::GetFileNames() const std::vector<std::string> FindSubdirectories(const std::vector<std::string>& directories, bool recursive)
{ {
return m_FileNames; return FileSearchWithTest(directories, true, [&](const File::FSTEntry& entry) {
return entry.isDirectory;
});
} }

View File

@ -7,18 +7,5 @@
#include <string> #include <string>
#include <vector> #include <vector>
class CFileSearch std::vector<std::string> DoFileSearch(const std::vector<std::string>& globs, const std::vector<std::string>& directories, bool recursive = false);
{ std::vector<std::string> FindSubdirectories(const std::vector<std::string>& directories, bool recursive);
public:
typedef std::vector<std::string>XStringVector;
CFileSearch(const XStringVector& _rSearchStrings, const XStringVector& _rDirectories);
const XStringVector& GetFileNames() const;
private:
void FindFiles(const std::string& _searchString, const std::string& _strPath);
XStringVector m_FileNames;
};

View File

@ -453,11 +453,14 @@ bool CreateEmptyFile(const std::string &filename)
// Scans the directory tree gets, starting from _Directory and adds the // Scans the directory tree gets, starting from _Directory and adds the
// results into parentEntry. Returns the number of files+directories found // results into parentEntry. Returns the number of files+directories found
u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) FSTEntry ScanDirectoryTree(const std::string &directory, bool recursive)
{ {
INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory.c_str()); INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory.c_str());
// How many files + directories we found // How many files + directories we found
u32 foundEntries = 0; FSTEntry parent_entry;
parent_entry.physicalName = directory;
parent_entry.isDirectory = true;
parent_entry.size = 0;
#ifdef _WIN32 #ifdef _WIN32
// Find the first file in the directory. // Find the first file in the directory.
WIN32_FIND_DATA ffd; WIN32_FIND_DATA ffd;
@ -466,59 +469,55 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry)
if (hFind == INVALID_HANDLE_VALUE) if (hFind == INVALID_HANDLE_VALUE)
{ {
FindClose(hFind); FindClose(hFind);
return foundEntries; return parent_entry;
} }
// Windows loop // Windows loop
do do
{ {
FSTEntry entry; const std::string virtual_name(TStrToUTF8(ffd.cFileName));
const std::string virtualName(TStrToUTF8(ffd.cFileName));
#else #else
struct dirent dirent, *result = nullptr; struct dirent dirent, *result = nullptr;
DIR *dirp = opendir(directory.c_str()); DIR *dirp = opendir(directory.c_str());
if (!dirp) if (!dirp)
return 0; return parent_entry;
// non Windows loop // non Windows loop
while (!readdir_r(dirp, &dirent, &result) && result) while (!readdir_r(dirp, &dirent, &result) && result)
{
FSTEntry entry;
const std::string virtualName(result->d_name);
#endif
// check for "." and ".."
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
((virtualName[0] == '.') && (virtualName[1] == '.') &&
(virtualName[2] == '\0')))
continue;
entry.virtualName = virtualName;
entry.physicalName = directory;
entry.physicalName += DIR_SEP + entry.virtualName;
if (IsDirectory(entry.physicalName))
{ {
entry.isDirectory = true; const std::string virtual_name(result->d_name);
// is a directory, lets go inside #endif
entry.size = ScanDirectoryTree(entry.physicalName, entry); if (virtual_name == "." || virtual_name == "..")
foundEntries += (u32)entry.size; continue;
} auto physical_name = directory + DIR_SEP + virtual_name;
else FSTEntry entry;
{ // is a file entry.isDirectory = IsDirectory(physical_name);
entry.isDirectory = false; if (entry.isDirectory)
entry.size = GetSize(entry.physicalName.c_str()); {
} if (recursive)
++foundEntries; entry = ScanDirectoryTree(physical_name, true);
// Push into the tree else
parentEntry.children.push_back(entry); entry.size = 0;
#ifdef _WIN32 }
} while (FindNextFile(hFind, &ffd) != 0); else
{
entry.size = GetSize(physical_name);
}
entry.virtualName = virtual_name;
entry.physicalName = physical_name;
++parent_entry.size;
// Push into the tree
parent_entry.children.push_back(entry);
#ifdef _WIN32
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind); FindClose(hFind);
#else #else
} }
closedir(dirp); closedir(dirp);
#endif #endif
// Return number of entries found. // Return number of entries found.
return foundEntries; return parent_entry;
} }

View File

@ -107,9 +107,8 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename);
// creates an empty file filename, returns true on success // creates an empty file filename, returns true on success
bool CreateEmptyFile(const std::string &filename); bool CreateEmptyFile(const std::string &filename);
// Scans the directory tree gets, starting from _Directory and adds the // Recursive or non-recursive list of files under directory.
// results into parentEntry. Returns the number of files+directories found FSTEntry ScanDirectoryTree(const std::string &directory, bool recursive);
u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry);
// deletes the given directory and anything under it. Returns true on success. // deletes the given directory and anything under it. Returns true on success.
bool DeleteDirRecursively(const std::string &directory); bool DeleteDirRecursively(const std::string &directory);

View File

@ -151,16 +151,7 @@ GCMemcardDirectory::GCMemcardDirectory(const std::string& directory, int slot, u
hdrfile.ReadBytes(&m_hdr, BLOCK_SIZE); hdrfile.ReadBytes(&m_hdr, BLOCK_SIZE);
} }
File::FSTEntry FST_Temp; std::vector<std::string> rFilenames = DoFileSearch({"*.gci"}, {m_SaveDirectory});
File::ScanDirectoryTree(m_SaveDirectory, FST_Temp);
CFileSearch::XStringVector Directory;
Directory.push_back(m_SaveDirectory);
CFileSearch::XStringVector Extensions;
Extensions.push_back("*.gci");
CFileSearch FileSearch(Extensions, Directory);
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
if (rFilenames.size() > 112) if (rFilenames.size() > 112)
{ {
@ -170,7 +161,7 @@ GCMemcardDirectory::GCMemcardDirectory(const std::string& directory, int slot, u
4000); 4000);
} }
for (auto gciFile : rFilenames) for (const std::string& gciFile : rFilenames)
{ {
if (m_saves.size() == DIRLEN) if (m_saves.size() == DIRLEN)
{ {

View File

@ -67,9 +67,8 @@ void CWiiSaveCrypted::ExportAllSaves()
const u32 path_mask = 0x00010000; const u32 path_mask = 0x00010000;
for (int i = 0; i < 8; ++i) for (int i = 0; i < 8; ++i)
{ {
File::FSTEntry fst_tmp;
std::string folder = StringFromFormat("%s/%08x/", title_folder.c_str(), path_mask | i); std::string folder = StringFromFormat("%s/%08x/", title_folder.c_str(), path_mask | i);
File::ScanDirectoryTree(folder, fst_tmp); File::FSTEntry fst_tmp = File::ScanDirectoryTree(folder, false);
for (const File::FSTEntry& entry : fst_tmp.children) for (const File::FSTEntry& entry : fst_tmp.children)
{ {
@ -627,8 +626,7 @@ void CWiiSaveCrypted::ScanForFiles(const std::string& save_directory, std::vecto
file_list.push_back(directories[i]); file_list.push_back(directories[i]);
} }
File::FSTEntry fst_tmp; File::FSTEntry fst_tmp = File::ScanDirectoryTree(directories[i], false);
File::ScanDirectoryTree(directories[i], fst_tmp);
for (const File::FSTEntry& elem : fst_tmp.children) for (const File::FSTEntry& elem : fst_tmp.children)
{ {
if (elem.virtualName != "banner.bin") if (elem.virtualName != "banner.bin")

View File

@ -5,7 +5,6 @@
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"
#include "Common/CommonPaths.h" #include "Common/CommonPaths.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/FileSearch.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/NandPaths.h" #include "Common/NandPaths.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
@ -106,25 +105,28 @@ IPCCommandResult CWII_IPC_HLE_Device_fs::IOCtlV(u32 _CommandAddress)
break; break;
} }
// make a file search File::FSTEntry entry = File::ScanDirectoryTree(DirName, false);
CFileSearch::XStringVector Directories;
Directories.push_back(DirName);
CFileSearch::XStringVector Extensions;
Extensions.push_back("*.*");
CFileSearch FileSearch(Extensions, Directories);
// it is one // it is one
if ((CommandBuffer.InBuffer.size() == 1) && (CommandBuffer.PayloadBuffer.size() == 1)) if ((CommandBuffer.InBuffer.size() == 1) && (CommandBuffer.PayloadBuffer.size() == 1))
{ {
size_t numFile = FileSearch.GetFileNames().size(); size_t numFile = entry.children.size();
INFO_LOG(WII_IPC_FILEIO, "\t%lu files found", (unsigned long)numFile); INFO_LOG(WII_IPC_FILEIO, "\t%lu files found", (unsigned long)numFile);
Memory::Write_U32((u32)numFile, CommandBuffer.PayloadBuffer[0].m_Address); Memory::Write_U32((u32)numFile, CommandBuffer.PayloadBuffer[0].m_Address);
} }
else else
{ {
for (File::FSTEntry& child : entry.children)
{
// Decode entities of invalid file system characters so that
// games (such as HP:HBP) will be able to find what they expect.
for (const Common::replace_t& r : replacements)
child.virtualName = ReplaceAll(child.virtualName, r.second, {r.first});
}
std::sort(entry.children.begin(), entry.children.end(), [](const File::FSTEntry& one, const File::FSTEntry& two) { return one.virtualName < two.virtualName; });
u32 MaxEntries = Memory::Read_U32(CommandBuffer.InBuffer[0].m_Address); u32 MaxEntries = Memory::Read_U32(CommandBuffer.InBuffer[0].m_Address);
memset(Memory::GetPointer(CommandBuffer.PayloadBuffer[0].m_Address), 0, CommandBuffer.PayloadBuffer[0].m_Size); memset(Memory::GetPointer(CommandBuffer.PayloadBuffer[0].m_Address), 0, CommandBuffer.PayloadBuffer[0].m_Size);
@ -132,22 +134,10 @@ IPCCommandResult CWII_IPC_HLE_Device_fs::IOCtlV(u32 _CommandAddress)
size_t numFiles = 0; size_t numFiles = 0;
char* pFilename = (char*)Memory::GetPointer((u32)(CommandBuffer.PayloadBuffer[0].m_Address)); char* pFilename = (char*)Memory::GetPointer((u32)(CommandBuffer.PayloadBuffer[0].m_Address));
for (size_t i=0; i<FileSearch.GetFileNames().size(); i++) for (size_t i=0; i < entry.children.size() && i < MaxEntries; i++)
{ {
if (i >= MaxEntries) const std::string& FileName = entry.children[i].virtualName;
break;
std::string name, ext;
SplitPath(FileSearch.GetFileNames()[i], nullptr, &name, &ext);
std::string FileName = name + ext;
// Decode entities of invalid file system characters so that
// games (such as HP:HBP) will be able to find what they expect.
for (const Common::replace_t& r : replacements)
{
for (size_t j = 0; (j = FileName.find(r.second, j)) != FileName.npos; ++j)
FileName.replace(j, r.second.length(), 1, r.first);
}
strcpy(pFilename, FileName.c_str()); strcpy(pFilename, FileName.c_str());
pFilename += FileName.length(); pFilename += FileName.length();
@ -192,10 +182,9 @@ IPCCommandResult CWII_IPC_HLE_Device_fs::IOCtlV(u32 _CommandAddress)
} }
else else
{ {
File::FSTEntry parentDir; File::FSTEntry parentDir = File::ScanDirectoryTree(path, true);
// add one for the folder itself, allows some games to create their save files // add one for the folder itself
// R8XE52 (Jurassic: The Hunted), STEETR (Tetris Party Deluxe) now create their saves with this change iNodes = 1 + (u32)parentDir.size;
iNodes = 1 + File::ScanDirectoryTree(path, parentDir);
u64 totalSize = ComputeTotalFileSize(parentDir); // "Real" size, to be converted to nand blocks u64 totalSize = ComputeTotalFileSize(parentDir); // "Real" size, to be converted to nand blocks
@ -542,8 +531,7 @@ void CWII_IPC_HLE_Device_fs::DoState(PointerWrap& p)
{ {
//recurse through tmp and save dirs and files //recurse through tmp and save dirs and files
File::FSTEntry parentEntry; File::FSTEntry parentEntry = File::ScanDirectoryTree(Path, true);
File::ScanDirectoryTree(Path, parentEntry);
std::deque<File::FSTEntry> todo; std::deque<File::FSTEntry> todo;
todo.insert(todo.end(), parentEntry.children.begin(), todo.insert(todo.end(), parentEntry.children.begin(),
parentEntry.children.end()); parentEntry.children.end());

View File

@ -339,7 +339,7 @@ void CVolumeDirectory::BuildFST()
File::FSTEntry rootEntry; File::FSTEntry rootEntry;
// read data from physical disk to rootEntry // read data from physical disk to rootEntry
u32 totalEntries = AddDirectoryEntries(m_rootDirectory, rootEntry) + 1; u64 totalEntries = AddDirectoryEntries(m_rootDirectory, rootEntry) + 1;
m_fstNameOffset = totalEntries * ENTRY_SIZE; // offset in FST nameTable m_fstNameOffset = totalEntries * ENTRY_SIZE; // offset in FST nameTable
m_FSTData.resize(m_fstNameOffset + m_totalNameSize); m_FSTData.resize(m_fstNameOffset + m_totalNameSize);
@ -423,7 +423,7 @@ void CVolumeDirectory::Write32(u32 data, u32 offset, std::vector<u8>* const buff
(*buffer)[offset] = (data) & 0xff; (*buffer)[offset] = (data) & 0xff;
} }
void CVolumeDirectory::WriteEntryData(u32& entryOffset, u8 type, u32 nameOffset, u64 dataOffset, u32 length) void CVolumeDirectory::WriteEntryData(u32& entryOffset, u8 type, u32 nameOffset, u64 dataOffset, u64 length)
{ {
m_FSTData[entryOffset++] = type; m_FSTData[entryOffset++] = type;
@ -451,7 +451,7 @@ void CVolumeDirectory::WriteEntry(const File::FSTEntry& entry, u32& fstOffset, u
{ {
u32 myOffset = fstOffset; u32 myOffset = fstOffset;
u32 myEntryNum = myOffset / ENTRY_SIZE; u32 myEntryNum = myOffset / ENTRY_SIZE;
WriteEntryData(fstOffset, DIRECTORY_ENTRY, nameOffset, parentEntryNum, (u32)(myEntryNum + entry.size + 1)); WriteEntryData(fstOffset, DIRECTORY_ENTRY, nameOffset, parentEntryNum, myEntryNum + entry.size + 1);
WriteEntryName(nameOffset, entry.virtualName); WriteEntryName(nameOffset, entry.virtualName);
for (const auto& child : entry.children) for (const auto& child : entry.children)
@ -462,7 +462,7 @@ void CVolumeDirectory::WriteEntry(const File::FSTEntry& entry, u32& fstOffset, u
else else
{ {
// put entry in FST // put entry in FST
WriteEntryData(fstOffset, FILE_ENTRY, nameOffset, dataOffset, (u32)entry.size); WriteEntryData(fstOffset, FILE_ENTRY, nameOffset, dataOffset, entry.size);
WriteEntryName(nameOffset, entry.virtualName); WriteEntryName(nameOffset, entry.virtualName);
// write entry to virtual disk // write entry to virtual disk
@ -490,11 +490,11 @@ static u32 ComputeNameSize(const File::FSTEntry& parentEntry)
return nameSize; return nameSize;
} }
u32 CVolumeDirectory::AddDirectoryEntries(const std::string& _Directory, File::FSTEntry& parentEntry) u64 CVolumeDirectory::AddDirectoryEntries(const std::string& _Directory, File::FSTEntry& parentEntry)
{ {
u32 foundEntries = ScanDirectoryTree(_Directory, parentEntry); parentEntry = File::ScanDirectoryTree(_Directory, true);
m_totalNameSize += ComputeNameSize(parentEntry); m_totalNameSize += ComputeNameSize(parentEntry);
return foundEntries; return parentEntry.size;
} }
} // namespace } // namespace

View File

@ -75,12 +75,12 @@ private:
void Write32(u32 data, u32 offset, std::vector<u8>* const buffer); void Write32(u32 data, u32 offset, std::vector<u8>* const buffer);
// FST creation // FST creation
void WriteEntryData(u32& entryOffset, u8 type, u32 nameOffset, u64 dataOffset, u32 length); void WriteEntryData(u32& entryOffset, u8 type, u32 nameOffset, u64 dataOffset, u64 length);
void WriteEntryName(u32& nameOffset, const std::string& name); void WriteEntryName(u32& nameOffset, const std::string& name);
void WriteEntry(const File::FSTEntry& entry, u32& fstOffset, u32& nameOffset, u64& dataOffset, u32 parentEntryNum); void WriteEntry(const File::FSTEntry& entry, u32& fstOffset, u32& nameOffset, u64& dataOffset, u32 parentEntryNum);
// returns number of entries found in _Directory // returns number of entries found in _Directory
u32 AddDirectoryEntries(const std::string& _Directory, File::FSTEntry& parentEntry); u64 AddDirectoryEntries(const std::string& _Directory, File::FSTEntry& parentEntry);
std::string m_rootDirectory; std::string m_rootDirectory;

View File

@ -25,9 +25,9 @@ void AbstractGameList::RemoveGames(QList<GameFile*> items)
DGameTracker::DGameTracker(QWidget* parent_widget) DGameTracker::DGameTracker(QWidget* parent_widget)
: QStackedWidget(parent_widget), : QStackedWidget(parent_widget),
m_watcher(this) m_watcher(new QFileSystemWatcher(this))
{ {
connect(&m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(ScanForGames())); connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(ScanForGames()));
m_tree_widget = new DGameTree(this); m_tree_widget = new DGameTree(this);
addWidget(m_tree_widget); addWidget(m_tree_widget);
@ -78,38 +78,20 @@ void DGameTracker::ScanForGames()
{ {
setDisabled(true); setDisabled(true);
CFileSearch::XStringVector dirs(SConfig::GetInstance().m_ISOFolder); delete m_watcher;
m_watcher = new QFileSystemWatcher(this);
if (SConfig::GetInstance().m_RecursiveISOFolder) if (SConfig::GetInstance().m_RecursiveISOFolder)
{ {
for (u32 i = 0; i < dirs.size(); i++) for (std::string dir : FindSubdirectories(SConfig::GetInstance().m_ISOFolder, /*recursive*/ true))
{ m_watcher->addPath(QString::fromStdString(dir));
File::FSTEntry FST_Temp; }
File::ScanDirectoryTree(dirs[i], FST_Temp); else
for (auto& entry : FST_Temp.children) {
{ for (std::string dir : SConfig::GetInstance().m_ISOFolder)
if (entry.isDirectory) m_watcher->addPath(QString::fromStdString(dir));
{
bool duplicate = false;
for (auto& dir : dirs)
{
if (dir == entry.physicalName)
{
duplicate = true;
break;
}
}
if (!duplicate)
dirs.push_back(entry.physicalName);
}
}
}
} }
for (std::string& dir : dirs) std::vector<std::string> exts;
m_watcher.addPath(QString::fromStdString(dir));
CFileSearch::XStringVector exts;
if (SConfig::GetInstance().m_ListGC) if (SConfig::GetInstance().m_ListGC)
{ {
exts.push_back("*.gcm"); exts.push_back("*.gcm");
@ -124,8 +106,7 @@ void DGameTracker::ScanForGames()
if (SConfig::GetInstance().m_ListWad) if (SConfig::GetInstance().m_ListWad)
exts.push_back("*.wad"); exts.push_back("*.wad");
CFileSearch FileSearch(exts, dirs); auto rFilenames = DoFileSearch(exts, SConfig::GetInstance().m_ISOFolder, SConfig::GetInstance().m_RecursiveISOFolder);
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
QList<GameFile*> newItems; QList<GameFile*> newItems;
QStringList allItems; QStringList allItems;

View File

@ -60,7 +60,7 @@ public slots:
private: private:
QMap<QString, GameFile*> m_games; QMap<QString, GameFile*> m_games;
QFileSystemWatcher m_watcher; QFileSystemWatcher* m_watcher;
GameListStyle m_current_style; GameListStyle m_current_style;
DGameGrid* m_grid_widget = nullptr; DGameGrid* m_grid_widget = nullptr;

View File

@ -162,12 +162,10 @@ void InterfaceConfigPane::LoadGUIValues()
void InterfaceConfigPane::LoadThemes() void InterfaceConfigPane::LoadThemes()
{ {
CFileSearch::XStringVector theme_dirs; auto sv = DoFileSearch({"*"}, {
theme_dirs.push_back(File::GetUserPath(D_THEMES_IDX)); File::GetUserPath(D_THEMES_IDX),
theme_dirs.push_back(File::GetSysDirectory() + THEMES_DIR); File::GetSysDirectory() + THEMES_DIR
}, /*recursive*/ false);
CFileSearch cfs(CFileSearch::XStringVector(1, "*"), theme_dirs);
auto const& sv = cfs.GetFileNames();
for (const std::string& filename : sv) for (const std::string& filename : sv)
{ {
std::string name, ext; std::string name, ext;

View File

@ -1966,15 +1966,9 @@ void CFrame::GameListChanged(wxCommandEvent& event)
SConfig::GetInstance().m_ListDrives = event.IsChecked(); SConfig::GetInstance().m_ListDrives = event.IsChecked();
break; break;
case IDM_PURGE_CACHE: case IDM_PURGE_CACHE:
CFileSearch::XStringVector Directories; std::vector<std::string> rFilenames = DoFileSearch({"*.cache"}, {File::GetUserPath(D_CACHE_IDX)});
Directories.push_back(File::GetUserPath(D_CACHE_IDX));
CFileSearch::XStringVector Extensions;
Extensions.push_back("*.cache");
CFileSearch FileSearch(Extensions, Directories); for (const std::string& rFilename : rFilenames)
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
for (auto& rFilename : rFilenames)
{ {
File::Delete(rFilename); File::Delete(rFilename);
} }

View File

@ -466,35 +466,7 @@ void CGameListCtrl::ScanForISOs()
{ {
ClearIsoFiles(); ClearIsoFiles();
CFileSearch::XStringVector Directories(SConfig::GetInstance().m_ISOFolder); std::vector<std::string> Extensions;
if (SConfig::GetInstance().m_RecursiveISOFolder)
{
for (u32 i = 0; i < Directories.size(); i++)
{
File::FSTEntry FST_Temp;
File::ScanDirectoryTree(Directories[i], FST_Temp);
for (auto& Entry : FST_Temp.children)
{
if (Entry.isDirectory)
{
bool duplicate = false;
for (auto& Directory : Directories)
{
if (Directory == Entry.physicalName)
{
duplicate = true;
break;
}
}
if (!duplicate)
Directories.push_back(Entry.physicalName);
}
}
}
}
CFileSearch::XStringVector Extensions;
if (SConfig::GetInstance().m_ListGC) if (SConfig::GetInstance().m_ListGC)
Extensions.push_back("*.gcm"); Extensions.push_back("*.gcm");
@ -508,8 +480,7 @@ void CGameListCtrl::ScanForISOs()
if (SConfig::GetInstance().m_ListWad) if (SConfig::GetInstance().m_ListWad)
Extensions.push_back("*.wad"); Extensions.push_back("*.wad");
CFileSearch FileSearch(Extensions, Directories); auto rFilenames = DoFileSearch(Extensions, SConfig::GetInstance().m_ISOFolder, SConfig::GetInstance().m_RecursiveISOFolder);
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
if (rFilenames.size() > 0) if (rFilenames.size() > 0)
{ {

View File

@ -172,18 +172,14 @@ void InputConfigDialog::UpdateProfileComboBox()
pname += PROFILES_PATH; pname += PROFILES_PATH;
pname += m_config.profile_name; pname += m_config.profile_name;
CFileSearch::XStringVector exts; std::vector<std::string> sv = DoFileSearch({"*.ini"}, {pname});
exts.push_back("*.ini");
CFileSearch::XStringVector dirs;
dirs.push_back(pname);
CFileSearch cfs(exts, dirs);
const CFileSearch::XStringVector& sv = cfs.GetFileNames();
wxArrayString strs; wxArrayString strs;
for (auto si = sv.cbegin(); si != sv.cend(); ++si) for (const std::string& filename : sv)
{ {
std::string str(si->begin() + si->find_last_of('/') + 1 , si->end() - 4) ; std::string base;
strs.push_back(StrToWxStr(str)); SplitPath(filename, nullptr, &base, nullptr);
strs.push_back(StrToWxStr(base));
} }
for (GamepadPage* page : m_padpages) for (GamepadPage* page : m_padpages)

View File

@ -38,9 +38,11 @@ Make AA apply instantly during gameplay if possible
#include <algorithm> #include <algorithm>
#include <cstdarg> #include <cstdarg>
#include <regex>
#include "Common/Atomic.h" #include "Common/Atomic.h"
#include "Common/CommonPaths.h" #include "Common/CommonPaths.h"
#include "Common/FileSearch.h"
#include "Common/Thread.h" #include "Common/Thread.h"
#include "Common/Logging/LogManager.h" #include "Common/Logging/LogManager.h"
@ -95,39 +97,16 @@ std::string VideoBackend::GetDisplayName() const
return "OpenGL"; return "OpenGL";
} }
static void GetShaders(std::vector<std::string> &shaders, const std::string &sub_dir = "") static std::vector<std::string> GetShaders(const std::string &sub_dir = "")
{ {
std::set<std::string> already_found; std::vector<std::string> paths = DoFileSearch({"*.glsl"}, {
shaders.clear();
const std::string directories[] = {
File::GetUserPath(D_SHADERS_IDX) + sub_dir, File::GetUserPath(D_SHADERS_IDX) + sub_dir,
File::GetSysDirectory() + SHADERS_DIR DIR_SEP + sub_dir, File::GetSysDirectory() + SHADERS_DIR DIR_SEP + sub_dir
}; });
for (auto& directory : directories) std::vector<std::string> result;
{ for (std::string path : paths)
if (!File::IsDirectory(directory)) result.push_back(std::regex_replace(path, std::regex("^.*/(.*)\\.glsl$"), "$1"));
continue; return result;
File::FSTEntry entry;
File::ScanDirectoryTree(directory, entry);
for (auto& file : entry.children)
{
std::string name = file.virtualName;
if (name.size() < 5)
continue;
if (strcasecmp(name.substr(name.size() - 5).c_str(), ".glsl"))
continue;
name = name.substr(0, name.size() - 5);
if (already_found.find(name) != already_found.end())
continue;
already_found.insert(name);
shaders.push_back(name);
}
}
std::sort(shaders.begin(), shaders.end());
} }
static void InitBackendInfo() static void InitBackendInfo()
@ -146,8 +125,8 @@ static void InitBackendInfo()
g_Config.backend_info.AAModes.assign(caamodes, caamodes + sizeof(caamodes)/sizeof(*caamodes)); g_Config.backend_info.AAModes.assign(caamodes, caamodes + sizeof(caamodes)/sizeof(*caamodes));
// pp shaders // pp shaders
GetShaders(g_Config.backend_info.PPShaders); g_Config.backend_info.PPShaders = GetShaders("");
GetShaders(g_Config.backend_info.AnaglyphShaders, std::string(ANAGLYPH_DIR DIR_SEP)); g_Config.backend_info.AnaglyphShaders = GetShaders(ANAGLYPH_DIR DIR_SEP);
} }
void VideoBackend::ShowConfig(void *_hParent) void VideoBackend::ShowConfig(void *_hParent)

View File

@ -79,38 +79,12 @@ void HiresTexture::Update()
s_textureCache.clear(); s_textureCache.clear();
} }
CFileSearch::XStringVector Directories; std::vector<std::string> Directories;
const std::string& gameCode = SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID; const std::string& gameCode = SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID;
std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str()); std::string szDir = StringFromFormat("%s%s", File::GetUserPath(D_HIRESTEXTURES_IDX).c_str(), gameCode.c_str());
Directories.push_back(szDir);
for (u32 i = 0; i < Directories.size(); i++) std::vector<std::string> Extensions {
{
File::FSTEntry FST_Temp;
File::ScanDirectoryTree(Directories[i], FST_Temp);
for (auto& entry : FST_Temp.children)
{
if (entry.isDirectory)
{
bool duplicate = false;
for (auto& Directory : Directories)
{
if (Directory == entry.physicalName)
{
duplicate = true;
break;
}
}
if (!duplicate)
Directories.push_back(entry.physicalName);
}
}
}
CFileSearch::XStringVector Extensions = {
"*.png", "*.png",
"*.bmp", "*.bmp",
"*.tga", "*.tga",
@ -118,8 +92,7 @@ void HiresTexture::Update()
"*.jpg" // Why not? Could be useful for large photo-like textures "*.jpg" // Why not? Could be useful for large photo-like textures
}; };
CFileSearch FileSearch(Extensions, Directories); auto rFilenames = DoFileSearch(Extensions, {szDir}, /*recursive*/ true);
const CFileSearch::XStringVector& rFilenames = FileSearch.GetFileNames();
const std::string code = StringFromFormat("%s_", gameCode.c_str()); const std::string code = StringFromFormat("%s_", gameCode.c_str());
const std::string code2 = ""; const std::string code2 = "";
@ -129,13 +102,8 @@ void HiresTexture::Update()
std::string FileName; std::string FileName;
SplitPath(rFilename, nullptr, &FileName, nullptr); SplitPath(rFilename, nullptr, &FileName, nullptr);
if (FileName.substr(0, code.length()) == code) if (FileName.substr(0, code.length()) == code ||
{ FileName.substr(0, s_format_prefix.length()) == s_format_prefix)
s_textureMap[FileName] = rFilename;
s_check_native_format = true;
}
if (FileName.substr(0, s_format_prefix.length()) == s_format_prefix)
{ {
s_textureMap[FileName] = rFilename; s_textureMap[FileName] = rFilename;
s_check_new_format = true; s_check_new_format = true;