Engine 0.0.4
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
35 while application and type(application) == 'function' do
37 end
38
39 return application
40end
41
42local function package(self, module_name, module, custom)
43 local system = module_name:sub(1, 1) == '@'
44 local name = system and module_name:sub(2) or module_name
45 local should_install =
46
47 self.list_append(name)
48
49 if system then
50 self.stdlib_required[name] = true
51 end
52
53 self.pipeline[#self.pipeline + 1] = function ()
54 if not self.list_exist(name) then return end
55 if not system and not self.lib_required[name] then return end
56
57 local try_install = function()
58 module.install(self.std, self.game, self.application, custom, module_name)
59 end
60
61 if not pcall(try_install) then return end
62
63 if system then
64 self.stdlib_installed[name] = true
65 else
66 self.lib_installed[name] = true
67 end
68 end
69
70 return self
71end
72
73local function require(std, game, application)
74 local application_require = application.config and application.config.require or ''
75 local next_library = application_require:gmatch('%S+')
76 local self = {
77 std = std,
78 game = game,
80 list = {},
81 lib_optional = {},
82 lib_required = {},
83 lib_installed = {},
84 stdlib_required = {},
85 stdlib_installed = {},
86 pipeline = {},
87 package = package,
88 pipe = zeebo_pipeline.pipe
89 }
90
91 self.list_exist = function (name)
92 return self.lib_optional[name] or self.lib_required[name] or self.stdlib_required[name]
93 end
94 self.list_append = function (name)
95 if not self.list_exist(name) then
96 self.list[#self.list + 1] = name
97 end
98 end
99 self.run = function()
100 local index = 1
101 zeebo_pipeline.run(self)
102 while index <= #self.list do
103 local name = self.list[index]
104 if self.stdlib_required[name] and not self.stdlib_installed[name] then
105 error('system library not loaded: '..name)
106 end
107 if self.lib_required[name] and not self.lib_installed[name] then
108 error('library not loaded: '..name)
109 end
110 index = index + 1
111 end
112 end
113
114 repeat
115 local lib = next_library()
116 if lib then
117 local name, optional = lib:match('(%w+)([?]?)')
118 self.list_append(name)
119 if optional and #optional > 0 then
120 self.lib_optional[name] = true
121 else
122 self.lib_required[name] = true
123 end
124 end
125 until not lib
126
127 return self
128end
129
130local function install(std, game, application, exit_func)
131 std.game = std.game or {}
132 std.game.load = loadgame
133 return {load=loadgame}
134end
135
136local P = {
137 load={install=install},
138 loadgame = loadgame,
139 require = require
140}
141
142return P
local function require(std, game, application)
local function loadgame(game_file)
safe load game
local function package(self, module_name, module, custom)
local zeebo_pipeline
Definition module.lua:1
local function install(std, game, application, exit_func)
local application
Definition main.lua:16
local game
Definition main.lua:17
local std
Definition main.lua:18
function love load(args)