Semi-functional require handler implementation

This commit is contained in:
KingRainbow44 2023-08-13 18:19:42 -04:00
parent 0175e207af
commit e7410a0be5
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE

View File

@ -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()) {