Add support for anchors, hooks, and hook injection in patching logic with comprehensive tests.
ci / image (42.21, public) (push) Successful in 1m43s
ci / image (42.21, public) (push) Successful in 1m43s
Added onServerStarted Hook and Patch.
This commit is contained in:
1 parent
e13132c202
commit
2b94a49352
19 files changed
+972
-62
No files matched your search
@@ -7,4 +7,10 @@ plugins {
|
||||
dependencies {
|
||||
implementation(project(":patcher"))
|
||||
implementation(libs.bundles.asm)
|
||||
|
||||
// Hook classes are read as class files to check that the patches' hooks exist.
|
||||
testImplementation(project(":compat:b42:runtime"))
|
||||
testImplementation(platform(libs.junit.bom))
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testRuntimeOnly(libs.junit.platform.launcher)
|
||||
}
|
||||
+7
-8
@@ -6,16 +6,15 @@ import dev.overlord.zomboidforge.patcher.PatchSet;
|
||||
import java.util.List;
|
||||
|
||||
/** Every build-time patch for Build 42, in application order. */
|
||||
public final class B42PatchSet implements PatchSet {
|
||||
private static final List<ClassPatch> PATCHES = List.of();
|
||||
public final class B42PatchSet implements PatchSet
|
||||
{
|
||||
private static final List<ClassPatch> PATCHES = List.of(
|
||||
new ServerStartedPatch()
|
||||
);
|
||||
|
||||
@Override
|
||||
public int build() {
|
||||
return 42;
|
||||
}
|
||||
public int build() { return 42; }
|
||||
|
||||
@Override
|
||||
public List<ClassPatch> patches() {
|
||||
return PATCHES;
|
||||
}
|
||||
public List<ClassPatch> patches() { return B42PatchSet.PATCHES; }
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
import dev.overlord.zomboidforge.patcher.HookInjectionPatch;
|
||||
|
||||
/**
|
||||
* Calls {@code ServerHooks.onServerStarted()} in {@code GameServer.main}, directly after
|
||||
* {@code UnitTests.runIfEnabled()} and before the {@code while (!done)} main loop: the world is
|
||||
* loaded, the network port is open, and RCON is initialized.
|
||||
*/
|
||||
public final class ServerStartedPatch extends HookInjectionPatch
|
||||
{
|
||||
private static final Anchor.Call ANCHOR = new Anchor.Call(
|
||||
new Anchor.Method("main", "([Ljava/lang/String;)V"),
|
||||
"zombie/UnitTests", "runIfEnabled", "()V"
|
||||
);
|
||||
|
||||
private static final Hook HOOK = new Hook(
|
||||
"dev/overlord/zomboidforge/compat/b42/hooks/ServerHooks", "onServerStarted", "()V"
|
||||
);
|
||||
|
||||
public ServerStartedPatch() {
|
||||
super("zombie/network/GameServer", ServerStartedPatch.ANCHOR, ServerStartedPatch.HOOK);
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||
|
||||
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* The patcher does not check that a hook exists; a missing one fails only when the patched
|
||||
* game code runs, with a {@code NoSuchMethodError}. The hook classes are read as class files
|
||||
* because their signatures may mention game classes, which are not on this class path.
|
||||
*/
|
||||
class HookExistenceTest
|
||||
{
|
||||
@Test
|
||||
void everyHookOfThePatchSetExistsAsPublicStaticMethod() throws IOException
|
||||
{
|
||||
List<String> problems = new ArrayList<>();
|
||||
|
||||
for (ClassPatch patch : new B42PatchSet().patches())
|
||||
{
|
||||
Hook hook = patch.hook();
|
||||
if (hook == null) continue;
|
||||
|
||||
String problem = HookExistenceTest.problemWith(hook);
|
||||
if (problem != null) problems.add("'" + patch.getClass().getSimpleName() + "': " + problem);
|
||||
}
|
||||
|
||||
assertTrue(problems.isEmpty(), () -> String.join("\n", problems));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsAnExistingPublicStaticMethod() throws IOException
|
||||
{
|
||||
assertNull(HookExistenceTest.problemWith(new Hook("java/lang/String", "valueOf", "(I)Ljava/lang/String;")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsMissingClass() throws IOException
|
||||
{
|
||||
assertEquals(
|
||||
"Hook class 't/Missing' not found on the class path",
|
||||
HookExistenceTest.problemWith(new Hook("t/Missing", "hook", "()V"))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsMissingMethodOrDescriptor() throws IOException
|
||||
{
|
||||
assertEquals(
|
||||
"Hook method 'java/lang/String.nope()V' not found",
|
||||
HookExistenceTest.problemWith(new Hook("java/lang/String", "nope", "()V"))
|
||||
);
|
||||
assertEquals(
|
||||
"Hook method 'java/lang/String.valueOf(Z)V' not found",
|
||||
HookExistenceTest.problemWith(new Hook("java/lang/String", "valueOf", "(Z)V"))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsMethodThatIsNotPublicStatic() throws IOException
|
||||
{
|
||||
assertEquals(
|
||||
"Hook method 'java/lang/String.length()I' is not public static",
|
||||
HookExistenceTest.problemWith(new Hook("java/lang/String", "length", "()I"))
|
||||
);
|
||||
}
|
||||
|
||||
/** @return a description of what is wrong with {@code hook}, or {@code null} if it is usable */
|
||||
private static String problemWith(Hook hook) throws IOException
|
||||
{
|
||||
try (InputStream in = ClassLoader.getSystemResourceAsStream(hook.owner() + ".class"))
|
||||
{
|
||||
if (in == null) return "Hook class '" + hook.owner() + "' not found on the class path";
|
||||
|
||||
ClassNode node = new ClassNode();
|
||||
new ClassReader(in).accept(node, ClassReader.SKIP_CODE);
|
||||
|
||||
String description = "Hook method '" + hook.owner() + "." + hook.name() + hook.desc() + "'";
|
||||
|
||||
for (MethodNode method : node.methods)
|
||||
{
|
||||
if (!method.name.equals(hook.name()) || !method.desc.equals(hook.desc())) continue;
|
||||
|
||||
boolean publicStatic = (method.access & Opcodes.ACC_PUBLIC) != 0 && (method.access & Opcodes.ACC_STATIC) != 0;
|
||||
return publicStatic ? null : description + " is not public static";
|
||||
}
|
||||
|
||||
return description + " not found";
|
||||
}
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.support.PackageScan;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Structure rules for Build 42 patches and hooks. Class names end in {@code Patch} and
|
||||
* {@code Hooks}, so a class is recognizable as one or the other from its name alone.
|
||||
*/
|
||||
class NamingRulesTest
|
||||
{
|
||||
private static final String PATCHES_PACKAGE = "dev/overlord/zomboidforge/compat/b42/patches";
|
||||
private static final String HOOKS_PACKAGE = "dev/overlord/zomboidforge/compat/b42/hooks";
|
||||
|
||||
private static final String PATCH_SET = "B42PatchSet";
|
||||
|
||||
@Test
|
||||
void patchClassesEndInPatch() throws Exception
|
||||
{
|
||||
List<String> problems = new ArrayList<>();
|
||||
List<String> names = PackageScan.topLevelClassNames(NamingRulesTest.PATCHES_PACKAGE);
|
||||
|
||||
assertFalse(names.isEmpty(), "No classes found in '" + NamingRulesTest.PATCHES_PACKAGE + "'");
|
||||
|
||||
for (String name : names)
|
||||
{
|
||||
if (!name.equals(NamingRulesTest.PATCH_SET) && !name.endsWith("Patch")) {
|
||||
problems.add("'" + name + "' must end in 'Patch' (or be '" + NamingRulesTest.PATCH_SET + "')");
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(problems.isEmpty(), () -> String.join("\n", problems));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hookClassesEndInHooksAndExposeOnlyPublicStaticVoidMethods() throws Exception
|
||||
{
|
||||
List<String> problems = new ArrayList<>();
|
||||
List<String> names = PackageScan.topLevelClassNames(NamingRulesTest.HOOKS_PACKAGE);
|
||||
|
||||
assertFalse(names.isEmpty(), "No classes found in '" + NamingRulesTest.HOOKS_PACKAGE + "'");
|
||||
|
||||
for (String name : names)
|
||||
{
|
||||
if (!name.endsWith("Hooks")) problems.add("'" + name + "' must end in 'Hooks'");
|
||||
|
||||
ClassNode node = PackageScan.read(NamingRulesTest.HOOKS_PACKAGE + "/" + name);
|
||||
if ((node.access & Opcodes.ACC_FINAL) == 0) problems.add("'" + name + "' must be final");
|
||||
|
||||
for (MethodNode method : node.methods)
|
||||
{
|
||||
if ((method.access & Opcodes.ACC_SYNTHETIC) != 0 || method.name.equals("<clinit>")) continue;
|
||||
|
||||
if (method.name.equals("<init>"))
|
||||
{
|
||||
if ((method.access & Opcodes.ACC_PRIVATE) == 0) problems.add("'" + name + "' constructor must be private");
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean publicStatic = (method.access & Opcodes.ACC_PUBLIC) != 0 && (method.access & Opcodes.ACC_STATIC) != 0;
|
||||
if (!publicStatic || Type.getReturnType(method.desc) != Type.VOID_TYPE) {
|
||||
problems.add("'" + name + "." + method.name + method.desc + "' must be public static and return void");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(problems.isEmpty(), () -> String.join("\n", problems));
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.support.PackageScan;
|
||||
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* A patch class that is not in {@link B42PatchSet} is never applied. Patches are switched off
|
||||
* for debug builds with the {@code zf.disabledPatches} Gradle property, not by removing them
|
||||
* from the patch set.
|
||||
*/
|
||||
class PatchSetRegistrationTest
|
||||
{
|
||||
private static final String PATCHES_PACKAGE = "dev/overlord/zomboidforge/compat/b42/patches";
|
||||
|
||||
@Test
|
||||
void everyPatchClassIsRegisteredExactlyOnce() throws Exception
|
||||
{
|
||||
List<String> problems = new ArrayList<>();
|
||||
Set<String> registered = new TreeSet<>();
|
||||
|
||||
for (ClassPatch patch : new B42PatchSet().patches())
|
||||
{
|
||||
String name = patch.getClass().getSimpleName();
|
||||
if (!registered.add(name)) problems.add("'" + name + "' is registered more than once");
|
||||
}
|
||||
|
||||
for (String name : PackageScan.topLevelClassNames(PatchSetRegistrationTest.PATCHES_PACKAGE))
|
||||
{
|
||||
boolean abstractClass = (PackageScan.read(PatchSetRegistrationTest.PATCHES_PACKAGE + "/" + name).access & Opcodes.ACC_ABSTRACT) != 0;
|
||||
|
||||
if (name.endsWith("Patch") && !abstractClass && !registered.contains(name)) {
|
||||
problems.add("'" + name + "' is not registered in 'B42PatchSet'");
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(problems.isEmpty(), () -> String.join("\n", problems));
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.patches.support;
|
||||
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Lists and reads the classes of a package on the test class path, without loading them. Lives in
|
||||
* a subpackage so that the scanned packages do not contain it.
|
||||
*/
|
||||
public final class PackageScan
|
||||
{
|
||||
private PackageScan() {}
|
||||
|
||||
/** Simple names of the top-level non-test classes in {@code packagePath}, from a directory or a jar on the class path. */
|
||||
public static List<String> topLevelClassNames(String packagePath) throws IOException, URISyntaxException
|
||||
{
|
||||
List<String> names = new ArrayList<>();
|
||||
|
||||
for (URL url : Collections.list(ClassLoader.getSystemResources(packagePath)))
|
||||
{
|
||||
if (url.getProtocol().equals("file"))
|
||||
{
|
||||
try (Stream<Path> files = Files.list(Path.of(url.toURI()))) {
|
||||
files.map(file -> file.getFileName().toString()).forEach(names::add);
|
||||
}
|
||||
}
|
||||
else if (url.getProtocol().equals("jar"))
|
||||
{
|
||||
JarURLConnection connection = (JarURLConnection) url.openConnection();
|
||||
connection.getJarFile().stream()
|
||||
.map(entry -> entry.getName())
|
||||
.filter(entry -> entry.startsWith(packagePath + "/") && entry.indexOf('/', packagePath.length() + 1) < 0)
|
||||
.map(entry -> entry.substring(packagePath.length() + 1))
|
||||
.forEach(names::add);
|
||||
}
|
||||
}
|
||||
|
||||
return names.stream()
|
||||
.filter(file -> file.endsWith(".class") && !file.contains("$") && !file.equals("package-info.class"))
|
||||
.filter(file -> !file.endsWith("Test.class"))
|
||||
.map(file -> file.substring(0, file.length() - ".class".length()))
|
||||
.sorted()
|
||||
.toList();
|
||||
}
|
||||
|
||||
public static ClassNode read(String internalName) throws IOException
|
||||
{
|
||||
try (InputStream in = ClassLoader.getSystemResourceAsStream(internalName + ".class"))
|
||||
{
|
||||
ClassNode node = new ClassNode();
|
||||
new ClassReader(in).accept(node, ClassReader.SKIP_CODE);
|
||||
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user