diff --git a/src/main/java/io/github/syst3ms/skriptparser/Skript.java b/src/main/java/io/github/syst3ms/skriptparser/Skript.java index f0ed8a2f..9e759160 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/Skript.java +++ b/src/main/java/io/github/syst3ms/skriptparser/Skript.java @@ -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; @@ -24,12 +25,12 @@ public Skript(String[] mainArgs) { } @Override - public void finishedLoading(@Nullable String scriptName) { + public void finishedLoading(@Nullable Script script) { List 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) { diff --git a/src/main/java/io/github/syst3ms/skriptparser/config/Config.java b/src/main/java/io/github/syst3ms/skriptparser/config/Config.java index 18468426..ea5a77ae 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/config/Config.java +++ b/src/main/java/io/github/syst3ms/skriptparser/config/Config.java @@ -52,7 +52,7 @@ public Config(Path path, String resourceToCopy, SkriptLogger logger) { throw new RuntimeException(e); } - this.fileElements = FileParser.parseFileLines(path.toString(), strings, 0, 1, logger); + this.fileElements = FileParser.parseFileLines(path, strings, 0, 1, logger); logger.debug("Loaded config file at " + path + " with " + fileElements.size() + " lines"); } diff --git a/src/main/java/io/github/syst3ms/skriptparser/expressions/LitScriptName.java b/src/main/java/io/github/syst3ms/skriptparser/expressions/LitScriptName.java index 3ae31a9c..bcd5584d 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/expressions/LitScriptName.java +++ b/src/main/java/io/github/syst3ms/skriptparser/expressions/LitScriptName.java @@ -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. @@ -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 diff --git a/src/main/java/io/github/syst3ms/skriptparser/file/FileElement.java b/src/main/java/io/github/syst3ms/skriptparser/file/FileElement.java index b5fee322..e10d6e84 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/file/FileElement.java +++ b/src/main/java/io/github/syst3ms/skriptparser/file/FileElement.java @@ -1,5 +1,7 @@ package io.github.syst3ms.skriptparser.file; +import java.nio.file.Path; + /** * Represents any non-blank and not comment-only line in a file. It is important to note that information about comments * is absent from this class, as they are discarded before being passed to the constructor.
@@ -9,13 +11,21 @@ * additional properties. */ public class FileElement { - private final String fileName; + private final Path filePath; private final int line; private final String content; private final int indentation; + /** + * @deprecated Use {@link #FileElement(Path, int, String, int)} instead. + */ + @Deprecated(forRemoval = true) public FileElement(String fileName, int line, String content, int indentation) { - this.fileName = fileName; + this(Path.of(fileName), line, content, indentation); + } + + public FileElement(Path filePath, int line, String content, int indentation) { + this.filePath = filePath; this.line = line; this.content = content; this.indentation = indentation; @@ -24,6 +34,7 @@ public FileElement(String fileName, int line, String content, int indentation) { /** * The returned {@link String} does not include the indentation of the line. To have the line content along with * indentation, use {@link #toString()} + * * @return the text content of this line, excluding any indentation. */ public String getLineContent() { @@ -34,14 +45,13 @@ public String getLineContent() { public boolean equals(Object obj) { if (this == obj) return true; - if (!(obj instanceof FileElement)) { + if (!(obj instanceof FileElement other)) { return false; } else { - var other = (FileElement) obj; return indentation == other.indentation && - line == other.line && - content.equalsIgnoreCase(other.content) && - fileName.equals(other.fileName); + line == other.line && + content.equalsIgnoreCase(other.content) && + filePath.toAbsolutePath().normalize().equals(other.filePath.toAbsolutePath().normalize()); } } @@ -58,14 +68,23 @@ public String toString() { } /** - * @return the name of the file this line is contained in + * @deprecated Use {@link #getFilePath()} instead. */ + @Deprecated(forRemoval = true) public String getFileName() { - return fileName; + return filePath.toString(); + } + + /** + * @return the path of the file this line is contained in + */ + public Path getFilePath() { + return filePath; } /** * Line numbering starts at 1, and blank and comment-only lines are accounted for. + * * @return the line in the file where this line is located at. */ public int getLine() { diff --git a/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java b/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java index 6b6dbfc1..0ce28478 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java +++ b/src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java @@ -4,6 +4,7 @@ import io.github.syst3ms.skriptparser.log.SkriptLogger; import io.github.syst3ms.skriptparser.util.FileUtils; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; @@ -15,12 +16,20 @@ public class FileParser { public static final Pattern COMMENT_PATTERN = Pattern.compile("#(?=(?:(?:[^\"]*\"){2})*[^\"]*$)"); + /** + * @deprecated Use {@link #parseFileLines(Path, List, int, int, SkriptLogger)} instead. + */ + @Deprecated(forRemoval = true) + public static List parseFileLines(String fileName, List lines, int expectedIndentation, int lastLine, SkriptLogger logger) { + return parseFileLines(Path.of(fileName), lines, expectedIndentation, lastLine, logger); + } + /** * Parses a {@linkplain List} of strings into a list of {@link FileElement}s. This creates {@link FileElement} and * {@link FileSection} objects from the lines, effectively structuring the lines into a tree. * This removes comments from each line, and discards any blank lines afterwards. * - * @param fileName the name of the file the lines belong to + * @param filePath the path of the file the lines belong to * @param lines the list of lines to parse * @param expectedIndentation the indentation level the first line is expected to be at * @param lastLine a parameter that keeps track of the line count throughout recursive calls of this method when @@ -28,7 +37,7 @@ public class FileParser { * @param logger the logger * @return a list of {@link FileElement}s */ - public static List parseFileLines(String fileName, List lines, int expectedIndentation, int lastLine, SkriptLogger logger) { + public static List parseFileLines(Path filePath, List lines, int expectedIndentation, int lastLine, SkriptLogger logger) { List elements = new ArrayList<>(); boolean multiLineComment = false; for (var i = 0; i < lines.size(); i++) { @@ -42,7 +51,7 @@ public static List parseFileLines(String fileName, List lin String content = removeComments(line); if (multiLineComment || content.isEmpty()) { - elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation)); + elements.add(new VoidElement(filePath, lastLine + i, expectedIndentation)); continue; } @@ -59,20 +68,20 @@ public static List parseFileLines(String fileName, List lin } if (content.endsWith(":")) { if (i + 1 == lines.size()) { - elements.add(new FileSection(fileName, lastLine + i, content.substring(0, content.length() - 1), + elements.add(new FileSection(filePath, lastLine + i, content.substring(0, content.length() - 1), new ArrayList<>(), expectedIndentation )); } else { - var sectionElements = parseFileLines(fileName, lines.subList(i + 1, lines.size()), + var sectionElements = parseFileLines(filePath, lines.subList(i + 1, lines.size()), expectedIndentation + 1, lastLine + i + 1, logger); - elements.add(new FileSection(fileName, lastLine + i, content.substring(0, content.length() - 1), + elements.add(new FileSection(filePath, lastLine + i, content.substring(0, content.length() - 1), sectionElements, expectedIndentation )); i += count(sectionElements); } } else { - elements.add(new FileElement(fileName, lastLine + i, content, expectedIndentation)); + elements.add(new FileElement(filePath, lastLine + i, content, expectedIndentation)); } } return elements; diff --git a/src/main/java/io/github/syst3ms/skriptparser/file/FileSection.java b/src/main/java/io/github/syst3ms/skriptparser/file/FileSection.java index 464adab9..5b543d8e 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/file/FileSection.java +++ b/src/main/java/io/github/syst3ms/skriptparser/file/FileSection.java @@ -2,6 +2,7 @@ import io.github.syst3ms.skriptparser.lang.entries.OptionLoader; +import java.nio.file.Path; import java.util.List; import java.util.Optional; import java.util.stream.Stream; @@ -15,8 +16,16 @@ public class FileSection extends FileElement { private final List elements; private int length = -1; + /** + * @deprecated Use {@link #FileSection(Path, int, String, List, int)} instead. + */ + @Deprecated(forRemoval = true) public FileSection(String fileName, int line, String content, List elements, int indentation) { - super(fileName, line, content, indentation); + this(Path.of(fileName), line, content, elements, indentation); + } + + public FileSection(Path filePath, int line, String content, List elements, int indentation) { + super(filePath, line, content, indentation); this.elements = elements; } diff --git a/src/main/java/io/github/syst3ms/skriptparser/file/VoidElement.java b/src/main/java/io/github/syst3ms/skriptparser/file/VoidElement.java index f309c12f..357a858b 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/file/VoidElement.java +++ b/src/main/java/io/github/syst3ms/skriptparser/file/VoidElement.java @@ -1,10 +1,21 @@ package io.github.syst3ms.skriptparser.file; +import java.nio.file.Path; + /** * A {@link FileElement} representing a blank line. */ public class VoidElement extends FileElement { + + /** + * @deprecated Use {@link #VoidElement(Path, int, int)} instead. + */ + @Deprecated(forRemoval = true) public VoidElement(String fileName, int line, int indentation) { - super(fileName, line, "", indentation); + this(Path.of(fileName), line, indentation); + } + + public VoidElement(Path filePath, int line, int indentation) { + super(filePath, line, "", indentation); } } diff --git a/src/main/java/io/github/syst3ms/skriptparser/lang/TriggerMap.java b/src/main/java/io/github/syst3ms/skriptparser/lang/TriggerMap.java index e3f6b1bb..ad764b21 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/lang/TriggerMap.java +++ b/src/main/java/io/github/syst3ms/skriptparser/lang/TriggerMap.java @@ -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; @@ -14,40 +16,67 @@ */ public class TriggerMap { - private static final Map, List>> TRIGGERS = new TreeMap<>(); + private static final Map, List>> TRIGGERS = new TreeMap<>(); + + /** + * @deprecated Use {@link #addTrigger(Script, Class, Trigger)} instead. + */ + @Deprecated(forRemoval = true) + public static void addTrigger(String scriptName, Class 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 context, Trigger trigger) { - TRIGGERS.computeIfAbsent(scriptName, k -> new HashMap<>()).computeIfAbsent(context, k -> new ArrayList<>()).add(trigger); + public static void addTrigger(Script script, Class 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, List> 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, List> getTriggersByScript(String scriptName) { - return TRIGGERS.getOrDefault(scriptName, Map.of()); + public static Map, List> getTriggersByScript(Script script) { + return TRIGGERS.getOrDefault(script, Map.of()); } /** diff --git a/src/main/java/io/github/syst3ms/skriptparser/log/SkriptLogger.java b/src/main/java/io/github/syst3ms/skriptparser/log/SkriptLogger.java index 5dc51d87..befd80cf 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/log/SkriptLogger.java +++ b/src/main/java/io/github/syst3ms/skriptparser/log/SkriptLogger.java @@ -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; @@ -56,7 +58,7 @@ public class SkriptLogger { private boolean hasError = false; private final LinkedList errorContext = new LinkedList<>(); // File - private String fileName; + private Script script; private List fileElements; private int line = -1; // Logs @@ -72,13 +74,19 @@ public SkriptLogger() { this(false); } + @Deprecated(forRemoval = true) + public void setFileInfo(String fileName, List 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 fileElements) { - this.fileName = fileName; + public void setFileInfo(Script script, List fileElements) { + this.script = script; this.fileElements = flatten(fileElements); } @@ -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 )); } @@ -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; } /** diff --git a/src/main/java/io/github/syst3ms/skriptparser/parsing/Script.java b/src/main/java/io/github/syst3ms/skriptparser/parsing/Script.java new file mode 100644 index 00000000..d7cdfe31 --- /dev/null +++ b/src/main/java/io/github/syst3ms/skriptparser/parsing/Script.java @@ -0,0 +1,27 @@ +package io.github.syst3ms.skriptparser.parsing; + +import java.nio.file.Path; + +/** + * Represents a script file. + * This record just encapsulates the file path of the script. + * + * @param scriptPath the {@link Path} pointing to the script file + */ +public record Script(Path scriptPath) implements Comparable