22
33import ch .njol .skript .ScriptLoader ;
44import ch .njol .skript .Skript ;
5- import ch .njol .skript .classes .ClassInfo ;
65import ch .njol .skript .command .ScriptCommand ;
76import ch .njol .skript .hooks .VaultHook ;
87import ch .njol .skript .hooks .regions .RegionsPlugin ;
98import ch .njol .skript .lang .SkriptEvent ;
109import ch .njol .skript .lang .SkriptEventInfo ;
1110import ch .njol .skript .lang .function .Signature ;
12- import ch .njol .skript .log .RedirectingLogHandler ;
13- import ch .njol .skript .structures .*;
11+ import ch .njol .skript .structures .StructCommand ;
12+ import ch .njol .skript .structures .StructEvent ;
13+ import ch .njol .skript .structures .StructFunction ;
14+ import ch .njol .skript .structures .StructOptions ;
1415import me .glicz .skanalyzer .AnalyzerFlag ;
15- import me .glicz .skanalyzer .ScriptAnalyzeResult ;
1616import me .glicz .skanalyzer .SkAnalyzer ;
17- import me .glicz .skanalyzer .bridge .util .ReflectionUtil ;
17+ import me .glicz .skanalyzer .bridge .log .CachingLogHandler ;
18+ import me .glicz .skanalyzer .bridge .util .FilesUtil ;
19+ import me .glicz .skanalyzer .result .ScriptAnalyzeResult ;
20+ import me .glicz .skanalyzer .result .ScriptAnalyzeResults ;
1821import me .glicz .skanalyzer .structure .ScriptStructure ;
1922import me .glicz .skanalyzer .structure .data .CommandData ;
2023import me .glicz .skanalyzer .structure .data .EventData ;
2730import java .nio .file .InvalidPathException ;
2831import java .util .*;
2932import java .util .concurrent .CompletableFuture ;
33+ import java .util .concurrent .Executor ;
34+ import java .util .function .Function ;
3035import java .util .stream .Collectors ;
3136
3237public class MockSkriptBridgeImpl extends MockSkriptBridge {
38+ private Executor mainThreadExecutor ;
39+
3340 public MockSkriptBridgeImpl (SkAnalyzer skAnalyzer ) {
3441 super (skAnalyzer );
3542 }
@@ -39,6 +46,11 @@ public void onLoad() {
3946 parseFlags ();
4047 }
4148
49+ @ Override
50+ public void onEnable () {
51+ mainThreadExecutor = getServer ().getScheduler ().getMainThreadExecutor (this );
52+ }
53+
4254 public void parseFlags () {
4355 if (skAnalyzer .getFlags ().contains (AnalyzerFlag .FORCE_VAULT_HOOK )) {
4456 try {
@@ -63,23 +75,68 @@ public void parseFlags() {
6375 }
6476
6577 @ Override
66- public CompletableFuture <ScriptAnalyzeResult > parseScript (String path ) {
78+ public CompletableFuture <ScriptAnalyzeResults > parseScript (String path , boolean load ) {
6779 File file = new File (path );
68- if (!file .exists () || !file .getName ().endsWith (".sk" )) {
80+ if (!file .exists () || ( !file .getName ().endsWith (".sk" ) && !( file . isDirectory () && load ) )) {
6981 skAnalyzer .getLogger ().error ("Invalid file path" );
7082 return CompletableFuture .failedFuture (new InvalidPathException (path , "Provided file doesn't end with '.sk'" ));
7183 }
72- AnalyzerCommandSender sender = new AnalyzerCommandSender (skAnalyzer );
73- RedirectingLogHandler logHandler = new RedirectingLogHandler (sender , null ).start ();
74- return ScriptLoader .loadScripts (file , logHandler , false )
75- .handle ((info , throwable ) -> {
76- if (throwable != null ) {
77- skAnalyzer .getLogger ().error ("Something went wrong while trying to parse '%s'" .formatted (path ), throwable );
78- return CompletableFuture .failedFuture (new RuntimeException (throwable ));
79- }
80- return CompletableFuture .completedFuture (info );
81- })
82- .thenApply (info -> sender .finish (file , handleParsedScript (file )));
84+
85+ Set <File > files = FilesUtil .listScripts (file );
86+ return CompletableFuture .supplyAsync (
87+ () -> {
88+ CachingLogHandler logHandler = new CachingLogHandler ().start ();
89+ return ScriptLoader .loadScripts (files , logHandler )
90+ .handle ((info , throwable ) -> {
91+ if (throwable != null ) {
92+ skAnalyzer .getLogger ().error ("Something went wrong while trying to parse '%s'" .formatted (path ), throwable );
93+ throw new RuntimeException (throwable );
94+ }
95+ return info ;
96+ })
97+ .thenApply (info -> {
98+ ScriptAnalyzeResults results = new ScriptAnalyzeResults (buildAnalyzeResults (files , logHandler ));
99+ if (!load ) {
100+ unloadScript (path );
101+ }
102+ return results ;
103+ })
104+ .join ();
105+ },
106+ mainThreadExecutor
107+ );
108+ }
109+
110+ @ Override
111+ public boolean unloadScript (String path ) {
112+ File file = new File (path );
113+ if (!file .exists () || !file .getName ().endsWith (".sk" )) {
114+ skAnalyzer .getLogger ().error ("Invalid file path" );
115+ return false ;
116+ }
117+
118+ Script script = ScriptLoader .getScript (file );
119+ if (script != null ) {
120+ ScriptLoader .unloadScript (script );
121+ return true ;
122+ }
123+ return false ;
124+ }
125+
126+ @ Override
127+ public void unloadAllScripts () {
128+ ScriptLoader .unloadScripts (ScriptLoader .getLoadedScripts ());
129+ }
130+
131+ private Map <File , ScriptAnalyzeResult > buildAnalyzeResults (Set <File > files , CachingLogHandler logHandler ) {
132+ return files .stream ().collect (Collectors .toMap (
133+ Function .identity (),
134+ file -> new ScriptAnalyzeResult (
135+ file ,
136+ logHandler .scriptErrors (file ),
137+ handleParsedScript (file )
138+ )
139+ ));
83140 }
84141
85142 private ScriptStructure handleParsedScript (File file ) {
@@ -92,15 +149,14 @@ private ScriptStructure handleParsedScript(File file) {
92149 if (script != null ) {
93150 script .getStructures ().forEach (structure -> {
94151 if (structure instanceof StructCommand command ) {
95- ScriptCommand scriptCommand = ReflectionUtil . getScriptCommand ( command ) ;
152+ ScriptCommand scriptCommand = command . scriptCommand ;
96153 if (scriptCommand == null ) return ;
97154 commandDataList .add (handleCommand (command , scriptCommand ));
98155 } else if (structure instanceof StructEvent event ) {
99- SkriptEventInfo <?> eventInfo = ReflectionUtil .getEventInfo (event .getSkriptEvent ());
100- if (eventInfo == null ) return ;
156+ SkriptEventInfo <?> eventInfo = event .getSkriptEvent ().skriptEventInfo ;
101157 eventDataList .add (handleEvent (event .getSkriptEvent (), eventInfo ));
102158 } else if (structure instanceof StructFunction function ) {
103- Signature <?> signature = ReflectionUtil . getFunctionSignature ( function ) ;
159+ Signature <?> signature = function . signature ;
104160 if (signature == null ) return ;
105161 functionDataList .add (handleFunction (function , signature ));
106162 }
@@ -110,8 +166,6 @@ private ScriptStructure handleParsedScript(File file) {
110166 if (optionsData != null ) {
111167 options .putAll (optionsData .getOptions ());
112168 }
113-
114- ScriptLoader .unloadScript (script );
115169 }
116170
117171 return new ScriptStructure (commandDataList , eventDataList , functionDataList , options );
@@ -122,35 +176,30 @@ private CommandData handleCommand(StructCommand command, ScriptCommand scriptCom
122176 command .getEntryContainer ().getSource ().getLine (),
123177 scriptCommand .getName (),
124178 scriptCommand .getAliases (),
125- StringUtils .defaultIfEmpty (ReflectionUtil . getCommandPermission ( scriptCommand ) , null ),
126- StringUtils .defaultIfEmpty (ReflectionUtil . getCommandDescription ( scriptCommand ) , null ),
179+ StringUtils .defaultIfEmpty (scriptCommand . permission , null ),
180+ StringUtils .defaultIfEmpty (scriptCommand . description , null ),
127181 scriptCommand .getPrefix (),
128- StringUtils .defaultIfEmpty (ReflectionUtil . getCommandUsage ( scriptCommand ) , null ),
182+ StringUtils .defaultIfEmpty (scriptCommand . usage , null ),
129183 scriptCommand .getArguments ().stream ()
130- .map (argument -> {
131- ClassInfo <?> argumentType = ReflectionUtil .getArgumentType (argument );
132- if (argumentType != null )
133- return argumentType .getCodeName ();
134- return null ;
135- })
136- .filter (Objects ::nonNull )
184+ .map (argument -> argument .type .getCodeName ())
137185 .toList ()
138186 );
139187 }
140188
141189 private EventData handleEvent (SkriptEvent event , SkriptEventInfo <?> eventInfo ) {
142190 return new EventData (
143191 event .getEntryContainer ().getSource ().getLine (),
144- ReflectionUtil . getEventExpression ( event ) ,
192+ event . expr ,
145193 Objects .requireNonNullElse (eventInfo .getDocumentationID (), eventInfo .getId ()),
146194 event .getEventPriority ()
147195 );
148196 }
149197
150198 private FunctionData handleFunction (StructFunction function , Signature <?> signature ) {
151199 String returnType = null ;
152- if (signature .getReturnType () != null )
200+ if (signature .getReturnType () != null ) {
153201 returnType = signature .getReturnType ().getCodeName ();
202+ }
154203
155204 return new FunctionData (
156205 function .getEntryContainer ().getSource ().getLine (),
0 commit comments