11package xyz .mrcraftteammc .grasslauncher .extension ;
22
3- import com .fasterxml .jackson .dataformat .yaml .YAMLMapper ;
43import org .slf4j .Logger ;
54import org .slf4j .LoggerFactory ;
65import xyz .mrcraftteammc .grasslauncher .common .CommonConstants ;
@@ -24,64 +23,65 @@ public final class ExtensionLoader {
2423 private static boolean Initialized = false ;
2524 private final Logger logger = LoggerFactory .getLogger ("GrassLauncher Extension Loader" );
2625 private final File file = new File (CommonConstants .EXTENSIONS_DIR );
27- private final YAMLMapper mapper = new YAMLMapper ();
28- private List <Extension > extensionList = new ArrayList <>();
26+ private final List < Extension > extensionList = new ArrayList <> ();
27+ private final List <ExtensionManifest > extensionManifestList = new ArrayList <>();
2928
3029 public ExtensionLoader () throws IOException , ClassNotFoundException , InstantiationException , IllegalAccessException {
3130 if (!Initialized ) {
3231 Initialized = true ;
33- this .extensionList = this .getExtensions ();
34- } else {
35- logger .error ("Cannot create `ExtensionLoader` instance again!" );
36- }
37- }
3832
39- public List < Extension > getExtensions () throws IOException , ClassNotFoundException , InstantiationException , IllegalAccessException {
40- if (!file .exists ()) throw new IOException ("The extension path does not exist ." );
33+ if (! file . exists ()) throw new IOException ( "The extension path does not exist." );
34+ if (!file .isDirectory ()) throw new IOException ("The extension path is not a directory ." );
4135
42- if (! file . isDirectory ()) throw new IOException ( "The extension path is not a directory." );
36+ List < File > files = new ArrayList <>( );
4337
44- List < File > files = new ArrayList <>( );
45- List < Extension > extensions = new ArrayList <>( );
38+ this . extensionList . add ( new DefaultExtension () );
39+ this . extensionList . add ( new i18nExtension () );
4640
47- extensions .add (new DefaultExtension ());
48- extensions .add (new i18nExtension ());
49-
50- for (File f : Objects .requireNonNull (file .listFiles ())) {
51- if (f .getName ().endsWith (".jar" )) {
52- files .add (f );
41+ for (File f : Objects .requireNonNull (file .listFiles ())) {
42+ if (f .getName ().endsWith (".jar" )) {
43+ files .add (f );
44+ }
5345 }
54- }
5546
56- for (File f : files ) {
57- try (JarFile jar = new JarFile (f )) {
58- Enumeration <JarEntry > entries = jar .entries ();
47+ for (File f : files ) {
48+ try (JarFile jar = new JarFile (f )) {
49+ Enumeration <JarEntry > entries = jar .entries ();
5950
60- while (entries .hasMoreElements ()) {
61- JarEntry entry = entries .nextElement ();
51+ while (entries .hasMoreElements ()) {
52+ JarEntry entry = entries .nextElement ();
6253
63- if (entry .isDirectory () || !entry .getName ().endsWith (".class" )) {
64- continue ;
65- }
66- String clz = entry .getName ()
67- .substring (0 , entry .getName ().length () - 6 )
68- .replace ('/' , '.' );
69- URLClassLoader loader = new URLClassLoader (new URL []{f .toURI ().toURL ()},
70- Thread .currentThread ().getContextClassLoader ());
71- Class <?> clazz = loader .loadClass (clz );
72-
73- if (clazz .getAnnotation (ExtensionInstance .class ) != null ) {
74- Object o = clazz .newInstance ();
75-
76- if (o instanceof Extension ) {
77- extensions .add ((Extension ) o );
54+ if (entry .isDirectory () || !entry .getName ().endsWith (".class" )) {
55+ continue ;
56+ }
57+
58+ String clz = entry .getName ()
59+ .substring (0 , entry .getName ().length () - 6 )
60+ .replace ('/' , '.' );
61+ URLClassLoader loader = new URLClassLoader (new URL []{f .toURI ().toURL ()},
62+ Thread .currentThread ().getContextClassLoader ());
63+ Class <?> clazz = loader .loadClass (clz );
64+
65+ if (clazz .getAnnotation (ExtensionInstance .class ) != null ) {
66+ Object o = clazz .newInstance ();
67+
68+ if (o instanceof Extension ) {
69+ extensionList .add ((Extension ) o );
70+ ExtensionManifest manifest = ((Extension ) o ).getManifest ();
71+
72+ if (this .extensionManifestList .isEmpty ()) {
73+ this .extensionManifestList .add (manifest );
74+ } else if (!this .extensionManifestList .contains (manifest )) {
75+ this .extensionManifestList .add (manifest );
76+ }
77+ }
7878 }
7979 }
8080 }
8181 }
82+ } else {
83+ logger .error ("Cannot create `ExtensionLoader` instance again!" );
8284 }
83-
84- return extensions ;
8585 }
8686
8787 public void loadExtensions () {
@@ -92,6 +92,8 @@ public void loadExtensions() {
9292 logger .info ("Loading Extensions..." );
9393
9494 this .extensionList .forEach (Extension ::onLoad );
95+
96+ this .extensionManifestList .forEach (System .err ::println );
9597 }
9698
9799 public void enableExtensions () {
0 commit comments