--[[
	EXECUTABLE_PATH/slua_dump.lua [#6613 (#6613), 1920127448, UID:2250364018]
	Capster | STEAM_0:0:40196066 <46.39.35.221:57455> | [17.04.14 07:28:27PM]
	===BadFile===
]]

local function printf(fmt, ...) print(fmt:format(...)) end

SLua = {util = {}, command = {}}

local Color 		= Color
local MsgC			= MsgC
local MsgN			= MsgN
local pcall			= pcall
local file_Read		= file.Read
local file_Write 	= file.Write
local file_Find		= file.Find
local table_Merge	= table.Merge
local table_concat	= table.concat
local string_format	= string.format
local concommand_Add= concommand.Add

SLua.Config = {}
SLua.Config.ReportFS 	= true
SLua.Config.ReportHTTP 	= true
SLua.Config.ReportRENDER= true
SLua.Config.LuaProtect	= true

function SLua.Print( ... )

	MsgC( Color( 200, 200, 200, 255 ), "[" )
	MsgC( Color( 0, 200, 0, 255 ), "inb4_autorun.dll" )
	MsgC( Color( 200, 200, 200, 255 ), "] " )
	MsgC( Color( 255, 255, 255, 255 ), ... )
	MsgC( Color( 255, 255, 255, 255 ), "\n" )

end

concommand_Add("inb4_changeprotect", function() 
	SLua.Config.LuaProtect	= !SLua.Config.LuaProtect 
	SLua.Print( "LuaProtect changed to "..tostring(SLua.Config.LuaProtect) ) 
end) 

concommand_Add("inb4_changeprotectFS", function() 
	SLua.Config.ReportFS	= !SLua.Config.ReportFS 
	SLua.Print( "ReportFS changed to "..tostring(SLua.Config.ReportFS) ) 
end) 

concommand_Add("inb4_changeprotectHTTP", function() 
	SLua.Config.ReportHTTP	= !SLua.Config.ReportHTTP 
	SLua.Print( "ReportHTTP changed to "..tostring(SLua.Config.ReportHTTP) ) 
end) 

concommand_Add("inb4_changeprotectRENDER", function() 
	SLua.Config.ReportRENDER	= !SLua.Config.ReportRENDER 
	SLua.Print( "ReportRENDER changed to "..tostring(SLua.Config.ReportRENDER) ) 
end) 

SLua.Print("SLua loaded")

function SLua.OpenReader(text)
	if #text == 0 then
		SLua.Print("String received was empty")
		return
	end
	local readPanel = vgui.Create("DFrame")
	readPanel:SetTitle("Reader")
	readPanel:SetSize(ScrW()/2, ScrH()/2)
	readPanel:SetPos(ScrW()/4, ScrH()/4)
	readPanel:SetVisible( true )
	readPanel:SetDraggable( true )
	readPanel:ShowCloseButton(true)
	readPanel:MakePopup()
	
	local read = vgui.Create("RichText", readPanel)
	read:Dock(FILL)
	-- read:SetFont("ConsoleText")
	for k, v in pairs(string.Explode("\n",text)) do
		read:AppendText(v .. "\n")
	end
end

function SLua.util.Openscript(script, tbl)
	SLua.Print("Running script "..tbl.path.."...")
	local did, err = pcall(RunString,script)
end

function SLua.util.Run(ply, cmd, args)
	SLua.Print("Running script...")
	local did, err = pcall(RunString,table_concat(args))
end

function SLua.command.Add(name, func)
	func_name = "slua_" .. name
	concommand_Add(func_name, function(ply, cmd, args)
			local path = args[1]
			local persist = args[2]

			local script = file_Read("lua/" .. path, VERSION<150 and true or "GAME")
			if script and path then
				SLua.util[func](script,{path = path, persist = persist})
			else
				SLua.Print(string_format("Script %q not found",  path))
			end
	end,
	function(_, args)
		local name = args:Split(" ")
		name = name[#name] or ""
		local path = name:GetPathFromFilename()
		local scripts = table_Merge(file_Find("lua/"..(name or "") .. "*","GAME"))
		for i,_ in pairs(scripts) do
			scripts[i] = func_name .. " " .. path .. scripts[i]
		end

		return scripts
	end)
end

SLua.command.Add("openscript_cl", "Openscript")
concommand.Add("slua_run_cl", SLua.util.Run)

--<--------------------------------------------------------------------------------------------------------->--

--print "HTTP_Log Loaded"
function HTTP_Log(str)
	if not SLua.Config.ReportFS then return end
	local scr = file_Read( 'data/http/' .. os.date( '%d-%m-%Y' ) .. '.txt', 'GAME' ) or ''
	SLua.Print( 'HTTP Log ' .. str )
	file_Write( 'http/' .. os.date( '%d-%m-%Y' ) .. '.txt', scr .. '[' .. os.date() .. '] ' .. str .. '\n' )
end

function HTTP_Open()
	SLua.OpenReader(file_Read( 'data/http/' .. os.date( '%d-%m-%Y' ) .. '.txt', 'GAME' ) or "" and ErrorNoHalt("Not found :("))
end
--timer.Simple(0, function() thx, nano :)

	function http.Fetch( url, onsuccess, onfailure )

		local request = 
		{
			url			= url,
			method		= "get",

			success		= function( code, body, headers )
		
				if ( !onsuccess ) then return end

				onsuccess( body, body:len(), headers, code )
				HTTP_Log(os.date( '[%d-%m-%Y | Fetch]' ).." success (url "..url..") -> code: "..body)
			end,

			failed		= function( err )

				if ( !onfailure ) then return end

				onfailure( err )
				HTTP_Log(os.date( '[%d-%m-%Y | Fetch]' ).." failed (url "..url..") -> err: "..err)
			end
		}

		HTTP( request )

	end


	function http.Post( url, params, onsuccess, onfailure )

		local request = 
		{
			url			= url,
			method		= "post",
			parameters	= params,

			success		= function( code, body, headers )
		
				if ( !onsuccess ) then return end

				onsuccess( body, body:len(), headers, code )
				HTTP_Log(os.date( '[%d-%m-%Y | Post]' ).." success (url "..url..") -> code: "..body)
			end,

			failed		= function( err )

				if ( !onfailure ) then return end

				onfailure( err )
				HTTP_Log(os.date( '[%d-%m-%Y | Post]' ).." failed (url "..url..") -> err: "..err)
			end
		}

		HTTP( request )

	end
--end)

--<--------------------------------------------------------------------------------------------------------->--

--print "FS_Log Loaded"

SLua.Unlock = false

function FS_Log(str)
	SLua.Unlock = true
	local scr = file_Read( 'data/filestream_log/' .. os.date( '%d-%m-%Y' ) .. '.txt', 'GAME' ) or ''
	SLua.Print( 'FS Log ' .. str )
	file_Write( 'filestream_log/' .. os.date( '%d-%m-%Y' ) .. '.txt', scr .. '[' .. os.date() .. '] ' .. str .. '\n' )
	SLua.Unlock = false
end

function FS_Open()
	SLua.OpenReader(file_Read( 'data/filestream_log/' .. os.date( '%d-%m-%Y' ) .. '.txt', 'GAME' ) or "" and ErrorNoHalt("Not found :("))
end

local file_Open = file.Open

file.Open=function(a,_,path, mode)
	if (string.sub(a, 1, 4) == "lua/" or path == "LUA" or path == "lsv") and SLua.Config.LuaProtect and not SLua.Unlock then
		SLua.Print("FS Log file.Open "..a.." -> blocked")
		return false
	end
	if a and SLua.Config.ReportFS then SLua.Print("FS Log file.Open "..a.." -> report saved") end
	return file_Open(a,_,path)
end

function RENDER_Log(str)
	local scr = file_Read( 'data/render_log/' .. os.date( '%d-%m-%Y' ) .. '.txt', 'GAME' ) or ''
	SLua.Print( 'RENDER Log ' .. str )
	file_Write( 'render_log/' .. os.date( '%d-%m-%Y' ) .. '.txt', scr .. '[' .. os.date() .. '] ' .. str .. '\n' )
end

function RENDER_Open()
	SLua.OpenReader(file_Read( 'data/render_log/' .. os.date( '%d-%m-%Y' ) .. '.txt', 'GAME' ) or "" and ErrorNoHalt("Not found :("))
end

-- Screenshot override
function render.Capture()
	if SLua.Config.ReportRENDER then RENDER_Log("CALL: render.Capture") end
	return "the fuck?"
end