summaryrefslogtreecommitdiff
path: root/src/module_list.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/module_list.lua')
-rw-r--r--src/module_list.lua35
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