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