add subproject for testing

This commit is contained in:
moehreag 2024-05-19 18:34:45 +02:00
parent 31480081bf
commit 12a77d9827
5 changed files with 89 additions and 19 deletions

View file

View file

@ -1,2 +1,18 @@
pluginManagement {
repositories {
maven {
name = "Esnesnos Maven Releases"
url = uri("https://maven-esnesnon.ecorous.org/releases")
}
maven {
name = "Esnesnos Maven Snapshots"
url = uri("https://maven-esnesnon.ecorous.org/snapshots")
}
gradlePluginPortal()
}
}
rootProject.name = "nonsense-loader"
include(":minecraft")

View file

@ -20,5 +20,6 @@ public interface Loader {
Path getGameDir();
Path getConfigDir();
Path getModsDir();
boolean isDevelopment();
}

View file

@ -1,6 +1,9 @@
package org.ecorous.esnesnon.nonsense.loader.impl;
import java.io.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -17,6 +20,7 @@ import org.slf4j.LoggerFactory;
public class LoaderImpl implements Loader {
// TODO decide this
public static final String MOD_FILE_EXTENSION = ".nonsense";
private final boolean DEV_ENV = Boolean.getBoolean("nonsense.development");
@Getter
private final String[] args;
@ -70,9 +74,36 @@ public class LoaderImpl implements Loader {
}
private void discoverPlugins() {
for (NonsensePlugin plugin : ServiceLoader.load(NonsensePlugin.class, classloader)) {
plugin.init(this);
plugins.add(plugin);
List<String> classes = new ArrayList<>();
this.getClass().getClassLoader().resources("META-INF/services/"+NonsensePlugin.class.getName()).distinct().forEach(url -> {
try (InputStream inputStream = url.openStream()) {
new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(classes::add);
} catch (IOException e) {
e.printStackTrace();
// TODO error handling
throw new UncheckedIOException(e);
}
});
LOGGER.info("Found plugins: \n{}", String.join("\t\n", classes));
for (Class<?> c : classes.stream().map((String className) -> {
try {
return classloader.findClass(className);
} catch (ClassNotFoundException e) {
// TODO error handling
throw new RuntimeException(e);
}
}).filter(NonsensePlugin.class::isAssignableFrom).toList()) {
try {
MethodHandle ctor = MethodHandles.publicLookup().findConstructor(c, MethodType.methodType(void.class));
NonsensePlugin plugin = (NonsensePlugin) ctor.invoke();
plugins.add(plugin);
if (plugin.init(this)) {
break;
}
} catch (Throwable e) {
LOGGER.error("Failed to instantiate plugin: ", e);
}
}
}
@ -92,4 +123,9 @@ public class LoaderImpl implements Loader {
}
return res;
}
@Override
public boolean isDevelopment() {
return DEV_ENV;
}
}

View file

@ -1,5 +1,7 @@
package org.ecorous.esnesnon.nonsense.loader.impl.plugin.game;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.ecorous.esnesnon.nonsense.loader.impl.Discovery;
import org.ecorous.esnesnon.nonsense.loader.impl.LoaderImpl;
import org.ecorous.esnesnon.nonsense.loader.impl.plugin.NonsensePlugin;
@ -35,12 +37,11 @@ public class Minecraft implements NonsensePlugin {
public boolean init(LoaderImpl loader) {
Path gameJar = findGame();
if (gameJar == null){
LOGGER.error("Could not find game jar!");
LOGGER.error("Could not find game jar on classpath! ({})", System.getProperty("java.class.path", ""));
return false;
//throw new IllegalStateException("Could not find game jar!");
}
version = loader.getArgument("version");
remappedGamePath = loader.getGameDir().resolve(".nonsense/remappedJars").resolve(version).resolve("game-"+version+"-remapped.jar");
if (!Files.exists(remappedGamePath)){
@ -64,7 +65,7 @@ public class Minecraft implements NonsensePlugin {
}).forEach(LoaderImpl.getInstance().getClassloader()::addURL);
LOGGER.info("Found {} mods", mods.size());
if (!Files.exists(remappedGamePath)){
if (!Files.exists(remappedGamePath) && !loader.isDevelopment()){
try {
NonsenseRemapper.run(version, gameJar, remappedGamePath, true, false);
} catch (Throwable e) {
@ -82,27 +83,43 @@ public class Minecraft implements NonsensePlugin {
private Path findGame() {
LOGGER.info("Locating game..");
String jar = System.getProperty("nonsense.plugin.minecraft.gameJar");
if (jar != null){
Path p = Paths.get(jar);
if (checkLocation(p)){
return p;
}
}
for (String s : System.getProperty("java.class.path", "").split(File.pathSeparator)) {
Path p = Paths.get(s);
if (!Files.exists(p) || Files.isDirectory(p)){
continue;
}
try (FileSystem fs = FileSystems.newFileSystem(p)) {
for (String n : MINECRAFT_CLASSES) {
if (Files.exists(fs.getPath(n))){
LOGGER.info("Found game: {}", p);
foundMainClass = n.substring(0, n.length()-6).replace("/", ".");
return p;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
if (checkLocation(p)){
return p;
}
}
LOGGER.warn("Could not locate game!");
return null;
}
private boolean checkLocation(Path jar){
if (!Files.exists(jar) || Files.isDirectory(jar)){
return false;
}
try (FileSystem fs = FileSystems.newFileSystem(jar)) {
for (String n : MINECRAFT_CLASSES) {
if (Files.exists(fs.getPath(n))){
LOGGER.info("Found game: {}", jar);
foundMainClass = n.substring(0, n.length()-6).replace("/", ".");
version = new Gson().fromJson(Files.readString(fs.getPath("version.json")), JsonObject.class).get("id").getAsString();
return true;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
@Override
public void run() {
try {