Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant locators and clean-up leftover pre-modules code #118

Merged
merged 4 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
5 changes: 1 addition & 4 deletions src/main/java/cpw/mods/modlauncher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.logging.log4j.LogManager;
import cpw.mods.modlauncher.serviceapi.ILaunchPluginService;

import java.nio.file.Path;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -57,7 +56,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 @@ -103,8 +101,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 Down Expand Up @@ -55,9 +53,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 @@ -113,10 +113,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