@@ -41,8 +41,8 @@ public final class Skript {
41
41
42
42
public static final ThreadGroup THREAD_GROUP = new ThreadGroup ("skript" );
43
43
public static final int JAVA_VERSION = 61 ;
44
- public static final RuntimeClassLoader LOADER = new RuntimeClassLoader (Skript .class .getClassLoader ());
45
44
static final GlobalVariableMap VARIABLES = new GlobalVariableMap ();
45
+ private static final RuntimeClassLoader LOADER = new RuntimeClassLoader (Skript .class .getClassLoader ());
46
46
private static Skript skript ;
47
47
final ExecutorService executor ;
48
48
final SkriptThreadProvider factory ;
@@ -51,12 +51,10 @@ public final class Skript {
51
51
final ModifiableCompiler compiler ;
52
52
final List <OperationController > processes ;
53
53
final Map <Class <? extends Event >, EventHandler > events ;
54
- final SkriptMirror mirror = new SkriptMirror (LOADER );
54
+ final RuntimeClassLoader parent = new RuntimeClassLoader (LOADER );
55
55
final WeakList <ScriptClassLoader > loaders = new WeakList <>();
56
56
final List <Script > scripts = new ArrayList <>(); // the only strong reference, be careful!
57
57
58
- //region Class Loaders
59
-
60
58
/**
61
59
* Create a Skript runtime with a custom (non-default) Skript compiler.
62
60
* This is used by the Jar-form script loader, which has no compiler.
@@ -101,6 +99,47 @@ public Skript() {
101
99
this (new SkriptThreadProvider (), SkriptCompiler .createBasic (), Thread .currentThread ());
102
100
}
103
101
102
+ /**
103
+ * Gets the parent class-loader local to this thread.
104
+ * This is only usable from a script thread.
105
+ *
106
+ * @return the local runtime loader
107
+ */
108
+ @ ThreadSpecific
109
+ public static RuntimeClassLoader localLoader () {
110
+ final Thread current = Thread .currentThread ();
111
+ if (!(current instanceof ScriptThread thread ))
112
+ throw new ScriptRuntimeError ("Not running on a script thread." );
113
+ return thread .skript .parent ;
114
+ }
115
+
116
+ /**
117
+ * Finds an arbitrary parent class-loader.
118
+ * This will use the most recently-created Skript runtime.
119
+ * This is unsafe, since it is unlikely to be the required loader.
120
+ *
121
+ * @return potentially null class-loader
122
+ */
123
+ @ Deprecated
124
+ public static RuntimeClassLoader currentLoader () {
125
+ return skript .parent ;
126
+ }
127
+
128
+ /**
129
+ * Attempts to find the parent class-loader.
130
+ * This will look for a local loader but default to the most-recently-created.
131
+ * This is designed for internal use.
132
+ *
133
+ * @return an arbitrary class-loader
134
+ */
135
+ @ ThreadSpecific
136
+ public static RuntimeClassLoader findLoader () {
137
+ final Thread current = Thread .currentThread ();
138
+ if (current instanceof ScriptThread thread )
139
+ return thread .skript .parent ;
140
+ return skript .parent ;
141
+ }
142
+
104
143
/**
105
144
* This is the map of global `{!var}` variables.
106
145
* This is a modifiable and atomic map.
@@ -114,7 +153,6 @@ public Skript() {
114
153
public static GlobalVariableMap getVariables () {
115
154
return VARIABLES ;
116
155
}
117
- //endregion
118
156
119
157
/**
120
158
* This returns the Skript instance that launched the current thread.
@@ -157,8 +195,6 @@ private static String createClassName(String name, String path) {
157
195
return path .substring (0 , index ).replace (File .separatorChar , '.' );
158
196
}
159
197
160
- //region Runtime Instances
161
-
162
198
private static List <File > getFiles (List <File > files , Path root ) {
163
199
try (DirectoryStream <Path > stream = Files .newDirectoryStream (root )) {
164
200
for (Path path : stream ) {
@@ -175,6 +211,16 @@ private static List<File> getFiles(List<File> files, Path root) {
175
211
return files ;
176
212
}
177
213
214
+ /**
215
+ * Gets the parent class-loader attached to this Skript runtime.
216
+ * This is used to search available libraries and scripts for classes.
217
+ *
218
+ * @return this runtime loader
219
+ */
220
+ public RuntimeClassLoader getLoader () {
221
+ return parent ;
222
+ }
223
+
178
224
/**
179
225
* Submits this instruction to a background thread.
180
226
* Background threads are safe for blocking.
@@ -195,9 +241,6 @@ public void runOnAsyncThread(final Instruction<?> runnable) {
195
241
public Future <?> getOnAsyncThread (final Instruction <?> runnable ) {
196
242
return executor .submit (runnable ::get );
197
243
}
198
- //endregion
199
-
200
- //region Thread Control
201
244
202
245
/**
203
246
* Submits this instruction to a background thread.
@@ -232,9 +275,6 @@ public Script[] getScripts() {
232
275
public Collection <OperationController > getProcesses () {
233
276
return processes ;
234
277
}
235
- //endregion
236
-
237
- //region Script Control
238
278
239
279
/**
240
280
* Runs a script with a completing future.
@@ -353,9 +393,6 @@ private static Class<?> getClass(String name, Class<?> owner) {
353
393
return null ;
354
394
}
355
395
}
356
- //endregion
357
-
358
- //region Libraries
359
396
360
397
/**
361
398
* Registers a single class library, typically compiled from a script to load
@@ -378,7 +415,6 @@ public Class<?> defineClass(String name, byte[] bytes) {
378
415
compiler .addLibrary (library );
379
416
}
380
417
381
- //region File Utilities
382
418
private static String getClassName (InputStream is )
383
419
throws IOException {
384
420
final DataInputStream stream = new DataInputStream (is );
@@ -428,9 +464,6 @@ public boolean unregisterLibrary(Library library) {
428
464
public ModifiableCompiler getCompiler () {
429
465
return compiler ;
430
466
}
431
- //endregion
432
-
433
- //region Script Compiling
434
467
435
468
/**
436
469
* Returns an array of all registered libraries.
@@ -598,9 +631,6 @@ public void unloadScript(Class<?> main) {
598
631
if (script .mainClass () == main ) this .unloadScript (script );
599
632
}
600
633
}
601
- //endregion
602
-
603
- //region Script Loading
604
634
605
635
/**
606
636
* Unloads a script. This is a destructive operation.
@@ -845,9 +875,6 @@ public Script loadScript(final InputStream stream, final String name)
845
875
throws IOException {
846
876
return loadScript (this .loadClass (name , stream .readAllBytes ()));
847
877
}
848
- //endregion
849
-
850
- //region Timings
851
878
852
879
/**
853
880
* Loads a script from its compiled class file.
@@ -904,7 +931,6 @@ public ScheduledExecutorService getScheduler() {
904
931
public ExecutorService getExecutor () {
905
932
return executor ;
906
933
}
907
- //endregion
908
934
909
935
/**
910
936
* Schedules a task to be run at some point in the future.
@@ -919,7 +945,7 @@ public Future<?> schedule(Runnable runnable, long millis) {
919
945
920
946
/**
921
947
* This class handles the class-loading delegation for libraries and scripts.
922
- * It should not be interacted with directly, see {@link Skript#LOADER } for an instance.
948
+ * It should not be interacted with directly, see {@link Skript#getLoader() } for an instance.
923
949
*/
924
950
public static class RuntimeClassLoader extends ClassLoader implements ClassProvider {
925
951
protected RuntimeClassLoader (ClassLoader parent ) {
@@ -987,6 +1013,5 @@ public Class<?> loadClass(String name, byte[] bytecode) {
987
1013
return super .loadClass (name , bytecode );
988
1014
}
989
1015
}
990
- //endregion
991
1016
992
1017
}
0 commit comments