maybe fix things

This commit is contained in:
moehreag 2024-05-13 18:16:28 +02:00
parent c9d6f0b1f3
commit edd5f5fedc
8 changed files with 61 additions and 28 deletions

View file

@ -10,9 +10,14 @@ import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.github.moehreag.nonsense.loader.impl.mixin.NonsenseMixinService;
import lombok.Getter;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.service.IPropertyKey;
public class Launcher {
@ -23,6 +28,12 @@ public class Launcher {
@Getter
private static Launcher instance;
@Getter
private final Map<IPropertyKey, Object> globalProperties = new HashMap<>();
@Getter
private final Env env;
public static void run(String[] args, Env env) {
new Launcher(args, env);
}
@ -32,6 +43,7 @@ public class Launcher {
throw new IllegalStateException();
}
instance = this;
this.env = env;
classPath = Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator)).filter(s -> !"*".equals(s)).map(Paths::get).toList();
targetClassLoader = new MixinClassloader(classPath.stream().map(Path::toUri).map((URI uri) -> {
try {
@ -49,5 +61,8 @@ public class Launcher {
} catch (Throwable e) {
throw new RuntimeException(e);
}
System.setProperty("mixin.service", NonsenseMixinService.class.getName());
MixinBootstrap.init();
}
}

View file

@ -14,7 +14,6 @@ import io.github.moehreag.nonsense.loader.impl.plugin.NonsensePlugin;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.service.IPropertyKey;
public class LoaderImpl implements Loader {
@ -30,9 +29,6 @@ public class LoaderImpl implements Loader {
@Getter
private static LoaderImpl instance;
@Getter
private final Map<IPropertyKey, Object> globalProperties = new HashMap<>();
private final Logger LOGGER = LoggerFactory.getLogger("Nonsense Loader");
@Getter

View file

@ -1,5 +1,7 @@
package io.github.moehreag.nonsense.loader.impl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
@ -18,4 +20,14 @@ public class MixinClassloader extends URLClassLoader {
return findLoadedClass(name) != null;
}
public byte[] getClassBytes(String name) throws IOException {
String binName = name.replace(".", "/");
String path = binName.concat(".class");
try (InputStream in = getResourceAsStream(path)) {
if (in == null)
return null;
return in.readAllBytes();
}
}
}

View file

@ -1,6 +1,6 @@
package io.github.moehreag.nonsense.loader.impl.mixin;
import io.github.moehreag.nonsense.loader.impl.LoaderImpl;
import io.github.moehreag.nonsense.loader.impl.Launcher;
import org.spongepowered.asm.service.IGlobalPropertyService;
import org.spongepowered.asm.service.IPropertyKey;
@ -18,22 +18,22 @@ public class NonsenseGlobalPropertyService implements IGlobalPropertyService {
@SuppressWarnings("unchecked")
@Override
public <T> T getProperty(IPropertyKey key) {
return (T) LoaderImpl.getInstance().getGlobalProperties().get(key);
return (T) Launcher.getInstance().getGlobalProperties().get(key);
}
@Override
public void setProperty(IPropertyKey key, Object value) {
LoaderImpl.getInstance().getGlobalProperties().put(key, value);
Launcher.getInstance().getGlobalProperties().put(key, value);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getProperty(IPropertyKey key, T defaultValue) {
return (T) LoaderImpl.getInstance().getGlobalProperties().getOrDefault(key, defaultValue);
return (T) Launcher.getInstance().getGlobalProperties().getOrDefault(key, defaultValue);
}
@Override
public String getPropertyString(IPropertyKey key, String defaultValue) {
return LoaderImpl.getInstance().getGlobalProperties().getOrDefault(key, defaultValue).toString();
return Launcher.getInstance().getGlobalProperties().getOrDefault(key, defaultValue).toString();
}
}

View file

@ -3,6 +3,8 @@ package io.github.moehreag.nonsense.loader.impl.mixin;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.logging.Level;
import org.spongepowered.asm.logging.LoggerAdapterAbstract;
@ -14,9 +16,11 @@ public class NonsenseMixinLogger extends LoggerAdapterAbstract {
return LOGGERS.computeIfAbsent(name, NonsenseMixinLogger::new);
}
private final Logger log;
public NonsenseMixinLogger(String name){
super(name);
log = LoggerFactory.getLogger(name);
}
@Override
@ -26,20 +30,21 @@ public class NonsenseMixinLogger extends LoggerAdapterAbstract {
@Override
public void catching(Level level, Throwable t) {
log.atLevel(org.slf4j.event.Level.valueOf(level.name())).setCause(t).log();
}
@Override
public void log(Level level, String message, Object... params) {
log.atLevel(org.slf4j.event.Level.valueOf(level.name())).log(message, params);
}
@Override
public void log(Level level, String message, Throwable t) {
log.atLevel(org.slf4j.event.Level.valueOf(level.name())).setCause(t).log(message);
}
@Override
public <T extends Throwable> T throwing(T t) {
log.atLevel(org.slf4j.event.Level.WARN).setCause(t).log();
return t;
}
}

View file

@ -1,14 +1,16 @@
package io.github.moehreag.nonsense.loader.impl.mixin;
import io.github.moehreag.nonsense.loader.impl.Launcher;
import io.github.moehreag.nonsense.loader.impl.LoaderImpl;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.launch.platform.container.ContainerHandleVirtual;
import org.spongepowered.asm.launch.platform.container.IContainerHandle;
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.transformer.IMixinTransformer;
import org.spongepowered.asm.mixin.transformer.IMixinTransformerFactory;
import org.spongepowered.asm.service.*;
import org.spongepowered.asm.transformers.MixinClassReader;
import org.spongepowered.asm.util.ReEntranceLock;
import java.io.IOException;
@ -20,6 +22,7 @@ import java.util.List;
public class NonsenseMixinService implements IMixinService, IClassProvider, IClassBytecodeProvider, ITransformerProvider, IClassTracker {
static IMixinTransformer transformer;
private final ReEntranceLock lock = new ReEntranceLock(1);
private final ContainerHandleVirtual handle = new ContainerHandleVirtual(getName());
@Override
public String getName() {
@ -100,7 +103,7 @@ public class NonsenseMixinService implements IMixinService, IClassProvider, ICla
@Override
public IContainerHandle getPrimaryContainer() {
return null;
return handle;
}
@Override
@ -115,7 +118,7 @@ public class NonsenseMixinService implements IMixinService, IClassProvider, ICla
@Override
public String getSideName() {
return LoaderImpl.getInstance().getEnv().getMixinName();
return Launcher.getInstance().getEnv().getMixinName();
}
@Override
@ -136,12 +139,22 @@ public class NonsenseMixinService implements IMixinService, IClassProvider, ICla
@Override
public ClassNode getClassNode(String name) throws ClassNotFoundException, IOException {
return null;
return getClassNode(name, true);
}
@Override
public ClassNode getClassNode(String name, boolean runTransformers) throws ClassNotFoundException, IOException {
return null;
public ClassNode getClassNode(String name, boolean runTransformers) throws IOException {
byte[] bytes;
if (runTransformers){
bytes = transformer.transformClass(MixinEnvironment.getCurrentEnvironment(), name,
Launcher.getInstance().getTargetClassLoader().getClassBytes(name));
} else {
bytes = Launcher.getInstance().getTargetClassLoader().getClassBytes(name);
}
ClassReader reader = new MixinClassReader(bytes, name);
ClassNode node = new ClassNode();
reader.accept(node, 0);
return node;
}
@Override

View file

@ -1,8 +1,6 @@
package io.github.moehreag.nonsense.loader.impl.plugin;
import io.github.moehreag.nonsense.loader.impl.Env;
import io.github.moehreag.nonsense.loader.impl.mixin.NonsenseMixinService;
import org.spongepowered.asm.launch.MixinBootstrap;
import java.util.Collections;
import java.util.Map;
@ -20,10 +18,4 @@ public interface NonsensePlugin extends Runnable {
default void init(){
}
default void initMixin(){
System.setProperty("mixin.service", NonsenseMixinService.class.getName());
MixinBootstrap.init();
}
}

View file

@ -28,7 +28,7 @@ public class Minecraft implements NonsensePlugin {
@Override
public void init() {
initMixin();
}
private Path findGame() {