Skip to content

Commit

Permalink
Android: Clear game profiles and GameSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebola16 committed Jul 24, 2020
1 parent f96f3b3 commit e7d5322
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
Expand Up @@ -527,7 +527,7 @@ public static boolean displayAlertMsg(final String caption, final String text,
{
lock.wait();
}
catch (Exception e)
catch (Exception ignored)
{
}
}
Expand Down
Expand Up @@ -6,7 +6,6 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import android.widget.Toast;

Expand All @@ -18,6 +17,7 @@
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;

Expand Down Expand Up @@ -97,12 +97,16 @@ public Dialog onCreateDialog(Bundle savedInstanceState)

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();
Expand All @@ -118,4 +122,33 @@ private void clearGameSettings(String gameId)
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;
}
}
Expand Up @@ -670,21 +670,21 @@ private static Setting settingFromLine(SettingSection current, String line)

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)
{
}

Expand Down
Expand Up @@ -446,9 +446,8 @@ private static void deleteFile(String path)
File file = new File(path);
file.delete();
}
catch (Exception ex)
catch (Exception ignored)
{
// fail safely
}
}
}
Expand Up @@ -69,9 +69,8 @@ public static void saveCover(Bitmap cover, String path)
cover.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
}
catch (Exception e)
catch (Exception ignored)
{
// Do nothing
}
}
}
Expand Up @@ -12,6 +12,7 @@
import android.os.Environment;
import android.preference.PreferenceManager;

import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import org.dolphinemu.dolphinemu.NativeLibrary;
Expand Down Expand Up @@ -173,14 +174,24 @@ private static void initializeExternalStorage(Context context)
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()
Expand Down Expand Up @@ -285,9 +296,8 @@ public static void copyFile(String from, String to)
OutputStream out = new FileOutputStream(to);
copyFile(in, out);
}
catch (IOException e)
catch (IOException ignored)
{

}
}

Expand Down

0 comments on commit e7d5322

Please sign in to comment.