Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do
- local _MITM = {}
- local split = function(t, s)
- local a={}
- for i in string.gmatch(t, "[^" .. (s or "%s") .. "]+") do
- a[#a + 1] = i
- end
- return a
- end
- setmetatable(_G, {
- __index = function(self, index)
- if _MITM[index] then
- return function(...)
- for i, v in next, {"before", "default", "after"} do
- if _MITM[index][v] then
- if v == "default" then
- _MITM[index][v](...)
- else
- for o, a in next, _MITM[index][v] do
- a(...)
- end
- end
- end
- end
- end
- else
- rawget(self, index)
- end
- end,
- __newindex = function(self, index, value)
- if type(value) == "function" then
- if not _MITM[index] then
- _MITM[index] = {}
- end
- _MITM[index].default = value
- else
- rawset(self, index, value)
- end
- end,
- __metatable = nil
- });
- function injectFunction(positionType, objectToProxy, objectToReplace)
- if type(objectToProxy) == "string" then
- local arg = split(objectToProxy, '.')
- if #arg > 1 then
- local tb = _G
- for i = 1, #arg do
- if i == #arg then
- local old = tb[arg[i]]
- tb[arg[i]] = {}
- if not _MITM[objectToProxy] then
- _MITM[objectToProxy] = {}
- end
- _MITM[objectToProxy][positionType] = objectToReplace
- if not getmetatable(tb[arg[i]]) then
- setmetatable(tb[arg[i]], {
- __call = function(self, ...)
- for o, a in next, {"before", "default", "after"} do
- if a == "default" and positionType ~= "default" then
- old(...)
- elseif a == positionType then
- _MITM[objectToProxy][positionType](old, ...)
- end
- end
- end
- });
- end
- else
- tb = tb[arg[i]]
- end
- end
- elseif arg[1] then
- if not _MITM[arg[1]] then
- _MITM[arg[1]] = {}
- end
- if positionType ~= "default" and not _MITM[arg[1]][positionType] then
- _MITM[arg[1]][positionType] = {}
- end
- if positionType == "default" then
- _MITM[arg[1]][positionType] = objectToReplace
- else
- _MITM[arg[1]][positionType][#_MITM[arg[1]][positionType] + 1] = objectToReplace
- end
- end
- end
- end
- end
- -- Example
- function eventChatMessage(name, message)
- print(message)
- end
- injectFunction("after", "eventChatMessage", function()
- print("after!")
- end)
- injectFunction("default", "eventChatMessage", function()
- print("default event removed")
- end)
Add Comment
Please, Sign In to add comment