-- EVILMANA.COM PSP LUA CODEBASE
-- www.evilmana.com/tutorials/codebase
-- Browse Files
-- SUBMITTED BY: TacticalPenguin
screen:clear()
screen:print(200,100,"Reassigning Flashes",white)
screen.waitVblankStart()
screen.flip()
System.unassign("flash3:")
System.assign("flash3:","lflash0:0,3","flashfat3:")
playing = "Stopped"
artpath = ""
dp = 0
dirp = "ms0:/"
System.currentDirectory("ms0:/")
white = Color.new(255,255,255)
red = Color.new(255,0,0)
fbrowser = {s = 1, fl = System.listDirectory("ms0:/"),ls = 1, sp = 20}

fbrowser.nf = table.getn(fbrowser.fl)
oldpad = Controls.read()

function runfbrowser()

for i = ((fbrowser.ls-1)*fbrowser.sp)+1, fbrowser.ls*fbrowser.sp do
if fbrowser.nf >= i then
screen:print(0,((i-((fbrowser.ls-1)*fbrowser.sp))*10)-10,
fbrowser.fl[i].name,white)
elseif fbrowser.nf < i then break end
end

screen:print(0,((fbrowser.s-((fbrowser.ls-1)*fbrowser.sp))*10)-10,
fbrowser.fl[fbrowser.s].name,red)

if pad:down() and not oldpad:down() then fbrowser.s = fbrowser.s + 1 end
if pad:up() and not oldpad:up() then fbrowser.s = fbrowser.s - 1 end

if fbrowser.s > fbrowser.nf then fbrowser.s = fbrowser.nf elseif fbrowser.s < 1 then fbrowser.s = 1 end
if fbrowser.s > fbrowser.sp*fbrowser.ls then fbrowser.ls = fbrowser.ls + 1
elseif fbrowser.s < ((fbrowser.ls-1) * fbrowser.sp)+1 then fbrowser.ls = fbrowser.ls - 1 fbrowser.s = (fbrowser.ls)*fbrowser.sp end

if pad:r() and not oldpad:r() then
sysdir = System.currentDirectory()
if not fbrowser.fl[fbrowser.s].directory then
	size = System.getFileSize(sysdir.."/"..fbrowser.fl[fbrowser.s].name)
else
	size = System.getDirSize(sysdir.."/"..fbrowser.fl[fbrowser.s].name)
end
size = size/1024/1024
System.message(fbrowser.fl[fbrowser.s].name.." Size is\n"..size.." MB",0)
end
if pad:cross() and not oldpad:cross() then
if not fbrowser.fl[fbrowser.s].directory then
if string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".pbp" or string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".PBP" then
ebootdir = System.currentDirectory()
img = System.getEboot(ebootdir.."/"..fbrowser.fl[fbrowser.s].name,1)
if img~=0 then
x = -1
y = 0
screen:blit(x,y, img)
screen.flip()
	viewing = true
	while viewing == true do
		pad = Controls.read()
		if pad:up() then
			screen:clear()
			y = y - 4
			screen:blit(x,y, img)
			screen.flip()
		end
		if pad:down() then
			screen:clear()
			y = y + 4
			screen:blit(x,y, img)
			screen.flip()
		end
		if pad:left() then
			screen:clear()
			x = x - 6
			screen:blit(x,y, img)
			screen.flip()
		end
		if pad:right() then
			screen:clear()
			x = x + 6
			screen:blit(x,y, img)
			screen.flip()
		end
		if pad:circle() then
			break
		end
	end
end
elseif string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".ELF" or string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".elf" then
ebootdir = System.currentDirectory()
System.loadElf(ebootdir.."/"..fbrowser.fl[fbrowser.s].name)

elseif string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".prx" or string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".PRX" then
prxdir = System.currentDirectory()
System.loadPrxKernel(prxdir.."/"..fbrowser.fl[fbrowser.s].name)

elseif string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".zip" or string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".ZIP" then
prxdir = System.currentDirectory()
ZIP.extract(prxdir.."/"..fbrowser.fl[fbrowser.s].name, "ms0:/psp/zip","")

elseif string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".jpg" or string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".png" then
imgdir = System.currentDirectory()
img = Image.load(imgdir.."/"..fbrowser.fl[fbrowser.s].name)
x = -1
y = 0
screen:blit(-1,0, img)
screen.flip()
	viewing = true
	while viewing == true do
		pad = Controls.read()
		if pad:circle() then
			break
		end
		if pad:up() then
			screen:clear()
			y = y - 4
			screen:blit(x,y, img)
			screen.flip()
		end
		if pad:down() then
			screen:clear()
			y = y + 4
			screen:blit(x,y, img)
			screen.flip()
		end
		if pad:left() then
			screen:clear()
			x = x - 6
			screen:blit(x,y, img)
			screen.flip()
		end
		if pad:right() then
			screen:clear()
			x = x + 6
			screen:blit(x,y, img)
			screen.flip()
		end
	end

elseif string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".iso" or string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".ISO" then
isodir = System.currentDirectory()
System.startISO(isodir.."/"..fbrowser.fl[fbrowser.s].name)

elseif string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".mp3" then
Mp3medir = System.currentDirectory()
Mp3me.load(fbrowser.fl[fbrowser.s].name)
Mp3me.play()
playing = "Playing"

elseif string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".ogg" then
Mp3medir = System.currentDirectory()
Ogg.load(fbrowser.fl[fbrowser.s].name)
Ogg.play()
playing = "Playing Ogg"

elseif string.lower(string.sub(fbrowser.fl[fbrowser.s].name, -4)) == ".at3" then
Mp3medir = System.currentDirectory()
Aa3me.load(fbrowser.fl[fbrowser.s].name)
Aa3me.play()
playing = "Playing at3"

end
elseif fbrowser.fl[fbrowser.s].directory then
dirp = System.currentDirectory(fbrowser.fl[fbrowser.s].name)
screen:print(280,230,""..dirp.."",red)
screen.flip()
System.sleep(1000)
fbrowser.s = 1
fbrowser.fl=System.listDirectory()
fbrowser.nf=table.getn(fbrowser.fl)
fbrowser.ls = 1
end
elseif pad:l() and not oldpad:l() then
		ebootdir = System.currentDirectory()
		Wlan.startGameShare(ebootdir.."/"..fbrowser.fl[fbrowser.s].name,"HMNETaaa")

elseif pad:triangle() and not oldpad:triangle() then
if System.currentDirectory() ~= "ms0:/" then
dirp = System.currentDirectory("./..")
fbrowser.s = 1
fbrowser.fl=System.listDirectory()
fbrowser.nf=table.getn(fbrowser.fl)
fbrowser.ls = 1
screen:print(280,230,""..System.currentDirectory().."",red)
screen.flip()
System.sleep(1000)
end
end
end

while true do
cpu = System.getCpuSpeed()
bus = System.getBusSpeed()
--fps = System.autoCpu()
pad = Controls.read()
hpad = Hprm.read()
screen:clear()
runfbrowser()
if hpad == "FWD" then
if playing == "Playing" then
	Mp3me.stop()
	fbrowser.s = fbrowser.s + 1
	Mp3medir = System.currentDirectory()
	Mp3me.load(fbrowser.fl[fbrowser.s].name)
	Mp3me.play()
	end
end
if hpad == "BACK" then
if playing == "Playing" then
	Mp3me.stop()
	fbrowser.s = fbrowser.s - 1
	Mp3medir = System.currentDirectory()
	Mp3me.load(fbrowser.fl[fbrowser.s].name)
	Mp3me.play()
	end
end
if hpad == "PLAY" then
	if pause == true then
		Mp3me.pause()
		playing = "Playing"
		pause = false
	else
		Mp3me.pause()
		playing = "Paused"
		pause = true
	end
end
if pad:circle() and not oldpad:circle() then
	if playing == "Playing Ogg" then
	Ogg.stop()
	playing = "Stopped"
	end
	if playing == "Playing" then
	Mp3me.stop()
	playing = "Stopped"
	end
end
if pad:square() and not oldpad:square() then
	if pause == true then
		Mp3me.pause()
		playing = "Playing"
		pause = false
	else
		Mp3me.pause()
		playing = "Paused"
		pause = true
	end
end
screen:print(280,260,"CPU: "..cpu.." BUS: "..bus,red)
screen:print(300,20,artpath,white)
screen:print(300,10,playing,white)
screen.waitVblankStart()
screen.flip()
if pad:select() and not oldpad:select() then
dp = dp + 1
if dp == 1 then
	System.currentDirectory("flash0:/")
	fbrowser = {s = 1, fl = System.listDirectory("flash0:/"),ls = 1, sp = 20}
	fbrowser.nf = table.getn(fbrowser.fl)
	runfbrowser()
end
if dp == 2 then
	System.currentDirectory("flash1:/")
	fbrowser = {s = 1, fl = System.listDirectory("flash1:/"),ls = 1, sp = 20}
	fbrowser.nf = table.getn(fbrowser.fl)
	runfbrowser()
end
if dp == 3 then
	System.currentDirectory("flash2:/")
	fbrowser = {s = 1, fl = System.listDirectory("flash2:/"),ls = 1, sp = 20}
	fbrowser.nf = table.getn(fbrowser.fl)
	runfbrowser()
end
if dp == 4 then
	System.currentDirectory("flash3:/")
	fbrowser = {s = 1, fl = System.listDirectory("flash3:/"),ls = 1, sp = 20}
	fbrowser.nf = table.getn(fbrowser.fl)
	runfbrowser()
end
if dp == 5 then
isok = UMD.checkDisk()
	if isok == 1 then
		UMD.init()
		System.currentDirectory("disc0:/")
		fbrowser = {s = 1, fl = System.listDirectory("disc0:/"),ls = 1, sp = 20}
		fbrowser.nf = table.getn(fbrowser.fl)
		runfbrowser()
	end
end
if dp == 6 then
	System.currentDirectory("ms0:/")
	fbrowser = {s = 1, fl = System.listDirectory("ms0:/"),ls = 1, sp = 20}
	fbrowser.nf = table.getn(fbrowser.fl)
	runfbrowser()
	dp = 0
end
end
if pad:start() then
break
end
oldpad = pad
oldhpad = hpad
--if pad:select() then
--isok = UMD.checkDisk()
		--if isok == 1 then
			--disk = UMD.getSize()
			--ms = System.getDevSize("ms0:")
			--if disk < ms then
			--	isoname = System.startOSK("ms0:/ISO/*.ISO","Enter Name and Path")
			--	if isoname ~= "" then
			--		UMD.ripISO(isoname)
			--	end
			--else
			--	System.message("UMD size is too big for the Memory Stick\nNot Ripping UMD",0)
			--end
		--end
--end
end

width = img:width()
height = img:height()
if (height > 275 or width > 485) then
	screen:clear()
	screen:print(70,100,"Please Wait Resizing Image To fit Screen", white)
	screen.flip()
	function scaleImage(newX, newY, theImage) 
		resizedImage = Image.createEmpty(newX, newY) 
		for x = 1, newX do 
		for y = 1, newY do 
		resizedImage:blit(x,y , theImage,
		math.floor(x*(theImage:width()/newX)),
		math.floor(y*(theImage:height()/newY)),1,1) 
		end 
		end 
		return resizedImage 
	end
	scaleImage(482,270, img) -- Resize image.
	going = true
	while going == true do
		screen:blit(-1,0, resizedImage) -- Show the resized image
		screen.flip()
		screen.waitVblankStart()
		going = false
	end
	viewing = true
	while viewing == true do
		pad = Controls.read()
		if pad:circle() then
			break
		end
	end
end
