Annotate

util/pluginloader.lua @ 1359:015d624a2a71

util.pluginloader: Initial commit - a plugin resource loader
author Waqas Hussain <waqas20@gmail.com>
date Sun, 14 Jun 2009 21:01:30 +0500
child 1387:64f18488b275
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1359
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local io_open = io.open;
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local loadstring = loadstring;
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 module "pluginloader"
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local function load_file(name)
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local file, err = io_open(plugin_dir..name);
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 if not file then return file, err; end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local content = file:read("*a");
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 file:close();
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 return content, name;
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 function load_resource(plugin, resource)
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 if not resource then
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 resource = "mod_"..plugin..".lua";
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local content, err = load_file(plugin.."/"..resource);
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 if not content then content, err = load_file(resource); end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 -- TODO add support for packed plugins
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 return content, err;
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 function load_code(plugin, resource)
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 local content, err = load_resource(plugin, resource);
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 if not content then return content, err; end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 return loadstring(content, err), err;
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32
015d624a2a71 util.pluginloader: Initial commit - a plugin resource loader
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 return _M;