Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -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");
}

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
37 changes: 28 additions & 9 deletions src/main/java/io/github/syst3ms/skriptparser/file/FileElement.java
Original file line number Diff line number Diff line change
@@ -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.<br>
Expand All @@ -9,13 +11,21 @@
* additional properties.</strong>
*/
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;
Expand All @@ -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() {
Expand All @@ -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());
}
}

Expand All @@ -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() {
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/io/github/syst3ms/skriptparser/file/FileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,20 +16,28 @@ 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<FileElement> parseFileLines(String fileName, List<String> 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
* parsing sections
* @param logger the logger
* @return a list of {@link FileElement}s
*/
public static List<FileElement> parseFileLines(String fileName, List<String> lines, int expectedIndentation, int lastLine, SkriptLogger logger) {
public static List<FileElement> parseFileLines(Path filePath, List<String> lines, int expectedIndentation, int lastLine, SkriptLogger logger) {
List<FileElement> elements = new ArrayList<>();
boolean multiLineComment = false;
for (var i = 0; i < lines.size(); i++) {
Expand All @@ -42,7 +51,7 @@ public static List<FileElement> parseFileLines(String fileName, List<String> 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;
}

Expand All @@ -59,20 +68,20 @@ public static List<FileElement> parseFileLines(String fileName, List<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,8 +16,16 @@ public class FileSection extends FileElement {
private final List<FileElement> 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<FileElement> 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<FileElement> elements, int indentation) {
super(filePath, line, content, indentation);
this.elements = elements;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
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
Loading
Loading