lua post钩函数 [英] lua post hooking a function

查看:107
本文介绍了lua post钩函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个与帖子挂接有关的主题,但是我认为这与我想要完成的事情不同. 找到的主题

I found one topic that's about post hooking, but I don't think it's the same thing as I want to accomplish. topic i found

我需要以下内容:

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),我希望someBoolean可以为真在整个功能范围内,但此后必须将其改回false

I need to ble able to run the code "someBoolean = false" after the return... (yes, I know that's not supposed to happen :p) considering aFunction may contain other functions itself, I want someBoolean to be true for the entire scope of aFunction, but after that, it HAS to be turned back to false

很抱歉,如果我没有足够好地解释它.复制粘贴我实际项目的相关代码会太大,我不想浪费您的时间. 我已经在这个问题上停留了一段时间了,但是我似乎无法弄清楚……

I'm sorry if I didn't manage to explain it well enough. Copy pasting the relevant code of my actual project would be way too large, and I don't want to waste your time. I've been stuck on this for a while now, and I just can't seem to figure it out...

(我不能只在函数后加上someBoolean = false,因为该函数实际上是代理表上的__index函数)

(edit: I can't just put someBoolean = false after the function, because the function is actually an __index function on a proxy table)

edit:相关的代码段.我希望这有点清楚

edit: relevent piece of code. I hope it's a bit clear

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

推荐答案

由于您需要返回一个函数(而不是评估一个函数),因此可以为aFunction创建一个代理,然后将其返回.这是它的工作方式(使用Nicol Bolas从解决方案中提取的一堆代码):

Since you need to return a function (rather than evaluate a function) you can create a proxy for the aFunction and return that instead. Here's how it could work (with a bunch of code taken from the solution by Nicol Bolas):

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

这篇关于lua post钩函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆