Android: Add VectorToJObjectArray utility function

This commit is contained in:
JosJuice 2022-12-27 22:28:06 +01:00
parent 0b9351c194
commit b3a368ae06
6 changed files with 23 additions and 43 deletions

View File

@ -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<const char16_t*>(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<std::string> JStringArrayToVector(JNIEnv* env, jobjectArray array)
return result;
}
jobjectArray VectorToJStringArray(JNIEnv* env, std::vector<std::string> vector)
jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector<std::string>& 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)

View File

@ -13,7 +13,20 @@ std::string GetJString(JNIEnv* env, jstring jstr);
jstring ToJString(JNIEnv* env, const std::string& str);
std::vector<std::string> JStringArrayToVector(JNIEnv* env, jobjectArray array);
jobjectArray VectorToJStringArray(JNIEnv* env, std::vector<std::string> vector);
jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector<std::string>& vector);
template <typename T, typename F>
jobjectArray VectorToJObjectArray(JNIEnv* env, const std::vector<T>& 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);

View File

@ -147,14 +147,7 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_ARCheat_loadCodes(JNIEnv* e
const std::vector<ActionReplay::ARCode> codes =
ActionReplay::LoadCodes(game_ini_default, game_ini_local);
const jobjectArray array =
env->NewObjectArray(static_cast<jsize>(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(

View File

@ -155,14 +155,7 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_loadCodes(JNIEnv
const std::vector<Gecko::GeckoCode> codes = Gecko::LoadCodes(game_ini_default, game_ini_local);
const jobjectArray array =
env->NewObjectArray(static_cast<jsize>(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(

View File

@ -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<jsize>(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

View File

@ -134,14 +134,7 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_loadCodes(JNIEnv
std::vector<PatchEngine::Patch> patches;
PatchEngine::LoadPatchSection("OnFrame", &patches, game_ini_default, game_ini_local);
const jobjectArray array = env->NewObjectArray(static_cast<jsize>(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(