gpt4 book ai didi

lua - 需要名称中带有点的文件夹中的模块

转载 作者:行者123 更新时间:2023-12-04 19:35:35 25 4
gpt4 key购买 nike

我想从名称中包含点 (.) 的文件夹中获取文件:

"Folder.ai/test.lua"

如果文件夹名称中没有点,我将使用:
require(Folder.test)

当点在那里时我应该怎么做?

最佳答案

require使用加载器查找文件,您可以通过将函数插入 package.loaders 来添加自定义加载器.

您的自定义加载程序可能如下所示:

local function load(modulename)
local errmsg = ""
for path in string.gmatch(package.path, "([^;]+)") do
local filename = string.gsub(path, "%?", modulename)
local file = io.open(filename, "rb")
if file then
-- Compile and return the module
return assert(loadstring(assert(file:read("*a")), filename))
end
errmsg = errmsg.."\n\tno file '"..filename.."' (checked with custom loader)"
end
return errmsg
end

table.insert(package.loaders, 2, load) -- this will run before the standard loader, if you want it to
-- run after you can call table.insert(package.loaders, load)

Resource: http://lua-users.org/wiki/LuaModulesLoader

关于lua - 需要名称中带有点的文件夹中的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59592169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com