From 8f410bff15e1549a3f54e33ec0f4cf511ebea7df Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 23 Jul 2022 17:14:26 +0200 Subject: [PATCH] Android: Add graphics mods support to CheatsActivity --- .../cheats/model/CheatsViewModel.java | 24 +++++ .../features/cheats/model/GraphicsMod.java | 61 ++++++++++++ .../cheats/model/GraphicsModGroup.java | 27 ++++++ .../features/cheats/ui/CheatsAdapter.java | 16 +++- .../app/src/main/res/values/strings.xml | 1 + Source/Android/jni/AndroidCommon/IDCache.cpp | 57 ++++++++++++ Source/Android/jni/AndroidCommon/IDCache.h | 8 ++ Source/Android/jni/CMakeLists.txt | 2 + Source/Android/jni/Cheats/GraphicsMod.cpp | 53 +++++++++++ .../Android/jni/Cheats/GraphicsModGroup.cpp | 92 +++++++++++++++++++ .../Config/GraphicsModListWidget.cpp | 17 +--- .../Config/GraphicsModGroup.cpp | 5 + .../Config/GraphicsModGroup.h | 1 + 13 files changed, 349 insertions(+), 15 deletions(-) create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/GraphicsMod.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/GraphicsModGroup.java create mode 100644 Source/Android/jni/Cheats/GraphicsMod.cpp create mode 100644 Source/Android/jni/Cheats/GraphicsModGroup.cpp diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java index 0238628319..4d9900c168 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java @@ -24,10 +24,13 @@ public class CheatsViewModel extends ViewModel private final MutableLiveData mGeckoCheatsDownloadedEvent = new MutableLiveData<>(null); private final MutableLiveData mOpenDetailsViewEvent = new MutableLiveData<>(false); + private GraphicsModGroup mGraphicsModGroup; + private ArrayList mGraphicsMods; private ArrayList mPatchCheats; private ArrayList mARCheats; private ArrayList mGeckoCheats; + private boolean mGraphicsModsNeedSaving = false; private boolean mPatchCheatsNeedSaving = false; private boolean mARCheatsNeedSaving = false; private boolean mGeckoCheatsNeedSaving = false; @@ -37,13 +40,23 @@ public class CheatsViewModel extends ViewModel if (mLoaded) return; + mGraphicsModGroup = GraphicsModGroup.load(gameID); + mGraphicsMods = new ArrayList<>(); + Collections.addAll(mGraphicsMods, mGraphicsModGroup.getMods()); + mPatchCheats = new ArrayList<>(); Collections.addAll(mPatchCheats, PatchCheat.loadCodes(gameID, revision)); + mARCheats = new ArrayList<>(); Collections.addAll(mARCheats, ARCheat.loadCodes(gameID, revision)); + mGeckoCheats = new ArrayList<>(); Collections.addAll(mGeckoCheats, GeckoCheat.loadCodes(gameID, revision)); + for (GraphicsMod mod : mGraphicsMods) + { + mod.setChangedCallback(() -> mGraphicsModsNeedSaving = true); + } for (PatchCheat cheat : mPatchCheats) { cheat.setChangedCallback(() -> mPatchCheatsNeedSaving = true); @@ -62,6 +75,12 @@ public class CheatsViewModel extends ViewModel public void saveIfNeeded(String gameID, int revision) { + if (mGraphicsModsNeedSaving) + { + mGraphicsModGroup.save(); + mGraphicsModsNeedSaving = false; + } + if (mPatchCheatsNeedSaving) { PatchCheat.saveCodes(gameID, revision, mPatchCheats.toArray(new PatchCheat[0])); @@ -280,6 +299,11 @@ public class CheatsViewModel extends ViewModel mOpenDetailsViewEvent.setValue(false); } + public ArrayList getGraphicsMods() + { + return mGraphicsMods; + } + public ArrayList getPatchCheats() { return mPatchCheats; diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/GraphicsMod.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/GraphicsMod.java new file mode 100644 index 0000000000..241d15093f --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/GraphicsMod.java @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.features.cheats.model; + +import androidx.annotation.Keep; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +public class GraphicsMod extends ReadOnlyCheat +{ + @Keep + private final long mPointer; + + // When a C++ GraphicsModGroup object is destroyed, it also destroys the GraphicsMods it owns. + // To avoid getting dangling pointers, we keep a reference to the GraphicsModGroup here. + @Keep + private final GraphicsModGroup mParent; + + @Keep + private GraphicsMod(long pointer, GraphicsModGroup parent) + { + mPointer = pointer; + mParent = parent; + } + + public boolean supportsCreator() + { + return true; + } + + public boolean supportsNotes() + { + return true; + } + + public boolean supportsCode() + { + return false; + } + + @NonNull + public native String getName(); + + @NonNull + public native String getCreator(); + + @NonNull + public native String getNotes(); + + public boolean getUserDefined() + { + // Technically graphics mods can be user defined, but we don't support editing graphics mods + // in the GUI, and editability is what this really controls + return false; + } + + public native boolean getEnabled(); + + @Override + protected native void setEnabledImpl(boolean enabled); +} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/GraphicsModGroup.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/GraphicsModGroup.java new file mode 100644 index 0000000000..0cb0a3c456 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/GraphicsModGroup.java @@ -0,0 +1,27 @@ +package org.dolphinemu.dolphinemu.features.cheats.model; + +import androidx.annotation.Keep; +import androidx.annotation.NonNull; + +public class GraphicsModGroup +{ + @Keep + private final long mPointer; + + @Keep + private GraphicsModGroup(long pointer) + { + mPointer = pointer; + } + + @Override + public native void finalize(); + + @NonNull + public native GraphicsMod[] getMods(); + + public native void save(); + + @NonNull + public static native GraphicsModGroup load(String gameId); +} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java index ec256cf6e2..e5be47a3f1 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java @@ -13,6 +13,7 @@ import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.features.cheats.model.ARCheat; import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel; import org.dolphinemu.dolphinemu.features.cheats.model.GeckoCheat; +import org.dolphinemu.dolphinemu.features.cheats.model.GraphicsMod; import org.dolphinemu.dolphinemu.features.cheats.model.PatchCheat; import java.util.ArrayList; @@ -90,8 +91,8 @@ public class CheatsAdapter extends RecyclerView.Adapter @Override public int getItemCount() { - return mViewModel.getARCheats().size() + mViewModel.getGeckoCheats().size() + - mViewModel.getPatchCheats().size() + 7; + return mViewModel.getGraphicsMods().size() + mViewModel.getPatchCheats().size() + + mViewModel.getARCheats().size() + mViewModel.getGeckoCheats().size() + 8; } @Override @@ -108,6 +109,17 @@ public class CheatsAdapter extends RecyclerView.Adapter private CheatItem getItemAt(int position) { + // Graphics mods + + if (position == 0) + return new CheatItem(CheatItem.TYPE_HEADER, R.string.cheats_header_graphics_mod); + position -= 1; + + ArrayList graphicsMods = mViewModel.getGraphicsMods(); + if (position < graphicsMods.size()) + return new CheatItem(graphicsMods.get(position)); + position -= graphicsMods.size(); + // Patches if (position == 0) diff --git a/Source/Android/app/src/main/res/values/strings.xml b/Source/Android/app/src/main/res/values/strings.xml index 8ae9374a50..7b1d1b7bf4 100644 --- a/Source/Android/app/src/main/res/values/strings.xml +++ b/Source/Android/app/src/main/res/values/strings.xml @@ -490,6 +490,7 @@ AR Codes Gecko Codes Patches + Graphics Mods Add New AR Code Add New Gecko Code Add New Patch diff --git a/Source/Android/jni/AndroidCommon/IDCache.cpp b/Source/Android/jni/AndroidCommon/IDCache.cpp index b125babb81..31511b485c 100644 --- a/Source/Android/jni/AndroidCommon/IDCache.cpp +++ b/Source/Android/jni/AndroidCommon/IDCache.cpp @@ -70,6 +70,14 @@ static jclass s_patch_cheat_class; static jfieldID s_patch_cheat_pointer; static jmethodID s_patch_cheat_constructor; +static jclass s_graphics_mod_group_class; +static jfieldID s_graphics_mod_group_pointer; +static jmethodID s_graphics_mod_group_constructor; + +static jclass s_graphics_mod_class; +static jfieldID s_graphics_mod_pointer; +static jmethodID s_graphics_mod_constructor; + static jclass s_riivolution_patches_class; static jfieldID s_riivolution_patches_pointer; @@ -331,6 +339,36 @@ jmethodID GetPatchCheatConstructor() return s_patch_cheat_constructor; } +jclass GetGraphicsModClass() +{ + return s_graphics_mod_class; +} + +jfieldID GetGraphicsModPointer() +{ + return s_graphics_mod_pointer; +} + +jmethodID GetGraphicsModConstructor() +{ + return s_graphics_mod_constructor; +} + +jclass GetGraphicsModGroupClass() +{ + return s_graphics_mod_group_class; +} + +jfieldID GetGraphicsModGroupPointer() +{ + return s_graphics_mod_group_pointer; +} + +jmethodID GetGraphicsModGroupConstructor() +{ + return s_graphics_mod_group_constructor; +} + jclass GetRiivolutionPatchesClass() { return s_riivolution_patches_class; @@ -480,6 +518,23 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) s_patch_cheat_constructor = env->GetMethodID(patch_cheat_class, "", "(J)V"); env->DeleteLocalRef(patch_cheat_class); + const jclass graphics_mod_group_class = + env->FindClass("org/dolphinemu/dolphinemu/features/cheats/model/GraphicsModGroup"); + s_graphics_mod_group_class = + reinterpret_cast(env->NewGlobalRef(graphics_mod_group_class)); + s_graphics_mod_group_pointer = env->GetFieldID(graphics_mod_group_class, "mPointer", "J"); + s_graphics_mod_group_constructor = env->GetMethodID(graphics_mod_group_class, "", "(J)V"); + env->DeleteLocalRef(graphics_mod_group_class); + + const jclass graphics_mod_class = + env->FindClass("org/dolphinemu/dolphinemu/features/cheats/model/GraphicsMod"); + s_graphics_mod_class = reinterpret_cast(env->NewGlobalRef(graphics_mod_class)); + s_graphics_mod_pointer = env->GetFieldID(graphics_mod_class, "mPointer", "J"); + s_graphics_mod_constructor = + env->GetMethodID(graphics_mod_class, "", + "(JLorg/dolphinemu/dolphinemu/features/cheats/model/GraphicsModGroup;)V"); + env->DeleteLocalRef(graphics_mod_class); + const jclass riivolution_patches_class = env->FindClass("org/dolphinemu/dolphinemu/features/riivolution/model/RiivolutionPatches"); s_riivolution_patches_class = @@ -516,6 +571,8 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) env->DeleteGlobalRef(s_ar_cheat_class); env->DeleteGlobalRef(s_gecko_cheat_class); env->DeleteGlobalRef(s_patch_cheat_class); + env->DeleteGlobalRef(s_graphics_mod_group_class); + env->DeleteGlobalRef(s_graphics_mod_class); env->DeleteGlobalRef(s_riivolution_patches_class); env->DeleteGlobalRef(s_wii_update_cb_class); } diff --git a/Source/Android/jni/AndroidCommon/IDCache.h b/Source/Android/jni/AndroidCommon/IDCache.h index 83eaa85894..58a0aa17bd 100644 --- a/Source/Android/jni/AndroidCommon/IDCache.h +++ b/Source/Android/jni/AndroidCommon/IDCache.h @@ -69,6 +69,14 @@ jclass GetPatchCheatClass(); jfieldID GetPatchCheatPointer(); jmethodID GetPatchCheatConstructor(); +jclass GetGraphicsModGroupClass(); +jfieldID GetGraphicsModGroupPointer(); +jmethodID GetGraphicsModGroupConstructor(); + +jclass GetGraphicsModClass(); +jfieldID GetGraphicsModPointer(); +jmethodID GetGraphicsModConstructor(); + jclass GetRiivolutionPatchesClass(); jfieldID GetRiivolutionPatchesPointer(); diff --git a/Source/Android/jni/CMakeLists.txt b/Source/Android/jni/CMakeLists.txt index 74d9d90e03..146019501e 100644 --- a/Source/Android/jni/CMakeLists.txt +++ b/Source/Android/jni/CMakeLists.txt @@ -2,6 +2,8 @@ add_library(main SHARED Cheats/ARCheat.cpp Cheats/Cheats.h Cheats/GeckoCheat.cpp + Cheats/GraphicsMod.cpp + Cheats/GraphicsModGroup.cpp Cheats/PatchCheat.cpp Config/NativeConfig.cpp Config/PostProcessing.cpp diff --git a/Source/Android/jni/Cheats/GraphicsMod.cpp b/Source/Android/jni/Cheats/GraphicsMod.cpp new file mode 100644 index 0000000000..627351f546 --- /dev/null +++ b/Source/Android/jni/Cheats/GraphicsMod.cpp @@ -0,0 +1,53 @@ +// Copyright 2022 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include + +#include + +#include "VideoCommon/GraphicsModSystem/Config/GraphicsMod.h" +#include "jni/AndroidCommon/AndroidCommon.h" +#include "jni/AndroidCommon/IDCache.h" + +static GraphicsModConfig* GetPointer(JNIEnv* env, jobject obj) +{ + return reinterpret_cast( + env->GetLongField(obj, IDCache::GetGraphicsModPointer())); +} + +extern "C" { + +JNIEXPORT jstring JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsMod_getName(JNIEnv* env, jobject obj) +{ + return ToJString(env, GetPointer(env, obj)->m_title); +} + +JNIEXPORT jstring JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsMod_getCreator(JNIEnv* env, + jobject obj) +{ + return ToJString(env, GetPointer(env, obj)->m_author); +} + +JNIEXPORT jstring JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsMod_getNotes(JNIEnv* env, jobject obj) +{ + return ToJString(env, GetPointer(env, obj)->m_description); +} + +JNIEXPORT jboolean JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsMod_getEnabled(JNIEnv* env, + jobject obj) +{ + return static_cast(GetPointer(env, obj)->m_enabled); +} + +JNIEXPORT void JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsMod_setEnabledImpl(JNIEnv* env, + jobject obj, + jboolean enabled) +{ + GetPointer(env, obj)->m_enabled = static_cast(enabled); +} +} diff --git a/Source/Android/jni/Cheats/GraphicsModGroup.cpp b/Source/Android/jni/Cheats/GraphicsModGroup.cpp new file mode 100644 index 0000000000..e7a9329b53 --- /dev/null +++ b/Source/Android/jni/Cheats/GraphicsModGroup.cpp @@ -0,0 +1,92 @@ +// Copyright 2022 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include + +#include +#include + +#include "VideoCommon/GraphicsModSystem/Config/GraphicsMod.h" +#include "VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.h" +#include "jni/AndroidCommon/AndroidCommon.h" +#include "jni/AndroidCommon/IDCache.h" + +static GraphicsModGroupConfig* GetPointer(JNIEnv* env, jobject obj) +{ + return reinterpret_cast( + env->GetLongField(obj, IDCache::GetGraphicsModGroupPointer())); +} + +jobject GraphicsModToJava(JNIEnv* env, GraphicsModConfig* mod, jobject jGraphicsModGroup) +{ + return env->NewObject(IDCache::GetGraphicsModClass(), IDCache::GetGraphicsModConstructor(), + reinterpret_cast(mod), jGraphicsModGroup); +} + +extern "C" { + +JNIEXPORT void JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsModGroup_finalize(JNIEnv* env, + jobject obj) +{ + delete GetPointer(env, obj); +} + +JNIEXPORT jobjectArray JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsModGroup_getMods(JNIEnv* env, + jobject obj) +{ + GraphicsModGroupConfig* mod_group = GetPointer(env, obj); + + std::set groups; + + for (const GraphicsModConfig& mod : mod_group->GetMods()) + { + for (const GraphicsTargetGroupConfig& group : mod.m_groups) + groups.insert(group.m_name); + } + + std::vector mods; + + for (GraphicsModConfig& mod : mod_group->GetMods()) + { + // If no group matches the mod's features, or if the mod has no features, skip it + if (std::none_of(mod.m_features.begin(), mod.m_features.end(), + [&groups](const GraphicsModFeatureConfig& feature) { + return groups.count(feature.m_group) == 1; + })) + { + continue; + } + + mods.push_back(&mod); + } + + const jobjectArray array = + env->NewObjectArray(static_cast(mods.size()), IDCache::GetGraphicsModClass(), nullptr); + + jsize i = 0; + for (GraphicsModConfig* mod : mods) + env->SetObjectArrayElement(array, i++, GraphicsModToJava(env, mod, obj)); + + return array; +} + +JNIEXPORT void JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsModGroup_save(JNIEnv* env, jobject obj) +{ + GetPointer(env, obj)->Save(); +} + +JNIEXPORT jobject JNICALL +Java_org_dolphinemu_dolphinemu_features_cheats_model_GraphicsModGroup_load(JNIEnv* env, jclass, + jstring jGameId) +{ + auto* mod_group = new GraphicsModGroupConfig(GetJString(env, jGameId)); + + mod_group->Load(); + + return env->NewObject(IDCache::GetGraphicsModGroupClass(), + IDCache::GetGraphicsModGroupConstructor(), mod_group); +} +} diff --git a/Source/Core/DolphinQt/Config/GraphicsModListWidget.cpp b/Source/Core/DolphinQt/Config/GraphicsModListWidget.cpp index cb9569d1d1..ac01158b2f 100644 --- a/Source/Core/DolphinQt/Config/GraphicsModListWidget.cpp +++ b/Source/Core/DolphinQt/Config/GraphicsModListWidget.cpp @@ -120,24 +120,15 @@ void GraphicsModListWidget::RefreshModList() std::set groups; - for (const auto& mod : m_mod_group.GetMods()) + for (const GraphicsModConfig& mod : m_mod_group.GetMods()) { - if (mod.m_groups.empty()) - continue; - - for (const auto& group : mod.m_groups) - { + for (const GraphicsTargetGroupConfig& group : mod.m_groups) groups.insert(group.m_name); - } } - for (const auto& mod : m_mod_group.GetMods()) + for (const GraphicsModConfig& mod : m_mod_group.GetMods()) { - // Group only mods shouldn't be shown - if (mod.m_features.empty()) - continue; - - // If the group doesn't exist in the available mod's features, skip + // If no group matches the mod's features, or if the mod has no features, skip it if (std::none_of(mod.m_features.begin(), mod.m_features.end(), [&groups](const GraphicsModFeatureConfig& feature) { return groups.count(feature.m_group) == 1; diff --git a/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.cpp b/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.cpp index 0d1ae30fb6..40685dff1b 100644 --- a/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.cpp +++ b/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.cpp @@ -168,6 +168,11 @@ const std::vector& GraphicsModGroupConfig::GetMods() const return m_graphics_mods; } +std::vector& GraphicsModGroupConfig::GetMods() +{ + return m_graphics_mods; +} + GraphicsModConfig* GraphicsModGroupConfig::GetMod(const std::string& absolute_path) const { if (const auto iter = m_path_to_graphics_mod.find(absolute_path); diff --git a/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.h b/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.h index ace5127c58..721c8e5c2c 100644 --- a/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.h +++ b/Source/Core/VideoCommon/GraphicsModSystem/Config/GraphicsModGroup.h @@ -32,6 +32,7 @@ public: u32 GetChangeCount() const; const std::vector& GetMods() const; + std::vector& GetMods(); GraphicsModConfig* GetMod(const std::string& absolute_path) const;