From b5ee1519140ec4159ba61ace6051d88c132f9a21 Mon Sep 17 00:00:00 2001 From: Sladyn Date: Thu, 9 Jan 2020 19:22:23 +0530 Subject: [PATCH 1/4] Added manual path chooser --- .../terasology/engine/paths/PathManager.java | 11 ++++++++--- .../org/terasology/engine/Terasology.java | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/engine/src/main/java/org/terasology/engine/paths/PathManager.java b/engine/src/main/java/org/terasology/engine/paths/PathManager.java index 01b11ad4047..af48ed78ec0 100644 --- a/engine/src/main/java/org/terasology/engine/paths/PathManager.java +++ b/engine/src/main/java/org/terasology/engine/paths/PathManager.java @@ -151,7 +151,7 @@ public static PathManager getInstance() { * @param rootPath Path to use as the home path. * @throws IOException Thrown when required directories cannot be accessed. */ - public void useOverrideHomePath(Path rootPath) throws IOException { + public void useOverrideHomePath(Path rootPath) throws Exception { this.homePath = rootPath; updateDirs(); } @@ -160,7 +160,7 @@ public void useOverrideHomePath(Path rootPath) throws IOException { * Uses a platform-specific default home path for this execution. * @throws IOException Thrown when required directories cannot be accessed. */ - public void useDefaultHomePath() throws IOException { + public void useDefaultHomePath() throws Exception { switch (LWJGLUtil.getPlatform()) { case LWJGLUtil.PLATFORM_LINUX: homePath = Paths.get(System.getProperty("user.home")).resolve(LINUX_HOME_SUBPATH); @@ -188,6 +188,11 @@ public void useDefaultHomePath() throws IOException { updateDirs(); } + public void chooseHomePathManually(Path manualPath) throws Exception { + this.homePath = manualPath; + updateDirs(); + } + /** * * @return This execution's home path. @@ -280,7 +285,7 @@ public Path getSandboxPath() { * Updates all of the path manager's file/directory references to match the path settings. Creates directories if they don't already exist. * @throws IOException Thrown when required directories cannot be accessed. */ - private void updateDirs() throws IOException { + private void updateDirs() throws Exception { Files.createDirectories(homePath); savesPath = homePath.resolve(SAVED_GAMES_DIR); Files.createDirectories(savesPath); diff --git a/facades/PC/src/main/java/org/terasology/engine/Terasology.java b/facades/PC/src/main/java/org/terasology/engine/Terasology.java index 54017b178db..f92c9381abc 100644 --- a/facades/PC/src/main/java/org/terasology/engine/Terasology.java +++ b/facades/PC/src/main/java/org/terasology/engine/Terasology.java @@ -17,6 +17,9 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; +import java.io.File; +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileSystemView; import org.terasology.config.Config; import org.terasology.config.SystemConfig; import org.terasology.crashreporter.CrashReporter; @@ -361,8 +364,22 @@ private static void handleLaunchArguments(String[] args) { PathManager.getInstance().useDefaultHomePath(); } - } catch (IOException e) { + } catch (Exception e) { reportException(e); + + // This would allow the user to choose the file path manually. + JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); + int returnValue = jfc.showOpenDialog(null); + if (returnValue == JFileChooser.APPROVE_OPTION) { + File selectedFile = jfc.getSelectedFile(); + homePath = Paths.get(selectedFile.getAbsolutePath()); + try { + PathManager.getInstance().chooseHomePathManually(homePath); + } catch (Exception ex) { + reportException(ex); + System.exit(0); + } + } System.exit(0); } } From 55de00b1c71128f805390dfa47cdd607660fa9f8 Mon Sep 17 00:00:00 2001 From: Sladyn Date: Mon, 13 Jan 2020 23:57:38 +0530 Subject: [PATCH 2/4] PathManager.java: Added user prompt on failure due to error in windowsXP. Giving the user a second chance to run the game. Added a headless server condition to set the homepath as current directory. --- .../terasology/engine/paths/PathManager.java | 37 +++++++++++++++---- .../org/terasology/engine/Terasology.java | 24 ++++-------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/engine/src/main/java/org/terasology/engine/paths/PathManager.java b/engine/src/main/java/org/terasology/engine/paths/PathManager.java index af48ed78ec0..d4422c2d412 100644 --- a/engine/src/main/java/org/terasology/engine/paths/PathManager.java +++ b/engine/src/main/java/org/terasology/engine/paths/PathManager.java @@ -32,6 +32,9 @@ import java.util.List; import javax.swing.JFileChooser; +import org.terasology.context.Context; +import org.terasology.engine.subsystem.DisplayDevice; + /** * Manager class that keeps track of the game's various paths and save directories. @@ -52,6 +55,8 @@ public final class PathManager { private static final String REGEX = "[^A-Za-z0-9-_ ]"; private static PathManager instance; + + private static Context context; private Path installPath; private Path homePath; private Path savesPath; @@ -151,7 +156,7 @@ public static PathManager getInstance() { * @param rootPath Path to use as the home path. * @throws IOException Thrown when required directories cannot be accessed. */ - public void useOverrideHomePath(Path rootPath) throws Exception { + public void useOverrideHomePath(Path rootPath) throws IOException { this.homePath = rootPath; updateDirs(); } @@ -160,7 +165,7 @@ public void useOverrideHomePath(Path rootPath) throws Exception { * Uses a platform-specific default home path for this execution. * @throws IOException Thrown when required directories cannot be accessed. */ - public void useDefaultHomePath() throws Exception { + public void useDefaultHomePath() throws IOException { switch (LWJGLUtil.getPlatform()) { case LWJGLUtil.PLATFORM_LINUX: homePath = Paths.get(System.getProperty("user.home")).resolve(LINUX_HOME_SUBPATH); @@ -169,15 +174,18 @@ public void useDefaultHomePath() throws Exception { homePath = Paths.get(System.getProperty("user.home"), "Library", "Application Support", TERASOLOGY_FOLDER_NAME); break; case LWJGLUtil.PLATFORM_WINDOWS: - String savedGamesPath = Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_SavedGames); + String savedGamesPath = Shell32Util + .getKnownFolderPath(KnownFolders.FOLDERID_SavedGames); if (savedGamesPath == null) { - savedGamesPath = Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Documents); + savedGamesPath = Shell32Util + .getKnownFolderPath(KnownFolders.FOLDERID_Documents); } Path rawPath; if (savedGamesPath != null) { rawPath = Paths.get(savedGamesPath); } else { - rawPath = new JFileChooser().getFileSystemView().getDefaultDirectory().toPath(); + rawPath = new JFileChooser().getFileSystemView().getDefaultDirectory() + .toPath(); } homePath = rawPath.resolve(TERASOLOGY_FOLDER_NAME); break; @@ -188,8 +196,21 @@ public void useDefaultHomePath() throws Exception { updateDirs(); } - public void chooseHomePathManually(Path manualPath) throws Exception { - this.homePath = manualPath; + /** + * Gives user the option to manually choose home path. + * @throws IOException Thrown when required directories cannot be accessed. + */ + public void chooseHomePathManually() throws IOException { + DisplayDevice display = context.get(DisplayDevice.class); + boolean isHeadless = display.isHeadless(); + if (!isHeadless) { + Path rawPath = new JFileChooser().getFileSystemView().getDefaultDirectory() + .toPath(); + homePath = rawPath.resolve("Terasology"); + } else { + // If the system is headless + homePath = Paths.get("").toAbsolutePath(); + } updateDirs(); } @@ -285,7 +306,7 @@ public Path getSandboxPath() { * Updates all of the path manager's file/directory references to match the path settings. Creates directories if they don't already exist. * @throws IOException Thrown when required directories cannot be accessed. */ - private void updateDirs() throws Exception { + private void updateDirs() throws IOException { Files.createDirectories(homePath); savesPath = homePath.resolve(SAVED_GAMES_DIR); Files.createDirectories(savesPath); diff --git a/facades/PC/src/main/java/org/terasology/engine/Terasology.java b/facades/PC/src/main/java/org/terasology/engine/Terasology.java index f92c9381abc..06da6e468f4 100644 --- a/facades/PC/src/main/java/org/terasology/engine/Terasology.java +++ b/facades/PC/src/main/java/org/terasology/engine/Terasology.java @@ -17,9 +17,6 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; -import java.io.File; -import javax.swing.JFileChooser; -import javax.swing.filechooser.FileSystemView; import org.terasology.config.Config; import org.terasology.config.SystemConfig; import org.terasology.crashreporter.CrashReporter; @@ -117,6 +114,7 @@ public final class Terasology { private static boolean splashEnabled = true; private static boolean loadLastGame; + private Terasology() { } @@ -364,21 +362,13 @@ private static void handleLaunchArguments(String[] args) { PathManager.getInstance().useDefaultHomePath(); } - } catch (Exception e) { + } catch (IOException e) { reportException(e); - - // This would allow the user to choose the file path manually. - JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); - int returnValue = jfc.showOpenDialog(null); - if (returnValue == JFileChooser.APPROVE_OPTION) { - File selectedFile = jfc.getSelectedFile(); - homePath = Paths.get(selectedFile.getAbsolutePath()); - try { - PathManager.getInstance().chooseHomePathManually(homePath); - } catch (Exception ex) { - reportException(ex); - System.exit(0); - } + try { + PathManager.getInstance().chooseHomePathManually(); + } catch (IOException ex) { + reportException(e); + System.exit(0); } System.exit(0); } From 2dce469b1d75e8e1c675430c1094344500132c26 Mon Sep 17 00:00:00 2001 From: Sladyn Date: Sat, 18 Jan 2020 18:54:16 +0530 Subject: [PATCH 3/4] Corrected typos --- facades/PC/src/main/java/org/terasology/engine/Terasology.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/facades/PC/src/main/java/org/terasology/engine/Terasology.java b/facades/PC/src/main/java/org/terasology/engine/Terasology.java index 06da6e468f4..271cf7d8bfd 100644 --- a/facades/PC/src/main/java/org/terasology/engine/Terasology.java +++ b/facades/PC/src/main/java/org/terasology/engine/Terasology.java @@ -367,10 +367,9 @@ private static void handleLaunchArguments(String[] args) { try { PathManager.getInstance().chooseHomePathManually(); } catch (IOException ex) { - reportException(e); + reportException(ex); System.exit(0); } - System.exit(0); } } From f6874fe045f76c5d5c617b6cda0ad2c045294b4d Mon Sep 17 00:00:00 2001 From: Sladyn Date: Tue, 21 Jan 2020 20:32:40 +0530 Subject: [PATCH 4/4] Added logger --- .../PC/src/main/java/org/terasology/engine/Terasology.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/facades/PC/src/main/java/org/terasology/engine/Terasology.java b/facades/PC/src/main/java/org/terasology/engine/Terasology.java index 271cf7d8bfd..9924035d316 100644 --- a/facades/PC/src/main/java/org/terasology/engine/Terasology.java +++ b/facades/PC/src/main/java/org/terasology/engine/Terasology.java @@ -17,6 +17,8 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.terasology.config.Config; import org.terasology.config.SystemConfig; import org.terasology.crashreporter.CrashReporter; @@ -41,6 +43,7 @@ import org.terasology.engine.subsystem.openvr.OpenVRInput; import org.terasology.engine.subsystem.rpc.DiscordRPCSubSystem; import org.terasology.game.GameManifest; +import org.terasology.logic.characters.CharacterSystem; import org.terasology.network.NetworkMode; import org.terasology.rendering.nui.layers.mainMenu.savedGames.GameInfo; import org.terasology.rendering.nui.layers.mainMenu.savedGames.GameProvider; @@ -107,6 +110,8 @@ public final class Terasology { private static final String NO_SPLASH = "-noSplash"; private static final String SERVER_PORT = "-serverPort="; private static final String OVERRIDE_DEFAULT_CONFIG = "-overrideDefaultConfig="; + private static final Logger logger = LoggerFactory.getLogger(Terasology.class); + private static boolean isHeadless; private static boolean crashReportEnabled = true; @@ -363,7 +368,7 @@ private static void handleLaunchArguments(String[] args) { } } catch (IOException e) { - reportException(e); + logger.warn("The game cannot detect default home directory"); try { PathManager.getInstance().chooseHomePathManually(); } catch (IOException ex) {