11package org .mangorage .bootstrap ;
22
3- import org .mangorage .bootstrap .internal .JarHandler ;
4- import org .mangorage .bootstrap .internal .MangoLoader ;
3+ import org .mangorage .bootstrap .api .launch .ILaunchTarget ;
54import org .mangorage .bootstrap .internal .Util ;
6-
7- import java .io .IOException ;
5+ import org .mangorage .bootstrap .internal .impl .MangoBotLaunchTarget ;
86import java .lang .module .Configuration ;
97import java .lang .module .ModuleFinder ;
108import java .nio .file .Files ;
119import java .nio .file .Path ;
10+ import java .util .HashMap ;
1211import java .util .List ;
13-
14- import static org .mangorage .bootstrap .internal .Util .*;
12+ import java .util .Map ;
13+ import java .util .ServiceLoader ;
14+ import java .util .Set ;
1515
1616public final class Bootstrap {
1717
18- public static void main (String [] args ) throws IOException {
18+ public static void main (String [] args ) throws Throwable {
19+ if (!(args .length >= 2 )) {
20+ throw new IllegalStateException ("Need to define a launchTarget, --launchTarget mangobot" );
21+ }
22+
23+ if (!args [0 ].equals ("--launchTarget" )) {
24+ throw new IllegalStateException ("Need to have --launchTarget be defined first..." );
25+ }
26+
27+ final var launchTarget = args [1 ];
28+
1929 ModuleLayer parent = null ;
2030
2131 if (Bootstrap .class .getModule () != null ) {
@@ -24,39 +34,52 @@ public static void main(String[] args) throws IOException {
2434 parent = ModuleLayer .boot (); // We dont have a module for Bootstrap..., so assume we are using the boot layer...
2535 }
2636
27- final var librariesPath = Path .of ("libraries" );
28- final var pluginsPath = Path .of ("plugins" );
29-
30- JarHandler .safeHandle (Path .of ("libraries" ), Path .of ("sorted-libraries" ));
31-
32- List <Path > deleteFiles = List .of (
33- Path .of ("sorted-libraries" ).resolve ("okio-3.6.0.jar" )
37+ // Where additional launch targets can be defined...
38+ Path launchPath = Path .of (
39+ "launch"
3440 );
3541
36- for (Path deleteFile : deleteFiles ) {
37- Files .deleteIfExists (deleteFile );
38- }
39-
40- final var moduleCfg = Configuration .resolve (
41- ModuleFinder .of (pluginsPath ),
42- List .of (
43- parent .configuration ()
44- ),
45- ModuleFinder .of (
46- Path .of ("sorted-libraries" )
47- ),
48- Util .getModuleNames (pluginsPath )
42+ final var moduleCfg = Configuration
43+ .resolveAndBind (
44+ ModuleFinder .of (
45+ launchPath
46+ ),
47+ List .of (
48+ parent .configuration ()
49+ ),
50+ ModuleFinder .of (),
51+ Files .exists (launchPath ) ?
52+ Util .getModuleNames (
53+ launchPath
54+ ) : Set .of ()
55+ );
56+
57+ final var moduleLayerController = ModuleLayer .defineModulesWithOneLoader (
58+ moduleCfg ,
59+ List .of (parent ),
60+ Thread .currentThread ().getContextClassLoader ()
4961 );
5062
51- final var moduleCl = new MangoLoader (moduleCfg .modules (), Thread .currentThread ().getContextClassLoader ());
52-
53- final var moduleLayerController = ModuleLayer .defineModules (moduleCfg , List .of (parent ), s -> moduleCl );
54- final var moduleLayer = moduleLayerController .layer ();
55-
56- Thread .currentThread ().setContextClassLoader (moduleCl );
63+ final Map <String , ILaunchTarget > launchTargetMap = new HashMap <>();
64+ ServiceLoader .load (ILaunchTarget .class )
65+ .stream ()
66+ .forEach (provider -> {
67+ if (provider .type () != MangoBotLaunchTarget .class ) {
68+ final var target = provider .get ();
69+ launchTargetMap .put (target .getId (), target );
70+ }
71+ });
72+
73+ // Only add if we dont have any other launch targets...
74+ if (launchTargetMap .isEmpty ()) {
75+ final var defaultLaunchTarget = new MangoBotLaunchTarget ();
76+ launchTargetMap .put (defaultLaunchTarget .getId (), defaultLaunchTarget );
77+ }
5778
58- moduleCl .load ();
79+ if (!launchTargetMap .containsKey (launchTarget )) {
80+ throw new IllegalStateException ("Cant find launch target '%s'" .formatted (launchTarget ));
81+ }
5982
60- callMain ( "org.mangorage.entrypoint.MangoBotCore" , args , moduleLayer . findModule ( "org.mangorage.mangobotcore" ). get () );
83+ launchTargetMap . get ( launchTarget ). launch ( parent , args );
6184 }
6285}
0 commit comments