local print=print
local CompileString=CompileString
local pcall=pcall
local xpcall=xpcall

concommand.Add("lua", function(_, _, _, line)
	local ret = CompileString(line, "LuaCmd", false)
	if isfunction(ret) then
		local tr = LocalPlayer():GetEyeTrace()
		_G.this = tr.Entity
		xpcall(ret, print)
		_G.this = nil
	else
		print(ret)
	end
end)

concommand.Add("script", function(_, _, _, line)
	local ret = CompileString(file.Read("lua/" .. line, "GAME"), line, false)
	if isfunction(ret) then
		xpcall(ret, print)
	else
		print(ret)
	end
end)

do -- luadump
	file.CreateDir("luadump")

	concommand.Add("luadump", function()
		local savepath = "luadump/" .. os.time()

		file.CreateDir(savepath)

		local iter

		iter = function(path)
			local files, folders = file.Find(path .. "*", "LUA")

			for k, v in pairs(files) do
				file.Write(savepath .. "/" .. path .. v:sub(1, -4) .. "txt", file.Read(path .. v, "LUA"))
			end

			for k, v in pairs(folders) do
				file.CreateDir(savepath .. "/" .. path .. v)
				iter(path .. v .. "/")
			end
		end

		iter ""
	end)
end