2222import org .byteskript .skript .error .ScriptRuntimeError ;
2323import org .byteskript .skript .runtime .internal .*;
2424import org .byteskript .skript .runtime .threading .*;
25+ import org .byteskript .skript .runtime .type .Converter ;
2526
2627import java .io .*;
2728import java .nio .charset .StandardCharsets ;
@@ -80,6 +81,8 @@ public final class Skript {
8081 final WeakList <ScriptClassLoader > loaders = new WeakList <>();
8182 @ Ignore
8283 final List <Script > scripts = new ArrayList <>(); // the only strong reference, be careful!
84+ @ Ignore
85+ final Map <Converter .Data , Converter <?, ?>> converters ;
8386
8487 @ Description ("""
8588 Create a Skript runtime with a custom (non-default) Skript compiler.
@@ -106,12 +109,16 @@ public Skript(SkriptThreadProvider threadProvider, ModifiableCompiler compiler,
106109 this .compiler = compiler ;
107110 this .factory = threadProvider ;
108111 this .factory .setSkriptInstance (this );
109- executor = Executors .newCachedThreadPool (factory );
112+ this . executor = Executors .newCachedThreadPool (factory );
110113 this .mainThread = main ;
111114 this .scheduler = new ScheduledThreadPoolExecutor (4 , factory );
112115 this .processes = new ArrayList <>();
113116 this .events = new HashMap <>();
117+ this .converters = new HashMap <>();
114118 skript = this ;
119+ if (compiler != null ) {
120+ this .converters .putAll (compiler .getConverters ());
121+ }
115122 }
116123
117124 @ Description ("""
@@ -186,12 +193,84 @@ public static Skript localInstance() {
186193 return thread .skript ;
187194 }
188195
196+ private static String createClassName (String name , String path ) {
197+ final int index = name .lastIndexOf ('.' );
198+ if (path .startsWith (File .separator )) path = path .substring (1 );
199+ if (index == -1 ) return path .replace (File .separatorChar , '.' );
200+ return path .substring (0 , index ).replace (File .separatorChar , '.' );
201+ }
202+
203+ private static List <File > getFiles (List <File > files , Path root ) {
204+ try (DirectoryStream <Path > stream = Files .newDirectoryStream (root )) {
205+ for (Path path : stream ) {
206+ if (path .toFile ().isDirectory ()) {
207+ getFiles (files , path );
208+ } else {
209+ if (!path .toFile ().getName ().endsWith (".bsk" )) continue ;
210+ files .add (path .toAbsolutePath ().toFile ());
211+ }
212+ }
213+ } catch (IOException e ) {
214+ e .printStackTrace ();
215+ }
216+ return files ;
217+ }
218+
219+ @ Deprecated
220+ public static Class <?> findAnyClass (String name ) {
221+ {
222+ final Class <?> found = getClass (name , Skript .class );
223+ if (found != null ) return found ;
224+ }
225+ final Skript skript = findInstance ();
226+ {
227+ final Class <?> found = skript .getClass (name );
228+ if (found != null ) return found ;
229+ }
230+ {
231+ for (final Script script : skript .scripts ) {
232+ for (final Class <?> type : script .classes ()) {
233+ if (type .getName ().equals (name )) return type ;
234+ }
235+ }
236+ for (final Script script : skript .scripts ) {
237+ for (final Class <?> type : script .classes ()) {
238+ if (type .getSimpleName ().equals (name )) return type ;
239+ }
240+ }
241+ }
242+ return null ;
243+ }
244+
245+ private static Class <?> getClass (String name , Class <?> owner ) {
246+ try {
247+ return Class .forName (name , false , owner .getClassLoader ());
248+ } catch (ClassNotFoundException ex ) {
249+ return null ;
250+ }
251+ }
252+
189253 private static Skript findInstance () {
190254 final Thread current = Thread .currentThread ();
191255 if (!(current instanceof ScriptThread thread )) return currentInstance ();
192256 return thread .skript ;
193257 }
194258
259+ @ Description ("""
260+ This searches the app class loader and all library class loaders for the given class.
261+ This does not search script class loaders.
262+ """ )
263+ @ GenerateExample
264+ public Class <?> getClass (String name ) {
265+ final Class <?> found = getClass (name , Skript .class );
266+ if (found != null ) return found ;
267+ for (Library library : compiler .getLibraries ()) {
268+ final Class <?> test = getClass (name , library .getClass ());
269+ if (test != null ) return test ;
270+ }
271+ return null ;
272+ }
273+
195274 @ Description ("""
196275 This returns the most recently-created Skript runtime.
197276 It is designed to be an entry-point for programs that attach in an unusual way, and have
@@ -205,27 +284,19 @@ public static Skript currentInstance() {
205284 return skript ;
206285 }
207286
208- private static String createClassName (String name , String path ) {
209- final int index = name .lastIndexOf ('.' );
210- if (path .startsWith (File .separator )) path = path .substring (1 );
211- if (index == -1 ) return path .replace (File .separatorChar , '.' );
212- return path .substring (0 , index ).replace (File .separatorChar , '.' );
287+ public <From , To > void registerConverter (Class <From > from , Class <To > to , Converter <From , To > converter ) {
288+ final Converter .Data data = new Converter .Data (from , to );
289+ this .converters .put (data , converter );
213290 }
214291
215- private static List <File > getFiles (List <File > files , Path root ) {
216- try (DirectoryStream <Path > stream = Files .newDirectoryStream (root )) {
217- for (Path path : stream ) {
218- if (path .toFile ().isDirectory ()) {
219- getFiles (files , path );
220- } else {
221- if (!path .toFile ().getName ().endsWith (".bsk" )) continue ;
222- files .add (path .toAbsolutePath ().toFile ());
223- }
224- }
225- } catch (IOException e ) {
226- e .printStackTrace ();
292+ public <From , To > Converter <From , To > getConverter (Class <From > from , Class <To > to ) {
293+ final Converter .Data data = new Converter .Data (from , to );
294+ if (converters .containsKey (data )) return (Converter <From , To >) converters .get (data );
295+ for (final Converter .Data found : converters .keySet ()) {
296+ if (from .isAssignableFrom (found .from ()) && to .isAssignableFrom (found .to ()))
297+ return (Converter <From , To >) converters .get (data );
227298 }
228- return files ;
299+ return null ;
229300 }
230301
231302 @ Description ("""
@@ -397,29 +468,6 @@ public void registerEventHandler(final Class<? extends Event> event, final Scrip
397468 this .events .get (event ).add (runner );
398469 }
399470
400- @ Description ("""
401- This searches the app class loader and all library class loaders for the given class.
402- This does not search script class loaders.
403- """ )
404- @ GenerateExample
405- public Class <?> getClass (String name ) {
406- final Class <?> found = getClass (name , Skript .class );
407- if (found != null ) return found ;
408- for (Library library : compiler .getLibraries ()) {
409- final Class <?> test = getClass (name , library .getClass ());
410- if (test != null ) return test ;
411- }
412- return null ;
413- }
414-
415- private static Class <?> getClass (String name , Class <?> owner ) {
416- try {
417- return Class .forName (name , false , owner .getClassLoader ());
418- } catch (ClassNotFoundException ex ) {
419- return null ;
420- }
421- }
422-
423471 @ Description ("""
424472 Registers a single class library, typically compiled from a script to load
425473 custom syntax.
0 commit comments