From 50db76cfa89dbf7d6292800bc90f01208d5d3620 Mon Sep 17 00:00:00 2001 From: tom-overton Date: Tue, 12 Jul 2022 14:39:46 -0700 Subject: If the randomizer tries to save to a place where it's not allowed to write, pop up a warning dialog instead of throwing an exception --- src/com/sneed/pkrandom/ctr/NCCH.java | 9 ++++-- .../exceptions/CannotWriteToLocationException.java | 36 ++++++++++++++++++++++ src/com/sneed/pkrandom/newgui/Bundle.properties | 1 + .../sneed/pkrandom/newgui/NewRandomizerGUI.java | 8 ++++- src/com/sneed/pkrandom/newnds/NDSRom.java | 9 ++++-- .../pkrandom/romhandlers/AbstractGBRomHandler.java | 9 ++++-- 6 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/com/sneed/pkrandom/exceptions/CannotWriteToLocationException.java diff --git a/src/com/sneed/pkrandom/ctr/NCCH.java b/src/com/sneed/pkrandom/ctr/NCCH.java index 1134095..ccf735c 100644 --- a/src/com/sneed/pkrandom/ctr/NCCH.java +++ b/src/com/sneed/pkrandom/ctr/NCCH.java @@ -23,6 +23,7 @@ package com.sneed.pkrandom.ctr; import com.sneed.pkrandom.FileFunctions; import com.sneed.pkrandom.SysConstants; +import com.sneed.pkrandom.exceptions.CannotWriteToLocationException; import com.sneed.pkrandom.exceptions.EncryptedROMException; import com.sneed.pkrandom.exceptions.RandomizerIOException; import cuecompressors.BLZCoder; @@ -249,8 +250,12 @@ public class NCCH { public void saveAsNCCH(String filename, String gameAcronym, long seed) throws IOException, NoSuchAlgorithmException { this.reopenROM(); - // Initialise new ROM - RandomAccessFile fNew = new RandomAccessFile(filename, "rw"); + // Initialize new ROM + File file = new File(filename); + if (!file.canWrite()) { + throw new CannotWriteToLocationException("The randomizer cannot write to this location: " + filename); + } + RandomAccessFile fNew = new RandomAccessFile(file, "rw"); // Read the header and exheader and write it to the output ROM byte[] header = new byte[header_and_exheader_size]; diff --git a/src/com/sneed/pkrandom/exceptions/CannotWriteToLocationException.java b/src/com/sneed/pkrandom/exceptions/CannotWriteToLocationException.java new file mode 100644 index 0000000..59f3adb --- /dev/null +++ b/src/com/sneed/pkrandom/exceptions/CannotWriteToLocationException.java @@ -0,0 +1,36 @@ +package com.sneed.pkrandom.exceptions; + +/*----------------------------------------------------------------------------*/ +/*-- CannotWriteToLocationException.java - exception for when a user tries --*/ +/*-- to write to a place where they --*/ +/*-- don't have permissions. --*/ +/*-- --*/ +/*-- Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team --*/ +/*-- Pokemon and any associated names and the like are --*/ +/*-- trademark and (C) Nintendo 1996-2020. --*/ +/*-- --*/ +/*-- The custom code written here is licensed under the terms of the GPL: --*/ +/*-- --*/ +/*-- This program is free software: you can redistribute it and/or modify --*/ +/*-- it under the terms of the GNU General Public License as published by --*/ +/*-- the Free Software Foundation, either version 3 of the License, or --*/ +/*-- (at your option) any later version. --*/ +/*-- --*/ +/*-- This program is distributed in the hope that it will be useful, --*/ +/*-- but WITHOUT ANY WARRANTY; without even the implied warranty of --*/ +/*-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --*/ +/*-- GNU General Public License for more details. --*/ +/*-- --*/ +/*-- You should have received a copy of the GNU General Public License --*/ +/*-- along with this program. If not, see . --*/ +/*----------------------------------------------------------------------------*/ + +public class CannotWriteToLocationException extends RuntimeException { + public CannotWriteToLocationException(Exception e) { + super(e); + } + + public CannotWriteToLocationException(String text) { + super(text); + } +} diff --git a/src/com/sneed/pkrandom/newgui/Bundle.properties b/src/com/sneed/pkrandom/newgui/Bundle.properties index 6bff613..b3e55da 100644 --- a/src/com/sneed/pkrandom/newgui/Bundle.properties +++ b/src/com/sneed/pkrandom/newgui/Bundle.properties @@ -401,6 +401,7 @@ GUI.saveFailedMessage=Error during randomization: %s.\nIf this is the first time GUI.saveFailedMessageNoLog=Error during randomization: %s.\nIf this is the first time you've seen this message, try again.\nIf it keeps happening, try loosening any restrictive settings you've selected. GUI.saveFailedIO=There was an unhandled exception trying to save your ROM to disk.\nA log file containing some details has been saved to %s.\nPlease include this file in any bug reports you do. GUI.saveFailedIONoLog=There was an unhandled exception trying to save your ROM to disk. +GUI.cannotWriteToLocation=The randomizer is not allowed to write this file: %s.\nPlease try saving your ROM to a different location. GUI.raceModeCheckValuePopup=Your check value for the race is:\n%08X\nDistribute this along with the preset file, if you're the race maker!\nIf you received this in a race, compare it to the value the race maker gave. GUI.saveLogDialog.text=Do you want to save a log file of the randomization performed?\nThis may allow you to gain an unfair advantage, do not do so if you are doing something like a race. GUI.saveLogDialog.title=Save Log? diff --git a/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java b/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java index 2adeaef..a694a1a 100644 --- a/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java +++ b/src/com/sneed/pkrandom/newgui/NewRandomizerGUI.java @@ -5,7 +5,7 @@ package com.sneed.pkrandom.newgui; /*-- the various options available and such --*/ /*-- --*/ /*-- Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team --*/ -/*-- Originally part of "Universal Pokemon Randomizer" by sneed --*/ +/*-- Originally part of "Universal Pokemon Randomizer" by sneed --*/ /*-- Pokemon and any associated names and the like are --*/ /*-- trademark and (C) Nintendo 1996-2020. --*/ /*-- --*/ @@ -28,6 +28,7 @@ package com.sneed.pkrandom.newgui; import com.sneed.pkrandom.*; import com.sneed.pkrandom.cli.CliRandomizer; import com.sneed.pkrandom.constants.GlobalConstants; +import com.sneed.pkrandom.exceptions.CannotWriteToLocationException; import com.sneed.pkrandom.exceptions.EncryptedROMException; import com.sneed.pkrandom.exceptions.InvalidSupplementFilesException; import com.sneed.pkrandom.exceptions.RandomizationException; @@ -951,6 +952,11 @@ public class NewRandomizerGUI { if (verboseLog != null) { verboseLog.close(); } + } catch (CannotWriteToLocationException ex) { + JOptionPane.showMessageDialog(mainPanel, String.format(bundle.getString("GUI.cannotWriteToLocation"), filename)); + if (verboseLog != null) { + verboseLog.close(); + } } catch (Exception ex) { attemptToLogException(ex, "GUI.saveFailedIO", "GUI.saveFailedIONoLog", settings.toString(), Long.toString(seed)); if (verboseLog != null) { diff --git a/src/com/sneed/pkrandom/newnds/NDSRom.java b/src/com/sneed/pkrandom/newnds/NDSRom.java index 911a751..1828b10 100755 --- a/src/com/sneed/pkrandom/newnds/NDSRom.java +++ b/src/com/sneed/pkrandom/newnds/NDSRom.java @@ -9,6 +9,7 @@ import com.sneed.pkrandom.SysConstants; import com.sneed.pkrandom.FileFunctions; import com.sneed.pkrandom.RomFunctions; +import com.sneed.pkrandom.exceptions.CannotWriteToLocationException; import com.sneed.pkrandom.exceptions.RandomizerIOException; import cuecompressors.BLZCoder; @@ -17,7 +18,7 @@ import cuecompressors.BLZCoder; /*-- Code based on "Nintendo DS rom tool", copyright (C) DevkitPro --*/ /*-- Original Code by Rafael Vuijk, Dave Murphy, Alexei Karpenko --*/ /*-- --*/ -/*-- Ported to Java by sneed under the terms of the GPL: --*/ +/*-- Ported to Java by sneed under the terms of the GPL: --*/ /*-- --*/ /*-- This program is free software: you can redistribute it and/or modify --*/ /*-- it under the terms of the GNU General Public License as published by --*/ @@ -229,7 +230,11 @@ public class NDSRom { public void saveTo(String filename) throws IOException { this.reopenROM(); - // Initialise new ROM + // Initialize new ROM + File file = new File(filename); + if (!file.canWrite()) { + throw new CannotWriteToLocationException("The randomizer cannot write to this location: " + filename); + } RandomAccessFile fNew = new RandomAccessFile(filename, "rw"); int headersize = readFromFile(this.baseRom, 0x84, 4); diff --git a/src/com/sneed/pkrandom/romhandlers/AbstractGBRomHandler.java b/src/com/sneed/pkrandom/romhandlers/AbstractGBRomHandler.java index 6520d72..6d24f1f 100755 --- a/src/com/sneed/pkrandom/romhandlers/AbstractGBRomHandler.java +++ b/src/com/sneed/pkrandom/romhandlers/AbstractGBRomHandler.java @@ -5,7 +5,7 @@ package com.sneed.pkrandom.romhandlers; /*-- which standardises common GB(A) functions.--*/ /*-- --*/ /*-- Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team --*/ -/*-- Originally part of "Universal Pokemon Randomizer" by sneed --*/ +/*-- Originally part of "Universal Pokemon Randomizer" by sneed --*/ /*-- Pokemon and any associated names and the like are --*/ /*-- trademark and (C) Nintendo 1996-2020. --*/ /*-- --*/ @@ -35,6 +35,7 @@ import java.nio.file.Paths; import java.util.Random; import com.sneed.pkrandom.FileFunctions; +import com.sneed.pkrandom.exceptions.CannotWriteToLocationException; import com.sneed.pkrandom.exceptions.RandomizerIOException; public abstract class AbstractGBRomHandler extends AbstractRomHandler { @@ -70,7 +71,11 @@ public abstract class AbstractGBRomHandler extends AbstractRomHandler { public boolean saveRomFile(String filename, long seed) { savingRom(); try { - FileOutputStream fos = new FileOutputStream(filename); + File file = new File(filename); + if (!file.canWrite()) { + throw new CannotWriteToLocationException("The randomizer cannot write to this location: " + filename); + } + FileOutputStream fos = new FileOutputStream(file); fos.write(rom); fos.close(); return true; -- cgit v1.2.3