aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gq/malwarefight/nosession/tweaks/cleanup/CleanupTweaker.java
blob: a0d458bb5da64f15a83932a4cde2785b7240828f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gq.malwarefight.nosession.tweaks.cleanup;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import net.minecraft.launchwrapper.ITweaker;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import net.minecraftforge.fml.common.asm.ASMTransformerWrapper;

import java.io.File;
import java.lang.reflect.*;
import java.net.URL;
import java.net.URLStreamHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import gq.malwarefight.nosession.utils.Utils;

public class CleanupTweaker implements ITweaker {
    /**
     * Cached to avoid expensive reflection operations
     */
    public Method makeWrapper;

    public CleanupTweaker() throws NoSuchMethodException {
        makeWrapper = ASMTransformerWrapper.class.getDeclaredMethod("makeWrapper", String.class);
        makeWrapper.setAccessible(true);
    }
    @Override
    @SuppressWarnings("unchecked")
    public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
        // add the FMLTweaker
        ArrayList<String> tweakerList = (ArrayList<String>) Launch.blackboard.get("TweakClasses");
        tweakerList.add("net.minecraftforge.fml.common.launcher.FMLTweaker");
        // reset ASMTransformerWrapper
        Class<ASMTransformerWrapper> cls = ASMTransformerWrapper.class;
        try {
            Utils.setStaticValue(cls, "wrapperModMap", new HashMap<String, String>());
            Utils.setStaticValue(cls, "wrapperParentMap", new HashMap<String, String>());
            //noinspection UnstableApiUsage
            Utils.setStaticValue(cls, "wrapperCache",  CacheBuilder.newBuilder().maximumSize(30L).weakValues().build(new CacheLoader<String, byte[]>() {
                public byte[] load(String file) throws Exception {
                    return (byte[]) makeWrapper.invoke(null, file);
                }
            }));
            Utils.setStaticValue(cls, "asmGenRoot", new URL("asmgen", null, -1, "/", getAsmGenHandler()));
            Utils.setStaticValue(cls, "injected", false);
        } catch (Exception e) {
            System.err.println("NoSession: Fixing ASMTransformerWrapper failed. Things may break.");
            e.printStackTrace();
        }
    }

    @Override
    public void injectIntoClassLoader(LaunchClassLoader classLoader) {}

    @Override
    public String getLaunchTarget() {
        return "net.minecraft.client.main.Main";
    }

    @Override
    public String[] getLaunchArguments() {
        return new String[0];
    }



    @SuppressWarnings("unchecked")
    private URLStreamHandler getAsmGenHandler() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<ASMTransformerWrapper> cls = ASMTransformerWrapper.class;
        for (Class<?> candidate: cls.getDeclaredClasses()) {
            if (candidate.getSimpleName().equals("ASMGenHandler")) {
                Method pgdc = Class.class.getDeclaredMethod("privateGetDeclaredConstructors", boolean.class);
                pgdc.setAccessible(true);
                Constructor<? extends URLStreamHandler> constructor = ((Constructor<? extends URLStreamHandler>[]) pgdc.invoke(candidate, false))[0];
                constructor.setAccessible(true);
                return constructor.newInstance();
            }
        }
        throw new RuntimeException("NoSession: unable to get the ASMGenHandler");
    }
}