diff options
author | Christian Pointner <equinox@spreadspace.org> | 2010-11-17 00:03:11 +0000 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2010-11-17 00:03:11 +0000 |
commit | 23545618fba2a79ebc9a9b96a12b5560af13c410 (patch) | |
tree | 45dfd2dc7b2b178f6ecea754eb866228b6493ae4 | |
parent | module loader added (diff) |
module list load module implicit using metatables
git-svn-id: https://svn.spreadspace.org/gcsd/trunk@31 ac14a137-c7f1-4531-abe0-07747231d213
-rw-r--r-- | src/module_list.lua | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/module_list.lua b/src/module_list.lua index 990fe5a..b42f2ec 100644 --- a/src/module_list.lua +++ b/src/module_list.lua @@ -35,6 +35,23 @@ local defines = require("defines") module_list = {} module_list.classes = {} +local mt = {} +function mt.__index(table, key) + local old_path = package.path + package.path = "./modules/?.lua" + + local value = rawget(table, key) + if(value ~= nil) then return value end + + log.printf(log.DEBUG, "load module class: %s", key) + value = require(key) + rawset(table, key, value) + + package.path = old_path + return value +end +setmetatable(module_list.classes, mt) + module_list.inputs = {} module_list.output = nil @@ -45,7 +62,7 @@ function module_list:init(opt) for k, v in pairs(config) do log.printf(log.DEBUG, " config['%s'] = '%s'", k, v) end - self.output = self:load(class):new(config) + self.output = self.classes[class]:new(config) for idx, input in ipairs(opt.cmd_ins) do local class, config = self:parse_config(input) @@ -54,26 +71,12 @@ function module_list:init(opt) for k, v in pairs(config) do log.printf(log.DEBUG, " config['%s'] = '%s'", k, v) end - table.insert(self.inputs, self:load(class):new(config)) + table.insert(self.inputs, self.classes[class]:new(config)) end return defines.OK end -function module_list:load(class) - local old_path = package.path - package.path = "./modules/?.lua" - - if(self.classes[class] == nil) then - log.printf(log.DEBUG, "load module class: %s", class) - self.classes[class] = require(class) - end - - package.path = old_path - - return self.classes[class] -end - function module_list:parse_config(module_config) local class, config_string = string.match(module_config, "^([%w-_]+):(.+)$") if(not class or not config_string) then |