From cb522d8352a19f40d8b949d644150f57d7c6d232 Mon Sep 17 00:00:00 2001 From: TwistedUmbrella Date: Sun, 9 Feb 2014 15:51:28 -0500 Subject: [PATCH] Back up configuration when writing to prevent failure loss The configuration is not always being properly written to the file. This method will ensure the original user configuration (especially when manually modified) is not lost in the process. --- shell/android/res/values/strings.xml | 2 +- .../reicast/emulator/ConfigureFragment.java | 27 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/shell/android/res/values/strings.xml b/shell/android/res/values/strings.xml index 3a5fa4960..da7bdeef6 100644 --- a/shell/android/res/values/strings.xml +++ b/shell/android/res/values/strings.xml @@ -99,7 +99,7 @@ OFF Copying logcat content to clipboard\nPlease paste in the issue report - Please generate a config\n(Run BIOS at least once) + Configuration failed! Submit Error Logs diff --git a/shell/android/src/com/reicast/emulator/ConfigureFragment.java b/shell/android/src/com/reicast/emulator/ConfigureFragment.java index 7dfe43061..f843b48e4 100644 --- a/shell/android/src/com/reicast/emulator/ConfigureFragment.java +++ b/shell/android/src/com/reicast/emulator/ConfigureFragment.java @@ -432,11 +432,13 @@ public class ConfigureFragment extends Fragment { public void executeAppendConfig(String identifier, String value) { File config = new File(home_directory, "emu.cfg"); + File modified = new File(home_directory, "emu.cfg.bak"); try { if (config.exists()) { + config.renameTo(modified); // Read existing emu.cfg and substitute new setting value StringBuilder rebuildFile = new StringBuilder(); - Scanner scanner = new Scanner(config); + Scanner scanner = new Scanner(modified); String currentLine; while (scanner.hasNextLine()) { currentLine = scanner.nextLine(); @@ -447,11 +449,18 @@ public class ConfigureFragment extends Fragment { } } scanner.close(); - config.delete(); FileOutputStream fos = new FileOutputStream(config); fos.write(rebuildFile.toString().getBytes()); fos.close(); - } else if (config.createNewFile()) { + if (config.exists()) { + modified.delete(); + } else { + Toast.makeText(parentActivity, + parentActivity.getString(R.string.bios_config), + Toast.LENGTH_SHORT).show(); + modified.renameTo(config); + } + } else { StringBuilder rebuildFile = new StringBuilder(); rebuildFile.append("[config]" + "\n"); rebuildFile.append("Dynarec.Enabled=" @@ -481,13 +490,17 @@ public class ConfigureFragment extends Fragment { FileOutputStream fos = new FileOutputStream(config); fos.write(rebuildFile.toString().getBytes()); fos.close(); - } else { - Toast.makeText(parentActivity, - parentActivity.getString(R.string.bios_config), - Toast.LENGTH_SHORT).show(); + if (!config.exists()) { + Toast.makeText(parentActivity, + parentActivity.getString(R.string.bios_config), + Toast.LENGTH_SHORT).show(); + } } } catch (Exception e) { Log.d("reicast", "Exception: " + e); + Toast.makeText(parentActivity, + parentActivity.getString(R.string.bios_config), + Toast.LENGTH_SHORT).show(); } } }