diff options
author | PandaNinjas <admin@malwarefight.gq> | 2022-12-23 17:29:28 -0800 |
---|---|---|
committer | PandaNinjas <admin@malwarefight.gq> | 2022-12-23 17:29:28 -0800 |
commit | bd6b4db71f1e07a845d922dc30cefcc64afa6294 (patch) | |
tree | 3ec253f13a0bfa20a8c0b467657d80265c12593a /src/main/java/gq/malwarefight/nosession/mixin/Utils.java | |
download | NoSession-bd6b4db71f1e07a845d922dc30cefcc64afa6294.tar.gz NoSession-bd6b4db71f1e07a845d922dc30cefcc64afa6294.tar.bz2 NoSession-bd6b4db71f1e07a845d922dc30cefcc64afa6294.zip |
Initial commit
Diffstat (limited to 'src/main/java/gq/malwarefight/nosession/mixin/Utils.java')
-rw-r--r-- | src/main/java/gq/malwarefight/nosession/mixin/Utils.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/main/java/gq/malwarefight/nosession/mixin/Utils.java b/src/main/java/gq/malwarefight/nosession/mixin/Utils.java new file mode 100644 index 0000000..e67bddc --- /dev/null +++ b/src/main/java/gq/malwarefight/nosession/mixin/Utils.java @@ -0,0 +1,89 @@ +package gq.malwarefight.nosession.mixin; + +import gq.malwarefight.nosession.mixin.asm.ReplacingMethodVisitor; +import net.minecraft.launchwrapper.ITweaker; +import net.minecraft.launchwrapper.Launch; +import org.objectweb.asm.*; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Arrays; + +public class Utils { + static int num = 0; + + public static byte[] read(InputStream i, Character delimiter) throws IOException { + byte[] buffer = new byte[512]; + int index = 0; + while (true) { + int in = i.read(); + if (in == -1 || (delimiter != null && delimiter == in)) { + return Arrays.copyOfRange(buffer, 0, index); + } + if (index == buffer.length) { + // grow the buffer + byte[] newBuffer = new byte[buffer.length * 2]; + System.arraycopy( + buffer, 0, newBuffer, 0, buffer.length + ); + buffer = newBuffer; + } + buffer[index] = (byte) in; + index++; + } + } + + public static String readString(InputStream i, Character delimiter) throws IOException { + return new String(read(i, delimiter)); + } + + public static void createClass(byte[] classArray, String name) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method m = ClassLoader.class.getDeclaredMethod( + "defineClass", String.class, byte[].class, int.class, int.class); + m.setAccessible(true); + m.invoke(Launch.classLoader, name, classArray, 0, classArray.length); + } + + public static String getUniqueClassName() { + + String className = "gq.malwarefight.nosession.mixin.L2TweakerClone" + num; + + byte[] L2TweakerBytes; + try { + //noinspection ConstantConditions + L2TweakerBytes = read(Utils.class.getResourceAsStream("/gq/malwarefight/nosession/mixin/L2Tweaker.class"), null); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ClassReader cr = new ClassReader(L2TweakerBytes); + ClassWriter cw = new ClassWriter(cr, 0); + cr.accept(new ClassVisitor(Opcodes.ASM5, cw) { + @Override + public void visit(int version, int access, String name, + String signature, String superName, String[] interfaces) { + super.visit(version, access, className.replace(".", "/"), signature, superName, interfaces); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + return new ReplacingMethodVisitor(super.visitMethod(access, name, desc, signature, exceptions), L2Tweaker.class.getName().replace(".", "/"), className.replace(".", "/")); + } + }, 0); + + byte[] code = cw.toByteArray(); + try { + createClass(code, className); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + num++; + return className; + } +} |