Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public void initialize(FabricLauncher launcher) {
if (!logJars.isEmpty() && !Boolean.getBoolean(SystemProperties.UNIT_TEST)) {
for (Path jar : logJars) {
if (gameJars.contains(jar)) {
launcher.addToClassPath(jar, ALLOWED_EARLY_CLASS_PREFIXES);
launcher.addToClassPathRestricted(jar, ALLOWED_EARLY_CLASS_PREFIXES);
} else {
launcher.addToClassPath(jar);
}
Expand Down Expand Up @@ -469,19 +469,30 @@ public boolean hasAwtSupport() {
}

@Override
public void unlockClassPath(FabricLauncher launcher) {
public void populateClassPath(FabricLauncher launcher) {
for (Path gameJar : gameJars) {
if (logJars.contains(gameJar)) {
launcher.setAllowedPrefixes(gameJar);
} else {
launcher.addToClassPath(gameJar);
if (!logJars.contains(gameJar)) {
launcher.addToClassPathRestricted(gameJar);
}
}

if (realmsJar != null) launcher.addToClassPath(realmsJar);
if (realmsJar != null) launcher.addToClassPathRestricted(realmsJar);

for (Path lib : miscGameLibraries) {
launcher.addToClassPathRestricted(lib);
}
}

@Override
public void unlockClassPath(FabricLauncher launcher) {
for (Path gameJar : gameJars) {
launcher.setAllPrefixesAllowed(gameJar);
}

if (realmsJar != null) launcher.setAllPrefixesAllowed(realmsJar);

for (Path lib : miscGameLibraries) {
launcher.addToClassPath(lib);
launcher.setAllPrefixesAllowed(lib);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.transformer.Proxy;

Expand Down Expand Up @@ -143,7 +142,6 @@ private void init() {
FabricLoaderImpl.INSTANCE.loadAccessWideners();

// Setup Mixin environment
MixinBootstrap.init();
FabricMixinBootstrap.init(getEnvironmentType(), FabricLoaderImpl.INSTANCE);
MixinEnvironment.getDefaultEnvironment().setSide(getEnvironmentType() == EnvType.CLIENT ? MixinEnvironment.Side.CLIENT : MixinEnvironment.Side.SERVER);

Expand All @@ -162,10 +160,9 @@ public String[] getLaunchArguments() {
}

@Override
public void addToClassPath(Path path, String... allowedPrefixes) {
public void addToClassPath(Path path) {
try {
launchClassLoader.addURL(UrlUtil.asUrl(path));
// allowedPrefixes handling is not implemented (no-op)
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Expand All @@ -176,6 +173,11 @@ public void setAllowedPrefixes(Path path, String... prefixes) {
// not implemented (no-op)
}

@Override
public void setAllPrefixesAllowed(Path path) {
// not implemented (no-op)
}

@Override
public void setValidParentClassPath(Collection<Path> paths) {
// not implemented (no-op)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum BuiltinTransform {
boolean locateGame(FabricLauncher launcher, String[] args);
void initialize(FabricLauncher launcher);
GameTransformer getEntrypointTransformer();
void populateClassPath(FabricLauncher launcher);
void unlockClassPath(FabricLauncher launcher);
void launch(ClassLoader loader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@
public interface FabricLauncher {
MappingConfiguration getMappingConfiguration();

void addToClassPath(Path path, String... allowedPrefixes);
void addToClassPath(Path path);

default void addToClassPathRestricted(Path path, String... allowedPrefixes) {
setAllowedPrefixes(path, allowedPrefixes);
addToClassPath(path);
}

void setAllowedPrefixes(Path path, String... prefixes);
void setAllPrefixesAllowed(Path path);
void setValidParentClassPath(Collection<Path> paths);

EnvType getEnvironmentType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Map;

import org.jetbrains.annotations.VisibleForTesting;
import org.spongepowered.asm.mixin.MixinEnvironment;

import net.fabricmc.loader.impl.FabricLoaderImpl;
import net.fabricmc.loader.impl.FormattedException;
Expand All @@ -36,7 +34,6 @@
public abstract class FabricLauncherBase implements FabricLauncher {
protected static final boolean IS_DEVELOPMENT = SystemProperties.isSet(SystemProperties.DEVELOPMENT);

private static boolean mixinReady;
private static Map<String, Object> properties;
private static FabricLauncher launcher;
private static MappingConfiguration mappingConfiguration = new MappingConfiguration();
Expand Down Expand Up @@ -140,25 +137,4 @@ public void uncaughtException(Thread t, Throwable e) {
}
});
}

protected static void finishMixinBootstrapping() {
if (mixinReady) {
throw new RuntimeException("Must not call FabricLauncherBase.finishMixinBootstrapping() twice!");
}

try {
Method m = MixinEnvironment.class.getDeclaredMethod("gotoPhase", MixinEnvironment.Phase.class);
m.setAccessible(true);
m.invoke(null, MixinEnvironment.Phase.INIT);
m.invoke(null, MixinEnvironment.Phase.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}

mixinReady = true;
}

public static boolean isMixinReady() {
return mixinReady;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package net.fabricmc.loader.impl.launch;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -28,6 +31,9 @@
import org.spongepowered.asm.mixin.Mixins;
import org.spongepowered.asm.mixin.extensibility.IMixinConfig;
import org.spongepowered.asm.mixin.transformer.Config;
import org.spongepowered.asm.mixin.transformer.IMixinTransformer;
import org.spongepowered.asm.service.IMixinService;
import org.spongepowered.asm.util.ReEntranceLock;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.SemanticVersion;
Expand All @@ -37,6 +43,7 @@
import net.fabricmc.loader.api.metadata.ModDependency.Kind;
import net.fabricmc.loader.api.metadata.version.VersionInterval;
import net.fabricmc.loader.impl.FabricLoaderImpl;
import net.fabricmc.loader.impl.FormattedException;
import net.fabricmc.loader.impl.ModContainerImpl;
import net.fabricmc.loader.impl.launch.knot.MixinServiceKnot;
import net.fabricmc.loader.impl.launch.knot.MixinServiceKnotBootstrap;
Expand Down Expand Up @@ -111,9 +118,46 @@ public static void init(EnvType side, FabricLoaderImpl loader) {
Log.info(LogCategory.MIXIN, "Detected old Mixin version without config decoration support");
}

try {
Method m = MixinEnvironment.class.getDeclaredMethod("gotoPhase", MixinEnvironment.Phase.class);
m.setAccessible(true);
m.invoke(null, MixinEnvironment.Phase.INIT);
m.invoke(null, MixinEnvironment.Phase.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}

initialized = true;
}

public static void configure(IMixinTransformer mixinTransformer) {
// run mixin configuration as normally done by the first invocation of org.spongepowered.asm.mixin.transformer.MixinProcessor.applyMixins

MixinEnvironment mixinEnv = MixinEnvironment.getDefaultEnvironment();
ReEntranceLock lock = null;

try {
Field serviceField = mixinEnv.getClass().getDeclaredField("service");
serviceField.setAccessible(true);
IMixinService service = (IMixinService) serviceField.get(mixinEnv);
lock = service.getReEntranceLock();
lock.push();

Field processorField = mixinTransformer.getClass().getDeclaredField("processor");
processorField.setAccessible(true);
Object processor = processorField.get(mixinTransformer);
Method checkSelectMethod = processor.getClass().getDeclaredMethod("checkSelect", MixinEnvironment.class);
checkSelectMethod.setAccessible(true);
checkSelectMethod.invoke(processor, mixinEnv);
} catch (InvocationTargetException e) {
throw new FormattedException("Mixin initialization failed", e.getCause());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
} finally {
if (lock != null) lock.pop();
}
}

private static final class MixinConfigDecorator {
private static final List<LoaderMixinVersionEntry> versions = new ArrayList<>();

Expand Down
19 changes: 13 additions & 6 deletions src/main/java/net/fabricmc/loader/impl/launch/knot/Knot.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class Knot extends FabricLauncherBase {
private EnvType envType;
private final List<Path> classPath = new ArrayList<>();
private GameProvider provider;
private boolean unlocked;
private boolean populated;

public static void launch(String[] args, EnvType type) {
setupUncaughtExceptionHandler();
Expand Down Expand Up @@ -145,12 +145,15 @@ public ClassLoader init(String[] args) {
FabricLoaderImpl.INSTANCE.loadAccessWideners();

FabricMixinBootstrap.init(getEnvironmentType(), loader);
FabricLauncherBase.finishMixinBootstrapping();

classLoader.initializeTransformers();

provider.populateClassPath(this);
populated = true;

FabricMixinBootstrap.configure(MixinServiceKnot.getTransformer());

provider.unlockClassPath(this);
unlocked = true;

try {
loader.invokeEntrypoints("preLaunch", PreLaunchEntrypoint.class, PreLaunchEntrypoint::onPreLaunch);
Expand Down Expand Up @@ -256,10 +259,9 @@ public List<Path> getClassPath() {
}

@Override
public void addToClassPath(Path path, String... allowedPrefixes) {
public void addToClassPath(Path path) {
Log.debug(LogCategory.KNOT, "Adding " + path + " to classpath.");

classLoader.setAllowedPrefixes(path, allowedPrefixes);
classLoader.addCodeSource(path);
}

Expand All @@ -268,6 +270,11 @@ public void setAllowedPrefixes(Path path, String... prefixes) {
classLoader.setAllowedPrefixes(path, prefixes);
}

@Override
public void setAllPrefixesAllowed(Path path) {
classLoader.setAllPrefixesAllowed(path);
}

@Override
public void setValidParentClassPath(Collection<Path> paths) {
classLoader.setValidParentClassPath(paths);
Expand Down Expand Up @@ -302,7 +309,7 @@ public ClassLoader getTargetClassLoader() {

@Override
public byte[] getClassByteArray(String name, boolean runTransformers) throws IOException {
if (!unlocked) throw new IllegalStateException("early getClassByteArray access");
if (!populated) throw new IllegalStateException("early getClassByteArray access");

if (runTransformers) {
return classLoader.getPreMixinClassBytes(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,13 @@ public void addCodeSource(Path path) {
public void setAllowedPrefixes(Path codeSource, String... prefixes) {
codeSource = LoaderUtil.normalizeExistingPath(codeSource);

if (prefixes.length == 0) {
allowedPrefixes.remove(codeSource);
} else {
allowedPrefixes.put(codeSource, prefixes);
}
allowedPrefixes.put(codeSource, prefixes);
}

@Override
public void setAllPrefixesAllowed(Path codeSource) {
codeSource = LoaderUtil.normalizeExistingPath(codeSource);
allowedPrefixes.remove(codeSource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static KnotClassLoaderInterface create(boolean useCompatibility, boolean isDevel

void addCodeSource(Path path);
void setAllowedPrefixes(Path codeSource, String... prefixes);
void setAllPrefixesAllowed(Path codeSource);
void setValidParentClassPath(Collection<Path> codeSources);

Manifest getManifest(Path codeSource);
Expand Down
Loading