Implement hook.lua with my adjustments, should be obvious

If you take a look here… at my file hook.lua

I have taken 1 minute out of my day to allow for more dynamic gamemode hooking…should be rather obvious in the differences.

includes/hook.lua ( http://luabin.foszor.com/code/lua/includes/modules/hook.lua )



function Call( name, gm, ... )

	local b, rA, rB, rC, rD, rE, rF, rG, rH
	local HookTable = Hooks[ name ]
	

	if ( HookTable != nil ) then
	
		for k, v in pairs( HookTable ) do 
			
			if ( v == nil ) then
			
				ErrorNoHalt("Hook '"..tostring(k).."' tried to call a nil function!
")
				HookTable[ k ] = nil // remove this hook
				break;
			
			else
				// Call hook function
				b, rA, rB, rC, rD, rE, rF, rG, rH = pcall( v, ... )
				
				if (!b) then
				
						ErrorNoHalt("Hook '"..tostring(k).."' Failed: "..tostring(rA).."
")
						HookTable[ k ] = nil // remove this hook
				
				else
				
					// Allow hooks to override return values
					if (rA != nil) then
						return rA, rB, rC, rD, rE, rF, rG, rH
					end
					
				end
			end
			
		end
	end
	
	if ( gm ) then
	
		local GamemodeFunction = gm[ name ]
		if ( GamemodeFunction == nil ) then return nil end
		
		if ( type( GamemodeFunction ) != "function" ) then
			Msg( "Calling Non Function!? ", GamemodeFunction, "
" )
		end
		
		// This calls the actual gamemode function - after all the hooks have had chance to override
		b, rA, rB, rC, rD, rE, rF, rG, rH = pcall( GamemodeFunction, gm, ... )
		
		if (!b) then
		
			gm[ name .. "_ERRORCOUNT" ] = gm[ name .. "_ERRORCOUNT" ] or 0
			gm[ name .. "_ERRORCOUNT" ] = gm[ name .. "_ERRORCOUNT" ] + 1
			ErrorNoHalt( "ERROR: GAMEMODE:'"..tostring(name).."' Failed: "..tostring(rA).."
" )
			return nil
			
		end
		
	end
	
	return rA, rB, rC, rD, rE, rF, rG, rH
	
end


My adjustments = i_am_dissapoint/hook.lua



function Call( name, gm, ... )

	local b, r = nil, {}
	local HookTable = Hooks[ name ]
	

	if ( HookTable != nil ) then
	
		for k, v in pairs( HookTable ) do 
			
			if ( v == nil ) then
			
				ErrorNoHalt("Hook '"..tostring(k).."' tried to call a nil function!
")
				HookTable[ k ] = nil // remove this hook
				break;
			
			else
				// Call hook function
				b, r = pcall_handle(pcall( v, ... ))
				
				if (!b) then
				
						ErrorNoHalt("Hook '"..tostring(k).."' Failed: "..tostring(r[1]).."
")
						HookTable[ k ] = nil // remove this hook
				
				else
				
					// Allow hooks to override return values
					if #r > 0 then
						return unpack(r)
					end
					
				end
			end
			
		end
	end
	
	if ( gm ) then
	
		local GamemodeFunction = gm[ name ]
		if ( GamemodeFunction == nil ) then return nil end
		
		if ( type( GamemodeFunction ) != "function" ) then
			Msg( "Calling Non Function!? ", GamemodeFunction, "
" )
		end
		
		// This calls the actual gamemode function - after all the hooks have had chance to override
		b, r = pcall_handle(pcall( GamemodeFunction, gm, ... ))
		
		if (!b) then
		
			gm[ name .. "_ERRORCOUNT" ] = gm[ name .. "_ERRORCOUNT" ] or 0
			gm[ name .. "_ERRORCOUNT" ] = gm[ name .. "_ERRORCOUNT" ] + 1
			ErrorNoHalt( "ERROR: GAMEMODE:'"..tostring(name).."' Failed: "..tostring(r[1]).."
" )
			return nil
			
		end
		
	end
	
	return unpack(r)
	
end


http://www.garrysmod.org/img/?t=dll&id=76351
hurr

Except you’re allocating a bunch of tables that are going to be garbage collected later. There are a lot of hook.Calls.

Good point, probably a way around this… fffff or not, whatever, it’s no biggie, but it sure looks stupid either way…kinda dumb