Guest User

Untitled

a guest
May 15th, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. Extended = {}
  2.  
  3. -- private variables
  4. local callbacks = {}
  5.  
  6.  
  7. -- private functions
  8.  
  9.  
  10. -- hooked functions
  11. local function onExtendedOpcode(opcode, buffer)
  12.   local callback = callbacks[opcode]
  13.   if callback then
  14.     callback(opcode, buffer)
  15.   end
  16. end
  17.  
  18. -- public functions
  19. function Extended.init()
  20.   connect(ProtocolGame, { onExtendedOpcode = onExtendedOpcode } )
  21. end
  22.  
  23. function Extended.terminate()
  24.   disconnect(ProtocolGame, { onExtendedOpcode = onExtendedOpcode } )
  25.   callbacks = nil
  26.   Extended = nil
  27. end
  28.  
  29. function Extended.register(opcode, callback)
  30.   if not callback or type(callback) ~= 'function' then
  31.     error('Invalid callback.')
  32.     return false
  33.   end
  34.  
  35.   if opcode < 0 or opcode > 255 then
  36.     error('Invalid opcode. Range: 0-255')
  37.     return false
  38.   end
  39.  
  40.   if callbacks[opcode] then
  41.     error('Opcode is already taken.')
  42.     return false
  43.   end
  44.  
  45.   callbacks[opcode] = callback
  46.   return true
  47. end
  48.  
  49. function Extended.unregister(opcode)
  50.   if opcode < 0 or opcode > 255 then
  51.     error('Invalid opcode. Range: 0-255')
  52.     return false
  53.   end
  54.  
  55.   if not callbacks[opcode] then
  56.     error('Opcode is not registered.')
  57.     return false
  58.   end
  59.  
  60.   callbacks[opcode] = nil
  61.   return true
  62. end
Advertisement
Add Comment
Please, Sign In to add comment