instructions = WorldGenUtilsPlacementPerformancePatchTest.instructions(node);
+ assertEquals(List.of(ALOAD, ALOAD, INVOKESTATIC, IRETURN), instructions.stream().map(AbstractInsnNode::getOpcode).toList());
+
+ MethodInsnNode call = (MethodInsnNode) instructions.get(2);
+ assertEquals("dev/overlord/zomboidforge/compat/b42/hooks/WorldGenHooks", call.owner);
+ assertEquals("canPlace", call.name);
+ assertEquals("(Ljava/util/List;Ljava/lang/String;)Z", call.desc);
+ }
+
+ @Test
+ void patchedClassIsValid() throws ClassNotFoundException
+ {
+ ClassNode node = WorldGenUtilsPlacementPerformancePatchTest.utils(false);
+ new WorldGenUtilsPlacementPerformancePatch().apply(node);
+
+ // Initializing links the class, which runs the bytecode verifier on the new body.
+ ClassDefiner definer = new ClassDefiner();
+ definer.define(node);
+ Class.forName(node.name.replace('/', '.'), true, definer);
+ }
+
+ @Test
+ void failsWhenTheBodyUsesThis()
+ {
+ ClassNode node = WorldGenUtilsPlacementPerformancePatchTest.utils(true);
+
+ assertThrows(IllegalStateException.class, () -> new WorldGenUtilsPlacementPerformancePatch().apply(node));
+ }
+
+ @Test
+ void appliesOnlyToVerifiedVersions()
+ {
+ WorldGenUtilsPlacementPerformancePatch patch = new WorldGenUtilsPlacementPerformancePatch();
+
+ assertTrue(patch.appliesTo(GameVersion.parse("42.21.0")));
+ assertTrue(patch.appliesTo(GameVersion.parse("42.21.3")));
+ assertFalse(patch.appliesTo(GameVersion.parse("42.20.3")));
+ assertFalse(patch.appliesTo(GameVersion.parse("42.22.0")));
+ }
+}
diff --git a/compat/b42/runtime/src/main/java/dev/overlord/zomboidforge/compat/b42/hooks/WorldGenHooks.java b/compat/b42/runtime/src/main/java/dev/overlord/zomboidforge/compat/b42/hooks/WorldGenHooks.java
new file mode 100644
index 0000000..63c88e1
--- /dev/null
+++ b/compat/b42/runtime/src/main/java/dev/overlord/zomboidforge/compat/b42/hooks/WorldGenHooks.java
@@ -0,0 +1,33 @@
+package dev.overlord.zomboidforge.compat.b42.hooks;
+
+import dev.overlord.zomboidforge.compat.b42.util.TileRules;
+
+import java.util.List;
+
+/**
+ * Hooks called by patched {@code zombie.iso.worldgen.WorldGenUtils} and
+ * {@code zombie.iso.worldgen.WorldGenChunk} code. Each returns what the vanilla code returns
+ * ({@link TileRules}).
+ */
+public final class WorldGenHooks
+{
+ private WorldGenHooks() {}
+
+ /** Replaces the body of {@code WorldGenUtils.canPlace(List, String)}, which reads no field. */
+ public static boolean canPlace(List> placement, String floorName)
+ {
+ return TileRules.canPlace(placement, floorName);
+ }
+
+ /** Replaces the body of {@code WorldGenChunk.isProtected(List, String)}, which reads no field. */
+ public static boolean isProtected(List> protectedTiles, String tile)
+ {
+ return TileRules.isProtected(protectedTiles, tile);
+ }
+
+ /** Replaces {@code floorMaterial.matches(mat)} in {@code WorldGenChunk.cleanChunk}. */
+ public static boolean matches(String input, String regex)
+ {
+ return TileRules.matches(input, regex);
+ }
+}
diff --git a/compat/b42/runtime/src/main/java/dev/overlord/zomboidforge/compat/b42/util/TileRules.java b/compat/b42/runtime/src/main/java/dev/overlord/zomboidforge/compat/b42/util/TileRules.java
new file mode 100644
index 0000000..18b0cb8
--- /dev/null
+++ b/compat/b42/runtime/src/main/java/dev/overlord/zomboidforge/compat/b42/util/TileRules.java
@@ -0,0 +1,121 @@
+package dev.overlord.zomboidforge.compat.b42.util;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Pattern;
+
+/**
+ * The tile rules of the world generation ({@code WorldGenUtils.canPlace},
+ * {@code WorldGenChunk.isProtected} and {@code cleanChunk}) with compiled patterns cached.
+ *
+ * Vanilla turns each rule into a regular expression per call ({@code "^" + rule}, then
+ * {@code .} quoted, {@code *} to {@code .*}, {@code ?} to {@code .?}) and matches it with
+ * {@code String.matches}, which compiles a new {@link Pattern} each time; this happens for every
+ * rule of every square of a new chunk. {@code String.matches(regex)} is
+ * {@code Pattern.compile(regex).matcher(this).matches()}, so a pattern compiled once gives the
+ * same answers. The rules come from the biome definitions, so the caches stay small. A rule that
+ * does not compile is not cached and throws on every call, as in vanilla.
+ */
+public final class TileRules
+{
+ private static final ConcurrentHashMap PATTERNS = new ConcurrentHashMap<>();
+ private static final ConcurrentHashMap PLACEMENTS = new ConcurrentHashMap<>();
+ private static final ConcurrentHashMap PROTECTED = new ConcurrentHashMap<>();
+
+ private TileRules() {}
+
+ /** A placement rule: {@code allows} is {@code false} for rules starting with {@code !}. */
+ private record Placement(boolean allows, Pattern pattern) {}
+
+ /** {@code input.matches(regex)}. */
+ public static boolean matches(String input, String regex)
+ {
+ // Vanilla calls matches on the input: a null input fails before the regex is compiled.
+ Objects.requireNonNull(input);
+
+ Pattern pattern = TileRules.PATTERNS.get(regex);
+ if (pattern == null)
+ {
+ pattern = Pattern.compile(regex);
+ TileRules.PATTERNS.putIfAbsent(regex, pattern);
+ }
+
+ return pattern.matcher(input).matches();
+ }
+
+ /**
+ * {@code WorldGenUtils.canPlace(placement, floorName)}: the last rule that matches decides,
+ * {@code false} when none matches.
+ */
+ public static boolean canPlace(List> placement, String floorName)
+ {
+ boolean toBePlaced = false;
+
+ for (Object entry : placement)
+ {
+ String rule = (String) entry;
+ Placement compiled = TileRules.PLACEMENTS.get(rule);
+
+ // Vanilla reads the rule first, then calls matches on floorName, then compiles.
+ Objects.requireNonNull(floorName);
+ if (compiled == null)
+ {
+ compiled = TileRules.placement(rule);
+ TileRules.PLACEMENTS.putIfAbsent(rule, compiled);
+ }
+ if (compiled.pattern().matcher(floorName).matches()) toBePlaced = compiled.allows();
+ }
+
+ return toBePlaced;
+ }
+
+ /**
+ * {@code WorldGenChunk.isProtected(protectedTiles, tile)}: whether any rule matches. Vanilla
+ * builds {@code "^" + rule}, so a {@code null} rule is the text {@code null}; it shares the
+ * cache key of that text.
+ */
+ public static boolean isProtected(List> protectedTiles, String tile)
+ {
+ for (Object entry : protectedTiles)
+ {
+ String rule = String.valueOf((String) entry);
+ Pattern compiled = TileRules.PROTECTED.get(rule);
+
+ Objects.requireNonNull(tile);
+ if (compiled == null)
+ {
+ compiled = Pattern.compile(TileRules.regex(rule));
+ TileRules.PROTECTED.putIfAbsent(rule, compiled);
+ }
+ if (compiled.matcher(tile).matches()) return true;
+ }
+
+ return false;
+ }
+
+ private static Placement placement(String rule)
+ {
+ boolean allows = true;
+ String body = rule;
+
+ if (body.startsWith("!"))
+ {
+ allows = false;
+ body = body.substring(1);
+ }
+
+ return new Placement(allows, Pattern.compile(TileRules.regex(body)));
+ }
+
+ /** The regular expression vanilla builds from a rule. */
+ private static String regex(String rule)
+ {
+ String regex = "^" + rule;
+ regex = regex.replace(".", "\\.");
+ regex = regex.replace("*", ".*");
+ regex = regex.replace("?", ".?");
+
+ return regex;
+ }
+}
diff --git a/compat/b42/runtime/src/test/java/dev/overlord/zomboidforge/compat/b42/util/TileRulesTest.java b/compat/b42/runtime/src/test/java/dev/overlord/zomboidforge/compat/b42/util/TileRulesTest.java
new file mode 100644
index 0000000..de4accf
--- /dev/null
+++ b/compat/b42/runtime/src/test/java/dev/overlord/zomboidforge/compat/b42/util/TileRulesTest.java
@@ -0,0 +1,198 @@
+package dev.overlord.zomboidforge.compat.b42.util;
+
+import org.junit.jupiter.api.Test;
+
+import zombie.iso.worldgen.WorldGenChunk;
+import zombie.iso.worldgen.WorldGenUtils;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+import java.util.TreeSet;
+import java.util.concurrent.Callable;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+/** Compares {@link TileRules} with the vanilla methods of the game jar. */
+class TileRulesTest
+{
+ /** Rules with every special character of the conversion, negation, and edge cases. */
+ private static final List RULES = List.of(
+ "blends_natural_01_*", "!blends_natural_02_*", "vegetation_drying*", "floors_exterior_natural_01_?",
+ "a.b", "*", "!*", "?", "blends_natural_01_1", "", "!", "null", "x*y?z"
+ );
+
+ private static final List NAMES = List.of(
+ "blends_natural_01_16", "blends_natural_02_3", "vegetation_drying_01_0", "floors_exterior_natural_01_1",
+ "floors_exterior_natural_01_12", "a.b", "axb", "", "null", "xyz", "x__y_z", "X", "blends_natural_01_1"
+ );
+
+ private static final Pattern QUOTED_RULE = Pattern.compile("\"(!?[A-Za-z0-9_.*?]+)\"");
+
+ /** Outcome of a call: its result or the class of what it threw. */
+ private static String outcome(Callable