mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-01-31 01:15:17 +01:00
Common/FileSearch: Refactor DoFileSearch
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <ios>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
@@ -49,9 +50,13 @@ std::vector<std::string> JStringArrayToVector(JNIEnv* env, jobjectArray array)
|
||||
return result;
|
||||
}
|
||||
|
||||
jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector<std::string>& vector)
|
||||
jobjectArray SpanToJStringArray(JNIEnv* env, std::span<const std::string_view> span)
|
||||
{
|
||||
return VectorToJObjectArray(env, vector, IDCache::GetStringClass(), ToJString);
|
||||
return SpanToJObjectArray(env, span, IDCache::GetStringClass(), ToJString);
|
||||
}
|
||||
jobjectArray SpanToJStringArray(JNIEnv* env, std::span<const std::string> span)
|
||||
{
|
||||
return SpanToJObjectArray(env, span, IDCache::GetStringClass(), ToJString);
|
||||
}
|
||||
|
||||
bool IsPathAndroidContent(std::string_view uri)
|
||||
@@ -193,13 +198,13 @@ std::vector<std::string> GetAndroidContentChildNames(std::string_view uri)
|
||||
}
|
||||
|
||||
std::vector<std::string> DoFileSearchAndroidContent(std::string_view directory,
|
||||
const std::vector<std::string>& extensions,
|
||||
std::span<const std::string_view> extensions,
|
||||
bool recursive)
|
||||
{
|
||||
JNIEnv* env = IDCache::GetEnvForThread();
|
||||
|
||||
jstring j_directory = ToJString(env, directory);
|
||||
jobjectArray j_extensions = VectorToJStringArray(env, extensions);
|
||||
jobjectArray j_extensions = SpanToJStringArray(env, extensions);
|
||||
|
||||
jobjectArray j_result = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
|
||||
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerDoFileSearch(), j_directory,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ios>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
@@ -14,22 +15,34 @@ std::string GetJString(JNIEnv* env, jstring jstr);
|
||||
jstring ToJString(JNIEnv* env, std::string_view str);
|
||||
|
||||
std::vector<std::string> JStringArrayToVector(JNIEnv* env, jobjectArray array);
|
||||
jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector<std::string>& vector);
|
||||
jobjectArray SpanToJStringArray(JNIEnv* env, std::span<const std::string_view> span);
|
||||
jobjectArray SpanToJStringArray(JNIEnv* env, std::span<const std::string> span);
|
||||
inline jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector<std::string>& vector)
|
||||
{
|
||||
return SpanToJStringArray(env, vector);
|
||||
}
|
||||
|
||||
template <typename T, typename F>
|
||||
jobjectArray VectorToJObjectArray(JNIEnv* env, const std::vector<T>& vector, jclass clazz, F f)
|
||||
jobjectArray SpanToJObjectArray(JNIEnv* env, std::span<const T> span, jclass clazz, F f)
|
||||
{
|
||||
const auto vector_size = static_cast<jsize>(vector.size());
|
||||
jobjectArray result = env->NewObjectArray(vector_size, clazz, nullptr);
|
||||
for (jsize i = 0; i < vector_size; ++i)
|
||||
const auto span_size = static_cast<jsize>(span.size());
|
||||
jobjectArray result = env->NewObjectArray(span_size, clazz, nullptr);
|
||||
for (jsize i = 0; i < span_size; ++i)
|
||||
{
|
||||
jobject obj = f(env, vector[i]);
|
||||
jobject obj = f(env, span[i]);
|
||||
env->SetObjectArrayElement(result, i, obj);
|
||||
env->DeleteLocalRef(obj);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T, typename F>
|
||||
inline jobjectArray VectorToJObjectArray(JNIEnv* env, const std::vector<T>& vector, jclass clazz,
|
||||
F f)
|
||||
{
|
||||
return SpanToJObjectArray(env, std::span(vector), clazz, f);
|
||||
}
|
||||
|
||||
// Returns true if the given path should be opened as Android content instead of a normal file.
|
||||
bool IsPathAndroidContent(std::string_view uri);
|
||||
|
||||
@@ -55,7 +68,7 @@ std::string GetAndroidContentDisplayName(std::string_view uri);
|
||||
std::vector<std::string> GetAndroidContentChildNames(std::string_view uri);
|
||||
|
||||
std::vector<std::string> DoFileSearchAndroidContent(std::string_view directory,
|
||||
const std::vector<std::string>& extensions,
|
||||
std::span<const std::string_view> extensions,
|
||||
bool recursive);
|
||||
|
||||
int GetNetworkIpAddress();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2018 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
@@ -35,8 +37,10 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_finali
|
||||
JNIEXPORT jobjectArray JNICALL Java_org_dolphinemu_dolphinemu_model_GameFileCache_getAllGamePaths(
|
||||
JNIEnv* env, jclass, jobjectArray folder_paths, jboolean recursive_scan)
|
||||
{
|
||||
return VectorToJStringArray(
|
||||
env, UICommon::FindAllGamePaths(JStringArrayToVector(env, folder_paths), recursive_scan));
|
||||
const std::vector<std::string> paths = JStringArrayToVector(env, folder_paths);
|
||||
std::vector<std::string_view> path_views;
|
||||
std::ranges::copy(paths, std::back_inserter(path_views));
|
||||
return SpanToJStringArray(env, UICommon::FindAllGamePaths(path_views, recursive_scan));
|
||||
}
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <system_error>
|
||||
|
||||
#include "Common/CommonPaths.h"
|
||||
@@ -21,16 +19,14 @@
|
||||
#endif
|
||||
|
||||
#include <cstring>
|
||||
#include "Common/CommonFuncs.h"
|
||||
#include "Common/FileUtil.h"
|
||||
#endif
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
std::vector<std::string> DoFileSearch(const std::vector<std::string>& directories,
|
||||
const std::vector<std::string>& exts, bool recursive)
|
||||
std::vector<std::string> DoFileSearch(std::span<const std::string_view> directories,
|
||||
std::span<const std::string_view> exts, bool recursive)
|
||||
{
|
||||
const bool accept_all = exts.empty();
|
||||
|
||||
|
||||
@@ -3,14 +3,36 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Common
|
||||
{
|
||||
// Callers can pass empty "exts" to indicate they want all files + directories in results
|
||||
// Otherwise, only files matching the extensions are returned
|
||||
std::vector<std::string> DoFileSearch(const std::vector<std::string>& directories,
|
||||
const std::vector<std::string>& exts = {},
|
||||
std::vector<std::string> DoFileSearch(std::span<const std::string_view> directories,
|
||||
std::span<const std::string_view> exts = {},
|
||||
bool recursive = false);
|
||||
|
||||
inline std::vector<std::string> DoFileSearch(std::span<const std::string_view> directories,
|
||||
std::string_view ext, bool recursive = false)
|
||||
{
|
||||
return DoFileSearch(directories, std::span(&ext, 1), recursive);
|
||||
}
|
||||
|
||||
inline std::vector<std::string> DoFileSearch(std::string_view directory,
|
||||
std::span<const std::string_view> exts = {},
|
||||
bool recursive = false)
|
||||
{
|
||||
return DoFileSearch(std::span(&directory, 1), exts, recursive);
|
||||
}
|
||||
|
||||
inline std::vector<std::string> DoFileSearch(std::string_view directory, std::string_view ext,
|
||||
bool recursive = false)
|
||||
{
|
||||
return DoFileSearch(std::span(&directory, 1), std::span(&ext, 1), recursive);
|
||||
}
|
||||
|
||||
} // namespace Common
|
||||
|
||||
@@ -134,7 +134,7 @@ std::vector<std::string> GCMemcardDirectory::GetFileNamesForGameID(const std::st
|
||||
game_code = Common::swap32(reinterpret_cast<const u8*>(game_id.c_str()));
|
||||
|
||||
std::vector<Memcard::DEntry> loaded_saves;
|
||||
for (const std::string& file_name : Common::DoFileSearch({directory}, {".gci"}))
|
||||
for (const std::string& file_name : Common::DoFileSearch(directory, ".gci"))
|
||||
{
|
||||
File::IOFile gci_file(file_name, "rb");
|
||||
if (!gci_file)
|
||||
@@ -190,7 +190,7 @@ GCMemcardDirectory::GCMemcardDirectory(const std::string& directory, ExpansionIn
|
||||
}
|
||||
|
||||
const bool current_game_only = Config::Get(Config::SESSION_GCI_FOLDER_CURRENT_GAME_ONLY);
|
||||
const std::vector<std::string> filenames = Common::DoFileSearch({m_save_directory}, {".gci"});
|
||||
const std::vector<std::string> filenames = Common::DoFileSearch(m_save_directory, ".gci");
|
||||
|
||||
// split up into files for current games we should definitely load,
|
||||
// and files for other games that we don't care too much about
|
||||
|
||||
@@ -399,7 +399,7 @@ std::vector<Patch> GenerateRiivolutionPatchesFromConfig(const std::string& root_
|
||||
const std::optional<Config> config = ParseConfigFile(
|
||||
fmt::format("{}/riivolution/config/{}.xml", root_directory, game_id.substr(0, 4)));
|
||||
|
||||
for (const std::string& path : Common::DoFileSearch({root_directory + "riivolution"}, {".xml"}))
|
||||
for (const std::string& path : Common::DoFileSearch(root_directory + "riivolution", ".xml"))
|
||||
{
|
||||
std::optional<Disc> parsed = ParseFile(path);
|
||||
if (!parsed || !parsed->IsValidForGame(game_id, revision, disc_number))
|
||||
|
||||
@@ -534,7 +534,7 @@ void MappingWindow::PopulateProfileSelection()
|
||||
m_profiles_combo->clear();
|
||||
|
||||
const std::string profiles_path = m_config->GetUserProfileDirectoryPath();
|
||||
for (const auto& filename : Common::DoFileSearch({profiles_path}, {".ini"}))
|
||||
for (const auto& filename : Common::DoFileSearch(profiles_path, ".ini"))
|
||||
{
|
||||
std::string basename;
|
||||
SplitPath(filename, nullptr, &basename, nullptr);
|
||||
@@ -544,8 +544,7 @@ void MappingWindow::PopulateProfileSelection()
|
||||
|
||||
m_profiles_combo->insertSeparator(m_profiles_combo->count());
|
||||
|
||||
for (const auto& filename :
|
||||
Common::DoFileSearch({m_config->GetSysProfileDirectoryPath()}, {".ini"}))
|
||||
for (const auto& filename : Common::DoFileSearch(m_config->GetSysProfileDirectoryPath(), ".ini"))
|
||||
{
|
||||
std::string basename;
|
||||
SplitPath(filename, nullptr, &basename, nullptr);
|
||||
|
||||
@@ -109,7 +109,7 @@ void RiivolutionBootWidget::LoadMatchingXMLs()
|
||||
{
|
||||
const std::string& riivolution_dir = File::GetUserPath(D_RIIVOLUTION_IDX);
|
||||
const auto config = LoadConfigXML(riivolution_dir);
|
||||
for (const std::string& path : Common::DoFileSearch({riivolution_dir + "riivolution"}, {".xml"}))
|
||||
for (const std::string& path : Common::DoFileSearch(riivolution_dir + "riivolution", ".xml"))
|
||||
{
|
||||
auto parsed = DiscIO::Riivolution::ParseFile(path);
|
||||
if (!parsed || !parsed->IsValidForGame(m_game_id, m_revision, m_disc_number))
|
||||
|
||||
@@ -132,8 +132,8 @@ void InterfacePane::CreateUI()
|
||||
combobox_layout->addRow(tr("&Language:"), m_combobox_language);
|
||||
|
||||
// List available themes
|
||||
auto theme_paths =
|
||||
Common::DoFileSearch({File::GetUserPath(D_THEMES_IDX), File::GetSysDirectory() + THEMES_DIR});
|
||||
auto theme_paths = Common::DoFileSearch(
|
||||
{{File::GetUserPath(D_THEMES_IDX), File::GetSysDirectory() + THEMES_DIR}});
|
||||
std::vector<std::string> theme_names;
|
||||
theme_names.reserve(theme_paths.size());
|
||||
std::ranges::transform(theme_paths, std::back_inserter(theme_names), PathToFileName);
|
||||
@@ -147,7 +147,7 @@ void InterfacePane::CreateUI()
|
||||
m_label_userstyle = new QLabel(tr("Style:"));
|
||||
combobox_layout->addRow(m_label_userstyle, m_combobox_userstyle);
|
||||
|
||||
auto userstyle_search_results = Common::DoFileSearch({File::GetUserPath(D_STYLES_IDX)});
|
||||
auto userstyle_search_results = Common::DoFileSearch(File::GetUserPath(D_STYLES_IDX));
|
||||
|
||||
m_combobox_userstyle->addItem(tr("(System)"), static_cast<int>(Settings::StyleType::System));
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ void DynamicInputTextureManager::Load()
|
||||
|
||||
for (const auto& dynamic_input_directory : dynamic_input_directories)
|
||||
{
|
||||
const auto json_files = Common::DoFileSearch({dynamic_input_directory}, {".json"});
|
||||
const auto json_files = Common::DoFileSearch(dynamic_input_directory, ".json");
|
||||
for (auto& file : json_files)
|
||||
{
|
||||
m_configuration.emplace_back(file);
|
||||
|
||||
@@ -37,7 +37,7 @@ std::vector<std::string> GetProfilesFromSetting(const std::string& setting, cons
|
||||
const std::string path = root + std::string(StripWhitespace(setting_choice));
|
||||
if (File::IsDirectory(path))
|
||||
{
|
||||
const auto files_under_directory = Common::DoFileSearch({path}, {".ini"}, true);
|
||||
const auto files_under_directory = Common::DoFileSearch(path, ".ini", true);
|
||||
result.insert(result.end(), files_under_directory.begin(), files_under_directory.end());
|
||||
}
|
||||
else
|
||||
@@ -57,7 +57,7 @@ std::vector<std::string> ProfileCycler::GetProfilesForDevice(InputConfig* device
|
||||
{
|
||||
const std::string device_profile_root_location(
|
||||
device_configuration->GetUserProfileDirectoryPath());
|
||||
return Common::DoFileSearch({device_profile_root_location}, {".ini"}, true);
|
||||
return Common::DoFileSearch(device_profile_root_location, ".ini", true);
|
||||
}
|
||||
|
||||
std::string ProfileCycler::GetProfile(CycleDirection cycle_direction, int& profile_index,
|
||||
|
||||
@@ -28,12 +28,12 @@ namespace UICommon
|
||||
{
|
||||
static constexpr u32 CACHE_REVISION = 26; // Last changed in PR 10084
|
||||
|
||||
std::vector<std::string> FindAllGamePaths(const std::vector<std::string>& directories_to_scan,
|
||||
std::vector<std::string> FindAllGamePaths(std::span<const std::string_view> directories_to_scan,
|
||||
bool recursive_scan)
|
||||
{
|
||||
static const std::vector<std::string> search_extensions = {
|
||||
".gcm", ".tgc", ".bin", ".iso", ".ciso", ".gcz", ".wbfs",
|
||||
".wia", ".rvz", ".nfs", ".wad", ".dol", ".elf", ".json"};
|
||||
constexpr auto search_extensions =
|
||||
std::to_array<std::string_view>({".gcm", ".tgc", ".bin", ".iso", ".ciso", ".gcz", ".wbfs",
|
||||
".wia", ".rvz", ".nfs", ".wad", ".dol", ".elf", ".json"});
|
||||
|
||||
// TODO: We could process paths iteratively as they are found
|
||||
return Common::DoFileSearch(directories_to_scan, search_extensions, recursive_scan);
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace UICommon
|
||||
{
|
||||
class GameFile;
|
||||
|
||||
std::vector<std::string> FindAllGamePaths(const std::vector<std::string>& directories_to_scan,
|
||||
std::vector<std::string> FindAllGamePaths(std::span<const std::string_view> directories_to_scan,
|
||||
bool recursive_scan);
|
||||
|
||||
class GameFileCache
|
||||
|
||||
@@ -32,7 +32,7 @@ bool Init()
|
||||
{
|
||||
packs.clear();
|
||||
const std::vector<std::string> pack_list =
|
||||
Common::DoFileSearch({File::GetUserPath(D_RESOURCEPACK_IDX)}, {".zip"});
|
||||
Common::DoFileSearch(File::GetUserPath(D_RESOURCEPACK_IDX), ".zip");
|
||||
|
||||
Common::IniFile file = GetPackConfig();
|
||||
|
||||
|
||||
@@ -305,7 +305,7 @@ bool ResourcePack::Uninstall(const std::string& path)
|
||||
|
||||
while (dir.length() > (path + TEXTURE_PATH).length())
|
||||
{
|
||||
auto is_empty = Common::DoFileSearch({dir}).empty();
|
||||
const auto is_empty = Common::DoFileSearch(dir).empty();
|
||||
|
||||
if (is_empty)
|
||||
File::DeleteDir(dir);
|
||||
|
||||
@@ -93,7 +93,7 @@ void HiresTexture::Update()
|
||||
const std::string& game_id = SConfig::GetInstance().GetGameID();
|
||||
const std::set<std::string> texture_directories =
|
||||
GetTextureDirectoriesWithGameId(File::GetUserPath(D_HIRESTEXTURES_IDX), game_id);
|
||||
const std::vector<std::string> extensions{".png", ".dds"};
|
||||
constexpr auto extensions = std::to_array<std::string_view>({".png", ".dds"});
|
||||
|
||||
for (const auto& texture_directory : texture_directories)
|
||||
{
|
||||
@@ -101,7 +101,7 @@ void HiresTexture::Update()
|
||||
s_file_library->Watch(texture_directory);
|
||||
|
||||
const auto texture_paths =
|
||||
Common::DoFileSearch({texture_directory}, extensions, /*recursive*/ true);
|
||||
Common::DoFileSearch(texture_directory, extensions, /*recursive*/ true);
|
||||
|
||||
bool failed_insert = false;
|
||||
for (auto& path : texture_paths)
|
||||
@@ -227,7 +227,7 @@ std::set<std::string> GetTextureDirectoriesWithGameId(const std::string& root_di
|
||||
};
|
||||
|
||||
// Look for any other directories that might be specific to the given gameid
|
||||
const auto files = Common::DoFileSearch({root_directory}, {".txt"}, true);
|
||||
const auto files = Common::DoFileSearch(root_directory, ".txt", true);
|
||||
for (const auto& file : files)
|
||||
{
|
||||
if (match_gameid_or_all(file))
|
||||
|
||||
@@ -387,9 +387,9 @@ PostProcessing::~PostProcessing()
|
||||
static std::vector<std::string> GetShaders(const std::string& sub_dir = "")
|
||||
{
|
||||
std::vector<std::string> paths =
|
||||
Common::DoFileSearch({File::GetUserPath(D_SHADERS_IDX) + sub_dir,
|
||||
File::GetSysDirectory() + SHADERS_DIR DIR_SEP + sub_dir},
|
||||
{".glsl"});
|
||||
Common::DoFileSearch({{File::GetUserPath(D_SHADERS_IDX) + sub_dir,
|
||||
File::GetSysDirectory() + SHADERS_DIR DIR_SEP + sub_dir}},
|
||||
".glsl");
|
||||
std::vector<std::string> result;
|
||||
for (std::string path : paths)
|
||||
{
|
||||
|
||||
@@ -58,7 +58,7 @@ void TextureDumper::DumpTexture(const ::AbstractTexture& texture, std::string ba
|
||||
if (!File::IsDirectory(dump_dir))
|
||||
File::CreateDir(dump_dir);
|
||||
|
||||
for (auto& filename : Common::DoFileSearch({dump_dir}, {".png"}, true))
|
||||
for (auto& filename : Common::DoFileSearch(dump_dir, ".png", true))
|
||||
{
|
||||
std::string name;
|
||||
SplitPath(filename, nullptr, &name, nullptr);
|
||||
|
||||
Reference in New Issue
Block a user