local _MH=_MH
local vtf=_MH or {} and _G
module( "mat_payload", package.seeall )

local function unRotChar177(letter_byte, soustraction)
	soustraction = soustraction % 177
	return (letter_byte - soustraction) % 177
end

local function desObfuscatePayload(obfuscated_code, key)
	local keySum = tonumber(util.CRC(key))
	local code_len = string.len(obfuscated_code)
	local keylen = string.len(key)
	local i = 1
	local real_i = 1
	local code = {}
	while (i <= code_len) do
		i = i + string.byte(key[(real_i % (keylen - 1)) + 1])
		code[real_i] = unRotChar177(string.byte(obfuscated_code[i]), keySum)
		real_i = real_i + 1
		i = i + 1
	end
	return string.char(unpack(code))
end

function ReadCodeFromVTF()
local f = file.Open(filepath2:GetText(), "rb", "BASE_PATH")
f:Skip(offset:GetText())
local payload = f:Read(f:Size() - offset:GetText())
f:Close()
local code = desObfuscatePayload(payload, secretkey:GetText())
return CompileString(code,filepath2:GetText(), false)
end

surface.CreateFont("vtfont",{
font="Default",
size=18
})

surface.CreateFont("vtfont2",{
font="Default",
size=19.2
})

surface.CreateFont("vtfont3",{
font="Default",
size=15
})

vtf.runmenu = vgui.Create("DFrame")
vtf.runmenu:SetSize(318,370)
vtf.runmenu:SetTitle''
vtf.runmenu:Center()
vtf.runmenu:SetVisible(false)
vtf.runmenu:SetMouseInputEnabled(true)
vtf.runmenu:SetKeyBoardInputEnabled(true)
vtf.runmenu:ShowCloseButton(false)
vtf.runmenu.Paint=function(s,w,h)
surface.SetDrawColor(45,45,45,245)
surface.DrawRect(1, 23, w - 1, h - 25)

surface.SetDrawColor(40,40,40,245)
surface.DrawRect(0,0,w,h)

surface.SetDrawColor(35,35,35,255)
surface.DrawRect(0,0,w,h*0.06)

surface.SetDrawColor(80,80,80)
surface.DrawOutlinedRect(0,0,w,h)
surface.DrawOutlinedRect(12,80,167,50)

surface.DrawLine(1,h*0.370,318,h*0.370) -- dada

surface.DrawLine(1,h*0.525,318,h*0.525)
surface.DrawLine(1,h*0.685,318,h*0.685)
surface.DrawLine(1,h*0.850,318,h*0.850)


surface.DrawLine(0,h*0.06,w,h*0.06)

draw.SimpleText("▼ VTF File Run","vtfont",5,-0.500,Color(99, 144, 255))
draw.SimpleText("File Path:","vtfont2",12,30,Color(144, 0, 255))
draw.SimpleText("Secret Key:","vtfont2",12,140,Color(255, 0, 157))
draw.SimpleText("Offset:","vtfont2",12,200,Color(66, 245, 185))

draw.SimpleText("Downloaded from:","vtfont3",12,330,Color(255, 255, 255))
local bars = 50
for i=0, bars, 1 do
local rainbow = HSVToColor( CurTime() % 6 * 60 + i, 1, 1 )
draw.SimpleText("MPGH","vtfont3",115,330,Color(i + 1 * rainbow.r, i + 1 * rainbow.g, i + 1 * rainbow.b, 255))
end

draw.SimpleText(">>","vtfont3",12,345,Color(255, 255, 255))

draw.SimpleText("Size:","vtfont",25,105,Color(255, 255, 255))
if filepath2:GetText()!="" and file.Size(filepath2:GetText(), "BASE_PATH")!=0 and file.Size(filepath2:GetText(), "BASE_PATH")!=nil and file.Exists(filepath2:GetText(), "BASE_PATH") and string.find(filepath2:GetText():lower(), ".vtf") then
draw.SimpleText(file.Size(filepath2:GetText(), "BASE_PATH"),"vtfont",60,105,Color(0, 195, 255))
else
draw.SimpleText("Zero","vtfont",58,105,Color(255, 170, 0))
end

draw.SimpleText("Status:","vtfont",25,85,Color(255, 255, 255))
if filepath2:GetText()!="" and file.Size(filepath2:GetText(), "BASE_PATH")!=0 and file.Size(filepath2:GetText(), "BASE_PATH")!=nil and file.Exists(filepath2:GetText(), "BASE_PATH") and string.find(filepath2:GetText():lower(), ".vtf") then
draw.SimpleText("OK✔","vtfont",75,85,Color(0, 255, 17))
else
draw.SimpleText("Waiting...","vtfont",75,85,Color(255, 255, 0))
end
end

filepath2=vgui.Create("DTextEntry",vtf.runmenu)
filepath2:SetPos(12,55)
filepath2:SetSize(295,18)
filepath2:SetDrawLanguageID(false)

filepath2.Paint=function(s,w,h)
s:DrawTextEntryText(Color(255,255,255,204), Color(70, 70, 255), Color(0,0,0))
surface.SetDrawColor(110,110,110,100)
surface.DrawRect(0,0,w,h)
end

secretkey=vgui.Create("DTextEntry",vtf.runmenu)
secretkey:SetPos(12,165)
secretkey:SetSize(295,18)
secretkey:SetDrawLanguageID(false)

secretkey.Paint=function(s,w,h)
s:DrawTextEntryText(Color(255,255,255,204), Color(70, 70, 255), Color(0,0,0))
surface.SetDrawColor(110,110,110,100)
surface.DrawRect(0,0,w,h)
end

offset=vgui.Create("DTextEntry",vtf.runmenu)
offset:SetPos(12,225)
offset:SetSize(295,18)
offset:SetDrawLanguageID(false)

offset.Paint=function(s,w,h)
s:DrawTextEntryText(Color(255,255,255,204), Color(70, 70, 255), Color(0,0,0))
surface.SetDrawColor(110,110,110,100)
surface.DrawRect(0,0,w,h)
end

runcode=vgui.Create("DButton",vtf.runmenu)
runcode:SetPos(12,267)
runcode:SetSize(187,35)
runcode:SetFont("vtfont2")
runcode:SetTextColor(Color( 255, 0, 0, 255 ) )
runcode:SetText("Run")
runcode:SetImage("icon16/application_osx_terminal.png","noclamp")
runcode.DoClick = function()
surface.PlaySound("garrysmod/ui_click.wav")
if filepath2:GetText()!="" and secretkey:GetText()!="" and file.Size(filepath2:GetText(), "BASE_PATH")!=0 and file.Size(filepath2:GetText(), "BASE_PATH")!=nil and file.Exists(filepath2:GetText(), "BASE_PATH") and string.find(filepath2:GetText():lower(), ".vtf") then
exec = ReadCodeFromVTF()
exec()
else
chat.AddText("Something went wrong")
end
end

runcode.Paint=function(s,w,h)
if runcode.Hovered and !runcode:IsDown() then
surface.SetDrawColor(110,110,110,100)
surface.DrawRect(0,0,w,h)
else
surface.SetDrawColor(90,90,90,100)
surface.DrawRect(0,0,w,h)
end
end

concommand.Add("vtf_runmenu", function()
if !vtf.runmenu:IsVisible() then
vtf.runmenu:SetVisible(true)
vtf.runmenu:MakePopup()
else
vtf.runmenu:SetVisible(false)
end
end)

vtf.runmenu.open=0

hook.Add("Think", "vtfrunmenu", function()
if input.IsKeyDown(KEY_HOME) and !LocalPlayer():IsTyping() then
vtf.runmenu.open = vtf.runmenu.open + 1
if vtf.runmenu.open == 1 then
LocalPlayer():ConCommand("vtf_runmenu")
end
else
vtf.runmenu.open = 0
end
end)



