Add BackupStoragePatch to wrap save folder backups, ensuring storage writes are paused during backups. Introduce ModStorage and ModTables for per-mod database management, supporting schema migrations and table versioning. Add ForgeInstance as a static reference to server-side Forge. Include relevant tests.
This commit is contained in:
1 parent
c56046469e
commit
68cd9fc4f9
31 files changed
+2124
-42
No files matched your search
+3
-8
@@ -1,13 +1,7 @@
|
||||
package dev.overlord.zomboidforge.compat.b42;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.ServerLoadPatch;
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.ServerStartedPatch;
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.ServerStoppingPatch;
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.ServerTickPatch;
|
||||
import dev.overlord.zomboidforge.compat.b42.perf.IsoGameCharacterStateMachinePerformancePatch;
|
||||
import dev.overlord.zomboidforge.compat.b42.perf.IsoZombieNetworkComponentPerformancePatch;
|
||||
import dev.overlord.zomboidforge.compat.b42.perf.IsoZombieSpotChancePerformancePatch;
|
||||
import dev.overlord.zomboidforge.compat.b42.perf.IsoZombieVehiclePerformancePatch;
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.*;
|
||||
import dev.overlord.zomboidforge.compat.b42.perf.*;
|
||||
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||
import dev.overlord.zomboidforge.patcher.PatchSet;
|
||||
|
||||
@@ -23,6 +17,7 @@ public final class B42PatchSet implements PatchSet
|
||||
new ServerStartedPatch(),
|
||||
new ServerTickPatch(),
|
||||
new ServerStoppingPatch(),
|
||||
new BackupStoragePatch(),
|
||||
new IsoZombieVehiclePerformancePatch(),
|
||||
new IsoZombieNetworkComponentPerformancePatch(),
|
||||
new IsoZombieSpotChancePerformancePatch(),
|
||||
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||
|
||||
import dev.overlord.zomboidforge.patcher.Anchor;
|
||||
import dev.overlord.zomboidforge.patcher.ClassPatch;
|
||||
import dev.overlord.zomboidforge.patcher.Hook;
|
||||
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.InsnList;
|
||||
import org.objectweb.asm.tree.InsnNode;
|
||||
import org.objectweb.asm.tree.LabelNode;
|
||||
import org.objectweb.asm.tree.MethodInsnNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
import org.objectweb.asm.tree.TryCatchBlockNode;
|
||||
import org.objectweb.asm.tree.VarInsnNode;
|
||||
|
||||
/**
|
||||
* Wraps {@code ZipBackup.makeBackupFile(String, BackupTypes)}, which zips the save folder
|
||||
* including {@code zomboidforge.db}, so that storage writes wait during a backup:
|
||||
*
|
||||
* <pre>{@code
|
||||
* private static void makeBackupFile(String serverName, BackupTypes type)
|
||||
* {
|
||||
* StorageHooks.backupRunning(true);
|
||||
* try { zomboidForge$makeBackupFile(serverName, type); }
|
||||
* finally { StorageHooks.backupRunning(false); }
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* The vanilla method is renamed to {@code zomboidForge$makeBackupFile} and stays unchanged. It
|
||||
* reads the zipped files until {@code ParallelScatterZipCreator.writeTo} returns and catches its
|
||||
* own exceptions, so only a wrapper around the whole method covers the entire backup.
|
||||
*/
|
||||
public final class BackupStoragePatch implements ClassPatch
|
||||
{
|
||||
private static final String TARGET = "zombie/core/backup/ZipBackup";
|
||||
private static final String RENAMED = "zomboidForge$makeBackupFile";
|
||||
|
||||
private static final Anchor.Method ANCHOR = new Anchor.Method(
|
||||
"makeBackupFile", "(Ljava/lang/String;Lzombie/core/backup/ZipBackup$BackupTypes;)V"
|
||||
);
|
||||
|
||||
private static final Hook HOOK = new Hook(
|
||||
"dev/overlord/zomboidforge/compat/b42/hooks/StorageHooks", "backupRunning", "(Z)V"
|
||||
);
|
||||
|
||||
@Override
|
||||
public String target() { return BackupStoragePatch.TARGET; }
|
||||
@Override
|
||||
public Anchor.Method anchor() { return BackupStoragePatch.ANCHOR; }
|
||||
@Override
|
||||
public Hook hook() { return BackupStoragePatch.HOOK; }
|
||||
|
||||
@Override
|
||||
public void apply(ClassNode node)
|
||||
{
|
||||
MethodNode original = BackupStoragePatch.ANCHOR.resolve(node);
|
||||
|
||||
if ((original.access & Opcodes.ACC_STATIC) == 0)
|
||||
throw new IllegalStateException("'" + node.name + "." + original.name + original.desc + "' is not static.");
|
||||
|
||||
boolean taken = node.methods.stream().anyMatch(method -> method.name.equals(BackupStoragePatch.RENAMED));
|
||||
if (taken) throw new IllegalStateException("'" + node.name + "' already has a method '" + BackupStoragePatch.RENAMED + "'.");
|
||||
|
||||
MethodNode wrapper = new MethodNode(original.access, original.name, original.desc, original.signature, original.exceptions.toArray(String[]::new));
|
||||
original.name = BackupStoragePatch.RENAMED;
|
||||
|
||||
BackupStoragePatch.wrap(node.name, original.desc, wrapper);
|
||||
node.methods.add(wrapper);
|
||||
}
|
||||
|
||||
/** {@code hook(true); try { renamed(args); } finally { hook(false); }} */
|
||||
private static void wrap(String owner, String desc, MethodNode wrapper)
|
||||
{
|
||||
Type[] arguments = Type.getArgumentTypes(desc);
|
||||
int thrown = arguments.length;
|
||||
LabelNode start = new LabelNode();
|
||||
LabelNode end = new LabelNode();
|
||||
LabelNode handler = new LabelNode();
|
||||
InsnList code = wrapper.instructions;
|
||||
|
||||
for (Type argument : arguments) {
|
||||
if (argument.getSize() != 1) throw new IllegalStateException("'" + owner + "." + BackupStoragePatch.ANCHOR.name() + desc + "' has a wide argument; the wrapper only passes one-slot arguments.");
|
||||
}
|
||||
|
||||
code.add(BackupStoragePatch.hook(true));
|
||||
code.add(start);
|
||||
|
||||
for (int i = 0; i < arguments.length; i++) code.add(new VarInsnNode(arguments[i].getOpcode(Opcodes.ILOAD), i));
|
||||
|
||||
code.add(new MethodInsnNode(Opcodes.INVOKESTATIC, owner, BackupStoragePatch.RENAMED, desc, false));
|
||||
code.add(end);
|
||||
code.add(BackupStoragePatch.hook(false));
|
||||
code.add(new InsnNode(Opcodes.RETURN));
|
||||
|
||||
code.add(handler);
|
||||
code.add(new VarInsnNode(Opcodes.ASTORE, thrown));
|
||||
code.add(BackupStoragePatch.hook(false));
|
||||
code.add(new VarInsnNode(Opcodes.ALOAD, thrown));
|
||||
code.add(new InsnNode(Opcodes.ATHROW));
|
||||
|
||||
wrapper.tryCatchBlocks.add(new TryCatchBlockNode(start, end, handler, null));
|
||||
}
|
||||
|
||||
private static InsnList hook(boolean running)
|
||||
{
|
||||
InsnList code = new InsnList();
|
||||
|
||||
code.add(new InsnNode(running ? Opcodes.ICONST_1 : Opcodes.ICONST_0));
|
||||
code.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BackupStoragePatch.HOOK.owner(), BackupStoragePatch.HOOK.name(), BackupStoragePatch.HOOK.desc(), false));
|
||||
|
||||
return code;
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.patches;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.support.CallRedirect;
|
||||
import dev.overlord.zomboidforge.compat.b42.patches.support.ClassDefiner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
|
||||
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
||||
import static org.objectweb.asm.Opcodes.ALOAD;
|
||||
import static org.objectweb.asm.Opcodes.ATHROW;
|
||||
import static org.objectweb.asm.Opcodes.DUP;
|
||||
import static org.objectweb.asm.Opcodes.IFEQ;
|
||||
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.RETURN;
|
||||
import static org.objectweb.asm.Opcodes.V25;
|
||||
|
||||
class BackupStoragePatchTest
|
||||
{
|
||||
private static final String ZIP_BACKUP = "zombie/core/backup/ZipBackup";
|
||||
private static final String TYPES = "zombie/core/backup/ZipBackup$BackupTypes";
|
||||
private static final String DESC = "(Ljava/lang/String;L" + BackupStoragePatchTest.TYPES + ";)V";
|
||||
private static final String CALLS = Type.getInternalName(BackupStoragePatchTest.Calls.class);
|
||||
|
||||
/** Stands in for {@code StorageHooks.backupRunning(boolean)} and records the calls. */
|
||||
public static final class Calls
|
||||
{
|
||||
public static final List<String> LOG = new ArrayList<>();
|
||||
|
||||
private Calls() {}
|
||||
|
||||
public static void record(String call) { Calls.LOG.add(call); }
|
||||
|
||||
public static void backupRunning(boolean running) { Calls.LOG.add("backupRunning " + running); }
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code ZipBackup} with the vanilla method's shape:
|
||||
*
|
||||
* <pre>{@code
|
||||
* private static void makeBackupFile(String serverName, BackupTypes type)
|
||||
* {
|
||||
* if (serverName.equals("fail")) throw new IllegalStateException("zip failed");
|
||||
* Calls.record("backup " + serverName);
|
||||
* }
|
||||
* }</pre>
|
||||
*/
|
||||
private static ClassNode zipBackup()
|
||||
{
|
||||
ClassNode node = BackupStoragePatchTest.emptyClass(BackupStoragePatchTest.ZIP_BACKUP);
|
||||
|
||||
MethodVisitor method = node.visitMethod(ACC_PRIVATE | ACC_STATIC, "makeBackupFile", BackupStoragePatchTest.DESC, null, null);
|
||||
Label normal = new Label();
|
||||
|
||||
method.visitVarInsn(ALOAD, 0);
|
||||
method.visitLdcInsn("fail");
|
||||
method.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
|
||||
method.visitJumpInsn(IFEQ, normal);
|
||||
method.visitTypeInsn(NEW, "java/lang/IllegalStateException");
|
||||
method.visitInsn(DUP);
|
||||
method.visitLdcInsn("zip failed");
|
||||
method.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V", false);
|
||||
method.visitInsn(ATHROW);
|
||||
|
||||
method.visitLabel(normal);
|
||||
method.visitVarInsn(ALOAD, 0);
|
||||
method.visitMethodInsn(INVOKESTATIC, BackupStoragePatchTest.CALLS, "record", "(Ljava/lang/String;)V", false);
|
||||
method.visitInsn(RETURN);
|
||||
method.visitMaxs(0, 0);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private static ClassNode emptyClass(String name)
|
||||
{
|
||||
ClassNode node = new ClassNode();
|
||||
node.visit(V25, ACC_PUBLIC, name, null, "java/lang/Object", null);
|
||||
|
||||
MethodVisitor init = node.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
|
||||
init.visitVarInsn(ALOAD, 0);
|
||||
init.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
||||
init.visitInsn(RETURN);
|
||||
init.visitMaxs(0, 0);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/** Patches {@code ZipBackup}, points the hook at {@link Calls}, and returns {@code makeBackupFile}. */
|
||||
private static Method patched() throws ReflectiveOperationException
|
||||
{
|
||||
BackupStoragePatch patch = new BackupStoragePatch();
|
||||
ClassNode zipBackup = BackupStoragePatchTest.zipBackup();
|
||||
|
||||
patch.apply(zipBackup);
|
||||
CallRedirect.redirect(zipBackup, patch.hook().owner(), patch.hook().name(), BackupStoragePatchTest.CALLS);
|
||||
|
||||
ClassDefiner definer = new ClassDefiner();
|
||||
Class<?> types = definer.define(BackupStoragePatchTest.emptyClass(BackupStoragePatchTest.TYPES));
|
||||
Class<?> loaded = definer.define(zipBackup);
|
||||
|
||||
Method method = loaded.getDeclaredMethod("makeBackupFile", String.class, types);
|
||||
method.setAccessible(true);
|
||||
|
||||
BackupStoragePatchTest.Calls.LOG.clear();
|
||||
return method;
|
||||
}
|
||||
|
||||
@Test
|
||||
void anchorMatchesTheTarget()
|
||||
{
|
||||
new BackupStoragePatch().anchor().verify(BackupStoragePatchTest.zipBackup());
|
||||
}
|
||||
|
||||
@Test
|
||||
void callsTheHookAroundTheBackup() throws ReflectiveOperationException
|
||||
{
|
||||
BackupStoragePatchTest.patched().invoke(null, "server", null);
|
||||
|
||||
assertEquals(List.of("backupRunning true", "server", "backupRunning false"), BackupStoragePatchTest.Calls.LOG);
|
||||
}
|
||||
|
||||
@Test
|
||||
void callsTheHookAfterAFailedBackupToo() throws ReflectiveOperationException
|
||||
{
|
||||
Method method = BackupStoragePatchTest.patched();
|
||||
|
||||
InvocationTargetException e = assertThrows(InvocationTargetException.class, () -> method.invoke(null, "fail", null));
|
||||
|
||||
assertInstanceOf(IllegalStateException.class, e.getCause());
|
||||
assertEquals(List.of("backupRunning true", "backupRunning false"), BackupStoragePatchTest.Calls.LOG);
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenTheRenamedMethodExists()
|
||||
{
|
||||
ClassNode zipBackup = BackupStoragePatchTest.zipBackup();
|
||||
zipBackup.visitMethod(ACC_PRIVATE | ACC_STATIC, "zomboidForge$makeBackupFile", "()V", null, null);
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () -> new BackupStoragePatch().apply(zipBackup));
|
||||
assertEquals("'zombie/core/backup/ZipBackup' already has a method 'zomboidForge$makeBackupFile'.", e.getMessage());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user