mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Skip hotloads while a touched pack JSON does not parse
A bad save still hotloaded: the loaders return null for unparseable files, so the pack lost those biomes or regions. The new PackHotloadGuard keeps files that fail to parse until they parse, skips the hotload, logs it and sends a failed studio hotload message. Both the Bukkit generator and the modded studio hotload service gate on it, and the callback returns normally so the watcher does not retry. v3: fe825040a
This commit is contained in:
1 parent
5efffcd0b2
commit
0b5aa54df7
6 files changed
+304
-3
No files matched your search
+9
-2
@@ -22,6 +22,7 @@ import art.arcane.iris.modded.ModdedIrisLog;
|
||||
import art.arcane.iris.core.gui.PregeneratorJob;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.project.PackHotloadFolder;
|
||||
import art.arcane.iris.core.project.PackHotloadGuard;
|
||||
import art.arcane.iris.core.tools.WorldMaintenance;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.EnginePlatformHooks;
|
||||
@@ -262,9 +263,15 @@ public final class ModdedStudioHotloadService implements ModdedTickableService,
|
||||
if (watch.engine != engine) {
|
||||
watch.close();
|
||||
watch.engine = engine;
|
||||
File packFolder = engine.getData().getDataFolder();
|
||||
PackHotloadGuard hotloadGuard = new PackHotloadGuard(packFolder);
|
||||
watch.folder = PackHotloadFolder.watch(
|
||||
engine.getData().getDataFolder(),
|
||||
(KList<File> created, KList<File> changed, KList<File> deleted) -> hotload(dimensionId, generator, engine));
|
||||
packFolder,
|
||||
(KList<File> created, KList<File> changed, KList<File> deleted) -> {
|
||||
if (hotloadGuard.allowsHotload(PackHotloadGuard.touched(created, changed))) {
|
||||
hotload(dimensionId, generator, engine);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
ReactiveFolder folder = watch.folder;
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package art.arcane.iris.core.project;
|
||||
|
||||
import art.arcane.iris.core.protocol.IrisProtocolServer;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.iris.spi.IrisServices;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Decides whether a pack hotload may run. A hotload is skipped, logged and announced to studio clients while any
|
||||
* touched pack JSON fails to parse; it never throws, so a watcher callback can return normally and stay quiet
|
||||
* until the next save.
|
||||
*/
|
||||
public final class PackHotloadGuard {
|
||||
private static final int NO_CHANGED_FILES = 0;
|
||||
|
||||
private final File packFolder;
|
||||
private final UnreadablePackFiles unreadable = new UnreadablePackFiles();
|
||||
|
||||
public PackHotloadGuard(File packFolder) {
|
||||
this.packFolder = packFolder;
|
||||
}
|
||||
|
||||
public static List<File> touched(Collection<File> created, Collection<File> changed) {
|
||||
List<File> touched = new ArrayList<>(created);
|
||||
touched.addAll(changed);
|
||||
return touched;
|
||||
}
|
||||
|
||||
public boolean allowsHotload(Collection<File> touched) {
|
||||
Map<File, String> blocking = unreadable.recheck(touched);
|
||||
if (blocking.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
blocking.forEach(this::reportSkipped);
|
||||
return false;
|
||||
}
|
||||
|
||||
private void reportSkipped(File file, String reason) {
|
||||
String message = "Hotload skipped, " + packFolder.toPath().relativize(file.toPath()) + ": " + reason;
|
||||
IrisLogging.warn("%s", message);
|
||||
announce(message);
|
||||
}
|
||||
|
||||
private void announce(String message) {
|
||||
try {
|
||||
IrisProtocolServer protocolServer = IrisServices.getOrNull(IrisProtocolServer.class);
|
||||
if (protocolServer == null) {
|
||||
return;
|
||||
}
|
||||
protocolServer.broadcastStudioHotload(packFolder.getName(), NO_CHANGED_FILES, true, message);
|
||||
} catch (Throwable announceFailure) {
|
||||
IrisLogging.error("Iris studio hotload skip broadcast failed: %s: %s",
|
||||
announceFailure.getClass().getSimpleName(), announceFailure.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package art.arcane.iris.core.project;
|
||||
|
||||
import art.arcane.volmlib.util.io.IO;
|
||||
import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONException;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
import art.arcane.volmlib.util.json.JSONTokener;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The pack files that do not parse right now. A hotload has to wait for them: it would replace the running engine
|
||||
* with one missing those files, which drops biomes from the world and can leave a region with nothing to generate.
|
||||
* A file stays here until it parses, so saving another file cannot hotload past it.
|
||||
*/
|
||||
public final class UnreadablePackFiles {
|
||||
private static final String JSON_EXTENSION = ".json";
|
||||
|
||||
private final Map<File, String> reasonByFile = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* @return the unreadable files after looking at the touched ones again, with what is wrong with each
|
||||
*/
|
||||
public synchronized Map<File, String> recheck(Collection<File> touched) {
|
||||
Set<File> toRead = new LinkedHashSet<>(reasonByFile.keySet());
|
||||
toRead.addAll(touched);
|
||||
reasonByFile.clear();
|
||||
toRead.stream().filter(UnreadablePackFiles::isJsonFile).forEach(this::check);
|
||||
return Map.copyOf(reasonByFile);
|
||||
}
|
||||
|
||||
private static boolean isJsonFile(File file) {
|
||||
return file.isFile() && file.getName().endsWith(JSON_EXTENSION);
|
||||
}
|
||||
|
||||
private void check(File file) {
|
||||
try {
|
||||
Object parsed = new JSONTokener(IO.readAll(file)).nextValue();
|
||||
if (!(parsed instanceof JSONObject) && !(parsed instanceof JSONArray)) {
|
||||
reasonByFile.put(file, "not a JSON object");
|
||||
}
|
||||
} catch (JSONException | IOException e) {
|
||||
reasonByFile.put(file, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ import art.arcane.iris.core.gui.PregeneratorJob;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.nms.INMS;
|
||||
import art.arcane.iris.core.project.PackHotloadFolder;
|
||||
import art.arcane.iris.core.project.PackHotloadGuard;
|
||||
import art.arcane.iris.core.runtime.ObjectStudioActivation;
|
||||
import art.arcane.iris.core.runtime.jigsaw.JigsawStudioActivation;
|
||||
import art.arcane.iris.core.runtime.jigsaw.JigsawStudioSession;
|
||||
@@ -150,7 +151,12 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun
|
||||
this.studioEntryBootstrapActive = new AtomicBoolean(studio);
|
||||
this.dataLocation = dataLocation;
|
||||
this.dimensionKey = dimensionKey;
|
||||
this.folder = PackHotloadFolder.watch(dataLocation, (_a, _b, _c) -> hotloadFromWatcher());
|
||||
PackHotloadGuard hotloadGuard = new PackHotloadGuard(dataLocation);
|
||||
this.folder = PackHotloadFolder.watch(dataLocation, (created, changed, deleted) -> {
|
||||
if (hotloadGuard.allowsHotload(PackHotloadGuard.touched(created, changed))) {
|
||||
hotloadFromWatcher();
|
||||
}
|
||||
});
|
||||
this.initializationFailure = null;
|
||||
this.jigsawStudioActive = false;
|
||||
this.validatedDimension = null;
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package art.arcane.iris.core.project;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PackHotloadGuardTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private File write(String relativePath, String content) throws IOException {
|
||||
File file = new File(temporaryFolder.getRoot(), relativePath);
|
||||
Files.createDirectories(file.getParentFile().toPath());
|
||||
Files.writeString(file.toPath(), content);
|
||||
return file;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowsAHotloadWhenEveryTouchedFileParses() throws IOException {
|
||||
PackHotloadGuard guard = new PackHotloadGuard(temporaryFolder.getRoot());
|
||||
|
||||
assertTrue(guard.allowsHotload(List.of(write("biomes/plains.json", "{}"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipsAHotloadWhileATouchedFileIsBroken() throws IOException {
|
||||
PackHotloadGuard guard = new PackHotloadGuard(temporaryFolder.getRoot());
|
||||
|
||||
assertFalse(guard.allowsHotload(List.of(write("biomes/plains.json", "{"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void keepsSkippingOtherSavesUntilTheBrokenFileIsFixed() throws IOException {
|
||||
PackHotloadGuard guard = new PackHotloadGuard(temporaryFolder.getRoot());
|
||||
write("biomes/plains.json", "{");
|
||||
guard.allowsHotload(List.of(new File(temporaryFolder.getRoot(), "biomes/plains.json")));
|
||||
File region = write("regions/hills.json", "{}");
|
||||
|
||||
assertFalse(guard.allowsHotload(List.of(region)));
|
||||
|
||||
write("biomes/plains.json", "{}");
|
||||
assertTrue(guard.allowsHotload(List.of(region)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void touchedFilesAreTheCreatedOnesFollowedByTheChangedOnes() {
|
||||
File created = new File("created.json");
|
||||
File changed = new File("changed.json");
|
||||
|
||||
assertEquals(List.of(created, changed), PackHotloadGuard.touched(List.of(created), List.of(changed)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package art.arcane.iris.core.project;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class UnreadablePackFilesTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private final UnreadablePackFiles files = new UnreadablePackFiles();
|
||||
|
||||
private File write(String name, String content) throws IOException {
|
||||
File file = new File(temporaryFolder.getRoot(), name);
|
||||
Files.writeString(file.toPath(), content);
|
||||
return file;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsTrailingCommasLikeTheLoaders() throws IOException {
|
||||
File biome = write("biome.json", """
|
||||
{
|
||||
"name": "Plains",
|
||||
"children": ["a", "b",],
|
||||
}
|
||||
""");
|
||||
|
||||
assertTrue(files.recheck(List.of(biome)).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reportsCommentsBecauseTheLoadersCannotReadThem() throws IOException {
|
||||
File biome = write("biome.json", """
|
||||
{
|
||||
// the loaders reject this
|
||||
"name": "Plains"
|
||||
}
|
||||
""");
|
||||
|
||||
assertEquals(1, files.recheck(List.of(biome)).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsListsAtTheTopOfAFile() throws IOException {
|
||||
assertTrue(files.recheck(List.of(write("list.json", "[1, 2]"))).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reportsWhereASyntaxErrorIs() throws IOException {
|
||||
File biome = write("biome.json", """
|
||||
{
|
||||
"name": "Plains",
|
||||
"children": [
|
||||
"a"
|
||||
"b"
|
||||
]
|
||||
}
|
||||
""");
|
||||
|
||||
Map<File, String> unreadable = files.recheck(List.of(biome));
|
||||
|
||||
assertTrue(unreadable.get(biome), unreadable.get(biome).contains("line"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reportsAnEmptyFile() throws IOException {
|
||||
File biome = write("biome.json", "");
|
||||
|
||||
assertEquals(1, files.recheck(List.of(biome)).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reportsAFileThatIsNotAnObject() throws IOException {
|
||||
File biome = write("biome.json", "42");
|
||||
|
||||
assertEquals(1, files.recheck(List.of(biome)).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void keepsHoldingAFileUntilItParses() throws IOException {
|
||||
File broken = write("broken.json", "{");
|
||||
File other = write("other.json", "{}");
|
||||
files.recheck(List.of(broken));
|
||||
|
||||
assertEquals(1, files.recheck(List.of(other)).size());
|
||||
|
||||
write("broken.json", "{}");
|
||||
assertTrue(files.recheck(List.of(other)).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forgetsADeletedFile() throws IOException {
|
||||
File broken = write("broken.json", "{");
|
||||
files.recheck(List.of(broken));
|
||||
|
||||
Files.delete(broken.toPath());
|
||||
|
||||
assertTrue(files.recheck(List.of()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void skipsObjectFiles() throws IOException {
|
||||
assertTrue(files.recheck(List.of(write("tree.iob", "not json"))).isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user