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

Fix function reloading #2835

Merged
merged 6 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 37 additions & 6 deletions src/main/java/ch/njol/skript/ScriptLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,6 @@ public static List<Config> loadStructures(File directory) {
String name = Skript.getInstance().getDataFolder().toPath().toAbsolutePath()
.resolve(Skript.SCRIPTSFOLDER).relativize(f.toPath().toAbsolutePath()).toString();
assert name != null;
Functions.clearFunctions(name); // Functions are still callable from other scripts
// We're just making it impossible to look them up
return loadStructure(new FileInputStream(f), name);
} catch (final IOException e) {
Skript.error("Could not load " + f.getName() + ": " + ExceptionUtils.toString(e));
Expand Down Expand Up @@ -948,10 +946,6 @@ private static ScriptInfo unloadScripts_(final File folder) {
*/
public static ScriptInfo unloadScript(final File script) {
final ScriptInfo r = unloadScript_(script);
String name = Skript.getInstance().getDataFolder().toPath().toAbsolutePath()
.resolve(Skript.SCRIPTSFOLDER).relativize(script.toPath().toAbsolutePath()).toString();
assert name != null;
Functions.clearFunctions(name);
Functions.validateFunctions();
return r;
}
Expand All @@ -964,11 +958,48 @@ private static ScriptInfo unloadScript_(final File script) {
}

loadedFiles.remove(script); // We just unloaded it, so...

// Clear functions, DO NOT validate them yet
// If unloading, our caller will do this immediately after we return
// However, if reloading, new version of this script is first loaded
String name = Skript.getInstance().getDataFolder().toPath().toAbsolutePath()
.resolve(Skript.SCRIPTSFOLDER).relativize(script.toPath().toAbsolutePath()).toString();
assert name != null;
Functions.clearFunctions(name);

return info; // Return how much we unloaded
}

return new ScriptInfo(); // Return that we unloaded literally nothing
}

/**
* Reloads a single script.
* @param script Script file.
* @return Statistics of the newly loaded script.
*/
public static ScriptInfo reloadScript(File script) {
if (!isAsync()) {
unloadScript_(script);
}
Config configs = loadStructure(script);
Functions.validateFunctions();
return loadScripts(configs);
}

/**
* Reloads all scripts in the given folder and its subfolders.
* @param folder A folder.
* @return Statistics of newly loaded scripts.
*/
public static ScriptInfo reloadScripts(File folder) {
if (!isAsync()) {
unloadScripts_(folder);
}
List<Config> configs = loadStructures(folder);
Functions.validateFunctions();
return loadScripts(configs);
}

/**
* Replaces options in a string.
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/ch/njol/skript/SkriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,11 @@ public boolean onCommand(final @Nullable CommandSender sender, final @Nullable C
return true;
}
reloading(sender, "script", f.getName());
if (!ScriptLoader.loadAsync)
ScriptLoader.unloadScript(f);
Config config = ScriptLoader.loadStructure(f);
ScriptLoader.loadScripts(config);
ScriptLoader.reloadScript(f);
reloaded(sender, r, "script", f.getName());
} else {
reloading(sender, "scripts in folder", f.getName());
if (!ScriptLoader.loadAsync)
ScriptLoader.unloadScripts(f);
List<Config> configs = ScriptLoader.loadStructures(f);
final int enabled = ScriptLoader.loadScripts(configs).files;
final int enabled = ScriptLoader.reloadScripts(f).files;
if (enabled == 0)
info(sender, "reload.empty folder", f.getName());
else
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/ch/njol/skript/effects/EffScriptFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ protected void execute(Event e) {
return;
}

if (!ScriptLoader.isAsync())
ScriptLoader.unloadScript(f);
Config config = ScriptLoader.loadStructure(f);
ScriptLoader.loadScripts(config);
ScriptLoader.reloadScripts(f);
break;
}
case DISABLE: {
Expand Down