2022-11-03 00:40:05 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* Prism Launcher
|
|
|
|
*
|
2022-11-04 22:48:17 +05:30
|
|
|
* Copyright (C) 2022 icelimetea <fr3shtea@outlook.com>
|
2022-11-03 00:40:05 +05:30
|
|
|
* Copyright (C) 2022 solonovamax <solonovamax@12oclockpoint.com>
|
|
|
|
* 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/>.
|
2022-11-04 22:48:17 +05:30
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 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.
|
2022-11-03 00:40:05 +05:30
|
|
|
*/
|
|
|
|
|
2022-11-01 21:57:04 +05:30
|
|
|
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 java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
public final class ReflectionUtils {
|
2022-11-03 22:10:23 +05:30
|
|
|
|
2022-11-01 21:57:04 +05:30
|
|
|
private static final Logger LOGGER = Logger.getLogger("ReflectionUtils");
|
|
|
|
|
2022-11-04 00:09:34 +05:30
|
|
|
private ReflectionUtils() {
|
|
|
|
}
|
2022-11-01 21:57:04 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate an applet class by name
|
|
|
|
*
|
|
|
|
* @param appletClassName The name of the applet class to resolve
|
|
|
|
*
|
|
|
|
* @return The instantiated applet class
|
|
|
|
*
|
|
|
|
* @throws ClassNotFoundException if the provided class name cannot be found
|
|
|
|
* @throws NoSuchMethodException if the no-args constructor cannot be found
|
2022-11-03 23:00:13 +05:30
|
|
|
* @throws IllegalAccessException if the constructor cannot be accessed via
|
|
|
|
* method handles
|
2022-11-01 21:57:04 +05:30
|
|
|
* @throws Throwable any exceptions from the class's constructor
|
|
|
|
*/
|
2022-11-04 00:09:34 +05:30
|
|
|
public static Applet createAppletClass(String appletClassName) throws Throwable {
|
2022-11-01 21:57:04 +05:30
|
|
|
Class<?> appletClass = ClassLoader.getSystemClassLoader().loadClass(appletClassName);
|
|
|
|
|
2022-11-03 23:00:13 +05:30
|
|
|
MethodHandle appletConstructor = MethodHandles.lookup().findConstructor(appletClass,
|
|
|
|
MethodType.methodType(void.class));
|
2022-11-01 21:57:04 +05:30
|
|
|
return (Applet) appletConstructor.invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a field that looks like a Minecraft base folder in a supplied class
|
|
|
|
*
|
|
|
|
* @param minecraftMainClass the class to scan
|
|
|
|
*
|
|
|
|
* @return The found field.
|
|
|
|
*/
|
2022-11-04 00:09:34 +05:30
|
|
|
public static Field getMinecraftGameDirField(Class<?> minecraftMainClass) {
|
2022-11-01 21:57:04 +05:30
|
|
|
LOGGER.fine("Resolving minecraft game directory field");
|
|
|
|
// Field we're looking for is always
|
|
|
|
// private static File obfuscatedName = null;
|
|
|
|
for (Field field : minecraftMainClass.getDeclaredFields()) {
|
|
|
|
// Has to be File
|
|
|
|
if (field.getType() != File.class) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fieldModifiers = field.getModifiers();
|
|
|
|
|
|
|
|
// Must be static
|
|
|
|
if (!Modifier.isStatic(fieldModifiers)) {
|
|
|
|
LOGGER.log(Level.FINE, "Rejecting field {0} because it is not static", field.getName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be private
|
|
|
|
if (!Modifier.isPrivate(fieldModifiers)) {
|
|
|
|
LOGGER.log(Level.FINE, "Rejecting field {0} because it is not private", field.getName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must not be final
|
|
|
|
if (Modifier.isFinal(fieldModifiers)) {
|
|
|
|
LOGGER.log(Level.FINE, "Rejecting field {0} because it is final", field.getName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-03 23:00:13 +05:30
|
|
|
LOGGER.log(Level.FINE, "Identified field {0} to match conditions for minecraft game directory field",
|
|
|
|
field.getName());
|
2022-11-01 21:57:04 +05:30
|
|
|
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve main entrypoint and returns method handle for it.
|
|
|
|
* <p>
|
2022-11-03 23:00:13 +05:30
|
|
|
* Resolves a method that matches the following signature <code>
|
2022-11-01 21:57:04 +05:30
|
|
|
* public static void main(String[] args) {
|
|
|
|
* <p>
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param entrypointClass The entrypoint class to resolve the method from
|
|
|
|
*
|
|
|
|
* @return The method handle for the resolved entrypoint
|
|
|
|
*
|
2022-11-03 23:00:13 +05:30
|
|
|
* @throws NoSuchMethodException If no method matching the correct signature
|
|
|
|
* can be found
|
2022-11-01 21:57:04 +05:30
|
|
|
* @throws IllegalAccessException If method handles cannot access the entrypoint
|
|
|
|
*/
|
2022-11-03 23:00:13 +05:30
|
|
|
public static MethodHandle findMainEntrypoint(Class<?> entrypointClass)
|
2022-11-04 00:09:34 +05:30
|
|
|
throws NoSuchMethodException, IllegalAccessException {
|
2022-11-03 23:00:13 +05:30
|
|
|
return MethodHandles.lookup().findStatic(entrypointClass, "main",
|
|
|
|
MethodType.methodType(void.class, String[].class));
|
2022-11-01 21:57:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve main entrypoint and returns method handle for it.
|
|
|
|
* <p>
|
2022-11-03 23:00:13 +05:30
|
|
|
* Resolves a method that matches the following signature <code>
|
2022-11-01 21:57:04 +05:30
|
|
|
* public static void main(String[] args) {
|
|
|
|
* <p>
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
2022-11-03 23:00:13 +05:30
|
|
|
* @param entrypointClassName The name of the entrypoint class to resolve the
|
|
|
|
* method from
|
2022-11-01 21:57:04 +05:30
|
|
|
*
|
|
|
|
* @return The method handle for the resolved entrypoint
|
|
|
|
*
|
2022-11-03 23:00:13 +05:30
|
|
|
* @throws ClassNotFoundException If a class cannot be found with the provided
|
|
|
|
* name
|
|
|
|
* @throws NoSuchMethodException If no method matching the correct signature
|
|
|
|
* can be found
|
2022-11-01 21:57:04 +05:30
|
|
|
* @throws IllegalAccessException If method handles cannot access the entrypoint
|
|
|
|
*/
|
2022-11-03 22:10:23 +05:30
|
|
|
public static MethodHandle findMainMethod(String entrypointClassName)
|
2022-11-04 00:09:34 +05:30
|
|
|
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException {
|
|
|
|
return findMainEntrypoint(ClassLoader.getSystemClassLoader().loadClass(entrypointClassName));
|
|
|
|
}
|
2022-11-03 22:10:23 +05:30
|
|
|
|
2022-11-01 21:57:04 +05:30
|
|
|
}
|