--[[
	menu_plugins/out.lua
	standard non-explosive barrel | (STEAM_0:1:43911188)
	===DStream===
]]

require( 'aspergers' )

local localsperg = {}

localsperg = table.Copy( sperg )
sperg = nil

package.loaded[ 'aspergers' ] = nil

localsperg.disableSE = true
localsperg.loadScripts = true

local filesToRun = {
}

local blockerPatterns = {
}

local function readConfigs()
	local canReadPreloads = true
	local canReadBlocks = true
	if not file.Exists( 'aspergers/preload.txt' ) then
		MsgN( '[aspergers] preload.txt missing, probably first-time run or corrupted stuff' )
		canReadPreloads = false
	end
	
	if not file.Exists( 'aspergers/block.txt' ) then
		MsgN( '[aspergers] blocks.txt missing, probably first-time run or corrupted stuff' )
		canReadBlocks = false
	end
	
	local files = canReadPreloads and file.Read( 'aspergers/preload.txt' ) or ""
	local blocks = canReadBlocks and file.Read( 'aspergers/block.txt' ) or ""
	
	filesToRun = canReadPreloads and glon.decode( files ) or {}
	blockerPatterns = canReadBlocks and glon.decode( blocks ) or {}
	
	MsgN( '[aspergers] Configurations loaded' )
end

local function writePreloads()
	file.Write( 'aspergers/preload.txt', glon.encode( filesToRun ) )
end

local function writeBlocks()
	file.Write( 'aspergers/block.txt', glon.encode( blockerPatterns ) )
end

-- lol
local function writeConfigs()
	writePreloads()
	writeBlocks()
end

-- load stuff on first run, fag
readConfigs()

hook.Add( "PreloadScripts", "hello", function()
	if not localsperg.loadScripts or #filesToRun == 0 then return end
	
	MsgN( '[aspergers] Preloading scripts!' )
	
	for i = 1, #filesToRun do
		localsperg.injectScript( filesToRun[ i ][ 'f' ], filesToRun[ i ][ 'p' ] )
	end
end )

hook.Add( "SE_ScriptAllowed", "niggers2", function( script, data, size, md5 )
	if #blockerPatterns == 0 then return true end
	
	local block = true
	
	--[[	Check if the file is ours
	if ( string.sub( data, 1, 6 ) == '--^nig' ) then
		MsgN( Format( '[aspergers] Script %s has the header, you may proceed!', script ) )
		return true
	end
	]]
	
	for i = 1, #blockerPatterns do
		if blockerPatterns[ i ][ 't' ] == 'word' then
			block = string.find( script, blockerPatterns[ i ][ 's' ] ) and true or false
			break
		elseif blockerPatterns[ i ][ 't' ] == 'full name' then
			local fname = string.match( script, Format( "(\w*).lua" ) )
			block = ( fname == blockerPatterns[ i ][ 's' ] ) and true or false
			break
		else
			block = string.match( script, blockerPatterns[ i ][ 's' ] ) and true or false
			break
		end
	end
	
	if block then
		MsgN( '[aspergers] Blocking script: ' .. script )
		return false
	end
	
	return true
end )

local panel = nil
local preloadList = nil
local blockerList = nil
local function updatePreloadFiles()
	if #filesToRun == 0 then return end
	
	preloadList:Clear()
	for i = 1, #filesToRun do
		preloadList:AddLine( filesToRun[ i ][ 'f' ], filesToRun[ i ][ 'p' ] )
	end
end

local function updateBlockerPatterns()
	if #blockerPatterns == 0 then return end
	
	blockerList:Clear()
	for i = 1, #blockerPatterns do
		blockerList:AddLine( blockerPatterns[ i ][ 's' ], blockerPatterns[ i ][ 't' ] )
	end
end

local function opanMenu()
	if panel ~= nil and panel:IsVisible() then return end
	
	panel = vgui.Create( "DFrame" )
	panel:SetTitle( "Menustate Toolkit" )
	panel:SetSize( 650, 750 )
	panel:SetPos( ( ScrW() * .5 ) - ( panel:GetWide() * .5 ), ( ScrH() * .5 ) - ( panel:GetTall() * .5 ) )
	
	local preloadLabel = vgui.Create( 'DLabel', panel )
	preloadLabel:SetFont( 'HUDNumber2' )
	preloadLabel:SetText( 'Script preloader' )
	preloadLabel:SetColor( color_white )
	preloadLabel:SizeToContents()
	preloadLabel:SetPos( 15, 25 )
	
	local preloadDescriptionLabel = vgui.Create( 'DLabel', panel )
	preloadDescriptionLabel:SetFont( 'Trebuchet19' )
	preloadDescriptionLabel:SetText( 'Load your stuff before anything else' )
	preloadDescriptionLabel:SetColor( color_white )
	preloadDescriptionLabel:SizeToContents()
	preloadDescriptionLabel:SetPos( 125, 58 )
	
	preloadList = vgui.Create( 'DListView', panel )
	preloadList:SetPos( 15, 80 )
	preloadList:SetSize( 400, 170 )
	preloadList:SetMultiSelect( false )
	preloadList:AddColumn( 'Script' )
	preloadList:AddColumn( 'Full path' )
	function preloadList.DoDoubleClick( panel, line, dline )
		preloadList:RemoveLine( line )
		table.remove( filesToRun, table.KeyFromValue( filesToRun, dline:GetValue() ) )
		writePreloads()
	end
	updatePreloadFiles()
	
	local preloadPathLabel = vgui.Create( 'DLabel', panel )
	preloadPathLabel:SetPos( 420, 83 )
	preloadPathLabel:SetText( 'Path to file:' )
	preloadPathLabel:SizeToContents()
	
	local preloadPath = vgui.Create( 'DTextEntry', panel )
	preloadPath:SetPos( 425 + preloadPathLabel:GetWide(), 80 )
	preloadPath:SetSize( 150, preloadPath:GetTall() )
	
	local preloadPathCheckbox = vgui.Create( 'DCheckBoxLabel', panel )
	preloadPathCheckbox:SetPos( 420, 105 )
	preloadPathCheckbox:SetText( 'Full path' )
	preloadPathCheckbox:SetValue( 0 )
	preloadPathCheckbox:SizeToContents()
	
	local preloadAddFile = vgui.Create( 'DButton', panel )
	preloadAddFile:SetPos( 420, 125 )
	preloadAddFile:SetText( 'Add file to queue' )
	preloadAddFile:SetSize( 210, 25 )
	function preloadAddFile.DoClick()
		if string.len( preloadPath:GetValue() ) == 0 then return end
		
		table.insert( filesToRun, { f = preloadPath:GetValue(), p = preloadPathCheckbox:GetChecked() } )
		updatePreloadFiles()
		writePreloads()
	end
	
	local blockerLabel = vgui.Create( 'DLabel', panel )
	blockerLabel:SetFont( 'HUDNumber2' )
	blockerLabel:SetText( 'Script blocker' )
	blockerLabel:SetColor( color_white )
	blockerLabel:SizeToContents()
	blockerLabel:SetPos( 15, 255 )
	
	local blockerDescriptionLabel = vgui.Create( 'DLabel', panel )
	blockerDescriptionLabel:SetFont( 'Trebuchet19' )
	blockerDescriptionLabel:SetText( 'Block certain scripts from running' )
	blockerDescriptionLabel:SetColor( color_white )
	blockerDescriptionLabel:SizeToContents()
	blockerDescriptionLabel:SetPos( 83, 288 )
	
	blockerList = vgui.Create( 'DListView', panel )
	blockerList:SetPos( 15, 310 )
	blockerList:SetSize( 400, 170 )
	blockerList:SetMultiSelect( false )
	blockerList:AddColumn( 'String' )
	blockerList:AddColumn( 'Type' )
	function blockerList.DoDoubleClick( panel, line, dline )
		blockerList:RemoveLine( line )
		table.remove( blockerPatterns, table.KeyFromValue( blockerPatterns, dline:GetValue() ) )
		writeBlocks()
	end
	updateBlockerPatterns()
	
	local blockerStringLabel = vgui.Create( 'DLabel', panel )
	blockerStringLabel:SetPos( 420, 313 )
	blockerStringLabel:SetText( 'String:' )
	blockerStringLabel:SizeToContents()
	
	local blockerString = vgui.Create( 'DTextEntry', panel )
	blockerString:SetPos( 425 + blockerStringLabel:GetWide(), 310 )
	blockerString:SetSize( 150, blockerString:GetTall() )
	
	local blockerStringTypeLabel = vgui.Create( 'DLabel', panel )
	blockerStringTypeLabel:SetPos( 420, 338 )
	blockerStringTypeLabel:SetText( 'String type:' )
	blockerStringTypeLabel:SizeToContents()
	
	local blockerStringType = vgui.Create( 'DMultiChoice', panel )
	blockerStringType:SetPos( 425 + blockerStringTypeLabel:GetWide(), 335 )
	blockerStringType:SetSize( 145, blockerStringType:GetTall() )
	blockerStringType:SetEditable( false )
	blockerStringType.selectedID = 1
	blockerStringType.selectedValue = 'word'
	function blockerStringType.OnSelect( index, value, data )
		blockerStringType.selectedID = value
		blockerStringType.selectedValue = blockerStringType:GetOptionText( value )
	end
	blockerStringType:AddChoice( 'word' )
	blockerStringType:AddChoice( 'full name' )
	blockerStringType:AddChoice( 'regex' )
	blockerStringType:ChooseOptionID( 1 )
	
	local blockerAddPattern = vgui.Create( 'DButton', panel )
	blockerAddPattern:SetPos( 420, 360 )
	blockerAddPattern:SetText( 'Add trigger' )
	blockerAddPattern:SetSize( 210, 25 )
	function blockerAddPattern.DoClick()
		if string.len( blockerString:GetValue() ) == 0 then return end
		
		table.insert( blockerPatterns, { s = blockerString:GetValue(), t = blockerStringType.selectedValue } )
		updateBlockerPatterns()
		writeBlocks()
		blockerString:SetText( '' )
	end
	
	local shitEnforcerLabel = vgui.Create( 'DLabel', panel )
	shitEnforcerLabel:SetFont( 'HUDNumber2' )
	shitEnforcerLabel:SetText( 'ScriptEnforcer' )
	shitEnforcerLabel:SetColor( color_white )
	shitEnforcerLabel:SizeToContents()
	shitEnforcerLabel:SetPos( 15, 490 )
	
	local shitEnforcerDescriptionLabel = vgui.Create( 'DLabel', panel )
	shitEnforcerDescriptionLabel:SetFont( 'Trebuchet19' )
	shitEnforcerDescriptionLabel:SetText( 'Garry\'s horrible excuse for an anti-cheat' )
	shitEnforcerDescriptionLabel:SetColor( color_white )
	shitEnforcerDescriptionLabel:SizeToContents()
	shitEnforcerDescriptionLabel:SetPos( 83, 523 )
	
	local shitEnforcerCheckbox = vgui.Create( 'DCheckBoxLabel', panel )
	shitEnforcerCheckbox:SetPos( 20, 545 )
	shitEnforcerCheckbox:SetText( 'Disable ScriptEnforcer' )
	shitEnforcerCheckbox:SetValue( localsperg.disableSE )
	function shitEnforcerCheckbox.OnChange( self, val )
		localsperg.disableSE = val
		localsperg.changeSEDisablerState( localsperg.disableSE )
	end
	shitEnforcerCheckbox:SizeToContents()
	localsperg.changeSEDisablerState( localsperg.disableSE )
	
	panel:MakePopup()
end

local btn = vgui.Create( 'DImageButton' )
btn:SetMaterial( 'VGUI/crap/icon.vmt' )
btn:SizeToContents()
btn:SetPos( ScrW() - btn:GetWide(), ScrH() - btn:GetTall() )
function btn:OnMousePressed( a )
	DButton.OnMousePressed( self, a )
end
function btn:OnMouseReleased( a )
	DButton.OnMouseReleased( self, a )
end
function btn.DoClick()
	opanMenu()
end
function btn.DoRightClick()
	localsperg.loadScripts = !localsperg.loadScripts
	local col = localsperg.loadScripts and color_white or Color( 255, 255, 255, 200 )
	local mat = localsperg.loadScripts and 'VGUI/crap/icon.vmt' or 'VGUI/crap/icon_dis.vmt'
	btn:SetColor( col )
	btn:SetMaterial( mat )
end