#Include <File.au3>
#include <GUIListBox.au3>
#include <GuiConstants.au3>
#Include <GuiTreeView.au3>
#include <Array.au3>
#include <WindowsConstants.au3>

; Setting variables
Opt("GUIDataSeparatorChar", "\")
Global $saveFlag=0
global $goldwavePath, $fsbankPath, $StdDatEdit, $originalISOPath, $modifiedISOPath, $trucha, $songedit, $pakedit, $qbgen, $wadDir

global $tempDir="C:\gh3temp"
;Song Name, Artist, Year, offset, song.ogg, guitar.ogg, rhythm.ogg, notes.chart
global $gh3songdata[70][9]
for $i=0 to 69
	$gh3songdata[$i][5]=@scriptDir&"\files\blank.ogg"
	$gh3songdata[$i][6]=@scriptDir&"\files\blank.ogg"
	$gh3songdata[$i][8]=1
Next

global $toprocess[70]

global $wadsize[70]=[25214976,43237376,31014912,30949376,19709952,36864000,30490624,27443200,23314432,34947072,28475392, _
					29458432,22036480,34013184,31768576,37781504,34783232,28065792,19234816,20480000,26263552,28409856, _
					20938752,32276480,23658496,30949376,27262976,40550400,28573696,26312704,22249472,20561920,17186816, _ 
					25395200,18628608,25116672,29130752,29474816,24576000,20561920,30687232,52133888,27246592,19922944, _ 
					24690688,25935872,16793600,29933568,26656768,25493504,30146560,24461312,21807104,22347776,24870912, _ 
					27688960,28966912,40665088,28852224,25198592,30801920,25083904,26902528,19677184,24608768,37240832, _ 
					25509888,50298880,31588352,25968640]
 


;references to input boxes corresponding to above
dim $gh3songinput[9]


global $tiers[9][4]=[ _
	["Starting out slow","Starting out slow","",17], _
	["Your first real gig","Your first real gig","",19], _ 
	["Making the video","Making the video","",16], _ 
	["European invasion","European invasion","",17], _ 
	["Bighouse Blues","Bighouse Blues","",14], _ 
	["The hottest band on Earth","The hottest band on Earth","",25], _
	["Live in Japan","Live in Japan","",13], _ 
	["Battle for your soul","Battle for your soul","",20], _ 
	["Bonus Songs","Bonus Songs","",11]]
	
global $tiernumber = -1
global $songnumber = -1
$len=0



; songname,qbgen id, songedit line number
global $gh3songs[70][4]=[ _
	["anarchyintheuk",0,1,""], _
	["avalancha",1,0,""], _
	["barracuda",2,2,""], _
	["beforeiforget",3,3,""], _
	["bellyofashark",4,4,""], _
	["blackmagicwoman",5,5,""], _
	["blacksunshine",6,6,""], _
	["bullsonparade",10,10,""], _
	["cantbesaved",11,11,""], _
	["cherubrock",12,12,""], _
	["citiesonflame",13,13,""], _
	["cliffsofdover",14,14,""], _
	["closer",15,15,""], _
	["cultofpersonality",16,17,""], _
	["dontholdback",17,18,""], _
	["downndirty",18,19,""], _
	["evenflow",19,20,""], _
	["fcpremix",20,21,""], _
	["generationrock",21,22,""], _
	["gothatfar",22,23,""], _
	["helicopter",23,24,""], _
	["hierkommtalex",24,25,""], _
	["hitmewithyourbestshot",25,26,""], _
	["holidayincambodia",26,27,""], _
	["imintheband",27,28,""], _
	["impulse",28,29,""], _
	["inlove",29,30,""], _
	["knightsofcydonia",30,32,""], _
	["koolthing",31,31,""], _
	["lagrange",32,33,""], _
	["laydown",33,34,""], _
	["mauvaisgarcon",34,35,""], _
	["metalheavylady",35,36,""], _
	["minuscelsius",36,37,""], _
	["mississippiqueen",37,38,""], _
	["missmurder",38,39,""], _
	["monsters",39,40,""], _
	["mycurse",41,41,""], _
	["mynameisjonas",42,42,""], _
	["nothingformehere",43,43,""], _
	["numberofthebeast",44,44,""], _
	["one",45,45,""], _
	["paintitblack",46,46,""], _
	["paranoid",47,47,""], _
	["prayeroftherefugee",48,48,""], _
	["pridenjoy",49,49,""], _
	["radiosong",50,51,""], _
	["rainingblood",51,50,""], _
	["reptilia",52,52,""], _
	["rocknrollallnite",53,53,""], _
	["rockulikeahurricane",54,54,""], _
	["ruby",55,55,""], _
	["sabotage",56,56,""], _
	["sameoldsonganddance",57,57,""], _
	["schoolsout",58,58,""], _
	["shebangsadrum",59,59,""], _
	["slowride",60,60,""], _
	["storyofmylife",61,61,""], _
	["stricken",62,62,""], _
	["suckmykiss",63,63,""], _
	["sunshineofyourlove",64,64,""], _
	["takethislife",68,68,""], _
	["talkdirtytome",69,69,""], _
	["themetal",70,70,""], _
	["theseeker",71,71,""], _
	["thewayitends",72,72,""], _
	["threesandsevens",73,73,""], _
	["thrufireandflames",74,74,""], _
	["welcometothejungle",88,75,""], _
	["whenyouwereyoung",89,76,""]]
	
global $region=""
	

; Creating GUI and controls
$hGui=GUICreate("GH3 Song Importer GUI", 800, 460, @DesktopWidth/2 - 400, @DesktopHeight/2 - 230, -1)

$filemenu = GuiCtrlCreateMenu ("File")
$fileopen = GuiCtrlCreateMenuitem ("Open Input File...",$filemenu)
$filesave = GuiCtrlCreateMenuitem ("Save Input File...",$filemenu)
$separator1 = GuiCtrlCreateMenuitem ("",$filemenu)
$fileexit = GuiCtrlCreateMenuitem ("Exit",$filemenu)


$hoptions = GUICtrlCreateMenu("Options")
$hextract = GUICtrlCreateMenuItem("Extract files from original ISO?",$hoptions)
GUICtrlSetState(-1,$GUI_CHECKED)
$hreplace = GUICtrlCreateMenuItem("Replace files in modified ISO?",$hoptions)
GUICtrlSetState(-1,$GUI_CHECKED)
$hsonglistedit = GUICtrlCreateMenuItem("Edit song list?",$hoptions)
GUICtrlSetState(-1,$GUI_CHECKED)
$htieredit = GUICtrlCreateMenuItem("Edit tier list?",$hoptions)
GUICtrlSetState(-1,$GUI_CHECKED)

dim $lang[6]
$lang[0] = GUICtrlCreateMenu("Region")
$lang[1] = GUICtrlCreateMenuItem("English",$lang[0],-1,1)
GUICtrlSetState(-1,$GUI_CHECKED)
$lang[2] = GUICtrlCreateMenuItem("French",$lang[0],-1,1)
$lang[3] = GUICtrlCreateMenuItem("German",$lang[0],-1,1)
$lang[4] = GUICtrlCreateMenuItem("Italian",$lang[0],-1,1)
$lang[5] = GUICtrlCreateMenuItem("Spanish",$lang[0],-1,1)

$helpmenu = GuiCtrlCreateMenu ("?")
$aboutitem = GuiCtrlCreateMenuitem ("About",$helpmenu)

GUICtrlCreateGroup("wad, dat, pak, and qb.pak.ngc Directory", 5, 2, 300, 40)


GUICtrlCreateGroup("GoldWave.exe", 5, 46, 300, 40)
$hgoldwave = GUICtrlCreateLabel(@ProgramFilesDir&"\GoldWave\GoldWave.exe",12,62,250,16,-1, 0x00020000)
$hgoldwaveSel = GUICtrlCreateButton("...", 270,  60, 26, 18)
GUICtrlCreateGroup("fsbank.exe", 5, 90, 300, 40)
$hfsbank = GUICtrlCreateLabel("",12,106,250,16,-1, 0x00020000)
$hfsbankSel = GUICtrlCreateButton("...", 270,  104, 26, 18)
GUICtrlCreateGroup("StdDatEdit.exe", 5, 134, 300, 40)
$hStdDatEdit = GUICtrlCreateLabel("",12,150,250,16,-1, 0x00020000)
$hdateditSel = GUICtrlCreateButton("...", 270,  148, 26, 18)
GUICtrlCreateGroup("trucha.exe", 5, 178, 300, 40)
$htrucha = GUICtrlCreateLabel("",12,194,250,16,-1, 0x00020000)
$htruchaSel = GUICtrlCreateButton("...", 270,  192, 26, 18)
GUICtrlCreateGroup("pakedit.exe", 5, 222, 300, 40)
$hpakedit = GUICtrlCreateLabel("",12,238,250,16,-1, 0x00020000)
$hpakeditSel = GUICtrlCreateButton("...", 270,  236, 26, 18)
GUICtrlCreateGroup("qbgen.exe", 5, 266, 300, 40)
$hqbgen = GUICtrlCreateLabel("",12,282,250,16,-1, 0x00020000)
$hqbgenSel = GUICtrlCreateButton("...", 270,  280, 26, 18)
GUICtrlCreateGroup("Original ISO", 5, 310, 300, 40)
$horiginalISO = GUICtrlCreateLabel("",12,326,250,16,-1, 0x00020000)
$horiISOSel = GUICtrlCreateButton("...", 270,  324, 26, 18)
GUICtrlCreateGroup("Modified ISO", 5, 354, 300, 40)
$hmodifiedISO = GUICtrlCreateLabel("",12,370,250,16,-1, 0x00020000)
$hmodISOSel = GUICtrlCreateButton("...", 270,  368, 26, 18)
GUICtrlCreateGroup("songedit.exe", 5, 398, 300, 40)
$hsongedit = GUICtrlCreateLabel("",12,414,250,16,-1, 0x00020000)
$hsongeditSel = GUICtrlCreateButton("...", 270,  412, 26, 18)



GUICtrlCreateGroup("Song Tree", 310, 2, 155, 435)
$hWadDir = GUICtrlCreateLabel("", 12,  18, 250, 16, -1, 0x00020000)
$hWadSel = GUICtrlCreateButton("...", 270,  16, 26, 18)



$maintree = GUICtrlCreateTreeView (320,20,135,410)

$tiers[0][2] = GUICtrlCreateTreeViewItem("Starting out slow", $maintree)
$gh3songs[56][3] = GUICtrlCreateTreeViewItem("slowride",$tiers[0][2])
$gh3songs[62][3] = GUICtrlCreateTreeViewItem("talkdirtytome",$tiers[0][2])
$gh3songs[22][3] = GUICtrlCreateTreeViewItem("hitmewithyourbestshot",$tiers[0][2])
$gh3songs[57][3] = GUICtrlCreateTreeViewItem("storyofmylife",$tiers[0][2])
$gh3songs[49][3] = GUICtrlCreateTreeViewItem("rocknrollallnite",$tiers[0][2])
$gh3songs[52][3] = GUICtrlCreateTreeViewItem("sabotage",$tiers[0][2])

$tiers[1][2] = GUICtrlCreateTreeViewItem("Your first real gig", $maintree)
$gh3songs[34][3] = GUICtrlCreateTreeViewItem("mississippiqueen",$tiers[1][2])
$gh3songs[54][3] = GUICtrlCreateTreeViewItem("schoolsout",$tiers[1][2])
$gh3songs[60][3] = GUICtrlCreateTreeViewItem("sunshineofyourlove",$tiers[1][2])
$gh3songs[2][3] = GUICtrlCreateTreeViewItem("barracuda",$tiers[1][2])
$gh3songs[7][3] = GUICtrlCreateTreeViewItem("bullsonparade",$tiers[1][2])
$gh3songs[48][3] = GUICtrlCreateTreeViewItem("reptilia",$tiers[1][2])

$tiers[2][2] = GUICtrlCreateTreeViewItem("Making the video", $maintree)
$gh3songs[69][3] = GUICtrlCreateTreeViewItem("whenyouwereyoung",$tiers[2][2])
$gh3songs[35][3] = GUICtrlCreateTreeViewItem("missmurder",$tiers[2][2])
$gh3songs[64][3] = GUICtrlCreateTreeViewItem("theseeker",$tiers[2][2])
$gh3songs[30][3] = GUICtrlCreateTreeViewItem("laydown",$tiers[2][2])
$gh3songs[42][3] = GUICtrlCreateTreeViewItem("paintitblack",$tiers[2][2])
$gh3songs[59][3] = GUICtrlCreateTreeViewItem("suckmykiss",$tiers[2][2])

$tiers[3][2] = GUICtrlCreateTreeViewItem("European invasion", $maintree)
$gh3songs[43][3] = GUICtrlCreateTreeViewItem("paranoid",$tiers[3][2])
$gh3songs[0][3] = GUICtrlCreateTreeViewItem("anarchyintheuk",$tiers[3][2])
$gh3songs[28][3] = GUICtrlCreateTreeViewItem("koolthing",$tiers[3][2])
$gh3songs[38][3] = GUICtrlCreateTreeViewItem("mynameisjonas",$tiers[3][2])
$gh3songs[16][3] = GUICtrlCreateTreeViewItem("evenflow",$tiers[3][2])
$gh3songs[10][3] = GUICtrlCreateTreeViewItem("citiesonflame",$tiers[3][2])

$tiers[4][2] = GUICtrlCreateTreeViewItem("Bighouse blues", $maintree)
$gh3songs[23][3] = GUICtrlCreateTreeViewItem("holidayincambodia",$tiers[4][2])
$gh3songs[50][3] = GUICtrlCreateTreeViewItem("rockulikeahurricane",$tiers[4][2])
$gh3songs[53][3] = GUICtrlCreateTreeViewItem("sameoldsonganddance",$tiers[4][2])
$gh3songs[29][3] = GUICtrlCreateTreeViewItem("lagrange",$tiers[4][2])
$gh3songs[68][3] = GUICtrlCreateTreeViewItem("welcometothejungle",$tiers[4][2])
$gh3songs[20][3] = GUICtrlCreateTreeViewItem("helicopter",$tiers[4][2])

$tiers[5][2] = GUICtrlCreateTreeViewItem("The hottest band on Earth", $maintree)
$gh3songs[5][3] = GUICtrlCreateTreeViewItem("blackmagicwoman",$tiers[5][2])
$gh3songs[9][3] = GUICtrlCreateTreeViewItem("cherubrock",$tiers[5][2])
$gh3songs[6][3] = GUICtrlCreateTreeViewItem("blacksunshine",$tiers[5][2])
$gh3songs[63][3] = GUICtrlCreateTreeViewItem("themetal",$tiers[5][2])
$gh3songs[45][3] = GUICtrlCreateTreeViewItem("pridenjoy",$tiers[5][2])
$gh3songs[36][3] = GUICtrlCreateTreeViewItem("monsters",$tiers[5][2])

$tiers[6][2] = GUICtrlCreateTreeViewItem("Live in Japan", $maintree)
$gh3songs[3][3] = GUICtrlCreateTreeViewItem("beforeiforget",$tiers[6][2])
$gh3songs[58][3] = GUICtrlCreateTreeViewItem("stricken",$tiers[6][2])
$gh3songs[66][3] = GUICtrlCreateTreeViewItem("threesandsevens",$tiers[6][2])
$gh3songs[27][3] = GUICtrlCreateTreeViewItem("knightsofcydonia",$tiers[6][2])
$gh3songs[13][3] = GUICtrlCreateTreeViewItem("cultofpersonality",$tiers[6][2])

$tiers[7][2] = GUICtrlCreateTreeViewItem("Battle for your soul", $maintree)
$gh3songs[47][3] = GUICtrlCreateTreeViewItem("rainingblood",$tiers[7][2])
$gh3songs[11][3] = GUICtrlCreateTreeViewItem("cliffsofdover",$tiers[7][2])
$gh3songs[40][3] = GUICtrlCreateTreeViewItem("numberofthebeast",$tiers[7][2])
$gh3songs[41][3] = GUICtrlCreateTreeViewItem("one",$tiers[7][2])

$tiers[8][2] = GUICtrlCreateTreeViewItem("Bonus Songs", $maintree)
$gh3songs[1][3] = GUICtrlCreateTreeViewItem("avalancha",$tiers[8][2])
$gh3songs[4][3] = GUICtrlCreateTreeViewItem("bellyofashark",$tiers[8][2])
$gh3songs[8][3] = GUICtrlCreateTreeViewItem("cantbesaved",$tiers[8][2])
$gh3songs[12][3] = GUICtrlCreateTreeViewItem("closer",$tiers[8][2])
$gh3songs[14][3] = GUICtrlCreateTreeViewItem("dontholdback",$tiers[8][2])
$gh3songs[15][3] = GUICtrlCreateTreeViewItem("downndirty",$tiers[8][2])
$gh3songs[17][3] = GUICtrlCreateTreeViewItem("fcpremix",$tiers[8][2])
$gh3songs[18][3] = GUICtrlCreateTreeViewItem("generationrock",$tiers[8][2])
$gh3songs[19][3] = GUICtrlCreateTreeViewItem("gothatfar",$tiers[8][2])
$gh3songs[21][3] = GUICtrlCreateTreeViewItem("hierkommtalex",$tiers[8][2])
$gh3songs[24][3] = GUICtrlCreateTreeViewItem("imintheband",$tiers[8][2])
$gh3songs[25][3] = GUICtrlCreateTreeViewItem("impulse",$tiers[8][2])
$gh3songs[26][3] = GUICtrlCreateTreeViewItem("inlove",$tiers[8][2])
$gh3songs[31][3] = GUICtrlCreateTreeViewItem("mauvaisgarcon",$tiers[8][2])
$gh3songs[32][3] = GUICtrlCreateTreeViewItem("metalheavylady",$tiers[8][2])
$gh3songs[33][3] = GUICtrlCreateTreeViewItem("minuscelsius",$tiers[8][2])
$gh3songs[37][3] = GUICtrlCreateTreeViewItem("mycurse",$tiers[8][2])
$gh3songs[39][3] = GUICtrlCreateTreeViewItem("nothingformehere",$tiers[8][2])
$gh3songs[44][3] = GUICtrlCreateTreeViewItem("prayeroftherefugee",$tiers[8][2])
$gh3songs[46][3] = GUICtrlCreateTreeViewItem("radiosong",$tiers[8][2])
$gh3songs[51][3] = GUICtrlCreateTreeViewItem("ruby",$tiers[8][2])
$gh3songs[55][3] = GUICtrlCreateTreeViewItem("shebangsadrum",$tiers[8][2])
$gh3songs[61][3] = GUICtrlCreateTreeViewItem("takethislife",$tiers[8][2])
$gh3songs[65][3] = GUICtrlCreateTreeViewItem("thewayitends",$tiers[8][2])
$gh3songs[67][3] = GUICtrlCreateTreeViewItem("thrufireandflames",$tiers[8][2])


;song data
GUICtrlCreateGroup("Song Name", 470, 2, 300, 40)
GUICtrlCreateGroup("Artist", 470, 46, 300, 40)
GUICtrlCreateGroup("Year", 470, 90, 300, 40)
GUICtrlCreateGroup("Offset", 470, 134, 300, 40)
GUICtrlCreateGroup("song.ogg", 470, 178, 300, 40)
GUICtrlCreateGroup("guitar.ogg", 470, 222, 300, 40)
GUICtrlCreateGroup("rhythm.ogg", 470, 266, 300, 40)
GUICtrlCreateGroup(".chart", 470, 310, 300, 40)
GUICtrlCreateGroup("Tier Name",470,354,300,40)

	for $j=0 to 1
		$gh3songinput[$j] = GUICtrlCreateInput("",480,$j*44+18,250,18)
		GUICtrlSetState (-1,$GUI_HIDE)
	Next
	
		$gh3songinput[2] = GUICtrlCreateInput("",480,2*44+18,150,18)
		GUICtrlSetState (-1,$GUI_HIDE)	
		
		$gh3songinput[8] = GUICtrlCreateCheckbox ("Original Artist?", 640, 88+16, 120, 20)
		GUICtrlSetState (-1,$GUI_HIDE)
	
		$gh3songinput[3] = GUICtrlCreateInput("",480,3*44+18,250,18)
		GUICtrlSetState (-1,$GUI_HIDE)	
	
	for $j=4 to 7
		$gh3songinput[$j] = GUICtrlCreateLabel("",480,$j*44+20,250,18,-1, 0x00020000)
		GUICtrlSetState (-1,$GUI_HIDE)
	Next

$itier=GUICtrlCreateInput("",480,370,200,18)
GUICtrlSetState (-1,$GUI_HIDE)
$hleft = GUICtrlCreateLabel("Left:0",700,370,60,18,-1)
GUICtrlSetState (-1,$GUI_HIDE)

dim $hSel[4]
for $i = 0 to 3 step 1
$hSel[$i] = GUICtrlCreateButton("...", 735,  192+44*$i, 26, 18)
GUICtrlSetState (-1,$GUI_HIDE)
next

$hprocess = GUICtrlCreateButton("Start Processing",550,400,90,30)

GUISetState()

While 1
			for $j=0 to 8 step 1
				if $songnumber > -1 then $gh3songdata[$songnumber][$j]=GUICtrlRead($gh3songinput[$j])
			Next
			
			if $tiernumber > -1 then $tiers[$tiernumber][1]=GUICtrlRead($itier)

$goldwavePath=GUICtrlRead($hgoldwave)
$fsbankPath=GUICtrlRead($hfsbank)
$StdDatEdit=GUICtrlRead($hstddatedit)
$songsDir=""
$originalISOPath=GUICtrlRead($horiginalISO)
$modifiedISOPath=GUICtrlRead($hmodifiedISO)
;$tempDir=""
;$oggPath=""
$trucha=GUICtrlRead($htrucha)
$songedit=GUICtrlRead($hsongedit)
$pakedit=GUICtrlRead($hpakedit)
$qbgen=GUICtrlRead($hqbgen)
$wadDir=GUICtrlRead($hwadDir)
;$owadDir=""

if (fileexists($goldwavepath) and fileexists($fsbankpath) and fileexists($StdDatEdit) and fileexists($qbgen) and (fileexists($trucha) or (BitAnd(GUICtrlRead($hextract),$GUI_UNCHECKED) = $GUI_UNCHECKED and BitAnd(GUICtrlRead($hreplace),$GUI_UNCHECKED) = $GUI_UNCHECKED)) _ 
	and (fileexists($waddir) or BitAnd(GUICtrlRead($hextract),$GUI_CHECKED) = $GUI_CHECKED) and (fileexists($pakedit) or BitAnd(GUICtrlRead($hsonglistedit),$GUI_UNCHECKED) = $GUI_UNCHECKED) and (fileexists($originalISOpath) or _
	BitAnd(GUICtrlRead($hextract),$GUI_UNCHECKED) = $GUI_UNCHECKED) and (fileexists($modifiedISOpath) or BitAnd(GUICtrlRead($hreplace),$GUI_UNCHECKED) = $GUI_UNCHECKED)) then
		if BitAND(GUICtrlGetState($hprocess),$GUI_DISABLE) = $GUI_DISABLE then GUICtrlSetState($hprocess,$GUI_ENABLE)
	Else
		if BitAND(GUICtrlGetState($hprocess),$GUI_ENABLE) = $GUI_ENABLE then GUICtrlSetState($hprocess,$GUI_DISABLE)
EndIf
	


$iMsg = GUIGetMsg()

    Select
	Case $imsg = $hsel[0]
			$sTmpFile = FileOpenDialog("Select the song file to replace "&$gh3songs[$songnumber][0], "", "Ogg Files (*.ogg)",1,"song.ogg")
            If @error Then ContinueLoop
            GUICtrlSetData($gh3songinput[4], $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			$filedir=StringTrimRight($sTmpFile,StringLen($sTmpFile)-StringInStr($sTmpFile,"\",0,-1)+1)
			$FileList=_FileListToArray($fileDir,"*",1)
			_ArrayDelete($FileList,0)		
			for $nfile in $FileList
				if $nfile == "guitar.ogg" Then GUICtrlSetData($gh3songinput[5],$filedir&"\"&$nfile)
				if $nfile == "rhythm.ogg" Then GUICtrlSetData($gh3songinput[6],$filedir&"\"&$nfile)
				if StringMid($nfile,StringLen($nfile)-4) == "chart" then GUICtrlSetData($gh3songinput[7],$filedir&"\"&$nfile)
				if $nfile == "song.ini" Then
					GUICtrlSetData($gh3songinput[0],iniread($filedir&"\"&$nfile,"song","name",""))
					GUICtrlSetData($gh3songinput[1],iniread($filedir&"\"&$nfile,"song","artist",""))
					GUICtrlSetData($gh3songinput[3],iniread($filedir&"\"&$nfile,"song","delay",""))
				EndIf
				if $nfile == "info.txt" then
					GUICtrlSetData($gh3songinput[0],filereadline($filedir&"\"&$nfile,1))
					GUICtrlSetData($gh3songinput[1],filereadline($filedir&"\"&$nfile,2))
					GUICtrlSetData($gh3songinput[2],filereadline($filedir&"\"&$nfile,3))
				EndIf
			next
			
			
		
	Case $imsg=$hsel[1]
			$sTmpFile = FileOpenDialog("Select the guitar file to replace "&$gh3songs[$songnumber][0], "", "Ogg Files (*.ogg)",1,"guitar.ogg")
            If @error Then ContinueLoop
            GUICtrlSetData($gh3songinput[5], $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1		
		
	Case $imsg = $hsel[2]
			$sTmpFile = FileOpenDialog("Select the rhythm file to replace "&$gh3songs[$songnumber][0], "", "Ogg Files (*.ogg)",1,"rhythm.ogg")
            If @error Then ContinueLoop
            GUICtrlSetData($gh3songinput[6], $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1		
		
	Case $imsg = $hsel[3]
			$sTmpFile = FileOpenDialog("Select the chart file to replace "&$gh3songs[$songnumber][0], "", "Chart Files (*.chart)",1,"notes.chart")
            If @error Then ContinueLoop
            GUICtrlSetData($gh3songinput[7], $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1	
		
		
        Case $iMsg = $hWadSel
            $sTmpFile = FileOpenDialog("Select any .wad file:", "", "Wad Files (*.wad.ngc)")
            If @error Then ContinueLoop
			While StringMid($sTmpFile,StringLen($sTmpFile)) <> "\"
				$sTmpFile = StringTrimRight($sTmpFile,1)
			WEnd
			$sTmpFile = StringTrimRight($sTmpFile,1)
            GUICtrlSetData($hWadDir, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
			
			
			
			
		        Case $iMsg = $hWadSel
              $sTmpFile = FileOpenDialog("Select any .wad file:", "", "Wad Files (*.wad.ngc)")
            If @error Then ContinueLoop
			While StringMid($sTmpFile,StringLen($sTmpFile)) <> "\"
				$sTmpFile = StringTrimRight($sTmpFile,1)
			WEnd
            GUICtrlSetData($hWadDir, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			

			Case $iMsg = $hgoldwaveSel
			            $sTmpFile = FileOpenDialog("Select Goldwave executable:", "", "Executables (*.exe)",1,"GoldWave.exe")
            If @error Then ContinueLoop
            GUICtrlSetData($hgoldwave, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
			        Case $iMsg = $hfsbankSel
            $sTmpFile = FileOpenDialog("Select fsbank executable:", "", "Executables (*.exe)",1,"fsbank.exe")
            If @error Then ContinueLoop
            GUICtrlSetData($hfsbank, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
			        Case $iMsg = $hdateditSel
            $sTmpFile = FileOpenDialog("Select StdDatEdit executable:", "", "Executables (*.exe)",1,"stddatedit.exe")
            If @error Then ContinueLoop
            GUICtrlSetData($hstddatedit, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
			        Case $iMsg = $htruchaSel
            $sTmpFile = FileOpenDialog("Select trucha executable:", "", "Executables (*.exe)",1,"trucha.exe")
            If @error Then ContinueLoop
            GUICtrlSetData($htrucha, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
			        Case $iMsg = $hpakeditSel
            $sTmpFile = FileOpenDialog("Select pakedit executable:", "", "Executables (*.exe)",1,"pakedit.exe")
            If @error Then ContinueLoop
            GUICtrlSetData($hpakedit, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
			        Case $iMsg = $hqbgenSel
            $sTmpFile = FileOpenDialog("Select qbgen executable:", "", "Executables (*.exe)",1,"qbgen.exe")
            If @error Then ContinueLoop
            GUICtrlSetData($hqbgen, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
			        Case $iMsg = $horiISOSel
            $sTmpFile = FileOpenDialog("Select original ISO file:", "", "ISO's (*.iso)",1)
            If @error Then ContinueLoop
            GUICtrlSetData($horiginalISO, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
						        Case $iMsg = $hmodISOSel
            $sTmpFile = FileOpenDialog("Select modified ISO file:", "", "ISO's (*.iso)")
            If @error Then ContinueLoop
            GUICtrlSetData($hmodifiedISO, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1
			
						        Case $iMsg = $hsongeditSel
            $sTmpFile = FileOpenDialog("Select songedit executable:", "", "Executables (*.exe)",1,"songedit.exe")
            If @error Then ContinueLoop
            GUICtrlSetData($hsongedit, $sTmpFile); GUI will be updated at next iteration
			$saveFlag=1

        Case $iMsg = $GUI_EVENT_CLOSE or $imsg = $fileexit
			
			if $saveFlag==1 Then
				Dim $iMsgBoxAnswer
				$iMsgBoxAnswer = MsgBox(35,"File Not Saved...","Exit without saving?")
				Select
					Case $iMsgBoxAnswer = 6 ;Yes
						Exit
					Case $iMsgBoxAnswer = 7 ;No
						$savefile=FileSaveDialog("Save input File As...","","Text File (*.txt)",16,"input.txt")
						_saveFunc($savefile)						
						Exit
					Case $iMsgBoxAnswer = 2 ;Cancel
				EndSelect
			Else
				Exit
			EndIf
						
		Case $iMsg = $filesave
			$savefile=FileSaveDialog("Save input File As...","","Text File (*.txt)",16,"input.txt")
			If @error Then ContinueLoop
			_saveFunc($savefile)	
			$saveflag=0
		
		Case $imsg = $fileopen
			$file = FileOpenDialog("Choose input File...","","Text File (*.txt)",1,"input.txt")
			If @error Then ContinueLoop
			_openFunc($file)			
			$saveflag = 0
		
	Case $imsg = $lang[1] 
		$region = ""
		GUICtrlSetState($htieredit,$GUI_ENABLE)
	Case $imsg = $lang[2] 
		$region = "_f"
		GUICtrlSetState($htieredit,BitOr($GUI_UNCHECKED,$GUI_DISABLE))
	Case $imsg = $lang[3] 
		$region = "_g"
		GUICtrlSetState($htieredit,BitOr($GUI_UNCHECKED,$GUI_DISABLE))
	Case $imsg = $lang[4] 
		$region = "_i"
		GUICtrlSetState($htieredit,BitOr($GUI_UNCHECKED,$GUI_DISABLE))
	Case $imsg = $lang[5] 
		$region = "_s"
		GUICtrlSetState($htieredit,BitOr($GUI_UNCHECKED,$GUI_DISABLE))
		
		
		Case $imsg = $aboutitem
			Msgbox(0,"About","Source code: th3l0rd and varneymi"&@CRLF&"Help available on scorehero forums")	
			
		Case $imsg = $tiers[8][2]
			for $j=0 to 8 step 1
					GUIctrlSetState ($gh3songinput[$j],$GUI_HIDE)
			Next
			GUIctrlSetState ($itier,$GUI_HIDE)
			for $j=0 to 3 step 1
				GUIctrlSetState ($hsel[$j],$GUI_HIDE)
			Next
			
		Case $imsg = $hoptions
			for $j=0 to 8 step 1
					GUIctrlSetState ($gh3songinput[$j],$GUI_HIDE)
			Next
			GUIctrlSetState ($itier,$GUI_HIDE)
			for $j=0 to 3 step 1
				GUIctrlSetState ($hsel[$j],$GUI_HIDE)
			Next
			
		Case $imsg = $hextract
			for $j=0 to 8 step 1
					GUIctrlSetState ($gh3songinput[$j],$GUI_HIDE)
			Next
			GUIctrlSetState ($itier,$GUI_HIDE)
			for $j=0 to 3 step 1
				GUIctrlSetState ($hsel[$j],$GUI_HIDE)
			Next
			If BitAnd(GUICtrlRead($hextract),$GUI_CHECKED) = $GUI_CHECKED Then
			GUICtrlSetState($hextract,$GUI_UNCHECKED)
			Else
			GUICtrlSetState($hextract,$GUI_CHECKED)
		EndIf
		
				Case $imsg = $hreplace
			for $j=0 to 8 step 1
					GUIctrlSetState ($gh3songinput[$j],$GUI_HIDE)
			Next
			GUIctrlSetState ($itier,$GUI_HIDE)
			for $j=0 to 3 step 1
				GUIctrlSetState ($hsel[$j],$GUI_HIDE)
			Next
			If BitAnd(GUICtrlRead($hreplace),$GUI_CHECKED) = $GUI_CHECKED Then
			GUICtrlSetState($hreplace,$GUI_UNCHECKED)
			Else
			GUICtrlSetState($hreplace,$GUI_CHECKED)
		EndIf
		
		Case $imsg = $hsonglistedit
			for $j=0 to 8 step 1
					GUIctrlSetState ($gh3songinput[$j],$GUI_HIDE)
			Next
			GUIctrlSetState ($itier,$GUI_HIDE)
			for $j=0 to 3 step 1
				GUIctrlSetState ($hsel[$j],$GUI_HIDE)
			Next
			If BitAnd(GUICtrlRead($hsonglistedit),$GUI_CHECKED) = $GUI_CHECKED Then
			GUICtrlSetState($hsonglistedit,$GUI_UNCHECKED)
			Else
			GUICtrlSetState($hsonglistedit,$GUI_CHECKED)
		EndIf
		
				Case $imsg = $htieredit
			for $j=0 to 8 step 1
					GUIctrlSetState ($gh3songinput[$j],$GUI_HIDE)
			Next
			GUIctrlSetState ($itier,$GUI_HIDE)
			for $j=0 to 3 step 1
				GUIctrlSetState ($hsel[$j],$GUI_HIDE)
			Next
			If BitAnd(GUICtrlRead($htieredit),$GUI_CHECKED) = $GUI_CHECKED Then
			GUICtrlSetState($htieredit,$GUI_UNCHECKED)
			Else
			GUICtrlSetState($htieredit,$GUI_CHECKED)
		EndIf
			
			
		Case $imsg = $hprocess
			if $saveFlag==1 Then
				Dim $iMsgBoxAnswer
				$iMsgBoxAnswer = MsgBox(35,"File Not Saved...","Start processing without saving?")
				Select
					Case $iMsgBoxAnswer = 6 ;Yes
						_process()
					Case $iMsgBoxAnswer = 7 ;No
						$savefile=FileSaveDialog("Save input File As...","","Text File (*.txt)",16,"input.txt")
						_saveFunc($savefile)
						_process()
					Case $iMsgBoxAnswer = 2 ;Cancel
					EndSelect
				Else
			_process()
			endif

            
		EndSelect


	for $i = 0 to 7 step 1
		if $imsg = $tiers[$i][2] then
			for $j=0 to 8 step 1
				GUIctrlSetState ($gh3songinput[$j],$GUI_HIDE)
			Next			
			for $j=0 to 3 step 1
				GUIctrlSetState ($hsel[$j],$GUI_HIDE)
			Next
			$tiernumber=$i
			$songnumber=-1
			GUICtrlSetData($itier,$tiers[$tiernumber][1])
			GUIctrlSetState ($itier,$GUI_SHOW)
			GUIctrlsetstate($hleft,$GUI_SHOW)

		EndIf
	Next
	

	for $i = 0 to 69 step 1
			if $imsg==$gh3songs[$i][3] Then
				GUIctrlSetState ($itier,$GUI_HIDE)
				GUIctrlsetstate($hleft,$GUI_HIDE)
				$tiernumber=-1
				$songnumber=$i
				for $k = 0 to 7 step 1
					GUICtrlSetData($gh3songinput[$k],$gh3songdata[$songnumber][$k])
				Next
				GUICtrlSetState($gh3songinput[8],$gh3songdata[$songnumber][8])
				for $j = 0 to 8 step 1
					GUIctrlSetState ($gh3songinput[$j],$GUI_SHOW)
				Next
				for $j=0 to 3 step 1
					GUIctrlSetState ($hsel[$j],$GUI_SHOW)
				Next
			EndIf

		Next
		
		if $tiernumber > -1 and $len <> StringLen(GUICtrlRead($itier)) Then
			$len = StringLen(GUICtrlRead($itier))
			if $len > $tiers[$tiernumber][3] Then
				GUICtrlSetData($hleft,"Too Long")
			Else
				GUICtrlSetData($hleft,"Left: "&$tiers[$tiernumber][3]-$len)
			EndIf
		EndIf
		
				
		

			
Wend
    


















Func _saveFunc($filename)
	;Very simplistic Save function, need to update with a more robust version
	$hfile=FileOpen($filename,2)
FileWriteLine($hfile,"Input File for GH3 Song Importer GUI")
FileClose($hfile)
$hfile=FileOpen($filename,1)

FileWriteLine($hfile,$goldwavePath)
FileWriteLine($hfile,$fsbankPath)
FileWriteLine($hfile,$StdDatEdit)
FileWriteLine($hfile,$originalISOPath)
FileWriteLine($hfile,$modifiedISOPath)
FileWriteLine($hfile,$trucha)
FileWriteLine($hfile,$songedit)
FileWriteLine($hfile,$pakedit)
FileWriteLine($hfile,$qbgen)
FileWriteLine($hfile,$wadDir)
for $i = 0 to 7
	FileWriteLine($hfile,$tiers[$i][1])
next
for $i = 0 to 69
	for $j = 0 to 7
		FileWriteLine($hfile,$gh3songdata[$i][$j])
	Next
Next
FileWriteLine($hfile,GUICtrlRead($hextract))
FileWriteLine($hfile,GUICtrlRead($hreplace))
FileWriteLine($hfile,GUICtrlRead($hsonglistedit))
FileWriteLine($hfile,GUICtrlRead($htieredit))
for $i = 0 to 69
	FileWriteLine($hfile,$gh3songdata[$i][8])
Next
FileWriteLine($hfile,$region)
FileClose($hfile)
$saveflag=0
EndFunc




Func _openFunc($filename)
		;Very simplistic Open function, need to update with a more robust version
	$hfile=FileOpen($filename,0)
FileReadLine($hfile)

GUICtrlSetData($hgoldwave,FileReadLine($hfile))
GUICtrlSetData($hfsbank,FileReadLine($hfile))
GUICtrlSetData($hstddatedit,FileReadLine($hfile))
GUICtrlSetData($horiginalISO,FileReadLine($hfile))
GUICtrlSetData($hmodifiedISO,FileReadLine($hfile))
GUICtrlSetData($htrucha,FileReadLine($hfile))
GUICtrlSetData($hsongedit,FileReadLine($hfile))
GUICtrlSetData($hpakedit,FileReadLine($hfile))
GUICtrlSetData($hqbgen,FileReadLine($hfile))
GUICtrlSetData($hwadDir,FileReadLine($hfile))
for $i = 0 to 7
	$tiers[$i][1]=FileReadLine($hfile)
next
for $i = 0 to 69
	for $j = 0 to 7
		$gh3songdata[$i][$j] = FileReadLine($hfile)
	Next
Next

GUICtrlsetState($hextract,FileReadLine($hfile))
GUICtrlsetState($hreplace,FileReadLine($hfile))
GUICtrlsetState($hsonglistedit,FileReadLine($hfile))
GUICtrlsetState($htieredit,FileReadLine($hfile))
for $i = 0 to 69
$gh3songdata[$i][8]=FileReadLine($hfile)
Next
$region=FileReadLine($hfile)
for $i = 1 to 5
	GUICtrlSetState($lang[$i],$GUI_UNCHECKED)
Next
if $region=="" Then GUICtrlSetState($lang[1],$GUI_CHECKED)
if $region=="_f" Then GUICtrlSetState($lang[2],$GUI_CHECKED)
if $region=="_g" Then GUICtrlSetState($lang[3],$GUI_CHECKED)
if $region=="_i" Then GUICtrlSetState($lang[4],$GUI_CHECKED)
if $region=="_s" Then GUICtrlSetState($lang[5],$GUI_CHECKED)
FileClose($hfile)
$saveflag=0
if $tiernumber > -1 then GUICtrlSetData($itier,$tiers[$tiernumber][1])
if $songnumber > -1 Then
	for $i = 0 to 7 step 1
		GUICtrlSetData($gh3songinput[$i],$gh3songdata[$songnumber][$i])
	Next
	GUICtrlSetState($gh3songinput[8],$gh3songdata[$songnumber][8])
EndIf
EndFunc



Func _GUICtrlTreeView_Find($hWnd, $sPath, $hStart = 0)
	Local $iIndex, $aParts, $sDelimiter = Opt("GUIDataSeparatorChar")
	$iIndex = 1
	$aParts = StringSplit($sPath, $sDelimiter)
	If $hStart = 0 Then $hStart = _GUICtrlTreeView_GetFirstItem($hWnd)
	While ($iIndex <= $aParts[0]) And ($hStart <> 0)
		If StringStripWS(_GUICtrlTreeView_GetText($hWnd, $hStart), 3) = StringStripWS($aParts[$iIndex], 3) Then
			If $iIndex = $aParts[0] Then Return $hStart
			$iIndex += 1
			$hStart = _GUICtrlTreeView_GetFirstChild($hWnd, $hStart)
		Else
			$hStart = _GUICtrlTreeView_GetNextSibling($hWnd, $hStart)
		EndIf
	WEnd
EndFunc














Func _ExtractWads()
	$gui = "Trucha Signer 0.21"
	$treeview = "SysTreeView321"	
	Run($trucha)
	Sleep(100)
	WinWait($gui)
	Sleep(100)
	Send("!S")
	Sleep(100)
	Send("c")
	Sleep(100)
	Send("{ENTER}")
	Sleep(100)
	Send("!i")
	Sleep(100)
	Send("{ENTER}")
	Sleep(100)
	WinWaitActive("[ACTIVE]")
	sleep(100)
	controlSetText("","","Edit1",$originalISOPath)
	Sleep(100)
	Send("{ENTER}")
	Sleep(100)
	WinWait("Operation completed")
	Sleep(100)
	Send("{ENTER}")
	$hWnd = ControlGetHandle( "", "", $treeview )
      for $i in $toprocess
	  Local $hItem = _GUICtrlTreeView_Find( $hWnd, "DISC (RGHE52)\PARTITION (RGHE52)\ROOT\music\"&$gh3songs[$i][0]&".dat.ngc" )
	  if ($hItem<>0) Then
		_GUICtrlTreeView_SelectItem( $hWnd, $hItem )
		_GUICtrlTreeView_ClickItem($hWnd, $hItem, "right", True)
		Sleep(100)
		Send("e")
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		Sleep(100)
		ControlSetText("[ACTIVE]","","Edit1",$wadDir);
		Sleep(100)
		Send("{ENTER}")
		WinWaitClose( "Processing...")
		WinWaitActive($gui)
	EndIf	
		  Local $hItem = _GUICtrlTreeView_Find( $hWnd, "DISC (RGHE52)\PARTITION (RGHE52)\ROOT\music\"&$gh3songs[$i][0]&".wad.ngc" )
	  if ($hItem<>0) Then
		_GUICtrlTreeView_SelectItem( $hWnd, $hItem )
		_GUICtrlTreeView_ClickItem($hWnd, $hItem, "right", True)
		Sleep(100)
		Send("e")
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		Sleep(100)
		ControlSetText("[ACTIVE]","","Edit1",$wadDir);
		Sleep(100)
		Send("{ENTER}")
		WinWaitClose( "Processing...")
		WinWaitActive($gui)
      EndIf	
      Local $hItem = _GUICtrlTreeView_Find( $hWnd, "DISC (RGHE52)\PARTITION (RGHE52)\ROOT\songs\"&$gh3songs[$i][0]&$region&".pak.ngc")
      if ($hItem<>0) Then
		_GUICtrlTreeView_SelectItem( $hWnd, $hItem )
		_GUICtrlTreeView_ClickItem($hWnd, $hItem, "right", True)
		Sleep(100)
		Send("e")
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		Sleep(100)
		ControlSetText("[ACTIVE]","","Edit1",$wadDir);
		Sleep(100)
		Send("{ENTER}")
		WinWaitClose( "Processing...")
		WinWaitActive($gui)
	EndIf		    
	next
    Local $hItem = _GUICtrlTreeView_Find( $hWnd, "DISC (RGHE52)\PARTITION (RGHE52)\ROOT\pak\qb"&$region&".pak.ngc" )
      if ($hItem<>0) Then
		_GUICtrlTreeView_SelectItem( $hWnd, $hItem )
		_GUICtrlTreeView_ClickItem($hWnd, $hItem, "right", True)
		Sleep(100)
		Send("e")
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		Sleep(100)
		ControlSetText("[ACTIVE]","","Edit1",$wadDir);
		Sleep(100)
		Send("{ENTER}")
		WinWaitClose( "Processing...")
		WinWaitActive($gui)
	EndIf		
	WinClose($gui)
	
EndFunc

Func _ReplaceWads()
	$gui = "Trucha Signer 0.21"
	$treeview = "SysTreeView321"	
	$partition = "PARTITION (RGHE52)"
	Run($trucha)
	Sleep(100)
	WinWaitActive($gui)
	Sleep(100)
	Send("!S")
	Sleep(100)
	Send("c")
	Sleep(100)
	Send("{ENTER}")
	Sleep(100)
	Send("!i")
	Sleep(100)
	Send("{ENTER}")
	Sleep(100)
	WinWaitActive("[ACTIVE]")
	sleep(100)
	controlSetText("","","Edit1",$modifiedISOPath)
	Sleep(100)
	Send("{ENTER}")
	Sleep(100)
	WinWait("Operation completed")
	Sleep(100)
	Send("{ENTER}")
	$hWnd = ControlGetHandle( "", "", $treeview )
	;$FileList=_FileListToArray($wadDir,"*",1)
	;_ArrayDelete($FileList,0)		
	for $i in $toprocess
	    for $j=1 to 3
			if $j==1 then $nfile = $gh3songs[$i][0]&".dat.ngc"
			if $j==2 then $nfile = $gh3songs[$i][0]&".wad.ngc"
			if $j==3 then $nfile = $gh3songs[$i][0]&$region&".pak.ngc"

			If StringMid($nfile,StringLen($nfile)-6,3)=="dat" or StringMid($nfile,StringLen($nfile)-6,3)=="wad" Then
				Local $hItem = _GUICtrlTreeView_Find( $hWnd, "DISC (RGHE52)\PARTITION (RGHE52)\ROOT\music\"&$nfile )
			Else
				If StringMid($nfile,StringLen($nfile)-6,3)=="pak" Then
					Local $hItem = _GUICtrlTreeView_Find( $hWnd, "DISC (RGHE52)\PARTITION (RGHE52)\ROOT\songs\"&$nfile )
				Else
					$hItem=0
				EndIf
			EndIf

	    if ($hItem) Then
	       _GUICtrlTreeView_SelectItem( $hWnd, $hItem )
	       _GUICtrlTreeView_ClickItem($hWnd, $hItem, "right", True)
		   Sleep(100)
	       Send("r")
		   Sleep(100)
	       WinWaitActive("[ACTIVE]")
		   Sleep(100)
	       ControlSetText("[ACTIVE]","","Edit1",$wadDir&"\"&$nfile);
	Sleep(100)
	       Send("{ENTER}")
	Sleep(100)
	       WinWaitActive("[CLASS:#32770]")
	Sleep(100)
	       Send("{ENTER}")
	Sleep(100)
	       WinWaitActive($gui)
	Sleep(100)
EndIf
	Next
Next

Local $hItem = _GUICtrlTreeView_Find( $hWnd, "DISC (RGHE52)\PARTITION (RGHE52)\ROOT\pak\qb"&$region&".pak.ngc" )


	    if ($hItem) Then
	       _GUICtrlTreeView_SelectItem( $hWnd, $hItem )
	       _GUICtrlTreeView_ClickItem($hWnd, $hItem, "right", True)
		   Sleep(100)
	       Send("r")
		   Sleep(100)
	       WinWaitActive("[ACTIVE]")
		   Sleep(100)
	       ControlSetText("[ACTIVE]","","Edit1",$wadDir&"\qb"&$region&".pak.ngc");
	Sleep(100)
	       Send("{ENTER}")
	Sleep(100)
	       WinWaitActive("[CLASS:#32770]")
	Sleep(100)
	       Send("{ENTER}")
	Sleep(100)
	       WinWaitActive($gui)
	Sleep(100)
	EndIf

	Sleep(100)
	Local $hItem = _GUICtrlTreeView_Find( $hWnd, "DISC (RGHE52)\PARTITION (RGHE52)" )
	if ($hItem) Then
		_GUICtrlTreeView_SelectItem( $hWnd, $hItem )
		_GUICtrlTreeView_ClickItem($hWnd, $hItem, "right", True)
		Sleep(100)
		Send("t")
		Sleep(100)
		WinWaitActive("[CLASS:#32770]")
		Sleep(100)
		Send("{ENTER}")
		Sleep(100)
		WinWaitActive($gui)
	Else
		MsgBox(0,"","Error: Cannot find the partition to sign")
	EndIf	
	WinClose($gui)
	Sleep(100)
EndFunc

Func _Rebuildqb()
	Run($pakedit)
	WinWait("PAK Editor GUI 0.1")
	Sleep(100)
	ControlClick("","","Button1","left",1)
	Sleep(100)
	WinWaitActive("[ACTIVE]")
	Sleep(100)
	ControlSetText("","","Edit1",$wadDir&"\qb"&$region&".pak.ngc")
	Sleep(100)
	ControlCommand("","","ComboBox3","SetCurrentSelection",1)
	Sleep(100)
	ControlClick("","","Button2","left",1)
	Sleep(100)
	WinWaitActive("PAK Editor GUI 0.1")
	$hWnd=ControlGetHandle("PAK Editor GUI 0.1","","SysTreeView321")
	Local $hItem = _GUICtrlTreeView_Find( $hWnd, "cripts\guitar\songlist.qb.ngc" )
	if ($hItem<>0) Then
		_GUICtrlTreeView_SelectItem( $hWnd, $hItem )
		_GUICtrlTreeView_ClickItem( $hWnd, $hItem, "left",1 )
		ControlClick("PAK Editor GUI 0.1","","Button2","left",1)
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		sleep(500)
		Send($wadDir&"\songlist.qb.ngc")
		sleep(500)
		ControlClick("","","Button2","left",1)
		Sleep(100)
		WinWaitActive("PAK Editor GUI 0.1")
		sleep(500)
		ControlClick("","","Button5","left",1)
		Sleep(100)
		WinWaitActive("PAK Editor GUI 0.1")
		sleep(500)
		ControlClick("","","Button3","left",1)
		sleep(500)
	EndIf
	WinClose("PAK Editor GUI 0.1")
	Sleep(100)	
EndFunc























Func _process()
	If @OSVersion <> "WIN_98" And @OSVersion <> "WIN_ME" Then
    BlockInput(1)
EndIf
	global $toprocess[70]
	$total=70
	for $i= 69 to 0 step -1
		$toprocess[$i]=$i
		if $gh3songdata[$i][4]=="" or $gh3songdata[$i][7]=="" then 
			_arraydelete($toprocess,$i)
			$total-=1
		EndIf
	Next
	WinMinimizeAll()
	ProgressOn("Progress:","","",@DesktopWidth-320,@DesktopHeight-170)
	GUISetState(@SW_HIDE)
	
	if not FileExists($tempdir) then 
		dirCreate($tempdir)
	EndIf
	If BitAnd(GUICtrlRead($hextract),$GUI_CHECKED) = $GUI_CHECKED Then
		ProgressSet(0,"This may take a while...","Extracting files from ISO")
		$wadDir = $tempDir&"\wad"
		if not FileExists($waddir) then 
			dirCreate($waddir)
		EndIf
		_extractWads()
	EndIf
	If (not FileExists($wadDir&"\songlist.qb.ngc")) and BitAnd(GUICtrlRead($hsonglistedit),$GUI_CHECKED) = $GUI_CHECKED then
		ProgressSet(0,"","Extracting songlist...")
			Run($pakedit)
			WinWait("PAK Editor GUI 0.1")
			Sleep(200)
			ControlClick("PAK Editor GUI 0.1","","Button1","left",1)
			Sleep(100)
			WinWaitActive("[ACTIVE]")
			ControlSetText("","","Edit1",$wadDir&"\qb"&$region&".pak.ngc")
			Sleep(100)
			ControlCommand("","","ComboBox3","SetCurrentSelection",1)
			Sleep(100)
			ControlClick("","","Button2","left",1)
			Sleep(100)
			WinWaitActive("PAK Editor GUI 0.1"	)
			Sleep(100)
			$hWnd=ControlGetHandle("PAK Editor GUI 0.1","","SysTreeView321")
			Sleep(100)
			Local $hItem = _GUICtrlTreeView_Find( $hWnd, "cripts\guitar\songlist.qb.ngc" )
			Sleep(100)
			if ($hItem<>0) Then
				_GUICtrlTreeView_SelectItem( $hWnd, $hItem )
				_GUICtrlTreeView_ClickItem( $hWnd, $hItem, "left",1 )
				Sleep(100)
				ControlClick("","","Button4","left",1)
				Sleep(100)
				WinWaitActive("[ACTIVE]")
				Sleep(100)
				ControlSetText("","","Edit1",$wadDir&"\songlist.qb.ngc")
				sleep(500)
				ControlClick("","","Button2","left",1)
				Sleep(100)
				WinWaitActive("PAK Editor GUI 0.1")
				Sleep(100)
				ControlClick("","","Button3","left",1)
			EndIf				
		     EndIf			
	
	;clear unused data from songlist file
		If BitAnd(GUICtrlRead($hsonglistedit),$GUI_CHECKED) = $GUI_CHECKED Then
		ProgressSet(0,"","Clearing unused song info...")
		Run($songedit)
		WinWait("GH3-Wii Songlist Editor v0.1")
		ControlClick("","","Button1","left",1)
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		sleep(100)
		controlSetText("","","Edit1",$wadDir&"\songlist.qb.ngc")
		Sleep(100)
		ControlClick("","","Button2","left",1)
		Sleep(100)
		WinWaitActive("GH3-Wii Songlist Editor v0.1")
		$hWnd=ControlGetHandle("","","ListBox1")
		dim $array[17] = [16,65,66,67,77,78,79,80,81,82,83,84,85,86,87,88,89]
		for $i in $array
		_GUICtrlListBox_SetCurSel($hWnd,$i)
		Sleep(100)
		ControlClick("","","Button2","left",1)
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		Sleep(100)
		ControlSetText("","","Edit1","")
		Sleep(100)
		ControlSetText("","","Edit2","")
		Sleep(100)
		ControlSetText("","","Edit3","")
		Sleep(100)
		ControlClick("","","Button5","left",1)
		Sleep(100)
		WinWaitActive("GH3-Wii Songlist Editor v0.1")
		Sleep(100)
		next
		ControlClick("","","Button4","left",1)
		Sleep(200)
		ControlClick("","","Button5","left",1)

		 EndIf	
	
	
	
	
	
	
	
	
	
	
	$songdir=$tempdir&"\song"
		
	
	
	;ADD window to display messages and Progress
	$p=0
	$percent=$total/10
	
	;main loop
	for $n in $toprocess 
		if $gh3songdata[$n][4] == "" or $gh3songdata[$n][7] == "" Then
			$p+=10
		Else
			
		ProgressSet($p/$percent,"Copying files...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$p+=1
		if fileExists($songdir) then dirremove($songdir,1)
		DirCreate($songdir)
		FileCopy($gh3songdata[$n][4],$songdir&"\song.ogg")
		FileCopy($gh3songdata[$n][5],$songdir&"\guitar.ogg")
		FileCopy($gh3songdata[$n][6],$songdir&"\rhythm.ogg")
		
		ProgressSet($p/$percent,"Making song.wav...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$p+=1
		$check=RunWait($goldwavePath&" /process:GH3stereo """&$songdir&"\song.ogg""")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		DirCreate($songdir&"\song")
		FileMove($songdir&"\song.wav",$songdir&"\song\"&$gh3songs[$n][0]&"_song.wav")
		
		ProgressSet($p/$percent,"Making guitar.wav...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$p+=1
		$check=RunWait($goldwavePath&" /process:GH3mono """&$songdir&"\guitar.ogg""")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		DirCreate($songdir&"\guitar")
		FileMove($songdir&"\guitar.wav",$songdir&"\guitar\"&$gh3songs[$n][0]&"_guitar.wav")
		
		ProgressSet($p/$percent,"Making rhythm.wav...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$p+=1
		$check=RunWait($goldwavePath&" /process:GH3mono """&$songdir&"\rhythm.ogg""")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		DirCreate($songdir&"\rhythm")
		FileMove($songdir&"\rhythm.wav",$songdir&"\rhythm\"&$gh3songs[$n][0]&"_rhythm.wav")
		
		ProgressSet($p/$percent,"Making preview.wav...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$p+=1
		$check=RunWait($goldwavePath&" /process:Mix """&$songdir&"\guitar.ogg"" -clipboard:"""&$songdir&"\rhythm.ogg""")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		$check=RunWait($goldwavePath&" /process:GH3preview """&$songdir&"\song.ogg"" -clipboard:"""&$songdir&"\guitar.wav""")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		DirCreate($songdir&"\preview")
		FileMove($songdir&"\song.wav",$songdir&"\preview\"&$gh3songs[$n][0]&"_preview.wav")
		
		ProgressSet($p/$percent,"Creating FSB files...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$p+=1		
		$check=RunWait($fsbankPath&" -o """&$songdir&"\"&$gh3songs[$n][0]&"_song.wav.fsb"" """&$songdir&"\song"" -p cross -f adpcm -h ")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		$check=RunWait($fsbankPath&" -o """&$songdir&"\"&$gh3songs[$n][0]&"_guitar.wav.fsb"" """&$songdir&"\guitar"" -p cross -f adpcm -h ")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		$check=RunWait($fsbankPath&" -o """&$songdir&"\"&$gh3songs[$n][0]&"_rhythm.wav.fsb"" """&$songdir&"\rhythm"" -p cross -f adpcm -h ")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		$check=RunWait($fsbankPath&" -o """&$songdir&"\"&$gh3songs[$n][0]&"_preview.wav.fsb"" """&$songdir&"\preview"" -p cross -f adpcm -h ")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		
		ProgressSet($p/$percent,"Replacing music files...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$p+=1
		$check=RunWait($StdDatEdit&" """&$wadDir&"\"&$gh3songs[$n][0]&".dat.ngc"" """&$wadDir&"\"&$gh3songs[$n][0]&".wad.ngc""  -replace_qbkey "&$gh3songs[$n][0]&"_guitar """&$songdir&"\"&$gh3songs[$n][0]&"_guitar.wav.fsb"" -replace_qbkey "&$gh3songs[$n][0]&"_rhythm """&$songdir&"\"&$gh3songs[$n][0]&"_rhythm.wav.fsb"" -replace_qbkey "&$gh3songs[$n][0]&"_song """&$songdir&"\"&$gh3songs[$n][0]&"_song.wav.fsb"" -replace_qbkey "&$gh3songs[$n][0]&"_preview """&$songdir&"\"&$gh3songs[$n][0]&"_preview.wav.fsb""")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
			
		if filegetsize($wadDir&"\"&$gh3songs[$n][0]&".wad.ngc") > $wadsize[$n] Then
		ProgressSet($p/$percent,"Uh oh... Wad is larger than original, converting to mono...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$check=RunWait($goldwavePath&" /process:GH3mono """&$songdir&"\song.ogg""")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		FileMove($songdir&"\song.wav",$songdir&"\song\"&$gh3songs[$n][0]&"_song.wav",1)
		$check=RunWait($fsbankPath&" -o """&$songdir&"\"&$gh3songs[$n][0]&"_song.wav.fsb"" """&$songdir&"\song"" -p cross -f adpcm -h ")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		$check=RunWait($StdDatEdit&" """&$wadDir&"\"&$gh3songs[$n][0]&".dat.ngc"" """&$wadDir&"\"&$gh3songs[$n][0]&".wad.ngc""  -replace_qbkey "&$gh3songs[$n][0]&"_guitar """&$songdir&"\"&$gh3songs[$n][0]&"_guitar.wav.fsb"" -replace_qbkey "&$gh3songs[$n][0]&"_rhythm """&$songdir&"\"&$gh3songs[$n][0]&"_rhythm.wav.fsb"" -replace_qbkey "&$gh3songs[$n][0]&"_song """&$songdir&"\"&$gh3songs[$n][0]&"_song.wav.fsb"" -replace_qbkey "&$gh3songs[$n][0]&"_preview """&$songdir&"\"&$gh3songs[$n][0]&"_preview.wav.fsb""")
		if $check <> 0 then msgbox(0,"Error","program returned : "&$check)
		if filegetsize($wadDir&"\"&$gh3songs[$n][0]&".wad.ngc") > $wadsize[$n] Then
			msgbox(0,"Error","Even after converting to mono sound, "&$gh3songdata[$n][0]&" is too large to replace "&$gh3songs[$n][0])
			Exit
		EndIf
		EndIf
		
		
		ProgressSet($p/$percent,"Replacing chart file...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0]) 
		$p+=1
		$schart=FileGetShortName($gh3songdata[$n][7])
		$spak=FileGetShortName($wadDir&"\"&$gh3songs[$n][0]&$region&".pak.ngc")
		$gen=Run($qbgen)
		WinWaitActive($qbgen)
		Sleep(400)
 		Send($schart,1)
		Sleep(400)
		Send("{ENTER}")
		Sleep(400)
		Send($gh3songs[$n][1],1)
		Sleep(400)
		Send("{ENTER}")
		Sleep(400)
		Send($spak)
		Sleep(400)
		Send("{ENTER}")
		Sleep(400)
		Send("1")
		Sleep(400)
		Send("{ENTER}")
		Sleep(400)
		Send("0")
		Sleep(400)
		Send("{ENTER}")
		Sleep(400)
		if ($gh3songs[$n][0]=="one" or $gh3songs[$n][0]=="thrufireandflames") Then
			Send("0")
			Sleep(400)
			Send("{ENTER}")
			Sleep(400)
		EndIf
		Send("{ENTER}")
		Sleep(500)
		
		If BitAnd(GUICtrlRead($hsonglistedit),$GUI_CHECKED) = $GUI_CHECKED Then
		ProgressSet($p/$percent,"Replacing songlist info...",$gh3songs[$n][0]&" -> "&$gh3songdata[$n][0])
		$p+=1
		Run($songedit)
		WinWait("GH3-Wii Songlist Editor v0.1")
		ControlClick("","","Button1","left",1)
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		Sleep(100)
		sleep(100)
		controlSetText("","","Edit1",$wadDir&"\songlist.qb.ngc")
		Sleep(100)
		ControlClick("","","Button2","left",1)
		Sleep(100)
		WinWaitActive("GH3-Wii Songlist Editor v0.1")
		$hWnd=ControlGetHandle("","","ListBox1")
		_GUICtrlListBox_SetCurSel($hWnd,$gh3songs[$n][2])
		Sleep(100)
		ControlClick("","","Button2","left",1)
		Sleep(100)
		WinWaitActive("[ACTIVE]")
		ControlSetText("","","Edit1",$gh3songdata[$n][0])
		ControlSetText("","","Edit2",$gh3songdata[$n][1])
		ControlSetText("","","Edit3",$gh3songdata[$n][2])
		ControlSetText("","","Edit5",$gh3songdata[$n][3])
		if $gh3songdata[$n][8] == 1 then
		ControlCommand("","","Button1","Check","")
		ControlCommand("","","ComboBox1","SetCurrentSelection",1)
		Else
		ControlCommand("","","Button1","UnCheck","")
		ControlCommand("","","ComboBox1","SetCurrentSelection",0)
	EndIf
		Sleep(100)
		ControlClick("","","Button5","left",1)
		sleep(100)
		WinWaitActive("GH3-Wii Songlist Editor v0.1")
		ControlClick("","","Button4","left",1)
		Sleep(200)
		ControlClick("","","Button5","left",1)
	Else
		 $p+=1
		 EndIf
	
		DirRemove($songdir,1)
		EndIf
	next
	
	;insert songlist
	If BitAnd(GUICtrlRead($hsonglistedit),$GUI_CHECKED) = $GUI_CHECKED Then
		 ProgressSet($p/$percent,"Replacing songlist info...","")
		$p+=30
		_rebuildqb()
	Else
		$p+=30
	EndIf
	
	;tieredit
	If BitAnd(GUICtrlRead($htieredit),$GUI_CHECKED) = $GUI_CHECKED Then
		ProgressSet($p/$percent,"Replacing tier list...","")
		$p+=30
		for $i=0 to 7
		While StringLen($tiers[$i][1])<$tiers[$i][3]
			$tiers[$i][1]=$tiers[$i][1]&" "
		WEnd
		While StringLen($tiers[$i][1])>$tiers[$i][3]
			$tiers[$i][1]=StringTrimRight($tiers[$i][1],1)
		WEnd
		next
		$filename=$waddir&"\qb"&$region&".pak.ngc"
		
		;read file
$hFile=FileOpen($filename,16)

$search="CHANGE SPEED"

$part0=FileRead($hfile,262400)


if BinaryMid($part0,1,16)==Binary("0xA7F505C400011D400000004C538D43F9") THEN
	
	$size=filegetsize($filename)
if $size>2645216 Then
	MsgBox(0,"Error","qb.pak.ngc filesize is too large.")
	Exit
EndIf

$place=226189
	while (BinarytoString(BinaryMid($part0,$place,12),4) <> $search) and $place>-1
	$place=$place-1
WEnd
if $place==0 Then
	MsgBox(0,"Read Error","the qb.pak.ngc file is invalid?")
	Exit
EndIf

$part1=FileRead($hfile,865451-262400-(226089-$place))
$tier1=BinarytoString(FileRead($hfile,17),4)
$part2=FileRead($hfile,191)
$tier2=BinarytoString(FileRead($hfile,19),4)
$part3=FileRead($hfile,193)
$tier3=BinarytoString(FileRead($hfile,16),4)
$part4=FileRead($hfile,172)
$tier4=BinarytoString(FileRead($hfile,17),4)
$part5=FileRead($hfile,175)
$tier5=BinarytoString(FileRead($hfile,14),4)
$part6=FileRead($hfile,194)
$tier6=BinarytoString(FileRead($hfile,25),4)
$part7=FileRead($hfile,175)
$tier7=BinarytoString(FileRead($hfile,13),4)
$part8=FileRead($hfile,175)
$tier8=BinarytoString(FileRead($hfile,20),4)
$part9=FileRead($hfile,244)
$part10=FileRead($hfile,18)
$part11=FileRead($hfile,178)
$part12=FileRead($hfile,19)
$part13=FileRead($hfile,177)
$part14=FileRead($hfile,16)
$part15=FileRead($hfile,160)
$part16=FileRead($hfile,17)
$part17=FileRead($hfile,163)
$part18=FileRead($hfile,14)
$part19=FileRead($hfile,162)
$part20=FileRead($hfile,25)
$part21=FileRead($hfile,163)
$part22=FileRead($hfile,13)
$part23=FileRead($hfile,143)
$part24=FileRead($hfile,20)
$part57=FileRead($hfile)

ELSE 
	IF BinaryMid($part0,1,16)==Binary("0xA7F505C4000121000000004C024A1287") THEN
	
	$size=filegetsize($filename)
if $size>2676544 Then
	MsgBox(0,"Error","qb.pak.ngc filesize is too large.")
	Exit
EndIf
	
	$place=262389
	while (BinarytoString(BinaryMid($part0,$place,12),4) <> $search) and $place>-1
	$place=$place-1
WEnd
if $place==0 Then
	MsgBox(0,"Read Error","the qb.pak.ngc file is invalid?")
	Exit
EndIf
	
	$part1=FileRead($hfile,901674-262400-(262344-$place)) 
$tier1=BinarytoString(FileRead($hfile,17),4)
$part2=FileRead($hfile,191)
$tier2=BinarytoString(FileRead($hfile,19),4)
$part3=FileRead($hfile,193)
$tier3=BinarytoString(FileRead($hfile,16),4)
$part4=FileRead($hfile,172)
$tier4=BinarytoString(FileRead($hfile,17),4)
$part5=FileRead($hfile,175)
$tier5=BinarytoString(FileRead($hfile,14),4)
$part6=FileRead($hfile,194)
$tier6=BinarytoString(FileRead($hfile,25),4)
$part7=FileRead($hfile,175)
$tier7=BinarytoString(FileRead($hfile,13),4)
$part8=FileRead($hfile,175)
$tier8=BinarytoString(FileRead($hfile,20),4)
$part9=FileRead($hfile,244)
$part10=FileRead($hfile,17)
$part11=FileRead($hfile,179)
$part12=FileRead($hfile,19)
$part13=FileRead($hfile,177)
$part14=FileRead($hfile,16)
$part15=FileRead($hfile,160)
$part16=FileRead($hfile,17)
$part17=FileRead($hfile,163)
$part18=FileRead($hfile,14)
$part19=FileRead($hfile,162)
$part20=FileRead($hfile,25)
$part21=FileRead($hfile,163)
$part22=FileRead($hfile,13)
$part23=FileRead($hfile,143)
$part24=FileRead($hfile,20)
$part57=FileRead($hfile)
Else
	MsgBox(0,"Error","Invalid qb.pak.ngc?")
	Exit
EndIf
EndIf

FileClose($hfile)
		

	;write File
	$hfile=FileOpen($filename,18)
FileWrite($hfile,$part0)
FileClose($hfile)
$hfile=FileOpen($filename,17)

FileWrite($hfile,$part1)

FileWrite($hfile,$tiers[0][1])
FileWrite($hfile,$part2)

FileWrite($hfile,$tiers[1][1])
FileWrite($hfile,$part3)

FileWrite($hfile,$tiers[2][1])
FileWrite($hfile,$part4)

FileWrite($hfile,$tiers[3][1])
FileWrite($hfile,$part5)

FileWrite($hfile,$tiers[4][1])
FileWrite($hfile,$part6)

FileWrite($hfile,$tiers[5][1])
FileWrite($hfile,$part7)

FileWrite($hfile,$tiers[6][1])
FileWrite($hfile,$part8)

FileWrite($hfile,$tiers[7][1])
FileWrite($hfile,$part9)

FileWrite($hfile,$tiers[0][1])
FileWrite($hfile,$part11)

FileWrite($hfile,$tiers[1][1])
FileWrite($hfile,$part13)

FileWrite($hfile,$tiers[2][1])
FileWrite($hfile,$part15)

FileWrite($hfile,$tiers[3][1])
FileWrite($hfile,$part17)

FileWrite($hfile,$tiers[4][1])
FileWrite($hfile,$part19)

FileWrite($hfile,$tiers[5][1])
FileWrite($hfile,$part21)

FileWrite($hfile,$tiers[6][1])
FileWrite($hfile,$part23)

FileWrite($hfile,$tiers[7][1])
FileWrite($hfile,$part57)


FileClose($hfile)

	Else
		$p+=30
	EndIf
	
	;replace files
	If BitAnd(GUICtrlRead($hreplace),$GUI_CHECKED) = $GUI_CHECKED Then
	ProgressSet($p/$percent,"Replacing files on ISO...","")
	_replacewads()
	EndIf
	
	
	ProgressOff()
	GUISetState(@SW_SHOW)
	WinMinimizeAllUndo()
	GUISetState(@SW_RESTORE ,$hgui)
	BlockInput(0)
	MsgBox(0,"Finished","The song replacement process is complete.")
EndFunc

