2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
12 local io_open, os_time = io.open, os.time;
13 local loadstring, pairs = loadstring, pairs;
15 local datamanager = require "util.datamanager";
19 local function load_from_datastore(name)
20 local content = datamanager.load(name, nil, "plugins");
21 if not content or not content[1] then return nil, "Resource not found"; end
22 return content[1], name;
25 local function load_file(name)
26 local file, err = io_open(plugin_dir..name);
27 if not file then return file, err; end
28 local content = file:read("*a");
33 function load_resource(plugin, resource, loader)
35 resource = "mod_"..plugin..".lua";
37 loader = loader or load_file;
39 local content, err = loader(plugin.."/"..resource);
40 if not content then content, err = loader(resource); end
41 -- TODO add support for packed plugins
43 if not content and loader == load_file then
44 return load_resource(plugin, resource, load_from_datastore);
50 function store_resource(plugin, resource, content, metadata)
52 resource = "mod_"..plugin..".lua";
54 local store = { content };
56 for k,v in pairs(metadata) do
60 datamanager.store(plugin.."/"..resource, nil, "plugins", store);
63 function load_code(plugin, resource)
64 local content, err = load_resource(plugin, resource);
65 if not content then return content, err; end
66 return loadstring(content, "@"..err);