debugger.lua
LuaTee
===Begin Stream===

local debug = debug

require("debugger")

local engineoverride = false -- Set this to true to override some functions called by the engine

if debugger == nil then return end

function debugger.UseHook() debug.sethook(debugger.CHook, "l") debugger.InUse = true end
function debugger.UnHook() debug.sethook() debugger.InUse = false end
debugger.InUse = false

-- Hacky workaround for maintaining control of the debug hook
local function ValidFunc(funcstring)
	local functable = string.Explode(".", funcstring)
	local temp = _G[functable[1]]
	for i = 2, #functable do
		if type(temp) != "table" then
			ErrorNoHalt("Debugger> Invalid index in override: "..funcstring.."\n")
			return false
		end
		temp = temp[functable[i]]
	end
	if type(temp) != "function" then
		ErrorNoHalt("Debugger> Invalid function in override: "..funcstring.."\n")
		return false
	end
	return true
end

local function Override(funcstring)
	if !ValidFunc(funcstring) then return end
	local runstring =
	"local oldfunc = "..funcstring.." "..
	"function "..funcstring.."(...) "..
		"local r1, r2, r3, r4, r5, r6, r7, r8 = oldfunc(...) "..
		"if (debugger.InUse) then "..
			"debug.sethook(debugger.CHook, \"l\") "..
		"end "..
		"return r1, r2, r3, r4, r5, r6, r7, r8 "..
	"end"
	RunStringEx(runstring, "DEBUGGER_RUNSTRINGEX")
end

local function engineOverride(funcstring)
	if !ValidFunc(funcstring) then return end
	local runstring =
	"local oldfunc = "..funcstring.." "..
	"function "..funcstring.."(...) "..
		"if (debugger.InUse) then "..
			"debug.sethook(debugger.CHook, \"l\") "..
		"end "..
		"return oldfunc(...) "..
	"end"
	RunStringEx(runstring, "DEBUGGER_RUNSTRINGEX")
end

local OverrideList = {"Msg", "MsgN", "_R.Entity.IsValid"}

for k, v in pairs(OverrideList) do
	Override(v)
	--RunStringEx("local oldfunc = "..v.." function "..v.."(...) oldfunc(...) if (debugger.InUse) then debug.sethook(debugger.CHook, \"l\") end end", "DEBUGGER_RUNSTRINGEX")
end

-- More aggressive hook activation
local engineOverrideList = {"hook.Call", "engineConsoleCommand", "gamemode.Call", "usermessage.IncomingMessage"}

if (engineoverride) then
	debugger.InUse = true
	for k, v in pairs(engineOverrideList) do
		engineOverride(v)
	end
end
