-
Notifications
You must be signed in to change notification settings - Fork 7
/
hotfix.lua
executable file
·418 lines (345 loc) · 11 KB
/
hotfix.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
local reloader = {}
local fixfname_tb
local locals
local collect_uv
local format = string.format
local function printE( ... )
print("[fixhot] ", ...)
end
local weak = {__mode = "kv"}
local catch_package = setmetatable({}, weak)
local function tsplit(str, reps)
local result = {}
string.gsub(str,'[^'..reps..']+', function(w)
table.insert(result, w)
end)
return result
end
local dump_env = {}
for k,v in pairs(_ENV) do
dump_env[k] = v
end
local function prev_loader_module( )
for k,v in pairs(package.loaded) do
catch_package[v] = k
end
end
local function findloader( name, env, extra )
local f = io.open(name, "r")
assert(f, name .. " file is not exit")
local str = f:read("*a")
f:close()
if extra then
str = extra .. str
end
return str, load(str, name, "bt", env)
end
local function getupvalues(f, uvf, uvt, unique, fname)
local i = 1
while true do
local name, value = debug.getupvalue(f, i)
if name == nil then
return
end
if name ~= "_ENV" and not dump_env[name] then
local t = type(value)
if not unique[name] then
unique[name] = true
if t == "function" and not uvf[name] then
uvf[name] = {func = {{f, i}}, value = value}
getupvalues(value, uvf, uvt, unique, name)
elseif t == "table" and not uvt[name] then
uvt[name] = {func = {{f, i}}, value = value}
if not catch_package[value] then
collect_uv(name, value, uvf, uvt, false)
end
else
-- value is basic type, and save it in uvt
-- like local a = 12
if not uvt[name] then
uvt[name] = {func = {{f, i}}}
end
end
else
if t == "function" then
-- 当一个模块中出现两个相同函数名时,看看是否存在二义性,是否唯一
if uvf[name].value and reloader.ambiguity then
if uvf[name].value ~= value then
error(format("%s ambiguity reference(%s) now value: %s, old value: %s",
fname or "", name, value, uvf[name].value))
end
end
local func = uvf[name].func
func[#func + 1] = {f, i}
elseif t == "table" then
local func = uvt[name].func
if func then
func[#func + 1] = {f, i}
end
end
end
end
i = i + 1
end
end
collect_uv = function( tname, tb, uvf, uvt, ismodule )
assert(tname)
local unique = {}
if ismodule and not uvt[tname] then
uvt[tname] = {value = tb}
end
for k,v in pairs(tb) do
if type(v) == "function" then
local key = format("%s.%s", tname, k)
uvf[key] = {func = {}, value = v}
getupvalues(v, uvf, uvt, unique, k)
end
end
-- add metatable support
local mt = getmetatable(tb) or {}
local mtbase = mt.__index
if mtbase and type(mtbase) == "table" then
collect_uv(tname, mtbase, uvf, uvt, ismodule)
end
end
local function push_new_func_upvalues(of, ot, f)
local i = 1
while true do
local name, value = debug.getupvalue(f, i)
if name == nil then
return
end
if not value then
local detail = of[name] or ot[name]
if detail then
local func = detail.func
if func then
debug.upvaluejoin(f, i, func[1][1], func[1][2])
elseif ot[name].value then
debug.setupvalue(f, i, detail.value)
end
else
error(format("Its value(%s) could not be found in the old module", name))
end
else
if type(value) == "function" then
push_new_func_upvalues(of, ot, value)
end
end
i = i + 1
end
end
local function patch_local_func(of, ot, fname, f)
local function tmpf( )
local up = f
end
local detail = of[fname] or ot[fname]
if detail then
local func = detail.func
if func then
for k=1, #func do
debug.upvaluejoin(func[k][1], func[k][2], tmpf, 1)
end
end
end
end
local function is_table_func(fname)
if string.find(fname, "%.") then
return true
end
return false
end
local function patch_func(t, k, f)
local of = t.__of
local ot = t.__ot
local check = t.__check
assert(type(f) == "function", format("hotfix %s failed, it's type is not function", k))
if of[k] then
push_new_func_upvalues(of, ot, f)
if not check then
patch_local_func(of, ot, k, f)
end
if is_table_func(k) then
local keys = tsplit(k, ".")
local tb_func = ot[keys[1]]
local vt = tb_func.value
assert(vt, format("1. hotfix failed, invalid function %s", k))
if not check then
vt[keys[2]] = f
end
end
else
if not is_table_func(k) then -- 方法名不能是一个表里面的方法
local vt = ot.__module.value
if vt[k] then
push_new_func_upvalues(of, ot, f)
if not check then
patch_local_func(of, ot, k, f)
vt[k] = f
end
else
error("2, hotfix failed, invalid function " .. k)
end
else
error("3. hotfix failed, invalid function " .. k)
end
end
end
local function load_new_module(module, uvf, uvt, check)
local _env = _ENV
local function global_write(t, k, v)
local oldv = _env[k]
if oldv then
if type(v) == "function" then
push_new_func_upvalues(uvf, uvt, v)
end
if not check then
_env[k] = v
end
else
error("forbid added a new global function " .. k)
end
end
local _u = {__of = uvf, __ot = uvt, __check = check}
local _U = setmetatable(_u, {__newindex = patch_func})
local function hotfix_func(fname, func)
patch_func(_U, fname, func)
end
local global_mt = {__index = _ENV, __newindex = global_write}
local env = setmetatable({FIX = _U, fix = hotfix_func}, global_mt) -- 读写全局变量,直接作用在旧的模块中,提高效率
local _, func, err = findloader(module, env)
if func then
ok, err = pcall(func)
_U.__of = nil
_U.__ot = nil
setmetatable(_U, nil)
global_mt.__newindex = _ENV
assert(ok, format("error: pcall new module(%s) failed: %s", module, err))
else
error(format("load file %s error: %s", module, err))
end
end
local function cache_func_name(t, fname, f)
assert(type(f) == "function")
local of = t.__of
local ot = t.__ot
local function collect_locals(func)
if not func then
return
end
local i = 1
while true do
local name, value = debug.getupvalue(func, i)
if name == nil then
return
end
if name ~= "_ENV" and not dump_env[name] then
if not locals[name] then
locals[name] = true
locals[#locals + 1] = format("local %s\n", name)
end
end
if type(value) == "function" then
if not locals[name] then
locals[name] = true
collect_locals(value)
end
end
i = i + 1
end
end
if of[fname] and of[fname].value then
local func = of[fname].value
collect_locals(func)
end
if not ot.__module then
return
end
local mf = ot.__module.value[fname]
if mf and type(mf) == "function" then
collect_locals(mf)
end
end
local function wrap_locals(module, content)
local file = io.open(module, "r+")
local gsub = string.gsub
local remove = table.remove
local insert = table.insert
if not file then
error("can not read the file: " .. module)
end
if #locals > 0 then
local lose = {}
for i=1, #locals do
local v = locals[i]
gsub(content, v, function (has)
if has then
insert(lose, locals[i])
end
end)
end
for i=1, #lose do
local v = lose[i]
for j=1,#locals do
if locals[j] == v then
remove(locals, j)
break
end
end
end
local extra = table.concat(locals, "")
content = extra .. content
end
if #locals > 0 then
io.output(file)
io.write(content)
end
io.close(file)
end
local function _init(module, uvf, uvt)
fixfname_tb = {}
locals = {}
local _u = {__of = uvf, __ot = uvt}
local _U = setmetatable(_u, {__newindex = cache_func_name})
local function hotfix_func(fname, func)
cache_func_name(_U, fname, func)
end
local c, f, err = findloader(module, {FIX = _U, fix = hotfix_func})
assert(f, err)
local ok, err = pcall(f)
assert(ok, err)
wrap_locals(module, c)
locals = nil
end
local function reload_module( list, check )
local ok, err
for _, info in ipairs(list) do
local oldmodule = info[1]
local newpath = info[2]
local uvf, uvt = {}, {}
if oldmodule and newpath then
assert(type(oldmodule) == "table")
collect_uv("__module", oldmodule, uvf, uvt, true)
if check then
_init(newpath, uvf, uvt)
end
load_new_module(newpath, uvf, uvt, check)
end
end
end
function reloader.module( list )
prev_loader_module()
local ok, err = pcall(reload_module, list, true)
if ok then
ok, err = pcall(reload_module, list)
if not ok then
printE("reloader.module failed:", err)
end
else
printE("reloader.module error:", err)
end
collectgarbage("collect")
end
-- check function is unique
reloader.ambiguity = true
return reloader