Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook.ProtectedRun and hook.ProtectedCall functions #2175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
73 changes: 73 additions & 0 deletions garrysmod/lua/includes/modules/hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local isbool = isbool
local IsValid = IsValid
local type = type
local ErrorNoHaltWithStack = ErrorNoHaltWithStack
local GProtectedCall = ProtectedCall

module( "hook" )

Expand Down Expand Up @@ -76,6 +77,21 @@ function Run( name, ... )
return Call( name, currentGM, ... )
end

--[[---------------------------------------------------------
Name: ProtectedRun
Args: string hookName, vararg args
Desc: Executes hooks associated with the given hook name.
Unlike hook.Run, it ensures that execution continues
even if a hook returns a value or throws an error.
-----------------------------------------------------------]]
function ProtectedRun( name, ... )
if ( !currentGM ) then
currentGM = gmod and gmod.GetGamemode() or nil
end

return ProtectedCall( name, currentGM, ... )
end


--[[---------------------------------------------------------
Name: Run
Expand Down Expand Up @@ -141,3 +157,60 @@ function Call( name, gm, ... )
return GamemodeFunction( gm, ... )

end

--[[---------------------------------------------------------
Name: ProtectedCall
Args: string hookName, table gamemodeTable, vararg args
Desc: Executes hooks associated with the given hook name.
Unlike hook.Call, it ensures that execution continues
even if a hook returns a value or throws an error.
-----------------------------------------------------------]]
function ProtectedCall( name, gm, ... )

--
-- Run hooks
--
local HookTable = Hooks[ name ]
if ( HookTable != nil ) then

for k, v in pairs( HookTable ) do

if ( isstring( k ) ) then

--
-- If it's a string, it's cool
--
GProtectedCall( v, ... )

else

--
-- If the key isn't a string - we assume it to be an entity
-- Or panel, or something else that IsValid works on.
--
if ( IsValid( k ) ) then
--
-- If the object is valid - pass it as the first argument (self)
--
GProtectedCall( v, k, ... )
else
--
-- If the object has become invalid - remove it
--
HookTable[ k ] = nil
end
end

end
end

--
-- Call the gamemode function
--
if ( !gm ) then return end

local GamemodeFunction = gm[ name ]
if ( GamemodeFunction == nil ) then return end

GProtectedCall( GamemodeFunction, gm, ... )
end