plguin loading + logging
and some mixin work
This commit is contained in:
parent
2f03504e49
commit
00c36a746c
|
@ -1,6 +1,6 @@
|
||||||
plugins {
|
plugins {
|
||||||
id("java")
|
id("java")
|
||||||
id("io.freefair.lombok").version("8.1.+")
|
id("io.freefair.lombok").version("8.+")
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "org.example"
|
group = "org.example"
|
||||||
|
@ -14,6 +14,9 @@ repositories {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("net.fabricmc:sponge-mixin:0.13.4+mixin.0.8.5")
|
implementation("net.fabricmc:sponge-mixin:0.13.4+mixin.0.8.5")
|
||||||
|
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:3.0.0-beta2")
|
||||||
|
implementation("org.apache.logging.log4j:log4j-api:3.0.0-beta2")
|
||||||
|
implementation("org.apache.logging.log4j:log4j-core:3.0.0-beta2")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.test {
|
tasks.test {
|
||||||
|
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
|
@ -1,5 +1,5 @@
|
||||||
#Sat May 11 16:50:23 CEST 2024
|
#Sat May 11 16:50:23 CEST 2024
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|
|
@ -5,52 +5,60 @@ import java.lang.invoke.MethodHandle;
|
||||||
import java.lang.invoke.MethodHandles;
|
import java.lang.invoke.MethodHandles;
|
||||||
import java.lang.invoke.MethodType;
|
import java.lang.invoke.MethodType;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import io.github.moehreag.nonsense.loader.api.Loader;
|
import io.github.moehreag.nonsense.loader.api.Loader;
|
||||||
import io.github.moehreag.nonsense.loader.impl.plugin.NonsensePlugin;
|
import io.github.moehreag.nonsense.loader.impl.plugin.NonsensePlugin;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.spongepowered.asm.launch.MixinBootstrap;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.spongepowered.asm.service.IPropertyKey;
|
||||||
|
|
||||||
public class LoaderImpl implements Loader {
|
public class LoaderImpl implements Loader {
|
||||||
|
|
||||||
private static final String PLUGIN_SERVICE_FILE_LOCATION = "META-INF/nonsense.plugin.service";
|
private static final String PLUGIN_SERVICE_FILE_LOCATION = "META-INF/services/io.github.moehreag.nonsense.loader.impl.plugin.NonsensePlugin";
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final String[] args;
|
private final String[] args;
|
||||||
@Getter
|
@Getter
|
||||||
private final Env env;
|
private final Env env;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private static LoaderImpl instance;
|
private static LoaderImpl instance;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final List<NonsensePlugin> plugins = new ArrayList<>();
|
private final Map<IPropertyKey, Object> globalProperties = new HashMap<>();
|
||||||
|
|
||||||
public LoaderImpl(String[] args, Env env) {
|
private Logger LOGGER = LoggerFactory.getLogger("Nonsense Loader");
|
||||||
this.args = args;
|
|
||||||
this.env = env;
|
@Getter
|
||||||
|
private final List<NonsensePlugin> plugins = new ArrayList<>();
|
||||||
|
|
||||||
|
public LoaderImpl(String[] args, Env env) {
|
||||||
|
this.args = args;
|
||||||
|
this.env = env;
|
||||||
|
|
||||||
MixinBootstrap.init();
|
|
||||||
|
|
||||||
discoverPlugins();
|
discoverPlugins();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void run(String[] args, Env env) {
|
public static void run(String[] args, Env env) {
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
throw new IllegalStateException("Loader was started multiple times!");
|
throw new IllegalStateException("Loader was started multiple times!");
|
||||||
}
|
}
|
||||||
|
|
||||||
instance = new LoaderImpl(args, env);
|
instance = new LoaderImpl(args, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void discoverPlugins(){
|
private void discoverPlugins() {
|
||||||
List<String> classes = new ArrayList<>();
|
List<String> classes = new ArrayList<>();
|
||||||
this.getClass().getClassLoader().resources(PLUGIN_SERVICE_FILE_LOCATION).forEach(url -> {
|
this.getClass().getClassLoader().resources(PLUGIN_SERVICE_FILE_LOCATION).forEach(url -> {
|
||||||
try (InputStream inputStream = url.openStream()) {
|
try (InputStream inputStream = url.openStream()) {
|
||||||
new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(classes::add);
|
new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(classes::add);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO error handling
|
// TODO error handling
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -59,20 +67,19 @@ public class LoaderImpl implements Loader {
|
||||||
try {
|
try {
|
||||||
return Class.forName(className);
|
return Class.forName(className);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
// TODO error handling
|
// TODO error handling
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}).filter(c -> c.isAssignableFrom(NonsensePlugin.class)).forEach(c -> {
|
}).filter(NonsensePlugin.class::isAssignableFrom).forEach(c -> {
|
||||||
try {
|
try {
|
||||||
MethodHandle ctor = MethodHandles.publicLookup().findConstructor(c, MethodType.methodType(void.class));
|
MethodHandle ctor = MethodHandles.publicLookup().findConstructor(c, MethodType.methodType(void.class));
|
||||||
NonsensePlugin plugin = (NonsensePlugin) ctor.invoke();
|
NonsensePlugin plugin = (NonsensePlugin) ctor.invoke();
|
||||||
plugins.add(plugin);
|
plugins.add(plugin);
|
||||||
plugin.init();
|
plugin.init();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
throw new RuntimeException(e);
|
e.printStackTrace();
|
||||||
|
LOGGER.error("Failed to instantiate plugin: ", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package io.github.moehreag.nonsense.loader.impl.mixin;
|
||||||
|
|
||||||
|
import io.github.moehreag.nonsense.loader.impl.LoaderImpl;
|
||||||
|
import org.spongepowered.asm.service.IGlobalPropertyService;
|
||||||
|
import org.spongepowered.asm.service.IPropertyKey;
|
||||||
|
|
||||||
|
public class NonsenseGlobalPropertyService implements IGlobalPropertyService {
|
||||||
|
@Override
|
||||||
|
public IPropertyKey resolveKey(String name) {
|
||||||
|
return new IPropertyKey() {
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public <T> T getProperty(IPropertyKey key) {
|
||||||
|
return (T) LoaderImpl.getInstance().getGlobalProperties().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setProperty(IPropertyKey key, Object value) {
|
||||||
|
LoaderImpl.getInstance().getGlobalProperties().put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public <T> T getProperty(IPropertyKey key, T defaultValue) {
|
||||||
|
return (T) LoaderImpl.getInstance().getGlobalProperties().getOrDefault(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPropertyString(IPropertyKey key, String defaultValue) {
|
||||||
|
return LoaderImpl.getInstance().getGlobalProperties().getOrDefault(key, defaultValue).toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
package io.github.moehreag.nonsense.loader.impl.mixin;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import org.spongepowered.asm.logging.ILogger;
|
||||||
|
import org.spongepowered.asm.logging.Level;
|
||||||
|
import org.spongepowered.asm.logging.LoggerAdapterAbstract;
|
||||||
|
|
||||||
|
public class NonsenseMixinLogger extends LoggerAdapterAbstract {
|
||||||
|
private static final Map<String, NonsenseMixinLogger> LOGGERS = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public static ILogger get(String name){
|
||||||
|
return LOGGERS.computeIfAbsent(name, NonsenseMixinLogger::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NonsenseMixinLogger(String name){
|
||||||
|
super(name);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return "Nonsense Loader/Mixin Logger";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void catching(Level level, Throwable t) {
|
||||||
|
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void log(Level level, String message, Object... params) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void log(Level level, String message, Throwable t) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Throwable> T throwing(T t) {
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +1,23 @@
|
||||||
package io.github.moehreag.nonsense.loader.impl.mixin;
|
package io.github.moehreag.nonsense.loader.impl.mixin;
|
||||||
|
|
||||||
import io.github.moehreag.nonsense.loader.impl.LoaderImpl;
|
import io.github.moehreag.nonsense.loader.impl.LoaderImpl;
|
||||||
|
import org.objectweb.asm.tree.ClassNode;
|
||||||
import org.spongepowered.asm.launch.platform.container.IContainerHandle;
|
import org.spongepowered.asm.launch.platform.container.IContainerHandle;
|
||||||
import org.spongepowered.asm.logging.ILogger;
|
import org.spongepowered.asm.logging.ILogger;
|
||||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||||
|
import org.spongepowered.asm.mixin.transformer.IMixinTransformer;
|
||||||
import org.spongepowered.asm.service.*;
|
import org.spongepowered.asm.service.*;
|
||||||
import org.spongepowered.asm.util.ReEntranceLock;
|
import org.spongepowered.asm.util.ReEntranceLock;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class NonsenseMixinService implements IMixinService {
|
public class NonsenseMixinService implements IMixinService, IClassProvider, IClassBytecodeProvider, ITransformerProvider, IClassTracker {
|
||||||
private final ReEntranceLock lock = new ReEntranceLock(2);
|
static IMixinTransformer transformer;
|
||||||
|
private final ReEntranceLock lock = new ReEntranceLock(1);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -61,22 +66,22 @@ public class NonsenseMixinService implements IMixinService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IClassProvider getClassProvider() {
|
public IClassProvider getClassProvider() {
|
||||||
return null;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IClassBytecodeProvider getBytecodeProvider() {
|
public IClassBytecodeProvider getBytecodeProvider() {
|
||||||
return null;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ITransformerProvider getTransformerProvider() {
|
public ITransformerProvider getTransformerProvider() {
|
||||||
return null;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IClassTracker getClassTracker() {
|
public IClassTracker getClassTracker() {
|
||||||
return null;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -121,6 +126,67 @@ public class NonsenseMixinService implements IMixinService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ILogger getLogger(String name) {
|
public ILogger getLogger(String name) {
|
||||||
|
return NonsenseMixinLogger.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClassNode getClassNode(String name) throws ClassNotFoundException, IOException {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClassNode getClassNode(String name, boolean runTransformers) throws ClassNotFoundException, IOException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URL[] getClassPath() {
|
||||||
|
return new URL[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> findClass(String name) throws ClassNotFoundException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> findClass(String name, boolean initialize) throws ClassNotFoundException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> findAgentClass(String name, boolean initialize) throws ClassNotFoundException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerInvalidClass(String className) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isClassLoaded(String className) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClassRestrictions(String className) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<ITransformer> getTransformers() {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<ITransformer> getDelegatedTransformers() {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addTransformerExclusion(String name) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package io.github.moehreag.nonsense.loader.impl.plugin;
|
package io.github.moehreag.nonsense.loader.impl.plugin;
|
||||||
|
|
||||||
import io.github.moehreag.nonsense.loader.impl.Env;
|
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.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -18,4 +20,10 @@ public interface NonsensePlugin extends Runnable {
|
||||||
default void init(){
|
default void init(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default void initMixin(){
|
||||||
|
System.setProperty("mixin.service", NonsenseMixinService.class.getName());
|
||||||
|
|
||||||
|
MixinBootstrap.init();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,4 +14,9 @@ public class Minecraft implements NonsensePlugin {
|
||||||
Env.SERVER, "net.minecraft.server.main.Main"
|
Env.SERVER, "net.minecraft.server.main.Main"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
initMixin();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
io.github.moehreag.nonsense.loader.impl.mixin.NonsenseGlobalPropertyService
|
Loading…
Reference in a new issue