mirror of
https://github.com/izzy2lost/dolphin.git
synced 2026-06-19 01:16:48 -07: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
|
||||
|
||||
Reference in New Issue
Block a user