-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGameModule.lua
281 lines (244 loc) · 6.28 KB
/
GameModule.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
module( 'GameModule', package.seeall )
local GameModules = setmetatable( {}, { __no_traverse = true } )
local callbackModuleRelease = {}
local callbackModuleLoad = {}
function addGameModuleReleaseListener( func )
table.insert( callbackModuleRelease, func )
end
function addGameModuleLoadListener( func )
table.insert( callbackModuleLoad, func )
end
function registerGameModule( name, m )
GameModules[ name ] = m
end
function getGameModule( name )
return GameModules[ name ]
end
function hasGameModule( name )
return GameModules[ name ] ~= nil
end
function findGameModule( name )
for k, m in pairs( GameModules ) do
if k == name or k:endwith( name ) then
return m
end
end
return nil
end
--[[
create a envrionment
]]
local gameModulePaths = {}
function addGameModulePath( pattern )
table.insert( gameModulePaths, pattern )
end
function getGameModulePath()
return gameModulePaths
end
function clearGameModulePath()
gameModulePaths = {}
end
local gameModuleMap = {} --for runtime cache
function addGameModuleMapping( srcPath, dstPath )
gameModuleMap[ srcPath ] = dstPath
end
local function searchFile( path )
local mapped = gameModuleMap[ path ]
if mapped then
local fp = io.open( mapped, 'r' )
if fp then
fp:close()
return mapped
else
_warn( 'mapped script not found:', path, mapped )
end
end
path = path:gsub( '%.','/' )
for i, pattern in ipairs( gameModulePaths ) do
local f = pattern:gsub( '?', path )
local fp = io.open(f,'r')
if fp then fp:close() return f end
end
return nil
end
--------------------------------------------------------------------
local GameModuleMT = { __index = _G }
local _createEmptyModule, _loadGameModule, _requireGameModule
local _errorInfos = {}
local function flushErrorInfo()
local infos = _errorInfos
_errorInfos = {}
return infos
end
local function pushErrorInfo( data )
table.insert( _errorInfos, data )
end
local _require = require
function _createEmptyModule( path, fullpath )
local m = {
_NAME = path,
_PATH = path,
_SOURCE = fullpath,
__REQUIRES = {},
__REQUIREDBY = {}
}
m._M = m
local requireInModule = function( path, ... )
local loaded, errType, errMsg, tracebackMsg = _requireGameModule( path )
if loaded then
m.__REQUIRES[ path ] = true
loaded.__REQUIREDBY[ m._PATH ] = true
return loaded
end
if errType ~= 'notfound' then
-- print( errMsg )
-- if tracebackMsg then print( tracebackMsg ) end
error( 'error loading game module', 2 )
end
return _require( path, ... )
end
local importInModule = function( path, ... )
local result = requireInModule( path, ... )
if result then
for k, v in pairs( result ) do
if type(k) == 'string' and not rawget( m, k ) and not k:startwith('_') then
m[ k ] = v
end
end
end
return result
end
m.require = requireInModule
m.import = importInModule
return setmetatable( m, GameModuleMT )
end
--------------------------------------------------------------------
function _requireGameModule( path )
local m = GameModules[ path ]
if m then return m end
return _loadGameModule( path )
end
--------------------------------------------------------------------
function _loadGameModule( path )
local fullpath = searchFile( path )
if not fullpath then
return nil, 'notfound'
end
_stat( 'loading module from', fullpath )
local chunk, compileErr = loadfile( fullpath )
if not chunk then
pushErrorInfo{
path = path,
fullpath = fullpath,
errtype = 'compile',
msg = compileErr
}
print( 'failtocompile', compileErr )
return nil, 'failtocompile', compileErr
end
local m = _createEmptyModule( path, fullpath )
setfenv( chunk, m )
local errMsg, tracebackMsg
local function _onError( msg )
errMsg = msg
tracebackMsg = debug.traceback(2)
end
local ok = xpcall( chunk, _onError )
if ok then
registerGameModule( path, m )
for i, func in ipairs( callbackModuleLoad ) do
func( path, m )
end
return m
else
pushErrorInfo{
path = path,
fullpath = fullpath,
errtype = 'load',
msg = errMsg,
traceback = tracebackMsg
}
print( 'failtoload', errMsg )
return nil, 'failtoload', errMsg, tracebackMsg
end
end
--------------------------------------------------------------------
function loadGameModule( path, flushError )
local loaded, errType, errMsg, tracebackMsg = _requireGameModule( path )
if loaded then return loaded, {} end
if flushError then
return false, flushErrorInfo()
else
return false, _errorInfos
end
end
--------------------------------------------------------------------
function releaseGameModule( path, releasedModules )
releasedModules = releasedModules or {}
_stat( 'release game module', path )
local oldModule = GameModules[ path ]
if not oldModule then
_warn( 'can not release module', path )
return nil
end
releasedModules[ path ] = true
for i, func in ipairs( callbackModuleRelease ) do
func( path, oldModule )
end
GameModules[ path ] = nil
for requiredBy in pairs( oldModule.__REQUIREDBY ) do
releaseGameModule( requiredBy, releasedModules )
end
return oldModule
end
--------------------------------------------------------------------
function getGameModule( path )
return GameModules[ path ]
end
--------------------------------------------------------------------
function reloadGameModule( path )
flushErrorInfo()
--release
local releasedModules = {}
releaseGameModule( path, releasedModules )
--reload
for path1 in pairs( releasedModules ) do
loadGameModule( path1 )
end
local m = findGameModule( path )
return m, flushErrorInfo()
end
---------------------------------------------------------------------
function unloadGameModule( path )
flushErrorInfo()
local released = {}
releaseGameModule( path, released )
for path1 in pairs( released ) do
if path1 ~= path then
loadGameModule( path1 )
end
end
return flushErrorInfo()
end
--------------------------------------------------------------------
function compilePlainLua( inputFile, outputFile )
local f = loadfile( inputFile )
if f then
local str = string.dump( f )
local fp = io.open( outputFile, 'w' )
if fp then
fp:write( str )
fp:close()
return true
end
end
return false
end
--------------------------------------------------------------------
function getErrorInfo()
if #_errorInfos > 0 then
return _errorInfos
else
return nil
end
end