From 229347ec342f63b722f5ecb487feaea7b029d940 Mon Sep 17 00:00:00 2001 From: soloturn Date: Sat, 12 Oct 2024 12:33:18 +0200 Subject: [PATCH] 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 --- engine/build.gradle.kts | 1 + .../terasology/engine/core/PathManager.java | 52 +++++-------------- .../engine/core/TerasologyEngine.java | 1 + .../org/terasology/engine/Terasology.java | 9 +--- 4 files changed, 16 insertions(+), 47 deletions(-) diff --git a/engine/build.gradle.kts b/engine/build.gradle.kts index 6a5a1d923c2..730f1556fa2 100644 --- a/engine/build.gradle.kts +++ b/engine/build.gradle.kts @@ -61,6 +61,7 @@ dependencies { api("com.google.guava:guava:31.0-jre") api("com.google.code.gson:gson:2.8.6") api("net.sf.trove4j:trove4j:3.0.3") + implementation("dev.dirs:directories:26") implementation("io.netty:netty-all:4.1.77.Final") implementation("com.google.protobuf:protobuf-java:3.22.0") implementation("org.lz4:lz4-java:1.8.0") diff --git a/engine/src/main/java/org/terasology/engine/core/PathManager.java b/engine/src/main/java/org/terasology/engine/core/PathManager.java index eaaa8b428ad..3981eceb0df 100644 --- a/engine/src/main/java/org/terasology/engine/core/PathManager.java +++ b/engine/src/main/java/org/terasology/engine/core/PathManager.java @@ -4,8 +4,7 @@ package org.terasology.engine.core; import com.google.common.collect.ImmutableList; -import com.sun.jna.platform.win32.KnownFolders; -import com.sun.jna.platform.win32.Shell32Util; +import dev.dirs.ProjectDirectories; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.terasology.engine.context.Context; @@ -33,15 +32,14 @@ */ public final class PathManager { private static final Logger LOGGER = LoggerFactory.getLogger(PathManager.class); - private static final String TERASOLOGY_FOLDER_NAME = "Terasology"; - private static final Path LINUX_HOME_SUBPATH = Paths.get(".local", "share", "terasology"); - + private static final ProjectDirectories projDirs = ProjectDirectories.from("org", "terasology", "terasology"); + private static final Path PROJECT_PATH = Paths.get(projDirs.dataDir); private static final String SAVED_GAMES_DIR = "saves"; private static final String RECORDINGS_LIBRARY_DIR = "recordings"; - private static final String LOG_DIR = "logs"; - private static final String SHADER_LOG_DIR = "shaders"; + private static final String LOG_DIR = projDirs.dataLocalDir + "/logs"; + private static final String SHADER_LOG_DIR = projDirs.dataLocalDir + "/shaders"; private static final String MODULE_DIR = "modules"; - private static final String MODULE_CACHE_DIR = "cachedModules"; + private static final String MODULE_CACHE_DIR = projDirs.cacheDir + "/cachedModules"; private static final String SCREENSHOT_DIR = "screenshots"; private static final String NATIVES_DIR = "natives"; private static final String CONFIGS_DIR = "configs"; @@ -67,7 +65,7 @@ public final class PathManager { private PathManager() { installPath = findInstallPath(); - homePath = installPath; + homePath = PROJECT_PATH; } private static Path findInstallPath() { @@ -159,7 +157,8 @@ static PathManager setInstance(PathManager pathManager) { } /** - * Uses the given path as the home instead of the default home path. + * Uses the given path as the home instead of the default home path. Especially interesting for unit tests, as java>17 does not + * make it easy to set environment variables. see: https://www.baeldung.com/java-unit-testing-environment-variables . * @param rootPath Path to use as the home path. * @throws IOException Thrown when required directories cannot be accessed. */ @@ -173,33 +172,8 @@ public void useOverrideHomePath(Path rootPath) throws IOException { * @throws IOException Thrown when required directories cannot be accessed. */ public void useDefaultHomePath() throws IOException { - switch (OS.get()) { - case LINUX: - homePath = Paths.get(System.getProperty("user.home")).resolve(LINUX_HOME_SUBPATH); - break; - case MACOSX: - homePath = Paths.get(System.getProperty("user.home"), "Library", "Application Support", TERASOLOGY_FOLDER_NAME); - break; - case WINDOWS: - String savedGamesPath = Shell32Util - .getKnownFolderPath(KnownFolders.FOLDERID_SavedGames); - if (savedGamesPath == null) { - savedGamesPath = Shell32Util - .getKnownFolderPath(KnownFolders.FOLDERID_Documents); - } - Path rawPath; - if (savedGamesPath != null) { - rawPath = Paths.get(savedGamesPath); - } else { - rawPath = new JFileChooser().getFileSystemView().getDefaultDirectory() - .toPath(); - } - homePath = rawPath.resolve(TERASOLOGY_FOLDER_NAME); - break; - default: - homePath = Paths.get(System.getProperty("user.home")).resolve(LINUX_HOME_SUBPATH); - break; - } + // use datadir, .local/share for linux e.g. + homePath = PROJECT_PATH; updateDirs(); } @@ -316,8 +290,8 @@ public Path getSandboxPath() { private void updateDirs() throws IOException { savesPath = homePath.resolve(SAVED_GAMES_DIR); recordingsPath = homePath.resolve(RECORDINGS_LIBRARY_DIR); - logPath = homePath.resolve(LOG_DIR); - shaderLogPath = logPath.resolve(SHADER_LOG_DIR); + logPath = Paths.get(LOG_DIR); + shaderLogPath = Paths.get(SHADER_LOG_DIR); screenshotPath = homePath.resolve(SCREENSHOT_DIR); nativesPath = installPath.resolve(NATIVES_DIR); configsPath = homePath.resolve(CONFIGS_DIR); diff --git a/engine/src/main/java/org/terasology/engine/core/TerasologyEngine.java b/engine/src/main/java/org/terasology/engine/core/TerasologyEngine.java index 9b704e6ffb7..8a0391c4e71 100644 --- a/engine/src/main/java/org/terasology/engine/core/TerasologyEngine.java +++ b/engine/src/main/java/org/terasology/engine/core/TerasologyEngine.java @@ -270,6 +270,7 @@ private void logEnvironmentInfo() { logger.info("{}", TerasologyVersion.getInstance()); logger.info("Home path: {}", PathManager.getInstance().getHomePath()); logger.info("Install path: {}", PathManager.getInstance().getInstallPath()); + logger.info("Log path: {}", PathManager.getInstance().getLogPath()); logger.info("Java: {} in {}", System.getProperty("java.version"), System.getProperty("java.home")); logger.info("Java VM: {}, version: {}", System.getProperty("java.vm.name"), System.getProperty("java.vm" + ".version")); 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 215862b7290..271083b79e4 100644 --- a/facades/PC/src/main/java/org/terasology/engine/Terasology.java +++ b/facades/PC/src/main/java/org/terasology/engine/Terasology.java @@ -274,14 +274,7 @@ private void handleLaunchArguments() throws IOException { setMemoryLimit(maxDataSize); } - if (homeDir != null) { - logger.info("homeDir is {}", homeDir); - PathManager.getInstance().useOverrideHomePath(homeDir); - // TODO: what is this? - // PathManager.getInstance().chooseHomePathManually(); - } else { - PathManager.getInstance().useDefaultHomePath(); - } + PathManager.getInstance().useDefaultHomePath(); if (isHeadless) { crashReportEnabled = false;