summaryrefslogtreecommitdiffstats
path: root/libraries/launcher/org/multimc/legacy/LegacyLauncher.java
blob: 347bb1a23492f44c1a94d86404e281cc91bf0efe (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package org.multimc.legacy;/*
 * Copyright 2012-2014 MultiMC Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.multimc.*;

import java.applet.Applet;
import java.awt.*;
import java.io.File;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class LegacyLauncher implements Launcher
{
	@Override
	public int launch(ParamBucket params)
	{
		String userName, sessionId, windowTitle, windowParams, lwjgl;
		String mainClass = "net.minecraft.client.Minecraft";
		try
		{
			userName = params.first("userName");
			sessionId = params.first("sessionId");
			windowTitle = params.first("windowTitle");
			windowParams = params.first("windowParams");
			lwjgl = params.first("lwjgl");
		} catch (NotFoundException e)
		{
			System.err.println("Not enough arguments.");
			return -1;
		}

		String cwd = System.getProperty("user.dir");
		Dimension winSize = new Dimension(854, 480);
		boolean maximize = false;

		String[] dimStrings = windowParams.split("x");

		if (windowParams.equalsIgnoreCase("max"))
		{
			maximize = true;
		}
		else if (dimStrings.length == 2)
		{
			try
			{
				winSize = new Dimension(Integer.parseInt(dimStrings[0]), Integer.parseInt(dimStrings[1]));
			} catch (NumberFormatException ignored) {}
		}

		File binDir = new File(cwd, "bin");
		File lwjglDir;
		if (lwjgl.equalsIgnoreCase("Mojang"))
		{
			lwjglDir = binDir;
		}
		else
		{
			lwjglDir = new File(lwjgl);
		}

		URL[] classpath;
		{
			try
			{
				classpath = new URL[]
				{
					new File(binDir, "minecraft.jar").toURI().toURL(),
					new File(lwjglDir, "lwjgl.jar").toURI().toURL(),
					new File(lwjglDir, "lwjgl_util.jar").toURI().toURL(),
					new File(lwjglDir, "jinput.jar").toURI().toURL(),
				};
			} catch (MalformedURLException e)
			{
				System.err.println("Class path entry is badly formed:");
				e.printStackTrace(System.err);
				return -1;
			}
		}

		String nativesDir = new File(lwjglDir, "natives").toString();

		System.setProperty("org.lwjgl.librarypath", nativesDir);
		System.setProperty("net.java.games.input.librarypath", nativesDir);

		// print the pretty things
		{
			Utils.log("Main Class:");
			Utils.log("  " + mainClass);
			Utils.log();

			Utils.log("Class Path:");
			for (URL s : classpath)
			{
				Utils.log("  " + s);
			}
			Utils.log();

			Utils.log("Native Path:");
			Utils.log("  " + nativesDir);
			Utils.log();
		}

		URLClassLoader cl = new URLClassLoader(classpath, LegacyLauncher.class.getClassLoader());

		// Get the Minecraft Class and set the base folder
		Class<?> mc;
		try
		{
			mc = cl.loadClass(mainClass);

			Field f = Utils.getMCPathField(mc);

			if (f == null)
			{
				System.err.println("Could not find Minecraft path field. Launch failed.");
				return -1;
			}

			f.setAccessible(true);
			f.set(null, new File(cwd));
		} catch (Exception e)
		{
			System.err.println("Could not set base folder. Failed to find/access Minecraft main class:");
			e.printStackTrace(System.err);
			return -1;
		}

		System.setProperty("minecraft.applet.TargetDirectory", cwd);

		String[] mcArgs = new String[2];
		mcArgs[0] = userName;
		mcArgs[1] = sessionId;

		Utils.log("Launching with applet wrapper...");
		try
		{
			Class<?> MCAppletClass = cl.loadClass("net.minecraft.client.MinecraftApplet");
			Applet mcappl = (Applet) MCAppletClass.newInstance();
			LegacyFrame mcWindow = new LegacyFrame(windowTitle);
			mcWindow.start(mcappl, userName, sessionId, winSize, maximize);
		} catch (Exception e)
		{
			Utils.log("Applet wrapper failed:", "Error");
			e.printStackTrace(System.err);
			Utils.log();
			Utils.log("Falling back to compatibility mode.");
			try
			{
				mc.getMethod("main", String[].class).invoke(null, (Object) mcArgs);
			} catch (Exception e1)
			{
				Utils.log("Failed to invoke the Minecraft main class:", "Fatal");
				e1.printStackTrace(System.err);
				return -1;
			}
		}

		return 0;
	}
}