Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/io/github/syst3ms/skriptparser/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.syst3ms.skriptparser.lang.Trigger;
import io.github.syst3ms.skriptparser.lang.TriggerMap;
import io.github.syst3ms.skriptparser.lang.event.StartOnLoadEvent;
import io.github.syst3ms.skriptparser.parsing.Script;
import io.github.syst3ms.skriptparser.registration.SkriptAddon;
import io.github.syst3ms.skriptparser.registration.SkriptRegistration;
import io.github.syst3ms.skriptparser.structures.functions.StructFunction;
Expand All @@ -24,12 +25,12 @@ public Skript(String[] mainArgs) {
}

@Override
public void finishedLoading(@Nullable String scriptName) {
public void finishedLoading(@Nullable Script script) {
List<Trigger> triggers;
if (scriptName == null) {
if (script == null) {
triggers = TriggerMap.getAllTriggers();
} else {
triggers = TriggerMap.getTriggersByScript(scriptName).values().stream().flatMap(List::stream).toList();
triggers = TriggerMap.getTriggersByScript(script).values().stream().flatMap(List::stream).toList();
}
triggers.stream().sorted(Comparator.comparing(trigger -> trigger.getEvent().getLoadingPriority())).forEach(trigger -> {
if (trigger.getEvent() instanceof StartOnLoadEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.github.syst3ms.skriptparser.lang.TriggerContext;
import io.github.syst3ms.skriptparser.log.SkriptLogger;
import io.github.syst3ms.skriptparser.parsing.ParseContext;
import io.github.syst3ms.skriptparser.util.FileUtils;

/**
* The name of the current executed script, without the extension.
Expand Down Expand Up @@ -38,7 +37,7 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContex

@Override
public String[] getValues() {
return new String[]{FileUtils.removeExtension(logger.getFileName())};
return new String[]{logger.getScript().scriptName()};
}

@Override
Expand Down
53 changes: 41 additions & 12 deletions src/main/java/io/github/syst3ms/skriptparser/lang/TriggerMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.syst3ms.skriptparser.lang;

import io.github.syst3ms.skriptparser.parsing.Script;
import io.github.syst3ms.skriptparser.parsing.ScriptLoader;
import io.github.syst3ms.skriptparser.structures.functions.Functions;
import io.github.syst3ms.skriptparser.variables.Variables;

Expand All @@ -14,40 +16,67 @@
*/
public class TriggerMap {

private static final Map<String, Map<Class<? extends TriggerContext>, List<Trigger>>> TRIGGERS = new TreeMap<>();
private static final Map<Script, Map<Class<? extends TriggerContext>, List<Trigger>>> TRIGGERS = new TreeMap<>();

/**
* @deprecated Use {@link #addTrigger(Script, Class, Trigger)} instead.
*/
@Deprecated(forRemoval = true)
public static void addTrigger(String scriptName, Class<? extends TriggerContext> context, Trigger trigger) {
Script script = ScriptLoader.getScriptByName(scriptName);
if (script != null) addTrigger(script, context, trigger);
}

/**
* Add a trigger to the map.
*
* @param scriptName Name of a script
* @param script The script to add the trigger to
* @param context Trigger context class to which the trigger should be added
* @param trigger Trigger to add
*/
public static void addTrigger(String scriptName, Class<? extends TriggerContext> context, Trigger trigger) {
TRIGGERS.computeIfAbsent(scriptName, k -> new HashMap<>()).computeIfAbsent(context, k -> new ArrayList<>()).add(trigger);
public static void addTrigger(Script script, Class<? extends TriggerContext> context, Trigger trigger) {
TRIGGERS.computeIfAbsent(script, k -> new HashMap<>()).computeIfAbsent(context, k -> new ArrayList<>()).add(trigger);
}

/**
* @deprecated Use {@link #clearTriggers(Script)} instead.
*/
@Deprecated(forRemoval = true)
public static void clearTriggers(String scriptName) {
Script script = ScriptLoader.getScriptByName(scriptName);
if (script != null) clearTriggers(script);
}

/**
* Clear all triggers for a script.
*
* @param scriptName Script name to clear triggers for
* @param script Script to clear triggers for
*/
public static void clearTriggers(String scriptName) {
TRIGGERS.getOrDefault(scriptName, Map.of()).values().forEach(triggers -> {
public static void clearTriggers(Script script) {
TRIGGERS.getOrDefault(script, Map.of()).values().forEach(triggers -> {
triggers.forEach(trigger -> trigger.getEvent().unload());
});
TRIGGERS.remove(scriptName);
Functions.removeFunctions(scriptName);
TRIGGERS.remove(script);
Functions.removeFunctions(script);
}

/**
* @deprecated Use {@link #getTriggersByScript(Script)} instead.
*/
@Deprecated(forRemoval = true)
public static Map<Class<? extends TriggerContext>, List<Trigger>> getTriggersByScript(String scriptName) {
Script script = ScriptLoader.getScriptByName(scriptName);
return script != null ? getTriggersByScript(script) : Map.of();
}

/**
* Get all the triggers associated with a script.
*
* @param scriptName Script name to get triggers for
* @param script Script to get triggers for
* @return Map of trigger contexts to triggers
*/
public static Map<Class<? extends TriggerContext>, List<Trigger>> getTriggersByScript(String scriptName) {
return TRIGGERS.getOrDefault(scriptName, Map.of());
public static Map<Class<? extends TriggerContext>, List<Trigger>> getTriggersByScript(Script script) {
return TRIGGERS.getOrDefault(script, Map.of());
}

/**
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/io/github/syst3ms/skriptparser/log/SkriptLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.github.syst3ms.skriptparser.file.FileElement;
import io.github.syst3ms.skriptparser.file.FileSection;
import io.github.syst3ms.skriptparser.parsing.Script;
import io.github.syst3ms.skriptparser.parsing.ScriptLoader;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand Down Expand Up @@ -56,7 +58,7 @@ public class SkriptLogger {
private boolean hasError = false;
private final LinkedList<ErrorContext> errorContext = new LinkedList<>();
// File
private String fileName;
private Script script;
private List<FileElement> fileElements;
private int line = -1;
// Logs
Expand All @@ -72,13 +74,19 @@ public SkriptLogger() {
this(false);
}

@Deprecated(forRemoval = true)
public void setFileInfo(String fileName, List<FileElement> fileElements) {
Script script = ScriptLoader.getScriptByName(fileName);
setFileInfo(script, fileElements);
}

/**
* Provides the logger information about the file it's currently parsing
* @param fileName the file name
* @param script the {@link Script} this logger is for
* @param fileElements the {@link FileElement}s of the current file
*/
public void setFileInfo(String fileName, List<FileElement> fileElements) {
this.fileName = fileName;
public void setFileInfo(Script script, List<FileElement> fileElements) {
this.script = script;
this.fileElements = flatten(fileElements);
}

Expand Down Expand Up @@ -153,7 +161,7 @@ private void log(String message, LogType type, @Nullable ErrorType error, @Nulla
message,
line + 1,
fileElements.get(line).getLineContent(),
fileName),
script.scriptName()),
type, line, ctx, error, tip
));
}
Expand Down Expand Up @@ -298,8 +306,16 @@ public void setDebug(boolean debug) {
this.debug = debug;
}

/**
* @deprecated use {@link #getScript()} instead.
*/
@Deprecated(forRemoval = true)
public String getFileName() {
return fileName;
return script.scriptName();
}

public Script getScript() {
return script;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/github/syst3ms/skriptparser/parsing/Script.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.syst3ms.skriptparser.parsing;

import java.nio.file.Path;

public record Script(Path scriptPath) implements Comparable<Script> {
Comment thread
3add marked this conversation as resolved.

/**
* Get the name of the script, without the {@code .sk} extension
*/
public String scriptName() {
String fileName = scriptPath.getFileName().toString();
int lastDot = fileName.lastIndexOf('.');
return lastDot == -1 ? fileName : fileName.substring(0, lastDot);
}

@Override
public int compareTo(Script o) {
return this.scriptPath.compareTo(o.scriptPath);
}
Comment thread
3add marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import io.github.syst3ms.skriptparser.log.LogEntry;
import io.github.syst3ms.skriptparser.log.SkriptLogger;
import io.github.syst3ms.skriptparser.util.FileUtils;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand All @@ -26,6 +28,29 @@
*/
public class ScriptLoader {

private static final List<Script> LOADED_SCRIPTS = new ArrayList<>();

/**
* Get a collection of all currently loaded scripts.
*
* @return A collection of loaded Script objects
*/
public static Collection<Script> getLoadedScripts() {
return Collections.unmodifiableCollection(LOADED_SCRIPTS);
}

/**
* Get a loaded Script object by its parsed name.
* This is method is inconsistent since scripts with the same name in different directories can exist.
*
* @param scriptName The name of the script
* @return The Script object, or null if not loaded
*/
@Deprecated(forRemoval = true)
public static @Nullable Script getScriptByName(String scriptName) {
return LOADED_SCRIPTS.stream().filter(s -> s.scriptName().equals(scriptName)).findFirst().orElse(null);
}

/**
* Parses and loads the provided script in memory.
*
Expand All @@ -46,14 +71,26 @@ public static List<LogEntry> loadScript(Path scriptPath, boolean debug) {
*/
public static List<LogEntry> loadScript(Path scriptPath, SkriptLogger logger, boolean debug) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should make this return a Pair<Script, List<LogEntry>> since this method should also return the created Script object. Wdyt

List<FileElement> elements;
String scriptName = scriptPath.getFileName().toString().replaceAll("(.+)\\..+", "$1");

// Clear triggers from unloaded events
TriggerMap.clearTriggers(scriptName);
Script oldScript = LOADED_SCRIPTS.stream()
.filter(s -> s.scriptPath().equals(scriptPath))
.findFirst()
.orElse(null);

if (oldScript != null) {
// Clean up the old instance from everywhere
TriggerMap.clearTriggers(oldScript);
LOADED_SCRIPTS.remove(oldScript);
}

Script script = new Script(scriptPath);
String scriptName = script.scriptName();

try {
var lines = FileUtils.readAllLines(scriptPath);

LOADED_SCRIPTS.add(script);

elements = FileParser.parseFileLines(scriptName,
lines,
0,
Expand All @@ -65,8 +102,10 @@ public static List<LogEntry> loadScript(Path scriptPath, SkriptLogger logger, bo
e.printStackTrace();
return Collections.emptyList();
}
logger.setFileInfo(scriptPath.getFileName().toString(), elements);

logger.setFileInfo(script, elements);
List<UnloadedTrigger> unloadedTriggers = new ArrayList<>();

for (var element : elements) {
logger.finalizeLogs();
logger.nextLine();
Expand Down Expand Up @@ -100,10 +139,10 @@ public static List<LogEntry> loadScript(Path scriptPath, SkriptLogger logger, bo
Set<Class<? extends TriggerContext>> contexts = unloaded.eventInfo().getContexts();
if (contexts.isEmpty()) {
// A dummy context will be used for this
TriggerMap.addTrigger(scriptName, TriggerContext.class, loaded);
TriggerMap.addTrigger(script, TriggerContext.class, loaded);
} else {
for (Class<? extends TriggerContext> context : contexts) {
TriggerMap.addTrigger(scriptName, context, loaded);
TriggerMap.addTrigger(script, context, loaded);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.syst3ms.skriptparser.Skript;
import io.github.syst3ms.skriptparser.lang.Trigger;
import io.github.syst3ms.skriptparser.lang.event.SkriptEvent;
import io.github.syst3ms.skriptparser.parsing.Script;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -52,16 +53,31 @@ public final String getAddonName() {
}

/**
* Is called when a script has finished loading. Optionally overridable.
* @deprecated Use {@link #finishedLoading(Script)} instead.
*/
@Deprecated(forRemoval = true)
public void finishedLoading(@Nullable String scriptName) {

}

/**
* Is called when a script has finished loading. Optionally overridable.
*/
public void finishedLoading(@Nullable Script script) {
if (script != null) {
// this is for backwards compatibility
// this should be made to contain no default implementation after removing all the other forRemovals, that are using scriptName instead of Script
finishedLoading(script.scriptName());
} else {
finishedLoading((String) null);
}
}

/**
* Is called when all scripts have finished loading. Optionally overridable.
*/
public void finishedLoading() {
finishedLoading(null);
finishedLoading((Script) null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected void execute(TriggerContext ctx) {
public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext) {
MatchResult result = parseContext.getMatches().get(0); // whole pattern matched because it is one single regex
String functionName = result.group(1);
Optional<Function<?>> optionalFunction = Functions.getFunctionByName(functionName, parseContext.getLogger().getFileName());
Optional<Function<?>> optionalFunction = Functions.getFunctionByName(functionName, parseContext.getLogger().getScript());
SkriptLogger logger = parseContext.getLogger();
if (optionalFunction.isEmpty()) {
logger.error("No function was found under the name '" + functionName + "'", ErrorType.SEMANTIC_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Object[] getValues(TriggerContext ctx) {
public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext) {
MatchResult result = parseContext.getMatches().get(0); // whole pattern matched because it is one single regex
String functionName = result.group(1);
Optional<Function<?>> optionalFunction = Functions.getFunctionByName(functionName, parseContext.getLogger().getFileName());
Optional<Function<?>> optionalFunction = Functions.getFunctionByName(functionName, parseContext.getLogger().getScript());
SkriptLogger logger = parseContext.getLogger();
if (optionalFunction.isEmpty()) {
logger.error("No function was found under the name '" + functionName + "'", ErrorType.SEMANTIC_ERROR);
Expand Down
Loading
Loading