Skip to content

Commit fe98759

Browse files
committed
use dev.dirs to calculate terasology directories
the library works according to the XDG standard, and offers translations for linux, macosx, windows. see for details: https://github.com/dirs-dev/directories-jvm
1 parent bceffdd commit fe98759

File tree

4 files changed

+19
-47
lines changed

4 files changed

+19
-47
lines changed

engine/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ dependencies {
6161
api("com.google.guava:guava:31.0-jre")
6262
api("com.google.code.gson:gson:2.8.6")
6363
api("net.sf.trove4j:trove4j:3.0.3")
64+
implementation("dev.dirs:directories:26")
6465
implementation("io.netty:netty-all:4.1.77.Final")
6566
implementation("com.google.protobuf:protobuf-java:3.22.0")
6667
implementation("org.lz4:lz4-java:1.8.0")

engine/src/main/java/org/terasology/engine/core/PathManager.java

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
package org.terasology.engine.core;
55

66
import com.google.common.collect.ImmutableList;
7-
import com.sun.jna.platform.win32.KnownFolders;
8-
import com.sun.jna.platform.win32.Shell32Util;
7+
import dev.dirs.ProjectDirectories;
98
import org.slf4j.Logger;
109
import org.slf4j.LoggerFactory;
1110
import org.terasology.engine.context.Context;
@@ -33,15 +32,14 @@
3332
*/
3433
public final class PathManager {
3534
private static final Logger LOGGER = LoggerFactory.getLogger(PathManager.class);
36-
private static final String TERASOLOGY_FOLDER_NAME = "Terasology";
37-
private static final Path LINUX_HOME_SUBPATH = Paths.get(".local", "share", "terasology");
38-
35+
private static final ProjectDirectories projDirs = ProjectDirectories.from("org", "terasology", "terasology");
36+
private static final Path PROJECT_PATH = Paths.get(projDirs.dataDir);
3937
private static final String SAVED_GAMES_DIR = "saves";
4038
private static final String RECORDINGS_LIBRARY_DIR = "recordings";
41-
private static final String LOG_DIR = "logs";
42-
private static final String SHADER_LOG_DIR = "shaders";
39+
private static final String LOG_DIR = projDirs.dataLocalDir + "/logs";
40+
private static final String SHADER_LOG_DIR = projDirs.dataLocalDir + "/shaders";
4341
private static final String MODULE_DIR = "modules";
44-
private static final String MODULE_CACHE_DIR = "cachedModules";
42+
private static final String MODULE_CACHE_DIR = projDirs.cacheDir + "/cachedModules";
4543
private static final String SCREENSHOT_DIR = "screenshots";
4644
private static final String NATIVES_DIR = "natives";
4745
private static final String CONFIGS_DIR = "configs";
@@ -67,7 +65,7 @@ public final class PathManager {
6765

6866
private PathManager() {
6967
installPath = findInstallPath();
70-
homePath = installPath;
68+
homePath = PROJECT_PATH;
7169
}
7270

7371
private static Path findInstallPath() {
@@ -159,7 +157,11 @@ static PathManager setInstance(PathManager pathManager) {
159157
}
160158

161159
/**
162-
* Uses the given path as the home instead of the default home path.
160+
* Uses the given path as the home instead of the default home path. Especially interesting for unit tests, as java>17 does not
161+
* make it easy to set environment variables. see: https://www.baeldung.com/java-unit-testing-environment-variables .
162+
*
163+
* Currently LOG_DIR and LOG_SHADER_DIR are not affected here, as not based on homePath.
164+
*
163165
* @param rootPath Path to use as the home path.
164166
* @throws IOException Thrown when required directories cannot be accessed.
165167
*/
@@ -173,33 +175,8 @@ public void useOverrideHomePath(Path rootPath) throws IOException {
173175
* @throws IOException Thrown when required directories cannot be accessed.
174176
*/
175177
public void useDefaultHomePath() throws IOException {
176-
switch (OS.get()) {
177-
case LINUX:
178-
homePath = Paths.get(System.getProperty("user.home")).resolve(LINUX_HOME_SUBPATH);
179-
break;
180-
case MACOSX:
181-
homePath = Paths.get(System.getProperty("user.home"), "Library", "Application Support", TERASOLOGY_FOLDER_NAME);
182-
break;
183-
case WINDOWS:
184-
String savedGamesPath = Shell32Util
185-
.getKnownFolderPath(KnownFolders.FOLDERID_SavedGames);
186-
if (savedGamesPath == null) {
187-
savedGamesPath = Shell32Util
188-
.getKnownFolderPath(KnownFolders.FOLDERID_Documents);
189-
}
190-
Path rawPath;
191-
if (savedGamesPath != null) {
192-
rawPath = Paths.get(savedGamesPath);
193-
} else {
194-
rawPath = new JFileChooser().getFileSystemView().getDefaultDirectory()
195-
.toPath();
196-
}
197-
homePath = rawPath.resolve(TERASOLOGY_FOLDER_NAME);
198-
break;
199-
default:
200-
homePath = Paths.get(System.getProperty("user.home")).resolve(LINUX_HOME_SUBPATH);
201-
break;
202-
}
178+
// use datadir, .local/share for linux e.g.
179+
homePath = PROJECT_PATH;
203180
updateDirs();
204181
}
205182

@@ -316,8 +293,8 @@ public Path getSandboxPath() {
316293
private void updateDirs() throws IOException {
317294
savesPath = homePath.resolve(SAVED_GAMES_DIR);
318295
recordingsPath = homePath.resolve(RECORDINGS_LIBRARY_DIR);
319-
logPath = homePath.resolve(LOG_DIR);
320-
shaderLogPath = logPath.resolve(SHADER_LOG_DIR);
296+
logPath = Paths.get(LOG_DIR);
297+
shaderLogPath = Paths.get(SHADER_LOG_DIR);
321298
screenshotPath = homePath.resolve(SCREENSHOT_DIR);
322299
nativesPath = installPath.resolve(NATIVES_DIR);
323300
configsPath = homePath.resolve(CONFIGS_DIR);

engine/src/main/java/org/terasology/engine/core/TerasologyEngine.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ private void logEnvironmentInfo() {
270270
logger.info("{}", TerasologyVersion.getInstance());
271271
logger.info("Home path: {}", PathManager.getInstance().getHomePath());
272272
logger.info("Install path: {}", PathManager.getInstance().getInstallPath());
273+
logger.info("Log path: {}", PathManager.getInstance().getLogPath());
273274
logger.info("Java: {} in {}", System.getProperty("java.version"), System.getProperty("java.home"));
274275
logger.info("Java VM: {}, version: {}", System.getProperty("java.vm.name"), System.getProperty("java.vm" +
275276
".version"));

facades/PC/src/main/java/org/terasology/engine/Terasology.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,7 @@ private void handleLaunchArguments() throws IOException {
274274
setMemoryLimit(maxDataSize);
275275
}
276276

277-
if (homeDir != null) {
278-
logger.info("homeDir is {}", homeDir);
279-
PathManager.getInstance().useOverrideHomePath(homeDir);
280-
// TODO: what is this?
281-
// PathManager.getInstance().chooseHomePathManually();
282-
} else {
283-
PathManager.getInstance().useDefaultHomePath();
284-
}
277+
PathManager.getInstance().useDefaultHomePath();
285278

286279
if (isHeadless) {
287280
crashReportEnabled = false;

0 commit comments

Comments
 (0)