#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[73][9]
For $i = 0 To 72
	$gh3songdata[$i][5] = @ScriptDir & "\files\blank.ogg"
	$gh3songdata[$i][6] = @ScriptDir & "\files\blank.ogg"
	$gh3songdata[$i][8] = 1
Next

Global $toprocess[73]

Global $wadsize[73] = [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, 24510464, 27197440, 44744704]



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


Global $tiers[9][4] = [ _
		["Starting out small", "Starting out small", "", 18], _
		["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[73][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, ""], _
		["bosstom", 7, 7, ""], _
		["bossslash", 8, 8, ""], _
		["bossdevil", 9, 9, ""]]

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)
$hmusicedit = GUICtrlCreateMenuItem("Replace music data?", $hoptions)
GUICtrlSetState(-1, $GUI_CHECKED)
$hchartedit = GUICtrlCreateMenuItem("Replace note charts?", $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)

$toolsmenu = GUICtrlCreateMenu("Tools")
$cleartemp = GUICtrlCreateMenuItem("Clear temporary cache", $toolsmenu)
$runtrucha = GUICtrlCreateMenuItem("Trucha 0.21", $toolsmenu)
$rungoldwave = GUICtrlCreateMenuItem("GoldWave", $toolsmenu)
$runpakedit = GUICtrlCreateMenuItem("PAK Editor", $toolsmenu)
$runsongedit = GUICtrlCreateMenuItem("Song List Editor", $toolsmenu)

$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("Slow ride", $tiers[0][2])
$gh3songs[62][3] = GUICtrlCreateTreeViewItem("Talk dirty to me", $tiers[0][2])
$gh3songs[22][3] = GUICtrlCreateTreeViewItem("Hit me with your best shot", $tiers[0][2])
$gh3songs[57][3] = GUICtrlCreateTreeViewItem("Story of my life", $tiers[0][2])
$gh3songs[49][3] = GUICtrlCreateTreeViewItem("Rock 'n roll all night", $tiers[0][2])
$gh3songs[52][3] = GUICtrlCreateTreeViewItem("Sabotage", $tiers[0][2])

$tiers[1][2] = GUICtrlCreateTreeViewItem("Your first real gig", $maintree)
$gh3songs[34][3] = GUICtrlCreateTreeViewItem("Mississippi queen", $tiers[1][2])
$gh3songs[54][3] = GUICtrlCreateTreeViewItem("Schools out", $tiers[1][2])
$gh3songs[60][3] = GUICtrlCreateTreeViewItem("Sunshine of your love", $tiers[1][2])
$gh3songs[2][3] = GUICtrlCreateTreeViewItem("Barracuda", $tiers[1][2])
$gh3songs[70][3] = GUICtrlCreateTreeViewItem("Battle vs. Tom Morello", $tiers[1][2])
$gh3songs[7][3] = GUICtrlCreateTreeViewItem("Bulls on parade", $tiers[1][2])
$gh3songs[48][3] = GUICtrlCreateTreeViewItem("Reptilia", $tiers[1][2])

$tiers[2][2] = GUICtrlCreateTreeViewItem("Making the video", $maintree)
$gh3songs[69][3] = GUICtrlCreateTreeViewItem("When you were young", $tiers[2][2])
$gh3songs[35][3] = GUICtrlCreateTreeViewItem("Miss Murder", $tiers[2][2])
$gh3songs[64][3] = GUICtrlCreateTreeViewItem("The seeker", $tiers[2][2])
$gh3songs[30][3] = GUICtrlCreateTreeViewItem("Lay down", $tiers[2][2])
$gh3songs[42][3] = GUICtrlCreateTreeViewItem("Paint it black", $tiers[2][2])
$gh3songs[59][3] = GUICtrlCreateTreeViewItem("Suck my kiss", $tiers[2][2])

$tiers[3][2] = GUICtrlCreateTreeViewItem("European invasion", $maintree)
$gh3songs[43][3] = GUICtrlCreateTreeViewItem("Paranoid", $tiers[3][2])
$gh3songs[0][3] = GUICtrlCreateTreeViewItem("Anarchy in the UK", $tiers[3][2])
$gh3songs[28][3] = GUICtrlCreateTreeViewItem("Kool thing", $tiers[3][2])
$gh3songs[38][3] = GUICtrlCreateTreeViewItem("My name is Jonas", $tiers[3][2])
$gh3songs[16][3] = GUICtrlCreateTreeViewItem("Evenflow", $tiers[3][2])
$gh3songs[10][3] = GUICtrlCreateTreeViewItem("Cities on flame with rock and roll", $tiers[3][2])

$tiers[4][2] = GUICtrlCreateTreeViewItem("Bighouse blues", $maintree)
$gh3songs[23][3] = GUICtrlCreateTreeViewItem("Holiday in Cambodia", $tiers[4][2])
$gh3songs[50][3] = GUICtrlCreateTreeViewItem("Rock you like a hurricane", $tiers[4][2])
$gh3songs[53][3] = GUICtrlCreateTreeViewItem("Same old song and dance", $tiers[4][2])
$gh3songs[29][3] = GUICtrlCreateTreeViewItem("La grange", $tiers[4][2])
$gh3songs[71][3] = GUICtrlCreateTreeViewItem("Battle vs. Slash", $tiers[4][2])
$gh3songs[68][3] = GUICtrlCreateTreeViewItem("Welcome to the jungle", $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("Black magic woman", $tiers[5][2])
$gh3songs[9][3] = GUICtrlCreateTreeViewItem("Cherub rock", $tiers[5][2])
$gh3songs[6][3] = GUICtrlCreateTreeViewItem("Black sunshine", $tiers[5][2])
$gh3songs[63][3] = GUICtrlCreateTreeViewItem("The metal", $tiers[5][2])
$gh3songs[45][3] = GUICtrlCreateTreeViewItem("Pride and joy", $tiers[5][2])
$gh3songs[36][3] = GUICtrlCreateTreeViewItem("Monsters", $tiers[5][2])

$tiers[6][2] = GUICtrlCreateTreeViewItem("Live in Japan", $maintree)
$gh3songs[3][3] = GUICtrlCreateTreeViewItem("Before I forget", $tiers[6][2])
$gh3songs[58][3] = GUICtrlCreateTreeViewItem("Stricken", $tiers[6][2])
$gh3songs[66][3] = GUICtrlCreateTreeViewItem("3's and 7's", $tiers[6][2])
$gh3songs[27][3] = GUICtrlCreateTreeViewItem("Knights of Cydonia", $tiers[6][2])
$gh3songs[13][3] = GUICtrlCreateTreeViewItem("Cult of personality", $tiers[6][2])

$tiers[7][2] = GUICtrlCreateTreeViewItem("Battle for your soul", $maintree)
$gh3songs[47][3] = GUICtrlCreateTreeViewItem("Raining blood", $tiers[7][2])
$gh3songs[11][3] = GUICtrlCreateTreeViewItem("Cliffs of Dover", $tiers[7][2])
$gh3songs[40][3] = GUICtrlCreateTreeViewItem("Number of the beast", $tiers[7][2])
$gh3songs[41][3] = GUICtrlCreateTreeViewItem("One", $tiers[7][2])
$gh3songs[72][3] = GUICtrlCreateTreeViewItem("Battle vs. Lou", $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] = GUICtrlCreateInput("", 480, $j * 44 + 20, 250, 18)
	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)

$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($trucha) Then
	GUICtrlSetState($runtrucha, $GUI_ENABLE)
Else
	GUICtrlSetState($runtrucha, $GUI_DISABLE)
EndIf

If FileExists($goldwavePath) Then
	GUICtrlSetState($rungoldwave, $GUI_ENABLE)
Else
	GUICtrlSetState($rungoldwave, $GUI_DISABLE)
EndIf

If FileExists($songedit) Then
	GUICtrlSetState($runsongedit, $GUI_ENABLE)
Else
	GUICtrlSetState($runsongedit, $GUI_DISABLE)
EndIf

If FileExists($pakedit) Then
	GUICtrlSetState($runpakedit, $GUI_ENABLE)
Else
	GUICtrlSetState($runpakedit, $GUI_DISABLE)
EndIf


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], "", "Music Files (*.ogg;*.mp3;*.wav;*.flac;*.wma)", 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], "", "Music Files (*.ogg;*.mp3;*.wav;*.flac;*.wma)", 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], "", "Music Files (*.ogg;*.mp3;*.wav;*.flac;*.wma)", 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 = $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
			If FileExists($trucha) Then
				GUICtrlSetState($runtrucha, $GUI_ENABLE)
			Else
				GUICtrlSetState($runtrucha, $GUI_DISABLE)
			EndIf
			
			If FileExists($goldwavePath) Then
				GUICtrlSetState($rungoldwave, $GUI_ENABLE)
			Else
				GUICtrlSetState($rungoldwave, $GUI_DISABLE)
			EndIf
			
			If FileExists($songedit) Then
				GUICtrlSetState($runsongedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runsongedit, $GUI_DISABLE)
			EndIf
			
			If FileExists($pakedit) Then
				GUICtrlSetState($runpakedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runpakedit, $GUI_DISABLE)
			EndIf
			
		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
			If FileExists($trucha) Then
				GUICtrlSetState($runtrucha, $GUI_ENABLE)
			Else
				GUICtrlSetState($runtrucha, $GUI_DISABLE)
			EndIf
			
			If FileExists($goldwavePath) Then
				GUICtrlSetState($rungoldwave, $GUI_ENABLE)
			Else
				GUICtrlSetState($rungoldwave, $GUI_DISABLE)
			EndIf
			
			If FileExists($songedit) Then
				GUICtrlSetState($runsongedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runsongedit, $GUI_DISABLE)
			EndIf
			
			If FileExists($pakedit) Then
				GUICtrlSetState($runpakedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runpakedit, $GUI_DISABLE)
			EndIf
			
		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
			If FileExists($trucha) Then
				GUICtrlSetState($runtrucha, $GUI_ENABLE)
			Else
				GUICtrlSetState($runtrucha, $GUI_DISABLE)
			EndIf
			
			If FileExists($goldwavePath) Then
				GUICtrlSetState($rungoldwave, $GUI_ENABLE)
			Else
				GUICtrlSetState($rungoldwave, $GUI_DISABLE)
			EndIf
			
			If FileExists($songedit) Then
				GUICtrlSetState($runsongedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runsongedit, $GUI_DISABLE)
			EndIf
			
			If FileExists($pakedit) Then
				GUICtrlSetState($runpakedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runpakedit, $GUI_DISABLE)
			EndIf
			
		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
			If FileExists($trucha) Then
				GUICtrlSetState($runtrucha, $GUI_ENABLE)
			Else
				GUICtrlSetState($runtrucha, $GUI_DISABLE)
			EndIf
			
			If FileExists($goldwavePath) Then
				GUICtrlSetState($rungoldwave, $GUI_ENABLE)
			Else
				GUICtrlSetState($rungoldwave, $GUI_DISABLE)
			EndIf
			
			If FileExists($songedit) Then
				GUICtrlSetState($runsongedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runsongedit, $GUI_DISABLE)
			EndIf
			
			If FileExists($pakedit) Then
				GUICtrlSetState($runpakedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runpakedit, $GUI_DISABLE)
			EndIf

		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
			If FileExists($trucha) Then
				GUICtrlSetState($runtrucha, $GUI_ENABLE)
			Else
				GUICtrlSetState($runtrucha, $GUI_DISABLE)
			EndIf
			
			If FileExists($goldwavePath) Then
				GUICtrlSetState($rungoldwave, $GUI_ENABLE)
			Else
				GUICtrlSetState($rungoldwave, $GUI_DISABLE)
			EndIf
			
			If FileExists($songedit) Then
				GUICtrlSetState($runsongedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runsongedit, $GUI_DISABLE)
			EndIf
			
			If FileExists($pakedit) Then
				GUICtrlSetState($runpakedit, $GUI_ENABLE)
			Else
				GUICtrlSetState($runpakedit, $GUI_DISABLE)
			EndIf
			
		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 = $hmusicedit
			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($hmusicedit), $GUI_CHECKED) = $GUI_CHECKED Then
				GUICtrlSetState($hmusicedit, $GUI_UNCHECKED)
			Else
				GUICtrlSetState($hmusicedit, $GUI_CHECKED)
			EndIf
			
		Case $iMsg = $hchartedit
			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($hchartedit), $GUI_CHECKED) = $GUI_CHECKED Then
				GUICtrlSetState($hchartedit, $GUI_UNCHECKED)
			Else
				GUICtrlSetState($hchartedit, $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 = $toolsmenu
			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 = $runtrucha
			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
			Run($trucha)
			
		Case $iMsg = $rungoldwave
			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
			Run($goldwavePath)
			
		Case $iMsg = $runpakedit
			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
			Run($pakedit)
			
		Case $iMsg = $runsongedit
			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
			Run($songedit)
			
		Case $iMsg = $cleartemp
			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
			DirRemove("C:\gh3temp", 1)
			
		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 72 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 _GUICtrlTreeView_ClickItem($hWnd, $hItem, $sButton = "left", $fMove = False, $iClicks = 1, $iSpeed = 0)
	If $Debug_TV Then _GUICtrlTreeView_ValidateClassName($hWnd)
	If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
	Local $tPoint, $tRect, $iX, $iY, $iMode, $aPos

	$tRect = _GUICtrlTreeView_DisplayRectEx($hWnd, $hItem, True)
	
	$iX1 = DllStructGetData($tRect, "Left")
	$iY1 = DllStructGetData($tRect, "Top")
	$iX2 = DllStructGetData($tRect, "Right")
	$iY2 = DllStructGetData($tRect, "Bottom")
	$iX1 = $iX1 + (($iX2 - $iX1) / 2)
	$iY1 = $iY1 + (($iY2 - $iY1) / 2)
	$tPoint = DllStructCreate($tagPOINT)
	DllStructSetData($tPoint, "X", $iX1)
	DllStructSetData($tPoint, "Y", $iY1)

	_WinAPI_ClientToScreen($hWnd, $tPoint)
	
	$iX = DllStructGetData($tPoint, "X")
	$iY = DllStructGetData($tPoint, "Y")
	
	If Not $fMove Then
		$iMode = Opt("MouseCoordMode", 1)
		$aPos = MouseGetPos()
		Opt("MouseCoordMode", $iMode)
		_WinAPI_ShowCursor(False)
		MouseClick($sButton, $iX, $iY, $iClicks, $iSpeed)
		MouseMove($aPos[0], $aPos[1], 0)
		_WinAPI_ShowCursor(True)
	Else
		MouseClick($sButton, $iX, $iY, $iClicks, $iSpeed)
	EndIf
EndFunc   ;==>_GUICtrlTreeView_ClickItem



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)
	For $i = 70 To 72
		For $j = 0 To 8
			FileWriteLine($hfile, $gh3songdata[$i][$j])
		Next
	Next
	FileWriteLine($hfile, GUICtrlRead($hmusicedit))
	FileWriteLine($hfile, GUICtrlRead($hchartedit))
	FileClose($hfile)
	$saveFlag = 0
EndFunc   ;==>_saveFunc




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)
	For $i = 70 To 72
		For $j = 0 To 8
			$gh3songdata[$i][$j] = FileReadLine($hfile)
		Next
	Next
	GUICtrlSetState($hmusicedit, FileReadLine($hfile))
	GUICtrlSetState($hchartedit, FileReadLine($hfile))
	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   ;==>_openFunc



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   ;==>_GUICtrlTreeView_Find














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)
	If BitAND(GUICtrlRead($hmusicedit), $GUI_CHECKED) = $GUI_CHECKED Then
		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
		Next
	EndIf
	Local $hItem = _GUICtrlTreeView_Find($hWnd, "DISC (RGHE52)\PARTITION (RGHE52)\ROOT\songs\tutorial_4c" & $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
	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   ;==>_ExtractWads

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 (BitAND(GUICtrlRead($hmusicedit), $GUI_CHECKED) <> $GUI_CHECKED And ($j == 1 Or $j == 2)) Then ContinueLoop
			If (BitAND(GUICtrlRead($hchartedit), $GUI_CHECKED) <> $GUI_CHECKED And $j == 3) Then ContinueLoop
			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   ;==>_ReplaceWads

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   ;==>_Rebuildqb


Func _createChart($chartfile)

	Dim $data
	_FileReadToArray($chartfile, $data)

	$thresh = 20
	$xs = 0
	$hs = 0
	$ms = 0
	$es = 0
	$xm = 0
	$hm = 0
	$mm = 0
	$em = 0


	For $i = 1 To $data[0]
		
		If $data[$i] = "[Song]" Then
			$j = 0
			$songStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$songEnd = $i
		EndIf

		If $data[$i] = "[SyncTrack]" Then
			$j = 0
			$syncStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$syncEnd = $i
		EndIf

		If $data[$i] = "[Events]" Then
			$j = 0
			$eventStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$eventEnd = $i
		EndIf
		
		If $data[$i] = "[ExpertSingle]" Then
			$j = 0
			$xsStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$xsEnd = $i
			If $j > $thresh Then $xs = 1
		EndIf
		
		If $data[$i] = "[HardSingle]" Then
			$j = 0
			$hsStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$hsEnd = $i
			If $j > $thresh Then $hs = 1
		EndIf

		If $data[$i] = "[MediumSingle]" Then
			$j = 0
			$msStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$msEnd = $i
			If $j > $thresh Then $ms = 1
		EndIf

		If $data[$i] = "[EasySingle]" Then
			$j = 0
			$esStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$esEnd = $i
			If $j > $thresh Then $es = 1
		EndIf


		If $data[$i] = "[ExpertDoubleBass]" Then
			$j = 0
			$xmStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$xmEnd = $i
			If $j > $thresh Then $xm = 1
		EndIf
		
		If $data[$i] = "[HardDoubleBass]" Then
			$j = 0
			$hmStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$hmEnd = $i
			If $j > $thresh Then $hm = 1
		EndIf

		If $data[$i] = "[MediumDoubleBass]" Then
			$j = 0
			$mmStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$mmEnd = $i
			If $j > $thresh Then $mm = 1
		EndIf

		If $data[$i] = "[EasyDoubleBass]" Then
			$j = 0
			$emStart = $i + 1
			While $data[$i] <> "}"
				$i += 1
				$j += 1
			WEnd
			$emEnd = $i
			If $j > $thresh Then $em = 1
		EndIf
		
	Next

	Select
		Case $xs = 1
		Case $hs = 1
			$xsStart = $hsStart
			$xsEnd = $hsEnd
		Case $ms = 1
			$xsStart = $msStart
			$xsEnd = $msEnd
		Case $es = 1
			$xsStart = $esStart
			$xsEnd = $esEnd
		Case Else
			MsgBox(0, "Error", "No notes data")
	EndSelect

	If $hs = 0 Then
		$hsStart = $xsStart
		$hsEnd = $xsEnd
	EndIf
	If $ms = 0 Then
		$msStart = $hsStart
		$msEnd = $hsEnd
	EndIf
	If $es = 0 Then
		$esStart = $msStart
		$esEnd = $msEnd
	EndIf

	If $xm = 0 And $hm = 0 And $mm = 0 And $em = 0 Then
		$xmStart = $xsStart
		$xmEnd = $xsEnd
		$xm = 1
		$hmStart = $hsStart
		$hmEnd = $hsEnd
		$hm = 1
		$mmStart = $msStart
		$mmEnd = $msEnd
		$mm = 1
		$emStart = $esStart
		$emEnd = $esEnd
		$em = 1
	EndIf

	Select
		Case $xm = 0 And $hm = 1
			$xmStart = $hmStart
			$xmEnd = $hmEnd
		Case $xm = 0 And $mm = 1
			$xmStart = $mmStart
			$xmEnd = $mmEnd
		Case $xm = 0 And $em = 1
			$xmStart = $emStart
			$xmEnd = $emEnd
	EndSelect

	If $hm = 0 Then
		$hmStart = $xmStart
		$hmEnd = $xmEnd
	EndIf
	If $mm = 0 Then
		$mmStart = $hmStart
		$mmEnd = $hmEnd
	EndIf
	If $em = 0 Then
		$emStart = $mmStart
		$emEnd = $mmEnd
	EndIf



	$hfile = FileOpen("C:\gh3temp\song\notes.chart", 2)
	FileWriteLine($hfile, "[Song]")
	For $i = $songStart To $songEnd
		FileWriteLine($hfile, $data[$i])
	Next
	FileWriteLine($hfile, "[SyncTrack]")
	For $i = $syncStart To $syncEnd
		FileWriteLine($hfile, $data[$i])
	Next
	FileWriteLine($hfile, "[Events]")
	For $i = $eventStart To $eventEnd
		FileWriteLine($hfile, $data[$i])
	Next

	FileWriteLine($hfile, "[ExpertSingle]")
	$starpower = 0
	For $i = $xsStart To $xsEnd
		If StringInStr($data[$i], "S", 1) <> 0 Then $starpower = 1
	Next
	If $starpower == 0 Then
		$SPfreq = 100
		$SPlen = 1000
		For $i = $xsStart To $xsEnd
			FileWriteLine($hfile, $data[$i])
			If ($i - $xsStart) <> 0 And Mod($i - $xsStart, $SPfreq) = 0 Then FileWriteLine($hfile, StringTrimRight($data[$i], StringLen($data[$i]) - StringInStr($data[$i], "=")) & "S 2 " & $SPlen)
		Next
	Else
		For $i = $xsStart To $xsEnd
			FileWriteLine($hfile, $data[$i])
		Next
	EndIf

	FileWriteLine($hfile, "[HardSingle]")
	$starpower = 0
	For $i = $hsStart To $hsEnd
		If StringInStr($data[$i], "S", 1) <> 0 Then $starpower = 1
	Next
	If $starpower == 0 Then
		$SPfreq = 90
		$SPlen = 1000
		For $i = $hsStart To $hsEnd
			FileWriteLine($hfile, $data[$i])
			If ($i - $hsStart) <> 0 And Mod($i - $hsStart, $SPfreq) = 0 Then FileWriteLine($hfile, StringTrimRight($data[$i], StringLen($data[$i]) - StringInStr($data[$i], "=")) & "S 2 " & $SPlen)
		Next
	Else
		For $i = $hsStart To $hsEnd
			FileWriteLine($hfile, $data[$i])
		Next
	EndIf

	FileWriteLine($hfile, "[MediumSingle]")
	$starpower = 0
	For $i = $msStart To $msEnd
		If StringInStr($data[$i], "S", 1) <> 0 Then $starpower = 1
	Next
	If $starpower == 0 Then
		$SPfreq = 70
		$SPlen = 1500
		For $i = $msStart To $msEnd
			FileWriteLine($hfile, $data[$i])
			If ($i - $msStart) <> 0 And Mod($i - $msStart, $SPfreq) = 0 Then FileWriteLine($hfile, StringTrimRight($data[$i], StringLen($data[$i]) - StringInStr($data[$i], "=")) & "S 2 " & $SPlen)
		Next
	Else
		For $i = $msStart To $msEnd
			FileWriteLine($hfile, $data[$i])
		Next
	EndIf

	FileWriteLine($hfile, "[EasySingle]")
	$starpower = 0
	For $i = $esStart To $esEnd
		If StringInStr($data[$i], "S", 1) <> 0 Then $starpower = 1
	Next
	If $starpower == 0 Then
		$SPfreq = 50
		$SPlen = 2000
		For $i = $esStart To $esEnd
			FileWriteLine($hfile, $data[$i])
			If ($i - $esStart) <> 0 And Mod($i - $esStart, $SPfreq) = 0 Then FileWriteLine($hfile, StringTrimRight($data[$i], StringLen($data[$i]) - StringInStr($data[$i], "=")) & "S 2 " & $SPlen)
		Next
	Else
		For $i = $esStart To $esEnd
			FileWriteLine($hfile, $data[$i])
		Next
	EndIf

	FileWriteLine($hfile, "[ExpertDoubleBass]")
	$starpower = 0
	For $i = $xmStart To $xmEnd
		If StringInStr($data[$i], "S", 1) <> 0 Then $starpower = 1
	Next
	If $starpower == 0 Then
		$SPfreq = 90
		$SPlen = 1000
		For $i = $xmStart To $xmEnd
			FileWriteLine($hfile, $data[$i])
			If ($i - $xmStart) <> 0 And Mod($i - $xmStart, $SPfreq) = 0 Then FileWriteLine($hfile, StringTrimRight($data[$i], StringLen($data[$i]) - StringInStr($data[$i], "=")) & "S 2 " & $SPlen)
		Next
	Else
		For $i = $xmStart To $xmEnd
			FileWriteLine($hfile, $data[$i])
		Next
	EndIf

	FileWriteLine($hfile, "[HardDoubleBass]")
	$starpower = 0
	For $i = $hmStart To $hmEnd
		If StringInStr($data[$i], "S", 1) <> 0 Then $starpower = 1
	Next
	If $starpower == 0 Then
		$SPfreq = 80
		$SPlen = 1000
		For $i = $hmStart To $hmEnd
			FileWriteLine($hfile, $data[$i])
			If ($i - $hmStart) <> 0 And Mod($i - $hmStart, $SPfreq) = 0 Then FileWriteLine($hfile, StringTrimRight($data[$i], StringLen($data[$i]) - StringInStr($data[$i], "=")) & "S 2 " & $SPlen)
		Next
	Else
		For $i = $hmStart To $hmEnd
			FileWriteLine($hfile, $data[$i])
		Next
	EndIf

	FileWriteLine($hfile, "[MediumDoubleBass]")
	$starpower = 0
	For $i = $mmStart To $mmEnd
		If StringInStr($data[$i], "S", 1) <> 0 Then $starpower = 1
	Next
	If $starpower == 0 Then
		$SPfreq = 70
		$SPlen = 1500
		For $i = $mmStart To $mmEnd
			FileWriteLine($hfile, $data[$i])
			If ($i - $mmStart) <> 0 And Mod($i - $mmStart, $SPfreq) = 0 Then FileWriteLine($hfile, StringTrimRight($data[$i], StringLen($data[$i]) - StringInStr($data[$i], "=")) & "S 2 " & $SPlen)
		Next
	Else
		For $i = $mmStart To $mmEnd
			FileWriteLine($hfile, $data[$i])
		Next
	EndIf

	FileWriteLine($hfile, "[EasyDoubleBass]")
	$starpower = 0
	For $i = $emStart To $emEnd
		If StringInStr($data[$i], "S", 1) <> 0 Then $starpower = 1
	Next
	If $starpower == 0 Then
		$SPfreq = 50
		$SPlen = 2000
		For $i = $emStart To $emEnd
			FileWriteLine($hfile, $data[$i])
			If ($i - $emStart) <> 0 And Mod($i - $emStart, $SPfreq) = 0 Then FileWriteLine($hfile, StringTrimRight($data[$i], StringLen($data[$i]) - StringInStr($data[$i], "=")) & "S 2 " & $SPlen)
		Next
	Else
		For $i = $emStart To $emEnd
			FileWriteLine($hfile, $data[$i])
		Next
	EndIf

	FileClose($hfile)

EndFunc   ;==>_createChart



























Func _process()
	If @OSVersion <> "WIN_98" And @OSVersion <> "WIN_ME" Then
		BlockInput(1)
	EndIf
	Global $toprocess[73]
	$total = 73
	For $i = 72 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
			
			If BitAND(GUICtrlRead($hmusicedit), $GUI_CHECKED) == $GUI_CHECKED Then
				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")
				If FileExists(StringTrimRight($gh3songdata[$n][4], StringLen($gh3songdata[$n][4]) - StringInStr($gh3songdata[$n][4], "\", 0, -1) + 1) & "\preview.wav") Then FileCopy(StringTrimRight($gh3songdata[$n][4], StringLen($gh3songdata[$n][4]) - StringInStr($gh3songdata[$n][4], "\", 0, -1) + 1) & "\preview.wav", "C:\gh3temp\song\preview.wav", 1)
				
				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")
				If FileExists("C:\gh3temp\song\preview.wav") Then
					FileMove("C:\gh3temp\song\preview.wav", "C:\gh3temp\song\preview\" & $gh3songs[$n][0] & "_preview.wav")
				Else
					FileMove($songdir & "\song.wav", $songdir & "\preview\" & $gh3songs[$n][0] & "_preview.wav")
				EndIf
				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
			Else
				$p += 7
			EndIf
			
			
			If BitAND(GUICtrlRead($hmusicedit), $GUI_CHECKED) == $GUI_CHECKED Then
				ProgressSet($p / $percent, "Replacing chart file...", $gh3songs[$n][0] & " -> " & $gh3songdata[$n][0])
				$p += 1
				_createChart($gh3songdata[$n][7])
				FileCopy($wadDir & "\" & "tutorial_4c" & $region & ".pak.ngc", "C:\gh3temp\song\temp.pak.ngc", 1)
				$schart = FileGetShortName("C:\gh3temp\song\notes.chart")
				$spak = FileGetShortName("C:\gh3temp\song\temp.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)
				If ($gh3songs[$n][0] == "one" Or $gh3songs[$n][0] == "thrufireandflames") Then
					Send("1")
					Sleep(400)
					Send("{ENTER}")
					Sleep(400)
				EndIf
				Send("2")
				Sleep(400)
				Send("{ENTER}")
				Sleep(400)
				Send("{ENTER}")
				Sleep(500)
				FileCopy("C:\gh3temp\song\temp.pak.ngc", $wadDir & "\" & $gh3songs[$n][0] & $region & ".pak.ngc", 1)
			Else
				$p += 1
			EndIf
			
			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, 18), 4)
			$part2 = FileRead($hfile, 190)
			$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, 18), 4)
				$part2 = FileRead($hfile, 190)
				$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
				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   ;==>_process