7 -- |
7 -- |
8 |
8 |
9 |
9 |
10 local plugin_dir = CFG_PLUGINDIR or "./plugins/"; |
10 local plugin_dir = CFG_PLUGINDIR or "./plugins/"; |
11 |
11 |
12 local io_open = io.open; |
12 local io_open, os_time = io.open, os.time; |
13 local loadstring = loadstring; |
13 local loadstring, pairs = loadstring, pairs; |
|
14 |
|
15 local datamanager = require "util.datamanager"; |
14 |
16 |
15 module "pluginloader" |
17 module "pluginloader" |
|
18 |
|
19 local function load_from_datastore(name) |
|
20 local content = datamanager.load(name, "*", "plugins"); |
|
21 if not content or not content[1] then return nil, "Resource not found"; end |
|
22 return content[1], name; |
|
23 end |
16 |
24 |
17 local function load_file(name) |
25 local function load_file(name) |
18 local file, err = io_open(plugin_dir..name); |
26 local file, err = io_open(plugin_dir..name); |
19 if not file then return file, err; end |
27 if not file then return file, err; end |
20 local content = file:read("*a"); |
28 local content = file:read("*a"); |
21 file:close(); |
29 file:close(); |
22 return content, name; |
30 return content, name; |
23 end |
31 end |
24 |
32 |
25 function load_resource(plugin, resource) |
33 function load_resource(plugin, resource, loader) |
26 if not resource then |
34 if not resource then |
27 resource = "mod_"..plugin..".lua"; |
35 resource = "mod_"..plugin..".lua"; |
28 end |
36 end |
29 local content, err = load_file(plugin.."/"..resource); |
37 loader = loader or load_file; |
30 if not content then content, err = load_file(resource); end |
38 |
|
39 local content, err = loader(plugin.."/"..resource); |
|
40 if not content then content, err = loader(resource); end |
31 -- TODO add support for packed plugins |
41 -- TODO add support for packed plugins |
|
42 |
|
43 if not content and loader == load_file then |
|
44 return load_resource(plugin, resource, load_from_datastore); |
|
45 end |
|
46 |
32 return content, err; |
47 return content, err; |
|
48 end |
|
49 |
|
50 function store_resource(plugin, resource, content, metadata) |
|
51 if not resource then |
|
52 resource = "mod_"..plugin..".lua"; |
|
53 end |
|
54 local store = { content }; |
|
55 if metadata then |
|
56 for k,v in pairs(metadata) do |
|
57 store[k] = v; |
|
58 end |
|
59 end |
|
60 datamanager.store(plugin.."/"..resource, "*", "plugins", store); |
33 end |
61 end |
34 |
62 |
35 function load_code(plugin, resource) |
63 function load_code(plugin, resource) |
36 local content, err = load_resource(plugin, resource); |
64 local content, err = load_resource(plugin, resource); |
37 if not content then return content, err; end |
65 if not content then return content, err; end |