Engine 0.0.6
Game engine in lua
Loading...
Searching...
No Matches
module.lua
Go to the documentation of this file.
1local zeebo_pipeline = require('src/lib/util/pipeline')
2
3--! @short safe load game
4--! @brief search by game in filesystem / lua modules
5--! @li https://love2d.org/wiki/love.filesystem.getSource
6local function loadgame(game_file)
7 if type(game_file) == 'table' then
8 return game_file
9 end
10
11 local cwd = '.'
12 local application = type(game_file) == 'function' and game_file
13 local game_title = game_file and game_file:gsub('%.lua$', '') or 'game'
14
15
16 if not application and game_file and game_file:find('\n') then
17 local ok, app = pcall(load, game_file)
18 if not ok then
19 ok, app = pcall(loadstring, game_file)
20 end
21 application = ok and app
22 else
23 if love and love.filesystem and love.filesystem.getSource then
24 cwd = love.filesystem.getSource()
25 end
26 if not application then
27 application = loadfile(cwd..'/'..game_title..'.lua')
28 end
29 if not application then
30 local ok, app = pcall(require, game_title)
31 application = ok and app
32 end
33 end
34 if not application and io and io.open then
35 local app_file = io.open(game_file)
36 if app_file then
37 local app_src = app_file:read('*a')
38 local ok, app = pcall(load, app_src)
39 if not ok then
40 ok, app = pcall(loadstring, app_src)
41 end
42 application = ok and app
43 app_file:close()
44 end
45 end
46
47 while application and type(application) == 'function' do
49 end
50
51 return application
52end
53
54local function register(self, register_func)
55 local listener_func = function(event_name)
56 local filtered_events = {}
57
58 do
59 local index = 1
60 while index <= #self.event do
61 local event = self.event[index][event_name]
62 if event then
63 filtered_events[#filtered_events + 1] = event
64 end
65 index = index + 1
66 end
67 end
68
69 return function(a, b, c, d, e, f)
70 local index = 1
71 while index <= #filtered_events do
72 filtered_events[index](self.std, self.game, self.application, a, b, c, d, e, f)
73 index = index + 1
74 end
75 end
76 end
77
78 self.pipeline[#self.pipeline + 1] = function()
79 register_func(listener_func)
80 end
81
82 return self
83end
84
85local function package(self, module_name, module, custom)
86 local system = module_name:sub(1, 1) == '@'
87 local name = system and module_name:sub(2) or module_name
88
89 if system then
90 self.stdlib_required[name] = true
91 end
92
93 self.pipeline[#self.pipeline + 1] = function ()
94 if not self.list_exist(name) then return end
95 if not system and not self.lib_required[name] then return end
96
97 local try_install = function()
98 local m = module.install(self.std, self.game, self.application, custom, module_name)
99 if m.event then
100 self.event[#self.event + 1] = m.event
101 end
102 end
103
104 if not pcall(try_install) then return end
105
106 if system then
107 self.stdlib_installed[name] = true
108 else
109 self.lib_installed[name] = true
110 end
111 end
112
113 return self
114end
115
116local function require(std, game, application)
117 local application_require = application.config and application.config.require or ''
118 local next_library = application_require:gmatch('%S+')
119 local self = {
120 -- objects
121 std=std,
122 game=game,
124 -- methods
125 register = register,
126 package = package,
127 -- data
128 event = {},
129 list = {},
130 lib_optional = {},
131 lib_required = {},
132 lib_installed = {},
133 stdlib_required = {},
134 stdlib_installed = {},
135 -- internal
136 pipeline = {},
137 pipe = zeebo_pipeline.pipe
138 }
139
140 self.list_exist = function (name)
141 return self.lib_optional[name] or self.lib_required[name] or self.stdlib_required[name]
142 end
143 self.list_append = function (name)
144 if not self.list_exist(name) then
145 self.list[#self.list + 1] = name
146 end
147 end
148 self.run = function()
149 local index = 1
150 zeebo_pipeline.run(self)
151 while index <= #self.list do
152 local name = self.list[index]
153 if self.stdlib_required[name] and not self.stdlib_installed[name] then
154 error('system library not loaded: '..name)
155 end
156 if self.lib_required[name] and not self.lib_installed[name] then
157 error('library not loaded: '..name)
158 end
159 index = index + 1
160 end
161 end
162
163 repeat
164 local lib = next_library()
165 if lib then
166 local name, optional = lib:match('([%w%.]+)([?]?)')
167 self.list_append(name)
168 if optional and #optional > 0 then
169 self.lib_optional[name] = true
170 else
171 self.lib_required[name] = true
172 end
173 end
174 until not lib
175
176 return self
177end
178
179local function install(std, game, application, exit_func)
180 std.game = std.game or {}
181 std.game.load = loadgame
182 return {load=loadgame}
183end
184
185local P = {
186 load={install=install},
187 loadgame = loadgame,
188 require = require
189}
190
191return P
local function require(std, game, application)
local function loadgame(game_file)
safe load game
local function package(self, module_name, module, custom)
local function register(self, register_func)
local zeebo_pipeline
Definition module.lua:1
local function install(std, game, application, exit_func)
local ok
Definition main.lua:40
local application
Definition main.lua:16
local game
Definition main.lua:17
local std
Definition main.lua:18
local event
nclua:event
Definition main.lua:26
function love load(args)