Skip to content

Commit

Permalink
Merge pull request #4262 from cwisniew/fix-block-campaign-install-dir
Browse files Browse the repository at this point in the history
Block loading/saving campaign from/to installation dir
  • Loading branch information
cwisniew authored Aug 30, 2023
2 parents 39a3f65 + be2ac33 commit c527a50
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 33 deletions.
81 changes: 63 additions & 18 deletions src/main/java/net/rptools/maptool/client/AppActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,19 @@ protected void executeAction() {

JFileChooser chooser = MapTool.getFrame().getSaveFileChooser();

// Get target location
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
boolean tryAgain = true;
while (tryAgain) {
// Get target location
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.exportRepoToInstallDir");
} else {
tryAgain = false;
}
}

// Default extension
Expand Down Expand Up @@ -2546,6 +2556,12 @@ public static void loadCampaign(final File campaignFile) {
return;
}

var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var openDir = campaignFile.toPath().getParent().toAbsolutePath();
if (openDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.loadCampaignFromInstallDir");
}

new CampaignLoader(campaignFile).execute();
}

Expand Down Expand Up @@ -2692,6 +2708,13 @@ public static void doSaveCampaign(Runnable onSuccess) {
doSaveCampaignAs(onSuccess);
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = AppState.getCampaignFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveCampaignToInstallDir");
doSaveCampaignAs(onSuccess);
return;
}
doSaveCampaign(AppState.getCampaignFile(), onSuccess);
}

Expand Down Expand Up @@ -2763,10 +2786,22 @@ protected void done() {
}

public static void doSaveCampaignAs(Runnable onSuccess) {
JFileChooser chooser = MapTool.getFrame().getSaveCmpgnFileChooser();
int saveStatus = chooser.showSaveDialog(MapTool.getFrame());
if (saveStatus == JFileChooser.APPROVE_OPTION) {
saveAndUpdateCampaignName(chooser.getSelectedFile(), onSuccess);
boolean tryAgain = true;
while (tryAgain) {
JFileChooser chooser = MapTool.getFrame().getSaveCmpgnFileChooser();
int saveStatus = chooser.showSaveDialog(MapTool.getFrame());
if (saveStatus == JFileChooser.APPROVE_OPTION) {
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveCampaignToInstallDir");
} else {
tryAgain = false;
saveAndUpdateCampaignName(chooser.getSelectedFile(), onSuccess);
}
} else {
tryAgain = false;
}
}
}

Expand Down Expand Up @@ -2811,20 +2846,30 @@ protected void executeAction() {
chooser.setFileFilter(MapTool.getFrame().getMapFileFilter());
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setSelectedFile(new File(zr.getZone().getName()));
if (chooser.showSaveDialog(MapTool.getFrame()) == JFileChooser.APPROVE_OPTION) {
try {
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) == JFileChooser.APPROVE_OPTION) {
File mapFile = chooser.getSelectedFile();
mapFile = getFileWithExtension(mapFile, AppConstants.MAP_FILE_EXTENSION);
if (mapFile.exists()) {
if (!MapTool.confirm("msg.confirm.fileExists")) {
return;
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveMapToInstallDir");
} else {
tryAgain = false;
try {
mapFile = getFileWithExtension(mapFile, AppConstants.MAP_FILE_EXTENSION);
if (mapFile.exists()) {
if (!MapTool.confirm("msg.confirm.fileExists")) {
return;
}
}
PersistenceUtil.saveMap(zr.getZone(), mapFile);
AppPreferences.setSaveMapDir(mapFile.getParentFile());
MapTool.showInformation("msg.info.mapSaved");
} catch (IOException ioe) {
MapTool.showError("msg.error.failedSaveMap", ioe);
}
}
PersistenceUtil.saveMap(zr.getZone(), mapFile);
AppPreferences.setSaveMapDir(mapFile.getParentFile());
MapTool.showInformation("msg.info.mapSaved");
} catch (IOException ioe) {
MapTool.showError("msg.error.failedSaveMap", ioe);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/net/rptools/maptool/client/AppUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,34 @@ public static String getAppInstallLocation() {
return path;
}

/**
* This function tries to determine the installation directory of the application. This can differ
* from the directory the application is running from as returned by {@link
* #getAppInstallLocation()}.
*
* @return the installation directory of the application.
*/
public static Path getInstallDirectory() {
var path = Path.of(getAppInstallLocation());
if (MapTool.isDevelopment()) {
// remove build/classes/java
path = path.getParent().getParent().getParent().getParent();
} else {
while (path != null) {
if (path.getFileName().toString().matches("(?i).*maptool.*")) {
path = path.getParent();
break;
}
path = path.getParent();
}
}
if (path == null) {
return Path.of(getAppInstallLocation());
} else {
return path;
}
}

/**
* Returns a File path representing the configuration file in the base directory that the
* application is running from. e.g. C:\Users\Troll\AppData\Local\MapTool\app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,43 @@ public void actionPerformed(ActionEvent e) {
if (showSaveDialog) {
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}

saveDirectory = chooser.getSelectedFile();
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveTokenToInstallDir");
} else {
tryAgain = false;
}
}

tokenSaveFile = chooser.getSelectedFile();
} else {
if (saveDirectory == null) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) return;
if (chooser.getFileFilter() == tokenFilterGM) saveAsGmName = true;
saveDirectory = chooser.getSelectedFile();
boolean tryAgain = true;
while (tryAgain) {
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
if (chooser.getFileFilter() == tokenFilterGM) {
saveAsGmName = true;
}
saveDirectory = chooser.getSelectedFile();
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveTokenToInstallDir");
} else {
tryAgain = false;
}
}
}

if (saveAsGmName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.swing.*;
import net.rptools.lib.FileUtil;
import net.rptools.maptool.client.AppConstants;
import net.rptools.maptool.client.AppUtil;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.swing.AbeillePanel;
import net.rptools.maptool.client.swing.SwingUtil;
Expand Down Expand Up @@ -904,7 +905,19 @@ private void initExportButton() {
// END HACK

JFileChooser chooser = MapTool.getFrame().getSavePropsFileChooser();
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) return;
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.savePropToInstallDir");
} else {
tryAgain = false;
}
}

File selectedFile = chooser.getSelectedFile();
if (selectedFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import net.rptools.maptool.client.AppUtil;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.swing.AbeillePanel;
import net.rptools.maptool.client.swing.ImagePanel;
Expand Down Expand Up @@ -277,8 +278,18 @@ public void initExportButton() {
e -> {
JFileChooser chooser = MapTool.getFrame().getSaveTableFileChooser();

if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveTableToInstallDir");
} else {
tryAgain = false;
}
}
final File selectedFile = chooser.getSelectedFile();
EventQueue.invokeLater(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,18 @@ public void actionPerformed(ActionEvent event) {

JFileChooser chooser = MapTool.getFrame().getSaveMacroSetFileChooser();

if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveMacrosToInstallDir");
} else {
tryAgain = false;
}
}

final File selectedFile = chooser.getSelectedFile();
Expand Down Expand Up @@ -632,8 +642,18 @@ public void actionPerformed(ActionEvent event) {

JFileChooser chooser = MapTool.getFrame().getSaveMacroSetFileChooser();

if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveMacrosToInstallDir");
} else {
tryAgain = false;
}
}

final File selectedFile = chooser.getSelectedFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,18 @@ private ExportMacroAction(String name) {
public void actionPerformed(ActionEvent event) {
JFileChooser chooser = MapTool.getFrame().getSaveMacroFileChooser();

if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
boolean tryAgain = true;
while (tryAgain) {
if (chooser.showSaveDialog(MapTool.getFrame()) != JFileChooser.APPROVE_OPTION) {
return;
}
var installDir = AppUtil.getInstallDirectory().toAbsolutePath();
var saveDir = chooser.getSelectedFile().toPath().getParent().toAbsolutePath();
if (saveDir.startsWith(installDir)) {
MapTool.showWarning("msg.warning.saveMacrosToInstallDir");
} else {
tryAgain = false;
}
}

final File selectedFile =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,15 @@ msg.warn.failedAutoSavingMessageHistory = Could not autosave message histo
msg.warning.macro.playerChangesNotAllowed = The GM has not allowed players to change this macro.
msg.warning.macro.willNotExport = The macro "{0}" will not be exported. Either it has been flagged by the GM as not player editable or you do not have ownership privileges over the source.</body></html>
msg.warning.prerelease.only = Warning {0} is for pre-release testing only and may not be available in the actual release.
msg.warning.loadCampaignFromInstallDir = Saving to the installation directory is not supported. After loading you will need to save to a different location.
msg.warning.saveCampaignToInstallDir = Saving to the installation directory is not supported, please choose a different location to save to.
msg.warning.exportRepoToInstallDir = Exporting Repository to the installation directory is not supported, please choose a different location to save to.
msg.warning.saveTokenToInstallDir = Saving Tokens to the installation directory is not supported, please choose a different location to save to.
msg.warning.saveMapToInstallDir = Saving Maps to the installation directory is not supported, please choose a different location to save to.
msg.warning.saveTableToInstallDir = Saving Tables to the installation directory is not supported, please choose a different location to save to.
msg.warning.savePropToInstallDir = Saving Campaign Properties to the installation directory is not supported, please choose a different location to save to.
msg.warning.saveMacrosToInstallDir = Saving Macros to the installation directory is not supported, please choose a different location to save to.

msg.error.serverOnly = Function {0} can only be used on the server.

msg.asset.error.invalidAsset = An invalid asset has been selected. If you get this message please report it on the MapTool forums or Discord server.
Expand Down

0 comments on commit c527a50

Please sign in to comment.