cheats: Use global cheat engine (#7291)

* cheats: Use global cheat engine

* cheats: Prevent wasted double-load of cheat file.

* android: Fix for cheat engine updates.

---------

Co-authored-by: GPUCode <geoster3d@gmail.com>
This commit is contained in:
Steveice10 2024-01-01 12:49:08 -08:00 committed by GitHub
parent 5a7f615da1
commit 7dd9174d31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 120 additions and 125 deletions

View file

@ -7,25 +7,13 @@ package org.citra.citra_emu.features.cheats.model
import androidx.annotation.Keep
@Keep
class CheatEngine(titleId: Long) {
@Keep
private val mPointer: Long
init {
mPointer = initialize(titleId)
}
protected external fun finalize()
object CheatEngine {
external fun loadCheatFile(titleId: Long)
external fun saveCheatFile(titleId: Long)
external fun getCheats(): Array<Cheat>
external fun addCheat(cheat: Cheat?)
external fun removeCheat(index: Int)
external fun updateCheat(index: Int, newCheat: Cheat?)
external fun saveCheatFile()
companion object {
@JvmStatic
private external fun initialize(titleId: Long): Long
}
}

View file

@ -47,18 +47,19 @@ class CheatsViewModel : ViewModel() {
val detailsViewFocusChange get() = _detailsViewFocusChange.asStateFlow()
private val _detailsViewFocusChange = MutableStateFlow(false)
private var cheatEngine: CheatEngine? = null
private var titleId: Long = 0
lateinit var cheats: Array<Cheat>
private var cheatsNeedSaving = false
private var selectedCheatPosition = -1
fun initialize(titleId: Long) {
cheatEngine = CheatEngine(titleId)
fun initialize(titleId_: Long) {
titleId = titleId_;
load()
}
private fun load() {
cheats = cheatEngine!!.getCheats()
CheatEngine.loadCheatFile(titleId)
cheats = CheatEngine.getCheats()
for (i in cheats.indices) {
cheats[i].setEnabledChangedCallback {
cheatsNeedSaving = true
@ -69,7 +70,7 @@ class CheatsViewModel : ViewModel() {
fun saveIfNeeded() {
if (cheatsNeedSaving) {
cheatEngine!!.saveCheatFile()
CheatEngine.saveCheatFile(titleId)
cheatsNeedSaving = false
}
}
@ -107,7 +108,7 @@ class CheatsViewModel : ViewModel() {
_isAdding.value = false
_isEditing.value = false
val position = cheats.size
cheatEngine!!.addCheat(cheat)
CheatEngine.addCheat(cheat)
cheatsNeedSaving = true
load()
notifyCheatAdded(position)
@ -123,7 +124,7 @@ class CheatsViewModel : ViewModel() {
}
fun updateSelectedCheat(newCheat: Cheat?) {
cheatEngine!!.updateCheat(selectedCheatPosition, newCheat)
CheatEngine.updateCheat(selectedCheatPosition, newCheat)
cheatsNeedSaving = true
load()
notifyCheatUpdated(selectedCheatPosition)
@ -141,7 +142,7 @@ class CheatsViewModel : ViewModel() {
fun deleteSelectedCheat() {
val position = selectedCheatPosition
setSelectedCheat(null, -1)
cheatEngine!!.removeCheat(position)
CheatEngine.removeCheat(position)
cheatsNeedSaving = true
load()
notifyCheatDeleted(position)

View file

@ -15,24 +15,24 @@
extern "C" {
static Cheats::CheatEngine* GetPointer(JNIEnv* env, jobject obj) {
return reinterpret_cast<Cheats::CheatEngine*>(
env->GetLongField(obj, IDCache::GetCheatEnginePointer()));
static Cheats::CheatEngine& GetEngine() {
Core::System& system{Core::System::GetInstance()};
return system.CheatEngine();
}
JNIEXPORT jlong JNICALL Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_initialize(
JNIEXPORT void JNICALL Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_loadCheatFile(
JNIEnv* env, jclass, jlong title_id) {
return reinterpret_cast<jlong>(new Cheats::CheatEngine(title_id, Core::System::GetInstance()));
GetEngine().LoadCheatFile(title_id);
}
JNIEXPORT void JNICALL
Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_finalize(JNIEnv* env, jobject obj) {
delete GetPointer(env, obj);
JNIEXPORT void JNICALL Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_saveCheatFile(
JNIEnv* env, jclass, jlong title_id) {
GetEngine().SaveCheatFile(title_id);
}
JNIEXPORT jobjectArray JNICALL
Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_getCheats(JNIEnv* env, jobject obj) {
auto cheats = GetPointer(env, obj)->GetCheats();
Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_getCheats(JNIEnv* env, jclass) {
auto cheats = GetEngine().GetCheats();
const jobjectArray array =
env->NewObjectArray(static_cast<jsize>(cheats.size()), IDCache::GetCheatClass(), nullptr);
@ -45,22 +45,19 @@ Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_getCheats(JNIEnv* en
}
JNIEXPORT void JNICALL Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_addCheat(
JNIEnv* env, jobject obj, jobject j_cheat) {
GetPointer(env, obj)->AddCheat(*CheatFromJava(env, j_cheat));
JNIEnv* env, jclass, jobject j_cheat) {
auto cheat = *CheatFromJava(env, j_cheat);
GetEngine().AddCheat(std::move(cheat));
}
JNIEXPORT void JNICALL Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_removeCheat(
JNIEnv* env, jobject obj, jint index) {
GetPointer(env, obj)->RemoveCheat(index);
JNIEnv* env, jclass, jint index) {
GetEngine().RemoveCheat(index);
}
JNIEXPORT void JNICALL Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_updateCheat(
JNIEnv* env, jobject obj, jint index, jobject j_new_cheat) {
GetPointer(env, obj)->UpdateCheat(index, *CheatFromJava(env, j_new_cheat));
}
JNIEXPORT void JNICALL Java_org_citra_citra_1emu_features_cheats_model_CheatEngine_saveCheatFile(
JNIEnv* env, jobject obj) {
GetPointer(env, obj)->SaveCheatFile();
JNIEnv* env, jclass, jint index, jobject j_new_cheat) {
auto cheat = *CheatFromJava(env, j_new_cheat);
GetEngine().UpdateCheat(index, std::move(cheat));
}
}

View file

@ -35,8 +35,6 @@ static jclass s_cheat_class;
static jfieldID s_cheat_pointer;
static jmethodID s_cheat_constructor;
static jfieldID s_cheat_engine_pointer;
static jfieldID s_game_info_pointer;
static jclass s_disk_cache_progress_class;
@ -116,10 +114,6 @@ jmethodID GetCheatConstructor() {
return s_cheat_constructor;
}
jfieldID GetCheatEnginePointer() {
return s_cheat_engine_pointer;
}
jfieldID GetGameInfoPointer() {
return s_game_info_pointer;
}
@ -195,12 +189,6 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
s_cheat_constructor = env->GetMethodID(cheat_class, "<init>", "(J)V");
env->DeleteLocalRef(cheat_class);
// Initialize CheatEngine
const jclass cheat_engine_class =
env->FindClass("org/citra/citra_emu/features/cheats/model/CheatEngine");
s_cheat_engine_pointer = env->GetFieldID(cheat_engine_class, "mPointer", "J");
env->DeleteLocalRef(cheat_engine_class);
// Initialize GameInfo
const jclass game_info_class = env->FindClass("org/citra/citra_emu/model/GameInfo");
s_game_info_pointer = env->GetFieldID(game_info_class, "pointer", "J");

View file

@ -35,8 +35,6 @@ jclass GetCheatClass();
jfieldID GetCheatPointer();
jmethodID GetCheatConstructor();
jfieldID GetCheatEnginePointer();
jfieldID GetGameInfoPointer();
jclass GetDiskCacheProgressClass();