mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-02 02:34:24 +00:00
Validate pack block keys against plugin blocks once they have loaded
This commit is contained in:
1 parent
5ea1a69190
commit
bfd2a762c7
3 files changed
+114
-23
No files matched your search
+68
-17
@@ -28,8 +28,10 @@ import art.arcane.iris.core.pack.PackValidationCache;
|
|||||||
import art.arcane.iris.core.pack.PackValidationRegistry;
|
import art.arcane.iris.core.pack.PackValidationRegistry;
|
||||||
import art.arcane.iris.core.pack.PackValidationResult;
|
import art.arcane.iris.core.pack.PackValidationResult;
|
||||||
import art.arcane.iris.core.pack.PackValidator;
|
import art.arcane.iris.core.pack.PackValidator;
|
||||||
|
import art.arcane.iris.core.service.ExternalDataSVC;
|
||||||
import art.arcane.iris.spi.IrisLogging;
|
import art.arcane.iris.spi.IrisLogging;
|
||||||
import art.arcane.iris.spi.IrisPlatforms;
|
import art.arcane.iris.spi.IrisPlatforms;
|
||||||
|
import art.arcane.iris.spi.IrisServices;
|
||||||
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
|
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
|
||||||
import art.arcane.iris.engine.object.world.IrisWorld;
|
import art.arcane.iris.engine.object.world.IrisWorld;
|
||||||
import art.arcane.iris.engine.platform.BukkitChunkGenerator;
|
import art.arcane.iris.engine.platform.BukkitChunkGenerator;
|
||||||
@@ -60,6 +62,8 @@ import java.util.function.Supplier;
|
|||||||
*/
|
*/
|
||||||
public final class IrisWorldGeneratorResolver {
|
public final class IrisWorldGeneratorResolver {
|
||||||
private static final int VALIDATION_STABILITY_ATTEMPTS = 2;
|
private static final int VALIDATION_STABILITY_ATTEMPTS = 2;
|
||||||
|
private static final String STARTUP_CACHE_FILE = "pack-validation.json";
|
||||||
|
private static final String PLUGIN_CONTENT_CACHE_FILE = "pack-validation-with-plugin-content.json";
|
||||||
private static final Object SNAPSHOT_VALIDATION_LOCK = new Object();
|
private static final Object SNAPSHOT_VALIDATION_LOCK = new Object();
|
||||||
private static final String IRIS_DIMENSION_NAMESPACE = "iris";
|
private static final String IRIS_DIMENSION_NAMESPACE = "iris";
|
||||||
private static final String PLOT_SQUARED_DISCOVERY_WORLD = "CheckingPlotSquaredGenerator";
|
private static final String PLOT_SQUARED_DISCOVERY_WORLD = "CheckingPlotSquaredGenerator";
|
||||||
@@ -70,12 +74,71 @@ public final class IrisWorldGeneratorResolver {
|
|||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates every pack before worlds load, so a broken pack refuses its worlds. Block plugins like CraftEngine
|
||||||
|
* register their blocks only after that, so the warnings wait for them: once they have loaded, a pack is validated
|
||||||
|
* again when the registry changed, and only then does it report the keys that are still unknown.
|
||||||
|
*/
|
||||||
public void validateAllPacks() {
|
public void validateAllPacks() {
|
||||||
|
PackValidationRun startupRun = validateAndPublish(STARTUP_CACHE_FILE);
|
||||||
|
reportBlockingErrors(startupRun.results());
|
||||||
|
IrisStartupValidation.markPacksReady();
|
||||||
|
IrisServices.get(ExternalDataSVC.class).afterProviderContentLoads(() -> reportWarningsWithPluginContent(startupRun));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reportWarningsWithPluginContent(PackValidationRun startupRun) {
|
||||||
|
if (safeContextFingerprint().equals(startupRun.contextFingerprint())) {
|
||||||
|
reportWarnings(startupRun);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
reportWarnings(validateAndPublish(PLUGIN_CONTENT_CACHE_FILE));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String safeContextFingerprint() {
|
||||||
|
try {
|
||||||
|
return PackValidationCache.contextFingerprint();
|
||||||
|
} catch (RuntimeException exception) {
|
||||||
|
Iris.reportError("Could not fingerprint the pack-validation context", exception);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void reportBlockingErrors(List<PackValidationResult> results) {
|
||||||
|
for (PackValidationResult result : results) {
|
||||||
|
if (result.isLoadable()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Iris.error("Pack '" + result.getPackName()
|
||||||
|
+ "' FAILED validation - world and Studio creation with this pack will be refused. Reasons:");
|
||||||
|
for (String reason : result.getBlockingErrors()) {
|
||||||
|
Iris.error(" - " + reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void reportWarnings(PackValidationRun run) {
|
||||||
|
for (PackValidationResult result : run.results()) {
|
||||||
|
if (!result.isLoadable()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!result.getWarnings().isEmpty()) {
|
||||||
|
Iris.info("Pack '" + result.getPackName() + "' validated ("
|
||||||
|
+ result.getWarnings().size() + " warning(s)).");
|
||||||
|
for (String warning : result.getWarnings()) {
|
||||||
|
Iris.warn(" [" + result.getPackName() + "] " + warning);
|
||||||
|
}
|
||||||
|
} else if (!run.fromCache()) {
|
||||||
|
Iris.success("Pack '" + result.getPackName() + "' validated.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PackValidationRun validateAndPublish(String cacheFileName) {
|
||||||
File packsRoot = plugin.getDataFolder("packs");
|
File packsRoot = plugin.getDataFolder("packs");
|
||||||
List<File> packDirs = PackDirectoryResolver.listVisiblePackDirectories(packsRoot);
|
List<File> packDirs = PackDirectoryResolver.listVisiblePackDirectories(packsRoot);
|
||||||
PackValidationRegistry.clear();
|
PackValidationRegistry.clear();
|
||||||
List<String> packNames = packDirs.stream().map(File::getName).sorted().toList();
|
List<String> packNames = packDirs.stream().map(File::getName).sorted().toList();
|
||||||
Path cacheFile = IrisPlatforms.get().dataFile("cache", "pack-validation.json").toPath();
|
Path cacheFile = IrisPlatforms.get().dataFile("cache", cacheFileName).toPath();
|
||||||
ServerConfigurator.PackContentSnapshot contentSnapshot =
|
ServerConfigurator.PackContentSnapshot contentSnapshot =
|
||||||
new ServerConfigurator.PackContentSnapshot("", Map.of());
|
new ServerConfigurator.PackContentSnapshot("", Map.of());
|
||||||
String contextFingerprint = "";
|
String contextFingerprint = "";
|
||||||
@@ -123,23 +186,8 @@ public final class IrisWorldGeneratorResolver {
|
|||||||
if (packDirectory != null && packFingerprint != null && !packFingerprint.isBlank()) {
|
if (packDirectory != null && packFingerprint != null && !packFingerprint.isBlank()) {
|
||||||
PackValidationRegistry.publish(packDirectory.toPath(), result, packFingerprint);
|
PackValidationRegistry.publish(packDirectory.toPath(), result, packFingerprint);
|
||||||
}
|
}
|
||||||
if (!result.isLoadable()) {
|
|
||||||
Iris.error("Pack '" + result.getPackName()
|
|
||||||
+ "' FAILED validation - world and Studio creation with this pack will be refused. Reasons:");
|
|
||||||
for (String reason : result.getBlockingErrors()) {
|
|
||||||
Iris.error(" - " + reason);
|
|
||||||
}
|
|
||||||
} else if (!result.getWarnings().isEmpty()) {
|
|
||||||
Iris.info("Pack '" + result.getPackName() + "' validated ("
|
|
||||||
+ result.getWarnings().size() + " warning(s)).");
|
|
||||||
for (String warning : result.getWarnings()) {
|
|
||||||
Iris.warn(" [" + result.getPackName() + "] " + warning);
|
|
||||||
}
|
|
||||||
} else if (cached.isEmpty()) {
|
|
||||||
Iris.success("Pack '" + result.getPackName() + "' validated.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
IrisStartupValidation.markPacksReady();
|
return new PackValidationRun(results, contextFingerprint, cached.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static FreshValidation validateStablePacks(
|
private static FreshValidation validateStablePacks(
|
||||||
@@ -495,6 +543,9 @@ public final class IrisWorldGeneratorResolver {
|
|||||||
return new BukkitChunkGenerator(world, false, snapshotRoot, dimension.getLoadKey());
|
return new BukkitChunkGenerator(world, false, snapshotRoot, dimension.getLoadKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record PackValidationRun(List<PackValidationResult> results, String contextFingerprint, boolean fromCache) {
|
||||||
|
}
|
||||||
|
|
||||||
private record FreshValidation(
|
private record FreshValidation(
|
||||||
List<File> packDirs,
|
List<File> packDirs,
|
||||||
ServerConfigurator.PackContentSnapshot contentSnapshot,
|
ServerConfigurator.PackContentSnapshot contentSnapshot,
|
||||||
|
|||||||
@@ -43,10 +43,12 @@ import org.bukkit.entity.Entity;
|
|||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.server.PluginEnableEvent;
|
import org.bukkit.event.server.PluginEnableEvent;
|
||||||
|
import org.bukkit.event.server.ServerLoadEvent;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.MissingResourceException;
|
import java.util.MissingResourceException;
|
||||||
@@ -73,6 +75,8 @@ public class ExternalDataSVC implements IrisService, CustomBlockSource {
|
|||||||
private final KList<ExternalDataProvider> providers = new KList<>();
|
private final KList<ExternalDataProvider> providers = new KList<>();
|
||||||
private final KList<ExternalDataProvider> activeProviders = new KList<>();
|
private final KList<ExternalDataProvider> activeProviders = new KList<>();
|
||||||
private final AtomicBoolean placeholdersHandedOut = new AtomicBoolean();
|
private final AtomicBoolean placeholdersHandedOut = new AtomicBoolean();
|
||||||
|
private final List<Runnable> actionsAwaitingProviderContent = new ArrayList<>();
|
||||||
|
private volatile boolean serverLoaded;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether a key that no provider claims may still belong to a provider that has not loaded its
|
* Whether a key that no provider claims may still belong to a provider that has not loaded its
|
||||||
@@ -87,8 +91,40 @@ public class ExternalDataSVC implements IrisService, CustomBlockSource {
|
|||||||
placeholdersHandedOut.set(true);
|
placeholdersHandedOut.set(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs {@code action} on the main thread once the server has started and every provider has loaded its blocks and
|
||||||
|
* items, so whatever it checks against the block registry sees them.
|
||||||
|
*/
|
||||||
|
public void afterProviderContentLoads(Runnable action) {
|
||||||
|
synchronized (actionsAwaitingProviderContent) {
|
||||||
|
actionsAwaitingProviderContent.add(action);
|
||||||
|
}
|
||||||
|
runActionsIfProviderContentLoaded();
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onServerLoad(ServerLoadEvent event) {
|
||||||
|
serverLoaded = true;
|
||||||
|
runActionsIfProviderContentLoaded();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runActionsIfProviderContentLoaded() {
|
||||||
|
if (!serverLoaded || isAwaitingProviderContent()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<Runnable> actions;
|
||||||
|
synchronized (actionsAwaitingProviderContent) {
|
||||||
|
actions = List.copyOf(actionsAwaitingProviderContent);
|
||||||
|
actionsAwaitingProviderContent.clear();
|
||||||
|
}
|
||||||
|
if (!actions.isEmpty()) {
|
||||||
|
J.s(() -> actions.forEach(Runnable::run));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Reloads the Iris worlds that resolved a block while a provider was still loading, now that it has loaded. */
|
/** Reloads the Iris worlds that resolved a block while a provider was still loading, now that it has loaded. */
|
||||||
public void onProviderContentLoaded(ExternalDataProvider provider) {
|
public void onProviderContentLoaded(ExternalDataProvider provider) {
|
||||||
|
runActionsIfProviderContentLoaded();
|
||||||
if (isAwaitingProviderContent() || !placeholdersHandedOut.getAndSet(false)) {
|
if (isAwaitingProviderContent() || !placeholdersHandedOut.getAndSet(false)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,6 +132,7 @@ public final class BukkitRegistries implements PlatformRegistries {
|
|||||||
keys.add(material.getKey().toString());
|
keys.add(material.getKey().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
keys.addAll(externalKeys(DataType.BLOCK));
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,15 +158,18 @@ public final class BukkitRegistries implements PlatformRegistries {
|
|||||||
keys.add(material.getKey().toString());
|
keys.add(material.getKey().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ExternalDataSVC external = IrisServices.getOrNull(ExternalDataSVC.class);
|
keys.addAll(externalKeys(DataType.ITEM));
|
||||||
if (external != null) {
|
|
||||||
for (Identifier identifier : external.getAllIdentifiers(DataType.ITEM)) {
|
|
||||||
keys.add(identifier.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<String> externalKeys(DataType type) {
|
||||||
|
ExternalDataSVC external = IrisServices.getOrNull(ExternalDataSVC.class);
|
||||||
|
if (external == null) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
return external.getAllIdentifiers(type).stream().map(Identifier::toString).toList();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> entityKeys() {
|
public List<String> entityKeys() {
|
||||||
List<String> keys = new ArrayList<>();
|
List<String> keys = new ArrayList<>();
|
||||||
|
|||||||
Reference in new issue
Block a user