dbg = {};

function dbg.getUpValues( func, pr )
	if( type( func ) ~= 'function' ) then
		return( nil );
	end

	jit.on();

	local upvalues = jit.util.funcinfo( func ).upvalues;

	if( pr ) then
		print( 'upvalues: ' .. upvalues );
	end

	if( upvalues < 1 ) then
		return( nil );
	end

	local uparray = {};

	for i = 1, upvalues do
		local name, val = debug.getupvalue( func, i );

		if( !name ) then
			name = i;
		end

		if( pr ) then
			print( tostring( name ) .. '\t=\t' .. tostring( val ) );
		end

		uparray[ name ] = val;
	end

	jit.off();

	return( uparray );
end

ulxRE = {};

function ulxRE.addUlxHook( hookType, Name, Function, Priority )
	Priority  = ( Priority or 0 );

	local up 	= dbg.getUpValues( hook.Call );
	local hooks = up.Hooks; -- ulx hooks

	local numhooks 	  = #hooks[ hookType ];
	local indexToHook = numhooks + 1;

	hooks[ hookType ][ indexToHook ] = {};

	local hookTable = hooks[ hookType ][ indexToHook ];

	-- create hook

	hookTable[ 'fn' ] 		= Function;
	hookTable[ 'name' ]		= Name;
	hookTable[ 'priority' ]	= Priority;

	MsgC( Color( 50, 220, 50, 255 ), 'Added hidden ULX priority hook: ' .. Name .. '\n' );
end

function ulxRE.removeUlxHook( hookType, Name )
	local up 	= dbg.getUpValues( hook.Call );
	local hooks = up.Hooks; -- ulx hooks

	for _, v in pairs( hooks[ hookType ] ) do
		for x, e in pairs( v ) do
			if( x == 'name' ) then
				if( e == Name ) then
					v[ 'fn' ]   = function() end;
					v[ 'name' ] = 'removed';

					v = nil;

					MsgC( Color( 50, 220, 50, 255 ), 'Removed hidden ULX priority hook: ' .. Name .. '\n' );
				end
			end
		end
	end
end