diff --git a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedLootApplier.java b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedLootApplier.java index 7129371bc..c57002ce8 100644 --- a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedLootApplier.java +++ b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedLootApplier.java @@ -30,6 +30,7 @@ import art.arcane.iris.world.loot.IrisLootTable; import art.arcane.iris.structure.object.IrisObjectLoot; import art.arcane.iris.structure.object.IrisObjectPlacement; import art.arcane.iris.structure.object.IrisObjectVanillaLoot; +import art.arcane.iris.pack.loading.IrisData; import art.arcane.iris.spi.IrisLogging; import art.arcane.volmlib.nativelib.terrain.NativeBlockState; import art.arcane.volmlib.util.mantle.runtime.MantleChunk; @@ -40,6 +41,9 @@ import java.util.ArrayList; import java.util.List; public final class ModdedLootApplier { + private static final String LOOT_TABLE_KEY_KIND = "missing-loot-table"; + private static final String VANILLA_LOOT_TABLE_KEY_KIND = "missing-vanilla-loot-table"; + private ModdedLootApplier() { } @@ -117,7 +121,8 @@ public final class ModdedLootApplier { } IrisLootTable table = engine.getData().getLootLoader().load(loot.getName()); if (table == null) { - IrisLogging.warn("Couldn't find loot table " + loot.getName()); + IrisLogging.warnOnce(IrisData.onceKey(engine.getData(), LOOT_TABLE_KEY_KIND, loot.getName()), + "Couldn't find loot table " + loot.getName()); continue; } bucket(engine, loot, new Candidate(new IrisLootSource(table), loot.getWeight()), access, exact, basic, global); @@ -129,7 +134,8 @@ public final class ModdedLootApplier { } NativeContainerLoot.Table table = access.table(loot.getName()); if (table == null) { - IrisLogging.warn("Couldn't find vanilla loot table " + loot.getName()); + IrisLogging.warnOnce(IrisData.onceKey(engine.getData(), VANILLA_LOOT_TABLE_KEY_KIND, loot.getName()), + "Couldn't find vanilla loot table " + loot.getName()); continue; } bucket(engine, loot, new Candidate(new NativeLootSource(table), loot.getWeight()), access, exact, basic, global); diff --git a/core/src/main/java/art/arcane/iris/generation/mantle/MantleObjectComponent.java b/core/src/main/java/art/arcane/iris/generation/mantle/MantleObjectComponent.java index ac53f6af3..4bb06aa4d 100644 --- a/core/src/main/java/art/arcane/iris/generation/mantle/MantleObjectComponent.java +++ b/core/src/main/java/art/arcane/iris/generation/mantle/MantleObjectComponent.java @@ -34,6 +34,7 @@ import art.arcane.iris.structure.object.IrisAxisRotationClamp; import art.arcane.iris.generation.biome.IrisBiome; import art.arcane.iris.generation.cave.IrisCaveAnchorMode; import art.arcane.iris.generation.cave.IrisCaveProfile; +import art.arcane.iris.pack.loading.IrisData; import art.arcane.iris.generation.terrain.IrisDimension; import art.arcane.iris.structure.object.IrisObject; import art.arcane.iris.structure.object.IrisObjectPlacement; @@ -86,7 +87,7 @@ public class MantleObjectComponent extends IrisMantleComponent { private static final int BEDROCK_CLEARANCE = 6; static final byte LIQUID_FORCED_AIR = 3; private static final Map CAVE_REJECT_LOG_STATE = new ConcurrentHashMap<>(); - private static final Set MISSING_LOAD_KEY_WARNED = ConcurrentHashMap.newKeySet(); + private static final String MISSING_LOAD_KEY_KIND = "marker-missing-load-key"; private static final long SOURCE_PLAN_STATS_INTERVAL_MS = 30_000L; private static final int MAXIMUM_TREE_MATERIALS = 8192; private static final Map TREE_MATERIALS = new ConcurrentHashMap<>(); @@ -104,21 +105,29 @@ public class MantleObjectComponent extends IrisMantleComponent { this.collisionRuleState = -1; } - private static String placementMarker(IrisObject object, int id, String context) { + static String placementMarker(IrisObject object, int id, String context) { String key = object == null ? null : object.getLoadKey(); if (key == null || key.isEmpty() || key.equals("null")) { - String fingerprint = context + "|" + (object == null ? "" : object.getClass().getSimpleName()); - if (MISSING_LOAD_KEY_WARNED.add(fingerprint)) { - java.io.File file = object == null ? null : object.getLoadFile(); - IrisLogging.warn("Skipping placement marker write: IrisObject has no loadKey (context=" + context - + ", file=" + (file == null ? "" : file.getPath()) + "). " - + "This would previously produce 'Couldn't find Object: null' warnings on chunk reload."); - } + warnMissingLoadKeyOnce(object, context); return null; } return key + "@" + id; } + /** + * Keyed by call-site context and object class only: the file path and coordinates stay in the message, + * never in the key, so the key space is bounded. + */ + private static void warnMissingLoadKeyOnce(IrisObject object, String context) { + String fingerprint = context + "|" + (object == null ? "" : object.getClass().getSimpleName()); + IrisData pack = object == null ? null : object.getLoader(); + java.io.File file = object == null ? null : object.getLoadFile(); + IrisLogging.warnOnce(IrisData.onceKey(pack, MISSING_LOAD_KEY_KIND, fingerprint), + "Skipping placement marker write: IrisObject has no loadKey (context=" + context + + ", file=" + (file == null ? "" : file.getPath()) + "). " + + "This would previously produce 'Couldn't find Object: null' warnings on chunk reload."); + } + private static boolean isTreePlacement(IrisObject object, IrisObjectPlacement placement) { String key = object == null ? null : object.getLoadKey(); return (key != null && key.startsWith("trees/")) diff --git a/core/src/main/java/art/arcane/iris/pack/loading/ImageResourceLoader.java b/core/src/main/java/art/arcane/iris/pack/loading/ImageResourceLoader.java index e43dafeee..d7ea34618 100644 --- a/core/src/main/java/art/arcane/iris/pack/loading/ImageResourceLoader.java +++ b/core/src/main/java/art/arcane/iris/pack/loading/ImageResourceLoader.java @@ -63,14 +63,14 @@ public class ImageResourceLoader extends ResourceLoader { PrecisionStopwatch p = PrecisionStopwatch.start(); try (ImageInputStream input = ImageIO.createImageInputStream(j)) { if (input == null) { - IrisLogging.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() - + " (unsupported or corrupt image)"); + warnReadFailureOnce(j, "Couldn't read " + resourceTypeName + " file: " + j.getPath() + + " (unsupported or corrupt image)", null); return null; } Iterator readers = ImageIO.getImageReaders(input); if (!readers.hasNext()) { - IrisLogging.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() - + " (unsupported or corrupt image)"); + warnReadFailureOnce(j, "Couldn't read " + resourceTypeName + " file: " + j.getPath() + + " (unsupported or corrupt image)", null); return null; } ImageReader reader = readers.next(); @@ -78,21 +78,21 @@ public class ImageResourceLoader extends ResourceLoader { reader.setInput(input, true, true); String format = reader.getFormatName().toLowerCase(Locale.ROOT); if (!"png".equals(format)) { - IrisLogging.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() - + " (expected PNG, got " + format + ")"); + warnReadFailureOnce(j, "Couldn't read " + resourceTypeName + " file: " + j.getPath() + + " (expected PNG, got " + format + ")", null); return null; } int width = reader.getWidth(0); int height = reader.getHeight(0); if (!supportedDimensions(width, height)) { - IrisLogging.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() - + " (dimensions " + width + "x" + height + " exceed the supported image-map limits)"); + warnReadFailureOnce(j, "Couldn't read " + resourceTypeName + " file: " + j.getPath() + + " (dimensions " + width + "x" + height + " exceed the supported image-map limits)", null); return null; } BufferedImage image = reader.read(0); if (image == null) { - IrisLogging.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() - + " (unsupported or corrupt image)"); + warnReadFailureOnce(j, "Couldn't read " + resourceTypeName + " file: " + j.getPath() + + " (unsupported or corrupt image)", null); return null; } IrisImage loaded = new IrisImage(image, format); @@ -107,8 +107,7 @@ public class ImageResourceLoader extends ResourceLoader { } } } catch (Throwable e) { - IrisLogging.reportError(e); - IrisLogging.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() + ": " + e.getMessage()); + warnReadFailureOnce(j, "Couldn't read " + resourceTypeName + " file: " + j.getPath() + ": " + e.getMessage(), e); return null; } } @@ -180,7 +179,7 @@ public class ImageResourceLoader extends ResourceLoader { return null; } if (name.equals("null")) { - IrisLogging.warn("Refusing " + resourceTypeName + " lookup for literal string \"null\" (called by " + callerHint() + ")"); + warnLiteralNullOnce("lookup"); return null; } @@ -190,7 +189,7 @@ public class ImageResourceLoader extends ResourceLoader { return file; } - IrisLogging.warn("Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + warnMissingOnce(name); return null; } @@ -206,7 +205,7 @@ public class ImageResourceLoader extends ResourceLoader { return loadFile(file, name); } - IrisLogging.warn("Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + warnMissingOnce(name); return null; } @@ -216,7 +215,7 @@ public class ImageResourceLoader extends ResourceLoader { return null; } if (name.equals("null") && warn) { - IrisLogging.warn("Refusing " + resourceTypeName + " load for literal string \"null\" (called by " + callerHint() + ")"); + warnLiteralNullOnce("load"); return null; } return loadCache.get(name); diff --git a/core/src/main/java/art/arcane/iris/pack/loading/IrisData.java b/core/src/main/java/art/arcane/iris/pack/loading/IrisData.java index d7e26f680..c32448929 100644 --- a/core/src/main/java/art/arcane/iris/pack/loading/IrisData.java +++ b/core/src/main/java/art/arcane/iris/pack/loading/IrisData.java @@ -111,6 +111,8 @@ import java.util.concurrent.ConcurrentHashMap; @Data public class IrisData implements ExclusionStrategy, TypeAdapterFactory { private static final int CUSTOM_BIOME_KEY_CACHE_LIMIT = 4096; + private static final String PACK_ONCE_KEY_PREFIX = "pack|"; + private static final String PACK_ONCE_KEY_SEPARATOR = "|"; private static final Map dataLoaders = new ConcurrentHashMap<>(); // Loaders (cached or detached) that currently have registered engines; see hasActiveEngines. // Identity-keyed on purpose: Lombok's @Data hashCode over this class's mutable loader state @@ -693,6 +695,8 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory { } public synchronized void hotloaded() { + // Studio hotload reopens or rebuilds the pack through here without ever calling clearLists(). + IrisLogging.forgetOnceKeys(onceKeyPrefix()); blockDropMaterials = null; StructureGraphCatalog.invalidate(this); IrisObjectScale.invalidate(this); @@ -814,7 +818,28 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory { throw failure; } + /** + * Prefix shared by every warn-once key that belongs to this pack, so {@link #clearLists()} can forget + * them together. The trailing separator keeps {@code /packs/a} from matching {@code /packs/ab}. + */ + public String onceKeyPrefix() { + return PACK_ONCE_KEY_PREFIX + dataFolder.getAbsolutePath() + PACK_ONCE_KEY_SEPARATOR; + } + + public String onceKey(String kind, String detail) { + return onceKeyPrefix() + kind + PACK_ONCE_KEY_SEPARATOR + detail; + } + + /** + * Key for a warning whose site may not know its pack: scoped to the pack when there is one so a + * hotload forgets it, otherwise a plain stable key. + */ + public static String onceKey(IrisData data, String kind, String detail) { + return data == null ? kind + PACK_ONCE_KEY_SEPARATOR + detail : data.onceKey(kind, detail); + } + public void clearLists() { + IrisLogging.forgetOnceKeys(onceKeyPrefix()); blockDropMaterials = null; for (ResourceLoader i : loaders.values()) { i.clearList(); diff --git a/core/src/main/java/art/arcane/iris/pack/loading/MatterObjectResourceLoader.java b/core/src/main/java/art/arcane/iris/pack/loading/MatterObjectResourceLoader.java index 2c5195678..525b13e73 100644 --- a/core/src/main/java/art/arcane/iris/pack/loading/MatterObjectResourceLoader.java +++ b/core/src/main/java/art/arcane/iris/pack/loading/MatterObjectResourceLoader.java @@ -58,8 +58,7 @@ public class MatterObjectResourceLoader extends ResourceLoader tlt.addAndGet(p.getMilliseconds()); return t; } catch (Throwable e) { - IrisLogging.reportError(e); - IrisLogging.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() + ": " + e.getMessage()); + warnReadFailureOnce(j, "Couldn't read " + resourceTypeName + " file: " + j.getPath() + ": " + e.getMessage(), e); return null; } } @@ -120,7 +119,7 @@ public class MatterObjectResourceLoader extends ResourceLoader return null; } if (name.equals("null")) { - IrisLogging.warn("Refusing " + resourceTypeName + " lookup for literal string \"null\" (called by " + callerHint() + ")"); + warnLiteralNullOnce("lookup"); return null; } @@ -130,7 +129,7 @@ public class MatterObjectResourceLoader extends ResourceLoader return file; } - IrisLogging.warn("Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + warnMissingOnce(name); return null; } @@ -146,7 +145,7 @@ public class MatterObjectResourceLoader extends ResourceLoader return loadFile(file, name); } - IrisLogging.warn("Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + warnMissingOnce(name); return null; } @@ -156,7 +155,7 @@ public class MatterObjectResourceLoader extends ResourceLoader return null; } if (name.equals("null") && warn) { - IrisLogging.warn("Refusing " + resourceTypeName + " load for literal string \"null\" (called by " + callerHint() + ")"); + warnLiteralNullOnce("load"); return null; } return loadCache.get(name); diff --git a/core/src/main/java/art/arcane/iris/pack/loading/ObjectResourceLoader.java b/core/src/main/java/art/arcane/iris/pack/loading/ObjectResourceLoader.java index c11320af1..46466f309 100644 --- a/core/src/main/java/art/arcane/iris/pack/loading/ObjectResourceLoader.java +++ b/core/src/main/java/art/arcane/iris/pack/loading/ObjectResourceLoader.java @@ -60,13 +60,12 @@ public class ObjectResourceLoader extends ResourceLoader { tlt.addAndGet(p.getMilliseconds()); return t; } catch (Throwable e) { - IrisLogging.reportError(e); String message = e.getMessage(); String reason = e.getClass().getSimpleName(); if (message != null && !message.isBlank()) { reason = reason + ": " + message; } - IrisLogging.warn("Couldn't read " + resourceTypeName + " file: " + j.getPath() + " (" + reason + ")"); + warnReadFailureOnce(j, "Couldn't read " + resourceTypeName + " file: " + j.getPath() + " (" + reason + ")", e); return null; } } @@ -127,7 +126,7 @@ public class ObjectResourceLoader extends ResourceLoader { return null; } if (name.equals("null")) { - IrisLogging.warn("Refusing " + resourceTypeName + " lookup for literal string \"null\" (called by " + callerHint() + ")"); + warnLiteralNullOnce("lookup"); return null; } @@ -137,7 +136,7 @@ public class ObjectResourceLoader extends ResourceLoader { return file; } - IrisLogging.warn("Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + warnMissingOnce(name); return null; } @@ -156,12 +155,12 @@ public class ObjectResourceLoader extends ResourceLoader { return null; } if (name.equals("null") && warn) { - IrisLogging.warn("Refusing " + resourceTypeName + " load for literal string \"null\" (called by " + callerHint() + ")"); + warnLiteralNullOnce("load"); return null; } IrisObject result = loadCache.get(name); if (result == null && warn) { - IrisLogging.warn("Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + warnMissingOnce(name); } return result; } diff --git a/core/src/main/java/art/arcane/iris/pack/loading/ResourceLoader.java b/core/src/main/java/art/arcane/iris/pack/loading/ResourceLoader.java index 3335e7262..183aaacdb 100644 --- a/core/src/main/java/art/arcane/iris/pack/loading/ResourceLoader.java +++ b/core/src/main/java/art/arcane/iris/pack/loading/ResourceLoader.java @@ -91,6 +91,9 @@ public class ResourceLoader implements MeteredCache { private static final int PREFETCH_ENTRY_LIMIT = 1_024; private static final int PREFETCH_KEY_LENGTH_LIMIT = 1_024; private static final long PREFETCH_FILE_SIZE_LIMIT = 4L * 1_024L * 1_024L; + private static final String MISSING_KEY_KIND = "missing:"; + private static final String LITERAL_NULL_KEY_KIND = "literal-null:"; + private static final String READ_KEY_KIND = "read:"; private static volatile ExecutorService schemaBuildExecutor; private static final Set schemaBuildQueue = ConcurrentHashMap.newKeySet(); protected final AtomicCache> folderCache; @@ -217,7 +220,7 @@ public class ResourceLoader implements MeteredCache { return null; } if (name.equals("null")) { - IrisLogging.warn("Refusing " + resourceTypeName + " lookup for literal string \"null\" (called by " + callerHint() + ")"); + warnLiteralNullOnce("lookup"); return null; } @@ -227,7 +230,7 @@ public class ResourceLoader implements MeteredCache { return file; } - IrisLogging.warn("Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + warnMissingOnce(name); return null; } @@ -306,6 +309,27 @@ public class ResourceLoader implements MeteredCache { .orElse("")); } + protected void warnMissingOnce(String name) { + IrisLogging.warnOnce(manager.onceKey(MISSING_KEY_KIND + resourceTypeName, name), + "Couldn't find " + resourceTypeName + ": " + name + " (called by " + callerHint() + ")"); + } + + protected void warnLiteralNullOnce(String action) { + IrisLogging.warnOnce(manager.onceKey(LITERAL_NULL_KEY_KIND + resourceTypeName, action), + "Refusing " + resourceTypeName + " " + action + " for literal string \"null\" (called by " + callerHint() + ")"); + } + + /** + * Warns once per file; only that first warning carries the stack trace, so a file that fails on every + * lookup does not repeat it. + */ + protected void warnReadFailureOnce(File file, String message, Throwable failure) { + String key = manager.onceKey(READ_KEY_KIND + resourceTypeName, file.getPath()); + if (IrisLogging.warnOnce(key, message) && failure != null) { + IrisLogging.reportError(failure); + } + } + public void logLoad(File path, T t) { loads.getAndIncrement(); @@ -529,7 +553,7 @@ public class ResourceLoader implements MeteredCache { return null; } if (name.equals("null") && warn) { - IrisLogging.warn("Refusing " + resourceTypeName + " load for literal string \"null\" (called by " + callerHint() + ")"); + warnLiteralNullOnce("load"); return null; } diff --git a/core/src/main/java/art/arcane/iris/structure/object/IrisObjectPlacement.java b/core/src/main/java/art/arcane/iris/structure/object/IrisObjectPlacement.java index 60d98d731..28fdc433f 100644 --- a/core/src/main/java/art/arcane/iris/structure/object/IrisObjectPlacement.java +++ b/core/src/main/java/art/arcane/iris/structure/object/IrisObjectPlacement.java @@ -88,6 +88,7 @@ import java.util.function.Function; @Data public class IrisObjectPlacement { private static final int SURFACE_WARP_CACHE_SIZE = 8; + private static final String LOOT_TABLE_KEY_KIND = "missing-loot-table"; @Getter(AccessLevel.NONE) @Setter(AccessLevel.NONE) private final transient LazyBoundedCache surfaceWarpCache = @@ -422,7 +423,7 @@ public class IrisObjectPlacement { return (int) Math.round(densityStyle.get(rng, x, z, data)); } - private TableCache getCache(IrisData manager) { + TableCache getCache(IrisData manager) { return cache.aquire(() -> { TableCache cache = new TableCache(); @@ -441,7 +442,8 @@ public class IrisObjectPlacement { continue; IrisLootTable table = loader.apply(loot.getName()); if (table == null) { - IrisLogging.warn("Couldn't find loot table " + loot.getName()); + IrisLogging.warnOnce(IrisData.onceKey(manager, LOOT_TABLE_KEY_KIND, loot.getName()), + "Couldn't find loot table " + loot.getName()); continue; } if (table.isCompatExcluded()) { diff --git a/core/src/main/java/art/arcane/iris/world/loot/IrisLoot.java b/core/src/main/java/art/arcane/iris/world/loot/IrisLoot.java index a3f11b834..a6170f5f0 100644 --- a/core/src/main/java/art/arcane/iris/world/loot/IrisLoot.java +++ b/core/src/main/java/art/arcane/iris/world/loot/IrisLoot.java @@ -36,6 +36,7 @@ import art.arcane.iris.pack.schema.annotation.RegistryListItemType; import art.arcane.iris.pack.schema.annotation.Required; import art.arcane.iris.pack.schema.annotation.Snippet; import art.arcane.iris.platform.bukkit.BukkitPlatform; +import art.arcane.iris.pack.loading.IrisData; import art.arcane.iris.spi.IrisLogging; import art.arcane.volmlib.util.collection.KList; import art.arcane.volmlib.util.collection.KMap; @@ -72,6 +73,7 @@ import java.util.Optional; @ToString(doNotUseGetters = true) public class IrisLoot { public static final int MAX_AMOUNT = 64; + private static final String UNKNOWN_MATERIAL_KEY_KIND = "unknown-loot-material"; private final transient AtomicCache dyeColorResolved = new AtomicCache<>(); @Description("The target inventory slot types to fill this loot with. World generation only ever fills STORAGE; the other values are inert during worldgen.") @@ -173,7 +175,7 @@ public class IrisLoot { public ItemStack get(boolean debug, RNG rng) { try { - ItemStack is = getItemStack(rng); + ItemStack is = getItemStack(rng, null); if (is == null) return new ItemStack(Material.AIR); is.setItemMeta(applyProperties(is, rng, debug, null)); @@ -191,7 +193,7 @@ public class IrisLoot { } try { - ItemStack is = getItemStack(rng); + ItemStack is = getItemStack(rng, table); if (is == null) return null; is.setItemMeta(applyProperties(is, rng, debug, table)); @@ -204,11 +206,12 @@ public class IrisLoot { } // TODO Better Third Party Item Acquisition - private ItemStack getItemStack(RNG rng) { + private ItemStack getItemStack(RNG rng, IrisLootTable table) { if (!type.startsWith("minecraft:") && type.contains(":")) { Optional opt = IrisServices.get(ExternalDataSVC.class).getItemStack(Identifier.fromString(type), customNbt); if (opt.isEmpty()) { - IrisLogging.warn("Unknown Material: " + type); + IrisData pack = table == null ? null : table.getLoader(); + IrisLogging.warnOnce(IrisData.onceKey(pack, UNKNOWN_MATERIAL_KEY_KIND, type), "Unknown Material: " + type); return new ItemStack(Material.AIR); } ItemStack is = opt.get(); diff --git a/core/src/test/java/art/arcane/iris/generation/mantle/PlacementMarkerOnceWarningTest.java b/core/src/test/java/art/arcane/iris/generation/mantle/PlacementMarkerOnceWarningTest.java new file mode 100644 index 000000000..6f3a02ae1 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/generation/mantle/PlacementMarkerOnceWarningTest.java @@ -0,0 +1,62 @@ +package art.arcane.iris.generation.mantle; + +import art.arcane.iris.structure.object.IrisObject; +import art.arcane.iris.testsupport.CapturedLog; +import art.arcane.iris.testsupport.PlatformLeakGuard; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class PlacementMarkerOnceWarningTest { + private static final String SKIPPED = "Skipping placement marker write"; + + @ClassRule + public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean(); + + private CapturedLog log; + + @Before + public void captureLog() { + log = CapturedLog.bind(); + } + + @After + public void releaseLog() { + log.close(); + } + + @Test + public void anObjectWithoutALoadKeyWarnsOncePerContextNoMatterTheId() { + IrisObject object = new IrisObject(0, 0, 0); + + assertNull(MantleObjectComponent.placementMarker(object, 1, "surface")); + assertNull(MantleObjectComponent.placementMarker(object, 2, "surface")); + assertNull(MantleObjectComponent.placementMarker(new IrisObject(0, 0, 0), 3, "surface")); + + assertEquals(1, log.warnings(SKIPPED).size()); + } + + @Test + public void aDifferentContextWarnsSeparately() { + IrisObject object = new IrisObject(0, 0, 0); + + MantleObjectComponent.placementMarker(object, 1, "surface"); + MantleObjectComponent.placementMarker(object, 1, "procedural"); + + assertEquals(2, log.warnings(SKIPPED).size()); + } + + @Test + public void aLoadKeyedObjectNeverWarns() { + IrisObject object = new IrisObject(0, 0, 0); + object.setLoadKey("trees/oak"); + + assertEquals("trees/oak@7", MantleObjectComponent.placementMarker(object, 7, "surface")); + + assertEquals(0, log.warnings(SKIPPED).size()); + } +} diff --git a/core/src/test/java/art/arcane/iris/pack/loading/ResourceLoaderOnceWarningTest.java b/core/src/test/java/art/arcane/iris/pack/loading/ResourceLoaderOnceWarningTest.java new file mode 100644 index 000000000..2aba25b85 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/pack/loading/ResourceLoaderOnceWarningTest.java @@ -0,0 +1,241 @@ +package art.arcane.iris.pack.loading; + +import art.arcane.iris.generation.runtime.PreservationRegistry; +import art.arcane.iris.spi.IrisServices; +import art.arcane.iris.world.history.GenerationRegistryContract; +import art.arcane.iris.testsupport.CapturedLog; +import art.arcane.iris.testsupport.PlatformLeakGuard; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.nio.file.Files; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; + +/** + * A pack typo is reached per chunk, so a loader miss or an unreadable file must be stated once per pack, + * resource type and key, and stated again after the pack's caches are cleared (hotload). + */ +public class ResourceLoaderOnceWarningTest { + private static final String MISSING = "Couldn't find "; + private static final String UNREADABLE = "Couldn't read "; + private static final byte[] GARBAGE = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + + @ClassRule + public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean(); + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private CapturedLog log; + private IrisData data; + + @Before + public void openPack() throws Exception { + log = CapturedLog.bind(temporaryFolder.newFolder("platform-data")); + IrisServices.register(PreservationRegistry.class, mock(PreservationRegistry.class)); + data = openPack("pack"); + assertNotNull(log.messages() + " " + log.reportedErrors(), data.getObjectLoader()); + } + + private IrisData openPack(String name) throws Exception { + IrisData opened = IrisData.openRuntime(temporaryFolder.newFolder(name)); + opened.bindGenerationRegistryContract(GenerationRegistryContract.empty()); + return opened; + } + + @After + public void closePack() { + data.close(); + IrisServices.remove(PreservationRegistry.class); + log.close(); + } + + @Test + public void theSameMissingKeyWarnsOnce() { + data.getObjectLoader().load("trees/typo"); + data.getObjectLoader().load("trees/typo"); + data.getObjectLoader().findFile("trees/typo"); + + assertEquals(1, log.warnings(MISSING).size()); + } + + @Test + public void aDifferentKeyWarnsSeparately() { + data.getObjectLoader().load("trees/typo"); + data.getObjectLoader().load("trees/other-typo"); + + assertEquals(2, log.warnings(MISSING).size()); + } + + @Test + public void theSameKeyInAnotherResourceTypeWarnsSeparately() { + data.getObjectLoader().load("shared"); + data.getMatterLoader().load("shared"); + data.getImageLoader().load("shared"); + + assertEquals(3, log.warnings(MISSING).size()); + } + + @Test + public void theSameKeyInAnotherPackWarnsSeparately() throws Exception { + IrisData other = openPack("other-pack"); + try { + data.getObjectLoader().load("shared"); + other.getObjectLoader().load("shared"); + + assertEquals(2, log.warnings(MISSING).size()); + } finally { + other.close(); + } + } + + @Test + public void literalNullRefusalWarnsOnce() { + data.getObjectLoader().load("null"); + data.getObjectLoader().load("null"); + data.getObjectLoader().findFile("null"); + data.getObjectLoader().findFile("null"); + + assertEquals(2, log.warnings("Refusing ").size()); + } + + @Test + public void clearingTheListsLetsTheSameMissingKeyWarnAgain() { + data.getObjectLoader().load("trees/typo"); + data.getObjectLoader().load("trees/typo"); + assertEquals(1, log.warnings(MISSING).size()); + + data.clearLists(); + data.getObjectLoader().load("trees/typo"); + + assertEquals(2, log.warnings(MISSING).size()); + } + + /** Studio hotload rebuilds the pack's loaders through hotloaded() (or reopens it) without clearLists(). */ + @Test + public void rebuildingThePackLoadersLetsTheSameMissingKeyWarnAgain() { + data.getObjectLoader().load("trees/typo"); + + data.hotloaded(); + data.getObjectLoader().load("trees/typo"); + + assertEquals(2, log.warnings(MISSING).size()); + } + + @Test + public void reopeningThePackFolderLetsTheSameMissingKeyWarnAgain() { + data.getObjectLoader().load("trees/typo"); + + IrisData reopened = IrisData.openRuntime(data.getDataFolder()); + try { + reopened.getObjectLoader().load("trees/typo"); + + assertEquals(2, log.warnings(MISSING).size()); + } finally { + reopened.close(); + } + } + + @Test + public void clearingOnePackKeepsAnotherPacksKeysClaimed() throws Exception { + IrisData other = openPack("other-pack"); + try { + data.getObjectLoader().load("shared"); + other.getObjectLoader().load("shared"); + + data.clearLists(); + data.getObjectLoader().load("shared"); + other.getObjectLoader().load("shared"); + + assertEquals(3, log.warnings(MISSING).size()); + } finally { + other.close(); + } + } + + @Test + public void aPackWhosePathIsAPrefixOfAnotherIsNotReset() throws Exception { + IrisData longer = openPack("pack-two"); + try { + longer.getObjectLoader().load("shared"); + + data.clearLists(); + longer.getObjectLoader().load("shared"); + + assertEquals(1, log.warnings(MISSING).size()); + } finally { + longer.close(); + } + } + + @Test + public void anUnreadableObjectWarnsOnceAndReportsItsStackOnce() throws Exception { + writeGarbage(data.getObjectLoader(), "broken.iob"); + + data.getObjectLoader().load("broken"); + data.getObjectLoader().load("broken"); + data.getObjectLoader().load("broken"); + + assertEquals(1, log.warnings(UNREADABLE).size()); + assertEquals(1, log.reportedErrors().size()); + } + + @Test + public void anUnreadableMatterObjectWarnsOnceAndReportsItsStackOnce() throws Exception { + writeGarbage(data.getMatterLoader(), "broken.mat"); + + data.getMatterLoader().load("broken"); + data.getMatterLoader().load("broken"); + + assertEquals(1, log.warnings(UNREADABLE).size()); + assertEquals(1, log.reportedErrors().size()); + } + + @Test + public void anUnreadableImageWarnsOnce() throws Exception { + writeGarbage(data.getImageLoader(), "broken.png"); + + data.getImageLoader().load("broken"); + data.getImageLoader().load("broken"); + + assertEquals(1, log.warnings(UNREADABLE).size()); + } + + @Test + public void aDifferentUnreadableFileWarnsSeparately() throws Exception { + writeGarbage(data.getObjectLoader(), "broken.iob"); + writeGarbage(data.getObjectLoader(), "also-broken.iob"); + + data.getObjectLoader().load("broken"); + data.getObjectLoader().load("also-broken"); + + assertEquals(2, log.warnings(UNREADABLE).size()); + assertEquals(2, log.reportedErrors().size()); + } + + @Test + public void clearingTheListsReportsTheStackAgainForTheNextFirstWarning() throws Exception { + writeGarbage(data.getObjectLoader(), "broken.iob"); + data.getObjectLoader().load("broken"); + + data.clearLists(); + data.getObjectLoader().load("broken"); + + assertEquals(2, log.warnings(UNREADABLE).size()); + assertEquals(2, log.reportedErrors().size()); + } + + private void writeGarbage(ResourceLoader loader, String fileName) throws Exception { + File folder = new File(temporaryFolder.getRoot(), "pack/" + loader.getFolderName()); + Files.createDirectories(folder.toPath()); + Files.write(new File(folder, fileName).toPath(), GARBAGE); + } +} diff --git a/core/src/test/java/art/arcane/iris/spi/IrisLoggingTest.java b/core/src/test/java/art/arcane/iris/spi/IrisLoggingTest.java index fb68e4b02..03a32aaa2 100644 --- a/core/src/test/java/art/arcane/iris/spi/IrisLoggingTest.java +++ b/core/src/test/java/art/arcane/iris/spi/IrisLoggingTest.java @@ -138,6 +138,34 @@ public class IrisLoggingTest { assertEquals(List.of(LogLevel.WARN), levels); } + @Test + public void forgettingAPrefixReleasesOnlyTheKeysUnderIt() { + String scope = uniqueKey() + "|"; + String otherScope = uniqueKey() + "|"; + assertTrue(IrisLogging.warnOnce(scope + "a", "a")); + assertTrue(IrisLogging.warnOnce(scope + "b", "b")); + assertTrue(IrisLogging.warnOnce(otherScope + "a", "other")); + levels.clear(); + + IrisLogging.forgetOnceKeys(scope); + + assertTrue(IrisLogging.warnOnce(scope + "a", "a")); + assertTrue(IrisLogging.warnOnce(scope + "b", "b")); + assertFalse(IrisLogging.warnOnce(otherScope + "a", "other")); + assertEquals(List.of(LogLevel.WARN, LogLevel.WARN, LogLevel.DEBUG), levels); + } + + @Test + public void forgettingANullOrEmptyPrefixKeepsEveryKeyClaimed() { + String key = uniqueKey(); + assertTrue(IrisLogging.warnOnce(key, "claimed")); + + IrisLogging.forgetOnceKeys(null); + IrisLogging.forgetOnceKeys(""); + + assertFalse(IrisLogging.warnOnce(key, "claimed")); + } + private void bindCapturingPlatform() { IrisPlatform platform = mock(IrisPlatform.class); capturingPlatform = platform; diff --git a/core/src/test/java/art/arcane/iris/structure/object/IrisObjectPlacementOnceWarningTest.java b/core/src/test/java/art/arcane/iris/structure/object/IrisObjectPlacementOnceWarningTest.java new file mode 100644 index 000000000..de6d26590 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/structure/object/IrisObjectPlacementOnceWarningTest.java @@ -0,0 +1,84 @@ +package art.arcane.iris.structure.object; + +import art.arcane.iris.generation.runtime.PreservationRegistry; +import art.arcane.iris.pack.loading.IrisData; +import art.arcane.iris.spi.IrisServices; +import art.arcane.iris.testsupport.CapturedLog; +import art.arcane.iris.testsupport.PlatformLeakGuard; +import art.arcane.iris.world.history.GenerationRegistryContract; +import art.arcane.volmlib.util.collection.KList; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +public class IrisObjectPlacementOnceWarningTest { + private static final String MISSING_TABLE = "Couldn't find loot table "; + + @ClassRule + public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean(); + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private CapturedLog log; + private IrisData data; + + @Before + public void openPack() throws Exception { + log = CapturedLog.bind(temporaryFolder.newFolder("platform-data")); + IrisServices.register(PreservationRegistry.class, mock(PreservationRegistry.class)); + data = IrisData.openRuntime(temporaryFolder.newFolder("pack")); + data.bindGenerationRegistryContract(GenerationRegistryContract.empty()); + } + + @After + public void closePack() { + data.close(); + IrisServices.remove(PreservationRegistry.class); + log.close(); + } + + @Test + public void aMissingLootTableWarnsOnceAcrossPlacements() { + lookUpTable(placementWithLoot("chests/typo")); + lookUpTable(placementWithLoot("chests/typo")); + + assertEquals(1, log.warnings(MISSING_TABLE).size()); + } + + @Test + public void aDifferentMissingLootTableWarnsSeparately() { + lookUpTable(placementWithLoot("chests/typo")); + lookUpTable(placementWithLoot("chests/other-typo")); + + assertEquals(2, log.warnings(MISSING_TABLE).size()); + } + + @Test + public void clearingThePackListsLetsTheSameMissingLootTableWarnAgain() { + lookUpTable(placementWithLoot("chests/typo")); + + data.clearLists(); + lookUpTable(placementWithLoot("chests/typo")); + + assertEquals(2, log.warnings(MISSING_TABLE).size()); + } + + private static IrisObjectPlacement placementWithLoot(String tableName) { + IrisObjectLoot loot = new IrisObjectLoot(); + loot.setName(tableName); + IrisObjectPlacement placement = new IrisObjectPlacement(); + placement.setLoot(new KList().qadd(loot)); + return placement; + } + + private void lookUpTable(IrisObjectPlacement placement) { + placement.getCache(data); + } +} diff --git a/core/src/test/java/art/arcane/iris/testsupport/CapturedLog.java b/core/src/test/java/art/arcane/iris/testsupport/CapturedLog.java new file mode 100644 index 000000000..57e762642 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/testsupport/CapturedLog.java @@ -0,0 +1,85 @@ +package art.arcane.iris.testsupport; + +import art.arcane.iris.spi.IrisPlatform; +import art.arcane.iris.spi.IrisPlatforms; +import art.arcane.iris.spi.LogLevel; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +/** + * Binds a mock platform that records every log line and reported stack trace. {@link #close()} unbinds it, + * which also clears the JVM-global warn-once keys, so tests using it do not depend on order. + */ +public final class CapturedLog implements AutoCloseable { + private final List levels = new ArrayList<>(); + private final List messages = new ArrayList<>(); + private final List reportedErrors = new ArrayList<>(); + + private CapturedLog() { + } + + public static CapturedLog bind() { + return bind(null); + } + + /** + * @param dataRoot where the platform keeps its data files (settings are read from there on first use); + * null for a platform that has none + */ + public static CapturedLog bind(File dataRoot) { + IrisPlatforms.unbind(); + CapturedLog log = new CapturedLog(); + IrisPlatform platform = mock(IrisPlatform.class); + if (dataRoot != null) { + doReturn(dataRoot).when(platform).dataFolder(); + doAnswer(invocation -> { + Object[] segments = invocation.getArguments(); + return new File(dataRoot, String.join(File.separator, Arrays.copyOf(segments, segments.length, String[].class))); + }).when(platform).dataFile(any(String[].class)); + } + doAnswer(invocation -> { + log.levels.add(invocation.getArgument(0, LogLevel.class)); + log.messages.add(invocation.getArgument(1, String.class)); + return null; + }).when(platform).log(any(LogLevel.class), anyString()); + doAnswer(invocation -> { + log.reportedErrors.add(invocation.getArgument(0, Throwable.class)); + return null; + }).when(platform).reportError(any(Throwable.class)); + IrisPlatforms.bind(platform); + return log; + } + + /** Messages logged at WARN that start with {@code prefix}. */ + public List warnings(String prefix) { + List found = new ArrayList<>(); + for (int index = 0; index < messages.size(); index++) { + if (levels.get(index) == LogLevel.WARN && messages.get(index).startsWith(prefix)) { + found.add(messages.get(index)); + } + } + return found; + } + + public List messages() { + return messages; + } + + public List reportedErrors() { + return reportedErrors; + } + + @Override + public void close() { + IrisPlatforms.unbind(); + } +} diff --git a/core/src/test/java/art/arcane/iris/world/loot/IrisLootOnceWarningTest.java b/core/src/test/java/art/arcane/iris/world/loot/IrisLootOnceWarningTest.java new file mode 100644 index 000000000..46269ffe9 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/world/loot/IrisLootOnceWarningTest.java @@ -0,0 +1,78 @@ +package art.arcane.iris.world.loot; + +import art.arcane.iris.integration.ExternalDataSVC; +import art.arcane.iris.spi.IrisServices; +import art.arcane.iris.testsupport.CapturedLog; +import art.arcane.iris.testsupport.PlatformLeakGuard; +import art.arcane.volmlib.util.math.RNG; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class IrisLootOnceWarningTest { + private static final String UNKNOWN_MATERIAL = "Unknown Material: "; + + @ClassRule + public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean(); + + private CapturedLog log; + + @Before + public void captureLogAndRegisterEmptyProvider() { + log = CapturedLog.bind(); + ExternalDataSVC external = mock(ExternalDataSVC.class); + when(external.getItemStack(any(), any())).thenReturn(Optional.empty()); + IrisServices.register(ExternalDataSVC.class, external); + } + + @After + public void release() { + IrisServices.remove(ExternalDataSVC.class); + log.close(); + } + + @Test + public void anUnknownMaterialWarnsOncePerType() { + IrisLoot loot = lootOf("mymod:ruby"); + + resolve(loot, 1); + resolve(loot, 2); + resolve(lootOf("mymod:ruby"), 3); + + assertEquals(1, log.warnings(UNKNOWN_MATERIAL).size()); + } + + @Test + public void aDifferentUnknownMaterialWarnsSeparately() { + resolve(lootOf("mymod:ruby"), 1); + resolve(lootOf("mymod:sapphire"), 1); + + assertEquals(2, log.warnings(UNKNOWN_MATERIAL).size()); + } + + private static IrisLoot lootOf(String type) { + IrisLoot loot = new IrisLoot(); + loot.setType(type); + return loot; + } + + /** + * The warning is logged before the AIR fallback is built, and building an ItemStack needs a Bukkit + * server this test does not have. + */ + private static void resolve(IrisLoot loot, long seed) { + try { + loot.get(false, new RNG(seed)); + } catch (NullPointerException noBukkitServer) { + // expected: see above + } + } +} diff --git a/spi/src/main/java/art/arcane/iris/spi/IrisLogging.java b/spi/src/main/java/art/arcane/iris/spi/IrisLogging.java index 3b35fd757..fe337842d 100644 --- a/spi/src/main/java/art/arcane/iris/spi/IrisLogging.java +++ b/spi/src/main/java/art/arcane/iris/spi/IrisLogging.java @@ -199,6 +199,19 @@ public final class IrisLogging { ONCE_KEYS.clear(); } + /** + * Forgets only the once-keys that start with {@code prefix}, so one scope (a pack that was just + * hotloaded) can warn again while every other scope stays claimed. A null or empty prefix is ignored + * rather than clearing everything; {@link #resetOnceKeys()} is the global reset. + */ + public static void forgetOnceKeys(String prefix) { + if (prefix == null || prefix.isEmpty()) { + return; + } + + ONCE_KEYS.removeIf(key -> key.startsWith(prefix)); + } + private static boolean once(LogLevel level, String key, String format, Object... args) { String message = format(format, args); if (!claim(key)) {