#Include <File.au3>
#include <GUIConstants.au3>

global $part1,$part2,$part3,$part4,$part5,$part6,$part7,$part8,$part9
global $tier1,$tier2,$tier3,$tier4,$tier5,$tier6,$tier7,$tier8

$hGui=GUICreate("GH3 Tier Editor for Wii v1.0", 385, 400, @DesktopWidth/2 - 192, _
@DesktopHeight/2 - 200, -1, "")

GUISetState()

$filename=FileOpenDialog("Open qb file","","(qb.pak.ngc)",1,"qb.pak.ngc")
if $filename=="" then exit



;create backup file
if FileExists($filename&".bak") then
	MsgBox(0,"","Backup of qb.pak.ngc already exists")
Else
	FileCopy($filename,$filename&".bak")
EndIf


;read file
$hFile=FileOpen($filename,16)

$search="CHANGE SPEED"

$part0=FileRead($hfile,262400)


;MsgBox(0,"",BinaryMid($part0,1,16))

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)
;$part25=FileRead($hfile,188)
;$part26=FileRead($hfile,18)
;$part27=FileRead($hfile,178)
;$part28=FileRead($hfile,19)
;$part29=FileRead($hfile,177)
;$part30=FileRead($hfile,16)
;$part31=FileRead($hfile,160)
;$part32=FileRead($hfile,17)
;$part33=FileRead($hfile,163)
;$part34=FileRead($hfile,14)
;$part35=FileRead($hfile,162)
;$part36=FileRead($hfile,25)
;$part37=FileRead($hfile,163)
;$part38=FileRead($hfile,13)
;$part39=FileRead($hfile,143)
;$part40=FileRead($hfile,20)
;$part41=FileRead($hfile,188)
;$part42=FileRead($hfile,18)
;$part43=FileRead($hfile,178)
;$part44=FileRead($hfile,19)
;$part45=FileRead($hfile,177)
;$part46=FileRead($hfile,16)
;$part47=FileRead($hfile,160)
;$part48=FileRead($hfile,17)
;$part49=FileRead($hfile,163)
;$part50=FileRead($hfile,14)
;$part51=FileRead($hfile,162)
;~ $part52=FileRead($hfile,25)
;~ $part53=FileRead($hfile,163)
;~ $part54=FileRead($hfile,13)
;~ $part55=FileRead($hfile,143)
;~ $part56=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)
;$part25=FileRead($hfile,188)
;$part26=FileRead($hfile,18)
;$part27=FileRead($hfile,178)
;$part28=FileRead($hfile,19)
;$part29=FileRead($hfile,177)
;$part30=FileRead($hfile,16)
;$part31=FileRead($hfile,160)
;$part32=FileRead($hfile,17)
;$part33=FileRead($hfile,163)
;$part34=FileRead($hfile,14)
;$part35=FileRead($hfile,162)
;$part36=FileRead($hfile,25)
;$part37=FileRead($hfile,163)
;$part38=FileRead($hfile,13)
;$part39=FileRead($hfile,143)
;$part40=FileRead($hfile,20)
;$part41=FileRead($hfile,188)
;$part42=FileRead($hfile,18)
;$part43=FileRead($hfile,178)
;$part44=FileRead($hfile,19)
;$part45=FileRead($hfile,177)
;$part46=FileRead($hfile,16)
;$part47=FileRead($hfile,160)
;$part48=FileRead($hfile,17)
;$part49=FileRead($hfile,163)
;$part50=FileRead($hfile,14)
;$part51=FileRead($hfile,162)
;~ $part52=FileRead($hfile,25)
;~ $part53=FileRead($hfile,163)
;~ $part54=FileRead($hfile,13)
;~ $part55=FileRead($hfile,143)
;~ $part56=FileRead($hfile,20)
$part57=FileRead($hfile)
	
	
Else
	MsgBox(0,"Error","Invalid qb.pak.ngc?")
	Exit
EndIf
EndIf


FileClose($hfile)


;GUI to edit tier names
GUICtrlCreateGroup("Tier 1", 2, 2, 375, 40)
$htier1 = GUICtrlCreateInput($tier1, 12,  18, 200, 18, -1)
$hleft1 = GUICtrlCreateLabel("Left:0",225,18,100,12,-1)
GUICtrlCreateGroup("Tier 2", 2, 46, 375, 40)
$htier2 = GUICtrlCreateInput($tier2, 12,  62, 200, 18, -1)
$hleft2 = GUICtrlCreateLabel("Left:0",225,62,100,12,-1)
GUICtrlCreateGroup("Tier 3", 2, 90, 375, 40)
$htier3 = GUICtrlCreateInput($tier3, 12,  106, 200, 18, -1)
$hleft3 = GUICtrlCreateLabel("Left:0",225,106,100,12,-1)
GUICtrlCreateGroup("Tier 4", 2, 134, 375, 40)
$htier4 = GUICtrlCreateInput($tier4, 12,  150, 200, 18, -1)
$hleft4 = GUICtrlCreateLabel("Left:0",225,150,100,12,-1)
GUICtrlCreateGroup("Tier 5", 2, 178, 375, 40)
$htier5 = GUICtrlCreateInput($tier5, 12,  194, 200, 18, -1)
$hleft5 = GUICtrlCreateLabel("Left:0",225,194,100,12,-1)
GUICtrlCreateGroup("Tier 6", 2, 222, 375, 40)
$htier6 = GUICtrlCreateInput($tier6, 12,  238, 200, 18, -1)
$hleft6 = GUICtrlCreateLabel("Left:0",225,238,100,12,-1)
GUICtrlCreateGroup("Tier 7", 2, 266, 375, 40)
$htier7 = GUICtrlCreateInput($tier7, 12,  282, 200, 18, -1)
$hleft7 = GUICtrlCreateLabel("Left:0",225,282,100,12,-1)
GUICtrlCreateGroup("Tier 8", 2, 310, 375, 40)
$htier8 = GUICtrlCreateInput($tier8, 12,  326, 200, 18, -1)
$hleft8 = GUICtrlCreateLabel("Left:0",225,326,100,12,-1)
$hSave = GUICtrlCreateButton("SAVE", 160,360)

$len1 = StringLen(GUICtrlRead($htier1))
$len2 = StringLen(GUICtrlRead($htier2))
$len3 = StringLen(GUICtrlRead($htier3))
$len4 = StringLen(GUICtrlRead($htier4))
$len5 = StringLen(GUICtrlRead($htier5))
$len6 = StringLen(GUICtrlRead($htier6))
$len7 = StringLen(GUICtrlRead($htier7))
$len8 = StringLen(GUICtrlRead($htier8))

While 1
	;Update character counts
	if $len1<>StringLen(GUICtrlRead($htier1)) then
	GUICtrlSetData($hleft1,"Left:"&18-StringLen(GUICtrlRead($htier1)))
	if StringMid(GUICtrlRead($hleft1),6,1)=="-" then GUICtrlSetData($hleft1,"Left: too long")
	$len1 = StringLen(GUICtrlRead($htier1))
	EndIf
	if $len2<>StringLen(GUICtrlRead($htier2)) then	
	GUICtrlSetData($hleft2,"Left:"&19-StringLen(GUICtrlRead($htier2)))
	if StringMid(GUICtrlRead($hleft2),6,1)=="-" then GUICtrlSetData($hleft2,"Left: too long")
	$len2 = StringLen(GUICtrlRead($htier2))
	EndIf
	if $len3<>StringLen(GUICtrlRead($htier3)) then	
	GUICtrlSetData($hleft3,"Left:"&16-StringLen(GUICtrlRead($htier3)))
	if StringMid(GUICtrlRead($hleft3),6,1)=="-" then GUICtrlSetData($hleft3,"Left: too long")
	$len3 = StringLen(GUICtrlRead($htier3))
	EndIf
	if $len4<>StringLen(GUICtrlRead($htier4)) then	
	GUICtrlSetData($hleft4,"Left:"&17-StringLen(GUICtrlRead($htier4)))
	if StringMid(GUICtrlRead($hleft4),6,1)=="-" then GUICtrlSetData($hleft4,"Left: too long")
	$len4 = StringLen(GUICtrlRead($htier4))
	EndIf
	if $len5<>StringLen(GUICtrlRead($htier5)) then
	GUICtrlSetData($hleft5,"Left:"&14-StringLen(GUICtrlRead($htier5)))
	if StringMid(GUICtrlRead($hleft5),6,1)=="-" then GUICtrlSetData($hleft5,"Left: too long")
	$len5 = StringLen(GUICtrlRead($htier5))
	EndIf
	if $len6<>StringLen(GUICtrlRead($htier6)) then	
	GUICtrlSetData($hleft6,"Left:"&25-StringLen(GUICtrlRead($htier6)))
	if StringMid(GUICtrlRead($hleft6),6,1)=="-" then GUICtrlSetData($hleft6,"Left: too long")
	$len6 = StringLen(GUICtrlRead($htier6))
	EndIf
	if $len7<>StringLen(GUICtrlRead($htier7)) then	
	GUICtrlSetData($hleft7,"Left:"&13-StringLen(GUICtrlRead($htier7)))
	if StringMid(GUICtrlRead($hleft7),6,1)=="-" then GUICtrlSetData($hleft7,"Left: too long")
	$len7 = StringLen(GUICtrlRead($htier7))
	EndIf
	if $len8<>StringLen(GUICtrlRead($htier8)) then
	GUICtrlSetData($hleft8,"Left:"&20-StringLen(GUICtrlRead($htier8)))
	if StringMid(GUICtrlRead($hleft8),6,1)=="-" then GUICtrlSetData($hleft8,"Left: too long")
	$len8 = StringLen(GUICtrlRead($htier8))
	EndIf
	
    $iMsg = GUIGetMsg()

   ; Main "Select" statement that handles other events
    Select
	Case $iMsg = $hSave
		$tier1=GUICtrlRead($htier1)
		$tier2=GUICtrlRead($htier2)
		$tier3=GUICtrlRead($htier3)
		$tier4=GUICtrlRead($htier4)
		$tier5=GUICtrlRead($htier5)
		$tier6=GUICtrlRead($htier6)
		$tier7=GUICtrlRead($htier7)
		$tier8=GUICtrlRead($htier8)
		While StringLen($tier1)<18
			$tier1=$tier1&" "
		WEnd
		While StringLen($tier1)>18
			$tier1=StringTrimRight($tier1,1)
		WEnd
		
		While StringLen($tier2)<19
			$tier2=$tier2&" "
		WEnd
		While StringLen($tier2)>19
			$tier2=StringTrimRight($tier2,1)
		WEnd
		
		While StringLen($tier3)<16
			$tier3=$tier3&" "
		WEnd
		While StringLen($tier3)>16
			$tier3=StringTrimRight($tier3,1)
		WEnd
		
		While StringLen($tier4)<17
			$tier4=$tier4&" "
		WEnd
		While StringLen($tier4)>17
			$tier4=StringTrimRight($tier4,1)
		WEnd
		
		While StringLen($tier5)<14
			$tier5=$tier5&" "
		WEnd
		While StringLen($tier5)>14
			$tier5=StringTrimRight($tier5,1)
		WEnd
		
		While StringLen($tier6)<25
			$tier6=$tier6&" "
		WEnd
		While StringLen($tier6)>25
			$tier6=StringTrimRight($tier6,1)
		WEnd
		
		While StringLen($tier7)<13
			$tier7=$tier7&" "
		WEnd
		While StringLen($tier7)>13
			$tier7=StringTrimRight($tier7,1)
		WEnd
		
		While StringLen($tier8)<20
			$tier8=$tier8&" "
		WEnd
		While StringLen($tier8)>20
			$tier8=StringTrimRight($tier8,1)
		WEnd
			
		_WriteFile($filename)
		
		if @error==0 then
			MsgBox(0,"Saved","File saved successfully")
		endif
		
	Case $iMsg = $GUI_EVENT_CLOSE 
		exit
            
    EndSelect
Wend


;write file (currently to qb.pak.new)
Func _WriteFile($filename)
$hfile=FileOpen($filename,18)
FileWrite($hfile,$part0)
FileClose($hfile)
$hfile=FileOpen($filename,17)

FileWrite($hfile,$part1)

FileWrite($hfile,$tier1)
FileWrite($hfile,$part2)

FileWrite($hfile,$tier2)
FileWrite($hfile,$part3)

FileWrite($hfile,$tier3)
FileWrite($hfile,$part4)

FileWrite($hfile,$tier4)
FileWrite($hfile,$part5)

FileWrite($hfile,$tier5)
FileWrite($hfile,$part6)

FileWrite($hfile,$tier6)
FileWrite($hfile,$part7)

FileWrite($hfile,$tier7)
FileWrite($hfile,$part8)

FileWrite($hfile,$tier8)
FileWrite($hfile,$part9)

FileWrite($hfile,$tier1)
FileWrite($hfile,$part11)

FileWrite($hfile,$tier2)
FileWrite($hfile,$part13)

FileWrite($hfile,$tier3)
FileWrite($hfile,$part15)

FileWrite($hfile,$tier4)
FileWrite($hfile,$part17)

FileWrite($hfile,$tier5)
FileWrite($hfile,$part19)

FileWrite($hfile,$tier6)
FileWrite($hfile,$part21)

FileWrite($hfile,$tier7)
FileWrite($hfile,$part23)

FileWrite($hfile,$tier8)
;~ FileWrite($hfile,$part25)

;~ FileWrite($hfile,$tier1)
;~ FileWrite($hfile,$part27)

;~ FileWrite($hfile,$tier2)
;~ FileWrite($hfile,$part29)

;~ FileWrite($hfile,$tier3)
;~ FileWrite($hfile,$part31)

;~ FileWrite($hfile,$tier4)
;~ FileWrite($hfile,$part33)

;~ FileWrite($hfile,$tier5)
;~ FileWrite($hfile,$part35)

;~ FileWrite($hfile,$tier6)
;~ FileWrite($hfile,$part37)

;~ FileWrite($hfile,$tier7)
;~ FileWrite($hfile,$part39)

;~ FileWrite($hfile,$tier8)
;~ FileWrite($hfile,$part41)

;~ FileWrite($hfile,$tier1)
;~ FileWrite($hfile,$part43)

;~ FileWrite($hfile,$tier2)
;~ FileWrite($hfile,$part45)

;~ FileWrite($hfile,$tier3)
;~ FileWrite($hfile,$part47)

;~ FileWrite($hfile,$tier4)
;~ FileWrite($hfile,$part49)

;~ FileWrite($hfile,$tier5)
;~ FileWrite($hfile,$part51)

;~ FileWrite($hfile,$tier6)
;~ FileWrite($hfile,$part53)

;~ FileWrite($hfile,$tier7)
;~ FileWrite($hfile,$part55)

;~ FileWrite($hfile,$tier8)
FileWrite($hfile,$part57)


FileClose($hfile)
EndFunc
