Skip to content

Commit

Permalink
Merge pull request #3823 from sladyn98/Fix_path
Browse files Browse the repository at this point in the history
Handle unexpected (old or weird) OSes that may not support our directory detection.
  • Loading branch information
skaldarnar authored Feb 19, 2020
2 parents 3f50a70 + f6874fe commit a9263f7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
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 {
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
15 changes: 13 additions & 2 deletions facades/PC/src/main/java/org/terasology/engine/Terasology.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -107,13 +110,16 @@ 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;
private static boolean soundEnabled = true;
private static boolean splashEnabled = true;
private static boolean loadLastGame;


private Terasology() {
}

Expand Down Expand Up @@ -362,8 +368,13 @@ private static void handleLaunchArguments(String[] args) {
}

} catch (IOException e) {
reportException(e);
System.exit(0);
logger.warn("The game cannot detect default home directory");
try {
PathManager.getInstance().chooseHomePathManually();
} catch (IOException ex) {
reportException(ex);
System.exit(0);
}
}
}

Expand Down

0 comments on commit a9263f7

Please sign in to comment.