2021-07-17 17:47:11 +00:00
|
|
|
// 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"
|
2021-08-07 14:08:07 +00:00
|
|
|
#include "jni/Cheats/Cheats.h"
|
2021-07-17 17:47:11 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:51:32 +00:00
|
|
|
JNIEXPORT jlong JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_createNew(JNIEnv* env, jobject obj)
|
|
|
|
{
|
|
|
|
auto* patch = new PatchEngine::Patch;
|
|
|
|
patch->user_defined = true;
|
|
|
|
return reinterpret_cast<jlong>(patch);
|
|
|
|
}
|
|
|
|
|
2021-07-17 17:47:11 +00:00
|
|
|
JNIEXPORT jstring JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_getName(JNIEnv* env, jobject obj)
|
|
|
|
{
|
|
|
|
return ToJString(env, GetPointer(env, obj)->name);
|
|
|
|
}
|
|
|
|
|
2021-08-09 13:24:32 +00:00
|
|
|
JNIEXPORT jstring JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_getCode(JNIEnv* env, jobject obj)
|
|
|
|
{
|
|
|
|
PatchEngine::Patch* patch = GetPointer(env, obj);
|
|
|
|
|
|
|
|
std::string code_string;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < patch->entries.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
code_string += '\n';
|
|
|
|
|
|
|
|
code_string += PatchEngine::SerializeLine(patch->entries[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToJString(env, code_string);
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:08:07 +00:00
|
|
|
JNIEXPORT jboolean JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_getUserDefined(JNIEnv* env,
|
|
|
|
jobject obj)
|
|
|
|
{
|
|
|
|
return static_cast<jboolean>(GetPointer(env, obj)->user_defined);
|
|
|
|
}
|
|
|
|
|
2021-07-18 16:54:01 +00:00
|
|
|
JNIEXPORT jboolean JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_getEnabled(JNIEnv* env, jobject obj)
|
|
|
|
{
|
|
|
|
return static_cast<jboolean>(GetPointer(env, obj)->enabled);
|
|
|
|
}
|
|
|
|
|
2023-03-01 18:33:16 +00:00
|
|
|
JNIEXPORT jint JNICALL Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_setCheatImpl(
|
2021-08-09 17:54:52 +00:00
|
|
|
JNIEnv* env, jobject obj, jstring name, jstring creator, jstring notes, jstring code_string)
|
2021-08-07 14:08:07 +00:00
|
|
|
{
|
|
|
|
PatchEngine::Patch* patch = GetPointer(env, obj);
|
2021-08-09 13:24:32 +00:00
|
|
|
|
|
|
|
std::vector<PatchEngine::PatchEntry> entries;
|
|
|
|
|
|
|
|
std::vector<std::string> lines = SplitString(GetJString(env, code_string), '\n');
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lines.size(); i++)
|
|
|
|
{
|
|
|
|
const std::string& line = lines[i];
|
|
|
|
|
|
|
|
if (line.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (std::optional<PatchEngine::PatchEntry> entry = PatchEngine::DeserializeLine(line))
|
|
|
|
entries.emplace_back(*std::move(entry));
|
|
|
|
else
|
|
|
|
return i + 1; // Parse error on line i
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entries.empty())
|
|
|
|
return Cheats::TRY_SET_FAIL_NO_CODE_LINES;
|
|
|
|
|
2021-08-07 14:08:07 +00:00
|
|
|
patch->name = GetJString(env, name);
|
2021-08-09 13:24:32 +00:00
|
|
|
patch->entries = std::move(entries);
|
2021-08-07 14:08:07 +00:00
|
|
|
|
2021-08-09 13:24:32 +00:00
|
|
|
return Cheats::TRY_SET_SUCCESS;
|
2021-08-07 14:08:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 16:54:01 +00:00
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_setEnabledImpl(JNIEnv* env,
|
|
|
|
jobject obj,
|
|
|
|
jboolean enabled)
|
|
|
|
{
|
|
|
|
GetPointer(env, obj)->enabled = static_cast<bool>(enabled);
|
|
|
|
}
|
|
|
|
|
2021-07-17 17:47:11 +00:00
|
|
|
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);
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile game_ini_local;
|
2021-07-17 17:47:11 +00:00
|
|
|
|
|
|
|
// 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");
|
2023-04-13 13:38:09 +00:00
|
|
|
const Common::IniFile game_ini_default = SConfig::LoadDefaultGameIni(game_id, revision);
|
2021-07-17 17:47:11 +00:00
|
|
|
|
|
|
|
std::vector<PatchEngine::Patch> patches;
|
|
|
|
PatchEngine::LoadPatchSection("OnFrame", &patches, game_ini_default, game_ini_local);
|
|
|
|
|
2022-12-27 21:28:06 +00:00
|
|
|
return VectorToJObjectArray(env, patches, IDCache::GetPatchCheatClass(), PatchCheatToJava);
|
2021-07-17 17:47:11 +00:00
|
|
|
}
|
2021-07-18 16:54:01 +00:00
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_features_cheats_model_PatchCheat_saveCodes(
|
|
|
|
JNIEnv* env, jclass, jstring jGameID, jint revision, jobjectArray jCodes)
|
|
|
|
{
|
|
|
|
const jsize size = env->GetArrayLength(jCodes);
|
|
|
|
std::vector<PatchEngine::Patch> vector;
|
|
|
|
vector.reserve(size);
|
|
|
|
|
|
|
|
for (jsize i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
jobject code = reinterpret_cast<jstring>(env->GetObjectArrayElement(jCodes, i));
|
|
|
|
vector.emplace_back(*GetPointer(env, code));
|
|
|
|
env->DeleteLocalRef(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string game_id = GetJString(env, jGameID);
|
|
|
|
const std::string ini_path = File::GetUserPath(D_GAMESETTINGS_IDX) + game_id + ".ini";
|
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile game_ini_local;
|
2021-07-18 16:54:01 +00:00
|
|
|
game_ini_local.Load(ini_path);
|
|
|
|
PatchEngine::SavePatchSection(&game_ini_local, vector);
|
|
|
|
game_ini_local.Save(ini_path);
|
|
|
|
}
|
2021-07-17 17:47:11 +00:00
|
|
|
}
|