aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gq/malwarefight/nosession/tweaks/initial/InitialTweaker.java
blob: 21b2646a260f59c54ee87a63703b0e4434c4e5f3 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gq.malwarefight.nosession.tweaks.initial;

import gq.malwarefight.nosession.relaunch.Relaunch;
import gq.malwarefight.nosession.utils.Utils;
import net.minecraft.launchwrapper.ITweaker;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Mixins;

import java.io.File;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class InitialTweaker implements ITweaker {

    public InitialTweaker() {
        System.gc(); // try to garbage collect the earlier launch data
    }

    /**
     * This handles the launch arguments passed towards minecraft
     * @param args The launch arguments
     * @param gameDir The game directory (ie: .minecraft)
     * @param assetsDir The assets directory
     * @param version The game version
     */
    @Override
    public final void acceptOptions(List<String> args, File gameDir, File assetsDir, String version) {
        boolean isRelaunch = false; // is this invocation from a relaunch?
        String uuid = null;
        ArrayList<String> argsCopy = new ArrayList<>(args);
        for (int i = 0; i < argsCopy.size(); i++) {
            if (argsCopy.get(i).equals("--accessToken")) {
                if (argsCopy.get(i + 1).equals("<noSessionAccessToken>")) {
                    isRelaunch = true;
                }
                try {
                    Utils.setToken(args.get(i + 1));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                argsCopy.set(i + 1, "<noSessionAccessToken>");
            } else if (argsCopy.get(i).equals("--uuid")) {
                uuid = argsCopy.get(i + 1);
            }
        }
        if (isRelaunch) {
            String finalUuid = uuid;
            if (finalUuid == null) {
                return; // if we don't have a uuid, quit
            }
            Runtime.getRuntime().addShutdownHook(
                    new Thread(() -> {
                        try {
                            Socket socket = Utils.getProperSocket(UUID.fromString(finalUuid));
                            socket.getOutputStream().write("fullquit\n".getBytes(StandardCharsets.UTF_8));
                            socket.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    })
            );
            return; // don't do anything, the change has already been made
        }
        System.out.println("=======================");
        System.out.println("NoSession: relaunching without the token");
        System.out.println("=======================");
        try {
            Relaunch.relaunch(argsCopy, gameDir, assetsDir, version);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    /**
     * Inject into the MC class loader
     * @param classLoader The class loader
     */
    @Override
    public final void injectIntoClassLoader(LaunchClassLoader classLoader) {
        MixinBootstrap.init();
        MixinEnvironment environment = MixinEnvironment.getDefaultEnvironment();
        Mixins.addConfiguration("mixins.nosession.json");
        // Check if the obfuscation context is null
        if (environment.getObfuscationContext() == null) {
            environment.setObfuscationContext("notch");
        }
        // This is a client side, client :)
        environment.setSide(MixinEnvironment.Side.CLIENT);
    }

    @Override
    public String getLaunchTarget() {
        return MixinBootstrap.getPlatform().getLaunchTarget();
    }

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

}