This commit is contained in:
commit
28c36c7eae
46 files changed
+2057
-82
No files matched your search
@@ -19,6 +19,9 @@ jobs:
|
|||||||
steam_branch: public
|
steam_branch: public
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
# release.sh derives the version from the newest v* tag
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Build and verify
|
- name: Build and verify
|
||||||
env:
|
env:
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ jobs:
|
|||||||
latest: "true"
|
latest: "true"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
# release.sh derives the version from the newest v* tag
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Log in to the registry
|
- name: Log in to the registry
|
||||||
env:
|
env:
|
||||||
|
|||||||
+10
-2
@@ -12,8 +12,11 @@
|
|||||||
# server reports another one. Default: pz.version in gradle.properties
|
# server reports another one. Default: pz.version in gradle.properties
|
||||||
# STEAMCLIENT bundled (default) or steamcmd: replace the server's steamclient.so
|
# STEAMCLIENT bundled (default) or steamcmd: replace the server's steamclient.so
|
||||||
# files with steamcmd's newer copies
|
# files with steamcmd's newer copies
|
||||||
# FORGE_VERSION Image label only; set by ci/release.sh
|
# FORGE_VERSION ZomboidForge version (jar manifest, image label); set by ci/release.sh.
|
||||||
|
# Default: 0.0.0-prerelease
|
||||||
# STEAM_BUILDID Image label only; set by ci/release.sh
|
# STEAM_BUILDID Image label only; set by ci/release.sh
|
||||||
|
# DISABLED_PATCHES Comma-separated simple class names of build-time patches to skip
|
||||||
|
# (debug builds only; ci/release.sh never sets it)
|
||||||
|
|
||||||
ARG PZ_BRANCH=public
|
ARG PZ_BRANCH=public
|
||||||
ARG PZ_VERSION=""
|
ARG PZ_VERSION=""
|
||||||
@@ -50,11 +53,16 @@ COPY agent agent
|
|||||||
COPY patcher patcher
|
COPY patcher patcher
|
||||||
COPY --from=server /server/java/projectzomboid.jar /pz/projectzomboid.jar
|
COPY --from=server /server/java/projectzomboid.jar /pz/projectzomboid.jar
|
||||||
|
|
||||||
# An empty PZ_VERSION keeps pz.version from gradle.properties.
|
# An empty PZ_VERSION keeps pz.version from gradle.properties; an empty FORGE_VERSION gives
|
||||||
|
# the build the fallback version 0.0.0-prerelease (the image has no git history).
|
||||||
ARG PZ_VERSION
|
ARG PZ_VERSION
|
||||||
|
ARG FORGE_VERSION
|
||||||
|
ARG DISABLED_PATCHES=""
|
||||||
RUN ./gradlew --no-daemon \
|
RUN ./gradlew --no-daemon \
|
||||||
-Ppz.jar=/pz/projectzomboid.jar \
|
-Ppz.jar=/pz/projectzomboid.jar \
|
||||||
${PZ_VERSION:+-Ppz.version=${PZ_VERSION}} \
|
${PZ_VERSION:+-Ppz.version=${PZ_VERSION}} \
|
||||||
|
${FORGE_VERSION:+-Pversion=${FORGE_VERSION}} \
|
||||||
|
${DISABLED_PATCHES:+-Pzf.disabledPatches=${DISABLED_PATCHES}} \
|
||||||
build :patcher:patchServerJar
|
build :patcher:patchServerJar
|
||||||
|
|
||||||
# --- Stage 3: runtime ------------------------------------------------------------
|
# --- Stage 3: runtime ------------------------------------------------------------
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.agent;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.api;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
@@ -2,6 +2,22 @@ plugins {
|
|||||||
java
|
java
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Version: -Pversion if given, else the newest v* git tag, else 0.0.0-prerelease.
|
||||||
|
if (version == Project.DEFAULT_VERSION)
|
||||||
|
{
|
||||||
|
val latestTag = providers.exec {
|
||||||
|
workingDir = layout.settingsDirectory.asFile
|
||||||
|
commandLine("git", "describe", "--tags", "--abbrev=0", "--match", "v*")
|
||||||
|
isIgnoreExitValue = true
|
||||||
|
}
|
||||||
|
|
||||||
|
version = latestTag.standardOutput.asText
|
||||||
|
.map { it.trim().removePrefix("v") }
|
||||||
|
.orElse("")
|
||||||
|
.get()
|
||||||
|
.ifEmpty { "0.0.0-prerelease" }
|
||||||
|
}
|
||||||
|
|
||||||
val projectZomboid = extensions.create<ProjectZomboidExtension>("projectZomboid")
|
val projectZomboid = extensions.create<ProjectZomboidExtension>("projectZomboid")
|
||||||
projectZomboid.serverJar.convention(
|
projectZomboid.serverJar.convention(
|
||||||
layout.settingsDirectory.file(
|
layout.settingsDirectory.file(
|
||||||
|
|||||||
+30
-6
@@ -7,13 +7,17 @@
|
|||||||
# <forge>-pz<pz> moves to the newest Steam build of that version
|
# <forge>-pz<pz> moves to the newest Steam build of that version
|
||||||
# latest only with LATEST=true
|
# latest only with LATEST=true
|
||||||
#
|
#
|
||||||
|
# <forge> is the newest v* git tag reachable from HEAD, without the "v" (v0.2.0 -> 0.2.0).
|
||||||
|
# A pre-release version (containing "-", e.g. 0.2.0-rc.1) is built and verified but never
|
||||||
|
# published. Without any v* tag the version is "0.0.0-prerelease".
|
||||||
|
#
|
||||||
# Nothing is built when the immutable tag already exists in the registry.
|
# Nothing is built when the immutable tag already exists in the registry.
|
||||||
#
|
#
|
||||||
# Environment:
|
# Environment:
|
||||||
# IMAGE Image name without tag, e.g. git.security-command.org/overlord/zomboidforge
|
# IMAGE Image name without tag, e.g. git.security-command.org/overlord/zomboidforge
|
||||||
# PZ_BRANCH Steam branch. Default: public
|
# PZ_BRANCH Steam branch. Default: public
|
||||||
# PZ_VERSION Expected Project Zomboid version. Default: pz.version in gradle.properties
|
# PZ_VERSION Expected Project Zomboid version. Default: pz.version in gradle.properties
|
||||||
# PUBLISH true: push the tags. Default: false (build and verify only)
|
# PUBLISH true: push the tags (not for pre-release versions). Default: false (build and verify only)
|
||||||
# LATEST true: also push "latest". Default: false
|
# LATEST true: also push "latest". Default: false
|
||||||
# CONTAINER_ENGINE docker or podman. Default: docker if installed, else podman
|
# CONTAINER_ENGINE docker or podman. Default: docker if installed, else podman
|
||||||
|
|
||||||
@@ -26,7 +30,7 @@ readonly REPO_ROOT
|
|||||||
|
|
||||||
readonly IMAGE="${IMAGE:?IMAGE is required}"
|
readonly IMAGE="${IMAGE:?IMAGE is required}"
|
||||||
readonly PZ_BRANCH="${PZ_BRANCH:-public}"
|
readonly PZ_BRANCH="${PZ_BRANCH:-public}"
|
||||||
readonly PUBLISH="${PUBLISH:-false}"
|
readonly REQUESTED_PUBLISH="${PUBLISH:-false}"
|
||||||
readonly LATEST="${LATEST:-false}"
|
readonly LATEST="${LATEST:-false}"
|
||||||
|
|
||||||
log() {
|
log() {
|
||||||
@@ -59,6 +63,16 @@ gradle_property() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Prints the forge version: the newest v* tag reachable from HEAD without its "v",
|
||||||
|
# or nothing when the repository has no such tag.
|
||||||
|
release_tag_version() {
|
||||||
|
local tag
|
||||||
|
|
||||||
|
if tag="$(git -C "${REPO_ROOT}" describe --tags --abbrev=0 --match 'v*' 2> /dev/null)"; then
|
||||||
|
echo "${tag#v}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
image_exists() {
|
image_exists() {
|
||||||
local reference="$1"
|
local reference="$1"
|
||||||
"$(container_engine)" manifest inspect "${reference}" > /dev/null 2>&1
|
"$(container_engine)" manifest inspect "${reference}" > /dev/null 2>&1
|
||||||
@@ -104,10 +118,20 @@ push_as() {
|
|||||||
|
|
||||||
main() {
|
main() {
|
||||||
local forge_version
|
local forge_version
|
||||||
|
local publish="${REQUESTED_PUBLISH}"
|
||||||
local pz_version
|
local pz_version
|
||||||
local build_id
|
local build_id
|
||||||
|
|
||||||
forge_version="$(gradle_property version)"
|
forge_version="$(release_tag_version)"
|
||||||
|
if [[ -z "${forge_version}" ]]; then
|
||||||
|
forge_version="0.0.0-prerelease"
|
||||||
|
log "No v* git tag found (shallow clone without tags?); using version ${forge_version}."
|
||||||
|
fi
|
||||||
|
if [[ "${forge_version}" == *-* ]]; then
|
||||||
|
publish=false
|
||||||
|
log "${forge_version} is a pre-release version; nothing will be published."
|
||||||
|
fi
|
||||||
|
|
||||||
pz_version="${PZ_VERSION:-$(gradle_property pz.version)}"
|
pz_version="${PZ_VERSION:-$(gradle_property pz.version)}"
|
||||||
build_id="$("${REPO_ROOT}/ci/steam-buildid.sh" "${PZ_BRANCH}")"
|
build_id="$("${REPO_ROOT}/ci/steam-buildid.sh" "${PZ_BRANCH}")"
|
||||||
|
|
||||||
@@ -115,7 +139,7 @@ main() {
|
|||||||
local version_tag="${IMAGE}:${forge_version}-pz${pz_version}"
|
local version_tag="${IMAGE}:${forge_version}-pz${pz_version}"
|
||||||
log "ZomboidForge ${forge_version}, Project Zomboid ${pz_version}, Steam branch ${PZ_BRANCH} build ${build_id}"
|
log "ZomboidForge ${forge_version}, Project Zomboid ${pz_version}, Steam branch ${PZ_BRANCH} build ${build_id}"
|
||||||
|
|
||||||
if [[ "${PUBLISH}" == true ]] && image_exists "${immutable_tag}"; then
|
if [[ "${publish}" == true ]] && image_exists "${immutable_tag}"; then
|
||||||
log "${immutable_tag} is already published; nothing to do."
|
log "${immutable_tag} is already published; nothing to do."
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@@ -123,8 +147,8 @@ main() {
|
|||||||
build_image "${immutable_tag}" "${forge_version}" "${pz_version}" "${build_id}"
|
build_image "${immutable_tag}" "${forge_version}" "${pz_version}" "${build_id}"
|
||||||
verify_build_id "${immutable_tag}" "${build_id}"
|
verify_build_id "${immutable_tag}" "${build_id}"
|
||||||
|
|
||||||
if [[ "${PUBLISH}" != true ]]; then
|
if [[ "${publish}" != true ]]; then
|
||||||
log "Built ${immutable_tag} (PUBLISH is not true; nothing pushed)"
|
log "Built ${immutable_tag} (not published)"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@@ -7,4 +7,10 @@ plugins {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(project(":patcher"))
|
implementation(project(":patcher"))
|
||||||
implementation(libs.bundles.asm)
|
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)
|
||||||
}
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.patches.ServerStartedPatch;
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.perf.IsoZombieNetworkComponentPerformancePatch;
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.perf.IsoZombieVehiclePerformancePatch;
|
||||||
|
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||||
|
import dev.overlord.zomboidforge.patcher.PatchSet;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
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(
|
||||||
|
new ServerStartedPatch(),
|
||||||
|
new IsoZombieVehiclePerformancePatch(),
|
||||||
|
new IsoZombieNetworkComponentPerformancePatch()
|
||||||
|
);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int build() { return 42; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull List<ClassPatch> patches() { return B42PatchSet.PATCHES; }
|
||||||
|
}
|
||||||
-21
@@ -1,21 +0,0 @@
|
|||||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
|
||||||
|
|
||||||
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
|
||||||
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();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int build() {
|
|
||||||
return 42;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ClassPatch> patches() {
|
|
||||||
return 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.Patches.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
+236
@@ -0,0 +1,236 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||||
|
import dev.overlord.zomboidforge.patcher.GameVersion;
|
||||||
|
import dev.overlord.zomboidforge.patcher.Patches.PerformancePatch;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import org.objectweb.asm.Opcodes;
|
||||||
|
import org.objectweb.asm.Type;
|
||||||
|
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||||
|
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.InsnNode;
|
||||||
|
import org.objectweb.asm.tree.JumpInsnNode;
|
||||||
|
import org.objectweb.asm.tree.LabelNode;
|
||||||
|
import org.objectweb.asm.tree.LdcInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
import org.objectweb.asm.tree.TypeInsnNode;
|
||||||
|
import org.objectweb.asm.tree.VarInsnNode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores the {@code NetworkZombieComponent} of an {@code IsoZombie} in a field instead of
|
||||||
|
* looking it up in the entity's component map on every call.
|
||||||
|
*
|
||||||
|
* <p>Every call site in {@code IsoZombie} has the same shape:
|
||||||
|
* {@code (NetworkZombieComponent) this.tryGetECSComponent(NetworkZombieComponent.class)}. The
|
||||||
|
* lookup walks the class hierarchy of the component class and reads a {@code HashMap}. The
|
||||||
|
* component is registered once by the {@code IsoObject} constructor and is never replaced or
|
||||||
|
* removed, so a value read once stays valid for the life of the zombie.
|
||||||
|
*
|
||||||
|
* <p>The patch adds the field and a private method that fills it on first use, and replaces the
|
||||||
|
* three instructions of each call site with a call to that method. A missing component is not
|
||||||
|
* stored: the method then returns {@code null} like the lookup does.
|
||||||
|
*/
|
||||||
|
public final class IsoZombieNetworkComponentPerformancePatch extends PerformancePatch
|
||||||
|
{
|
||||||
|
private static final String ZOMBIE = "zombie/characters/IsoZombie";
|
||||||
|
private static final String COMPONENT = "zombie/characters/component/NetworkZombieComponent";
|
||||||
|
|
||||||
|
private static final String LOOKUP_NAME = "tryGetECSComponent";
|
||||||
|
private static final String LOOKUP_DESC = "(Ljava/lang/Class;)Lzombie/characters/ecs/ECSComponent;";
|
||||||
|
|
||||||
|
private static final String FIELD_NAME = "zomboidForge$networkComponent";
|
||||||
|
private static final String FIELD_DESC = "L" + IsoZombieNetworkComponentPerformancePatch.COMPONENT + ";";
|
||||||
|
|
||||||
|
private static final String ACCESSOR_NAME = "zomboidForge$networkComponent";
|
||||||
|
private static final String ACCESSOR_DESC = "()" + IsoZombieNetworkComponentPerformancePatch.FIELD_DESC;
|
||||||
|
|
||||||
|
private static final Anchor.Call ANCHOR = new Anchor.Call(
|
||||||
|
new Anchor.Method("getOwner", "()Lzombie/core/raknet/UdpConnection;"),
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.LOOKUP_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.LOOKUP_DESC
|
||||||
|
);
|
||||||
|
|
||||||
|
public IsoZombieNetworkComponentPerformancePatch() {
|
||||||
|
super(IsoZombieNetworkComponentPerformancePatch.ZOMBIE, IsoZombieNetworkComponentPerformancePatch.ANCHOR, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ClassNode node)
|
||||||
|
{
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.requireNoMemberNamed(node);
|
||||||
|
|
||||||
|
node.fields.add(new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC, IsoZombieNetworkComponentPerformancePatch.FIELD_NAME, IsoZombieNetworkComponentPerformancePatch.FIELD_DESC, null, null));
|
||||||
|
node.methods.add(IsoZombieNetworkComponentPerformancePatch.accessor());
|
||||||
|
|
||||||
|
for (MethodNode method : node.methods) {
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.replaceLookups(method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The patch must not collide with a member of the game class, e.g. after an update. */
|
||||||
|
private static void requireNoMemberNamed(ClassNode node)
|
||||||
|
{
|
||||||
|
boolean fieldExists = node.fields.stream()
|
||||||
|
.anyMatch(field -> field.name.equals(IsoZombieNetworkComponentPerformancePatch.FIELD_NAME));
|
||||||
|
boolean methodExists = node.methods.stream()
|
||||||
|
.anyMatch(method -> method.name.equals(IsoZombieNetworkComponentPerformancePatch.ACCESSOR_NAME));
|
||||||
|
|
||||||
|
if (fieldExists || methodExists)
|
||||||
|
throw new IllegalStateException("'" + node.name + "' already has a member named '" + IsoZombieNetworkComponentPerformancePatch.FIELD_NAME + "'.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>{@code
|
||||||
|
* private NetworkZombieComponent zomboidForge$networkComponent()
|
||||||
|
* {
|
||||||
|
* NetworkZombieComponent component = this.zomboidForge$networkComponent;
|
||||||
|
*
|
||||||
|
* if (component == null) {
|
||||||
|
* component = (NetworkZombieComponent) this.tryGetECSComponent(NetworkZombieComponent.class);
|
||||||
|
* this.zomboidForge$networkComponent = component;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* return component;
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
|
*/
|
||||||
|
private static MethodNode accessor()
|
||||||
|
{
|
||||||
|
MethodNode method = new MethodNode(
|
||||||
|
Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ACCESSOR_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ACCESSOR_DESC,
|
||||||
|
null, null
|
||||||
|
);
|
||||||
|
LabelNode filled = new LabelNode();
|
||||||
|
InsnList code = method.instructions;
|
||||||
|
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||||
|
code.add(new FieldInsnNode(
|
||||||
|
Opcodes.GETFIELD,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.FIELD_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.FIELD_DESC
|
||||||
|
));
|
||||||
|
code.add(new VarInsnNode(Opcodes.ASTORE, 1));
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 1));
|
||||||
|
code.add(new JumpInsnNode(Opcodes.IFNONNULL, filled));
|
||||||
|
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||||
|
code.add(new LdcInsnNode(Type.getObjectType(IsoZombieNetworkComponentPerformancePatch.COMPONENT)));
|
||||||
|
code.add(new MethodInsnNode(
|
||||||
|
Opcodes.INVOKEVIRTUAL,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.LOOKUP_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.LOOKUP_DESC,
|
||||||
|
false
|
||||||
|
));
|
||||||
|
code.add(new TypeInsnNode(Opcodes.CHECKCAST, IsoZombieNetworkComponentPerformancePatch.COMPONENT));
|
||||||
|
code.add(new VarInsnNode(Opcodes.ASTORE, 1));
|
||||||
|
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 0));
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 1));
|
||||||
|
code.add(new FieldInsnNode(
|
||||||
|
Opcodes.PUTFIELD,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.FIELD_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.FIELD_DESC
|
||||||
|
));
|
||||||
|
|
||||||
|
code.add(filled);
|
||||||
|
code.add(new VarInsnNode(Opcodes.ALOAD, 1));
|
||||||
|
code.add(new InsnNode(Opcodes.ARETURN));
|
||||||
|
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces {@code ldc NetworkZombieComponent; invokevirtual tryGetECSComponent; checkcast
|
||||||
|
* NetworkZombieComponent} with {@code invokespecial zomboidForge$networkComponent}. The
|
||||||
|
* receiver is on the operand stack before and after, and the component instead of the
|
||||||
|
* {@code Class} leaves it, so the stack stays the same. The accessor itself is skipped: it
|
||||||
|
* contains the same three instructions.
|
||||||
|
*/
|
||||||
|
private static void replaceLookups(MethodNode method)
|
||||||
|
{
|
||||||
|
if (method.name.equals(IsoZombieNetworkComponentPerformancePatch.ACCESSOR_NAME)) return;
|
||||||
|
|
||||||
|
for (AbstractInsnNode instruction : method.instructions.toArray())
|
||||||
|
{
|
||||||
|
if (!IsoZombieNetworkComponentPerformancePatch.isLookup(instruction)) continue;
|
||||||
|
|
||||||
|
@Nullable AbstractInsnNode load = IsoZombieNetworkComponentPerformancePatch.previousInstruction(instruction);
|
||||||
|
@Nullable AbstractInsnNode cast = IsoZombieNetworkComponentPerformancePatch.nextInstruction(instruction);
|
||||||
|
|
||||||
|
if (!IsoZombieNetworkComponentPerformancePatch.isComponentClassLiteral(load)) continue;
|
||||||
|
if (!IsoZombieNetworkComponentPerformancePatch.isComponentCast(cast)) continue;
|
||||||
|
|
||||||
|
method.instructions.remove(load);
|
||||||
|
method.instructions.remove(cast);
|
||||||
|
method.instructions.set(
|
||||||
|
instruction, new MethodInsnNode(
|
||||||
|
Opcodes.INVOKESPECIAL,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ZOMBIE,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ACCESSOR_NAME,
|
||||||
|
IsoZombieNetworkComponentPerformancePatch.ACCESSOR_DESC,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
private static boolean isLookup(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
return instruction instanceof MethodInsnNode call
|
||||||
|
&& call.getOpcode() == Opcodes.INVOKEVIRTUAL
|
||||||
|
&& call.owner.equals(IsoZombieNetworkComponentPerformancePatch.ZOMBIE)
|
||||||
|
&& call.name.equals(IsoZombieNetworkComponentPerformancePatch.LOOKUP_NAME)
|
||||||
|
&& call.desc.equals(IsoZombieNetworkComponentPerformancePatch.LOOKUP_DESC);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isComponentClassLiteral(@Nullable AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
return instruction instanceof LdcInsnNode ldc
|
||||||
|
&& ldc.cst instanceof Type type
|
||||||
|
&& type.getInternalName().equals(IsoZombieNetworkComponentPerformancePatch.COMPONENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isComponentCast(@Nullable AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
return instruction instanceof TypeInsnNode cast
|
||||||
|
&& cast.getOpcode() == Opcodes.CHECKCAST
|
||||||
|
&& cast.desc.equals(IsoZombieNetworkComponentPerformancePatch.COMPONENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The closest earlier instruction that is not a label, line number, or frame; {@code null} at the start of the method. */
|
||||||
|
private static @Nullable AbstractInsnNode previousInstruction(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
AbstractInsnNode previous = instruction.getPrevious();
|
||||||
|
while (previous != null && previous.getOpcode() == -1) previous = previous.getPrevious();
|
||||||
|
|
||||||
|
return previous;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The closest later instruction that is not a label, line number, or frame; {@code null} at the end of the method. */
|
||||||
|
private static @Nullable AbstractInsnNode nextInstruction(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
AbstractInsnNode next = instruction.getNext();
|
||||||
|
while (next != null && next.getOpcode() == -1) next = next.getNext();
|
||||||
|
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
}
|
||||||
+92
@@ -0,0 +1,92 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||||
|
import dev.overlord.zomboidforge.patcher.GameVersion;
|
||||||
|
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.InsnList;
|
||||||
|
import org.objectweb.asm.tree.InsnNode;
|
||||||
|
import org.objectweb.asm.tree.JumpInsnNode;
|
||||||
|
import org.objectweb.asm.tree.LabelNode;
|
||||||
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes {@code IsoZombie.isVehicleBetween} cheaper without changing its result, in two steps:
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>Returns {@code false} at the start of the method when the cell has no vehicles. The
|
||||||
|
* loop would not run, but the three pooled vectors are allocated and released first.</li>
|
||||||
|
* <li>Replaces the call to {@code BaseVehicle.getIntersectPoint} with
|
||||||
|
* {@code ZombieVehicleHooks.getIntersectPoint}, which skips vehicles that the segment
|
||||||
|
* cannot reach.</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
public final class IsoZombieVehiclePerformancePatch extends PerformancePatch
|
||||||
|
{
|
||||||
|
private static final Anchor.Call ANCHOR = new Anchor.Call(
|
||||||
|
new Anchor.Method("isVehicleBetween", "(FFF)Z"),
|
||||||
|
"zombie/vehicles/BaseVehicle", "getIntersectPoint",
|
||||||
|
"(Lorg/joml/Vector3f;Lorg/joml/Vector3f;Lorg/joml/Vector3f;)Lorg/joml/Vector3f;"
|
||||||
|
);
|
||||||
|
|
||||||
|
private static final Hook HOOK = new Hook(
|
||||||
|
"dev/overlord/zomboidforge/compat/b42/hooks/ZombieVehicleHooks", "getIntersectPoint",
|
||||||
|
"(Lzombie/vehicles/BaseVehicle;Lorg/joml/Vector3f;Lorg/joml/Vector3f;Lorg/joml/Vector3f;)Lorg/joml/Vector3f;"
|
||||||
|
);
|
||||||
|
|
||||||
|
public IsoZombieVehiclePerformancePatch() {
|
||||||
|
super("zombie/characters/IsoZombie", IsoZombieVehiclePerformancePatch.ANCHOR, IsoZombieVehiclePerformancePatch.HOOK);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean appliesTo(GameVersion version) { return version.matches("42.21"); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ClassNode node)
|
||||||
|
{
|
||||||
|
MethodNode method = IsoZombieVehiclePerformancePatch.ANCHOR.in().resolve(node);
|
||||||
|
MethodInsnNode call = IsoZombieVehiclePerformancePatch.ANCHOR.resolve(node);
|
||||||
|
|
||||||
|
// The hook takes the receiver as its first argument, so the operand stack is the same
|
||||||
|
// as for the virtual call it replaces: vehicle, start, end, result -> Vector3f.
|
||||||
|
method.instructions.set(call, new MethodInsnNode(
|
||||||
|
Opcodes.INVOKESTATIC,
|
||||||
|
IsoZombieVehiclePerformancePatch.HOOK.owner(),
|
||||||
|
IsoZombieVehiclePerformancePatch.HOOK.name(),
|
||||||
|
IsoZombieVehiclePerformancePatch.HOOK.desc(),
|
||||||
|
false
|
||||||
|
));
|
||||||
|
|
||||||
|
method.instructions.insert(IsoZombieVehiclePerformancePatch.returnFalseWithoutVehicles());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code if (IsoWorld.instance.currentCell.getVehicles().isEmpty()) return false;}, the same
|
||||||
|
* expression the method iterates over. The method starts with an empty operand stack, so the
|
||||||
|
* stack map frame at the jump target is the one of the method entry; the class writer
|
||||||
|
* computes it.
|
||||||
|
*/
|
||||||
|
private static InsnList returnFalseWithoutVehicles()
|
||||||
|
{
|
||||||
|
LabelNode hasVehicles = new LabelNode();
|
||||||
|
InsnList code = new InsnList();
|
||||||
|
|
||||||
|
code.add(new FieldInsnNode(Opcodes.GETSTATIC, "zombie/iso/IsoWorld", "instance", "Lzombie/iso/IsoWorld;"));
|
||||||
|
code.add(new FieldInsnNode(Opcodes.GETFIELD, "zombie/iso/IsoWorld", "currentCell", "Lzombie/iso/IsoCell;"));
|
||||||
|
code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "zombie/iso/IsoCell", "getVehicles", "()Ljava/util/Set;", false));
|
||||||
|
code.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/util/Set", "isEmpty", "()Z", true));
|
||||||
|
code.add(new JumpInsnNode(Opcodes.IFEQ, hasVehicles));
|
||||||
|
code.add(new InsnNode(Opcodes.ICONST_0));
|
||||||
|
code.add(new InsnNode(Opcodes.IRETURN));
|
||||||
|
code.add(hasVehicles);
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
+1
-1
@@ -1 +1 @@
|
|||||||
dev.overlord.zomboidforge.compat.b42.patches.B42PatchSet
|
dev.overlord.zomboidforge.compat.b42.B42PatchSet
|
||||||
+105
@@ -0,0 +1,105 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.B42PatchSet;
|
||||||
|
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.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 List<String> PATCH_PACKAGES = List.of(
|
||||||
|
"dev/overlord/zomboidforge/compat/b42/patches",
|
||||||
|
"dev/overlord/zomboidforge/compat/b42/perf"
|
||||||
|
);
|
||||||
|
private static final String HOOKS_PACKAGE = "dev/overlord/zomboidforge/compat/b42/hooks";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void patchClassesEndInPatch() throws Exception
|
||||||
|
{
|
||||||
|
List<String> problems = new ArrayList<>();
|
||||||
|
|
||||||
|
for (String patchPackage : NamingRulesTest.PATCH_PACKAGES)
|
||||||
|
{
|
||||||
|
List<String> names = PackageScan.topLevelClassNames(patchPackage);
|
||||||
|
|
||||||
|
assertFalse(names.isEmpty(), "No classes found in '" + patchPackage + "'.");
|
||||||
|
|
||||||
|
for (String name : names)
|
||||||
|
{
|
||||||
|
if (!name.endsWith("Patch")) problems.add("'" + patchPackage + "/" + name + "' must end in 'Patch'.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(problems.isEmpty(), () -> String.join("\n", problems));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hookClassesEndInHooksAndExposeOnlyPublicStaticMethods() 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) problems.add("'" + name + "." + method.name + method.desc + "' must be public static");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(problems.isEmpty(), () -> String.join("\n", problems));
|
||||||
|
}
|
||||||
|
}
|
||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.B42PatchSet;
|
||||||
|
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 List<String> PATCH_PACKAGES = List.of(
|
||||||
|
"dev/overlord/zomboidforge/compat/b42/patches",
|
||||||
|
"dev/overlord/zomboidforge/compat/b42/perf"
|
||||||
|
);
|
||||||
|
|
||||||
|
@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 patchPackage : PatchSetRegistrationTest.PATCH_PACKAGES)
|
||||||
|
{
|
||||||
|
for (String name : PackageScan.topLevelClassNames(patchPackage))
|
||||||
|
{
|
||||||
|
boolean abstractClass = (PackageScan.read(patchPackage + "/" + 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.compat.b42.patches.support;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
+209
@@ -0,0 +1,209 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.GameVersion;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.objectweb.asm.ClassReader;
|
||||||
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
import org.objectweb.asm.Type;
|
||||||
|
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
import org.objectweb.asm.tree.FieldInsnNode;
|
||||||
|
import org.objectweb.asm.tree.LdcInsnNode;
|
||||||
|
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_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.ARETURN;
|
||||||
|
import static org.objectweb.asm.Opcodes.CHECKCAST;
|
||||||
|
import static org.objectweb.asm.Opcodes.GETFIELD;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
|
||||||
|
import static org.objectweb.asm.Opcodes.POP;
|
||||||
|
import static org.objectweb.asm.Opcodes.PUTFIELD;
|
||||||
|
import static org.objectweb.asm.Opcodes.RETURN;
|
||||||
|
import static org.objectweb.asm.Opcodes.V25;
|
||||||
|
|
||||||
|
class IsoZombieNetworkComponentPerformancePatchTest
|
||||||
|
{
|
||||||
|
private static final String ZOMBIE = "zombie/characters/IsoZombie";
|
||||||
|
private static final String COMPONENT = "zombie/characters/component/NetworkZombieComponent";
|
||||||
|
private static final String OTHER = "zombie/characters/component/OtherComponent";
|
||||||
|
private static final String LOOKUP = "(Ljava/lang/Class;)Lzombie/characters/ecs/ECSComponent;";
|
||||||
|
|
||||||
|
/** {@code this.tryGetECSComponent(component.class)} cast to {@code component}, result dropped. */
|
||||||
|
private static void lookup(MethodVisitor method, String component)
|
||||||
|
{
|
||||||
|
method.visitVarInsn(ALOAD, 0);
|
||||||
|
method.visitLdcInsn(Type.getObjectType(component));
|
||||||
|
method.visitMethodInsn(INVOKEVIRTUAL, IsoZombieNetworkComponentPerformancePatchTest.ZOMBIE, "tryGetECSComponent", IsoZombieNetworkComponentPerformancePatchTest.LOOKUP, false);
|
||||||
|
method.visitTypeInsn(CHECKCAST, component);
|
||||||
|
method.visitInsn(POP);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code IsoZombie} with {@code getOwner()} and {@code setOwner()}, each looking up the
|
||||||
|
* network component, and {@code other()}, which looks up an unrelated component.
|
||||||
|
*/
|
||||||
|
private static ClassNode target()
|
||||||
|
{
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||||
|
cw.visit(V25, ACC_PUBLIC, IsoZombieNetworkComponentPerformancePatchTest.ZOMBIE, null, "java/lang/Object", null);
|
||||||
|
|
||||||
|
MethodVisitor getOwner = cw.visitMethod(ACC_PUBLIC, "getOwner", "()Lzombie/core/raknet/UdpConnection;", null, null);
|
||||||
|
IsoZombieNetworkComponentPerformancePatchTest.lookup(getOwner, IsoZombieNetworkComponentPerformancePatchTest.COMPONENT);
|
||||||
|
getOwner.visitInsn(ACONST_NULL);
|
||||||
|
getOwner.visitInsn(ARETURN);
|
||||||
|
getOwner.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
MethodVisitor setOwner = cw.visitMethod(ACC_PUBLIC, "setOwner", "()V", null, null);
|
||||||
|
IsoZombieNetworkComponentPerformancePatchTest.lookup(setOwner, IsoZombieNetworkComponentPerformancePatchTest.COMPONENT);
|
||||||
|
setOwner.visitInsn(RETURN);
|
||||||
|
setOwner.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
MethodVisitor other = cw.visitMethod(ACC_PUBLIC, "other", "()V", null, null);
|
||||||
|
IsoZombieNetworkComponentPerformancePatchTest.lookup(other, IsoZombieNetworkComponentPerformancePatchTest.OTHER);
|
||||||
|
other.visitInsn(RETURN);
|
||||||
|
other.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
cw.visitEnd();
|
||||||
|
|
||||||
|
ClassNode node = new ClassNode();
|
||||||
|
new ClassReader(cw.toByteArray()).accept(node, 0);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MethodNode method(ClassNode node, String name)
|
||||||
|
{
|
||||||
|
return node.methods.stream().filter(method -> method.name.equals(name)).findFirst().orElseThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<MethodInsnNode> calls(MethodNode method, String name)
|
||||||
|
{
|
||||||
|
List<MethodInsnNode> calls = new ArrayList<>();
|
||||||
|
|
||||||
|
for (AbstractInsnNode instruction : method.instructions) {
|
||||||
|
if (instruction instanceof MethodInsnNode call && call.name.equals(name)) calls.add(call);
|
||||||
|
}
|
||||||
|
|
||||||
|
return calls;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ClassNode patched()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieNetworkComponentPerformancePatchTest.target();
|
||||||
|
new IsoZombieNetworkComponentPerformancePatch().apply(node);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void anchorMatchesTheTarget()
|
||||||
|
{
|
||||||
|
new IsoZombieNetworkComponentPerformancePatch().anchor().verify(IsoZombieNetworkComponentPerformancePatchTest.target());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addsAPrivateFieldAndAnAccessor()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieNetworkComponentPerformancePatchTest.patched();
|
||||||
|
|
||||||
|
assertEquals(1, node.fields.size());
|
||||||
|
assertEquals("zomboidForge$networkComponent", node.fields.getFirst().name);
|
||||||
|
assertEquals("L" + IsoZombieNetworkComponentPerformancePatchTest.COMPONENT + ";", node.fields.getFirst().desc);
|
||||||
|
assertTrue((node.fields.getFirst().access & ACC_PRIVATE) != 0);
|
||||||
|
|
||||||
|
MethodNode accessor = IsoZombieNetworkComponentPerformancePatchTest.method(node, "zomboidForge$networkComponent");
|
||||||
|
assertTrue((accessor.access & ACC_PRIVATE) != 0);
|
||||||
|
assertEquals("()L" + IsoZombieNetworkComponentPerformancePatchTest.COMPONENT + ";", accessor.desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void accessorFillsTheFieldOnceFromTheLookup()
|
||||||
|
{
|
||||||
|
MethodNode accessor = IsoZombieNetworkComponentPerformancePatchTest.method(IsoZombieNetworkComponentPerformancePatchTest.patched(), "zomboidForge$networkComponent");
|
||||||
|
|
||||||
|
assertEquals(1, IsoZombieNetworkComponentPerformancePatchTest.calls(accessor, "tryGetECSComponent").size());
|
||||||
|
|
||||||
|
int reads = 0;
|
||||||
|
int writes = 0;
|
||||||
|
for (AbstractInsnNode instruction : accessor.instructions)
|
||||||
|
{
|
||||||
|
if (instruction instanceof FieldInsnNode field && field.getOpcode() == GETFIELD) reads++;
|
||||||
|
if (instruction instanceof FieldInsnNode field && field.getOpcode() == PUTFIELD) writes++;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(1, reads);
|
||||||
|
assertEquals(1, writes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void replacesEveryLookupOfTheNetworkComponentWithTheAccessor()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieNetworkComponentPerformancePatchTest.patched();
|
||||||
|
|
||||||
|
for (String name : List.of("getOwner", "setOwner"))
|
||||||
|
{
|
||||||
|
MethodNode method = IsoZombieNetworkComponentPerformancePatchTest.method(node, name);
|
||||||
|
|
||||||
|
assertTrue(IsoZombieNetworkComponentPerformancePatchTest.calls(method, "tryGetECSComponent").isEmpty(), name);
|
||||||
|
|
||||||
|
List<MethodInsnNode> accessors = IsoZombieNetworkComponentPerformancePatchTest.calls(method, "zomboidForge$networkComponent");
|
||||||
|
assertEquals(1, accessors.size(), name);
|
||||||
|
assertEquals(INVOKESPECIAL, accessors.getFirst().getOpcode(), name);
|
||||||
|
|
||||||
|
// No class literal and no cast remain: the accessor returns the component itself.
|
||||||
|
for (AbstractInsnNode instruction : method.instructions)
|
||||||
|
{
|
||||||
|
assertFalse(instruction instanceof LdcInsnNode, name);
|
||||||
|
assertFalse(instruction.getOpcode() == CHECKCAST, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void leavesLookupsOfOtherComponentsAlone()
|
||||||
|
{
|
||||||
|
MethodNode other = IsoZombieNetworkComponentPerformancePatchTest.method(IsoZombieNetworkComponentPerformancePatchTest.patched(), "other");
|
||||||
|
|
||||||
|
assertEquals(1, IsoZombieNetworkComponentPerformancePatchTest.calls(other, "tryGetECSComponent").size());
|
||||||
|
assertTrue(IsoZombieNetworkComponentPerformancePatchTest.calls(other, "zomboidForge$networkComponent").isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void patchedClassIsWrittenWithComputedFrames()
|
||||||
|
{
|
||||||
|
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||||
|
IsoZombieNetworkComponentPerformancePatchTest.patched().accept(writer);
|
||||||
|
|
||||||
|
assertTrue(writer.toByteArray().length > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void failsWhenTheGameClassAlreadyHasTheMember()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieNetworkComponentPerformancePatchTest.patched();
|
||||||
|
|
||||||
|
assertThrows(IllegalStateException.class, () -> new IsoZombieNetworkComponentPerformancePatch().apply(node));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void appliesOnlyToVerifiedVersions()
|
||||||
|
{
|
||||||
|
IsoZombieNetworkComponentPerformancePatch patch = new IsoZombieNetworkComponentPerformancePatch();
|
||||||
|
|
||||||
|
assertTrue(patch.appliesTo(GameVersion.parse("42.21.0")));
|
||||||
|
assertFalse(patch.appliesTo(GameVersion.parse("42.20.3")));
|
||||||
|
assertFalse(patch.appliesTo(GameVersion.parse("42.22.0")));
|
||||||
|
}
|
||||||
|
}
|
||||||
+162
@@ -0,0 +1,162 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.GameVersion;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.objectweb.asm.ClassReader;
|
||||||
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
import org.objectweb.asm.tree.JumpInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
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.assertTrue;
|
||||||
|
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.CHECKCAST;
|
||||||
|
import static org.objectweb.asm.Opcodes.GETFIELD;
|
||||||
|
import static org.objectweb.asm.Opcodes.GETSTATIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.ICONST_0;
|
||||||
|
import static org.objectweb.asm.Opcodes.IFEQ;
|
||||||
|
import static org.objectweb.asm.Opcodes.ILOAD;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
|
||||||
|
import static org.objectweb.asm.Opcodes.IRETURN;
|
||||||
|
import static org.objectweb.asm.Opcodes.ISTORE;
|
||||||
|
import static org.objectweb.asm.Opcodes.POP;
|
||||||
|
import static org.objectweb.asm.Opcodes.V25;
|
||||||
|
|
||||||
|
class IsoZombieVehiclePerformancePatchTest
|
||||||
|
{
|
||||||
|
private static final String VECTOR = "org/joml/Vector3f";
|
||||||
|
private static final String CALL = "(L" + VECTOR + ";L" + VECTOR + ";L" + VECTOR + ";)L" + VECTOR + ";";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code IsoZombie} with a reduced {@code isVehicleBetween}: one call to
|
||||||
|
* {@code BaseVehicle.getIntersectPoint}, result ignored, then {@code return intersect}.
|
||||||
|
*/
|
||||||
|
private static ClassNode target()
|
||||||
|
{
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||||
|
cw.visit(V25, ACC_PUBLIC, "zombie/characters/IsoZombie", null, "java/lang/Object", null);
|
||||||
|
|
||||||
|
MethodVisitor m = cw.visitMethod(ACC_PRIVATE, "isVehicleBetween", "(FFF)Z", null, null);
|
||||||
|
m.visitInsn(ICONST_0);
|
||||||
|
m.visitVarInsn(ISTORE, 4);
|
||||||
|
m.visitInsn(ACONST_NULL);
|
||||||
|
m.visitTypeInsn(CHECKCAST, "zombie/vehicles/BaseVehicle");
|
||||||
|
m.visitInsn(ACONST_NULL);
|
||||||
|
m.visitInsn(ACONST_NULL);
|
||||||
|
m.visitInsn(ACONST_NULL);
|
||||||
|
m.visitMethodInsn(INVOKEVIRTUAL, "zombie/vehicles/BaseVehicle", "getIntersectPoint", IsoZombieVehiclePerformancePatchTest.CALL, false);
|
||||||
|
m.visitInsn(POP);
|
||||||
|
m.visitVarInsn(ILOAD, 4);
|
||||||
|
m.visitInsn(IRETURN);
|
||||||
|
m.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
cw.visitEnd();
|
||||||
|
|
||||||
|
ClassNode node = new ClassNode();
|
||||||
|
new ClassReader(cw.toByteArray()).accept(node, 0);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MethodNode isVehicleBetween(ClassNode node)
|
||||||
|
{
|
||||||
|
return node.methods.stream().filter(method -> method.name.equals("isVehicleBetween")).findFirst().orElseThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Opcodes of the real instructions: no labels, line numbers, or frames. */
|
||||||
|
private static List<Integer> opcodes(MethodNode method)
|
||||||
|
{
|
||||||
|
List<Integer> opcodes = new ArrayList<>();
|
||||||
|
|
||||||
|
for (AbstractInsnNode instruction : method.instructions) {
|
||||||
|
if (instruction.getOpcode() != -1) opcodes.add(instruction.getOpcode());
|
||||||
|
}
|
||||||
|
|
||||||
|
return opcodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void anchorMatchesTheTarget()
|
||||||
|
{
|
||||||
|
new IsoZombieVehiclePerformancePatch().anchor().verify(IsoZombieVehiclePerformancePatchTest.target());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void returnsFalseFirstWhenTheCellHasNoVehicles()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieVehiclePerformancePatchTest.target();
|
||||||
|
new IsoZombieVehiclePerformancePatch().apply(node);
|
||||||
|
|
||||||
|
List<Integer> opcodes = IsoZombieVehiclePerformancePatchTest.opcodes(IsoZombieVehiclePerformancePatchTest.isVehicleBetween(node));
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
List.of(GETSTATIC, GETFIELD, INVOKEVIRTUAL, INVOKEINTERFACE, IFEQ, ICONST_0, IRETURN),
|
||||||
|
opcodes.subList(0, 7)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void jumpsOverTheEarlyReturnToTheOriginalCode()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieVehiclePerformancePatchTest.target();
|
||||||
|
new IsoZombieVehiclePerformancePatch().apply(node);
|
||||||
|
|
||||||
|
MethodNode method = IsoZombieVehiclePerformancePatchTest.isVehicleBetween(node);
|
||||||
|
JumpInsnNode jump = Arrays.stream(method.instructions.toArray())
|
||||||
|
.filter(JumpInsnNode.class::isInstance)
|
||||||
|
.map(JumpInsnNode.class::cast)
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow();
|
||||||
|
|
||||||
|
// The label sits directly before the original first instruction, iconst_0 of "intersectVision = false".
|
||||||
|
AbstractInsnNode firstOriginal = jump.label.getNext();
|
||||||
|
while (firstOriginal.getOpcode() == -1) firstOriginal = firstOriginal.getNext();
|
||||||
|
|
||||||
|
assertEquals(ICONST_0, firstOriginal.getOpcode());
|
||||||
|
assertEquals(ISTORE, firstOriginal.getNext().getOpcode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void replacesTheVirtualCallWithTheHook()
|
||||||
|
{
|
||||||
|
ClassNode node = IsoZombieVehiclePerformancePatchTest.target();
|
||||||
|
new IsoZombieVehiclePerformancePatch().apply(node);
|
||||||
|
|
||||||
|
List<MethodInsnNode> calls = new ArrayList<>();
|
||||||
|
for (AbstractInsnNode instruction : IsoZombieVehiclePerformancePatchTest.isVehicleBetween(node).instructions) {
|
||||||
|
if (instruction instanceof MethodInsnNode call && call.name.equals("getIntersectPoint")) calls.add(call);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(1, calls.size());
|
||||||
|
MethodInsnNode call = calls.getFirst();
|
||||||
|
|
||||||
|
assertEquals(INVOKESTATIC, call.getOpcode());
|
||||||
|
assertEquals("dev/overlord/zomboidforge/compat/b42/hooks/ZombieVehicleHooks", call.owner);
|
||||||
|
assertEquals("(Lzombie/vehicles/BaseVehicle;L" + VECTOR + ";L" + VECTOR + ";L" + VECTOR + ";)L" + VECTOR + ";", call.desc);
|
||||||
|
assertFalse(call.itf);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void appliesOnlyToVerifiedVersions()
|
||||||
|
{
|
||||||
|
IsoZombieVehiclePerformancePatch patch = new IsoZombieVehiclePerformancePatch();
|
||||||
|
|
||||||
|
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")));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,4 +9,8 @@ dependencies {
|
|||||||
implementation(project(":core"))
|
implementation(project(":core"))
|
||||||
implementation(project(":api"))
|
implementation(project(":api"))
|
||||||
compileOnly(files(projectZomboid.serverJar))
|
compileOnly(files(projectZomboid.serverJar))
|
||||||
|
|
||||||
|
testImplementation(platform(libs.junit.bom))
|
||||||
|
testImplementation(libs.junit.jupiter)
|
||||||
|
testRuntimeOnly(libs.junit.platform.launcher)
|
||||||
}
|
}
|
||||||
+17
@@ -0,0 +1,17 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.api.ZomboidForge;
|
||||||
|
|
||||||
|
/** Hooks called by patched {@code zombie.network.GameServer} code. */
|
||||||
|
public final class ServerHooks
|
||||||
|
{
|
||||||
|
private ServerHooks() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called once on the main thread, after the world is loaded, the network port is open, and
|
||||||
|
* RCON is initialized; directly before the main loop starts.
|
||||||
|
*/
|
||||||
|
public static void onServerStarted() {
|
||||||
|
System.out.println("[ZomboidForge] Server started (ZomboidForge " + ZomboidForge.VERSION + ").");
|
||||||
|
}
|
||||||
|
}
|
||||||
+57
@@ -0,0 +1,57 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.compat.b42.util.SegmentReach;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
|
import zombie.core.physics.WorldSimulation;
|
||||||
|
import zombie.scripting.objects.VehicleScript;
|
||||||
|
import zombie.vehicles.BaseVehicle;
|
||||||
|
|
||||||
|
/** Hooks called by patched {@code zombie.characters.IsoZombie} code. */
|
||||||
|
public final class ZombieVehicleHooks
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* World units added to the bounding radius. Float resolution near the world coordinates
|
||||||
|
* (about 10000) is 0.001, so this covers rounding differences to the vanilla test.
|
||||||
|
*/
|
||||||
|
private static final float MARGIN = 0.1F;
|
||||||
|
|
||||||
|
private ZombieVehicleHooks() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the call {@code vehicle.getIntersectPoint(start, end, result)} in
|
||||||
|
* {@code IsoZombie.isVehicleBetween}. Returns what that call returns.
|
||||||
|
*
|
||||||
|
* <p>The vanilla test is 2-dimensional: {@code getLocalPos} drops the height of both
|
||||||
|
* points, and the vehicle's box is centered on its physics origin with the half extents
|
||||||
|
* {@code extents * 0.5 + centerOfMassOffset}. Whatever its rotation, the box lies inside
|
||||||
|
* the circle around that origin with the radius of its half diagonal. A segment that stays
|
||||||
|
* outside the circle cannot hit the box, so the vanilla call returns {@code null} for it
|
||||||
|
* and is skipped here.
|
||||||
|
*/
|
||||||
|
public static @Nullable Vector3f getIntersectPoint(
|
||||||
|
@NotNull BaseVehicle vehicle,
|
||||||
|
@NotNull Vector3f start,
|
||||||
|
@NotNull Vector3f end,
|
||||||
|
@Nullable Vector3f result
|
||||||
|
) {
|
||||||
|
VehicleScript script = vehicle.getScript();
|
||||||
|
Vector3f extents = script.getExtents();
|
||||||
|
Vector3f centerOfMass = script.getCenterOfMassOffset();
|
||||||
|
|
||||||
|
float halfX = extents.x * 0.5F + centerOfMass.x;
|
||||||
|
float halfZ = extents.z * 0.5F + centerOfMass.z;
|
||||||
|
float radius = (float) Math.sqrt(halfX * halfX + halfZ * halfZ) + ZombieVehicleHooks.MARGIN;
|
||||||
|
|
||||||
|
// Same position that BaseVehicle.getWorldPos and getLocalPos use for the physics origin.
|
||||||
|
float centerX = vehicle.jniTransform.origin.x + WorldSimulation.instance.offsetX;
|
||||||
|
float centerY = vehicle.jniTransform.origin.z + WorldSimulation.instance.offsetY;
|
||||||
|
|
||||||
|
if (SegmentReach.missesCircle(start.x, start.y, end.x, end.y, centerX, centerY, radius)) return null;
|
||||||
|
|
||||||
|
return vehicle.getIntersectPoint(start, end, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
+5
@@ -1,5 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* Build 42 runtime code: hooks that patched game classes call, and adapters between
|
* Build 42 runtime code: hooks that patched game classes call, and adapters between
|
||||||
* game classes and the version-independent {@code core} and {@code api}.
|
* game classes and the version-independent {@code core} and {@code api}.
|
||||||
|
*
|
||||||
|
* @noinspection UnstableApiUsage, RedundantSuppression
|
||||||
*/
|
*/
|
||||||
|
@NotNullByDefault
|
||||||
package dev.overlord.zomboidforge.compat.b42;
|
package dev.overlord.zomboidforge.compat.b42;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.util;
|
||||||
|
|
||||||
|
/** Distance checks in the XY plane. Uses no game classes, so it can be tested on its own. */
|
||||||
|
public final class SegmentReach
|
||||||
|
{
|
||||||
|
private SegmentReach() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether every point of the segment from ({@code startX}, {@code startY}) to
|
||||||
|
* ({@code endX}, {@code endY}) is farther than {@code radius} from
|
||||||
|
* ({@code centerX}, {@code centerY}).
|
||||||
|
*
|
||||||
|
* <p>False when any input is NaN. A caller that skips work only on {@code true} therefore
|
||||||
|
* still runs the full computation for such input.
|
||||||
|
*/
|
||||||
|
public static boolean missesCircle(
|
||||||
|
float startX, float startY, float endX, float endY,
|
||||||
|
float centerX, float centerY, float radius
|
||||||
|
) {
|
||||||
|
float directionX = endX - startX;
|
||||||
|
float directionY = endY - startY;
|
||||||
|
float lengthSquared = directionX * directionX + directionY * directionY;
|
||||||
|
|
||||||
|
// Position of the point on the segment closest to the center, from 0 (start) to 1 (end).
|
||||||
|
// A segment of length 0 is its start. NaN passes through every comparison unchanged.
|
||||||
|
float along = lengthSquared == 0.0F
|
||||||
|
? 0.0F
|
||||||
|
: ((centerX - startX) * directionX + (centerY - startY) * directionY) / lengthSquared;
|
||||||
|
|
||||||
|
if (along < 0.0F) along = 0.0F;
|
||||||
|
else if (along > 1.0F) along = 1.0F;
|
||||||
|
|
||||||
|
float offsetX = startX + along * directionX - centerX;
|
||||||
|
float offsetY = startY + along * directionY - centerY;
|
||||||
|
|
||||||
|
return offsetX * offsetX + offsetY * offsetY > radius * radius;
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.compat.b42.util;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
+88
@@ -0,0 +1,88 @@
|
|||||||
|
package dev.overlord.zomboidforge.compat.b42.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class SegmentReachTest
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
void segmentFarFromTheCircleMisses()
|
||||||
|
{
|
||||||
|
assertTrue(SegmentReach.missesCircle(0, 10, 20, 10, 10, 0, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void segmentThroughTheCenterDoesNotMiss()
|
||||||
|
{
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, 0, 20, 0, 10, 0, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void segmentThatEndsBeforeTheCircleMissesEvenWhenItsLineWouldHit()
|
||||||
|
{
|
||||||
|
// The line y = 0 passes through the circle at (10, 0); the segment stops at x = 5.
|
||||||
|
assertTrue(SegmentReach.missesCircle(0, 0, 5, 0, 10, 0, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void segmentThatStartsAfterTheCircleMissesEvenWhenItsLineWouldHit()
|
||||||
|
{
|
||||||
|
assertTrue(SegmentReach.missesCircle(15, 0, 30, 0, 10, 0, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void segmentThatTouchesTheCircleEdgeDoesNotMiss()
|
||||||
|
{
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, 5, 20, 5, 10, 0, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void segmentJustOutsideTheCircleMisses()
|
||||||
|
{
|
||||||
|
assertTrue(SegmentReach.missesCircle(0, 5.001F, 20, 5.001F, 10, 0, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void segmentStartingInsideTheCircleDoesNotMiss()
|
||||||
|
{
|
||||||
|
assertFalse(SegmentReach.missesCircle(10, 1, 100, 100, 10, 0, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void segmentOfLengthZeroIsItsStart()
|
||||||
|
{
|
||||||
|
assertFalse(SegmentReach.missesCircle(10, 1, 10, 1, 10, 0, 2));
|
||||||
|
assertTrue(SegmentReach.missesCircle(50, 50, 50, 50, 10, 0, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void diagonalSegmentIsMeasuredToItsClosestPoint()
|
||||||
|
{
|
||||||
|
// Closest point of (0,0)-(10,10) to (10,0) is (5,5), distance sqrt(50) = 7.07.
|
||||||
|
assertTrue(SegmentReach.missesCircle(0, 0, 10, 10, 10, 0, 7.0F));
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, 0, 10, 10, 10, 0, 7.2F));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void worksAtWorldCoordinates()
|
||||||
|
{
|
||||||
|
assertFalse(SegmentReach.missesCircle(10720, 10195, 10760, 10195, 10740, 10196, 2));
|
||||||
|
assertTrue(SegmentReach.missesCircle(10720, 10195, 10760, 10195, 10740, 10210, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void neverMissesOnNaN()
|
||||||
|
{
|
||||||
|
float nan = Float.NaN;
|
||||||
|
|
||||||
|
assertFalse(SegmentReach.missesCircle(nan, 0, 20, 0, 10, 100, 1));
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, nan, 20, 0, 10, 100, 1));
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, 0, nan, 0, 10, 100, 1));
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, 0, 20, nan, 10, 100, 1));
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, 0, 20, 0, nan, 100, 1));
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, 0, 20, 0, 10, nan, 1));
|
||||||
|
assertFalse(SegmentReach.missesCircle(0, 0, 20, 0, 10, 100, nan));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* Version-independent runtime: mod loading, events, configuration. Contains no
|
* Version-independent runtime: mod loading, events, configuration. Contains no
|
||||||
* references to game classes; game-specific code is in the compat modules.
|
* references to game classes; game-specific code is in the compat modules.
|
||||||
|
*
|
||||||
|
* @noinspection UnstableApiUsage, RedundantSuppression
|
||||||
*/
|
*/
|
||||||
|
@NotNullByDefault
|
||||||
package dev.overlord.zomboidforge.core;
|
package dev.overlord.zomboidforge.core;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
group=dev.overlord.zomboidforge
|
group=dev.overlord.zomboidforge
|
||||||
version=0.1.0-SNAPSHOT
|
|
||||||
|
|
||||||
# Project Zomboid version the build targets. Selects compat/b<major>/, and
|
# Project Zomboid version the build targets. Selects compat/b<major>/, and
|
||||||
# :patcher:patchServerJar fails when the server jar reports another version.
|
# :patcher:patchServerJar fails when the server jar reports another version.
|
||||||
|
|||||||
@@ -39,12 +39,18 @@ tasks.register<JavaExec>("patchServerJar") {
|
|||||||
val vanillaJar = projectZomboid.serverJar
|
val vanillaJar = projectZomboid.serverJar
|
||||||
val patchedJar = layout.buildDirectory.file("server/projectzomboid.jar")
|
val patchedJar = layout.buildDirectory.file("server/projectzomboid.jar")
|
||||||
val expectedVersion = projectZomboid.version
|
val expectedVersion = projectZomboid.version
|
||||||
|
// Simple class names of patches to skip, e.g. -Pzf.disabledPatches=ServerStartedPatch (debug builds).
|
||||||
|
val disabledPatches = providers.gradleProperty("zf.disabledPatches").orElse("")
|
||||||
|
inputs.property("disabledPatches", disabledPatches)
|
||||||
inputs.file(vanillaJar).withPropertyName("vanillaJar")
|
inputs.file(vanillaJar).withPropertyName("vanillaJar")
|
||||||
inputs.property("expectedVersion", expectedVersion)
|
inputs.property("expectedVersion", expectedVersion)
|
||||||
outputs.file(patchedJar).withPropertyName("patchedJar")
|
outputs.file(patchedJar).withPropertyName("patchedJar")
|
||||||
|
|
||||||
classpath = sourceSets.main.get().runtimeClasspath + serverPatches
|
classpath = sourceSets.main.get().runtimeClasspath + serverPatches
|
||||||
mainClass = "dev.overlord.zomboidforge.patcher.JarPatcher"
|
mainClass = "dev.overlord.zomboidforge.patcher.JarPatcher"
|
||||||
|
jvmArgumentProviders.add(CommandLineArgumentProvider {
|
||||||
|
listOf("-Dzomboidforge.disabledPatches=${disabledPatches.get()}")
|
||||||
|
})
|
||||||
argumentProviders.add(CommandLineArgumentProvider {
|
argumentProviders.add(CommandLineArgumentProvider {
|
||||||
listOf(
|
listOf(
|
||||||
vanillaJar.get().asFile.absolutePath,
|
vanillaJar.get().asFile.absolutePath,
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package dev.overlord.zomboidforge.patcher;
|
||||||
|
|
||||||
|
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The landmark in the target class that a {@link ClassPatch} depends on. A patch can only
|
||||||
|
* apply when its anchor matches exactly once; otherwise the build fails, e.g. after a
|
||||||
|
* Project Zomboid update changed the surrounding code.
|
||||||
|
*/
|
||||||
|
public sealed interface Anchor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Fails with an {@link IllegalStateException} unless this anchor matches exactly once in
|
||||||
|
* {@code node}.
|
||||||
|
*/
|
||||||
|
void verify(ClassNode node);
|
||||||
|
|
||||||
|
/** A method of the target class, identified by name and descriptor. */
|
||||||
|
record Method(String name, String desc) implements Anchor
|
||||||
|
{
|
||||||
|
public MethodNode resolve(ClassNode node)
|
||||||
|
{
|
||||||
|
List<MethodNode> matches = node.methods.stream()
|
||||||
|
.filter(method -> method.name.equals(this.name) && method.desc.equals(this.desc))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
if (matches.size() != 1) throw new IllegalStateException("Expected exactly one method '" + node.name + "." + this.name + this.desc + "', found " + matches.size());
|
||||||
|
|
||||||
|
return matches.getFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void verify(ClassNode node) { this.resolve(node); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A static or instance call to {@code owner.name desc} inside the method {@code in}. */
|
||||||
|
record Call(Method in, String owner, String name, String desc) implements Anchor
|
||||||
|
{
|
||||||
|
public MethodInsnNode resolve(ClassNode node)
|
||||||
|
{
|
||||||
|
List<MethodInsnNode> matches = new ArrayList<>();
|
||||||
|
|
||||||
|
for (AbstractInsnNode instruction : this.in.resolve(node).instructions) {
|
||||||
|
if (this.isCall(instruction)) matches.add((MethodInsnNode) instruction);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matches.size() != 1) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Expected exactly one call to '" + this.owner + "." + this.name + this.desc
|
||||||
|
+ "' in '" + node.name + "." + this.in.name() + this.in.desc() + "', found " + matches.size()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return matches.getFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void verify(ClassNode node) { this.resolve(node); }
|
||||||
|
|
||||||
|
private boolean isCall(AbstractInsnNode instruction)
|
||||||
|
{
|
||||||
|
return instruction instanceof MethodInsnNode call
|
||||||
|
&& call.owner.equals(this.owner)
|
||||||
|
&& call.name.equals(this.name)
|
||||||
|
&& call.desc.equals(this.desc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,25 @@
|
|||||||
package dev.overlord.zomboidforge.patcher;
|
package dev.overlord.zomboidforge.patcher;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.annotations.ApiStatus.Internal;
|
||||||
|
|
||||||
import org.objectweb.asm.tree.ClassNode;
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
|
||||||
/** A build-time change to one class of the vanilla server jar. */
|
/** A build-time change to one class of the vanilla server jar. */
|
||||||
|
@Internal
|
||||||
public interface ClassPatch {
|
public interface ClassPatch {
|
||||||
/** Internal name of the class to patch, e.g. {@code zombie/network/GameServer}. */
|
/** Internal name of the class to patch, e.g. {@code zombie/network/GameServer}. */
|
||||||
String target();
|
String target();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The landmark in {@link #target()} that the patch depends on. {@link JarPatcher} verifies
|
||||||
|
* it before calling {@link #apply}, so the build fails when a game update removed it.
|
||||||
|
*/
|
||||||
|
Anchor anchor();
|
||||||
|
|
||||||
|
/** The runtime method the patched code calls, or {@code null} when the patch calls none. */
|
||||||
|
@Nullable Hook hook();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the patch applies to the given game version. Patches that differ
|
* Whether the patch applies to the given game version. Patches that differ
|
||||||
* between minor versions of the same Build provide one implementation per
|
* between minor versions of the same Build provide one implementation per
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package dev.overlord.zomboidforge.patcher;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@code public static} method of the runtime module that a patched game class calls.
|
||||||
|
* {@code owner} is an internal name, e.g. {@code dev/overlord/zomboidforge/compat/b42/hooks/ServerHooks}.
|
||||||
|
*/
|
||||||
|
public record Hook(String owner, String name, String desc) {}
|
||||||
@@ -14,6 +14,8 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.TreeSet;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
@@ -26,6 +28,9 @@ public final class JarPatcher
|
|||||||
{
|
{
|
||||||
private static final String VERSIONED_PREFIX = "META-INF/versions/";
|
private static final String VERSIONED_PREFIX = "META-INF/versions/";
|
||||||
|
|
||||||
|
/** System property: comma-separated simple class names of patches to skip, e.g. {@code ServerStartedPatch}. */
|
||||||
|
static final String DISABLED_PATCHES_PROPERTY = "zomboidforge.disabledPatches";
|
||||||
|
|
||||||
private final Map<String, List<ClassPatch>> patchesByEntry = new LinkedHashMap<>();
|
private final Map<String, List<ClassPatch>> patchesByEntry = new LinkedHashMap<>();
|
||||||
|
|
||||||
public JarPatcher(Collection<? extends ClassPatch> patches) {
|
public JarPatcher(Collection<? extends ClassPatch> patches) {
|
||||||
@@ -39,7 +44,8 @@ public final class JarPatcher
|
|||||||
/**
|
/**
|
||||||
* Usage: {@code JarPatcher <vanilla.jar> <patched.jar> <expected-version>}. Loads the
|
* Usage: {@code JarPatcher <vanilla.jar> <patched.jar> <expected-version>}. Loads the
|
||||||
* single {@link PatchSet} on the class path and applies every patch that applies to
|
* single {@link PatchSet} on the class path and applies every patch that applies to
|
||||||
* the jar's game version.
|
* the jar's game version, except those named in the system property
|
||||||
|
* {@value #DISABLED_PATCHES_PROPERTY} (for debug builds).
|
||||||
*/
|
*/
|
||||||
static void main(String[] args) throws IOException
|
static void main(String[] args) throws IOException
|
||||||
{
|
{
|
||||||
@@ -52,7 +58,7 @@ public final class JarPatcher
|
|||||||
Path patchedJar = Path.of(args[1]);
|
Path patchedJar = Path.of(args[1]);
|
||||||
String expectedVersion = args[2];
|
String expectedVersion = args[2];
|
||||||
|
|
||||||
GameVersion version = readGameVersion(vanillaJar);
|
GameVersion version = JarPatcher.readGameVersion(vanillaJar);
|
||||||
if (!version.matches(expectedVersion)) {
|
if (!version.matches(expectedVersion)) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"Server jar is Project Zomboid " + version +
|
"Server jar is Project Zomboid " + version +
|
||||||
@@ -61,14 +67,16 @@ public final class JarPatcher
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
PatchSet patchSet = loadPatchSet();
|
Set<String> disabled = JarPatcher.parseNames(System.getProperty(JarPatcher.DISABLED_PATCHES_PROPERTY, ""));
|
||||||
List<ClassPatch> applicable = applicablePatches(patchSet, version);
|
PatchSet patchSet = JarPatcher.loadPatchSet();
|
||||||
|
List<ClassPatch> applicable = JarPatcher.applicablePatches(patchSet, version, disabled);
|
||||||
int patchedClasses = new JarPatcher(applicable).patch(vanillaJar, patchedJar);
|
int patchedClasses = new JarPatcher(applicable).patch(vanillaJar, patchedJar);
|
||||||
|
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"Project Zomboid " + version + ": patched " + patchedClasses + " class(es) with "
|
"Project Zomboid " + version + ": patched " + patchedClasses + " class(es) with "
|
||||||
+ applicable.size() + " of " + patchSet.patches().size() + " patch(es) -> " + patchedJar
|
+ applicable.size() + " of " + patchSet.patches().size() + " patch(es) -> " + patchedJar
|
||||||
);
|
);
|
||||||
|
if (!disabled.isEmpty()) System.out.println("Disabled patches: " + JarPatcher.quoted(disabled));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Reads the game version from zombie/core/Core.class in the jar. */
|
/** Reads the game version from zombie/core/Core.class in the jar. */
|
||||||
@@ -85,6 +93,16 @@ public final class JarPatcher
|
|||||||
|
|
||||||
/** Patches of {@code patchSet} that apply to {@code version}, in application order. */
|
/** Patches of {@code patchSet} that apply to {@code version}, in application order. */
|
||||||
public static List<ClassPatch> applicablePatches(PatchSet patchSet, GameVersion version)
|
public static List<ClassPatch> applicablePatches(PatchSet patchSet, GameVersion version)
|
||||||
|
{
|
||||||
|
return JarPatcher.applicablePatches(patchSet, version, Set.of());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Patches of {@code patchSet} that apply to {@code version} and are not named in
|
||||||
|
* {@code disabled} (simple class names), in application order. Fails when a name in
|
||||||
|
* {@code disabled} matches no patch of the set, so a typo does not silently keep a patch.
|
||||||
|
*/
|
||||||
|
public static List<ClassPatch> applicablePatches(PatchSet patchSet, GameVersion version, Set<String> disabled)
|
||||||
{
|
{
|
||||||
if (patchSet.build() != version.major()) {
|
if (patchSet.build() != version.major()) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
@@ -93,11 +111,35 @@ public final class JarPatcher
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Set<String> unknown = new TreeSet<>(disabled);
|
||||||
|
patchSet.patches().forEach(patch -> unknown.remove(patch.getClass().getSimpleName()));
|
||||||
|
if (!unknown.isEmpty()) {
|
||||||
|
throw new IllegalStateException("Cannot disable unknown patch(es) " + JarPatcher.quoted(unknown));
|
||||||
|
}
|
||||||
|
|
||||||
return patchSet.patches().stream()
|
return patchSet.patches().stream()
|
||||||
|
.filter(patch -> !disabled.contains(patch.getClass().getSimpleName()))
|
||||||
.filter(patch -> patch.appliesTo(version))
|
.filter(patch -> patch.appliesTo(version))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Splits a comma-separated list of names; blank entries are ignored. */
|
||||||
|
static Set<String> parseNames(String list)
|
||||||
|
{
|
||||||
|
Set<String> names = new TreeSet<>();
|
||||||
|
|
||||||
|
for (String name : list.split(","))
|
||||||
|
{
|
||||||
|
if (!name.isBlank()) names.add(name.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String quoted(Set<String> names) {
|
||||||
|
return String.join(", ", names.stream().map(name -> "'" + name + "'").toList());
|
||||||
|
}
|
||||||
|
|
||||||
private static PatchSet loadPatchSet()
|
private static PatchSet loadPatchSet()
|
||||||
{
|
{
|
||||||
List<PatchSet> patchSets = ServiceLoader.load(PatchSet.class)
|
List<PatchSet> patchSets = ServiceLoader.load(PatchSet.class)
|
||||||
@@ -120,7 +162,7 @@ public final class JarPatcher
|
|||||||
ZipFile jar = new ZipFile(in.toFile());
|
ZipFile jar = new ZipFile(in.toFile());
|
||||||
ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(out))
|
ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(out))
|
||||||
) {
|
) {
|
||||||
checkTargets(jar);
|
this.checkTargets(jar);
|
||||||
int patched = 0;
|
int patched = 0;
|
||||||
|
|
||||||
for (Enumeration<? extends ZipEntry> entries = jar.entries(); entries.hasMoreElements();)
|
for (Enumeration<? extends ZipEntry> entries = jar.entries(); entries.hasMoreElements();)
|
||||||
@@ -163,9 +205,7 @@ public final class JarPatcher
|
|||||||
.filter(n -> patchesByEntry.containsKey(n.substring(n.indexOf('/', VERSIONED_PREFIX.length()) + 1)))
|
.filter(n -> patchesByEntry.containsKey(n.substring(n.indexOf('/', VERSIONED_PREFIX.length()) + 1)))
|
||||||
.forEach(n -> problems.add("'" + n + "' overrides a patched class."));
|
.forEach(n -> problems.add("'" + n + "' overrides a patched class."));
|
||||||
|
|
||||||
if (!problems.isEmpty()) {
|
if (!problems.isEmpty()) throw new IllegalStateException("Cannot patch '" + jar.getName() + "':\n " + String.join("\n ", problems));
|
||||||
throw new IllegalStateException("Cannot patch '" + jar.getName() + "':\n " + String.join("\n ", problems));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] transform(ZipFile jar, byte[] bytes, List<ClassPatch> patches)
|
private static byte[] transform(ZipFile jar, byte[] bytes, List<ClassPatch> patches)
|
||||||
@@ -174,6 +214,7 @@ public final class JarPatcher
|
|||||||
new ClassReader(bytes).accept(node, 0);
|
new ClassReader(bytes).accept(node, 0);
|
||||||
|
|
||||||
for (ClassPatch patch : patches) {
|
for (ClassPatch patch : patches) {
|
||||||
|
JarPatcher.verifyAnchor(patch, node);
|
||||||
patch.apply(node);
|
patch.apply(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,6 +224,12 @@ public final class JarPatcher
|
|||||||
return writer.toByteArray();
|
return writer.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void verifyAnchor(ClassPatch patch, ClassNode node)
|
||||||
|
{
|
||||||
|
try { patch.anchor().verify(node); }
|
||||||
|
catch (IllegalStateException e) { throw new IllegalStateException("'" + patch.getClass().getSimpleName() + "': " + e.getMessage(), e); }
|
||||||
|
}
|
||||||
|
|
||||||
private static byte[] read(ZipFile jar, String entryName)
|
private static byte[] read(ZipFile jar, String entryName)
|
||||||
{
|
{
|
||||||
ZipEntry entry = jar.getEntry(entryName);
|
ZipEntry entry = jar.getEntry(entryName);
|
||||||
|
|||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
package dev.overlord.zomboidforge.patcher.Patches;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||||
|
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||||
|
import dev.overlord.zomboidforge.patcher.Hook;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import org.objectweb.asm.Opcodes;
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a call to a {@link Hook} directly after a call in the target class. The hook takes
|
||||||
|
* no arguments and returns nothing, so the operand stack and the stack map frames of the
|
||||||
|
* patched method stay unchanged.
|
||||||
|
*/
|
||||||
|
public abstract class HookInjectionPatch implements ClassPatch
|
||||||
|
{
|
||||||
|
private static final String HOOK_DESC = "()V";
|
||||||
|
|
||||||
|
private final String target;
|
||||||
|
private final Anchor.Call anchor;
|
||||||
|
private final Hook hook;
|
||||||
|
|
||||||
|
protected HookInjectionPatch(String target, Anchor.Call anchor, @NotNull Hook hook)
|
||||||
|
{
|
||||||
|
if (!hook.desc().equals(HookInjectionPatch.HOOK_DESC))
|
||||||
|
throw new IllegalArgumentException("Hook '" + hook + "' must have descriptor '" + HookInjectionPatch.HOOK_DESC + "'.");
|
||||||
|
|
||||||
|
this.target = Objects.requireNonNull(target);
|
||||||
|
this.anchor = Objects.requireNonNull(anchor);
|
||||||
|
this.hook = hook;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final String target() { return this.target; }
|
||||||
|
@Override
|
||||||
|
public final Anchor.Call anchor() { return this.anchor; }
|
||||||
|
@Override
|
||||||
|
public final @NotNull Hook hook() { return this.hook; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void apply(ClassNode node)
|
||||||
|
{
|
||||||
|
MethodInsnNode call = this.anchor.resolve(node);
|
||||||
|
|
||||||
|
this.anchor.in().resolve(node).instructions.insert(
|
||||||
|
call, new MethodInsnNode(
|
||||||
|
Opcodes.INVOKESTATIC,
|
||||||
|
this.hook.owner(),
|
||||||
|
this.hook.name(),
|
||||||
|
this.hook.desc(),
|
||||||
|
false
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package dev.overlord.zomboidforge.patcher.Patches;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||||
|
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||||
|
import dev.overlord.zomboidforge.patcher.GameVersion;
|
||||||
|
import dev.overlord.zomboidforge.patcher.Hook;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for patches that make a vanilla method cheaper without changing what it returns
|
||||||
|
* or does. Unlike {@link HookInjectionPatch}, which only inserts a call, a performance patch
|
||||||
|
* may rewrite instructions of the anchor method or add fields to the target class, so
|
||||||
|
* {@link #apply} is left to the subclass.
|
||||||
|
*
|
||||||
|
* <p>{@link #appliesTo} has no default: every performance patch names the game versions it was
|
||||||
|
* verified on.
|
||||||
|
*/
|
||||||
|
public abstract class PerformancePatch implements ClassPatch
|
||||||
|
{
|
||||||
|
private final String target;
|
||||||
|
private final Anchor anchor;
|
||||||
|
private final Hook hook;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param hook the runtime method the patched code calls, or {@code null} when the patch
|
||||||
|
* only changes the target class itself
|
||||||
|
*/
|
||||||
|
protected PerformancePatch(String target, @NotNull Anchor anchor, @Nullable Hook hook)
|
||||||
|
{
|
||||||
|
this.target = Objects.requireNonNull(target);
|
||||||
|
this.anchor = Objects.requireNonNull(anchor);
|
||||||
|
this.hook = hook;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final String target() { return this.target; }
|
||||||
|
@Override
|
||||||
|
public final Anchor anchor() { return this.anchor; }
|
||||||
|
@Override
|
||||||
|
public final @Nullable Hook hook() { return this.hook; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract boolean appliesTo(GameVersion version);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract void apply(ClassNode node);
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.patcher.Patches;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
/** @noinspection UnstableApiUsage, RedundantSuppression */
|
||||||
|
@NotNullByDefault
|
||||||
|
package dev.overlord.zomboidforge.patcher;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNullByDefault;
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package dev.overlord.zomboidforge.patcher;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
import org.objectweb.asm.tree.MethodInsnNode;
|
||||||
|
import org.objectweb.asm.tree.MethodNode;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.RETURN;
|
||||||
|
|
||||||
|
class AnchorTest
|
||||||
|
{
|
||||||
|
private static final Anchor.Method RUN = new Anchor.Method("run", "()V");
|
||||||
|
private static final Anchor.Call CALL = new Anchor.Call(RUN, "t/Dep", "go", "()V");
|
||||||
|
|
||||||
|
private static ClassNode classWithRun(int callsToGo)
|
||||||
|
{
|
||||||
|
ClassNode node = new ClassNode();
|
||||||
|
node.name = "t/Target";
|
||||||
|
|
||||||
|
MethodNode run = new MethodNode(ACC_STATIC, "run", "()V", null, null);
|
||||||
|
for (int i = 0; i < callsToGo; i++) {
|
||||||
|
run.instructions.add(new MethodInsnNode(INVOKESTATIC, "t/Dep", "go", "()V", false));
|
||||||
|
}
|
||||||
|
run.visitInsn(RETURN);
|
||||||
|
|
||||||
|
node.methods.add(run);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void methodAnchorMatchesExactlyOnce()
|
||||||
|
{
|
||||||
|
assertDoesNotThrow(() -> AnchorTest.RUN.verify(AnchorTest.classWithRun(0)));
|
||||||
|
|
||||||
|
ClassNode duplicate = AnchorTest.classWithRun(0);
|
||||||
|
duplicate.methods.add(new MethodNode(ACC_STATIC, "run", "()V", null, null));
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"Expected exactly one method 't/Target.run()V', found 2",
|
||||||
|
assertThrows(IllegalStateException.class, () -> AnchorTest.RUN.verify(duplicate)).getMessage()
|
||||||
|
);
|
||||||
|
assertThrows(IllegalStateException.class, () -> new Anchor.Method("run", "(I)V").verify(AnchorTest.classWithRun(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void callAnchorMatchesExactlyOnce()
|
||||||
|
{
|
||||||
|
assertDoesNotThrow(() -> AnchorTest.CALL.verify(AnchorTest.classWithRun(1)));
|
||||||
|
|
||||||
|
assertTrue(
|
||||||
|
assertThrows(IllegalStateException.class, () -> AnchorTest.CALL.verify(AnchorTest.classWithRun(0)))
|
||||||
|
.getMessage().contains("call to 't/Dep.go()V' in 't/Target.run()V', found 0")
|
||||||
|
);
|
||||||
|
assertTrue(
|
||||||
|
assertThrows(IllegalStateException.class, () -> AnchorTest.CALL.verify(AnchorTest.classWithRun(2)))
|
||||||
|
.getMessage().contains("found 2")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void callAnchorIgnoresOtherMethods()
|
||||||
|
{
|
||||||
|
ClassNode node = AnchorTest.classWithRun(1);
|
||||||
|
|
||||||
|
MethodNode other = new MethodNode(ACC_STATIC, "other", "()V", null, null);
|
||||||
|
other.instructions.add(new MethodInsnNode(INVOKESTATIC, "t/Dep", "go", "()V", false));
|
||||||
|
other.visitInsn(RETURN);
|
||||||
|
node.methods.add(other);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> AnchorTest.CALL.verify(node));
|
||||||
|
}
|
||||||
|
}
|
||||||
+95
-26
@@ -4,58 +4,127 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.objectweb.asm.tree.ClassNode;
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
class ApplicablePatchesTest {
|
class ApplicablePatchesTest
|
||||||
private static ClassPatch patch(String name, int sinceMinor) {
|
{
|
||||||
return new ClassPatch() {
|
private static final class FirstPatch implements ClassPatch
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
public String target() {
|
public String target() { return "t/First"; }
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean appliesTo(GameVersion version) {
|
public Anchor anchor() { return new Anchor.Method("m", "()V"); }
|
||||||
return version.isAtLeast(42, sinceMinor);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(ClassNode node) {
|
public Hook hook() { return null; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ClassNode node) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final class SecondPatch implements ClassPatch
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public String target() { return "t/Second"; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Anchor anchor() { return new Anchor.Method("m", "()V"); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Hook hook() { return null; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ClassNode node) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ClassPatch patch(String name, int sinceMinor)
|
||||||
|
{
|
||||||
|
return new ClassPatch()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public String target() { return name; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Anchor anchor() { return new Anchor.Method("m", "()V"); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Hook hook() { return null; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean appliesTo(GameVersion version) { return version.isAtLeast(42, sinceMinor); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ClassNode node) {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PatchSet build42(List<ClassPatch> patches) {
|
private static PatchSet build42(List<ClassPatch> patches)
|
||||||
|
{
|
||||||
return new PatchSet() {
|
return new PatchSet() {
|
||||||
@Override
|
@Override
|
||||||
public int build() {
|
public int build() { return 42; }
|
||||||
return 42;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ClassPatch> patches() {
|
public List<ClassPatch> patches() { return patches; }
|
||||||
return patches;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void keepsOrderAndSkipsPatchesForOtherVersions() {
|
void keepsOrderAndSkipsPatchesForOtherVersions()
|
||||||
ClassPatch old = patch("t/Old", 0);
|
{
|
||||||
ClassPatch fromTwenty = patch("t/New", 20);
|
ClassPatch old = ApplicablePatchesTest.patch("t/Old", 0);
|
||||||
ClassPatch fromTwentyTwo = patch("t/Newer", 22);
|
ClassPatch fromTwenty = ApplicablePatchesTest.patch("t/New", 20);
|
||||||
PatchSet patchSet = build42(List.of(old, fromTwenty, fromTwentyTwo));
|
ClassPatch fromTwentyTwo = ApplicablePatchesTest.patch("t/Newer", 22);
|
||||||
|
PatchSet patchSet = ApplicablePatchesTest.build42(List.of(old, fromTwenty, fromTwentyTwo));
|
||||||
|
|
||||||
assertEquals(List.of(old, fromTwenty), JarPatcher.applicablePatches(patchSet, new GameVersion(42, 21, 0)));
|
assertEquals(List.of(old, fromTwenty), JarPatcher.applicablePatches(patchSet, new GameVersion(42, 21, 0)));
|
||||||
assertEquals(List.of(old), JarPatcher.applicablePatches(patchSet, new GameVersion(42, 19, 2)));
|
assertEquals(List.of(old), JarPatcher.applicablePatches(patchSet, new GameVersion(42, 19, 2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void rejectsPatchSetOfAnotherBuild() {
|
void rejectsPatchSetOfAnotherBuild()
|
||||||
PatchSet patchSet = build42(List.of());
|
{
|
||||||
assertThrows(IllegalStateException.class,
|
PatchSet patchSet = ApplicablePatchesTest.build42(List.of());
|
||||||
() -> JarPatcher.applicablePatches(patchSet, new GameVersion(43, 0, 0)));
|
|
||||||
|
assertThrows(
|
||||||
|
IllegalStateException.class,
|
||||||
|
() -> JarPatcher.applicablePatches(patchSet, new GameVersion(43, 0, 0))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void skipsDisabledPatchesByClassName()
|
||||||
|
{
|
||||||
|
ClassPatch first = new FirstPatch();
|
||||||
|
ClassPatch second = new SecondPatch();
|
||||||
|
PatchSet patchSet = ApplicablePatchesTest.build42(List.of(first, second));
|
||||||
|
|
||||||
|
assertEquals(List.of(second), JarPatcher.applicablePatches(patchSet, new GameVersion(42, 21, 0), Set.of("FirstPatch")));
|
||||||
|
assertEquals(List.of(), JarPatcher.applicablePatches(patchSet, new GameVersion(42, 21, 0), Set.of("FirstPatch", "SecondPatch")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsDisabledNameThatMatchesNoPatch()
|
||||||
|
{
|
||||||
|
PatchSet patchSet = ApplicablePatchesTest.build42(List.of(new FirstPatch()));
|
||||||
|
|
||||||
|
IllegalStateException e = assertThrows(
|
||||||
|
IllegalStateException.class,
|
||||||
|
() -> JarPatcher.applicablePatches(patchSet, new GameVersion(42, 21, 0), Set.of("FirstPatch", "TypoPatch"))
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals("Cannot disable unknown patch(es) 'TypoPatch'", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void parsesCommaSeparatedNames()
|
||||||
|
{
|
||||||
|
assertEquals(Set.of("A", "B"), JarPatcher.parseNames(" A, B ,,"));
|
||||||
|
assertTrue(JarPatcher.parseNames("").isEmpty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,174 @@
|
|||||||
|
package dev.overlord.zomboidforge.patcher;
|
||||||
|
|
||||||
|
import dev.overlord.zomboidforge.patcher.Patches.HookInjectionPatch;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.DUP;
|
||||||
|
import static org.objectweb.asm.Opcodes.GETSTATIC;
|
||||||
|
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.NEW;
|
||||||
|
import static org.objectweb.asm.Opcodes.POP;
|
||||||
|
import static org.objectweb.asm.Opcodes.PUTSTATIC;
|
||||||
|
import static org.objectweb.asm.Opcodes.RETURN;
|
||||||
|
import static org.objectweb.asm.Opcodes.V25;
|
||||||
|
|
||||||
|
class HookInjectionPatchTest
|
||||||
|
{
|
||||||
|
private static final Anchor.Call ANCHOR = new Anchor.Call(
|
||||||
|
new Anchor.Method("run", "()V"), "t/Dep", "anchor", "()V"
|
||||||
|
);
|
||||||
|
private static final Hook HOOK = new Hook("t/Hooks", "hook", "()V");
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path dir;
|
||||||
|
|
||||||
|
/** Target class with {@code run()}: {@code Dep.anchor(); Dep.after();}. */
|
||||||
|
private static byte[] target()
|
||||||
|
{
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||||
|
cw.visit(V25, ACC_PUBLIC, "t/Target", null, "java/lang/Object", null);
|
||||||
|
|
||||||
|
MethodVisitor run = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "run", "()V", null, null);
|
||||||
|
run.visitMethodInsn(INVOKESTATIC, "t/Dep", "anchor", "()V", false);
|
||||||
|
run.visitMethodInsn(INVOKESTATIC, "t/Dep", "after", "()V", false);
|
||||||
|
run.visitInsn(RETURN);
|
||||||
|
run.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
cw.visitEnd();
|
||||||
|
return cw.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Class with static methods {@code names}, each appending its own name to {@code log}. */
|
||||||
|
private static byte[] recorder(String className, List<String> names)
|
||||||
|
{
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||||
|
cw.visit(V25, ACC_PUBLIC, className, null, "java/lang/Object", null);
|
||||||
|
|
||||||
|
for (String name : names)
|
||||||
|
{
|
||||||
|
MethodVisitor m = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, name, "()V", null, null);
|
||||||
|
m.visitFieldInsn(GETSTATIC, "t/Log", "log", "Ljava/lang/StringBuilder;");
|
||||||
|
m.visitLdcInsn(name + ";");
|
||||||
|
m.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
|
||||||
|
m.visitInsn(POP);
|
||||||
|
m.visitInsn(RETURN);
|
||||||
|
m.visitMaxs(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
cw.visitEnd();
|
||||||
|
return cw.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] log()
|
||||||
|
{
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
|
||||||
|
cw.visit(V25, ACC_PUBLIC, "t/Log", null, "java/lang/Object", null);
|
||||||
|
cw.visitField(ACC_PUBLIC | ACC_STATIC, "log", "Ljava/lang/StringBuilder;", null, null).visitEnd();
|
||||||
|
|
||||||
|
MethodVisitor init = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
|
||||||
|
init.visitTypeInsn(NEW, "java/lang/StringBuilder");
|
||||||
|
init.visitInsn(DUP);
|
||||||
|
init.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
|
||||||
|
init.visitFieldInsn(PUTSTATIC, "t/Log", "log", "Ljava/lang/StringBuilder;");
|
||||||
|
init.visitInsn(RETURN);
|
||||||
|
init.visitMaxs(0, 0);
|
||||||
|
|
||||||
|
cw.visitEnd();
|
||||||
|
return cw.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path jar(Map<String, byte[]> entries) throws Exception
|
||||||
|
{
|
||||||
|
Path jar = Files.createTempFile(this.dir, "in", ".jar");
|
||||||
|
|
||||||
|
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(jar)))
|
||||||
|
{
|
||||||
|
for (Map.Entry<String, byte[]> e : entries.entrySet()) {
|
||||||
|
zip.putNextEntry(new ZipEntry(e.getKey()));
|
||||||
|
zip.write(e.getValue());
|
||||||
|
zip.closeEntry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return jar;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class TestPatch extends HookInjectionPatch
|
||||||
|
{
|
||||||
|
TestPatch() {
|
||||||
|
super("t/Target", HookInjectionPatchTest.ANCHOR, HookInjectionPatchTest.HOOK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void callsHookDirectlyAfterAnchor() throws Exception
|
||||||
|
{
|
||||||
|
Path in = this.jar(Map.of(
|
||||||
|
"t/Target.class", HookInjectionPatchTest.target(),
|
||||||
|
"t/Dep.class", recorder("t/Dep", List.of("anchor", "after")),
|
||||||
|
"t/Hooks.class", recorder("t/Hooks", List.of("hook")),
|
||||||
|
"t/Log.class", log()
|
||||||
|
));
|
||||||
|
Path out = this.dir.resolve("patched.jar");
|
||||||
|
|
||||||
|
assertEquals(1, new JarPatcher(List.of(new TestPatch())).patch(in, out));
|
||||||
|
|
||||||
|
try (URLClassLoader loader = new URLClassLoader(new URL[]{out.toUri().toURL()}, null))
|
||||||
|
{
|
||||||
|
loader.loadClass("t.Target").getMethod("run").invoke(null);
|
||||||
|
|
||||||
|
Object log = loader.loadClass("t.Log").getField("log").get(null);
|
||||||
|
assertEquals("anchor;hook;after;", log.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void reportsPatchAndAnchorWhenAnchorIsMissing() throws Exception
|
||||||
|
{
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||||
|
cw.visit(V25, ACC_PUBLIC, "t/Target", null, "java/lang/Object", null);
|
||||||
|
MethodVisitor run = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "run", "()V", null, null);
|
||||||
|
run.visitInsn(RETURN);
|
||||||
|
run.visitMaxs(0, 0);
|
||||||
|
cw.visitEnd();
|
||||||
|
|
||||||
|
Path in = this.jar(Map.of("t/Target.class", cw.toByteArray()));
|
||||||
|
|
||||||
|
IllegalStateException e = assertThrows(
|
||||||
|
IllegalStateException.class,
|
||||||
|
() -> new JarPatcher(List.of(new TestPatch())).patch(in, this.dir.resolve("out.jar"))
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"'TestPatch': Expected exactly one call to 't/Dep.anchor()V' in 't/Target.run()V', found 0",
|
||||||
|
e.getMessage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsHookWithArguments()
|
||||||
|
{
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> new HookInjectionPatch(
|
||||||
|
"t/Target", HookInjectionPatchTest.ANCHOR, new Hook("t/Hooks", "hook", "(I)V")
|
||||||
|
) {});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,9 +45,13 @@ class JarPatcherTest
|
|||||||
private static final ClassPatch ADD_PICK = new ClassPatch()
|
private static final ClassPatch ADD_PICK = new ClassPatch()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public String target() {
|
public String target() { return "t/Target"; }
|
||||||
return "t/Target";
|
|
||||||
}
|
@Override
|
||||||
|
public Anchor anchor() { return new Anchor.Method("<init>", "()V"); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Hook hook() { return null; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(ClassNode node)
|
public void apply(ClassNode node)
|
||||||
@@ -59,12 +63,12 @@ class JarPatcherTest
|
|||||||
m.visitVarInsn(ILOAD, 0);
|
m.visitVarInsn(ILOAD, 0);
|
||||||
m.visitJumpInsn(IFEQ, elseBranch);
|
m.visitJumpInsn(IFEQ, elseBranch);
|
||||||
|
|
||||||
newInstance(m, "t/A");
|
JarPatcherTest.newInstance(m, "t/A");
|
||||||
|
|
||||||
m.visitJumpInsn(GOTO, end);
|
m.visitJumpInsn(GOTO, end);
|
||||||
m.visitLabel(elseBranch);
|
m.visitLabel(elseBranch);
|
||||||
|
|
||||||
newInstance(m, "t/B");
|
JarPatcherTest.newInstance(m, "t/B");
|
||||||
|
|
||||||
m.visitLabel(end);
|
m.visitLabel(end);
|
||||||
m.visitInsn(ARETURN);
|
m.visitInsn(ARETURN);
|
||||||
@@ -78,7 +82,7 @@ class JarPatcherTest
|
|||||||
@Test
|
@Test
|
||||||
void patchesTargetAndCopiesEverythingElse() throws Exception
|
void patchesTargetAndCopiesEverythingElse() throws Exception
|
||||||
{
|
{
|
||||||
Path in = jar(Map.of(
|
Path in = this.jar(Map.of(
|
||||||
"t/Base.class", classBytes("t/Base", "java/lang/Object"),
|
"t/Base.class", classBytes("t/Base", "java/lang/Object"),
|
||||||
"t/A.class", classBytes("t/A", "t/Base"),
|
"t/A.class", classBytes("t/A", "t/Base"),
|
||||||
"t/B.class", classBytes("t/B", "t/Base"),
|
"t/B.class", classBytes("t/B", "t/Base"),
|
||||||
@@ -86,17 +90,17 @@ class JarPatcherTest
|
|||||||
"media/readme.txt", "untouched".getBytes()
|
"media/readme.txt", "untouched".getBytes()
|
||||||
));
|
));
|
||||||
|
|
||||||
Path out = dir.resolve("out/patched.jar");
|
Path out = this.dir.resolve("out/patched.jar");
|
||||||
|
|
||||||
assertEquals(1, new JarPatcher(List.of(ADD_PICK)).patch(in, out));
|
assertEquals(1, new JarPatcher(List.of(JarPatcherTest.ADD_PICK)).patch(in, out));
|
||||||
|
|
||||||
try (JarFile original = new JarFile(in.toFile()); JarFile patched = new JarFile(out.toFile()))
|
try (JarFile original = new JarFile(in.toFile()); JarFile patched = new JarFile(out.toFile()))
|
||||||
{
|
{
|
||||||
for (String name : List.of("t/Base.class", "t/A.class", "t/B.class", "media/readme.txt"))
|
for (String name : List.of("t/Base.class", "t/A.class", "t/B.class", "media/readme.txt"))
|
||||||
{
|
{
|
||||||
assertArrayEquals(
|
assertArrayEquals(
|
||||||
read(original, name),
|
JarPatcherTest.read(original, name),
|
||||||
read(patched, name),
|
JarPatcherTest.read(patched, name),
|
||||||
name
|
name
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -114,11 +118,11 @@ class JarPatcherTest
|
|||||||
@Test
|
@Test
|
||||||
void failsWhenTargetIsMissing() throws IOException
|
void failsWhenTargetIsMissing() throws IOException
|
||||||
{
|
{
|
||||||
Path in = jar(Map.of("t/Other.class", classBytes("t/Other", "java/lang/Object")));
|
Path in = this.jar(Map.of("t/Other.class", JarPatcherTest.classBytes("t/Other", "java/lang/Object")));
|
||||||
|
|
||||||
IllegalStateException e = assertThrows(
|
IllegalStateException e = assertThrows(
|
||||||
IllegalStateException.class,
|
IllegalStateException.class,
|
||||||
() -> new JarPatcher(List.of(ADD_PICK)).patch(in, dir.resolve("out.jar"))
|
() -> new JarPatcher(List.of(JarPatcherTest.ADD_PICK)).patch(in, this.dir.resolve("out.jar"))
|
||||||
);
|
);
|
||||||
|
|
||||||
assertTrue(e.getMessage().contains("'t/Target.class' not found"), e.getMessage());
|
assertTrue(e.getMessage().contains("'t/Target.class' not found"), e.getMessage());
|
||||||
@@ -127,17 +131,53 @@ class JarPatcherTest
|
|||||||
@Test
|
@Test
|
||||||
void failsWhenTargetHasMultiReleaseOverride() throws IOException
|
void failsWhenTargetHasMultiReleaseOverride() throws IOException
|
||||||
{
|
{
|
||||||
byte[] target = classBytes("t/Target", "java/lang/Object");
|
byte[] target = JarPatcherTest.classBytes("t/Target", "java/lang/Object");
|
||||||
Path in = jar(Map.of("t/Target.class", target, "META-INF/versions/21/t/Target.class", target));
|
Path in = this.jar(Map.of("t/Target.class", target, "META-INF/versions/21/t/Target.class", target));
|
||||||
|
|
||||||
IllegalStateException e = assertThrows(
|
IllegalStateException e = assertThrows(
|
||||||
IllegalStateException.class,
|
IllegalStateException.class,
|
||||||
() -> new JarPatcher(List.of(ADD_PICK)).patch(in, dir.resolve("out.jar"))
|
() -> new JarPatcher(List.of(JarPatcherTest.ADD_PICK)).patch(in, this.dir.resolve("out.jar"))
|
||||||
);
|
);
|
||||||
|
|
||||||
assertTrue(e.getMessage().contains("overrides a patched class"), e.getMessage());
|
assertTrue(e.getMessage().contains("overrides a patched class"), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void failsWhenAnchorIsMissing() throws IOException
|
||||||
|
{
|
||||||
|
ClassPatch missingAnchor = new ClassPatch()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public String target() {
|
||||||
|
return "t/Target";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Anchor anchor() {
|
||||||
|
return new Anchor.Method("gone", "()V");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Hook hook() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ClassNode node) {
|
||||||
|
throw new AssertionError("apply must not run when the anchor is missing");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Path in = this.jar(Map.of("t/Target.class", JarPatcherTest.classBytes("t/Target", "java/lang/Object")));
|
||||||
|
|
||||||
|
IllegalStateException e = assertThrows(
|
||||||
|
IllegalStateException.class,
|
||||||
|
() -> new JarPatcher(List.of(missingAnchor)).patch(in, this.dir.resolve("out.jar"))
|
||||||
|
);
|
||||||
|
|
||||||
|
assertTrue(e.getMessage().contains("Expected exactly one method 't/Target.gone()V', found 0"), e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
private static void newInstance(MethodVisitor m, String type) {
|
private static void newInstance(MethodVisitor m, String type) {
|
||||||
m.visitTypeInsn(NEW, type);
|
m.visitTypeInsn(NEW, type);
|
||||||
m.visitInsn(DUP);
|
m.visitInsn(DUP);
|
||||||
@@ -161,7 +201,7 @@ class JarPatcherTest
|
|||||||
|
|
||||||
private Path jar(Map<String, byte[]> entries) throws IOException
|
private Path jar(Map<String, byte[]> entries) throws IOException
|
||||||
{
|
{
|
||||||
Path jar = Files.createTempFile(dir, "in", ".jar");
|
Path jar = Files.createTempFile(this.dir, "in", ".jar");
|
||||||
|
|
||||||
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(jar)))
|
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(jar)))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user