如何从 C# 调用具有“require”的 Lua 脚本中的 Lua 函数?

发布于 2025-01-06 21:51:26 字数 2291 浏览 2 评论 0原文

我有一个使用 LuaInterface 执行 Lua 脚本的 C# 程序。到目前为止,它运行良好,除非我的 Lua 脚本需要特定的包,例如 LuaXML。

我想将 XML 字符串从 C# 发送到 Lua 函数。

这是 XML,保存在 C:\temp 中:

<?xml version="1.0" encoding="utf-16" ?>
<library id="101">
    <book id="10" author="Balzac" title="Le Père Goriot"></book>
    <book id="20" quantity="Stendhal" price="Le Rouge et le noir"></book>
</library>

这是 C# 代码:

        Lua lua = new Lua();

        XmlDocument xmlDocument = new XmlDocument();

        xmlDocument.Load(@"C:\temp\library.xml");

        lua.DoFile(@"C:\temp\myScript.lua");

        LuaFunction luaFunction = lua.GetFunction("transformXML");
        Object o = luaFunction.Call(xmlDocument.OuterXml); 

这是 Lua 脚本,保存在 C\temp 中:

require("LuaXML")

function transformXML(input)

x = xml.eval(input)

output = nil

local library = x:find("library")

return library[1].id

end


local s = '<?xml version="1.0" encoding="utf-16" ?><library id="101"><book id="10" author="Balzac" title="Le Père Goriot"></book><book id="20" quantity="Stendhal" price="Le Rouge et le noir"></book></library>'

print(transformXML(s))

这是我得到的错误:

C:\temp\myScript.lua:1: module 'LuaXML' not found:
no field package.preload['LuaXML']
no file '.\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\lua\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\lua\LuaXML\init.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\LuaXML.luac'
no file '.\LuaXML.dll'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML.dll'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\loadall.dll'

我是否必须将所有这些文件(.lua、. dll)手动到所有这些位置?

当我从 Lua 控制台执行 Lua 脚本时,它会起作用。它返回 10,即第一本书的 id

另外,如上所述,只要脚本中没有“require”,我就可以从 C# 调用 Lua 函数。不仅仅是 LuaXML 会抛出这种类型的异常。任何包裹都会。

我已经尝试过环境变量,但没有成功。然而,我对此不太擅长。

预先感谢您的帮助。

I have a C# program executing Lua scripts using the LuaInterface. So far, it is working well, unless my Lua script requires a specific package, like LuaXML.

I want to send an XML string from C# to a Lua function.

This is the XML, saved in C:\temp:

<?xml version="1.0" encoding="utf-16" ?>
<library id="101">
    <book id="10" author="Balzac" title="Le Père Goriot"></book>
    <book id="20" quantity="Stendhal" price="Le Rouge et le noir"></book>
</library>

This is the C# code:

        Lua lua = new Lua();

        XmlDocument xmlDocument = new XmlDocument();

        xmlDocument.Load(@"C:\temp\library.xml");

        lua.DoFile(@"C:\temp\myScript.lua");

        LuaFunction luaFunction = lua.GetFunction("transformXML");
        Object o = luaFunction.Call(xmlDocument.OuterXml); 

This is the Lua script, saved on C\temp:

require("LuaXML")

function transformXML(input)

x = xml.eval(input)

output = nil

local library = x:find("library")

return library[1].id

end


local s = '<?xml version="1.0" encoding="utf-16" ?><library id="101"><book id="10" author="Balzac" title="Le Père Goriot"></book><book id="20" quantity="Stendhal" price="Le Rouge et le noir"></book></library>'

print(transformXML(s))

This is the error I got:

C:\temp\myScript.lua:1: module 'LuaXML' not found:
no field package.preload['LuaXML']
no file '.\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\lua\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\lua\LuaXML\init.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\LuaXML.luac'
no file '.\LuaXML.dll'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML.dll'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\loadall.dll'

Do I have to put all these files (.lua, .dll) manually to all these locations?

The Lua script works when I execute it from the Lua console. It returns 10, the id of the first book.

Also, as mentionned, I was able to call Lua functions from C# as long as there is no 'require' in the script. It is not only LuaXML that throws that type of exception. Any package would.

I've played with the environment variables, but was not successful. However, I am not very good at that.

Thank you in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最近可好 2025-01-13 21:51:26

您必须将 LuaXML 共享库放入其中一个位置(查看输出,尤其是查找 DLL 的最后几行),以便 Lua require 可以找到它。或者,您可以更改 package.cpath 指向保存 LuaXML DLL 的位置。例子:

package.cpath = package.cpath .. ";./libs/?.dll"

You have to put the LuaXML shared library into one of those locations (see the output, especially the last lines which look for a DLL), so that Lua require can find it. Alternatively, you can change package.cpath to point where your LuaXML DLL is saved. Example:

package.cpath = package.cpath .. ";./libs/?.dll"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文