_Dispatch.lua 847 B

123456789101112131415161718192021222324252627
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by 無心道(15388152619).
  4. --- DateTime: 2024/11/1 15:06
  5. ---
  6. local dispatch_tab = {}
  7. --- 调用函数
  8. --- @param function_name string 函数名
  9. --- @param ... any 参数
  10. function dispatch(function_name, ...)
  11. --查找函数 通过load字符串的形式 动态编译 返回函数
  12. local chunk = "return " .. function_name
  13. if dispatch_tab[chunk] == nil then
  14. dispatch_tab[chunk] = load(chunk)()
  15. end
  16. local loadFunc = dispatch_tab[chunk]
  17. --调用函数
  18. local success, result = xpcall(loadFunc, debug.traceback, ...)
  19. if not success then
  20. local var = "[Error] dispatch func name [" .. function_name .. "] 参数:" .. gameDebug.toStrings(" ", ...)
  21. var = var .. "\n" .. result
  22. _LUA_Error(var)
  23. end
  24. return result
  25. end