mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +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
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user