Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic function calling. #6713

Open
wants to merge 41 commits into
base: feature/script-reflection
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
106c555
Create Script type.
Moderocky May 17, 2024
fe9a1fb
Support script string/name conversion.
Moderocky May 17, 2024
da4d4b1
Script expression.
Moderocky May 17, 2024
ef742cc
Add script lang entry.
Moderocky May 17, 2024
571d302
Tests for script expression & names.
Moderocky May 17, 2024
5da3884
Support all scripts expression.
Moderocky May 17, 2024
deb484c
Script effects & tests.
Moderocky May 17, 2024
f1c0c72
Create dummy Script handle for disabled scripts.
Moderocky May 18, 2024
6c5a55a
Script reflection feature flag.
Moderocky May 18, 2024
e0debc8
Restrict literal parsing to commands & parse.
Moderocky May 18, 2024
5a692e3
Test feature flag for resolving name.
Moderocky May 18, 2024
e9c7b74
Split ExprScripts by feature to support disabled scripts.
Moderocky May 18, 2024
1b54e08
Fix ExprName tests for new & old behaviour.
Moderocky May 18, 2024
064f1ce
Add tests for disabled script handles.
Moderocky May 18, 2024
c46a5c9
Apply suggestions from code review
Moderocky May 18, 2024
1c95947
Improve script loading/unloading safety.
Moderocky May 18, 2024
43b245d
Add feature check for script hotswapping.
Moderocky May 18, 2024
c102949
Use expression stream.
Moderocky May 18, 2024
40e92b1
Conformity for file names and proper loading safety.
Moderocky May 18, 2024
803b34c
Document validity & add condition support.
Moderocky May 18, 2024
3ec719c
Add script is loaded condition + tests.
Moderocky May 19, 2024
a82c3cd
Dynamic function calling + tests.
Moderocky May 19, 2024
e35f9d2
Add language entry for types.
Moderocky May 20, 2024
c19648d
Single-encounter input bootstrapping.
Moderocky May 20, 2024
1eddf56
Apply suggestions from code review
Moderocky Aug 17, 2024
ab861c9
Fix inspection.
Moderocky Aug 17, 2024
19a99d2
Update src/main/java/ch/njol/skript/expressions/ExprFunction.java
Moderocky Sep 6, 2024
97f0356
Update src/main/java/ch/njol/skript/expressions/ExprResult.java
Moderocky Sep 6, 2024
ab096cc
Changes from review.
Moderocky Sep 7, 2024
ca3d648
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky Sep 7, 2024
d543809
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky Nov 21, 2024
58dd1b4
Fix merge problems.
Moderocky Nov 21, 2024
9e81543
Remove script command method usage.
Moderocky Nov 21, 2024
5bf9856
Fix branch muck.
Moderocky Nov 21, 2024
0143f2b
Fix more branch muck.
Moderocky Nov 21, 2024
9772529
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky Nov 23, 2024
757ab53
Fix up ExprName.
Moderocky Nov 23, 2024
820c9c6
Add docs.
Moderocky Nov 23, 2024
4ce50d6
Add docs.
Moderocky Nov 23, 2024
fb240c5
Add docs.
Moderocky Nov 23, 2024
36a9afd
Fix bits.
Moderocky Nov 23, 2024
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
128 changes: 72 additions & 56 deletions src/main/java/ch/njol/skript/ScriptLoader.java

Large diffs are not rendered by default.

31 changes: 18 additions & 13 deletions src/main/java/ch/njol/skript/SkriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import java.util.stream.Collectors;

public class SkriptCommand implements CommandExecutor {

private static final String CONFIG_NODE = "skript command";
private static final ArgsMessage m_reloading = new ArgsMessage(CONFIG_NODE + ".reload.reloading");

Expand Down Expand Up @@ -90,15 +90,15 @@ public class SkriptCommand implements CommandExecutor {
if (TestMode.DEV_MODE)
SKRIPT_COMMAND_HELP.add("test");
}

private static void reloading(CommandSender sender, String what, Object... args) {
what = args.length == 0 ? Language.get(CONFIG_NODE + ".reload." + what) : Language.format(CONFIG_NODE + ".reload." + what, args);
Skript.info(sender, StringUtils.fixCapitalization(m_reloading.toString(what)));
}

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");

private static void reloaded(CommandSender sender, RedirectingLogHandler r, TimingLogHandler timingLogHandler, String what, Object... args) {
what = args.length == 0 ? Language.get(CONFIG_NODE + ".reload." + what) : PluralizingArgsMessage.format(Language.format(CONFIG_NODE + ".reload." + what, args));
String timeTaken = String.valueOf(timingLogHandler.getTimeTaken());
Expand All @@ -108,12 +108,12 @@ private static void reloaded(CommandSender sender, RedirectingLogHandler r, Timi
else
Skript.error(sender, StringUtils.fixCapitalization(PluralizingArgsMessage.format(m_reload_error.toString(what, r.numErrors(), timeTaken))));
}

private static void info(CommandSender sender, String what, Object... args) {
what = args.length == 0 ? Language.get(CONFIG_NODE + "." + what) : PluralizingArgsMessage.format(Language.format(CONFIG_NODE + "." + what, args));
Skript.info(sender, StringUtils.fixCapitalization(what));
}

private static void error(CommandSender sender, String what, Object... args) {
what = args.length == 0 ? Language.get(CONFIG_NODE + "." + what) : PluralizingArgsMessage.format(Language.format(CONFIG_NODE + "." + what, args));
Skript.error(sender, StringUtils.fixCapitalization(what));
Expand Down Expand Up @@ -457,10 +457,10 @@ else if (args[0].equalsIgnoreCase("help")) {

return true;
}

private static final ArgsMessage m_invalid_script = new ArgsMessage(CONFIG_NODE + ".invalid script");
private static final ArgsMessage m_invalid_folder = new ArgsMessage(CONFIG_NODE + ".invalid folder");

@Nullable
private static File getScriptFromArgs(CommandSender sender, String[] args) {
String script = StringUtils.join(args, " ", 1, args.length);
Expand All @@ -473,7 +473,7 @@ private static File getScriptFromArgs(CommandSender sender, String[] args) {
}
return f;
}

@Nullable
public static File getScriptFromName(String script) {
if (script.endsWith("/") || script.endsWith("\\")) { // Always allow '/' and '\' regardless of OS
Expand All @@ -488,15 +488,20 @@ public static File getScriptFromName(String script) {
if (script.startsWith(ScriptLoader.DISABLED_SCRIPT_PREFIX))
script = script.substring(ScriptLoader.DISABLED_SCRIPT_PREFIX_LENGTH);

File scriptFile = new File(Skript.getInstance().getScriptsFolder(), script);
File scriptsFolder = Skript.getInstance().getScriptsFolder();
File scriptFile = new File(scriptsFolder, script);
if (!scriptFile.exists()) {
scriptFile = new File(scriptFile.getParentFile(), ScriptLoader.DISABLED_SCRIPT_PREFIX + scriptFile.getName());
if (!scriptFile.exists()) {
return null;
}
}
try {
return scriptFile.getCanonicalFile();
// Unless it's a test, check if the user is asking for a script in the scripts folder
// and not something outside Skript's domain.
if (TestMode.ENABLED || scriptFile.getCanonicalPath().startsWith(scriptsFolder.getCanonicalPath() + File.separator))
return scriptFile.getCanonicalFile();
return null;
} catch (IOException e) {
throw Skript.exception(e, "An exception occurred while trying to get the script file from the string '" + script + "'");
}
Expand All @@ -515,7 +520,7 @@ private static File toggleFile(File file, boolean enable) throws IOException {
false
);
}

private static Set<File> toggleFiles(File folder, boolean enable) throws IOException {
FileFilter filter = enable ? ScriptLoader.getDisabledScriptsFilter() : ScriptLoader.getLoadedScriptsFilter();

Expand All @@ -537,5 +542,5 @@ private static Set<File> toggleFiles(File folder, boolean enable) throws IOExcep

return changed;
}

}
113 changes: 95 additions & 18 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
*/
package ch.njol.skript.classes.data;

import java.io.File;
import java.io.StreamCorruptedException;
import java.util.Iterator;
import java.util.Locale;
import java.util.regex.Pattern;

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.SkriptCommand;
import ch.njol.skript.expressions.ExprScripts;
import ch.njol.skript.lang.function.DynamicFunctionReference;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -64,6 +69,9 @@
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.skriptlang.skript.util.Executable;

import java.util.Arrays;

Expand All @@ -73,7 +81,7 @@
@SuppressWarnings("rawtypes")
public class SkriptClasses {
public SkriptClasses() {}

static {
//noinspection unchecked
Classes.registerClass(new ClassInfo<>(ClassInfo.class, "classinfo")
Expand All @@ -96,17 +104,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 @@ -120,17 +128,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 @@ -141,20 +149,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 @@ -171,20 +179,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 @@ -626,7 +634,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 @@ -639,12 +647,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 @@ -680,7 +688,7 @@ public String toVariableNameString(VisualEffect e) {

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

Classes.registerClass(new ClassInfo<>(GameruleValue.class, "gamerulevalue")
.user("gamerule values?")
.name("Gamerule Value")
Expand All @@ -690,6 +698,75 @@ 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>() {

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

@Override
@Nullable
public Script parse(final String name, final ParseContext context) {
switch (context) {
case PARSE:
case COMMAND:
@Nullable File file = SkriptCommand.getScriptFromName(name);
if (file == null || !file.isFile())
return null;
return ScriptLoader.getScript(file);
default:
return 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 ExprScripts.SCRIPTS_PATH.relativize(file.toPath().toAbsolutePath()).toString();
}
}));

Classes.registerClass(new ClassInfo<>(Executable.class, "executable")
.user("executables?")
.name("Executable")
.description("Something that can be executed (run) and may accept arguments, e.g. a function.",
"This may also return a result.")
.usage("")
.examples("run {_function} with arguments 1 and true")
.since("INSERT VERSION"));

Classes.registerClass(new ClassInfo<>(DynamicFunctionReference.class, "function")
.user("functions?")
.name("Function")
.description("A function loaded by Skript.",
"This can be executed (with arguments) and may return a result.")
.usage("")
.examples("run {_function} with arguments 1 and true",
"set {_result} to the result of {_function}")
.since("INSERT VERSION"));
}

}
Loading