4
4
package org .terasology .engine .core ;
5
5
6
6
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 ;
9
8
import org .slf4j .Logger ;
10
9
import org .slf4j .LoggerFactory ;
11
10
import org .terasology .engine .context .Context ;
33
32
*/
34
33
public final class PathManager {
35
34
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 );
39
37
private static final String SAVED_GAMES_DIR = "saves" ;
40
38
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" ;
43
41
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" ;
45
43
private static final String SCREENSHOT_DIR = "screenshots" ;
46
44
private static final String NATIVES_DIR = "natives" ;
47
45
private static final String CONFIGS_DIR = "configs" ;
@@ -67,7 +65,7 @@ public final class PathManager {
67
65
68
66
private PathManager () {
69
67
installPath = findInstallPath ();
70
- homePath = installPath ;
68
+ homePath = PROJECT_PATH ;
71
69
}
72
70
73
71
private static Path findInstallPath () {
@@ -159,7 +157,11 @@ static PathManager setInstance(PathManager pathManager) {
159
157
}
160
158
161
159
/**
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
+ *
163
165
* @param rootPath Path to use as the home path.
164
166
* @throws IOException Thrown when required directories cannot be accessed.
165
167
*/
@@ -173,33 +175,8 @@ public void useOverrideHomePath(Path rootPath) throws IOException {
173
175
* @throws IOException Thrown when required directories cannot be accessed.
174
176
*/
175
177
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 ;
203
180
updateDirs ();
204
181
}
205
182
@@ -316,8 +293,8 @@ public Path getSandboxPath() {
316
293
private void updateDirs () throws IOException {
317
294
savesPath = homePath .resolve (SAVED_GAMES_DIR );
318
295
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 );
321
298
screenshotPath = homePath .resolve (SCREENSHOT_DIR );
322
299
nativesPath = installPath .resolve (NATIVES_DIR );
323
300
configsPath = homePath .resolve (CONFIGS_DIR );
0 commit comments