Add BackupHooks and ParallelZip for Build 42.21. Implement optimized backup file creation with parallel processing. Include hooks, utilities, and unit tests for validation.
ci / image (42, public) (push) Successful in 3m38s
ci / image (42, public) (push) Successful in 3m38s
This commit is contained in:
1 parent
7ffcbdbb06
commit
07dc3f0fad
9 files changed
+1153
-5
No files matched your search
@@ -12,18 +12,20 @@ import org.objectweb.asm.tree.MethodNode;
|
||||
import org.objectweb.asm.tree.TryCatchBlockNode;
|
||||
import org.objectweb.asm.tree.VarInsnNode;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/** Replaces the body of a method of the target class with a call to a {@link Hook}, or wraps it in hook calls. */
|
||||
public final class MethodBody
|
||||
{
|
||||
private static final Type OBJECT = Type.getObjectType("java/lang/Object");
|
||||
|
||||
private MethodBody() {}
|
||||
|
||||
/**
|
||||
* Replaces the body of {@code method} with {@code return hook(arguments);}: the hook receives
|
||||
* the method's arguments, without {@code this}, and has the method's parameter and return
|
||||
* types. Fails when the method has no body, when the types differ, or when an instance
|
||||
* method's old body uses {@code this}, which the hook would not receive.
|
||||
* types; a parameter of a reference type may be {@code Object} in the hook, for types the hook
|
||||
* cannot name (private classes of the game). Fails when the method has no body, when the types
|
||||
* differ otherwise, or when an instance method's old body uses {@code this}, which the hook
|
||||
* would not receive.
|
||||
*/
|
||||
public static void delegate(ClassNode node, MethodNode method, Hook hook)
|
||||
{
|
||||
@@ -35,7 +37,7 @@ public final class MethodBody
|
||||
if ((method.access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) != 0)
|
||||
throw new IllegalStateException(name + " has no body.");
|
||||
|
||||
if (!Arrays.equals(type.getArgumentTypes(), hookType.getArgumentTypes()) || !type.getReturnType().equals(hookType.getReturnType()))
|
||||
if (!MethodBody.accepts(hookType.getArgumentTypes(), type.getArgumentTypes()) || !type.getReturnType().equals(hookType.getReturnType()))
|
||||
throw new IllegalStateException(name + " and its hook '" + hook.owner() + "." + hook.name() + hook.desc() + "' have different types.");
|
||||
|
||||
if (!isStatic && MethodBody.usesThis(method))
|
||||
@@ -123,6 +125,22 @@ public final class MethodBody
|
||||
return new MethodInsnNode(Opcodes.INVOKESTATIC, hook.owner(), hook.name(), hook.desc(), false);
|
||||
}
|
||||
|
||||
/** Whether a hook with {@code parameters} takes {@code arguments}: equal types, or {@code Object} for a reference type. */
|
||||
private static boolean accepts(Type[] parameters, Type[] arguments)
|
||||
{
|
||||
if (parameters.length != arguments.length) return false;
|
||||
|
||||
for (int i = 0; i < arguments.length; i++)
|
||||
{
|
||||
boolean reference = arguments[i].getSort() == Type.OBJECT || arguments[i].getSort() == Type.ARRAY;
|
||||
boolean asObject = reference && parameters[i].equals(MethodBody.OBJECT);
|
||||
|
||||
if (!parameters[i].equals(arguments[i]) && !asObject) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Whether the method reads or writes local variable 0, which holds {@code this}. */
|
||||
private static boolean usesThis(MethodNode method)
|
||||
{
|
||||
|
||||
@@ -45,6 +45,10 @@ public class MethodBodyTest
|
||||
|
||||
public static void record(String value) { MethodBodyTest.recorded = value; }
|
||||
|
||||
public static void recordAny(String value, Object other) { MethodBodyTest.recorded = value + " " + other; }
|
||||
|
||||
public static void recordObject(Object value) { MethodBodyTest.recorded = String.valueOf(value); }
|
||||
|
||||
/** Calls of the hooks below, in order. */
|
||||
public static final List<String> EVENTS = new ArrayList<>();
|
||||
|
||||
@@ -148,6 +152,28 @@ public class MethodBodyTest
|
||||
assertEquals("saved", MethodBodyTest.recorded);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hookTakesAReferenceArgumentAsObject() throws ReflectiveOperationException
|
||||
{
|
||||
ClassNode node = MethodBodyTest.target(ACC_PUBLIC | ACC_STATIC, "save", "(Ljava/lang/String;Ljava/lang/StringBuilder;)V", false);
|
||||
MethodBody.delegate(node, MethodBodyTest.method(node, "save"), new Hook(MethodBodyTest.HOOKS, "recordAny", "(Ljava/lang/String;Ljava/lang/Object;)V"));
|
||||
|
||||
MethodBodyTest.recorded = null;
|
||||
MethodBodyTest.run(node, "save", new Class<?>[] { String.class, StringBuilder.class }, "saved", new StringBuilder("text"));
|
||||
assertEquals("saved text", MethodBodyTest.recorded);
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenTheHookTakesAPrimitiveArgumentAsObject()
|
||||
{
|
||||
ClassNode node = MethodBodyTest.target(ACC_PUBLIC | ACC_STATIC, "save", "(I)V", false);
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, () ->
|
||||
MethodBody.delegate(node, MethodBodyTest.method(node, "save"), new Hook(MethodBodyTest.HOOKS, "recordObject", "(Ljava/lang/Object;)V"))
|
||||
);
|
||||
assertTrue(e.getMessage().contains("different types"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dropsTheLocalVariablesOfTheOldBody()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user