Skip to content

Commit

Permalink
Release 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
GliczDev committed Jul 9, 2024
2 parents ceb4fe5 + 87ec4b0 commit 3d7e686
Show file tree
Hide file tree
Showing 42 changed files with 713 additions and 513 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "MockSkript"]
path = MockSkript
url = https://github.com/SkEditorPlus/MockSkript
url = https://github.com/SkEditorTeam/MockSkript
2 changes: 1 addition & 1 deletion MockSkript
Submodule MockSkript updated 149 files
24 changes: 2 additions & 22 deletions MockSkriptBridge/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
plugins {
id("java")
id("net.minecrell.plugin-yml.bukkit") version "0.6.0"
}

repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
}

dependencies {
compileOnly(project(":api"))
compileOnly(project(":MockSkript", "shadow")) {
exclude("*", "*")
}
compileOnly("org.projectlombok:lombok:1.18.30")
annotationProcessor("org.projectlombok:lombok:1.18.30")
}

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

tasks {
compileJava {
options.encoding = Charsets.UTF_8.name()
options.release.set(17)
dependsOn(clean)
}

jar.get().archiveVersion = ""
compileOnly("org.projectlombok:lombok:1.18.32")
annotationProcessor("org.projectlombok:lombok:1.18.32")
}

bukkit {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.command.ScriptCommand;
import ch.njol.skript.hooks.VaultHook;
import ch.njol.skript.hooks.regions.RegionsPlugin;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptEventInfo;
import ch.njol.skript.lang.function.Signature;
import ch.njol.skript.log.RedirectingLogHandler;
import ch.njol.skript.structures.*;
import ch.njol.skript.structures.StructCommand;
import ch.njol.skript.structures.StructEvent;
import ch.njol.skript.structures.StructFunction;
import ch.njol.skript.structures.StructOptions;
import me.glicz.skanalyzer.AnalyzerFlag;
import me.glicz.skanalyzer.ScriptAnalyzeResult;
import me.glicz.skanalyzer.SkAnalyzer;
import me.glicz.skanalyzer.bridge.util.ReflectionUtil;
import me.glicz.skanalyzer.bridge.log.CachingLogHandler;
import me.glicz.skanalyzer.bridge.util.FilesUtil;
import me.glicz.skanalyzer.result.ScriptAnalyzeResult;
import me.glicz.skanalyzer.result.ScriptAnalyzeResults;
import me.glicz.skanalyzer.structure.ScriptStructure;
import me.glicz.skanalyzer.structure.data.CommandData;
import me.glicz.skanalyzer.structure.data.EventData;
Expand All @@ -27,9 +30,13 @@
import java.nio.file.InvalidPathException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Collectors;

public class MockSkriptBridgeImpl extends MockSkriptBridge {
private Executor mainThreadExecutor;

public MockSkriptBridgeImpl(SkAnalyzer skAnalyzer) {
super(skAnalyzer);
}
Expand All @@ -39,6 +46,11 @@ public void onLoad() {
parseFlags();
}

@Override
public void onEnable() {
mainThreadExecutor = getServer().getScheduler().getMainThreadExecutor(this);
}

public void parseFlags() {
if (skAnalyzer.getFlags().contains(AnalyzerFlag.FORCE_VAULT_HOOK)) {
try {
Expand All @@ -63,23 +75,68 @@ public void parseFlags() {
}

@Override
public CompletableFuture<ScriptAnalyzeResult> parseScript(String path) {
public CompletableFuture<ScriptAnalyzeResults> parseScript(String path, boolean load) {
File file = new File(path);
if (!file.exists() || !file.getName().endsWith(".sk")) {
if (!file.exists() || (!file.getName().endsWith(".sk") && !(file.isDirectory() && load))) {
skAnalyzer.getLogger().error("Invalid file path");
return CompletableFuture.failedFuture(new InvalidPathException(path, "Provided file doesn't end with '.sk'"));
}
AnalyzerCommandSender sender = new AnalyzerCommandSender(skAnalyzer);
RedirectingLogHandler logHandler = new RedirectingLogHandler(sender, null).start();
return ScriptLoader.loadScripts(file, logHandler, false)
.handle((info, throwable) -> {
if (throwable != null) {
skAnalyzer.getLogger().error("Something went wrong while trying to parse '%s'".formatted(path), throwable);
return CompletableFuture.failedFuture(new RuntimeException(throwable));
}
return CompletableFuture.completedFuture(info);
})
.thenApply(info -> sender.finish(file, handleParsedScript(file)));

Set<File> files = FilesUtil.listScripts(file);
return CompletableFuture.supplyAsync(
() -> {
CachingLogHandler logHandler = new CachingLogHandler().start();
return ScriptLoader.loadScripts(files, logHandler)
.handle((info, throwable) -> {
if (throwable != null) {
skAnalyzer.getLogger().error("Something went wrong while trying to parse '%s'".formatted(path), throwable);
throw new RuntimeException(throwable);
}
return info;
})
.thenApply(info -> {
ScriptAnalyzeResults results = new ScriptAnalyzeResults(buildAnalyzeResults(files, logHandler));
if (!load) {
unloadScript(path);
}
return results;
})
.join();
},
mainThreadExecutor
);
}

@Override
public boolean unloadScript(String path) {
File file = new File(path);
if (!file.exists() || !file.getName().endsWith(".sk")) {
skAnalyzer.getLogger().error("Invalid file path");
return false;
}

Script script = ScriptLoader.getScript(file);
if (script != null) {
ScriptLoader.unloadScript(script);
return true;
}
return false;
}

@Override
public void unloadAllScripts() {
ScriptLoader.unloadScripts(ScriptLoader.getLoadedScripts());
}

private Map<File, ScriptAnalyzeResult> buildAnalyzeResults(Set<File> files, CachingLogHandler logHandler) {
return files.stream().collect(Collectors.toMap(
Function.identity(),
file -> new ScriptAnalyzeResult(
file,
logHandler.scriptErrors(file),
handleParsedScript(file)
)
));
}

private ScriptStructure handleParsedScript(File file) {
Expand All @@ -92,15 +149,14 @@ private ScriptStructure handleParsedScript(File file) {
if (script != null) {
script.getStructures().forEach(structure -> {
if (structure instanceof StructCommand command) {
ScriptCommand scriptCommand = ReflectionUtil.getScriptCommand(command);
ScriptCommand scriptCommand = command.scriptCommand;
if (scriptCommand == null) return;
commandDataList.add(handleCommand(command, scriptCommand));
} else if (structure instanceof StructEvent event) {
SkriptEventInfo<?> eventInfo = ReflectionUtil.getEventInfo(event.getSkriptEvent());
if (eventInfo == null) return;
SkriptEventInfo<?> eventInfo = event.getSkriptEvent().skriptEventInfo;
eventDataList.add(handleEvent(event.getSkriptEvent(), eventInfo));
} else if (structure instanceof StructFunction function) {
Signature<?> signature = ReflectionUtil.getFunctionSignature(function);
Signature<?> signature = function.signature;
if (signature == null) return;
functionDataList.add(handleFunction(function, signature));
}
Expand All @@ -110,8 +166,6 @@ private ScriptStructure handleParsedScript(File file) {
if (optionsData != null) {
options.putAll(optionsData.getOptions());
}

ScriptLoader.unloadScript(script);
}

return new ScriptStructure(commandDataList, eventDataList, functionDataList, options);
Expand All @@ -122,35 +176,30 @@ private CommandData handleCommand(StructCommand command, ScriptCommand scriptCom
command.getEntryContainer().getSource().getLine(),
scriptCommand.getName(),
scriptCommand.getAliases(),
StringUtils.defaultIfEmpty(ReflectionUtil.getCommandPermission(scriptCommand), null),
StringUtils.defaultIfEmpty(ReflectionUtil.getCommandDescription(scriptCommand), null),
StringUtils.defaultIfEmpty(scriptCommand.permission, null),
StringUtils.defaultIfEmpty(scriptCommand.description, null),
scriptCommand.getPrefix(),
StringUtils.defaultIfEmpty(ReflectionUtil.getCommandUsage(scriptCommand), null),
StringUtils.defaultIfEmpty(scriptCommand.usage, null),
scriptCommand.getArguments().stream()
.map(argument -> {
ClassInfo<?> argumentType = ReflectionUtil.getArgumentType(argument);
if (argumentType != null)
return argumentType.getCodeName();
return null;
})
.filter(Objects::nonNull)
.map(argument -> argument.type.getCodeName())
.toList()
);
}

private EventData handleEvent(SkriptEvent event, SkriptEventInfo<?> eventInfo) {
return new EventData(
event.getEntryContainer().getSource().getLine(),
ReflectionUtil.getEventExpression(event),
event.expr,
Objects.requireNonNullElse(eventInfo.getDocumentationID(), eventInfo.getId()),
event.getEventPriority()
);
}

private FunctionData handleFunction(StructFunction function, Signature<?> signature) {
String returnType = null;
if (signature.getReturnType() != null)
if (signature.getReturnType() != null) {
returnType = signature.getReturnType().getCodeName();
}

return new FunctionData(
function.getEntryContainer().getSource().getLine(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.glicz.skanalyzer.bridge.log;

import ch.njol.skript.log.LogEntry;
import ch.njol.skript.log.LogHandler;
import ch.njol.skript.log.SkriptLogger;
import me.glicz.skanalyzer.error.ScriptError;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CachingLogHandler extends LogHandler {
private final Map<File, List<ScriptError>> scriptErrors = new HashMap<>();

public @Unmodifiable List<ScriptError> scriptErrors(File file) {
if (scriptErrors.containsKey(file)) {
return List.copyOf(scriptErrors.get(file));
}
return List.of();
}

@Override
public @NotNull LogResult log(@NotNull LogEntry entry) {
if (entry.node != null) {
this.scriptErrors.computeIfAbsent(entry.node.getConfig().getFile(), file -> new ArrayList<>())
.add(new ScriptError(entry.node.getLine(), entry.message, entry.level));
}

return LogResult.CACHED;
}

@Override
public @NotNull CachingLogHandler start() {
return SkriptLogger.startLogHandler(this);
}
}
Loading

0 comments on commit 3d7e686

Please sign in to comment.