Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. SCRIPT ERROR: @vrp/modules/gui.lua:182: attempt to call a table value (local 'cb')
  2. > handler (local Tools = module("lib/Tools")
  3. local Debug = module("lib/Debug")
  4.  
  5. -- this file describes a two way proxy between the server and the clients (request system)
  6.  
  7. local Tunnel = {}
  8.  
  9. -- define per dest regulator
  10. Tunnel.delays = {}
  11.  
  12. -- set the base delay between Triggers for this destination in milliseconds (0 for instant trigger)
  13. function Tunnel.setDestDelay(dest, delay)
  14. Tunnel.delays[dest] = {delay, 0}
  15. end
  16.  
  17. local function tunnel_resolve(itable,key)
  18. local mtable = getmetatable(itable)
  19. local iname = mtable.name
  20. local ids = mtable.tunnel_ids
  21. local callbacks = mtable.tunnel_callbacks
  22. local identifier = mtable.identifier
  23.  
  24. -- generate access function
  25. local fcall = function(dest,args,callback)
  26. if args == nil then
  27. args = {}
  28. end
  29.  
  30. -- get delay data
  31. local delay_data = Tunnel.delays[dest]
  32. if delay_data == nil then
  33. delay_data = {0,0}
  34. end
  35.  
  36. -- increase delay
  37. local add_delay = delay_data[1]
  38. delay_data[2] = delay_data[2]+add_delay
  39.  
  40. if delay_data[2] > 0 then -- delay trigger
  41. SetTimeout(delay_data[2], function()
  42. -- remove added delay
  43. delay_data[2] = delay_data[2]-add_delay
  44.  
  45. -- send request
  46. if type(callback) == "function" then -- ref callback if exists (become a request)
  47. local rid = ids:gen()
  48. callbacks[rid] = callback
  49. TriggerClientEvent(iname..":tunnel_req",dest,key,args,identifier,rid)
  50. else -- regular trigger
  51. TriggerClientEvent(iname..":tunnel_req",dest,key,args,"",-1)
  52. end
  53. end)
  54. else -- no delay
  55. -- send request
  56. if type(callback) == "function" then -- ref callback if exists (become a request)
  57. local rid = ids:gen()
  58. callbacks[rid] = callback
  59. TriggerClientEvent(iname..":tunnel_req",dest,key,args,identifier,rid)
  60. else -- regular trigger
  61. TriggerClientEvent(iname..":tunnel_req",dest,key,args,"",-1)
  62. end
  63. end
  64. end
  65.  
  66. itable[key] = fcall -- add generated call to table (optimization)
  67. return fcall
  68. end
  69.  
  70. -- bind an interface (listen to net requests)
  71. -- name: interface name
  72. -- interface: table containing functions
  73. function Tunnel.bindInterface(name,interface)
  74. -- receive request
  75. RegisterServerEvent(name..":tunnel_req")
  76. AddEventHandler(name..":tunnel_req",function(member,args,identifier,rid)
  77. local source = source
  78. local delayed = false
  79.  
  80. if Debug.active then
  81. Debug.pbegin("tunnelreq#"..rid.."_"..name..":"..member.." "..json.encode(Debug.safeTableCopy(args)))
  82. end
  83.  
  84. local f = interface[member]
  85.  
  86. local rets = {}
  87. if type(f) == "function" then
  88. -- bind the global function to delay the return values using the returned function with args
  89. TUNNEL_DELAYED = function()
  90. delayed = true
  91. return function(rets)
  92. rets = rets or {}
  93.  
  94. if rid >= 0 then
  95. TriggerClientEvent(name..":"..identifier..":tunnel_res",source,rid,rets)
  96. end
  97. end
  98. end
  99.  
  100. rets = {f(table.unpack(args))} -- call function
  101. -- CancelEvent() -- cancel event doesn't seem to cancel the event for the other handlers, but if it does, uncomment this
  102. end
  103.  
  104. -- send response (even if the function doesn't exist)
  105. if not delayed and rid >= 0 then
  106. TriggerClientEvent(name..":"..identifier..":tunnel_res",source,rid,rets)
  107. end
  108.  
  109. if Debug.active then
  110. Debug.pend()
  111. end
  112. end)
  113. end
  114.  
  115. -- get a tunnel interface to send requests
  116. -- name: interface name
  117. -- identifier: unique string to identify this tunnel interface access (the name of the current resource should be fine)
  118. function Tunnel.getInterface(name,identifier)
  119. local ids = Tools.newIDGenerator()
  120. local callbacks = {}
  121.  
  122. -- build interface
  123. local r = setmetatable({},{ __index = tunnel_resolve, name = name, tunnel_ids = ids, tunnel_callbacks = callbacks, identifier = identifier })
  124.  
  125. -- receive response
  126. RegisterServerEvent(name..":"..identifier..":tunnel_res")
  127. AddEventHandler(name..":"..identifier..":tunnel_res",function(rid,args)
  128. if Debug.active then
  129. Debug.pbegin("tunnelres#"..rid.."_"..name.." "..json.encode(Debug.safeTableCopy(args)))
  130. end
  131.  
  132. local callback = callbacks[rid]
  133. if callback ~= nil then
  134. -- free request id
  135. ids:free(rid)
  136. callbacks[rid] = nil
  137.  
  138. -- call
  139. callback(table.unpack(args))
  140. end
  141.  
  142. Debug.pend()
  143. end)
  144.  
  145. return r
  146. end
  147.  
  148. return Tunnel
  149. :100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement