diff --git a/modules/game/game.otmod b/modules/game/game.otmod index 2894f3a9..13c0a81c 100644 --- a/modules/game/game.otmod +++ b/modules/game/game.otmod @@ -5,6 +5,7 @@ Module website: www.otclient.info dependencies: + - gamelib_extended - game_tibiafiles - client_background //- game_shaders diff --git a/modules/gamelib_extended/extended.lua b/modules/gamelib_extended/extended.lua new file mode 100644 index 00000000..6fa09eb7 --- /dev/null +++ b/modules/gamelib_extended/extended.lua @@ -0,0 +1,58 @@ +Extended = {} + +-- private variables +local callbacks = {} + +-- hooked functions +local function onExtendedOpcode(protocol, opcode, buffer) + local callback = callbacks[opcode] + if callback then + callback(opcode, buffer) + end +end + +-- public functions +function Extended.init() + connect(ProtocolGame, { onExtendedOpcode = onExtendedOpcode } ) +end + +function Extended.terminate() + disconnect(ProtocolGame, { onExtendedOpcode = onExtendedOpcode } ) + callbacks = nil + Extended = nil +end + +function Extended.register(opcode, callback) + if not callback or type(callback) ~= 'function' then + error('Invalid callback.') + return false + end + + if opcode < 0 or opcode > 255 then + error('Invalid opcode. Range: 0-255') + return false + end + + if callbacks[opcode] then + error('Opcode is already taken.') + return false + end + + callbacks[opcode] = callback + return true +end + +function Extended.unregister(opcode) + if opcode < 0 or opcode > 255 then + error('Invalid opcode. Range: 0-255') + return false + end + + if not callbacks[opcode] then + error('Opcode is not registered.') + return false + end + + callbacks[opcode] = nil + return true +end \ No newline at end of file diff --git a/modules/gamelib_extended/extended.otmod b/modules/gamelib_extended/extended.otmod new file mode 100644 index 00000000..df5093e0 --- /dev/null +++ b/modules/gamelib_extended/extended.otmod @@ -0,0 +1,12 @@ +Module + name: gamelib_extended + description: Manage client extended messages + author: baxnie + website: www.otclient.info + + @onLoad: | + dofile 'extended' + Extended.init() + + @onUnload: | + Extended.terminate()