/*

*
* AUTHOR: paradox
*
* PURPOSE: Handles detouring for all global functions.
*

*/

// TODO: setmetatable, we can't just reuse debug since debug works with any and setmetatable only works with tables
// TODO: getmetatable
// TODO: IsValid
// TODO: get/setfenv since it throws an error if passed bad values, debug.getfenv just returns nil
// TODO: istable

//-/~ Is Valid (IsValid)
function detour.IsValid( Object )
    if majestic.ProtectedTable( Object ) then 
        return false 
    end

    return IsValid( Object )
end

_G.IsValid = majestic.Detour( _G.IsValid, detour.IsValid )

//-/~ Set Metatable (setmetatable)
function detour.GlobalSetMetatable( Object, Metatable )
    if majestic.ProtectedTable( Object ) or majestic.IsProtected( Object ) then 
        detour.ObjectMetatablesFunc[ Object ] = Metatable

        return Object
    end

    return setmetatable( Object, Metatable )
end

_G.setmetatable = majestic.Detour( _G.setmetatable, detour.GlobalSetMetatable )

//-/~ Get Metatable (getmetatable)
function detour.GlobalGetMetatable( Object )
    local ObjectData = detour.ObjectMetatablesFunc[ Object ]

    if ObjectData then
        return ObjectData
    elseif majestic.ProtectedTable( Object ) or majestic.IsProtected( Object ) then 
        return nil
    end

    return getmetatable( Object )
end

_G.getmetatable = majestic.Detour( _G.getmetatable, detour.GlobalGetMetatable )