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.
This commit is contained in:
TwistedUmbrella 2014-02-09 15:51:28 -05:00
parent ae6d694c52
commit cb522d8352
2 changed files with 21 additions and 8 deletions

View File

@ -99,7 +99,7 @@
<string name="textOff">OFF</string>
<string name="platform">Copying logcat content to clipboard\nPlease paste in the issue report</string>
<string name="bios_config">Please generate a config\n(Run BIOS at least once)</string>
<string name="bios_config">Configuration failed!</string>
<string name="menu_debug">Submit Error Logs</string>
</resources>

View File

@ -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();
}
}
}