-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathhotfix_helper.lua
84 lines (74 loc) · 1.76 KB
/
hotfix_helper.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
--- Hotfix helper which hotfixes modified modules.
-- Using lfs to detect files' modification.
local M = { }
local lfs = require("lfs")
local hotfix = require("hotfix.hotfix")
-- Map file path to file time to detect modification.
local path_to_time = { }
-- global_objects which must not hotfix.
local global_objects = {
arg,
assert,
bit32,
collectgarbage,
coroutine,
debug,
dofile,
error,
getmetatable,
io,
ipairs,
lfs,
load,
loadfile,
loadstring,
math,
module,
next,
os,
package,
pairs,
pcall,
print,
rawequal,
rawget,
rawlen,
rawset,
require,
select,
setmetatable,
string,
table,
tonumber,
tostring,
type,
unpack,
utf8,
xpcall,
}
--- Check modules and hotfix.
function M.check()
local MOD_NAME = "hotfix_module_names"
if not package.searchpath(MOD_NAME, package.path) then return end
package.loaded[MOD_NAME] = nil -- always reload it
local module_names = require(MOD_NAME)
for _, module_name in pairs(module_names) do
local path, err = package.searchpath(module_name, package.path)
-- Skip non-exist module.
if not path then
print(string.format("No such module: %s. %s", module_name, err))
goto continue
end
local file_time = lfs.attributes (path, "modification")
if file_time == path_to_time[path] then goto continue end
print(string.format("Hot fix module %s (%s)", module_name, path))
path_to_time[path] = file_time
hotfix.hotfix_module(module_name)
::continue::
end -- for
end -- check()
function M.init()
hotfix.log_debug = function(s) print(s) end
hotfix.add_protect(global_objects)
end
return M