Skip to content

Commit

Permalink
Script type & expression. (#6702)
Browse files Browse the repository at this point in the history
* Create Script type.

* Support script string/name conversion.

* Script expression.

* Add script lang entry.

* Tests for script expression & names.

* Support all scripts expression.

* Script effects & tests.

* Create dummy Script handle for disabled scripts.

* Script reflection feature flag.

* Restrict literal parsing to commands & parse.

* Test feature flag for resolving name.

* Split ExprScripts by feature to support disabled scripts.

* Fix ExprName tests for new & old behaviour.

* Add tests for disabled script handles.

* Apply suggestions from code review

Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>

* Improve script loading/unloading safety.

* Add feature check for script hotswapping.

* Use expression stream.

* Conformity for file names and proper loading safety.

* Document validity & add condition support.

* Add script is loaded condition + tests.

* Some changes from code review.

* More changes.

* Apply suggestions from code review

Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>

* Clean up EffScriptFile and rename ExprScripts variations.

* Finish code review changes.

* Clean up script loader stuff.

* Fix conflicts.

* Suggested changes.

* Apply suggestions from code review

Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>

* Finished.

---------

Co-authored-by: Patrick Miller <apickledwalrus@gmail.com>
Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 21, 2024
1 parent a45c0b9 commit 6099204
Show file tree
Hide file tree
Showing 22 changed files with 1,003 additions and 358 deletions.
154 changes: 104 additions & 50 deletions src/main/java/ch/njol/skript/ScriptLoader.java

Large diffs are not rendered by default.

32 changes: 6 additions & 26 deletions src/main/java/ch/njol/skript/SkriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ private static void reloading(CommandSender sender, String what, RedirectingLogH
logHandler.log(new LogEntry(Level.INFO, Utils.replaceEnglishChatStyles(text)));
}


private static final ArgsMessage m_reloaded = new ArgsMessage(CONFIG_NODE + ".reload.reloaded");
private static final ArgsMessage m_reload_error = new ArgsMessage(CONFIG_NODE + ".reload.error");

Expand Down Expand Up @@ -468,7 +467,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
@Nullable
private static File getScriptFromArgs(CommandSender sender, String[] args) {
String script = StringUtils.join(args, " ", 1, args.length);
File f = getScriptFromName(script);
File f = ScriptLoader.getScriptFromName(script);
if (f == null) {
// Always allow '/' and '\' regardless of OS
boolean directory = script.endsWith("/") || script.endsWith("\\") || script.endsWith(File.separator);
Expand All @@ -478,32 +477,13 @@ private static File getScriptFromArgs(CommandSender sender, String[] args) {
return f;
}

/**
* Moved to {@link ScriptLoader#getScriptFromName(String)}
*/
@Nullable
@Deprecated(forRemoval = true)
public static File getScriptFromName(String script) {
if (script.endsWith("/") || script.endsWith("\\")) { // Always allow '/' and '\' regardless of OS
script = script.replace('/', File.separatorChar).replace('\\', File.separatorChar);
} else if (!StringUtils.endsWithIgnoreCase(script, ".sk")) {
int dot = script.lastIndexOf('.');
if (dot > 0 && !script.substring(dot + 1).equals(""))
return null;
script = script + ".sk";
}

if (script.startsWith(ScriptLoader.DISABLED_SCRIPT_PREFIX))
script = script.substring(ScriptLoader.DISABLED_SCRIPT_PREFIX_LENGTH);

File scriptFile = new File(Skript.getInstance().getScriptsFolder(), script);
if (!scriptFile.exists()) {
scriptFile = new File(scriptFile.getParentFile(), ScriptLoader.DISABLED_SCRIPT_PREFIX + scriptFile.getName());
if (!scriptFile.exists()) {
return null;
}
}
try {
return scriptFile.getCanonicalFile();
} catch (IOException e) {
throw Skript.exception(e, "An exception occurred while trying to get the script file from the string '" + script + "'");
}
return ScriptLoader.getScriptFromName(script);
}

private static File toggleFile(File file, boolean enable) throws IOException {
Expand Down
89 changes: 71 additions & 18 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ch.njol.skript.classes.data;

import ch.njol.skript.Skript;
import ch.njol.skript.ScriptLoader;
import ch.njol.skript.SkriptCommand;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemData;
import ch.njol.skript.aliases.ItemType;
Expand Down Expand Up @@ -36,12 +38,16 @@
import ch.njol.skript.util.visual.VisualEffect;
import ch.njol.skript.util.visual.VisualEffects;
import ch.njol.yggdrasil.Fields;
import org.jetbrains.annotations.NotNull;
import org.skriptlang.skript.lang.script.Script;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.io.StreamCorruptedException;
import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;
Expand All @@ -50,7 +56,7 @@
@SuppressWarnings("rawtypes")
public class SkriptClasses {
public SkriptClasses() {}

static {
//noinspection unchecked
Classes.registerClass(new ClassInfo<>(ClassInfo.class, "classinfo")
Expand All @@ -73,17 +79,17 @@ public SkriptClasses() {}
public ClassInfo parse(final String s, final ParseContext context) {
return Classes.getClassInfoFromUserInput(Noun.stripIndefiniteArticle(s));
}

@Override
public String toString(final ClassInfo c, final int flags) {
return c.toString(flags);
}

@Override
public String toVariableNameString(final ClassInfo c) {
return c.getCodeName();
}

@Override
public String getDebugMessage(final ClassInfo c) {
return c.getCodeName();
Expand All @@ -97,17 +103,17 @@ public Fields serialize(final ClassInfo c) {
f.putObject("codeName", c.getCodeName());
return f;
}

@Override
public boolean canBeInstantiated() {
return false;
}

@Override
public void deserialize(final ClassInfo o, final Fields f) throws StreamCorruptedException {
assert false;
}

@Override
protected ClassInfo deserialize(final Fields fields) throws StreamCorruptedException {
final String codeName = fields.getObject("codeName", String.class);
Expand All @@ -118,20 +124,20 @@ protected ClassInfo deserialize(final Fields fields) throws StreamCorruptedExcep
throw new StreamCorruptedException("Invalid ClassInfo " + codeName);
return ci;
}

// return c.getCodeName();
@Override
@Nullable
public ClassInfo deserialize(final String s) {
return Classes.getClassInfoNoError(s);
}

@Override
public boolean mustSyncDeserialization() {
return false;
}
}));

Classes.registerClass(new ClassInfo<>(WeatherType.class, "weathertype")
.user("weather ?types?", "weather conditions?", "weathers?")
.name("Weather Type")
Expand All @@ -148,20 +154,20 @@ public boolean mustSyncDeserialization() {
public WeatherType parse(final String s, final ParseContext context) {
return WeatherType.parse(s);
}

@Override
public String toString(final WeatherType o, final int flags) {
return o.toString(flags);
}

@Override
public String toVariableNameString(final WeatherType o) {
return "" + o.name().toLowerCase(Locale.ENGLISH);
}

})
.serializer(new EnumSerializer<>(WeatherType.class)));

Classes.registerClass(new ClassInfo<>(ItemType.class, "itemtype")
.user("item ?types?", "materials?")
.name("Item Type")
Expand Down Expand Up @@ -603,7 +609,7 @@ public String toVariableNameString(final EnchantmentType o) {
.since("2.0")
.parser(new Parser<Experience>() {
private final RegexMessage pattern = new RegexMessage("types.experience.pattern", Pattern.CASE_INSENSITIVE);

@Override
@Nullable
public Experience parse(String s, final ParseContext context) {
Expand All @@ -616,12 +622,12 @@ public Experience parse(String s, final ParseContext context) {
return new Experience(xp);
return null;
}

@Override
public String toString(final Experience xp, final int flags) {
return xp.toString();
}

@Override
public String toVariableNameString(final Experience xp) {
return "" + xp.getXP();
Expand Down Expand Up @@ -658,7 +664,7 @@ public String toVariableNameString(VisualEffect e) {

})
.serializer(new YggdrasilSerializer<>()));

Classes.registerClass(new ClassInfo<>(GameruleValue.class, "gamerulevalue")
.user("gamerule values?")
.name("Gamerule Value")
Expand All @@ -668,6 +674,53 @@ public String toVariableNameString(VisualEffect e) {
.since("2.5")
.serializer(new YggdrasilSerializer<GameruleValue>())
);

Classes.registerClass(new ClassInfo<>(Script.class, "script")
.user("scripts?")
.name("Script")
.description("A script loaded by Skript.",
"Disabled scripts will report as being empty since their content has not been loaded.")
.usage("")
.examples("the current script")
.since("INSERT VERSION")
.parser(new Parser<Script>() {
final Path path = Skript.getInstance().getScriptsFolder().getAbsoluteFile().toPath();

@Override
public boolean canParse(final ParseContext context) {
return switch (context) {
case PARSE, COMMAND -> true;
default -> false;
};
}

@Override
@Nullable
public Script parse(final String name, final ParseContext context) {
return switch (context) {
case PARSE, COMMAND -> {
@Nullable File file = ScriptLoader.getScriptFromName(name);
if (file == null || !file.isFile())
yield null;
yield ScriptLoader.getScript(file);
}
default -> null;
};
}

@Override
public String toString(final Script script, final int flags) {
return this.toVariableNameString(script);
}

@Override
public String toVariableNameString(final Script script) {
@Nullable File file = script.getConfig().getFile();
if (file == null)
return script.getConfig().getFileName();
return path.relativize(file.toPath().toAbsolutePath()).toString();
}
}));
}

}
Loading

0 comments on commit 6099204

Please sign in to comment.