Skip to content

Commit

Permalink
Remove redundant locators and clean-up leftover pre-modules code (#118)
Browse files Browse the repository at this point in the history
* Remove unused additional class/resource locators

* Remove unused `ITransformingClassLoaderBuilder`

* Secure jars are enabled at all times

* Remove redundant SECURED_JARS_ENABLED env property
  • Loading branch information
Su5eD authored Dec 26, 2023
1 parent 868a4d5 commit 26f7226
Show file tree
Hide file tree
Showing 17 changed files with 6 additions and 296 deletions.
6 changes: 0 additions & 6 deletions src/main/java/cpw/mods/modlauncher/ArgumentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class ArgumentHandler {
private OptionSpec<String> profileOption;
private OptionSpec<Path> gameDirOption;
private OptionSpec<Path> assetsDirOption;
private OptionSpec<Path> minecraftJarOption;
private OptionSpec<String> nonOption;
private OptionSpec<String> launchTarget;
private OptionSpec<String> uuidOption;
Expand All @@ -56,7 +55,6 @@ void processArguments(Environment env, Consumer<OptionParser> parserConsumer, Bi
profileOption = parser.accepts("version", "The version we launched with").withRequiredArg();
gameDirOption = parser.accepts("gameDir", "Alternative game directory").withRequiredArg().withValuesConvertedBy(new PathConverter(PathProperties.DIRECTORY_EXISTING)).defaultsTo(Path.of("."));
assetsDirOption = parser.accepts("assetsDir", "Assets directory").withRequiredArg().withValuesConvertedBy(new PathConverter(PathProperties.DIRECTORY_EXISTING));
minecraftJarOption = parser.accepts("minecraftJar", "Path to minecraft jar").withRequiredArg().withValuesConvertedBy(new PathConverter(PathProperties.READABLE)).withValuesSeparatedBy(',');
uuidOption = parser.accepts("uuid", "The UUID of the logging in player").withRequiredArg();
launchTarget = parser.accepts("launchTarget", "LauncherService target to launch").withRequiredArg();

Expand All @@ -71,10 +69,6 @@ void processArguments(Environment env, Consumer<OptionParser> parserConsumer, Bi
resultConsumer.accept(this.optionSet, this::optionResults);
}

Path[] getSpecialJars() {
return this.optionSet.valuesOf(minecraftJarOption).toArray(new Path[0]);
}

String getLaunchTarget() {
return this.optionSet.valueOf(launchTarget);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.lang.reflect.*;
import java.nio.file.*;
import java.util.concurrent.*;

/**
* Default launch handler service - will launch minecraft
Expand All @@ -36,14 +35,6 @@ public String name() {
return "minecraft";
}

@Override
public void configureTransformationClassLoader(final ITransformingClassLoaderBuilder builder) {
if (LAUNCH_PATH_STRING == null) {
throw new IllegalStateException("Missing "+ LAUNCH_PROPERTY +" environment property. Update your launcher!");
}
builder.addTransformationPath(FileSystems.getDefault().getPath(LAUNCH_PATH_STRING));
}

@Override
public ServiceRunner launchService(String[] arguments, ModuleLayer gameLayer) {

Expand Down
8 changes: 0 additions & 8 deletions src/main/java/cpw/mods/modlauncher/LaunchServiceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ public void launch(ArgumentHandler argumentHandler, ModuleLayer gameLayer, Trans
launch(launchTarget, args, gameLayer, classLoader, launchPluginHandler);
}

TransformingClassLoaderBuilder identifyTransformationTargets(final ArgumentHandler argumentHandler) {
final String launchTarget = argumentHandler.getLaunchTarget();
final TransformingClassLoaderBuilder builder = new TransformingClassLoaderBuilder();
Arrays.stream(argumentHandler.getSpecialJars()).forEach(builder::addTransformationPath);
launchHandlerLookup.get(launchTarget).configureTransformationClassLoaderBuilder(builder);
return builder;
}

void validateLaunchTarget(final ArgumentHandler argumentHandler) {
if (!launchHandlerLookup.containsKey(argumentHandler.getLaunchTarget())) {
LOGGER.error(MODLAUNCHER, "Cannot find launch target {}, unable to launch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,4 @@ public void launch(String[] arguments, ModuleLayer gameLayer) {
throw new RuntimeException(e);
}
}

public void configureTransformationClassLoaderBuilder(ITransformingClassLoaderBuilder builder) {
this.service.configureTransformationClassLoader(builder);
}
}
4 changes: 1 addition & 3 deletions src/main/java/cpw/mods/modlauncher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ private Launcher() {
environment.computePropertyIfAbsent(IEnvironment.Keys.MLSPEC_VERSION.get(), s->IEnvironment.class.getPackage().getSpecificationVersion());
environment.computePropertyIfAbsent(IEnvironment.Keys.MLIMPL_VERSION.get(), s->IEnvironment.class.getPackage().getImplementationVersion());
environment.computePropertyIfAbsent(IEnvironment.Keys.MODLIST.get(), s->new ArrayList<>());
environment.computePropertyIfAbsent(IEnvironment.Keys.SECURED_JARS_ENABLED.get(), k-> ProtectionDomainHelper.canHandleSecuredJars());
this.transformStore = new TransformStore();
this.transformationServicesHandler = new TransformationServicesHandler(this.transformStore, this.moduleLayerHandler);
this.argumentHandler = new ArgumentHandler();
Expand Down Expand Up @@ -99,8 +98,7 @@ private void run(String... args) {
this.transformationServicesHandler.initialiseServiceTransformers();
this.launchPlugins.offerScanResultsToPlugins(gameContents);
this.launchService.validateLaunchTarget(this.argumentHandler);
final TransformingClassLoaderBuilder classLoaderBuilder = this.launchService.identifyTransformationTargets(this.argumentHandler);
this.classLoader = this.transformationServicesHandler.buildTransformingClassLoader(this.launchPlugins, classLoaderBuilder, this.environment, this.moduleLayerHandler);
this.classLoader = this.transformationServicesHandler.buildTransformingClassLoader(this.launchPlugins, this.environment, this.moduleLayerHandler);
Thread.currentThread().setContextClassLoader(this.classLoader);
this.launchService.launch(this.argumentHandler, this.moduleLayerHandler.getLayer(IModuleLayerManager.Layer.GAME).orElseThrow(), this.classLoader, this.launchPlugins);
}
Expand Down
50 changes: 0 additions & 50 deletions src/main/java/cpw/mods/modlauncher/ProtectionDomainHelper.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public String name() {
return "testharness";
}

@Override
public void configureTransformationClassLoader(final ITransformingClassLoaderBuilder builder) {
}

public ServiceRunner launchService(String[] arguments, ModuleLayer gameLayer) {
try {
Class<?> callableLaunch = Class.forName(System.getProperty("test.harness.callable"), true, Thread.currentThread().getContextClassLoader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.jetbrains.annotations.Nullable;
import java.net.URL;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.*;

import static cpw.mods.modlauncher.LogMarkers.*;
Expand All @@ -38,8 +34,6 @@ public class TransformationServiceDecorator {
private static final Logger LOGGER = LogManager.getLogger();
private final ITransformationService service;
private boolean isValid;
private static Set<String> classPrefixes = new HashSet<>();
private static Set<String> resourceNames = new HashSet<>();

TransformationServiceDecorator(ITransformationService service) {
this.service = service;
Expand Down Expand Up @@ -103,64 +97,6 @@ List<ITransformationService.Resource> runScan(final Environment environment) {
return scanResults;
}

Function<String,Optional<URL>> getClassLoader() {
final Map.Entry<Set<String>, Supplier<Function<String, Optional<URL>>>> classesLocator = this.service.additionalClassesLocator();
if (classesLocator != null) {
final HashSet<String> packagePrefixes = new HashSet<>(classesLocator.getKey());
final Set<String> badPrefixes = packagePrefixes.stream().
filter(s ->
// No prefixes starting with net.minecraft.
s.startsWith("net.minecraft.") ||
// No prefixes starting with net.minecraftforge.
s.startsWith("net.minecraftforge.") ||
// No prefixes starting with net.neoforged.
s.startsWith("net.neoforged.") ||
// No prefixes already claimed
classPrefixes.contains(s) ||
// No prefixes not ending in a dot
!s.endsWith(".") ||
// No prefixes starting without a dot after the first character
s.indexOf('.') <= 0).
collect(Collectors.toSet());
if (!badPrefixes.isEmpty()) {
badPrefixes.forEach(s -> LOGGER.error("Illegal prefix specified for {} : {}", this.service.name(), s));
throw new IllegalArgumentException("Bad prefixes specified");
}
classPrefixes.addAll(classesLocator.getKey());
}

final Map.Entry<Set<String>, Supplier<Function<String, Optional<URL>>>> resourcesLocator = this.service.additionalResourcesLocator();
if (resourcesLocator!=null) {
final HashSet<String> resNames = new HashSet<>(resourcesLocator.getKey());
final Set<String> badResourceNames = resNames.stream().
filter(s -> s.endsWith(".class") || resourceNames.contains(s)).
collect(Collectors.toSet());
if (!badResourceNames.isEmpty()) {
badResourceNames.forEach(s -> LOGGER.error("Illegal resource name specified for {} : {}", this.service.name(), s));
throw new IllegalArgumentException("Bad resources specified");
}
resourceNames.addAll(resourcesLocator.getKey());
}
return s -> getOptionalURL(classesLocator, resourcesLocator, s);
}

private Optional<URL> getOptionalURL(@Nullable Map.Entry<Set<String>, Supplier<Function<String, Optional<URL>>>> classes, @org.jetbrains.annotations.Nullable Map.Entry<Set<String>, Supplier<Function<String, Optional<URL>>>> resources, final String name) {
if (classes != null && name.endsWith(".class")) {
for (String pfx : classes.getKey()) {
if (name.startsWith(pfx.replace('.','/'))) {
return classes.getValue().get().apply(name);
}
}
} else if (resources != null && !name.endsWith(".class")) {
for (String pfx : resources.getKey()) {
if (Objects.equals(name, pfx)) {
return resources.getValue().get().apply(name);
}
}
}
return Optional.empty();
}

public List<ITransformationService.Resource> onCompleteScan(IModuleLayerManager moduleLayerManager) {
return this.service.completeScan(moduleLayerManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import java.net.URL;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
Expand All @@ -52,9 +50,8 @@ List<ITransformationService.Resource> initializeTransformationServices(ArgumentH
return runScanningTransformationServices(environment);
}

TransformingClassLoader buildTransformingClassLoader(final LaunchPluginHandler pluginHandler, final TransformingClassLoaderBuilder builder, final Environment environment, final ModuleLayerHandler layerHandler) {
final List<Function<String, Optional<URL>>> classLocatorList = serviceLookup.values().stream().map(TransformationServiceDecorator::getClassLoader).filter(Objects::nonNull).collect(Collectors.toList());
final var layerInfo = layerHandler.buildLayer(IModuleLayerManager.Layer.GAME, (cf, parents)->new TransformingClassLoader(transformStore, pluginHandler, builder, environment, cf, parents));
TransformingClassLoader buildTransformingClassLoader(final LaunchPluginHandler pluginHandler, final Environment environment, final ModuleLayerHandler layerHandler) {
final var layerInfo = layerHandler.buildLayer(IModuleLayerManager.Layer.GAME, (cf, parents)->new TransformingClassLoader(transformStore, pluginHandler, environment, cf, parents));
layerHandler.updateLayer(IModuleLayerManager.Layer.PLUGIN, li->li.cl().setFallbackClassLoader(layerInfo.cl()));
return (TransformingClassLoader) layerInfo.cl();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandle
this.classTransformer = new ClassTransformer(transformStore, pluginHandler, this);
}

TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, TransformingClassLoaderBuilder builder, final Environment environment, final Configuration configuration, List<ModuleLayer> parentLayers) {
TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, final Environment environment, final Configuration configuration, List<ModuleLayer> parentLayers) {
super("TRANSFORMER", configuration, parentLayers);
TransformerAuditTrail tat = new TransformerAuditTrail();
environment.computePropertyIfAbsent(IEnvironment.Keys.AUDITTRAIL.get(), v->tat);
Expand Down

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/cpw/mods/modlauncher/api/IEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ final class Keys {
* The implementation version for ModLauncher.
*/
public static final Supplier<TypesafeMap.Key<String>> MLIMPL_VERSION = buildKey("mlimplVersion", String.class);
/**
* True if we can compute secured JAR state. JVMs < 8.0.61 do not have this feature because reasons
*/
public static final Supplier<TypesafeMap.Key<Boolean>> SECURED_JARS_ENABLED = buildKey("securedJarsEnabled", Boolean.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@

package cpw.mods.modlauncher.api;

import java.nio.file.Path;
import java.util.concurrent.*;

/**
* A singleton instance of this is loaded by the system to designate the launch target
*/
public interface ILaunchHandlerService {
String name();

@Deprecated(forRemoval = true, since = "10.0")
void configureTransformationClassLoader(final ITransformingClassLoaderBuilder builder);

ServiceRunner launchService(String[] arguments, ModuleLayer gameLayer);

default NamedPath[] getPaths() { return new NamedPath[0]; }
Expand Down
Loading

0 comments on commit 26f7226

Please sign in to comment.