Skip to content

Commit

Permalink
feat: Native模式下更轻松的加载c语言动态链接库
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileYik committed Oct 23, 2022
1 parent 2bad993 commit a4c85d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public boolean loadPlugin(LuaPlugin plugin) {
}
pluginGlobals.pushObjectValue(plugin);
pluginGlobals.setGlobal("self");
setLuaRequireSearchPath(pluginGlobals, plugin);
// 环境生成完毕

try {
Expand Down Expand Up @@ -136,6 +137,28 @@ public boolean loadPlugin(LuaPlugin plugin) {
return true;
}

private void setLuaRequireSearchPath(LuaState luaState, LuaPlugin luaPlugin) throws LuaException, IOException {
File reallyCPath = new File(luaPlugin.getPluginPath(), "c_libraries");
File reallyLuaPath = new File(luaPlugin.getPluginPath(), "lua_libraries");

// create path
boolean flag =
(!reallyLuaPath.exists() || !reallyCPath.exists()) &&
reallyLuaPath.mkdirs() && reallyCPath.mkdirs();
// 添加依赖搜索路径
LuaObject luaObject = luaState.getLuaObject("package");
String cpath = luaObject.getField("cpath").getString();
String path = luaObject.getField("path").getString();
luaState.getGlobal("package");
luaState.pushString(
cpath + ";" + reallyCPath.toPath().toRealPath() + "/?.so");
luaState.setField(-2, "cpath");
luaState.pushString(
path + ";" + reallyLuaPath.toPath().toRealPath() + "/?.lua");
luaState.setField(-2, "path");
luaState.pop(1);
}

public void setLuaBukkit(LuaState luaState) {
LuaTableBuilder.getBuilder(new LuaEnvironmentOutside(luaState))
.put("pluginHelper", LuaPluginHelper.class) // 将被删除, 被helper替代
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ setting:run_mode("Hybrid")
-- lua_5_4: lua5.4版本
-- luajit_2_1_0_beta3: luajit2.1.0_beta3版本
-- 在Inside模式中无效
setting:native_version("lua5.4")
setting:native_version("lua_5_4")

0 comments on commit a4c85d0

Please sign in to comment.