]> git.lizzy.rs Git - dragonblocks3d-lua.git/blob - src/module_manager.lua
Added task manager and window
[dragonblocks3d-lua.git] / src / module_manager.lua
1 local module_ref = {}
2
3 function module_ref:preinit()
4         self._dependencies = {}
5         self._started = false
6         local depfile = io.open(self._path .. "/dependencies.txt")
7         if depfile then
8                 local data = depfile:read()
9                 depfile:close()
10                 self._dependencies = data:split("\n")
11         end
12         
13 end
14
15 function module_ref:init()
16         self._started = true
17 end
18
19 function module_ref:run_script(s)
20         return require(self._path .. "src/" .. s)
21 end
22
23 function module_ref:start()
24         _G[self._name] = self
25         self:run_script("init")
26         print("Started module " .. self._name)
27 end
28
29 function module_ref:get_path()
30         return self._path
31 end
32
33 function module_ref:get_data_path()
34         local p = self._data_path
35         if not lfs.attributes(p, "mode") then
36                 lfs.mkdir(p)
37         end
38 end
39
40 local module_manager = {}
41
42 module_manager.module_path = "modules/"
43 module_manager.data_path = "data/"
44
45 function module_manager:init()
46         if not lfs.attributes(self.data_path, "mode") then
47                 lfs.mkdir(self.data_path)
48         end
49         self._modules = {}
50         for modulename in lfs.dir(self.module_path) do
51                 if modulename:sub(1, 1) ~= "." then
52                         local m = ObjectMgr.create()
53                         m._name = modulename
54                         m._path = self.module_path .. modulename .. "/"
55                         m._data_path = self.data_path .. modulename .. "/"
56                         m:add_proto(module_ref)
57                         m:preinit()
58                         self._modules[modulename] = m
59                 end
60         end
61 end
62
63 function module_manager:start_module(name)
64         local m = self._modules[name]
65         if not m then
66                 error("Module '" .. name .. "' not found.")
67         elseif m._started then
68                 return
69         end
70         for _, dep in ipairs(m._dependencies) do
71                 self:start_module(dep)
72         end
73         m:start()
74 end
75
76 return module_manager