Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions server/hooks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
local eventHooks = {}
local hookId = 0

function TriggerEventHooks(event, payload)
local callbacks = eventHooks[event]
if not callbacks then return end

local response = nil

for i=1, #callbacks do
local callback = callbacks[i]

if callback.nameFilter and (payload.door.name and not payload.door.name:match(callback.nameFilter)) then
goto continue
end

if callback.print then
lib.print.info(('Triggering event hook "%s:%s:%s".'):format(callback.resource, event, i))
end

local start = os.microtime()
local _, result = pcall(callback, payload)
local executionTime = os.microtime() - start

if executionTime >= 100000 then
warn(('Execution of event hook "%s:%s:%s" took %.2fms.'):format(callback.resource, event, i, executionTime / 1e3))
end

if result ~= nil then
response = result
end

::continue::
end

return response
end

function registerHook(event, callback, options)
if not eventHooks[event] then eventHooks[event] = {} end

local mt = getmetatable(callback)
mt.__index = nil
mt.__newindex = nil
callback.resource = GetInvokingResource()
hookId = hookId + 1
callback.hookId = hookId

if options then
for k,v in pairs(options) do
callback[k] = v
end
end

eventHooks[event][#eventHooks[event] + 1] = callback
return hookId
end

exports('registerHook', registerHook)

function removeResourceHook(resource, hookId)
for event, callbacks in pairs(eventHooks) do
for i = #callbacks, 1, -1 do
local callback = callbacks[i]
if callback.resource == resource and (not hookId or callback.hookId == hookId) then
table.remove(eventHooks[event], i)
end
end
end
end

AddEventHandler('onResourceStop', removeResourceHook)
exports('removeResourceHook', function(id)
removeResourceHook(GetInvokingResource() or cache.resource, id)
end)

return TriggerEventHooks
18 changes: 15 additions & 3 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ lib.versionCheck('communityox/ox_doorlock')
require 'server.convert'

local utils = require 'server.utils'
local TriggerEventHooks = require 'server.hooks'
local doors = {}


local function encodeData(door)
local double = door.doors

Expand Down Expand Up @@ -218,7 +218,8 @@ local function isAuthorised(playerId, door, lockpick)
end

if door.characters and table.contains(door.characters, GetCharacterId(player)) then
return true
authorised = true
goto continue
end

if door.groups then
Expand All @@ -234,7 +235,18 @@ local function isAuthorised(playerId, door, lockpick)
authorised = door.passcode == lib.callback.await('ox_doorlock:inputPassCode', playerId)
end

return authorised
::continue::

local hookResult = TriggerEventHooks('doorAuthorization', {
source = playerId,
door = door,
lockpick = lockpick,
authorised = authorised,
})

if hookResult == nil then return authorised end

return authorised or hookResult == nil or hookResult
end

local sql = LoadResourceFile(cache.resource, 'sql/ox_doorlock.sql')
Expand Down