diff --git a/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/B42PatchSet.java b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/B42PatchSet.java index c056012..dfc0c6c 100644 --- a/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/B42PatchSet.java +++ b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/B42PatchSet.java @@ -25,7 +25,8 @@ public final class B42PatchSet implements PatchSet new IsoZombieNetworkComponentPerformancePatch(), new IsoZombieSpotChancePerformancePatch(), new IsoGameCharacterStateMachinePerformancePatch(), - new ZombieVoronoiPerformancePatch() + new ZombieVoronoiPerformancePatch(), + new ZomboidFileSystemInitPerformancePatch() ); @Override diff --git a/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/ZomboidFileSystemInitPerformancePatch.java b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/ZomboidFileSystemInitPerformancePatch.java new file mode 100644 index 0000000..1fc2bc1 --- /dev/null +++ b/compat/b42/patches/src/main/java/dev/overlord/zomboidforge/compat/b42/perf/ZomboidFileSystemInitPerformancePatch.java @@ -0,0 +1,113 @@ +package dev.overlord.zomboidforge.compat.b42.perf; + +import dev.overlord.zomboidforge.api.GameVersion; +import dev.overlord.zomboidforge.patcher.Anchor; +import dev.overlord.zomboidforge.patcher.Hook; +import dev.overlord.zomboidforge.patcher.Patches.PerformancePatch; + +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.InsnList; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; +import org.objectweb.asm.tree.VarInsnNode; + +/** + * Replaces the call {@code this.searchFolders(workdir)} in {@code ZomboidFileSystem.init} with + * {@code ZomboidFileSystemHooks.searchAndIndex}, which also does the work of the loop after it: + * + *
{@code
+ * ZomboidFileSystemHooks.searchAndIndex(this, this.workdir.canonicalFile, this.loadList, this.allAbsolutePaths);
+ * for (int n = 0; n < this.loadList.size(); ++n) { ... } // loadList is empty now
+ * this.loadList.clear();
+ * }
+ *
+ * Vanilla checks each of the ~39,000 entries under {@code media/} for being a folder twice, and
+ * builds two URIs per entry for its relative path; the hook walks the folder once.
+ */
+public final class ZomboidFileSystemInitPerformancePatch extends PerformancePatch
+{
+ private static final String OWNER = "zombie/ZomboidFileSystem";
+ private static final Anchor.Method INIT = new Anchor.Method("init", "()V");
+
+ private static final Anchor.Call ANCHOR = new Anchor.Call(
+ ZomboidFileSystemInitPerformancePatch.INIT,
+ ZomboidFileSystemInitPerformancePatch.OWNER, "searchFolders", "(Ljava/io/File;)V"
+ );
+
+ /** The loop the hook takes over: the relative path of each entry. */
+ private static final Anchor.Call RELATIVE_PATH = new Anchor.Call(
+ ZomboidFileSystemInitPerformancePatch.INIT,
+ ZomboidFileSystemInitPerformancePatch.OWNER, "getRelativeFile", "(Ljava/lang/String;)Ljava/lang/String;"
+ );
+
+ /** {@code this.loadList.clear()} after the loop. */
+ private static final Anchor.Call CLEAR = new Anchor.Call(
+ ZomboidFileSystemInitPerformancePatch.INIT,
+ "java/util/List", "clear", "()V"
+ );
+
+ private static final Hook HOOK = new Hook(
+ "dev/overlord/zomboidforge/compat/b42/hooks/ZomboidFileSystemHooks", "searchAndIndex",
+ "(L" + ZomboidFileSystemInitPerformancePatch.OWNER + ";Ljava/io/File;Ljava/util/List;Ljava/util/Set;)V"
+ );
+
+ public ZomboidFileSystemInitPerformancePatch() {
+ super(ZomboidFileSystemInitPerformancePatch.OWNER, ZomboidFileSystemInitPerformancePatch.ANCHOR, ZomboidFileSystemInitPerformancePatch.HOOK);
+ }
+
+ //
+
+ @Override
+ public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
+
+ @Override
+ public void apply(ClassNode node)
+ {
+ MethodNode method = ZomboidFileSystemInitPerformancePatch.INIT.resolve(node);
+ MethodInsnNode call = ZomboidFileSystemInitPerformancePatch.ANCHOR.resolve(node);
+
+ ZomboidFileSystemInitPerformancePatch.RELATIVE_PATH.verify(node);
+ ZomboidFileSystemInitPerformancePatch.CLEAR.verify(node);
+ ZomboidFileSystemInitPerformancePatch.requireField(node, "loadList", "Ljava/util/List;");
+ ZomboidFileSystemInitPerformancePatch.requireField(node, "allAbsolutePaths", "Ljava/util/Set;");
+
+ int callIndex = method.instructions.indexOf(call);
+ if (callIndex > method.instructions.indexOf(ZomboidFileSystemInitPerformancePatch.RELATIVE_PATH.resolve(node)))
+ throw new IllegalStateException("'" + node.name + ".init': 'searchFolders' is called after 'getRelativeFile'.");
+
+ // The stack holds this and the folder for the virtual call; the hook takes both, then the two fields.
+ method.instructions.insertBefore(call, ZomboidFileSystemInitPerformancePatch.loadFields(node.name));
+ method.instructions.set(call, new MethodInsnNode(
+ Opcodes.INVOKESTATIC,
+ ZomboidFileSystemInitPerformancePatch.HOOK.owner(),
+ ZomboidFileSystemInitPerformancePatch.HOOK.name(),
+ ZomboidFileSystemInitPerformancePatch.HOOK.desc(),
+ false
+ ));
+ }
+
+ /** {@code this.loadList, this.allAbsolutePaths}. */
+ private static InsnList loadFields(String owner)
+ {
+ InsnList code = new InsnList();
+
+ code.add(new VarInsnNode(Opcodes.ALOAD, 0));
+ code.add(new FieldInsnNode(Opcodes.GETFIELD, owner, "loadList", "Ljava/util/List;"));
+ code.add(new VarInsnNode(Opcodes.ALOAD, 0));
+ code.add(new FieldInsnNode(Opcodes.GETFIELD, owner, "allAbsolutePaths", "Ljava/util/Set;"));
+
+ return code;
+ }
+
+ private static void requireField(ClassNode node, String name, String desc)
+ {
+ for (FieldNode field : node.fields) {
+ if (field.name.equals(name) && field.desc.equals(desc) && (field.access & Opcodes.ACC_STATIC) == 0) return;
+ }
+
+ throw new IllegalStateException("Expected the instance field '" + node.name + "." + name + " " + desc + "'.");
+ }
+}
diff --git a/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/ZomboidFileSystemInitPerformancePatchTest.java b/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/ZomboidFileSystemInitPerformancePatchTest.java
new file mode 100644
index 0000000..3c21ab1
--- /dev/null
+++ b/compat/b42/patches/src/test/java/dev/overlord/zomboidforge/compat/b42/perf/ZomboidFileSystemInitPerformancePatchTest.java
@@ -0,0 +1,197 @@
+package dev.overlord.zomboidforge.compat.b42.perf;
+
+import dev.overlord.zomboidforge.api.GameVersion;
+import dev.overlord.zomboidforge.compat.b42.patches.support.ClassDefiner;
+
+import org.junit.jupiter.api.Test;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.tree.AbstractInsnNode;
+import org.objectweb.asm.tree.ClassNode;
+import org.objectweb.asm.tree.FieldInsnNode;
+import org.objectweb.asm.tree.MethodInsnNode;
+import org.objectweb.asm.tree.MethodNode;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.objectweb.asm.Opcodes.ACC_FINAL;
+import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
+import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
+import static org.objectweb.asm.Opcodes.ACONST_NULL;
+import static org.objectweb.asm.Opcodes.ALOAD;
+import static org.objectweb.asm.Opcodes.CHECKCAST;
+import static org.objectweb.asm.Opcodes.GETFIELD;
+import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
+import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static org.objectweb.asm.Opcodes.INVOKESTATIC;
+import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
+import static org.objectweb.asm.Opcodes.POP;
+import static org.objectweb.asm.Opcodes.RETURN;
+import static org.objectweb.asm.Opcodes.V25;
+
+class ZomboidFileSystemInitPerformancePatchTest
+{
+ private static final String FILE_SYSTEM = "zombie/ZomboidFileSystem";
+ private static final String HOOKS = "dev/overlord/zomboidforge/compat/b42/hooks/ZomboidFileSystemHooks";
+
+ /**
+ * {@code ZomboidFileSystem} with a reduced {@code init}:
+ *
+ * {@code
+ * public void init()
+ * {
+ * this.searchFolders((File) null); // when search is true
+ * this.getRelativeFile((String) null); // when relative is true
+ * this.searchFolders((File) null); // when searchLast is true
+ * this.loadList.clear();
+ * }
+ * }
+ */
+ private static ClassNode fileSystem(boolean withSets, boolean search, boolean relative, boolean searchLast)
+ {
+ ClassNode node = new ClassNode();
+ node.visit(V25, ACC_PUBLIC | ACC_FINAL, ZomboidFileSystemInitPerformancePatchTest.FILE_SYSTEM, null, "java/lang/Object", null);
+
+ node.visitField(ACC_PRIVATE | ACC_FINAL, "loadList", "Ljava/util/List;", null, null);
+ if (withSets) node.visitField(ACC_PRIVATE | ACC_FINAL, "allAbsolutePaths", "Ljava/util/Set;", null, null);
+
+ MethodVisitor constructor = node.visitMethod(ACC_PRIVATE, "The loop, per entry of {@code loadList}: the relative path + * ({@code getRelativeFile(entry)}), lowercased, maps to the absolute path, with a trailing + * separator for folders, in {@code activeFileMap}; the absolute path goes into + * {@code allAbsolutePaths}. Here the folder checks of {@code searchFolders}, of the loop, and + * of {@code getRelativeFile} come from one walk ({@link MediaScan}). + * + *
{@code searchFolders} also yields to the render loop on clients
+ * ({@code Core.DoFrameReady()} per entry); without {@code GameServer.server} the vanilla call
+ * runs unchanged.
+ */
+ public static void searchAndIndex(ZomboidFileSystem fileSystem, File root, List Vanilla stats every entry under the media folder ({@code File.isDirectory}) and lists every
+ * folder; {@code init} then stats every entry again and builds two URIs per entry for its
+ * relative path. Here one walk stats each entry once and remembers which ones are folders.
+ */
+public final class MediaScan
+{
+ private MediaScan() {}
+
+ /**
+ * Appends what {@code searchFolders(root)} appends to {@code loadList}: every file, and every
+ * folder whose path contains {@code media/maps/}, depth first, in directory order, links
+ * followed. Returns for each appended path whether it is a folder, for the paths whose
+ * appended form names the same file (vanilla appends {@code path.replace("\\", "/")
+ * .replace("./", "")}, which may name another file).
+ */
+ public static Map