Prevent invalid cheats from being enabled and used

This commit is contained in:
Rafael Caetano 2021-02-11 23:00:38 +00:00
parent d360611943
commit 766098b619
3 changed files with 19 additions and 3 deletions

View File

@ -9,6 +9,8 @@
#include <InputAndroid.h>
#include <android/asset_manager_jni.h>
#define MAX_CHEAT_SIZE (2*64)
MelonDSAndroid::EmulatorConfiguration buildEmulatorConfiguration(JNIEnv* env, jobject emulatorConfiguration);
GPU::RenderSettings buildRenderSettings(JNIEnv* env, jobject renderSettings);
void* emulate(void*);
@ -73,6 +75,11 @@ Java_me_magnum_melonds_MelonEmulator_setupCheats(JNIEnv* env, jclass type, jobje
unsigned long section = strtoul(sectionString.c_str(), &endPointer, 16);
if (*endPointer == 0) {
if (sectionCounter >= MAX_CHEAT_SIZE) {
isBad = true;
break;
}
internalCheat.code[sectionCounter] = (u32) section;
sectionCounter++;
} else {
@ -90,7 +97,7 @@ Java_me_magnum_melonds_MelonEmulator_setupCheats(JNIEnv* env, jclass type, jobje
isBad = true;
} else {
unsigned long section = strtoul(sectionString.c_str(), &endPointer, 16);
if (*endPointer == 0) {
if (*endPointer == 0 && sectionCounter < MAX_CHEAT_SIZE) {
internalCheat.code[sectionCounter] = (u32) section;
sectionCounter++;
} else {

View File

@ -1,3 +1,9 @@
package me.magnum.melonds.domain.model
data class Cheat(val id: Long?, val name: String, val description: String?, val code: String, val enabled: Boolean)
data class Cheat(val id: Long?, val name: String, val description: String?, val code: String, val enabled: Boolean) {
fun isValid(): Boolean {
// A cheat code can only have 128 parts (512 bytes). Since each part has 8 characters, we can add 1 to the length (to ensure that each part has a matching space
// separator) and divide by 9 (part length + space separator) to calculate the total number of parts in the cheat
return ((code.length + 1) / 9) < 128
}
}

View File

@ -6,6 +6,7 @@ import androidx.core.view.isGone
import androidx.recyclerview.widget.RecyclerView
import me.magnum.melonds.databinding.ItemCheatsCheatBinding
import me.magnum.melonds.domain.model.Cheat
import me.magnum.melonds.extensions.setViewEnabledRecursive
class CheatsSubScreenFragment : SubScreenFragment() {
override fun getSubScreenAdapter(): RecyclerView.Adapter<*> {
@ -27,10 +28,12 @@ class CheatsSubScreenFragment : SubScreenFragment() {
fun setCheat(cheat: Cheat) {
this.cheat = cheat
val isCheatValid = cheat.isValid()
binding.root.setViewEnabledRecursive(isCheatValid)
binding.textCheatName.text = cheat.name
binding.textCheatDescription.isGone = cheat.description.isNullOrEmpty()
binding.textCheatDescription.text = cheat.description
binding.checkboxCheatEnabled.isChecked = cheat.enabled
binding.checkboxCheatEnabled.isChecked = isCheatValid && cheat.enabled
}
fun toggle() {