2014-08-14 11:49:42 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2014-04-23 13:57:48 +00:00
|
|
|
|
|
|
|
local PluginLoader = {
|
2014-06-07 06:53:47 +00:00
|
|
|
plugin_path = "plugins"
|
2014-04-23 13:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-02-04 18:24:39 +00:00
|
|
|
local ok, plugin_module = pcall(dofile, mainfile)
|
2016-12-27 10:03:11 +00:00
|
|
|
if not ok or not plugin_module then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.warn("Error when loading", mainfile, plugin_module)
|
2014-04-23 13:57:48 +00:00
|
|
|
end
|
2016-12-27 10:03:11 +00:00
|
|
|
if ok and plugin_module and not plugin_module.disabled then
|
2016-12-04 23:42:22 +00:00
|
|
|
package.path = package_path
|
|
|
|
package.cpath = package_cpath
|
2016-02-04 18:24:39 +00:00
|
|
|
plugin_module.path = path
|
|
|
|
plugin_module.name = plugin_module.name or path:match("/(.-)%.koplugin")
|
|
|
|
table.insert(self.plugins, plugin_module)
|
2014-04-23 13:57:48 +00:00
|
|
|
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)
|
|
|
|
|
2014-04-23 13:57:48 +00:00
|
|
|
return self.plugins
|
|
|
|
end
|
|
|
|
|
|
|
|
return PluginLoader
|