mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-01-23 05:12:53 +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
|
||||
serializer = new LuaSerializer();
|
||||
var ctx = (LuajContext) engine.getContext();
|
||||
|
||||
// Set engine to replace require as a temporary fix to missing scripts
|
||||
LuajContext ctx = (LuajContext) engine.getContext();
|
||||
ctx.globals.set(
|
||||
"require",
|
||||
new OneArgFunction() {
|
||||
@Override
|
||||
public LuaValue call(LuaValue arg0) {
|
||||
return LuaValue.ZERO;
|
||||
}
|
||||
});
|
||||
// Set the 'require' function handler.
|
||||
ctx.globals.set("require", new RequireFunction());
|
||||
|
||||
addEnumByIntValue(ctx, EntityType.values(), "EntityType");
|
||||
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) {
|
||||
var sc = tryGet(scriptsCache.get(path));
|
||||
if (sc.isPresent()) {
|
||||
|
Loading…
Reference in New Issue
Block a user