From e7d53224b2c7e83196478205e993126ec56701cc Mon Sep 17 00:00:00 2001 From: Ryan Meredith Date: Fri, 24 Jul 2020 13:36:38 -0400 Subject: [PATCH] Android: Clear game profiles and GameSettings --- .../dolphinemu/dolphinemu/NativeLibrary.java | 2 +- .../dialogs/GamePropertiesDialog.java | 43 ++++++++++++++++--- .../features/settings/utils/SettingsFile.java | 8 ++-- .../fragments/EmulationFragment.java | 3 +- .../dolphinemu/utils/CoverHelper.java | 3 +- .../utils/DirectoryInitialization.java | 20 ++++++--- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java index ff4521ffdb..d11ac98c26 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java @@ -527,7 +527,7 @@ public final class NativeLibrary { lock.wait(); } - catch (Exception e) + catch (Exception ignored) { } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GamePropertiesDialog.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GamePropertiesDialog.java index 09ae7a0075..e720c20ccd 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GamePropertiesDialog.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GamePropertiesDialog.java @@ -6,7 +6,6 @@ import android.os.Bundle; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.DialogFragment; -import androidx.fragment.app.FragmentActivity; import android.widget.Toast; @@ -18,6 +17,7 @@ import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivity; import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile; import org.dolphinemu.dolphinemu.ui.platform.Platform; import org.dolphinemu.dolphinemu.utils.DirectoryInitialization; +import org.dolphinemu.dolphinemu.utils.Log; import java.io.File; @@ -97,12 +97,16 @@ public class GamePropertiesDialog extends DialogFragment private void clearGameSettings(String gameId) { - String path = + String gameSettingsPath = DirectoryInitialization.getUserDirectory() + "/GameSettings/" + gameId + ".ini"; - File gameSettingsFile = new File(path); - if (gameSettingsFile.exists()) + String gameProfilesPath = DirectoryInitialization.getUserDirectory() + "/Config/Profiles/"; + File gameSettingsFile = new File(gameSettingsPath); + File gameProfilesDirectory = new File(gameProfilesPath); + boolean hadGameProfiles = recursivelyDeleteGameProfiles(gameProfilesDirectory, gameId); + + if (gameSettingsFile.exists() || hadGameProfiles) { - if (gameSettingsFile.delete()) + if (gameSettingsFile.delete() || hadGameProfiles) { Toast.makeText(getContext(), "Cleared settings for " + gameId, Toast.LENGTH_SHORT) .show(); @@ -118,4 +122,33 @@ public class GamePropertiesDialog extends DialogFragment Toast.makeText(getContext(), "No game settings to delete", Toast.LENGTH_SHORT).show(); } } + + private boolean recursivelyDeleteGameProfiles(@NonNull final File file, String gameId) + { + boolean hadGameProfiles = false; + + if (file.isDirectory()) + { + File[] files = file.listFiles(); + + if (files == null) + { + return false; + } + + for (File child : files) + { + if (child.getName().startsWith(gameId) && child.isFile()) + { + if (!child.delete()) + { + Log.error("[GamePropertiesDialog] Failed to delete " + child.getAbsolutePath()); + } + hadGameProfiles = true; + } + hadGameProfiles |= recursivelyDeleteGameProfiles(child, gameId); + } + } + return hadGameProfiles; + } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/utils/SettingsFile.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/utils/SettingsFile.java index e4bc8b9bd9..a17fdf1d61 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/utils/SettingsFile.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/utils/SettingsFile.java @@ -670,21 +670,21 @@ public final class SettingsFile try { - int valueAsInt = Integer.valueOf(value); + int valueAsInt = Integer.parseInt(value); return new IntSetting(key, current.getName(), valueAsInt); } - catch (NumberFormatException ex) + catch (NumberFormatException ignored) { } try { - float valueAsFloat = Float.valueOf(value); + float valueAsFloat = Float.parseFloat(value); return new FloatSetting(key, current.getName(), valueAsFloat); } - catch (NumberFormatException ex) + catch (NumberFormatException ignored) { } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java index 0315a9e133..3b73367130 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java @@ -446,9 +446,8 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C File file = new File(path); file.delete(); } - catch (Exception ex) + catch (Exception ignored) { - // fail safely } } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/CoverHelper.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/CoverHelper.java index 2c0cd393a0..08b232f4d9 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/CoverHelper.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/CoverHelper.java @@ -69,9 +69,8 @@ public final class CoverHelper cover.compress(Bitmap.CompressFormat.PNG, 100, out); out.close(); } - catch (Exception e) + catch (Exception ignored) { - // Do nothing } } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/DirectoryInitialization.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/DirectoryInitialization.java index 62aa4d3cac..c679dbe581 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/DirectoryInitialization.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/DirectoryInitialization.java @@ -12,6 +12,7 @@ import android.content.SharedPreferences; import android.os.Environment; import android.preference.PreferenceManager; +import androidx.annotation.NonNull; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import org.dolphinemu.dolphinemu.NativeLibrary; @@ -173,14 +174,24 @@ public final class DirectoryInitialization context); } - private static void deleteDirectoryRecursively(File file) + private static void deleteDirectoryRecursively(@NonNull final File file) { if (file.isDirectory()) { - for (File child : file.listFiles()) + File[] files = file.listFiles(); + + if (files == null) + { + return; + } + + for (File child : files) deleteDirectoryRecursively(child); } - file.delete(); + if (!file.delete()) + { + Log.error("[DirectoryInitialization] Failed to delete " + file.getAbsolutePath()); + } } public static boolean areDolphinDirectoriesReady() @@ -285,9 +296,8 @@ public final class DirectoryInitialization OutputStream out = new FileOutputStream(to); copyFile(in, out); } - catch (IOException e) + catch (IOException ignored) { - } }