Skip to content

Commit 42a5a16

Browse files
committed
Add ProtectedRun and ProtectedCall functions
1 parent e47ac04 commit 42a5a16

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Diff for: garrysmod/lua/includes/modules/hook.lua

+73
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local isbool = isbool
77
local IsValid = IsValid
88
local type = type
99
local ErrorNoHaltWithStack = ErrorNoHaltWithStack
10+
local GProtectedCall = ProtectedCall
1011

1112
module( "hook" )
1213

@@ -76,6 +77,21 @@ function Run( name, ... )
7677
return Call( name, currentGM, ... )
7778
end
7879

80+
--[[---------------------------------------------------------
81+
Name: ProtectedRun
82+
Args: string hookName, vararg args
83+
Desc: Executes hooks associated with the given hook name.
84+
Unlike hook.Run, it ensures that execution continues
85+
even if a hook returns a value or throws an error.
86+
-----------------------------------------------------------]]
87+
function ProtectedRun( name, ... )
88+
if ( !currentGM ) then
89+
currentGM = gmod and gmod.GetGamemode() or nil
90+
end
91+
92+
return ProtectedCall( name, currentGM, ... )
93+
end
94+
7995

8096
--[[---------------------------------------------------------
8197
Name: Run
@@ -141,3 +157,60 @@ function Call( name, gm, ... )
141157
return GamemodeFunction( gm, ... )
142158

143159
end
160+
161+
--[[---------------------------------------------------------
162+
Name: ProtectedCall
163+
Args: string hookName, table gamemodeTable, vararg args
164+
Desc: Executes hooks associated with the given hook name.
165+
Unlike hook.Call, it ensures that execution continues
166+
even if a hook returns a value or throws an error.
167+
-----------------------------------------------------------]]
168+
function ProtectedCall( name, gm, ... )
169+
170+
--
171+
-- Run hooks
172+
--
173+
local HookTable = Hooks[ name ]
174+
if ( HookTable != nil ) then
175+
176+
for k, v in pairs( HookTable ) do
177+
178+
if ( isstring( k ) ) then
179+
180+
--
181+
-- If it's a string, it's cool
182+
--
183+
GProtectedCall( v, ... )
184+
185+
else
186+
187+
--
188+
-- If the key isn't a string - we assume it to be an entity
189+
-- Or panel, or something else that IsValid works on.
190+
--
191+
if ( IsValid( k ) ) then
192+
--
193+
-- If the object is valid - pass it as the first argument (self)
194+
--
195+
GProtectedCall( v, k, ... )
196+
else
197+
--
198+
-- If the object has become invalid - remove it
199+
--
200+
HookTable[ k ] = nil
201+
end
202+
end
203+
204+
end
205+
end
206+
207+
--
208+
-- Call the gamemode function
209+
--
210+
if ( !gm ) then return end
211+
212+
local GamemodeFunction = gm[ name ]
213+
if ( GamemodeFunction == nil ) then return end
214+
215+
GProtectedCall( GamemodeFunction, gm, ... )
216+
end

0 commit comments

Comments
 (0)