17 end |
17 end |
18 |
18 |
19 local io_open = io.open; |
19 local io_open = io.open; |
20 local envload = require "util.envload".envload; |
20 local envload = require "util.envload".envload; |
21 |
21 |
22 local function load_file(names) |
22 local pluginloader_methods = {}; |
|
23 local pluginloader_mt = { __index = pluginloader_methods }; |
|
24 |
|
25 function pluginloader_methods:load_file(names) |
23 local file, err, path; |
26 local file, err, path; |
24 for i=1,#plugin_dir do |
27 for i=1,#plugin_dir do |
25 for j=1,#names do |
28 for j=1,#names do |
26 path = plugin_dir[i]..names[j]; |
29 path = plugin_dir[i]..names[j]; |
27 file, err = io_open(path); |
30 file, err = io_open(path); |
33 end |
36 end |
34 end |
37 end |
35 return file, err; |
38 return file, err; |
36 end |
39 end |
37 |
40 |
38 local function load_resource(plugin, resource) |
41 function pluginloader_methods:load_resource(plugin, resource) |
39 resource = resource or "mod_"..plugin..".lua"; |
|
40 local names = { |
42 local names = { |
41 "mod_"..plugin..dir_sep..plugin..dir_sep..resource; -- mod_hello/hello/mod_hello.lua |
43 "mod_"..plugin..dir_sep..plugin..dir_sep..resource; -- mod_hello/hello/mod_hello.lua |
42 "mod_"..plugin..dir_sep..resource; -- mod_hello/mod_hello.lua |
44 "mod_"..plugin..dir_sep..resource; -- mod_hello/mod_hello.lua |
43 plugin..dir_sep..resource; -- hello/mod_hello.lua |
45 plugin..dir_sep..resource; -- hello/mod_hello.lua |
44 resource; -- mod_hello.lua |
46 resource; -- mod_hello.lua |
45 "share"..dir_sep.."lua"..dir_sep..lua_version..dir_sep..resource; |
47 "share"..dir_sep.."lua"..dir_sep..lua_version..dir_sep..resource; |
46 "share"..dir_sep.."lua"..dir_sep..lua_version..dir_sep.."mod_"..plugin..dir_sep..resource; |
48 "share"..dir_sep.."lua"..dir_sep..lua_version..dir_sep.."mod_"..plugin..dir_sep..resource; |
47 }; |
49 }; |
48 |
50 |
49 return load_file(names); |
51 return self:load_file(names); |
50 end |
52 end |
51 |
53 |
52 local function load_code(plugin, resource, env) |
54 function pluginloader_methods:load_code(plugin, resource, env) |
53 local content, err = load_resource(plugin, resource); |
55 local content, err = load_resource(plugin, resource); |
54 if not content then return content, err; end |
56 if not content then return content, err; end |
55 local path = err; |
57 local path = err; |
56 local f, err = envload(content, "@"..path, env); |
58 local f, err = envload(content, "@"..path, env); |
57 if not f then return f, err; end |
59 if not f then return f, err; end |
58 return f, path; |
60 return f, path; |
59 end |
61 end |
60 |
62 |
61 local function load_code_ext(plugin, resource, extension, env) |
63 function pluginloader_methods:load_code_ext(plugin, resource, extension, env) |
62 local content, err = load_resource(plugin, resource.."."..extension); |
64 local content, err = load_resource(plugin, resource.."."..extension); |
63 if not content and extension == "lib.lua" then |
65 if not content and extension == "lib.lua" then |
64 content, err = load_resource(plugin, resource..".lua"); |
66 content, err = load_resource(plugin, resource..".lua"); |
65 end |
67 end |
66 if not content then |
68 if not content then |
73 local f, err = envload(content, "@"..path, env); |
75 local f, err = envload(content, "@"..path, env); |
74 if not f then return f, err; end |
76 if not f then return f, err; end |
75 return f, path; |
77 return f, path; |
76 end |
78 end |
77 |
79 |
|
80 local function init(options) |
|
81 return setmetatable({ |
|
82 _options = options or {}; |
|
83 }, pluginloader_mt); |
|
84 end |
|
85 |
|
86 local function bind(self, method) |
|
87 return function (...) |
|
88 return method(self, ...); |
|
89 end; |
|
90 end |
|
91 |
|
92 local default_loader = init(); |
|
93 |
78 return { |
94 return { |
79 load_file = load_file; |
95 load_file = bind(default_loader, default_loader.load_file); |
80 load_resource = load_resource; |
96 load_resource = bind(default_loader, default_loader.load_resource); |
81 load_code = load_code; |
97 load_code = bind(default_loader, default_loader.load_code); |
82 load_code_ext = load_code_ext; |
98 load_code_ext = bind(default_loader, default_loader.load_code_ext); |
|
99 |
|
100 init = init; |
83 }; |
101 }; |