Keep admin edits in iris.toml

This commit is contained in:
piexel committed 2026-11-26 13:16:46 +01:00
1 parent 590084e1c6
commit 742c9c22f4
3 files changed
+141 -19

No files matched your search

@@ -126,7 +126,7 @@ public class ModdedPlatformPathsTest {
File iris = new File(temporaryFolder.getRoot(), "iris");
assertEquals(iris, platform.dataFolder());
assertEquals(new File(iris, "iris.json"), platform.dataFile("iris.json"));
assertEquals(new File(iris, "iris.toml"), platform.dataFile("iris.toml"));
assertEquals(new File(iris, "parity"), platform.dataFolder("parity"));
}
@@ -4,12 +4,21 @@ import art.arcane.iris.spi.IrisLogging;
import art.arcane.iris.spi.IrisPlatforms;
import art.arcane.volmlib.util.config.ConfigComments;
import art.arcane.volmlib.util.config.ConfigExposePolicy;
import art.arcane.volmlib.util.config.ConfigJson;
import art.arcane.volmlib.util.config.TomlCodec;
import art.arcane.volmlib.util.config.TomlDocumentEditor;
import art.arcane.volmlib.util.io.AtomicFileIO;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Reads and writes the Iris settings file. Settings live in iris.toml so each key can carry a comment.
@@ -26,8 +35,8 @@ public final class IrisSettingsFile {
}
public static void write(IrisSettings settings, File file) throws IOException {
Files.createDirectories(file.getAbsoluteFile().getParentFile().toPath());
Files.writeString(file.toPath(), render(settings), StandardCharsets.UTF_8);
requireWritable(file);
AtomicFileIO.writeString(file.toPath(), documentWithSettings(file, settings));
}
static IrisSettings load() {
@@ -67,12 +76,109 @@ public final class IrisSettingsFile {
return new IrisSettings();
}
if (!sameContent(raw, render(loaded))) {
if (!containsEverySetting(raw, loaded)) {
rewrite(file, loaded);
}
return loaded;
}
private static boolean containsEverySetting(String raw, IrisSettings loaded) {
try {
JsonObject document = parseDocument(raw);
return settingPaths(settingsTree(loaded)).stream().allMatch(path -> valueAt(document, path) != null);
} catch (IOException unreadable) {
return false;
}
}
private static void requireWritable(File file) throws AccessDeniedException {
if (file.exists() && !file.canWrite()) {
throw new AccessDeniedException(file.getAbsolutePath());
}
}
private static String documentWithSettings(File file, IrisSettings settings) {
if (!file.exists()) {
return render(settings);
}
try {
return applySettings(Files.readString(file.toPath(), StandardCharsets.UTF_8), settingsTree(settings));
} catch (IOException | RuntimeException uneditable) {
return render(settings);
}
}
private static String applySettings(String document, JsonObject settings) throws IOException {
JsonObject written = parseDocument(document);
String updated = document;
for (List<String> path : settingPaths(settings)) {
JsonElement value = valueAt(settings, path);
if (value.equals(valueAt(written, path))) {
continue;
}
updated = TomlDocumentEditor.set(updated, path, value);
}
return updated;
}
/**
* Each table's own settings come out last to first: the editor inserts a missing setting directly below its
* table header, so this order leaves inserted settings in their documented order.
*/
private static List<List<String>> settingPaths(JsonObject table) {
List<List<String>> paths = new ArrayList<>();
collectSettingPaths(table, List.of(), paths);
return paths;
}
private static void collectSettingPaths(JsonObject table, List<String> tablePath, List<List<String>> paths) {
List<List<String>> ownSettings = new ArrayList<>();
List<Map.Entry<String, JsonElement>> childTables = new ArrayList<>();
for (Map.Entry<String, JsonElement> entry : table.entrySet()) {
if (entry.getValue().isJsonObject()) {
childTables.add(entry);
} else {
ownSettings.add(childPath(tablePath, entry.getKey()));
}
}
paths.addAll(ownSettings.reversed());
for (Map.Entry<String, JsonElement> child : childTables) {
collectSettingPaths(child.getValue().getAsJsonObject(), childPath(tablePath, child.getKey()), paths);
}
}
private static List<String> childPath(List<String> parent, String key) {
List<String> path = new ArrayList<>(parent);
path.add(key);
return List.copyOf(path);
}
private static JsonObject settingsTree(IrisSettings settings) {
return ConfigJson.NORMAL.toJsonTree(settings).getAsJsonObject();
}
private static JsonElement valueAt(JsonObject document, List<String> path) {
JsonElement current = document;
for (String key : path) {
if (!current.isJsonObject()) {
return null;
}
current = current.getAsJsonObject().get(key);
if (current == null) {
return null;
}
}
return current;
}
private static JsonObject parseDocument(String toml) throws IOException {
JsonElement parsed = TomlCodec.toJsonElement(toml);
if (parsed == null || !parsed.isJsonObject()) {
throw new IOException("Iris settings did not contain a table");
}
return parsed.getAsJsonObject();
}
private static boolean rewrite(File file, IrisSettings settings) {
try {
write(settings, file);
@@ -84,14 +190,6 @@ public final class IrisSettingsFile {
}
}
private static boolean sameContent(String raw, String canonical) {
return normalizeLineEndings(raw).strip().equals(normalizeLineEndings(canonical).strip());
}
private static String normalizeLineEndings(String text) {
return text.replace("\r\n", "\n");
}
private static String rootCause(Throwable failure) {
Throwable root = failure;
while (root.getCause() != null && root.getCause() != root) {
@@ -89,24 +89,48 @@ public class IrisSettingsFileTest {
}
@Test
public void upToDateFileIsNotRewritten() throws Exception {
Files.writeString(settingsFile.toPath(), IrisSettingsFile.render(new IrisSettings()));
public void fileWithEverySettingIsNotRewritten() throws Exception {
String adminEdited = IrisSettingsFile.render(new IrisSettings())
.replace("debug = false\n", "")
.replace("[general]\n", "[general]\n# Turned on while chasing a bug\ndebug = true\n")
.replace("[world]\n", "[world]\nretiredOption = 3\n")
+ "\n[custom]\nnote = \"kept\"\n";
Files.writeString(settingsFile.toPath(), adminEdited);
Files.setLastModifiedTime(settingsFile.toPath(), LONG_AGO);
IrisSettingsFile.load();
IrisSettings loaded = IrisSettingsFile.load();
assertTrue(loaded.getGeneral().isDebug());
assertEquals(adminEdited, Files.readString(settingsFile.toPath()));
assertEquals(LONG_AGO, Files.getLastModifiedTime(settingsFile.toPath()));
}
@Test
public void missingKeysAreAddedOnLoad() throws Exception {
Files.writeString(settingsFile.toPath(), "[general]\ndebug = true\n");
Files.writeString(settingsFile.toPath(), "# Admin header\n[general]\n# keep debug on\ndebug = true\n");
IrisSettingsFile.load();
IrisSettings loaded = IrisSettingsFile.load();
String rewritten = Files.readString(settingsFile.toPath());
assertTrue(rewritten.contains("\ndebug = true"));
assertTrue(rewritten.contains("\n[studio]"));
assertTrue(rewritten.startsWith("# Admin header\n[general]\nlanguage = \"en_US\"\nmetrics = true\n"));
assertTrue(rewritten.contains("\n# keep debug on\ndebug = true\n"));
assertTrue(rewritten.contains("\n[studio]\n"));
assertEquals(new Gson().toJson(loaded), new Gson().toJson(IrisSettingsFile.parse(rewritten)));
Files.setLastModifiedTime(settingsFile.toPath(), LONG_AGO);
IrisSettingsFile.load();
assertEquals(LONG_AGO, Files.getLastModifiedTime(settingsFile.toPath()));
}
@Test
public void savingKeepsAdminComments() throws Exception {
String adminEdited = "# Admin header\n" + IrisSettingsFile.render(new IrisSettings());
Files.writeString(settingsFile.toPath(), adminEdited);
IrisSettings loaded = IrisSettingsFile.load();
loaded.getGeneral().setDebug(true);
loaded.forceSave();
assertEquals(adminEdited.replace("\ndebug = false\n", "\ndebug = true\n"), Files.readString(settingsFile.toPath()));
}
@Test