Android: Implement basic read-only cheats list

This commit is contained in:
JosJuice
2021-07-17 19:47:11 +02:00
parent ee3a5a4a81
commit 4d609c769f
19 changed files with 636 additions and 0 deletions

View File

@ -58,6 +58,18 @@ static jmethodID s_network_helper_get_network_gateway;
static jclass s_boolean_supplier_class;
static jmethodID s_boolean_supplier_get;
static jclass s_ar_cheat_class;
static jfieldID s_ar_cheat_pointer;
static jmethodID s_ar_cheat_constructor;
static jclass s_gecko_cheat_class;
static jfieldID s_gecko_cheat_pointer;
static jmethodID s_gecko_cheat_constructor;
static jclass s_patch_cheat_class;
static jfieldID s_patch_cheat_pointer;
static jmethodID s_patch_cheat_constructor;
namespace IDCache
{
JNIEnv* GetEnvForThread()
@ -268,6 +280,51 @@ jmethodID GetBooleanSupplierGet()
return s_boolean_supplier_get;
}
jclass GetARCheatClass()
{
return s_ar_cheat_class;
}
jfieldID GetARCheatPointer()
{
return s_ar_cheat_pointer;
}
jmethodID GetARCheatConstructor()
{
return s_ar_cheat_constructor;
}
jclass GetGeckoCheatClass()
{
return s_gecko_cheat_class;
}
jfieldID GetGeckoCheatPointer()
{
return s_gecko_cheat_pointer;
}
jmethodID GetGeckoCheatConstructor()
{
return s_gecko_cheat_constructor;
}
jclass GetPatchCheatClass()
{
return s_patch_cheat_class;
}
jfieldID GetPatchCheatPointer()
{
return s_patch_cheat_pointer;
}
jmethodID GetPatchCheatConstructor()
{
return s_patch_cheat_constructor;
}
} // namespace IDCache
extern "C" {
@ -376,6 +433,27 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
s_boolean_supplier_get = env->GetMethodID(s_boolean_supplier_class, "get", "()Z");
env->DeleteLocalRef(boolean_supplier_class);
const jclass ar_cheat_class =
env->FindClass("org/dolphinemu/dolphinemu/features/cheats/model/ARCheat");
s_ar_cheat_class = reinterpret_cast<jclass>(env->NewGlobalRef(ar_cheat_class));
s_ar_cheat_pointer = env->GetFieldID(ar_cheat_class, "mPointer", "J");
s_ar_cheat_constructor = env->GetMethodID(ar_cheat_class, "<init>", "(J)V");
env->DeleteLocalRef(ar_cheat_class);
const jclass gecko_cheat_class =
env->FindClass("org/dolphinemu/dolphinemu/features/cheats/model/GeckoCheat");
s_gecko_cheat_class = reinterpret_cast<jclass>(env->NewGlobalRef(gecko_cheat_class));
s_gecko_cheat_pointer = env->GetFieldID(gecko_cheat_class, "mPointer", "J");
s_gecko_cheat_constructor = env->GetMethodID(gecko_cheat_class, "<init>", "(J)V");
env->DeleteLocalRef(gecko_cheat_class);
const jclass patch_cheat_class =
env->FindClass("org/dolphinemu/dolphinemu/features/cheats/model/PatchCheat");
s_patch_cheat_class = reinterpret_cast<jclass>(env->NewGlobalRef(patch_cheat_class));
s_patch_cheat_pointer = env->GetFieldID(patch_cheat_class, "mPointer", "J");
s_patch_cheat_constructor = env->GetMethodID(patch_cheat_class, "<init>", "(J)V");
env->DeleteLocalRef(patch_cheat_class);
return JNI_VERSION;
}
@ -396,5 +474,8 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved)
env->DeleteGlobalRef(s_content_handler_class);
env->DeleteGlobalRef(s_network_helper_class);
env->DeleteGlobalRef(s_boolean_supplier_class);
env->DeleteGlobalRef(s_ar_cheat_class);
env->DeleteGlobalRef(s_gecko_cheat_class);
env->DeleteGlobalRef(s_patch_cheat_class);
}
}

View File

@ -57,4 +57,16 @@ jmethodID GetNetworkHelperGetNetworkGateway();
jmethodID GetBooleanSupplierGet();
jclass GetARCheatClass();
jfieldID GetARCheatPointer();
jmethodID GetARCheatConstructor();
jclass GetGeckoCheatClass();
jfieldID GetGeckoCheatPointer();
jmethodID GetGeckoCheatConstructor();
jclass GetPatchCheatClass();
jfieldID GetPatchCheatPointer();
jmethodID GetPatchCheatConstructor();
} // namespace IDCache

View File

@ -1,4 +1,7 @@
add_library(main SHARED
Cheats/ARCheat.cpp
Cheats/GeckoCheat.cpp
Cheats/PatchCheat.cpp
Config/NativeConfig.cpp
Config/PostProcessing.cpp
GameList/GameFile.cpp

View File

@ -0,0 +1,67 @@
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <string>
#include <vector>
#include <jni.h>
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Core/ActionReplay.h"
#include "Core/ConfigManager.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"
static ActionReplay::ARCode* GetPointer(JNIEnv* env, jobject obj)
{
return reinterpret_cast<ActionReplay::ARCode*>(
env->GetLongField(obj, IDCache::GetARCheatPointer()));
}
jobject ARCheatToJava(JNIEnv* env, const ActionReplay::ARCode& code)
{
return env->NewObject(IDCache::GetARCheatClass(), IDCache::GetARCheatConstructor(),
reinterpret_cast<jlong>(new ActionReplay::ARCode(code)));
}
extern "C" {
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_ARCheat_finalize(JNIEnv* env, jobject obj)
{
delete GetPointer(env, obj);
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_ARCheat_getName(JNIEnv* env, jobject obj)
{
return ToJString(env, GetPointer(env, obj)->name);
}
JNIEXPORT jobjectArray JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_ARCheat_loadCodes(JNIEnv* env, jclass,
jstring jGameID,
jint revision)
{
const std::string game_id = GetJString(env, jGameID);
IniFile game_ini_local;
// We don't use LoadLocalGameIni() here because user cheat codes that are installed via the UI
// will always be stored in GS/${GAMEID}.ini
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + game_id + ".ini");
const IniFile game_ini_default = SConfig::LoadDefaultGameIni(game_id, revision);
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;
}
}

View File

@ -0,0 +1,67 @@
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <string>
#include <vector>
#include <jni.h>
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Core/ConfigManager.h"
#include "Core/GeckoCode.h"
#include "Core/GeckoCodeConfig.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"
static Gecko::GeckoCode* GetPointer(JNIEnv* env, jobject obj)
{
return reinterpret_cast<Gecko::GeckoCode*>(
env->GetLongField(obj, IDCache::GetGeckoCheatPointer()));
}
jobject GeckoCheatToJava(JNIEnv* env, const Gecko::GeckoCode& code)
{
return env->NewObject(IDCache::GetGeckoCheatClass(), IDCache::GetGeckoCheatConstructor(),
reinterpret_cast<jlong>(new Gecko::GeckoCode(code)));
}
extern "C" {
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_finalize(JNIEnv* env, jobject obj)
{
delete GetPointer(env, obj);
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_getName(JNIEnv* env, jobject obj)
{
return ToJString(env, GetPointer(env, obj)->name);
}
JNIEXPORT jobjectArray JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_loadCodes(JNIEnv* env, jclass,
jstring jGameID,
jint revision)
{
const std::string game_id = GetJString(env, jGameID);
IniFile game_ini_local;
// We don't use LoadLocalGameIni() here because user cheat codes that are installed via the UI
// will always be stored in GS/${GAMEID}.ini
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + game_id + ".ini");
const IniFile game_ini_default = SConfig::LoadDefaultGameIni(game_id, revision);
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;
}
}

View File

@ -0,0 +1,67 @@
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <string>
#include <vector>
#include <jni.h>
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Core/ConfigManager.h"
#include "Core/PatchEngine.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"
static PatchEngine::Patch* GetPointer(JNIEnv* env, jobject obj)
{
return reinterpret_cast<PatchEngine::Patch*>(
env->GetLongField(obj, IDCache::GetPatchCheatPointer()));
}
jobject PatchCheatToJava(JNIEnv* env, const PatchEngine::Patch& patch)
{
return env->NewObject(IDCache::GetPatchCheatClass(), IDCache::GetPatchCheatConstructor(),
reinterpret_cast<jlong>(new PatchEngine::Patch(patch)));
}
extern "C" {
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_finalize(JNIEnv* env, jobject obj)
{
delete GetPointer(env, obj);
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_getName(JNIEnv* env, jobject obj)
{
return ToJString(env, GetPointer(env, obj)->name);
}
JNIEXPORT jobjectArray JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_loadCodes(JNIEnv* env, jclass,
jstring jGameID,
jint revision)
{
const std::string game_id = GetJString(env, jGameID);
IniFile game_ini_local;
// We don't use LoadLocalGameIni() here because user cheat codes that are installed via the UI
// will always be stored in GS/${GAMEID}.ini
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + game_id + ".ini");
const IniFile game_ini_default = SConfig::LoadDefaultGameIni(game_id, revision);
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;
}
}