2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/apps/reader/pluginloader.lua

48 lines
1.5 KiB
Lua
Raw Normal View History

2014-08-14 11:49:42 +00:00
local lfs = require("libs/libkoreader-lfs")
local DEBUG = require("dbg")
local PluginLoader = {
plugin_path = "plugins"
}
function PluginLoader:loadPlugins()
if self.plugins then return self.plugins end
self.plugins = {}
for f in lfs.dir(self.plugin_path) do
local path = self.plugin_path.."/"..f
local mode = lfs.attributes(path, "mode")
-- valid koreader plugin directory
if mode == "directory" and f:find(".+%.koplugin$") then
local mainfile = path.."/".."main.lua"
local package_path = package.path
local package_cpath = package.cpath
package.path = path.."/?.lua;"..package.path
package.cpath = path.."/lib/?.so;"..package.cpath
2014-06-23 14:11:12 +00:00
local ok, module = pcall(dofile, mainfile)
if not ok then
DEBUG("Error when loading", mainfile, module)
end
package.path = package_path
package.cpath = package_cpath
if ok then
module.path = path
2014-10-09 04:38:33 +00:00
module.name = module.name or path:match("/(.-)%.koplugin")
table.insert(self.plugins, module)
end
end
end
for _,plugin in ipairs(self.plugins) do
package.path = package.path..";"..plugin.path.."/?.lua"
package.cpath = package.cpath..";"..plugin.path.."/lib/?.so"
end
2014-06-23 14:11:12 +00:00
table.sort(self.plugins, function(v1,v2) return v1.path < v2.path end)
return self.plugins
end
return PluginLoader