mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +00:00
Parse pack JSON once per validation
- Share folder listings and parsed JSON across all validation steps within one PackValidator run - Route validators that read JSON on their own through the shared parse - Cover per-run sharing and edits seen by the next run in PackValidationIoRunTest
This commit is contained in:
1 parent
21475b3c86
commit
22c8e68007
10 files changed
+168
-27
No files matched your search
@@ -30,7 +30,6 @@ import art.arcane.volmlib.util.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
@@ -540,7 +539,7 @@ public final class ContentKeyValidator {
|
||||
boolean inEntities = relative.startsWith("entities/");
|
||||
JSONObject json;
|
||||
try {
|
||||
json = new JSONObject(Files.readString(path, StandardCharsets.UTF_8));
|
||||
json = PackValidationIo.parseJson(path.toFile());
|
||||
} catch (Throwable ignored) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -26,8 +26,6 @@ import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -53,7 +51,7 @@ final class PackDimensionValidator {
|
||||
String dimensionKey = PackValidationIo.deriveKey(dimensionsFolder, dimFile);
|
||||
JSONObject dimJson;
|
||||
try {
|
||||
dimJson = new JSONObject(Files.readString(dimFile.toPath(), StandardCharsets.UTF_8));
|
||||
dimJson = PackValidationIo.parseJson(dimFile);
|
||||
} catch (Throwable e) {
|
||||
blockingErrors.add("Dimension '" + dimensionKey + "' has invalid JSON: " + e.getMessage());
|
||||
continue;
|
||||
@@ -92,7 +90,7 @@ final class PackDimensionValidator {
|
||||
|
||||
JSONObject regionJson;
|
||||
try {
|
||||
regionJson = new JSONObject(Files.readString(regionFile.toPath(), StandardCharsets.UTF_8));
|
||||
regionJson = PackValidationIo.parseJson(regionFile);
|
||||
} catch (Throwable e) {
|
||||
blockingErrors.add("Region '" + regionKey + "' has invalid JSON: " + e.getMessage());
|
||||
continue;
|
||||
@@ -269,7 +267,7 @@ final class PackDimensionValidator {
|
||||
blockingErrors.add(context + " references missing style snippet '" + reference + "'.");
|
||||
return;
|
||||
}
|
||||
new JSONObject(Files.readString(snippetFile.toPath(), StandardCharsets.UTF_8));
|
||||
PackValidationIo.parseJson(snippetFile);
|
||||
} catch (Throwable error) {
|
||||
blockingErrors.add(context + " references invalid style snippet '" + reference + "': "
|
||||
+ error.getMessage());
|
||||
@@ -619,7 +617,7 @@ final class PackDimensionValidator {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return new JSONObject(Files.readString(snippet.toPath(), StandardCharsets.UTF_8));
|
||||
return PackValidationIo.parseJson(snippet);
|
||||
} catch (Throwable e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
@@ -68,7 +66,7 @@ final class PackGeneratorDuplicateValidator {
|
||||
String key = PackValidationIo.deriveKey(generatorsFolder, generatorFile);
|
||||
String fingerprint;
|
||||
try {
|
||||
fingerprint = fingerprint(new JSONObject(Files.readString(generatorFile.toPath(), StandardCharsets.UTF_8)));
|
||||
fingerprint = fingerprint(PackValidationIo.parseJson(generatorFile));
|
||||
} catch (Throwable e) {
|
||||
continue;
|
||||
}
|
||||
@@ -98,7 +96,7 @@ final class PackGeneratorDuplicateValidator {
|
||||
for (File biomeFile : PackValidationIo.listJsonRecursive(biomesFolder)) {
|
||||
JSONObject biome;
|
||||
try {
|
||||
biome = new JSONObject(Files.readString(biomeFile.toPath(), StandardCharsets.UTF_8));
|
||||
biome = PackValidationIo.parseJson(biomeFile);
|
||||
} catch (Throwable e) {
|
||||
continue;
|
||||
}
|
||||
@@ -133,7 +131,7 @@ final class PackGeneratorDuplicateValidator {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return new JSONObject(Files.readString(snippet.toPath(), StandardCharsets.UTF_8));
|
||||
return PackValidationIo.parseJson(snippet);
|
||||
} catch (Throwable e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
@@ -59,7 +58,7 @@ final class PackSpawnValidator {
|
||||
String spawnerKey = PackValidationIo.deriveKey(spawnersFolder, spawnerFile);
|
||||
JSONObject spawner;
|
||||
try {
|
||||
spawner = new JSONObject(Files.readString(spawnerFile.toPath(), StandardCharsets.UTF_8));
|
||||
spawner = PackValidationIo.parseJson(spawnerFile);
|
||||
} catch (Throwable e) {
|
||||
blockingErrors.add("Spawner '" + spawnerKey + "' has invalid JSON: " + e.getMessage());
|
||||
continue;
|
||||
@@ -147,7 +146,7 @@ final class PackSpawnValidator {
|
||||
}
|
||||
|
||||
try {
|
||||
new JSONObject(Files.readString(entityFile, StandardCharsets.UTF_8));
|
||||
PackValidationIo.parseJson(entityFile.toFile());
|
||||
validEntityFiles.add(entityFile);
|
||||
} catch (Throwable e) {
|
||||
String message = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage();
|
||||
@@ -170,7 +169,7 @@ final class PackSpawnValidator {
|
||||
String biomeKey = PackValidationIo.deriveKey(biomesFolder, biomeFile);
|
||||
JSONObject biome;
|
||||
try {
|
||||
biome = new JSONObject(Files.readString(biomeFile.toPath(), StandardCharsets.UTF_8));
|
||||
biome = PackValidationIo.parseJson(biomeFile);
|
||||
} catch (Throwable e) {
|
||||
blockingErrors.add("Biome '" + biomeKey + "' has invalid JSON: " + e.getMessage());
|
||||
continue;
|
||||
|
||||
@@ -8,7 +8,6 @@ import art.arcane.volmlib.util.json.JSONObject;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
@@ -174,7 +173,7 @@ final class PackStaticObjectValidator {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return new JSONObject(Files.readString(snippet, StandardCharsets.UTF_8));
|
||||
return PackValidationIo.parseJson(snippet.toFile());
|
||||
} catch (IOException | RuntimeException e) {
|
||||
errors.add(path + " could not read snippet '" + reference + "': " + e.getMessage());
|
||||
return null;
|
||||
|
||||
@@ -27,8 +27,6 @@ import art.arcane.volmlib.util.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
@@ -782,7 +780,7 @@ final class PackStructurePlacementValidator {
|
||||
String resourceKey,
|
||||
List<String> blockingErrors) {
|
||||
try {
|
||||
return new JSONObject(Files.readString(file.toPath(), StandardCharsets.UTF_8));
|
||||
return PackValidationIo.parseJson(file);
|
||||
} catch (IOException | RuntimeException e) {
|
||||
String reason = e.getMessage();
|
||||
if (reason == null || reason.isBlank()) {
|
||||
|
||||
@@ -22,8 +22,6 @@ import art.arcane.volmlib.util.json.JSONArray;
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -67,7 +65,7 @@ final class PackStyledRangeDefaultValidator {
|
||||
String key = PackValidationIo.deriveKey(resourceFolder, resourceFile);
|
||||
JSONObject json;
|
||||
try {
|
||||
json = new JSONObject(Files.readString(resourceFile.toPath(), StandardCharsets.UTF_8));
|
||||
json = PackValidationIo.parseJson(resourceFile);
|
||||
} catch (Throwable e) {
|
||||
continue;
|
||||
}
|
||||
@@ -124,7 +122,7 @@ final class PackStyledRangeDefaultValidator {
|
||||
return; // Missing snippets are reported by the content-key machinery.
|
||||
}
|
||||
try {
|
||||
range = new JSONObject(Files.readString(snippet.toPath(), StandardCharsets.UTF_8));
|
||||
range = PackValidationIo.parseJson(snippet);
|
||||
via = " (via snippet '" + resolved + "')";
|
||||
} catch (Throwable e) {
|
||||
return;
|
||||
|
||||
@@ -26,18 +26,51 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class PackValidationIo {
|
||||
private static final ThreadLocal<ValidationRunReads> ACTIVE_RUN = new ThreadLocal<>();
|
||||
|
||||
private PackValidationIo() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs one validation pass in which every folder listing and every JSON parse is done once and shared by all
|
||||
* steps. The shared trees are read-only by contract. Nothing outlives the pass, so the next pass sees edits.
|
||||
*/
|
||||
static <T> T withinValidationRun(Supplier<T> validation) {
|
||||
if (ACTIVE_RUN.get() != null) {
|
||||
return validation.get();
|
||||
}
|
||||
ACTIVE_RUN.set(new ValidationRunReads());
|
||||
try {
|
||||
return validation.get();
|
||||
} finally {
|
||||
ACTIVE_RUN.remove();
|
||||
}
|
||||
}
|
||||
|
||||
static JSONObject parseJson(File file) throws IOException {
|
||||
ValidationRunReads run = ACTIVE_RUN.get();
|
||||
if (run == null) {
|
||||
return parseFile(file);
|
||||
}
|
||||
return run.parsedJson(file).jsonOrThrow();
|
||||
}
|
||||
|
||||
private static JSONObject parseFile(File file) throws IOException {
|
||||
return new JSONObject(Files.readString(file.toPath(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
static JSONObject readJson(File file) {
|
||||
try {
|
||||
return new JSONObject(Files.readString(file.toPath(), StandardCharsets.UTF_8));
|
||||
return parseJson(file);
|
||||
} catch (IOException | RuntimeException parseFailure) {
|
||||
// Every folder scanned through readJson is also scanned by a readGraphJson pass
|
||||
// (validateUnsupportedStructureTransforms / validateStructureStartPools), which records the
|
||||
@@ -80,6 +113,14 @@ public final class PackValidationIo {
|
||||
}
|
||||
|
||||
static List<File> listJsonRecursive(File root) {
|
||||
ValidationRunReads run = ACTIVE_RUN.get();
|
||||
if (run == null) {
|
||||
return walkJsonFiles(root);
|
||||
}
|
||||
return new ArrayList<>(run.jsonFiles(root));
|
||||
}
|
||||
|
||||
private static List<File> walkJsonFiles(File root) {
|
||||
List<File> out = new ArrayList<>();
|
||||
try (Stream<Path> stream = Files.walk(root.toPath())) {
|
||||
stream.filter(Files::isRegularFile)
|
||||
@@ -124,4 +165,38 @@ public final class PackValidationIo {
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
private static final class ValidationRunReads {
|
||||
private final Map<File, List<File>> jsonFilesByRoot = new HashMap<>();
|
||||
private final Map<Path, ParsedJson> parsedByPath = new HashMap<>();
|
||||
|
||||
List<File> jsonFiles(File root) {
|
||||
return jsonFilesByRoot.computeIfAbsent(root, PackValidationIo::walkJsonFiles);
|
||||
}
|
||||
|
||||
ParsedJson parsedJson(File file) {
|
||||
return parsedByPath.computeIfAbsent(file.toPath().toAbsolutePath().normalize(),
|
||||
ignored -> ParsedJson.parse(file));
|
||||
}
|
||||
}
|
||||
|
||||
private record ParsedJson(JSONObject json, Exception failure) {
|
||||
static ParsedJson parse(File file) {
|
||||
try {
|
||||
return new ParsedJson(parseFile(file), null);
|
||||
} catch (IOException | RuntimeException failure) {
|
||||
return new ParsedJson(null, failure);
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject jsonOrThrow() throws IOException {
|
||||
if (failure instanceof IOException readFailure) {
|
||||
throw readFailure;
|
||||
}
|
||||
if (failure instanceof RuntimeException parseFailure) {
|
||||
throw parseFailure;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,10 @@ public final class PackValidator {
|
||||
}
|
||||
|
||||
private static PackValidationResult validate(File packFolder, boolean validateLiveRegistries) {
|
||||
return PackValidationIo.withinValidationRun(() -> validateWithinRun(packFolder, validateLiveRegistries));
|
||||
}
|
||||
|
||||
private static PackValidationResult validateWithinRun(File packFolder, boolean validateLiveRegistries) {
|
||||
String packName = packFolder == null ? "<unknown>" : packFolder.getName();
|
||||
List<String> blockingErrors = new ArrayList<>();
|
||||
List<String> warnings = new ArrayList<>();
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package art.arcane.iris.pack;
|
||||
|
||||
import art.arcane.volmlib.util.json.JSONObject;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PackValidationIoRunTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void secondValidationSeesFileEditedAfterFirst() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("pack");
|
||||
write(pack, "dimensions/main.json", "{\"regions\":[\"region\"]}");
|
||||
write(pack, "regions/region.json", "{\"landBiomes\":[\"biome\"]}");
|
||||
write(pack, "biomes/biome.json", "{\"name\":\"Biome\"}");
|
||||
assertTrue(PackValidator.validate(pack).isLoadable());
|
||||
|
||||
write(pack, "biomes/biome.json", "{\"name\":\"Biome\",\"fluidBodies\":{}}");
|
||||
PackValidationResult edited = PackValidator.validate(pack);
|
||||
|
||||
assertFalse(edited.isLoadable());
|
||||
assertTrue(edited.getBlockingErrors().toString().contains("Biome 'biome' declares removed field 'fluidBodies'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsedJsonIsSharedOnlyWithinOneRun() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("shared");
|
||||
File biome = write(pack, "biomes/biome.json", "{\"name\":\"Biome\"}");
|
||||
|
||||
JSONObject[] withinRun = PackValidationIo.withinValidationRun(
|
||||
() -> new JSONObject[]{PackValidationIo.readJson(biome), PackValidationIo.readJson(biome)});
|
||||
|
||||
assertSame(withinRun[0], withinRun[1]);
|
||||
assertNotSame(PackValidationIo.readJson(biome), PackValidationIo.readJson(biome));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listingWithinRunIsACopyPerCaller() throws Exception {
|
||||
File pack = temporaryFolder.newFolder("listing");
|
||||
write(pack, "biomes/a.json", "{}");
|
||||
write(pack, "biomes/b.json", "{}");
|
||||
File biomes = new File(pack, "biomes");
|
||||
|
||||
int secondListingSize = PackValidationIo.withinValidationRun(() -> {
|
||||
List<File> first = PackValidationIo.listJsonRecursive(biomes);
|
||||
first.clear();
|
||||
return PackValidationIo.listJsonRecursive(biomes).size();
|
||||
});
|
||||
|
||||
assertEquals(2, secondListingSize);
|
||||
}
|
||||
|
||||
private File write(File root, String relative, String content) throws Exception {
|
||||
Path path = root.toPath().resolve(relative);
|
||||
Files.createDirectories(path.getParent());
|
||||
Files.writeString(path, content, StandardCharsets.UTF_8);
|
||||
return path.toFile();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user