Configure mod bootstrap in main thread

This commit is contained in:
IzzelAliz 2021-10-27 16:24:52 +08:00
parent 961f2d0a8a
commit b979227322
No known key found for this signature in database
GPG Key ID: EE50E123A11D8338
4 changed files with 9 additions and 7 deletions

View File

@ -69,6 +69,7 @@ dependencies {
implementation 'net.minecraftforge:forgespi:4.0.9'
gson 'com.google.code.gson:gson:2.8.8'
implementation 'org.apache.logging.log4j:log4j-api:2.14.1'
implementation 'org.apache.logging.log4j:log4j-core:2.14.1'
implementation 'org.jetbrains:annotations:19.0.0'
implementation 'org.spongepowered:mixin:0.8.3'
implementation 'org.apache.logging.log4j:log4j-jul:2.14.1'

View File

@ -3,6 +3,7 @@ package io.izzel.arclight.boot.asm;
import cpw.mods.modlauncher.api.NamedPath;
import cpw.mods.modlauncher.serviceapi.ILaunchPluginService;
import io.izzel.arclight.boot.log.ArclightI18nLogger;
import io.izzel.arclight.boot.mod.ModBootstrap;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
@ -46,6 +47,8 @@ public class ArclightImplementer implements ILaunchPluginService {
@Override
public void initializeLaunch(ITransformerLoader transformerLoader, NamedPath[] specialPaths) {
// runs after TX CL built
ModBootstrap.postRun();
this.transformerLoader = transformerLoader;
this.implementers.put("inventory", new InventoryImplementer());
this.implementers.put("switch", SwitchTableFixer.INSTANCE);

View File

@ -42,8 +42,6 @@ public class ArclightLocator_Forge implements IModLocator {
@Override
public void scanFile(IModFile file, Consumer<Path> pathConsumer) {
// runs after TX CL built
ModBootstrap.postRun();
final Function<Path, SecureJar.Status> status = p -> file.getSecureJar().verifyPath(p);
try (Stream<Path> files = Files.find(file.getSecureJar().getRootPath(), Integer.MAX_VALUE, (p, a) -> p.getNameCount() > 0 && p.getFileName().toString().endsWith(".class"))) {
file.setSecurityStatus(files.peek(pathConsumer).map(status).reduce((s1, s2) -> SecureJar.Status.values()[Math.min(s1.ordinal(), s2.ordinal())]).orElse(SecureJar.Status.INVALID));

View File

@ -26,9 +26,9 @@ import java.util.Set;
public class ModBootstrap extends AbstractBootstrap {
public static record ModBoot(Configuration configuration, Thread thread, ClassLoader parent) {}
public static record ModBoot(Configuration configuration, ClassLoader parent) {}
private static volatile ModBoot modBoot;
private static ModBoot modBoot;
static void run() {
var plugin = Launcher.INSTANCE.environment().findLaunchPlugin("arclight_implementer");
@ -46,12 +46,12 @@ public class ModBootstrap extends AbstractBootstrap {
}
@SuppressWarnings("unchecked")
static void postRun() {
public static void postRun() {
if (modBoot == null) return;
try {
var conf = modBoot.configuration();
var parent = modBoot.parent();
var classLoader = (ModuleClassLoader) modBoot.thread().getContextClassLoader();
var classLoader = (ModuleClassLoader) Thread.currentThread().getContextClassLoader();
var parentField = ModuleClassLoader.class.getDeclaredField("parentLoaders");
var parentLoaders = (Map<String, ClassLoader>) Unsafe.getObject(classLoader, Unsafe.objectFieldOffset(parentField));
for (var mod : conf.modules()) {
@ -116,7 +116,7 @@ public class ModBootstrap extends AbstractBootstrap {
var confOffset = Unsafe.objectFieldOffset(configurationField);
var oldConf = (Configuration) Unsafe.getObject(classLoader, confOffset);
var conf = oldConf.resolveAndBind(JarModuleFinder.of(secureJar), ModuleFinder.of(), List.of(secureJar.name()));
modBoot = new ModBoot(conf, Thread.currentThread(), classLoader);
modBoot = new ModBoot(conf, classLoader);
Unsafe.putObjectVolatile(classLoader, confOffset, conf);
var pkgField = ModuleClassLoader.class.getDeclaredField("packageLookup");
var packageLookup = (Map<String, ResolvedModule>) Unsafe.getObject(classLoader, Unsafe.objectFieldOffset(pkgField));