Skip to content

Commit 1c27d78

Browse files
committed
Add support for portable settings detection
Introduces logic to detect a preferences.txt file in the same folder as the running executable or jar. If found, settings are loaded from this location, improving portability and allowing users to override default settings without modifying system directories.
1 parent 15588f3 commit 1c27d78

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

app/utils/src/main/java/processing/utils/Settings.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.BufferedReader;
44
import java.io.File;
55
import java.io.InputStreamReader;
6+
import java.util.Optional;
67

78
public class Settings {
89
public static File getFolder() throws SettingsFolderException {
@@ -23,6 +24,11 @@ private static File getFolderForPlatform() throws SettingsFolderException {
2324
return new File(settingsOverride);
2425
}
2526

27+
var portableSettings = FindPortableSettings();
28+
if (portableSettings.isPresent()) {
29+
return portableSettings.get();
30+
}
31+
2632
if (Platform.isWindows()) {
2733
var options = new String[]{
2834
"APPDATA",
@@ -88,6 +94,32 @@ private static File getFolderForPlatform() throws SettingsFolderException {
8894
return new File(System.getProperty("user.home"), ".processing");
8995
}
9096

97+
/**
98+
* find a preferences.txt file in the same folder as the running jar/executable
99+
*
100+
* @return Optional File pointing to preferences.txt if found, empty otherwise
101+
*/
102+
private static Optional<File> FindPortableSettings() {
103+
var command = ProcessHandle.current().info().command();
104+
if (command.isEmpty()) return Optional.empty();
105+
106+
var path = command.get();
107+
path = path.replaceAll("/[^/]+$", "");
108+
109+
if (Platform.isMacOS()) {
110+
// On macOS, the executable is inside the .app bundle, so we need to go up to above the .app folder
111+
path = path.replaceAll("/[^/]+\\.app/.*$", "");
112+
}
113+
var file = new File(path, "preferences.txt");
114+
if (System.getenv().containsKey("DEBUG"))
115+
System.out.println("Looking for portable settings at: " + file.getAbsolutePath());
116+
117+
if (!file.exists()) {
118+
return Optional.empty();
119+
}
120+
return Optional.of(new File(path));
121+
122+
}
91123

92124
public static class SettingsFolderException extends Exception {
93125
public enum Type {

0 commit comments

Comments
 (0)