Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unexpected (old or weird) OSes that may not support our directory detection. #3823

Merged
merged 4 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions engine/src/main/java/org/terasology/engine/paths/PathManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -169,15 +174,18 @@ public void useDefaultHomePath() throws IOException {
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;
Expand All @@ -188,6 +196,24 @@ public void useDefaultHomePath() throws IOException {
updateDirs();
}

/**
* Gives user the option to manually choose home path.
* @throws IOException Thrown when required directories cannot be accessed.
*/
public void chooseHomePathManually() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should/can this be part of useDefaultHomePath?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be but then it would have to catch the exception inside the useDefaultHomePath, moreover this provides a much modular approach and a generic solution to all further weird OSeS

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

/**
*
* @return This execution's home path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public final class Terasology {
private static boolean splashEnabled = true;
private static boolean loadLastGame;


private Terasology() {
}

Expand Down Expand Up @@ -363,6 +364,12 @@ private static void handleLaunchArguments(String[] args) {

} catch (IOException e) {
reportException(e);
try {
PathManager.getInstance().chooseHomePathManually();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this can recover any exception that was thrown before, there is no need to reportException before trying the backup. At least, it should not be reported as an error (maybe as a warning, or probably just on INFO level).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah , you have a valid point I just thought that it would be good to report an exception without stopping execution. A Warning sounds like a much better approach :)

} catch (IOException ex) {
reportException(e);
sladyn98 marked this conversation as resolved.
Show resolved Hide resolved
System.exit(0);
}
System.exit(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be taken out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should 👍

}
}
Expand Down