Android: Expose a proper interface for C++ IniFile class

Replaces the inflexible INI functions in NativeLibrary.
This commit is contained in:
JosJuice
2020-07-06 16:57:49 +02:00
parent c8c4ec28ce
commit 74f197caed
17 changed files with 360 additions and 249 deletions

View File

@ -30,6 +30,12 @@ static jclass s_linked_hash_map_class;
static jmethodID s_linked_hash_map_init;
static jmethodID s_linked_hash_map_put;
static jclass s_ini_file_class;
static jfieldID s_ini_file_pointer;
static jclass s_ini_file_section_class;
static jfieldID s_ini_file_section_pointer;
static jmethodID s_ini_file_section_constructor;
namespace IDCache
{
JNIEnv* GetEnvForThread()
@ -89,6 +95,7 @@ jmethodID GetAnalyticsValue()
{
return s_get_analytics_value;
}
jclass GetGameFileClass()
{
return s_game_file_class;
@ -129,6 +136,31 @@ jmethodID GetLinkedHashMapPut()
return s_linked_hash_map_put;
}
jclass GetIniFileClass()
{
return s_ini_file_class;
}
jfieldID GetIniFilePointer()
{
return s_ini_file_pointer;
}
jclass GetIniFileSectionClass()
{
return s_ini_file_section_class;
}
jfieldID GetIniFileSectionPointer()
{
return s_ini_file_section_pointer;
}
jmethodID GetIniFileSectionConstructor()
{
return s_ini_file_section_constructor;
}
} // namespace IDCache
#ifdef __cplusplus
@ -150,16 +182,19 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved)
s_do_rumble = env->GetStaticMethodID(s_native_library_class, "rumble", "(ID)V");
s_get_update_touch_pointer =
env->GetStaticMethodID(s_native_library_class, "updateTouchPointer", "()V");
env->DeleteLocalRef(native_library_class);
const jclass game_file_class = env->FindClass("org/dolphinemu/dolphinemu/model/GameFile");
s_game_file_class = reinterpret_cast<jclass>(env->NewGlobalRef(game_file_class));
s_game_file_pointer = env->GetFieldID(game_file_class, "mPointer", "J");
s_game_file_constructor = env->GetMethodID(game_file_class, "<init>", "(J)V");
env->DeleteLocalRef(game_file_class);
const jclass game_file_cache_class =
env->FindClass("org/dolphinemu/dolphinemu/model/GameFileCache");
s_game_file_cache_class = reinterpret_cast<jclass>(env->NewGlobalRef(game_file_cache_class));
s_game_file_cache_pointer = env->GetFieldID(game_file_cache_class, "mPointer", "J");
env->DeleteLocalRef(game_file_cache_class);
const jclass analytics_class = env->FindClass("org/dolphinemu/dolphinemu/utils/Analytics");
s_analytics_class = reinterpret_cast<jclass>(env->NewGlobalRef(analytics_class));
@ -167,6 +202,20 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved)
env->GetStaticMethodID(s_analytics_class, "sendReport", "(Ljava/lang/String;[B)V");
s_get_analytics_value = env->GetStaticMethodID(s_analytics_class, "getValue",
"(Ljava/lang/String;)Ljava/lang/String;");
env->DeleteLocalRef(analytics_class);
const jclass ini_file_class = env->FindClass("org/dolphinemu/dolphinemu/utils/IniFile");
s_ini_file_class = reinterpret_cast<jclass>(env->NewGlobalRef(ini_file_class));
s_ini_file_pointer = env->GetFieldID(ini_file_class, "mPointer", "J");
env->DeleteLocalRef(ini_file_class);
const jclass ini_file_section_class =
env->FindClass("org/dolphinemu/dolphinemu/utils/IniFile$Section");
s_ini_file_section_class = reinterpret_cast<jclass>(env->NewGlobalRef(ini_file_section_class));
s_ini_file_section_pointer = env->GetFieldID(ini_file_section_class, "mPointer", "J");
s_ini_file_section_constructor = env->GetMethodID(
ini_file_section_class, "<init>", "(Lorg/dolphinemu/dolphinemu/utils/IniFile;J)V");
env->DeleteLocalRef(ini_file_section_class);
const jclass map_class = env->FindClass("java/util/LinkedHashMap");
s_linked_hash_map_class = reinterpret_cast<jclass>(env->NewGlobalRef(map_class));
@ -188,6 +237,8 @@ void JNI_OnUnload(JavaVM* vm, void* reserved)
env->DeleteGlobalRef(s_game_file_cache_class);
env->DeleteGlobalRef(s_analytics_class);
env->DeleteGlobalRef(s_linked_hash_map_class);
env->DeleteGlobalRef(s_ini_file_class);
env->DeleteGlobalRef(s_ini_file_section_class);
}
#ifdef __cplusplus

View File

@ -32,4 +32,10 @@ jclass GetLinkedHashMapClass();
jmethodID GetLinkedHashMapInit();
jmethodID GetLinkedHashMapPut();
jclass GetIniFileClass();
jfieldID GetIniFilePointer();
jclass GetIniFileSectionClass();
jfieldID GetIniFileSectionPointer();
jmethodID GetIniFileSectionConstructor();
} // namespace IDCache

View File

@ -3,6 +3,7 @@ add_library(main SHARED
AndroidCommon/IDCache.cpp
GameList/GameFile.cpp
GameList/GameFileCache.cpp
IniFile.cpp
MainAndroid.cpp
)

View File

@ -0,0 +1,121 @@
// Copyright 2020 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <jni.h>
#include "Common/IniFile.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"
static IniFile::Section* GetSectionPointer(JNIEnv* env, jobject obj)
{
return reinterpret_cast<IniFile::Section*>(
env->GetLongField(obj, IDCache::GetIniFileSectionPointer()));
}
static IniFile* GetIniFilePointer(JNIEnv* env, jobject obj)
{
return reinterpret_cast<IniFile*>(env->GetLongField(obj, IDCache::GetIniFilePointer()));
}
static jobject SectionToJava(JNIEnv* env, jobject ini_file, IniFile::Section* section)
{
if (!section)
return nullptr;
return env->NewObject(IDCache::GetIniFileSectionClass(), IDCache::GetIniFileSectionConstructor(),
ini_file, reinterpret_cast<jlong>(section));
}
template <typename T>
static T Get(JNIEnv* env, jobject obj, jstring section_name, jstring key, T default_value)
{
T result;
GetIniFilePointer(env, obj)
->GetOrCreateSection(GetJString(env, section_name))
->Get(GetJString(env, key), &result, default_value);
return result;
}
template <typename T>
static void Set(JNIEnv* env, jobject obj, jstring section_name, jstring key, T new_value)
{
GetIniFilePointer(env, obj)
->GetOrCreateSection(GetJString(env, section_name))
->Set(GetJString(env, key), new_value);
}
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_load(
JNIEnv* env, jobject obj, jstring path, jboolean keep_current_data)
{
return static_cast<jboolean>(
GetIniFilePointer(env, obj)->Load(GetJString(env, path), keep_current_data));
}
JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_save(JNIEnv* env,
jobject obj,
jstring path)
{
return static_cast<jboolean>(GetIniFilePointer(env, obj)->Save(GetJString(env, path)));
}
JNIEXPORT jstring JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_getString(
JNIEnv* env, jobject obj, jstring section_name, jstring key, jstring default_value)
{
return ToJString(env, Get(env, obj, section_name, key, GetJString(env, default_value)));
}
JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_getBoolean(
JNIEnv* env, jobject obj, jstring section_name, jstring key, jboolean default_value)
{
return static_cast<jboolean>(Get(env, obj, section_name, key, static_cast<bool>(default_value)));
}
JNIEXPORT jint JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_getInt(JNIEnv* env, jobject obj,
jstring section_name,
jstring key,
jint default_value)
{
return Get(env, obj, section_name, key, default_value);
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_setString(
JNIEnv* env, jobject obj, jstring section_name, jstring key, jstring new_value)
{
Set(env, obj, section_name, key, GetJString(env, new_value));
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_setBoolean(
JNIEnv* env, jobject obj, jstring section_name, jstring key, jboolean new_value)
{
Set(env, obj, section_name, key, static_cast<bool>(new_value));
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_setInt(JNIEnv* env, jobject obj,
jstring section_name,
jstring key,
jint new_value)
{
Set(env, obj, section_name, key, new_value);
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_finalize(JNIEnv* env,
jobject obj)
{
delete GetIniFilePointer(env, obj);
}
JNIEXPORT jlong JNICALL Java_org_dolphinemu_dolphinemu_utils_IniFile_newIniFile(JNIEnv* env,
jobject obj)
{
return reinterpret_cast<jlong>(new IniFile);
}
#ifdef __cplusplus
}
#endif

View File

@ -213,10 +213,6 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveScreenSh
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_eglBindAPI(JNIEnv* env,
jobject obj,
jint api);
JNIEXPORT jstring JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GetConfig(
JNIEnv* env, jobject obj, jstring jFile, jstring jSection, jstring jKey, jstring jDefault);
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetConfig(
JNIEnv* env, jobject obj, jstring jFile, jstring jSection, jstring jKey, jstring jValue);
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetFilename(JNIEnv* env,
jobject obj,
jstring jFile);
@ -355,129 +351,6 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_eglBindAPI(J
eglBindAPI(api);
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_InitGameIni(JNIEnv* env,
jobject obj,
jstring jGameID)
{
// Initialize an empty INI file
IniFile ini;
std::string gameid = GetJString(env, jGameID);
__android_log_print(ANDROID_LOG_DEBUG, "InitGameIni", "Initializing base game config file");
ini.Save(File::GetUserPath(D_GAMESETTINGS_IDX) + gameid + ".ini");
}
JNIEXPORT jstring JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GetUserSetting(
JNIEnv* env, jobject obj, jstring jGameID, jstring jSection, jstring jKey)
{
IniFile ini;
std::string gameid = GetJString(env, jGameID);
std::string section = GetJString(env, jSection);
std::string key = GetJString(env, jKey);
ini = SConfig::LoadGameIni(gameid, 0);
std::string value;
ini.GetOrCreateSection(section)->Get(key, &value, "-1");
return ToJString(env, value);
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_NewGameIniFile(JNIEnv* env,
jobject obj)
{
s_ini = IniFile();
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_LoadGameIniFile(JNIEnv* env,
jobject obj,
jstring jGameID)
{
std::string gameid = GetJString(env, jGameID);
s_ini.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + gameid + ".ini");
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveGameIniFile(JNIEnv* env,
jobject obj,
jstring jGameID)
{
std::string gameid = GetJString(env, jGameID);
s_ini.Save(File::GetUserPath(D_GAMESETTINGS_IDX) + gameid + ".ini");
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetUserSetting(
JNIEnv* env, jobject obj, jstring jGameID, jstring jSection, jstring jKey, jstring jValue)
{
std::string gameid = GetJString(env, jGameID);
std::string section = GetJString(env, jSection);
std::string key = GetJString(env, jKey);
std::string val = GetJString(env, jValue);
if (val != "-1")
{
s_ini.GetOrCreateSection(section)->Set(key, val);
}
else
{
s_ini.GetOrCreateSection(section)->Delete(key);
}
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetProfileSetting(
JNIEnv* env, jobject obj, jstring jProfile, jstring jSection, jstring jKey, jstring jValue)
{
IniFile ini;
std::string profile = GetJString(env, jProfile);
std::string section = GetJString(env, jSection);
std::string key = GetJString(env, jKey);
std::string val = GetJString(env, jValue);
ini.Load(File::GetUserPath(D_CONFIG_IDX) + "Profiles/Wiimote/" + profile + ".ini");
if (val != "-1")
{
ini.GetOrCreateSection(section)->Set(key, val);
}
else
{
ini.GetOrCreateSection(section)->Delete(key);
}
ini.Save(File::GetUserPath(D_CONFIG_IDX) + "Profiles/Wiimote/" + profile + ".ini");
}
JNIEXPORT jstring JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GetConfig(
JNIEnv* env, jobject obj, jstring jFile, jstring jSection, jstring jKey, jstring jDefault)
{
IniFile ini;
std::string file = GetJString(env, jFile);
std::string section = GetJString(env, jSection);
std::string key = GetJString(env, jKey);
std::string defaultValue = GetJString(env, jDefault);
ini.Load(File::GetUserPath(D_CONFIG_IDX) + std::string(file));
std::string value;
ini.GetOrCreateSection(section)->Get(key, &value, defaultValue);
return ToJString(env, value);
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetConfig(
JNIEnv* env, jobject obj, jstring jFile, jstring jSection, jstring jKey, jstring jValue)
{
IniFile ini;
std::string file = GetJString(env, jFile);
std::string section = GetJString(env, jSection);
std::string key = GetJString(env, jKey);
std::string value = GetJString(env, jValue);
ini.Load(File::GetUserPath(D_CONFIG_IDX) + std::string(file));
ini.GetOrCreateSection(section)->Set(key, value);
ini.Save(File::GetUserPath(D_CONFIG_IDX) + std::string(file));
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveState(JNIEnv* env,
jobject obj,
jint slot,