From 03fa91ba3c52c0371f0d57ea8a5618feaf3012e7 Mon Sep 17 00:00:00 2001 From: t895 Date: Fri, 19 Jan 2024 16:37:34 -0500 Subject: android: Add addon delete button Required some refactoring of retrieving patches in order for the frontend to pass the right information to ContentManager for deletion. --- .../main/java/org/yuzu/yuzu_emu/NativeLibrary.kt | 25 +++++++++- .../org/yuzu/yuzu_emu/adapters/AddonAdapter.kt | 21 +++++---- .../org/yuzu/yuzu_emu/fragments/AddonsFragment.kt | 17 ++++++- .../src/main/java/org/yuzu/yuzu_emu/model/Addon.kt | 10 ---- .../java/org/yuzu/yuzu_emu/model/AddonViewModel.kt | 42 +++++++++++------ .../src/main/java/org/yuzu/yuzu_emu/model/Patch.kt | 16 +++++++ .../main/java/org/yuzu/yuzu_emu/model/PatchType.kt | 14 ++++++ src/android/app/src/main/jni/id_cache.cpp | 55 ++++++++++++++++++++++ src/android/app/src/main/jni/id_cache.h | 9 ++++ src/android/app/src/main/jni/native.cpp | 48 +++++++++++++------ .../app/src/main/res/layout/list_item_addon.xml | 32 +++++++++---- src/android/app/src/main/res/values/strings.xml | 3 ++ 12 files changed, 233 insertions(+), 59 deletions(-) delete mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Addon.kt create mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Patch.kt create mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/model/PatchType.kt (limited to 'src/android') diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt index 8cb98d6d7..1c9fb0675 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt @@ -22,6 +22,7 @@ import org.yuzu.yuzu_emu.utils.FileUtil import org.yuzu.yuzu_emu.utils.Log import org.yuzu.yuzu_emu.utils.SerializableHelper.serializable import org.yuzu.yuzu_emu.model.InstallResult +import org.yuzu.yuzu_emu.model.Patch /** * Class which contains methods that interact @@ -539,9 +540,29 @@ object NativeLibrary { * * @param path Path to game file. Can be a [Uri]. * @param programId String representation of a game's program ID - * @return Array of pairs where the first value is the name of an addon and the second is the version + * @return Array of available patches */ - external fun getAddonsForFile(path: String, programId: String): Array>? + external fun getPatchesForFile(path: String, programId: String): Array? + + /** + * Removes an update for a given [programId] + * @param programId String representation of a game's program ID + */ + external fun removeUpdate(programId: String) + + /** + * Removes all DLC for a [programId] + * @param programId String representation of a game's program ID + */ + external fun removeDLC(programId: String) + + /** + * Removes a mod installed for a given [programId] + * @param programId String representation of a game's program ID + * @param name The name of a mod as given by [getPatchesForFile]. This corresponds with the name + * of the mod's directory in a game's load folder. + */ + external fun removeMod(programId: String, name: String) /** * Gets the save location for a specific game diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AddonAdapter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AddonAdapter.kt index 94c151325..ff254d9b7 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AddonAdapter.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/AddonAdapter.kt @@ -6,27 +6,32 @@ package org.yuzu.yuzu_emu.adapters import android.view.LayoutInflater import android.view.ViewGroup import org.yuzu.yuzu_emu.databinding.ListItemAddonBinding -import org.yuzu.yuzu_emu.model.Addon +import org.yuzu.yuzu_emu.model.Patch +import org.yuzu.yuzu_emu.model.AddonViewModel import org.yuzu.yuzu_emu.viewholder.AbstractViewHolder -class AddonAdapter : AbstractDiffAdapter() { +class AddonAdapter(val addonViewModel: AddonViewModel) : + AbstractDiffAdapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddonViewHolder { ListItemAddonBinding.inflate(LayoutInflater.from(parent.context), parent, false) .also { return AddonViewHolder(it) } } inner class AddonViewHolder(val binding: ListItemAddonBinding) : - AbstractViewHolder(binding) { - override fun bind(model: Addon) { + AbstractViewHolder(binding) { + override fun bind(model: Patch) { binding.root.setOnClickListener { - binding.addonSwitch.isChecked = !binding.addonSwitch.isChecked + binding.addonCheckbox.isChecked = !binding.addonCheckbox.isChecked } - binding.title.text = model.title + binding.title.text = model.name binding.version.text = model.version - binding.addonSwitch.setOnCheckedChangeListener { _, checked -> + binding.addonCheckbox.setOnCheckedChangeListener { _, checked -> model.enabled = checked } - binding.addonSwitch.isChecked = model.enabled + binding.addonCheckbox.isChecked = model.enabled + binding.buttonDelete.setOnClickListener { + addonViewModel.setAddonToDelete(model) + } } } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/AddonsFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/AddonsFragment.kt index b63ece9a4..adb65812c 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/AddonsFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/AddonsFragment.kt @@ -74,7 +74,7 @@ class AddonsFragment : Fragment() { binding.listAddons.apply { layoutManager = LinearLayoutManager(requireContext()) - adapter = AddonAdapter() + adapter = AddonAdapter(addonViewModel) } viewLifecycleOwner.lifecycleScope.apply { @@ -110,6 +110,21 @@ class AddonsFragment : Fragment() { } } } + launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + addonViewModel.addonToDelete.collect { + if (it != null) { + MessageDialogFragment.newInstance( + requireActivity(), + titleId = R.string.confirm_uninstall, + descriptionId = R.string.confirm_uninstall_description, + positiveAction = { addonViewModel.onDeleteAddon(it) } + ).show(parentFragmentManager, MessageDialogFragment.TAG) + addonViewModel.setAddonToDelete(null) + } + } + } + } } binding.buttonInstall.setOnClickListener { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Addon.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Addon.kt deleted file mode 100644 index ed79a8b02..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Addon.kt +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-FileCopyrightText: 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.yuzu.yuzu_emu.model - -data class Addon( - var enabled: Boolean, - val title: String, - val version: String -) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/AddonViewModel.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/AddonViewModel.kt index 075252f5b..b9c8e49ca 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/AddonViewModel.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/AddonViewModel.kt @@ -15,8 +15,8 @@ import org.yuzu.yuzu_emu.utils.NativeConfig import java.util.concurrent.atomic.AtomicBoolean class AddonViewModel : ViewModel() { - private val _addonList = MutableStateFlow(mutableListOf()) - val addonList get() = _addonList.asStateFlow() + private val _patchList = MutableStateFlow(mutableListOf()) + val addonList get() = _patchList.asStateFlow() private val _showModInstallPicker = MutableStateFlow(false) val showModInstallPicker get() = _showModInstallPicker.asStateFlow() @@ -24,6 +24,9 @@ class AddonViewModel : ViewModel() { private val _showModNoticeDialog = MutableStateFlow(false) val showModNoticeDialog get() = _showModNoticeDialog.asStateFlow() + private val _addonToDelete = MutableStateFlow(null) + val addonToDelete = _addonToDelete.asStateFlow() + var game: Game? = null private val isRefreshing = AtomicBoolean(false) @@ -40,36 +43,47 @@ class AddonViewModel : ViewModel() { isRefreshing.set(true) viewModelScope.launch { withContext(Dispatchers.IO) { - val addonList = mutableListOf() - val disabledAddons = NativeConfig.getDisabledAddons(game!!.programId) - NativeLibrary.getAddonsForFile(game!!.path, game!!.programId)?.forEach { - val name = it.first.replace("[D] ", "") - addonList.add(Addon(!disabledAddons.contains(name), name, it.second)) - } - addonList.sortBy { it.title } - _addonList.value = addonList + val patchList = ( + NativeLibrary.getPatchesForFile(game!!.path, game!!.programId) + ?: emptyArray() + ).toMutableList() + patchList.sortBy { it.name } + _patchList.value = patchList isRefreshing.set(false) } } } + fun setAddonToDelete(patch: Patch?) { + _addonToDelete.value = patch + } + + fun onDeleteAddon(patch: Patch) { + when (PatchType.from(patch.type)) { + PatchType.Update -> NativeLibrary.removeUpdate(patch.programId) + PatchType.DLC -> NativeLibrary.removeDLC(patch.programId) + PatchType.Mod -> NativeLibrary.removeMod(patch.programId, patch.name) + } + refreshAddons() + } + fun onCloseAddons() { - if (_addonList.value.isEmpty()) { + if (_patchList.value.isEmpty()) { return } NativeConfig.setDisabledAddons( game!!.programId, - _addonList.value.mapNotNull { + _patchList.value.mapNotNull { if (it.enabled) { null } else { - it.title + it.name } }.toTypedArray() ) NativeConfig.saveGlobalConfig() - _addonList.value.clear() + _patchList.value.clear() game = null } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Patch.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Patch.kt new file mode 100644 index 000000000..25cb9e365 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Patch.kt @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.model + +import androidx.annotation.Keep + +@Keep +data class Patch( + var enabled: Boolean, + val name: String, + val version: String, + val type: Int, + val programId: String, + val titleId: String +) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/PatchType.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/PatchType.kt new file mode 100644 index 000000000..e9a54162b --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/PatchType.kt @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.model + +enum class PatchType(val int: Int) { + Update(0), + DLC(1), + Mod(2); + + companion object { + fun from(int: Int): PatchType = entries.firstOrNull { it.int == int } ?: Update + } +} diff --git a/src/android/app/src/main/jni/id_cache.cpp b/src/android/app/src/main/jni/id_cache.cpp index 19ced175f..96f2ad3d4 100644 --- a/src/android/app/src/main/jni/id_cache.cpp +++ b/src/android/app/src/main/jni/id_cache.cpp @@ -43,6 +43,15 @@ static jfieldID s_overlay_control_data_landscape_position_field; static jfieldID s_overlay_control_data_portrait_position_field; static jfieldID s_overlay_control_data_foldable_position_field; +static jclass s_patch_class; +static jmethodID s_patch_constructor; +static jfieldID s_patch_enabled_field; +static jfieldID s_patch_name_field; +static jfieldID s_patch_version_field; +static jfieldID s_patch_type_field; +static jfieldID s_patch_program_id_field; +static jfieldID s_patch_title_id_field; + static jclass s_double_class; static jmethodID s_double_constructor; static jfieldID s_double_value_field; @@ -194,6 +203,38 @@ jfieldID GetOverlayControlDataFoldablePositionField() { return s_overlay_control_data_foldable_position_field; } +jclass GetPatchClass() { + return s_patch_class; +} + +jmethodID GetPatchConstructor() { + return s_patch_constructor; +} + +jfieldID GetPatchEnabledField() { + return s_patch_enabled_field; +} + +jfieldID GetPatchNameField() { + return s_patch_name_field; +} + +jfieldID GetPatchVersionField() { + return s_patch_version_field; +} + +jfieldID GetPatchTypeField() { + return s_patch_type_field; +} + +jfieldID GetPatchProgramIdField() { + return s_patch_program_id_field; +} + +jfieldID GetPatchTitleIdField() { + return s_patch_title_id_field; +} + jclass GetDoubleClass() { return s_double_class; } @@ -310,6 +351,19 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { env->GetFieldID(overlay_control_data_class, "foldablePosition", "Lkotlin/Pair;"); env->DeleteLocalRef(overlay_control_data_class); + const jclass patch_class = env->FindClass("org/yuzu/yuzu_emu/model/Patch"); + s_patch_class = reinterpret_cast(env->NewGlobalRef(patch_class)); + s_patch_constructor = env->GetMethodID( + patch_class, "", + "(ZLjava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V"); + s_patch_enabled_field = env->GetFieldID(patch_class, "enabled", "Z"); + s_patch_name_field = env->GetFieldID(patch_class, "name", "Ljava/lang/String;"); + s_patch_version_field = env->GetFieldID(patch_class, "version", "Ljava/lang/String;"); + s_patch_type_field = env->GetFieldID(patch_class, "type", "I"); + s_patch_program_id_field = env->GetFieldID(patch_class, "programId", "Ljava/lang/String;"); + s_patch_title_id_field = env->GetFieldID(patch_class, "titleId", "Ljava/lang/String;"); + env->DeleteLocalRef(patch_class); + const jclass double_class = env->FindClass("java/lang/Double"); s_double_class = reinterpret_cast(env->NewGlobalRef(double_class)); s_double_constructor = env->GetMethodID(double_class, "", "(D)V"); @@ -353,6 +407,7 @@ void JNI_OnUnload(JavaVM* vm, void* reserved) { env->DeleteGlobalRef(s_string_class); env->DeleteGlobalRef(s_pair_class); env->DeleteGlobalRef(s_overlay_control_data_class); + env->DeleteGlobalRef(s_patch_class); env->DeleteGlobalRef(s_double_class); env->DeleteGlobalRef(s_integer_class); env->DeleteGlobalRef(s_boolean_class); diff --git a/src/android/app/src/main/jni/id_cache.h b/src/android/app/src/main/jni/id_cache.h index 0e5267b73..a002e705d 100644 --- a/src/android/app/src/main/jni/id_cache.h +++ b/src/android/app/src/main/jni/id_cache.h @@ -43,6 +43,15 @@ jfieldID GetOverlayControlDataLandscapePositionField(); jfieldID GetOverlayControlDataPortraitPositionField(); jfieldID GetOverlayControlDataFoldablePositionField(); +jclass GetPatchClass(); +jmethodID GetPatchConstructor(); +jfieldID GetPatchEnabledField(); +jfieldID GetPatchNameField(); +jfieldID GetPatchVersionField(); +jfieldID GetPatchTypeField(); +jfieldID GetPatchProgramIdField(); +jfieldID GetPatchTitleIdField(); + jclass GetDoubleClass(); jmethodID GetDoubleConstructor(); jfieldID GetDoubleValueField(); diff --git a/src/android/app/src/main/jni/native.cpp b/src/android/app/src/main/jni/native.cpp index b8fef5c6f..be0a723b1 100644 --- a/src/android/app/src/main/jni/native.cpp +++ b/src/android/app/src/main/jni/native.cpp @@ -774,9 +774,9 @@ jboolean Java_org_yuzu_yuzu_1emu_NativeLibrary_isFirmwareAvailable(JNIEnv* env, return true; } -jobjectArray Java_org_yuzu_yuzu_1emu_NativeLibrary_getAddonsForFile(JNIEnv* env, jobject jobj, - jstring jpath, - jstring jprogramId) { +jobjectArray Java_org_yuzu_yuzu_1emu_NativeLibrary_getPatchesForFile(JNIEnv* env, jobject jobj, + jstring jpath, + jstring jprogramId) { const auto path = GetJString(env, jpath); const auto vFile = Core::GetGameFileFromPath(EmulationSession::GetInstance().System().GetFilesystem(), path); @@ -793,20 +793,40 @@ jobjectArray Java_org_yuzu_yuzu_1emu_NativeLibrary_getAddonsForFile(JNIEnv* env, FileSys::VirtualFile update_raw; loader->ReadUpdateRaw(update_raw); - auto addons = pm.GetPatchVersionNames(update_raw); - auto jemptyString = ToJString(env, ""); - auto jemptyStringPair = env->NewObject(IDCache::GetPairClass(), IDCache::GetPairConstructor(), - jemptyString, jemptyString); - jobjectArray jaddonsArray = - env->NewObjectArray(addons.size(), IDCache::GetPairClass(), jemptyStringPair); + auto patches = pm.GetPatches(update_raw); + jobjectArray jpatchArray = + env->NewObjectArray(patches.size(), IDCache::GetPatchClass(), nullptr); int i = 0; - for (const auto& addon : addons) { - jobject jaddon = env->NewObject(IDCache::GetPairClass(), IDCache::GetPairConstructor(), - ToJString(env, addon.first), ToJString(env, addon.second)); - env->SetObjectArrayElement(jaddonsArray, i, jaddon); + for (const auto& patch : patches) { + jobject jpatch = env->NewObject( + IDCache::GetPatchClass(), IDCache::GetPatchConstructor(), patch.enabled, + ToJString(env, patch.name), ToJString(env, patch.version), + static_cast(patch.type), ToJString(env, std::to_string(patch.program_id)), + ToJString(env, std::to_string(patch.title_id))); + env->SetObjectArrayElement(jpatchArray, i, jpatch); ++i; } - return jaddonsArray; + return jpatchArray; +} + +void Java_org_yuzu_yuzu_1emu_NativeLibrary_removeUpdate(JNIEnv* env, jobject jobj, + jstring jprogramId) { + auto program_id = EmulationSession::GetProgramId(env, jprogramId); + ContentManager::RemoveUpdate(EmulationSession::GetInstance().System().GetFileSystemController(), + program_id); +} + +void Java_org_yuzu_yuzu_1emu_NativeLibrary_removeDLC(JNIEnv* env, jobject jobj, + jstring jprogramId) { + auto program_id = EmulationSession::GetProgramId(env, jprogramId); + ContentManager::RemoveAllDLC(&EmulationSession::GetInstance().System(), program_id); +} + +void Java_org_yuzu_yuzu_1emu_NativeLibrary_removeMod(JNIEnv* env, jobject jobj, jstring jprogramId, + jstring jname) { + auto program_id = EmulationSession::GetProgramId(env, jprogramId); + ContentManager::RemoveMod(EmulationSession::GetInstance().System().GetFileSystemController(), + program_id, GetJString(env, jname)); } jstring Java_org_yuzu_yuzu_1emu_NativeLibrary_getSavePath(JNIEnv* env, jobject jobj, diff --git a/src/android/app/src/main/res/layout/list_item_addon.xml b/src/android/app/src/main/res/layout/list_item_addon.xml index 74ca04ef1..3a1382fe2 100644 --- a/src/android/app/src/main/res/layout/list_item_addon.xml +++ b/src/android/app/src/main/res/layout/list_item_addon.xml @@ -14,12 +14,11 @@ android:id="@+id/text_container" android:layout_width="0dp" android:layout_height="wrap_content" - android:layout_marginEnd="16dp" android:orientation="vertical" - app:layout_constraintBottom_toBottomOf="@+id/addon_switch" - app:layout_constraintEnd_toStartOf="@+id/addon_switch" + android:layout_marginEnd="16dp" + app:layout_constraintEnd_toStartOf="@+id/addon_checkbox" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="@+id/addon_switch"> + app:layout_constraintTop_toTopOf="parent"> - + +