Function lua连接函数的post
我发现了一个关于后钓的话题,但我认为这和我想完成的事情不一样 我需要的是:Function lua连接函数的post,function,lua,hook,Function,Lua,Hook,我发现了一个关于后钓的话题,但我认为这和我想完成的事情不一样 我需要的是: local someBoolean = false function doSomething() -- this is the function used in __index in a proxy table someBoolean = true return aFunction -- this is function that we can NOT alter end 我需要能够在返回后运行代码“someB
local someBoolean = false
function doSomething() -- this is the function used in __index in a proxy table
someBoolean = true
return aFunction -- this is function that we can NOT alter
end
我需要能够在返回后运行代码“someBoolean=false”。。。(是的,我知道这是不应该发生的:p)考虑到函数本身可能包含其他函数,我希望某个布尔值在函数的整个范围内都为true,但在那之后,它必须返回false
如果我解释得不够好,我很抱歉。复制粘贴我实际项目的相关代码会太大,我不想浪费你的时间。
我已经在这个问题上纠缠了一段时间了,我似乎无法理解
(编辑:我不能将someBoolean=false放在函数后面,因为该函数实际上是代理表上的_索引函数)
编辑:相关的代码段。我希望有点清楚
local function objectProxyDelegate(t, key)
if not done then -- done = true when our object is fully initialised
return cls[key] -- cls is the class, newinst is the new instance (duh...)
end
print("trying to delegate " .. key)
if accessTable.public[key] then
print(key .. " passed the test")
objectScope = true
if accessTable.static[key] then -- static function. return the static one
return cls[key] -- we need to somehow set objectScope back to false after this, otherwise we'll keep overriding protected/private functions
else
return newinst[key]
end
elseif objectScope then
print("overridden protected/private")
return cls[key]
end
if accessTable.private[key] then
error ("This function is not visible. (private)", 2)
elseif accessTable.protected[key] then
error ("This function is not visible to an instance. (protected)", 2)
else
error ("The function " .. key .. " doesn't exiist in " .. newinst:getType(), 2) -- den deze...
end
end
不能在return
语句之后执行代码。正确的答案是调用函数,捕捉返回值,设置变量,然后返回返回值。例如:
local someBoolean = false
function doSomething(...) -- this is the function used in __index in a proxy table
someBoolean = true
local rets = { aFunction(...) } -- this is function that we can NOT alter
someBoolean = false
return table.unpack(rets)
end
由于需要返回函数(而不是计算函数),因此可以为
a函数创建一个代理,然后返回该代理。下面是它的工作原理(使用Nicol Bolas解决方案中的一组代码):
考虑到它是一个_索引函数,这是行不通的,我应该更清楚一点抱歉:p它没有得到我们将要调用他的参数的函数。。。只是表格和钥匙我更新了我的第一篇文章,也许现在更清楚了一点好!我提出了类似的解决方案,但我的解决方案让我能够跟踪活动函数lol(将其存储在本地)您的解决方案非常干净:-)谢谢!
local someBoolean = false
function doSomething(...) -- this is the function used in __index in a proxy table
someBoolean = true
-- Return a proxy function instead of aFunction
return function(...)
local rets = { aFunction(...) }
someBoolean = false
return table.unpack(rets)
end
end