common: fs: Expand android macros
This commit is contained in:
parent
e7c4c8b993
commit
c8e8c614a0
|
@ -1,63 +1,38 @@
|
||||||
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
|
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
#include "common/android/android_common.h"
|
||||||
|
#include "common/android/id_cache.h"
|
||||||
|
#include "common/assert.h"
|
||||||
#include "common/fs/fs_android.h"
|
#include "common/fs/fs_android.h"
|
||||||
#include "common/string_util.h"
|
#include "common/string_util.h"
|
||||||
|
|
||||||
namespace Common::FS::Android {
|
namespace Common::FS::Android {
|
||||||
|
|
||||||
JNIEnv* GetEnvForThread() {
|
|
||||||
thread_local static struct OwnedEnv {
|
|
||||||
OwnedEnv() {
|
|
||||||
status = g_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
|
|
||||||
if (status == JNI_EDETACHED)
|
|
||||||
g_jvm->AttachCurrentThread(&env, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
~OwnedEnv() {
|
|
||||||
if (status == JNI_EDETACHED)
|
|
||||||
g_jvm->DetachCurrentThread();
|
|
||||||
}
|
|
||||||
|
|
||||||
int status;
|
|
||||||
JNIEnv* env = nullptr;
|
|
||||||
} owned;
|
|
||||||
return owned.env;
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegisterCallbacks(JNIEnv* env, jclass clazz) {
|
void RegisterCallbacks(JNIEnv* env, jclass clazz) {
|
||||||
env->GetJavaVM(&g_jvm);
|
env->GetJavaVM(&g_jvm);
|
||||||
native_library = clazz;
|
native_library = clazz;
|
||||||
|
|
||||||
#define FH(FunctionName, JMethodID, Caller, JMethodName, Signature) \
|
s_get_parent_directory = env->GetStaticMethodID(native_library, "getParentDirectory",
|
||||||
F(JMethodID, JMethodName, Signature)
|
"(Ljava/lang/String;)Ljava/lang/String;");
|
||||||
#define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature) \
|
s_get_filename = env->GetStaticMethodID(native_library, "getFilename",
|
||||||
F(JMethodID, JMethodName, Signature)
|
"(Ljava/lang/String;)Ljava/lang/String;");
|
||||||
#define FS(FunctionName, ReturnValue, Parameters, JMethodID, JMethodName, Signature) \
|
s_get_size = env->GetStaticMethodID(native_library, "getSize", "(Ljava/lang/String;)J");
|
||||||
F(JMethodID, JMethodName, Signature)
|
s_is_directory = env->GetStaticMethodID(native_library, "isDirectory", "(Ljava/lang/String;)Z");
|
||||||
#define F(JMethodID, JMethodName, Signature) \
|
s_file_exists = env->GetStaticMethodID(native_library, "exists", "(Ljava/lang/String;)Z");
|
||||||
JMethodID = env->GetStaticMethodID(native_library, JMethodName, Signature);
|
s_open_content_uri = env->GetStaticMethodID(native_library, "openContentUri",
|
||||||
ANDROID_SINGLE_PATH_HELPER_FUNCTIONS(FH)
|
"(Ljava/lang/String;Ljava/lang/String;)I");
|
||||||
ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(FR)
|
|
||||||
ANDROID_STORAGE_FUNCTIONS(FS)
|
|
||||||
#undef F
|
|
||||||
#undef FS
|
|
||||||
#undef FR
|
|
||||||
#undef FH
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnRegisterCallbacks() {
|
void UnRegisterCallbacks() {
|
||||||
#define FH(FunctionName, JMethodID, Caller, JMethodName, Signature) F(JMethodID)
|
s_get_parent_directory = nullptr;
|
||||||
#define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature) F(JMethodID)
|
s_get_filename = nullptr;
|
||||||
#define FS(FunctionName, ReturnValue, Parameters, JMethodID, JMethodName, Signature) F(JMethodID)
|
|
||||||
#define F(JMethodID) JMethodID = nullptr;
|
s_get_size = nullptr;
|
||||||
ANDROID_SINGLE_PATH_HELPER_FUNCTIONS(FH)
|
s_is_directory = nullptr;
|
||||||
ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(FR)
|
s_file_exists = nullptr;
|
||||||
ANDROID_STORAGE_FUNCTIONS(FS)
|
|
||||||
#undef F
|
s_open_content_uri = nullptr;
|
||||||
#undef FS
|
|
||||||
#undef FR
|
|
||||||
#undef FH
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsContentUri(const std::string& path) {
|
bool IsContentUri(const std::string& path) {
|
||||||
|
@ -69,8 +44,8 @@ bool IsContentUri(const std::string& path) {
|
||||||
return path.find(prefix) == 0;
|
return path.find(prefix) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int OpenContentUri(const std::string& filepath, OpenMode openmode) {
|
s32 OpenContentUri(const std::string& filepath, OpenMode openmode) {
|
||||||
if (open_content_uri == nullptr)
|
if (s_open_content_uri == nullptr)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
const char* mode = "";
|
const char* mode = "";
|
||||||
|
@ -82,50 +57,66 @@ int OpenContentUri(const std::string& filepath, OpenMode openmode) {
|
||||||
UNIMPLEMENTED();
|
UNIMPLEMENTED();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
auto env = GetEnvForThread();
|
auto env = Common::Android::GetEnvForThread();
|
||||||
jstring j_filepath = env->NewStringUTF(filepath.c_str());
|
jstring j_filepath = Common::Android::ToJString(env, filepath);
|
||||||
jstring j_mode = env->NewStringUTF(mode);
|
jstring j_mode = Common::Android::ToJString(env, mode);
|
||||||
return env->CallStaticIntMethod(native_library, open_content_uri, j_filepath, j_mode);
|
return env->CallStaticIntMethod(native_library, s_open_content_uri, j_filepath, j_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature) \
|
u64 GetSize(const std::string& filepath) {
|
||||||
F(FunctionName, ReturnValue, JMethodID, Caller)
|
if (s_get_size == nullptr) {
|
||||||
#define F(FunctionName, ReturnValue, JMethodID, Caller) \
|
return 0;
|
||||||
ReturnValue FunctionName(const std::string& filepath) { \
|
|
||||||
if (JMethodID == nullptr) { \
|
|
||||||
return 0; \
|
|
||||||
} \
|
|
||||||
auto env = GetEnvForThread(); \
|
|
||||||
jstring j_filepath = env->NewStringUTF(filepath.c_str()); \
|
|
||||||
return env->Caller(native_library, JMethodID, j_filepath); \
|
|
||||||
}
|
}
|
||||||
ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(FR)
|
auto env = Common::Android::GetEnvForThread();
|
||||||
#undef F
|
return static_cast<u64>(env->CallStaticLongMethod(
|
||||||
#undef FR
|
native_library, s_get_size,
|
||||||
|
Common::Android::ToJString(Common::Android::GetEnvForThread(), filepath)));
|
||||||
|
}
|
||||||
|
|
||||||
#define FH(FunctionName, JMethodID, Caller, JMethodName, Signature) \
|
bool IsDirectory(const std::string& filepath) {
|
||||||
F(FunctionName, JMethodID, Caller)
|
if (s_is_directory == nullptr) {
|
||||||
#define F(FunctionName, JMethodID, Caller) \
|
return 0;
|
||||||
std::string FunctionName(const std::string& filepath) { \
|
|
||||||
if (JMethodID == nullptr) { \
|
|
||||||
return 0; \
|
|
||||||
} \
|
|
||||||
auto env = GetEnvForThread(); \
|
|
||||||
jstring j_filepath = env->NewStringUTF(filepath.c_str()); \
|
|
||||||
jstring j_return = \
|
|
||||||
static_cast<jstring>(env->Caller(native_library, JMethodID, j_filepath)); \
|
|
||||||
if (!j_return) { \
|
|
||||||
return {}; \
|
|
||||||
} \
|
|
||||||
const jchar* jchars = env->GetStringChars(j_return, nullptr); \
|
|
||||||
const jsize length = env->GetStringLength(j_return); \
|
|
||||||
const std::u16string_view string_view(reinterpret_cast<const char16_t*>(jchars), length); \
|
|
||||||
const std::string converted_string = Common::UTF16ToUTF8(string_view); \
|
|
||||||
env->ReleaseStringChars(j_return, jchars); \
|
|
||||||
return converted_string; \
|
|
||||||
}
|
}
|
||||||
ANDROID_SINGLE_PATH_HELPER_FUNCTIONS(FH)
|
auto env = Common::Android::GetEnvForThread();
|
||||||
#undef F
|
return env->CallStaticBooleanMethod(
|
||||||
#undef FH
|
native_library, s_is_directory,
|
||||||
|
Common::Android::ToJString(Common::Android::GetEnvForThread(), filepath));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Exists(const std::string& filepath) {
|
||||||
|
if (s_file_exists == nullptr) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
auto env = Common::Android::GetEnvForThread();
|
||||||
|
return env->CallStaticBooleanMethod(
|
||||||
|
native_library, s_file_exists,
|
||||||
|
Common::Android::ToJString(Common::Android::GetEnvForThread(), filepath));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string GetParentDirectory(const std::string& filepath) {
|
||||||
|
if (s_get_parent_directory == nullptr) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
auto env = Common::Android::GetEnvForThread();
|
||||||
|
jstring j_return = static_cast<jstring>(env->CallStaticObjectMethod(
|
||||||
|
native_library, s_get_parent_directory, Common::Android::ToJString(env, filepath)));
|
||||||
|
if (!j_return) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return Common::Android::GetJString(env, j_return);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string GetFilename(const std::string& filepath) {
|
||||||
|
if (s_get_filename == nullptr) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
auto env = Common::Android::GetEnvForThread();
|
||||||
|
jstring j_return = static_cast<jstring>(env->CallStaticObjectMethod(
|
||||||
|
native_library, s_get_filename, Common::Android::ToJString(env, filepath)));
|
||||||
|
if (!j_return) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return Common::Android::GetJString(env, j_return);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Common::FS::Android
|
} // namespace Common::FS::Android
|
||||||
|
|
|
@ -7,38 +7,17 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
|
|
||||||
#define ANDROID_STORAGE_FUNCTIONS(V) \
|
|
||||||
V(OpenContentUri, int, (const std::string& filepath, OpenMode openmode), open_content_uri, \
|
|
||||||
"openContentUri", "(Ljava/lang/String;Ljava/lang/String;)I")
|
|
||||||
|
|
||||||
#define ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(V) \
|
|
||||||
V(GetSize, std::uint64_t, get_size, CallStaticLongMethod, "getSize", "(Ljava/lang/String;)J") \
|
|
||||||
V(IsDirectory, bool, is_directory, CallStaticBooleanMethod, "isDirectory", \
|
|
||||||
"(Ljava/lang/String;)Z") \
|
|
||||||
V(Exists, bool, file_exists, CallStaticBooleanMethod, "exists", "(Ljava/lang/String;)Z")
|
|
||||||
|
|
||||||
#define ANDROID_SINGLE_PATH_HELPER_FUNCTIONS(V) \
|
|
||||||
V(GetParentDirectory, get_parent_directory, CallStaticObjectMethod, "getParentDirectory", \
|
|
||||||
"(Ljava/lang/String;)Ljava/lang/String;") \
|
|
||||||
V(GetFilename, get_filename, CallStaticObjectMethod, "getFilename", \
|
|
||||||
"(Ljava/lang/String;)Ljava/lang/String;")
|
|
||||||
|
|
||||||
namespace Common::FS::Android {
|
namespace Common::FS::Android {
|
||||||
|
|
||||||
static JavaVM* g_jvm = nullptr;
|
static JavaVM* g_jvm = nullptr;
|
||||||
static jclass native_library = nullptr;
|
static jclass native_library = nullptr;
|
||||||
|
|
||||||
#define FH(FunctionName, JMethodID, Caller, JMethodName, Signature) F(JMethodID)
|
static jmethodID s_get_parent_directory;
|
||||||
#define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature) F(JMethodID)
|
static jmethodID s_get_filename;
|
||||||
#define FS(FunctionName, ReturnValue, Parameters, JMethodID, JMethodName, Signature) F(JMethodID)
|
static jmethodID s_get_size;
|
||||||
#define F(JMethodID) static jmethodID JMethodID = nullptr;
|
static jmethodID s_is_directory;
|
||||||
ANDROID_SINGLE_PATH_HELPER_FUNCTIONS(FH)
|
static jmethodID s_file_exists;
|
||||||
ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(FR)
|
static jmethodID s_open_content_uri;
|
||||||
ANDROID_STORAGE_FUNCTIONS(FS)
|
|
||||||
#undef F
|
|
||||||
#undef FS
|
|
||||||
#undef FR
|
|
||||||
#undef FH
|
|
||||||
|
|
||||||
enum class OpenMode {
|
enum class OpenMode {
|
||||||
Read,
|
Read,
|
||||||
|
@ -57,24 +36,11 @@ void UnRegisterCallbacks();
|
||||||
|
|
||||||
bool IsContentUri(const std::string& path);
|
bool IsContentUri(const std::string& path);
|
||||||
|
|
||||||
#define FS(FunctionName, ReturnValue, Parameters, JMethodID, JMethodName, Signature) \
|
int OpenContentUri(const std::string& filepath, OpenMode openmode);
|
||||||
F(FunctionName, Parameters, ReturnValue)
|
std::uint64_t GetSize(const std::string& filepath);
|
||||||
#define F(FunctionName, Parameters, ReturnValue) ReturnValue FunctionName Parameters;
|
bool IsDirectory(const std::string& filepath);
|
||||||
ANDROID_STORAGE_FUNCTIONS(FS)
|
bool Exists(const std::string& filepath);
|
||||||
#undef F
|
std::string GetParentDirectory(const std::string& filepath);
|
||||||
#undef FS
|
std::string GetFilename(const std::string& filepath);
|
||||||
|
|
||||||
#define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature) \
|
|
||||||
F(FunctionName, ReturnValue)
|
|
||||||
#define F(FunctionName, ReturnValue) ReturnValue FunctionName(const std::string& filepath);
|
|
||||||
ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(FR)
|
|
||||||
#undef F
|
|
||||||
#undef FR
|
|
||||||
|
|
||||||
#define FH(FunctionName, JMethodID, Caller, JMethodName, Signature) F(FunctionName)
|
|
||||||
#define F(FunctionName) std::string FunctionName(const std::string& filepath);
|
|
||||||
ANDROID_SINGLE_PATH_HELPER_FUNCTIONS(FH)
|
|
||||||
#undef F
|
|
||||||
#undef FH
|
|
||||||
|
|
||||||
} // namespace Common::FS::Android
|
} // namespace Common::FS::Android
|
||||||
|
|
Reference in New Issue