forked from asqbtcupid/lua_hotupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluahotupdate.lua
More file actions
314 lines (298 loc) · 9.55 KB
/
luahotupdate.lua
File metadata and controls
314 lines (298 loc) · 9.55 KB
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
local HU = {}
function HU.Init(UpdateListFile, RootPath, FailNotify, ENV)
HU.UpdateListFile = UpdateListFile
HU.HUMap = {}
HU.FileMap = {}
HU.NotifyFunc = FailNotify
HU.OldCode = {}
HU.ChangedFuncList = {}
HU.VisitedSig = {}
HU.FakeENV = nil
HU.ENV = ENV or _G
HU.InitFileMap(RootPath)
HU.FakeT = HU.InitFakeTable()
HU.InitProtection()
HU.ALL = false
end
function HU.FailNotify(...)
if HU.NotifyFunc then HU.NotifyFunc(...) end
end
function HU.DebugNofity(...)
if HU.DebugNofityFunc then HU.DebugNofityFunc(...) end
end
function HU.InitFileMap(RootPath)
for _, rootpath in pairs(RootPath) do
local file = io.popen("dir /S/B /A:A "..rootpath)
io.input(file)
for line in io.lines() do
local FileName = string.match(line,".*\\(.*)%.lua")
if FileName ~= nil then
if HU.FileMap[FileName] == nil then
HU.FileMap[FileName] = {}
end
local luapath = string.sub(line, #rootpath+2, #line-4)
luapath = string.gsub(luapath, "\\", ".")
table.insert(HU.FileMap[FileName], {SysPath = line, LuaPath = luapath})
end
end
file:close()
end
end
function HU.InitFakeTable()
local meta = {}
HU.Meta = meta
local function FakeT() return setmetatable({}, meta) end
local function EmptyFunc() end
local function pairs() return EmptyFunc end
local function setmetatable(t, metaT)
HU.MetaMap[t] = metaT
return t
end
function meta.__index(t, k)
if k == "setmetatable" then
return setmetatable
elseif k == "pairs" or k == "ipairs" then
return pairs
elseif k == "next" then
return EmptyFunc
else
return FakeT()
end
end
function meta.__newindex(t, k, v) rawset(t, k, v) end
function meta.__call() return FakeT(), FakeT(), FakeT() end
function meta.__add() return meta.__call() end
function meta.__sub() return meta.__call() end
function meta.__mul() return meta.__call() end
function meta.__div() return meta.__call() end
function meta.__mod() return meta.__call() end
function meta.__pow() return meta.__call() end
function meta.__unm() return meta.__call() end
function meta.__concat() return meta.__call() end
function meta.__eq() return meta.__call() end
function meta.__lt() return meta.__call() end
function meta.__le() return meta.__call() end
return FakeT
end
function HU.InitProtection()
HU.Protection = {}
HU.Protection[setmetatable] = true
HU.Protection[pairs] = true
HU.Protection[ipairs] = true
HU.Protection[next] = true
HU.Protection[HU] = true
HU.Protection[HU.Meta] = true
end
function HU.Update()
HU.AddFileFromHUList()
HU.HotUpdateCode()
end
function HU.AddFileFromHUList()
package.loaded[HU.UpdateListFile] = nil
local FileList = require (HU.UpdateListFile)
HU.ALL = false
HU.HUMap = {}
for _, file in pairs(FileList) do
if file == "_ALL_" then
HU.ALL = true
for k, v in pairs(HU.FileMap) do
for _, path in pairs(v) do
HU.HUMap[path.LuaPath] = path.SysPath
end
end
return
end
if HU.FileMap[file] then
for _, path in pairs(HU.FileMap[file]) do
HU.HUMap[path.LuaPath] = path.SysPath
end
else
HU.FailNotify("HotUpdate can't not find "..file)
end
end
end
function HU.HotUpdateCode()
for LuaPath, SysPath in pairs(HU.HUMap) do
local OldObject = package.loaded[LuaPath]
if OldObject ~= nil then
HU.VisitedSig = {}
HU.ChangedFuncList = {}
local Success, NewObject = HU.BuildNewCode(SysPath, LuaPath)
if Success then
if type(OldObject) == type(NewObject) then
if type(NewObject) == "table" then
HU.UpdateAllFunction(OldObject, NewObject, LuaPath, "Main", "")
elseif type(NewObject) == "function" then
HU.UpdateOneFunction(OldObject, NewObject, LuaPath, nil, "Main", "")
end
end
setmetatable(HU.FakeENV, nil)
HU.UpdateAllFunction(HU.ENV, HU.FakeENV, " ENV ", "Main", "")
if #HU.ChangedFuncList > 0 then
HU.Travel_G()
end
end
end
end
collectgarbage("collect")
end
function HU.BuildNewCode(FilePath, LuaPath)
io.input(FilePath)
local NewCode = io.read("*all")
if HU.ALL and HU.OldCode[FilePath] == nil then
HU.OldCode[FilePath] = NewCode
return
end
if HU.OldCode[FilePath] == NewCode then
io.input():close()
return false
end
HU.DebugNofity(FilePath)
io.input(FilePath)
local chunk = "--[["..LuaPath.."]] "
chunk = chunk..NewCode
io.input():close()
local NewFunction = loadstring(chunk)
if not NewFunction then
HU.FailNotify(FilePath.." has syntax error.")
return false
else
HU.FakeENV = HU.FakeT()
HU.MetaMap = {}
setfenv(NewFunction, HU.FakeENV)
local NewObject
HU.ErrorHappen = false
xpcall(function () NewObject = NewFunction() end, HU.ErrorHandle)
if not HU.ErrorHappen then
HU.OldCode[FilePath] = NewCode
return true, NewObject
else
return false
end
end
end
function HU.ErrorHandle(e)
HU.FailNotify("HotUpdate Error\n"..tostring(e))
HU.ErrorHappen = true
end
function HU.UpdateOneFunction(OldObject, NewObject, FuncName, OldTable, From, Deepth)
if HU.Protection[OldObject] or HU.Protection[NewObject] then return end
if OldObject == NewObject then return end
local signature = tostring(OldObject)..tostring(NewObject)
if HU.VisitedSig[signature] then return end
HU.VisitedSig[signature] = true
HU.DebugNofity(Deepth.."HU.UpdateOneFunction "..FuncName.." from:"..From)
if pcall(setfenv, NewObject, getfenv(OldObject)) then
HU.UpdateUpvalue(OldObject, NewObject, FuncName, "HU.UpdateOneFunction", Deepth.." ")
HU.ChangedFuncList[#HU.ChangedFuncList + 1] = {OldObject, NewObject, FuncName, OldTable}
end
end
function HU.UpdateAllFunction(OldTable, NewTable, Name, From, Deepth)
if HU.Protection[OldTable] or HU.Protection[NewTable] then return end
if OldTable == NewTable then return end
local signature = tostring(OldTable)..tostring(NewTable)
if HU.VisitedSig[signature] then return end
HU.VisitedSig[signature] = true
HU.DebugNofity(Deepth.."HU.UpdateAllFunction "..Name.." from:"..From)
for ElementName, Element in pairs(NewTable) do
local OldElement = OldTable[ElementName]
if type(Element) == type(OldElement) then
if type(Element) == "function" then
HU.UpdateOneFunction(OldElement, Element, ElementName, OldTable, "HU.UpdateAllFunction", Deepth.." ")
elseif type(Element) == "table" then
HU.UpdateAllFunction(OldElement, Element, ElementName, "HU.UpdateAllFunction", Deepth.." ")
end
elseif OldElement == nil and type(Element) == "function" then
OldTable[ElementName] = Element
setfenv(Element, HU.ENV)
end
end
local OldMeta = debug.getmetatable(OldTable)
local NewMeta = HU.MetaMap[NewTable]
if type(OldMeta) == "table" and type(NewMeta) == "table" then
HU.UpdateAllFunction(OldMeta, NewMeta, Name.."'s Meta", "HU.UpdateAllFunction", Deepth.." ")
end
end
function HU.ResetENV(object)
local visited = {}
local function f(object)
if visited[object] then return end
visited[object] = true
if type(object) == "function" then
setfenv(object, HU.ENV)
elseif type(object) == "table" then
for k, v in pairs(object) do
f(k);f(v)
end
end
end
f(object)
end
function HU.UpdateUpvalue(OldFunction, NewFunction, Name, From, Deepth)
HU.DebugNofity(Deepth.."HU.UpdateUpvalue", Name, " from:"..From)
local OldUpvalueMap = {}
local OldExistName = {}
for i = 1, math.huge do
local name, value = debug.getupvalue(OldFunction, i)
if not name then break end
OldUpvalueMap[name] = value
OldExistName[name] = true
end
for i = 1, math.huge do
local name, value = debug.getupvalue(NewFunction, i)
if not name then break end
if OldExistName[name] then
local OldValue = OldUpvalueMap[name]
if type(OldValue) ~= type(value) then
debug.setupvalue(NewFunction, i, OldValue)
elseif type(OldValue) == "function" then
HU.UpdateOneFunction(OldValue, value, name, nil, "HU.UpdateUpvalue", Deepth.." ")
elseif type(OldValue) == "table" then
HU.UpdateAllFunction(OldValue, value, name, "HU.UpdateUpvalue", Deepth.." ")
debug.setupvalue(NewFunction, i, OldValue)
else
debug.setupvalue(NewFunction, i, OldValue)
end
else
HU.ResetENV(value)
end
end
end
function HU.Travel_G()
local visited = {}
visited[HU] = true
local function f(t)
if not t or visited[t] or HU.Protection[t] then return end
visited[t] = true
if type(t) == "function" then
for i = 1, math.huge do
local name, value = debug.getupvalue(t, i)
if not name then break end
f(value);f(debug.getmetatable(value))
end
elseif type(t) == "table" then
for k,v in pairs(t) do
f(k); f(debug.getmetatable(k)); f(v); f(debug.getmetatable(v))
if type(v) == "function" or type(k) == "function" then
for _, funcs in ipairs(HU.ChangedFuncList) do
if v == funcs[1] then t[k] = funcs[2] end
if k == funcs[1] then t[funcs[2]] = t[k]; t[k] = nil end
end
end
end
end
end
f(_G)
local registryTable = debug.getregistry()
for _, funcs in ipairs(HU.ChangedFuncList) do
for k, v in pairs(registryTable) do
if v == funcs[1] then
registryTable[k] = funcs[2]
end
end
end
for _, funcs in ipairs(HU.ChangedFuncList) do
if funcs[3] == "HUDebug" then funcs[4]:HUDebug() end
end
end
return HU