mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-02-02 19:32:58 +08:00
Semi-functional require
handler implementation
This commit is contained in:
parent
0175e207af
commit
e7410a0be5
@ -45,17 +45,10 @@ public class ScriptLoader {
|
|||||||
|
|
||||||
// Lua stuff
|
// Lua stuff
|
||||||
serializer = new LuaSerializer();
|
serializer = new LuaSerializer();
|
||||||
|
var ctx = (LuajContext) engine.getContext();
|
||||||
|
|
||||||
// Set engine to replace require as a temporary fix to missing scripts
|
// Set the 'require' function handler.
|
||||||
LuajContext ctx = (LuajContext) engine.getContext();
|
ctx.globals.set("require", new RequireFunction());
|
||||||
ctx.globals.set(
|
|
||||||
"require",
|
|
||||||
new OneArgFunction() {
|
|
||||||
@Override
|
|
||||||
public LuaValue call(LuaValue arg0) {
|
|
||||||
return LuaValue.ZERO;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
addEnumByIntValue(ctx, EntityType.values(), "EntityType");
|
addEnumByIntValue(ctx, EntityType.values(), "EntityType");
|
||||||
addEnumByIntValue(ctx, QuestState.values(), "QuestState");
|
addEnumByIntValue(ctx, QuestState.values(), "QuestState");
|
||||||
@ -112,6 +105,34 @@ public class ScriptLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static final class RequireFunction extends OneArgFunction {
|
||||||
|
@Override
|
||||||
|
public LuaValue call(LuaValue arg) {
|
||||||
|
// Resolve the script path.
|
||||||
|
var scriptName = arg.checkjstring();
|
||||||
|
var scriptPath = FileUtils.getScriptPath(
|
||||||
|
"Common/" + scriptName + ".lua");
|
||||||
|
|
||||||
|
// Load & compile the script.
|
||||||
|
var script = ScriptLoader.getScript(scriptPath.toString());
|
||||||
|
if (script == null) {
|
||||||
|
return LuaValue.NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append the script to the context.
|
||||||
|
try {
|
||||||
|
script.eval();
|
||||||
|
} catch (Exception exception) {
|
||||||
|
Grasscutter.getLogger()
|
||||||
|
.error("Loading script {} failed! - {}",
|
||||||
|
scriptPath, exception.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: What is the proper return value?
|
||||||
|
return LuaValue.NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static CompiledScript getScript(String path) {
|
public static CompiledScript getScript(String path) {
|
||||||
var sc = tryGet(scriptsCache.get(path));
|
var sc = tryGet(scriptsCache.get(path));
|
||||||
if (sc.isPresent()) {
|
if (sc.isPresent()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user