From b3a368ae06c5f82afdd617355afba388c90d19e8 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 27 Dec 2022 22:28:06 +0100 Subject: [PATCH] Android: Add VectorToJObjectArray utility function --- .../Android/jni/AndroidCommon/AndroidCommon.cpp | 13 +++---------- Source/Android/jni/AndroidCommon/AndroidCommon.h | 15 ++++++++++++++- Source/Android/jni/Cheats/ARCheat.cpp | 9 +-------- Source/Android/jni/Cheats/GeckoCheat.cpp | 9 +-------- Source/Android/jni/Cheats/GraphicsModGroup.cpp | 11 +++-------- Source/Android/jni/Cheats/PatchCheat.cpp | 9 +-------- 6 files changed, 23 insertions(+), 43 deletions(-) diff --git a/Source/Android/jni/AndroidCommon/AndroidCommon.cpp b/Source/Android/jni/AndroidCommon/AndroidCommon.cpp index 0a2667b1b6..53ef9174f4 100644 --- a/Source/Android/jni/AndroidCommon/AndroidCommon.cpp +++ b/Source/Android/jni/AndroidCommon/AndroidCommon.cpp @@ -20,7 +20,7 @@ std::string GetJString(JNIEnv* env, jstring jstr) const jchar* jchars = env->GetStringChars(jstr, nullptr); const jsize length = env->GetStringLength(jstr); const std::u16string_view string_view(reinterpret_cast(jchars), length); - const std::string converted_string = UTF16ToUTF8(string_view); + std::string converted_string = UTF16ToUTF8(string_view); env->ReleaseStringChars(jstr, jchars); return converted_string; } @@ -48,16 +48,9 @@ std::vector JStringArrayToVector(JNIEnv* env, jobjectArray array) return result; } -jobjectArray VectorToJStringArray(JNIEnv* env, std::vector vector) +jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector& vector) { - jobjectArray result = env->NewObjectArray(vector.size(), IDCache::GetStringClass(), nullptr); - for (jsize i = 0; i < vector.size(); ++i) - { - jstring str = ToJString(env, vector[i]); - env->SetObjectArrayElement(result, i, str); - env->DeleteLocalRef(str); - } - return result; + return VectorToJObjectArray(env, vector, IDCache::GetStringClass(), ToJString); } bool IsPathAndroidContent(const std::string& uri) diff --git a/Source/Android/jni/AndroidCommon/AndroidCommon.h b/Source/Android/jni/AndroidCommon/AndroidCommon.h index 5f1b4e1d51..bb300efc9f 100644 --- a/Source/Android/jni/AndroidCommon/AndroidCommon.h +++ b/Source/Android/jni/AndroidCommon/AndroidCommon.h @@ -13,7 +13,20 @@ std::string GetJString(JNIEnv* env, jstring jstr); jstring ToJString(JNIEnv* env, const std::string& str); std::vector JStringArrayToVector(JNIEnv* env, jobjectArray array); -jobjectArray VectorToJStringArray(JNIEnv* env, std::vector vector); +jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector& vector); + +template +jobjectArray VectorToJObjectArray(JNIEnv* env, const std::vector& vector, jclass clazz, F f) +{ + jobjectArray result = env->NewObjectArray(vector.size(), clazz, nullptr); + for (jsize i = 0; i < vector.size(); ++i) + { + jobject obj = f(env, vector[i]); + env->SetObjectArrayElement(result, i, obj); + env->DeleteLocalRef(obj); + } + return result; +} // Returns true if the given path should be opened as Android content instead of a normal file. bool IsPathAndroidContent(const std::string& uri); diff --git a/Source/Android/jni/Cheats/ARCheat.cpp b/Source/Android/jni/Cheats/ARCheat.cpp index 14da758539..3211e35c18 100644 --- a/Source/Android/jni/Cheats/ARCheat.cpp +++ b/Source/Android/jni/Cheats/ARCheat.cpp @@ -147,14 +147,7 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_ARCheat_loadCodes(JNIEnv* e const std::vector codes = ActionReplay::LoadCodes(game_ini_default, game_ini_local); - const jobjectArray array = - env->NewObjectArray(static_cast(codes.size()), IDCache::GetARCheatClass(), nullptr); - - jsize i = 0; - for (const ActionReplay::ARCode& code : codes) - env->SetObjectArrayElement(array, i++, ARCheatToJava(env, code)); - - return array; + return VectorToJObjectArray(env, codes, IDCache::GetARCheatClass(), ARCheatToJava); } JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_features_cheats_model_ARCheat_saveCodes( diff --git a/Source/Android/jni/Cheats/GeckoCheat.cpp b/Source/Android/jni/Cheats/GeckoCheat.cpp index 15038984c2..eb1df24947 100644 --- a/Source/Android/jni/Cheats/GeckoCheat.cpp +++ b/Source/Android/jni/Cheats/GeckoCheat.cpp @@ -155,14 +155,7 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_loadCodes(JNIEnv const std::vector codes = Gecko::LoadCodes(game_ini_default, game_ini_local); - const jobjectArray array = - env->NewObjectArray(static_cast(codes.size()), IDCache::GetGeckoCheatClass(), nullptr); - - jsize i = 0; - for (const Gecko::GeckoCode& code : codes) - env->SetObjectArrayElement(array, i++, GeckoCheatToJava(env, code)); - - return array; + return VectorToJObjectArray(env, codes, IDCache::GetGeckoCheatClass(), GeckoCheatToJava); } JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_saveCodes( diff --git a/Source/Android/jni/Cheats/GraphicsModGroup.cpp b/Source/Android/jni/Cheats/GraphicsModGroup.cpp index e7a9329b53..c21584f326 100644 --- a/Source/Android/jni/Cheats/GraphicsModGroup.cpp +++ b/Source/Android/jni/Cheats/GraphicsModGroup.cpp @@ -62,14 +62,9 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsModGroup_getMods(JN mods.push_back(&mod); } - const jobjectArray array = - env->NewObjectArray(static_cast(mods.size()), IDCache::GetGraphicsModClass(), nullptr); - - jsize i = 0; - for (GraphicsModConfig* mod : mods) - env->SetObjectArrayElement(array, i++, GraphicsModToJava(env, mod, obj)); - - return array; + return VectorToJObjectArray( + env, mods, IDCache::GetGraphicsModClass(), + [obj](JNIEnv* env, GraphicsModConfig* mod) { return GraphicsModToJava(env, mod, obj); }); } JNIEXPORT void JNICALL diff --git a/Source/Android/jni/Cheats/PatchCheat.cpp b/Source/Android/jni/Cheats/PatchCheat.cpp index 37a65b21aa..7958fbdf1c 100644 --- a/Source/Android/jni/Cheats/PatchCheat.cpp +++ b/Source/Android/jni/Cheats/PatchCheat.cpp @@ -134,14 +134,7 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_loadCodes(JNIEnv std::vector patches; PatchEngine::LoadPatchSection("OnFrame", &patches, game_ini_default, game_ini_local); - const jobjectArray array = env->NewObjectArray(static_cast(patches.size()), - IDCache::GetPatchCheatClass(), nullptr); - - jsize i = 0; - for (const PatchEngine::Patch& patch : patches) - env->SetObjectArrayElement(array, i++, PatchCheatToJava(env, patch)); - - return array; + return VectorToJObjectArray(env, patches, IDCache::GetPatchCheatClass(), PatchCheatToJava); } JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_saveCodes(