global GameConfig, EngineConfig, Memdata, OldRoot

fn GetBSps  =
	(
	try 
		(	
		Bsplist = getfiles (GameConfig.BSPRoot.text + "\\*.bsp") 
		NewItems = #()
		For entry in Bsplist do append newitems (filenamefrompath entry)
		EngineConfig.BSPs.items = NewItems
		)
 	catch (return false)
	)
	

fn GuessPaths TestRoot = 
	(
	TestModels = GetDirectories (Testroot + "\\models") ---- guess model source directory
	GameConfig.ContentRoot.text = \
		(if TestModels != #() then TestRoot + "\\models" else "")
	
	GameConfig.Engine.text = ""
	For Enginetest in #("tfc.exe","hl.exe","hl2.exe","tf2.exe","dummy.xyz") do 
		(
		if (getfiles (TestRoot + "\\" + Enginetest))[1] == TestRoot + "\\" + Enginetest  then
			(
			GameConfig.Engine.text = (TestRoot + "\\" + Enginetest)
			)	
		)
		
	TestStudio = Getfiles (TestRoot + "\\bin\\studiomdl.exe")
	Gameconfig.StudioMdl.text  = if TestStudio != #() then (TestRoot + "\\bin\\studiomdl.exe") else ""
	
	TestHLMV = Getfiles (TestRoot + "\\bin\\hlmv.exe")
	Gameconfig.HLMV.text =  if TestHLMV != #() then (TestRoot + "\\bin\\HLMV.exe") else ""
	
	
	TestMdls = filterstring GameConfig.GameRoot.text "\\"
	TestMdls = TestMdls[TestMdls.count] 
	GameConfig.BSPRoot.text = ""
	for MapDir in #(Testmdls + "\\models","TFC\\models","Valve\\models", "HL2\\models", "TF2\\models" ) do
		(
		Mdltest = (getfiles (TestRoot +"\\" + MapDir + "\\*.mdl"))
		if Mdltest.count > 0 then GameConfig.MdlRoot.text = TestRoot +"\\" + MapDir
		)
	
	
	TestMaps = filterstring GameConfig.GameRoot.text "\\"
	TestMaps = TestMaps[TestMaps.count] 
	GameConfig.BSPRoot.text = ""
	for MapDir in #(Testmaps + "\\maps","TFC\\maps","Valve\\Maps", "HL2\\maps", "TF2\\maps" ) do
		(
		BspTest = (getfiles (TestRoot +"\\" + MapDir + "\\*.bsp"))
		if BspTest.count > 0 then GameConfig.BSPRoot.text = TestRoot +"\\" + MapDir
		)
	GetBsps()
	)

	
Rollout GameConfig "Valve Content Setup"
	(
	label GRL "Game Root Directory" across:3 align:#right offset:[12,0]
	EditText GameRoot fieldwidth:150 offset:[12,0]
	button GameRootSet width:16 offset:[44,-2]
	
	on GameRoot entered text do
		
			(
			if OldRoot != text then
				 (
				OldRoot = text
				if Querybox "Attempt to update Paths?" then guesspaths text
				)		
			)
	
	on GameRootSet pressed do
		(
		rootpath = getSavePath "Choose Game Root Folder"
		if rootpath != undefined then 
			(
			GameRoot.text = rootpath
			if Querybox "Attempt to update Paths?" then guesspaths rootpath
			)
		)
	
	label CRL "Source root Directory" across:3 align:#right offset:[12,0]
	EditText ContentRoot fieldwidth:150 offset:[12,0]
	button ContentRootSet width:16 offset:[44,-2]
	
	on ContentRootSet pressed do
		(
		contentpath = getSavePath "Choose Source Root Folder"
		if contentpath != undefined then ContentRoot.text = contentpath
		)
		
	label ENL "Game Engine:" across:3 align:#right offset:[12,0]
	EditText Engine fieldwidth:150 offset:[12,0]
	button EngineSet width:16 offset:[44,-2]
	
	on EngineSet pressed do 
		(
		seed = GameRoot.text+"\\hl.exe" 
		EngPath = getopenfilename caption:"Locate game engine" \
		filename:seed types:" |*.exe|"
		if EngPath != undefined then Engine.text = EngPath
		)
	
	label BUILDL "StudioMdl location:" across:3 align:#right offset:[12,0]
	EditText StudioMdl fieldwidth:150 offset:[12,0]
	button StudioMdlSet width:16 offset:[44,-2]
	on StudioMdlSet pressed do 
		(
		seed = GameRoot.text+"\\bin\\StudioMdl.exe" 
		EngPath = getopenfilename caption:"Find Studiomdl" \
		filename:seed types:" |*.exe|"
		if EngPath != undefined then Engine.text = EngPath
		)

	
	label HLMVL "Modelviewer location:" across:3 align:#right offset:[12,0]
	Edittext HLMV fieldwidth:150 offset:[12,0]
	button HLMVSet width:16 offset:[44,-2]
	
	on HLMVset pressed do 
		(
		seed = GameRoot.text+"\\bin\\HLMV.exe" 
		hlmvpath = getopenfilename caption:"Find Hlmv" \
		filename:seed types:" |*.exe|"
		if hlmvpath != undefined then HLMV.text = hlmvpath
		)

	label MdlL ".mdl files location:" across:3 align:#right offset:[12,0]
	Edittext MdlRoot fieldwidth:150 offset:[12,0]
	button MdlSet width:16 offset:[44,-2]
	
	on MdlSet Pressed do
		(
		MdlRoot.text = ""
		MdlPath = getSavePath "Choose .mdl file Folder"
		if MdlPath != undefined then 
			(
			MdlRoot.text = MdlPath
			)

		)
	
	
	label BSPl "*.bsp files location:" across:3 align:#right offset:[12,0]
	Edittext BSProot fieldwidth:150 offset:[12,0]
	button BSPset width:16 offset:[44,-2]
	
	on BSPset pressed do
		(
		BspRoot.text = ""
		BspPath = getSavePath "Choose BSP file Folder"
		if BSPpath != undefined then 
			(
			BSProot.text = BspPath
			GetBsps()
			)

		)
	on BsProot entered text do (getbsps())
	
	--- startupscripts
	on GameConfig open do
		(
		MemData = openfile (scriptspath + "\\ValveConfig.dat")
		if Memdata == undefined then 
			(
			ProjectSeed = ""
			doscommand "Echo %qproject% > c:\\QP.txt"
			QPfileHandle = openfile ("C:\\QP.txt")
			ProjectSeed = readline QPFileHandle as string
			close QPFilehandle
			deletefile "c:\\qp.txt"
			if (getdirectories Projectseed) != #() then
				(
				GameConfig.GameRoot.text = "E:\\tf2" -- replace with QProject error warning
				guesspaths "E:\\tf2"
				)
			else
				(
				GameRoot.text == Projectseed
				GuessPaths ProjectSeed
				)
			)
		else -- read saved values
			(
			dummy = readline MemData
			GameRoot.text = readline MemData
			ContentRoot.text = readline MemData
			Engine.text = readline Memdata
			StudioMDL.text = Readline MemData
			HLMV.text = readline MemData
			MdlRoot.text = readline Memdata
			BSPRoot.text = readline MemData
			) --- Memdata is left open for second rollout
		)
	on GameConfig OKtoclose do  ---------------close and write data to file
		(
		OutPutFile = createfile (scriptspath + "\\Valveconfig.dat")
		warningflag = false
		format "Valve Game Configuration data\n" to:OutputFile
		for item in #(GameRoot.text, ContentRoot.text, Engine.text, Studiomdl.text, hlmv.text,Mdlroot.text,BSPRoot.text) do	
			(
			if item == "" do warningflag = true
			format "%\n" item to:OutPutFIle
			)
		format "%\n" EngineConfig.otherswitch.text to:Outputfile
		format "%\n" EngineConfig.console.state to:OutputFile
		format "%\n" EngineConfig.dev.state to:OutputFile
		format "%\n" EngineConfig.svcheats.state to:Outputfile
		format "%\n" EngineConfig.Map.state to:OutputFile
		format "%\n" EngineConfig.BSPs.selected to:Outputfile
		format "\n\n\n" to:outputfile
		close Outputfile
		
		if warningflag then 
			(
			if not Querybox "One or more of your settings are incorrect. \n This may cause problems with other tools. \n Proceed anyway?" then
				( 
				close Outputfile
				deletefile (scriptspath + "\\Valveconfig.dat")
				return false
				)
			)
		
		
		)
	) --- end of Game Config ROllout

Rollout EngineConfig "Engine Launch Options"
	(
	group "Command switches"
		(
		checkbox console "console" checked:true across:3
		checkbox dev "developer" checked:true
		checkbox svcheats "cheats" checked:true
		edittext otherswitch "other command line options"
		)
	label spacer ""
	radiobuttons Map "launch engine with" labels:#("no map","Ask at runtime","map:") columns:1 default:2 offset:[-90,0]	
	combobox BSPS width:200 offset:[42,-21] height:9
	on BSPs selected selection do
		(Map.state = 3)
	on EngineConfig open do
		(
		if GameConfig.BSProot.text != "" then GetBsps()
		if Memdata != undefined then
			(
			Engineconfig.otherswitch.text = readline Memdata
			if (readline Memdata) == "false" then EngineConfig.console.state = off
			if readline Memdata == "false" then EngineConfig.dev.state = off
			if readline Memdata == "false" then EngineConfig.svcheats.state = off
			try (EngineConfig.Map.state = readline Memdata) catch (EngineConfig.map.State = 1)
			try (EngineConfig.BSPs.selected = readline Memdata) catch ()
			close Memdata
			)		
	

		
		)
	)
Rollout Acc "Save settings"
	(
	button Accept "Accept and save these settings"
	
	On Accept pressed do 
		(
		closeRolloutFloater Valveconfig
		if QcMaster != undefined do (QCMaster.RefreshQCMaster())
		)
	)

ValveConfig = NewRolloutFloater "Valve Tools Configuration" 320 560
AddRollout GameConfig ValveConfig
AddRollout EngineConfig ValveConfig
AddRollout Acc Valveconfig
	
