Skip to content

Commit bdd4aaf

Browse files
committed
Updated to be better!
1 parent 5c75e76 commit bdd4aaf

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/main/java/org/mangorage/bootstrap/LibraryHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
import java.util.Comparator;
1212
import java.util.HashMap;
1313
import java.util.Map;
14+
import java.util.Set;
1415
import java.util.jar.JarFile;
1516
import java.util.zip.ZipEntry;
1617

1718
public class LibraryHandler {
1819

19-
public static void handle() throws IOException {
20+
public static Set<String> handle() throws IOException {
2021
Path source = Paths.get("libraries");
2122
Path target = Paths.get("sortedLibraries");
2223

@@ -48,6 +49,8 @@ public static void handle() throws IOException {
4849
}
4950

5051
System.out.println("Finished deduplicating modules. Result at: " + target);
52+
53+
return seenModules.keySet();
5154
}
5255

5356
private static void deleteDirectory(Path dir) throws IOException {

src/main/java/org/mangorage/bootstrap/Test.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,35 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.lang.module.Configuration;
6+
import java.lang.module.ModuleDescriptor;
67
import java.lang.module.ModuleFinder;
8+
import java.lang.module.ModuleReference;
79
import java.lang.reflect.Method;
810
import java.net.URLClassLoader;
11+
import java.nio.file.Files;
912
import java.nio.file.Path;
10-
import java.nio.file.Paths;
13+
import java.util.HashSet;
14+
import java.util.Optional;
1115
import java.util.Set;
16+
import java.util.stream.Collectors;
1217

1318
import static org.mangorage.bootstrap.Bootstrap.fetchJars;
1419

1520
public final class Test {
1621
public static void main(String[] args) throws IOException {
1722
try {
1823
System.out.println("you have 15 seconds!");
19-
Thread.sleep(15_000);
24+
2025

2126
} catch (Throwable ignored) {}
2227

23-
LibraryHandler.handle();
28+
final var list = LibraryHandler.handle();
2429

2530
Path libsPath = Path.of("sortedLibraries");
2631
Path pluginsPath = Path.of("plugins");
2732

33+
Files.deleteIfExists(libsPath.resolve("okio-jvm-3.6.0.jar"));
34+
2835
ModuleFinder plugins = ModuleFinder.of(pluginsPath);
2936

3037
ModuleFinder finder = ModuleFinder.of(libsPath, pluginsPath);
@@ -34,7 +41,7 @@ public static void main(String[] args) throws IOException {
3441
.resolveAndBind(
3542
finder,
3643
ModuleFinder.of(),
37-
Set.of()
44+
getModuleNames(pluginsPath)
3845
);
3946

4047

@@ -46,6 +53,33 @@ public static void main(String[] args) throws IOException {
4653
callMain("org.mangorage.entrypoint.MangoBotCore", args, cl, layer);
4754
}
4855

56+
public static Set<String> getModuleNames(Path folder) {
57+
Set<String> moduleNames = new HashSet<>();
58+
59+
if (folder == null || !folder.toFile().isDirectory()) {
60+
throw new IllegalArgumentException("That's not a valid folder, genius: " + folder);
61+
}
62+
63+
File[] jarFiles = folder.toFile().listFiles((dir, name) -> name.endsWith(".jar"));
64+
if (jarFiles == null) return moduleNames;
65+
66+
for (File jar : jarFiles) {
67+
try {
68+
ModuleFinder finder = ModuleFinder.of(jar.toPath());
69+
Set<ModuleReference> modules = finder.findAll();
70+
71+
for (ModuleReference moduleRef : modules) {
72+
var descriptor = moduleRef.descriptor();
73+
moduleNames.add(descriptor.name());
74+
}
75+
} catch (Exception e) {
76+
System.err.println("Couldn't process " + jar.getName() + ": " + e.getMessage());
77+
}
78+
}
79+
80+
return moduleNames;
81+
}
82+
4983
public static void callMain(String className, String[] args, ClassLoader classLoader, ModuleLayer moduleLayer) {
5084
try {
5185
Class<?> clazz = Class.forName(moduleLayer.findModule("org.mangorage.mangobotcore").get(), className);

0 commit comments

Comments
 (0)