Skip to content

Commit 382d422

Browse files
committed
feat: add a workarround for #4
1 parent d8220d9 commit 382d422

File tree

4 files changed

+103
-21
lines changed

4 files changed

+103
-21
lines changed

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common_coremod/ConfigManagerStartup.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.thebossmagnus.mods.config_manager.common_coremod;
22

33

4+
45
import java.nio.file.Files;
56
import java.nio.file.Path;
67

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.thebossmagnus.mods.config_manager.common_coremod;
22

33

4+
import io.github.thebossmagnus.mods.config_manager.common_coremod.compat.ModrinthAppCompat;
5+
46
import java.io.IOException;
57
import java.nio.file.Files;
68
import java.nio.file.Path;
@@ -24,31 +26,42 @@ public static void init(Path gameDir) {
2426
throw new RuntimeException(String.format("A subfolder called \"%s\" is inside config/%s: %s", DIR_NAME, DIR_NAME, nestedDir));
2527
}
2628

29+
//Check if there's already an Options.txt generated by the ModrinthApp for the fullscreen setting
30+
int fullscreenSetting = ModrinthAppCompat.getFullScreenSetting(gameDir);
31+
if (fullscreenSetting != -1) {
32+
try {
33+
Files.deleteIfExists(configDir.resolve("options.txt"));
34+
} catch (IOException e) {
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
2739

2840
// Copy contents of root/config/dirName into root
2941
try (var paths = Files.walk(configDir)) {
30-
paths.filter(path -> !path.equals(configDir))
31-
.forEach(source -> {
32-
Path relative = configDir.relativize(source);
33-
Path target = gameDir.resolve(relative);
34-
35-
if (Files.exists(target)) {
36-
return;
37-
}
38-
39-
try {
40-
if (Files.isDirectory(source)) {
41-
Files.createDirectories(target);
42-
} else {
43-
Files.createDirectories(target.getParent());
44-
Files.copy(source, target);
45-
}
46-
} catch (IOException e) {
47-
throw new RuntimeException("Error copying " + source + " to " + target, e);
48-
}
49-
});
42+
paths.filter(path -> !path.equals(configDir)).forEach(source -> {
43+
Path relative = configDir.relativize(source);
44+
Path target = gameDir.resolve(relative);
45+
46+
if (Files.exists(target)) {
47+
return;
48+
}
49+
50+
try {
51+
if (Files.isDirectory(source)) {
52+
Files.createDirectories(target);
53+
} else {
54+
Files.createDirectories(target.getParent());
55+
Files.copy(source, target);
56+
}
57+
} catch (IOException e) {
58+
throw new RuntimeException("Error copying " + source + " to " + target, e);
59+
}
60+
});
5061
} catch (IOException e) {
5162
throw new RuntimeException("Failed to copy modpack_defaults", e);
5263
}
64+
65+
ModrinthAppCompat.reapplyFullScreenSetting(gameDir, fullscreenSetting);
5366
}
5467
}

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common_coremod/OverwriteConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
public final class OverwriteConfig {
1313

1414

15-
1615
public static void run(Path gameDir) {
1716
Path configDir = gameDir.resolve("config");
1817
Path defaultsDir = configDir.resolve(Constants.DIR_NAME);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.github.thebossmagnus.mods.config_manager.common_coremod.compat;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
8+
import static io.github.thebossmagnus.mods.config_manager.common.Constants.LOGGER;
9+
10+
/*
11+
In the Modrinth app, enabling instance options under window size enable Fullscreen.
12+
This will cause Modrinth to generate an options.txt file with Fullscreen enabled which doesn't get overridden by Config Manager.
13+
Fixing this requires a slightly hacky workaround.
14+
The launcher option will override the pack preference (only for the full screen obv)
15+
To do that we first check if the file, that only "fullscreen:..." exits or not (-1), than we save the status of fullscreen (0 or 1),
16+
delete the file, copy and later reapply it after the copy
17+
*/
18+
public final class ModrinthAppCompat {
19+
20+
21+
public static int getFullScreenSetting(@NotNull Path gameDir) {
22+
23+
//check if the file content equals fullscreen:true
24+
if (Files.exists(gameDir.resolve("options.txt"))) {
25+
try {
26+
String content = Files.readString(gameDir.resolve("options.txt"));
27+
if (content.equals("fullscreen:true")) {
28+
LOGGER.info("Detected fullscreen preference imposed by the launcher: true, Merging it with modpack preference");
29+
return 1;
30+
} else if (content.equals("fullscreen:false")) {
31+
LOGGER.info("Detected fullscreen preference imposed by the launcher: false, Merging it with modpack preference");
32+
return 0;
33+
}
34+
} catch (Exception e) {
35+
return -1;
36+
}
37+
}
38+
39+
return -1;
40+
}
41+
42+
public static void reapplyFullScreenSetting(@NotNull Path gameDir, int status) {
43+
if (status == -1) {
44+
return;
45+
}
46+
String fullscreenSetting = (status == 1) ? "fullscreen:true" : "fullscreen:false";
47+
48+
try {
49+
Path optionsFile = gameDir.resolve("options.txt");
50+
java.util.List<String> lines = Files.readAllLines(optionsFile);
51+
boolean replaced = false;
52+
for (int i = 0; i < lines.size(); i++) {
53+
if (lines.get(i).startsWith("fullscreen:")) {
54+
lines.set(i, fullscreenSetting);
55+
replaced = true;
56+
break;
57+
}
58+
}
59+
if (!replaced) {
60+
lines.add(fullscreenSetting);
61+
}
62+
Path tmp = Files.createTempFile(gameDir, "options", ".tmp");
63+
Files.write(tmp, lines);
64+
java.nio.file.Files.move(tmp, optionsFile, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
65+
} catch (Exception e) {
66+
throw new RuntimeException("Could not reapply fullscreen preference chosen by the launcher", e);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)