Move legacy support classes to another jar

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad
2023-01-06 09:21:09 +00:00
parent cb32711077
commit 17317ea308
23 changed files with 150 additions and 161 deletions

View File

@ -1,94 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module. An independent module is a module which is
* not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the
* library, but you are not obliged to do so. If you do not wish to do
* so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.utils;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.charset.StandardCharsets;
import org.prismlauncher.utils.logging.Log;
/**
* Uses Base64 with Java 8 or later, otherwise DatatypeConverter. In the latter
* case, reflection is used to allow using newer compilers.
*/
public final class Base64 {
private static boolean supported = true;
private static MethodHandle legacy;
static {
try {
Class.forName("java.util.Base64");
} catch (ClassNotFoundException e) {
try {
Class<?> datatypeConverter = Class.forName("javax.xml.bind.DatatypeConverter");
legacy = MethodHandles.lookup().findStatic(datatypeConverter, "parseBase64Binary",
MethodType.methodType(byte[].class, String.class));
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e1) {
Log.error("Base64 not supported", e1);
supported = false;
}
}
}
/**
* Determines whether base64 is supported.
*
* @return <code>true</code> if base64 can be parsed
*/
public static boolean isSupported() {
return supported;
}
public static byte[] decode(String input) {
if (!isSupported())
throw new UnsupportedOperationException();
if (legacy == null)
return java.util.Base64.getDecoder().decode(input.getBytes(StandardCharsets.UTF_8));
try {
return (byte[]) legacy.invokeExact(input);
} catch (Error | RuntimeException e) {
throw e;
} catch (Throwable e) {
throw new Error(e);
}
}
}

View File

@ -1,412 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module. An independent module is a module which is
* not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the
* library, but you are not obliged to do so. If you do not wish to do
* so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.prismlauncher.exception.JsonParseException;
/**
* Single-file JSON parser to allow for usage in versions without GSON.
*/
public final class JsonParser {
private final Reader in;
private char[] buffer;
private int pos, length;
public static Object parse(String in) throws JsonParseException, IOException {
return parse(new StringReader(in));
}
public static Object parse(InputStream in) throws JsonParseException, IOException {
return parse(new InputStreamReader(in, StandardCharsets.UTF_8));
}
public static Object parse(Reader in) throws JsonParseException, IOException {
return new JsonParser(in).readSingleValue();
}
private JsonParser(Reader in) throws IOException {
this.in = in;
pos = length = 0;
read();
}
private int character() {
if (length == -1)
return -1;
return buffer[pos];
}
private int read() throws IOException {
if (length == -1)
return -1;
if (buffer == null || pos++ == length - 1) {
pos = 0;
buffer = new char[8192];
length = in.read(buffer);
}
return character();
}
private void assertCharacter(char character) throws JsonParseException {
if (character() != character)
throw new JsonParseException("Expected '" + character + "' but got "
+ (character() != -1 ? ("'" + (char) character() + "'") : "EOF"));
}
private void assertNoEOF(String expected) throws JsonParseException {
if (character() == -1)
throw new JsonParseException("Expected " + expected + " but got EOF");
}
private void skipWhitespace() throws IOException {
while (isWhitespace())
read();
}
private boolean isWhitespace() {
return character() == ' ' || character() == '\n' || character() == '\r' || character() == '\t';
}
private Object readSingleValue() throws IOException {
skipWhitespace();
Object result = readValue();
if (!(result instanceof Double))
read();
skipWhitespace();
if (character() != -1)
throw new JsonParseException("Found trailing non-whitespace characters");
return result;
}
private Object readValue() throws IOException {
assertNoEOF("a value");
int character = character();
switch (character) {
case '{':
return readObject();
case '[':
return readArray();
case '"':
return readString();
case 't':
case 'f':
// probably boolean
Boolean bool = readBoolean();
if (bool != null)
return bool;
break;
case 'n':
// probably null
if (readNull())
return null;
break;
}
if (character == '-' || isDigit())
// probably a number
return readNumber();
throw new JsonParseException("Expected a JSON value but got '" + (char) character + "'");
}
private Map<String, Object> readObject() throws IOException {
assertCharacter('{');
Map<String, Object> obj = new HashMap<>();
boolean comma = false;
read();
skipWhitespace();
while (character() != '}') {
if (comma) {
assertCharacter(',');
read();
skipWhitespace();
}
String key = readString();
read();
skipWhitespace();
assertCharacter(':');
read();
skipWhitespace();
Object value = readValue();
obj.put(key, value);
if (!(value instanceof Double))
read();
skipWhitespace();
comma = true;
}
return obj;
}
private List<Object> readArray() throws IOException {
assertCharacter('[');
List<Object> array = new ArrayList<>();
boolean comma = false;
read();
skipWhitespace();
while (character() != ']') {
if (comma) {
assertCharacter(',');
read();
skipWhitespace();
}
Object value = readValue();
array.add(value);
if (!(value instanceof Double))
read();
skipWhitespace();
comma = true;
}
return array;
}
private String readString() throws IOException {
assertCharacter('"');
StringBuilder result = new StringBuilder();
while (read() != '"') {
int character = character();
if (character >= '\u0000' && character <= '\u001F')
throw new JsonParseException("Found unescaped control character within string");
switch (character) {
case -1:
throw new JsonParseException("Expected '\"' but got EOF");
case 0x7F:
if (read() == '"') {
return result.toString();
}
continue;
case '\\':
int seq = read();
switch (seq) {
case -1:
throw new JsonParseException("Expected an escape sequence but got EOF");
case '\\':
break;
case '/':
case '\"':
character = seq;
break;
case 'b':
character = '\b';
break;
case 'f':
character = '\f';
break;
case 'n':
character = '\n';
break;
case 'r':
character = '\r';
break;
case 't':
character = '\t';
break;
case 'u':
// char array to allow allocation in advance.
char[] digits = new char[4];
for (int index = 0; index < digits.length; index++) {
character = read();
if (index == 0 && character() == '-') {
throw new JsonParseException("Hex sequence may not be negative");
} else if (character() == -1) {
throw new JsonParseException("Expected a hex sequence but got EOF");
}
digits[index] = (char) character;
}
String digitsString = new String(digits);
try {
character = Integer.parseInt(digitsString, 16);
} catch (NumberFormatException e) {
throw new JsonParseException("Could not parse hex sequence \"" + digitsString + "\"");
}
break;
default:
throw new JsonParseException("Invalid escape sequence: \\" + (char) seq);
}
break;
}
result.append((char) character);
}
return result.toString();
}
private boolean isDigit() {
return character() >= '0' && character() <= '9';
}
private Double readNumber() throws IOException {
StringBuilder result = new StringBuilder();
if (character() == '-') {
result.append((char) character());
read();
}
if (character() == '0') {
result.append((char) character());
read();
if (isDigit())
throw new JsonParseException("Found superfluous leading zero");
} else if (!isDigit())
throw new JsonParseException("Expected digits");
while (character() != -1 && isDigit()) {
result.append((char) character());
read();
}
if (character() == '.') {
result.append('.');
read();
assertNoEOF("digits");
if (!isDigit())
throw new JsonParseException("Expected digits after decimal point");
while (character() != -1 && isDigit()) {
result.append((char) character());
read();
}
}
if (character() == 'e' || character() == 'E') {
result.append('E');
read();
assertNoEOF("digits");
if (character() == '+' || character() == '-') {
result.append((char) character());
read();
}
if (!(character() == '+' || character() == '-' || isDigit()))
throw new JsonParseException("Expected exponent digits");
while (character() != -1 && isDigit()) {
result.append((char) character());
read();
}
}
String resultStr = result.toString();
try {
return Double.parseDouble(resultStr);
} catch (NumberFormatException e) {
throw new JsonParseException("Failed to parse number '" + resultStr + "'");
}
}
private Boolean readBoolean() throws IOException {
if (character() == 't') {
if (read() == 'r' && read() == 'u' && read() == 'e') {
return true;
}
} else if (character() == 'f') {
if (read() == 'a' && read() == 'l' && read() == 's' && read() == 'e') {
return false;
}
}
return null;
}
private boolean readNull() throws IOException {
return character() == 'n' && read() == 'u' && read() == 'l' && read() == 'l';
}
}

View File

@ -54,76 +54,15 @@
package org.prismlauncher.utils;
import java.applet.Applet;
import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.prismlauncher.utils.logging.Log;
public final class ReflectionUtils {
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final ClassLoader LOADER = ClassLoader.getSystemClassLoader();
/**
* Construct a Java applet by its class name.
*
* @param clazz The class name
* @return The applet instance
* @throws Throwable
*/
public static Applet createAppletClass(String clazz) throws Throwable {
Class<?> appletClass = LOADER.loadClass(clazz);
MethodHandle appletConstructor = LOOKUP.findConstructor(appletClass, MethodType.methodType(void.class));
return (Applet) appletConstructor.invoke();
}
/**
* Best guess of the game directory field within net.minecraft.client.Minecraft.
* Designed for legacy versions - newer versions do not use a static field.
*
* @param clazz The class
* @return The first field matching criteria
*/
public static Field findMinecraftGameDirField(Class<?> clazz) {
Log.debug("Resolving minecraft game directory field");
// search for private static File
for (Field field : clazz.getDeclaredFields()) {
if (field.getType() != File.class) {
continue;
}
int fieldModifiers = field.getModifiers();
if (!Modifier.isStatic(fieldModifiers)) {
Log.debug("Rejecting field " + field.getName() + " because it is not static");
continue;
}
if (!Modifier.isPrivate(fieldModifiers)) {
Log.debug("Rejecting field " + field.getName() + " because it is not private");
continue;
}
if (Modifier.isFinal(fieldModifiers)) {
Log.debug("Rejecting field " + field.getName() + " because it is final");
continue;
}
Log.debug("Identified field " + field.getName() + " to match conditions for game directory field");
return field;
}
return null;
}
/**
* Gets the main method within a class.
*

View File

@ -1,101 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module. An independent module is a module which is
* not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the
* library, but you are not obliged to do so. If you do not wish to do
* so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.utils.api;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import org.prismlauncher.utils.Base64;
import org.prismlauncher.utils.JsonParser;
/**
* Basic access to Mojang's Minecraft API.
*/
@SuppressWarnings("unchecked")
public final class MojangApi {
public static String getUuid(String username) throws IOException {
try (InputStream in = new URL("https://api.mojang.com/users/profiles/minecraft/" + username).openStream()) {
Map<String, Object> map = (Map<String, Object>) JsonParser.parse(in);
return (String) map.get("id");
}
}
public static Texture getTexture(String player, String name) throws IOException {
Map<String, Object> map = getTextures(player);
if (map != null) {
map = (Map<String, Object>) map.get(name);
if (map == null)
return null;
URL url = new URL((String) map.get("url"));
boolean slim = false;
if (name.equals("SKIN")) {
map = (Map<String, Object>) map.get("metadata");
if (map != null && "slim".equals(map.get("model")))
slim = true;
}
return new Texture(url, slim);
}
return null;
}
public static Map<String, Object> getTextures(String player) throws IOException {
try (InputStream profileIn = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + player)
.openStream()) {
Map<String, Object> profile = (Map<String, Object>) JsonParser.parse(profileIn);
for (Map<String, Object> property : (Iterable<Map<String, Object>>) profile.get("properties")) {
if (property.get("name").equals("textures")) {
Map<String, Object> result = (Map<String, Object>) JsonParser
.parse(new String(Base64.decode((String) property.get("value"))));
result = (Map<String, Object>) result.get("textures");
return result;
}
}
return null;
}
}
}

View File

@ -1,61 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module. An independent module is a module which is
* not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the
* library, but you are not obliged to do so. If you do not wish to do
* so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.utils.api;
import java.net.URL;
/**
* Represents a texture from the Mojang API.
*/
public final class Texture {
private final URL url;
private final boolean slim;
public Texture(URL url, boolean slim) {
this.url = url;
this.slim = slim;
}
public URL getUrl() {
return url;
}
public boolean isSlim() {
return slim;
}
}

View File

@ -45,7 +45,7 @@ import java.io.PrintStream;
public final class Log {
// original before possibly overridden by MC
private static final PrintStream OUT = new PrintStream(System.out), ERR = new PrintStream(System.err);
private static final PrintStream OUT = new PrintStream(System.out), ERR = new PrintStream(System.err);
private static final boolean DEBUG = Boolean.getBoolean("org.prismlauncher.debug");
public static void launcher(String message) {

View File

@ -1,79 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module. An independent module is a module which is
* not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the
* library, but you are not obliged to do so. If you do not wish to do
* so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.utils.url;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
public class CustomUrlConnection extends HttpURLConnection {
private InputStream in;
public CustomUrlConnection(byte[] data) {
this(new ByteArrayInputStream(data));
}
public CustomUrlConnection(InputStream in) {
super(null);
this.in = in;
}
@Override
public void connect() throws IOException {
responseCode = 200;
}
@Override
public void disconnect() {
try {
in.close();
} catch (IOException e) {
}
}
@Override
public InputStream getInputStream() throws IOException {
return in;
}
@Override
public boolean usingProxy() {
return false;
}
}

View File

@ -1,110 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module. An independent module is a module which is
* not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the
* library, but you are not obliged to do so. If you do not wish to do
* so, delete this exception statement from your version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.utils.url;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import org.prismlauncher.utils.logging.Log;
/**
* A utility class for URLs which uses reflection to access constructors for
* internal classes.
*/
public final class UrlUtils {
private static URLStreamHandler http;
private static MethodHandle openConnection;
static {
try {
// we first obtain the stock URLStreamHandler for http as we overwrite it later
Method getURLStreamHandler = URL.class.getDeclaredMethod("getURLStreamHandler", String.class);
getURLStreamHandler.setAccessible(true);
http = (URLStreamHandler) getURLStreamHandler.invoke(null, "http");
// we next find the openConnection method
Method openConnectionReflect = URLStreamHandler.class.getDeclaredMethod("openConnection", URL.class,
Proxy.class);
openConnectionReflect.setAccessible(true);
openConnection = MethodHandles.lookup().unreflect(openConnectionReflect);
} catch (Throwable e) {
Log.error("URL reflection failed - some features may not work", e);
}
}
/**
* Determines whether all the features of this class are available.
*
* @return <code>true</code> if all features can be used
*/
public static boolean isSupported() {
return http != null && openConnection != null;
}
public static URLConnection openConnection(URL url, Proxy proxy) throws IOException {
if (http == null)
throw new UnsupportedOperationException();
if (url.getProtocol().equals("http"))
return openConnection(http, url, proxy);
// fall back to Java's default method
// at this point, this should not cause a StackOverflowError unless we've missed
// a protocol out from the if statements
return url.openConnection();
}
public static URLConnection openConnection(URLStreamHandler handler, URL url, Proxy proxy) throws IOException {
if (openConnection == null)
throw new UnsupportedOperationException();
try {
return (URLConnection) openConnection.invokeExact(handler, url, proxy);
} catch (IOException | Error | RuntimeException e) {
throw e; // rethrow if possible
} catch (Throwable e) {
throw new AssertionError(e); // oh dear! this isn't meant to happen
}
}
}