Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
- #AutoIt3Wrapper_Icon=main.ico
- #AutoIt3Wrapper_Outfile=..\ThunderSwitch.exe
- #AutoIt3Wrapper_UseUpx=n
- #AutoIt3Wrapper_UseX64=n
- #AutoIt3Wrapper_Res_Fileversion=1.0.3.0
- #AutoIt3Wrapper_Res_LegalCopyright=Paraly
- #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
- FileInstall("F:\Documents\AutoIt\ThunderSwitch\Source\ThunderSwitchSVC.xml",@TempDir & "\ThunderSwitchSVC.xml",1)
- Global $GPU = False, $GPU_Cache = -1, $GPU_Name = "", $GPU_Mode = "IGPU", $Paused = False, $FirstStart = False, $JustStarted = True, $RunOn_PID = ""
- $CheckGPUState_Timer = TimerInit()
- Opt("TrayMenuMode", 1 + 2)
- Global $TRAY_Status = TrayCreateItem("Current Mode = ")
- TrayCreateItem("")
- Global $TRAY_Service = TrayCreateItem("Register Service for ThunderSwitch..")
- Global $TRAY_Pause = TrayCreateItem("Pause")
- Global $TRAY_Exit = TrayCreateItem("Exit")
- TraySetToolTip("ThunderSwitch")
- _LogWrite("=================" & @CRLF,1)
- _LogWrite("Startup...")
- AdlibRegister("_TrayMSG", 150)
- If FileExists(@ScriptDir & "\config.ini") = 0 Then
- $f = FileOpen(@ScriptDir & "\config.ini",10)
- FileWrite($f,"[Settings]" & @CRLF & "RunOnIGPU=" & @CRLF & "RunOnEGPU=" & @CRLF & @CRLF & "[Gamelist]" & @CRLF & "File_1=DragonQuestXI_GameUserSettings.ini" & @CRLF & "Path_1=C:\Users\ParalyMax\Documents\my games\DRAGON QUEST XI\Saved\Config\WindowsNoEditor\GameUserSettings.ini" & @CRLF & "File_2=Borderlands3_GameUserSettings.ini" & @CRLF & "Path_2=C:\Users\ParalyMax\Documents\my games\Borderlands 3\Saved\Config\WindowsNoEditor\GameUserSettings.ini" & @CRLF & "File_3=MonsterHunterWorld_graphics_option.ini" & @CRLF & "Path_3=C:\Program Files (x86)\Steam\steamapps\common\Monster Hunter World\graphics_option.ini")
- FileClose($f)
- $FirstStart = True
- EndIf
- If FileExists(@ScriptDir & "\EGPU") = 0 Then
- DirCreate(@ScriptDir & "\EGPU")
- EndIf
- If FileExists(@ScriptDir & "\IGPU") = 0 Then
- DirCreate(@ScriptDir & "\IGPU")
- EndIf
- If $FirstStart = True Then
- MsgBox(0,"ThunderSwitch","config.ini file got created" & @CRLF & "You've to copy setting files into the EGPU and IGPU folders and then declare them at the config.ini" & @CRLF & "The config.ini file expects the file name within the EGPU/IGPU folder and the full path it should copy it to, a few example lines got added")
- EndIf
- While 1
- If TimerDiff($CheckGPUState_Timer) > 750 Then
- $GPU = _CheckGPUState()
- If $GPU <> $GPU_Cache Or $JustStarted = True Then
- _LogWrite("GPU Switched to " & $GPU_Name)
- _ApplySettings($GPU)
- $GPU_Cache = $GPU
- If $GPU = True Then
- $GPU_Mode = "EGPU"
- ElseIf $GPU = False Then
- $GPU_Mode = "IGPU"
- EndIf
- If IniRead(@ScriptDir & "\config.ini","Settings","RunOn" & $GPU_Mode,"") <> "" Then
- If ProcessExists($RunOn_PID) > 0 Then
- ProcessClose($RunOn_PID)
- _LogWrite("Closed RunOn Process PID " & $RunOn_PID)
- EndIf
- $RunOn_PID = Run(IniRead(@ScriptDir & "\config.ini","Settings","RunOn" & $GPU_Mode,""))
- _LogWrite("Started [" & IniRead(@ScriptDir & "\config.ini","Settings","RunOn" & $GPU_Mode,"") & "] - PID " & $RunOn_PID)
- EndIf
- $JustStarted = False
- EndIf
- TrayItemSetText($TRAY_Status,"Current Mode = " & $GPU_Mode)
- $CheckGPUState_Timer = TimerInit()
- EndIf
- Sleep(50)
- WEnd
- Func _ApplySettings($mode)
- If $mode = False Then; IGPU
- _LogWrite("Applying IGPU Settings...")
- $CopyError = 0
- $CopySuccess = 0
- For $i = 1 To 2000
- $t_File = IniRead(@ScriptDir & "\config.ini","Gamelist","File_" & $i,-1)
- $t_Path = IniRead(@ScriptDir & "\config.ini","Gamelist","Path_" & $i,-1)
- If $t_File <> -1 And $t_Path <> -1 Then
- If FileCopy(@ScriptDir & "\IGPU\" & $t_File, $t_Path,9) <> 1 Then
- $CopyError += 1
- Else
- $CopySuccess += 1
- EndIf
- Else
- ExitLoop
- EndIf
- Next
- _LogWrite($CopySuccess & " IGPU Settings Applied (" & $CopyError & " Errors)")
- ElseIf $mode = True Then; EGPU
- _LogWrite("Applying EGPU Settings...")
- $CopyError = 0
- $CopySuccess = 0
- For $i = 1 To 2000
- $t_File = IniRead(@ScriptDir & "\config.ini","Gamelist","File_" & $i,-1)
- $t_Path = IniRead(@ScriptDir & "\config.ini","Gamelist","Path_" & $i,-1)
- If $t_File <> -1 And $t_Path <> -1 Then
- If FileCopy(@ScriptDir & "\EGPU\" & $t_File, $t_Path,9) <> 1 Then
- $CopyError += 1
- Else
- $CopySuccess += 1
- EndIf
- Else
- ExitLoop
- EndIf
- Next
- _LogWrite($CopySuccess & " EGPU Settings Applied (" & $CopyError & " Errors)")
- EndIf
- EndFunc
- Func _LogWrite($tString,$mode = 0)
- If FileExists(@ScriptDir & "\log.txt") = 0 Then
- $tf = FileOpen(@ScriptDir & "\log.txt",10)
- FileClose($tf)
- EndIf
- $f = FileOpen(@ScriptDir & "\log.txt",1)
- If $mode = 0 Then
- FileWrite($f,@MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " -- " & $tString & @CRLF)
- ElseIf $mode = 1 Then
- FileWrite($f,$tString)
- EndIf
- FileClose($f)
- EndFunc
- Func _CheckGPUState()
- $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
- $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DisplayConfiguration", "WQL", 0x10 + 0x20)
- If IsObj($colItems) then
- For $objItem In $colItems
- $GPU_Name = $objItem.DeviceName
- Next
- Endif
- If StringRegExp($GPU_Name,"(?i)Intel",0) = 1 Then
- Return False
- Else
- Return True
- EndIf
- EndFunc
- Func _TrayMSG()
- Local $msg = TrayGetMsg()
- Select
- Case $msg = $TRAY_Status
- $t = MsgBox(4,"ThunderSwitch", "Re-apply " & $GPU_Mode & " Settings?")
- If $t = 6 Then
- _ApplySettings($GPU)
- EndIf
- Case $msg = $TRAY_Service
- if IsAdmin() Then
- $f = FileOpen(@TempDir & "\ThunderSwitchSVC.xml",0)
- $ThunderSwitchSVC_XML = FileRead($f)
- FileClose($f)
- $f = FileOpen(@TempDir & "\ThunderSwitchSVC.xml",10)
- FileWrite($f,StringRegExpReplace($ThunderSwitchSVC_XML,"REPLACEMARK",StringRegExpReplace(@ScriptFullPath,"\\","\\\\")))
- FileClose($f)
- If _ServiceExist() = True Then
- $t = MsgBox(64 + 4,"ThunderSwitch","Service already exists, remove the service?")
- If $t = 6 Then
- RunWait('schtasks.exe /delete /tn ThunderSwitch /f',"",@SW_HIDE)
- If _ServiceExist() = False Then
- _LogWrite("ThunderSwitch Service successfully removed")
- MsgBox(64,"ThunderSwitch","ThunderSwitch Service got removed.")
- Else
- _LogWrite("ThunderSwitch Service couldn't be removed")
- MsgBox(16,"ThunderSwitch","Error, couldn't remove the ThunderSwitch Service.")
- EndIf
- EndIf
- EndIf
- If _ServiceExist() = False Then
- MsgBox(64,"ThunderSwitch","You will be asked to enter your Windows password in the next window to register ThunderSwitch as a Serivce.")
- RunWait('schtasks.exe /create /tn ThunderSwitch /xml "' & @TempDir & '\ThunderSwitchSVC.xml" /ru ' & @ComputerName & '\' & @UserName)
- EndIf
- If _ServiceExist() = True Then
- _LogWrite("Registered ThunderSwitch as Service")
- MsgBox(64,"ThunderSwitch","ThunderSwitch got registered as Service.")
- Else
- _LogWrite("Couldn't register ThunderSwitch as Service")
- MsgBox(16,"ThunderSwitch","Error at registering ThunderSwitch as Service.")
- EndIf
- Else
- _LogWrite("Couldn't register ThunderSwitch as Service because of insufficient permissions")
- MsgBox(16,"ThunderSwitch","Error, registering ThunderSwitch as Service requires Admin privileges." & @CRLF & "Restart ThunderSwitch as Admin")
- EndIf
- Case $msg = $TRAY_Pause
- $Paused = Not $Paused
- If $Paused = True Then
- _LogWrite("Toggled Pause ON")
- While $Paused = True
- Local $msg = TrayGetMsg()
- Select
- Case $msg = $TRAY_Pause
- _LogWrite("Toggled Pause OFF")
- $Paused = False
- Case $msg = $TRAY_Exit
- _LogWrite("Exiting...")
- _LogWrite("=================" & @CRLF,1)
- Exit
- EndSelect
- WEnd
- EndIf
- Case $msg = $TRAY_Exit
- _LogWrite("Exiting...")
- _LogWrite("=================" & @CRLF,1)
- Exit
- EndSelect
- EndFunc
- Func _ServiceExist()
- $Powershell_PID = run('powershell Get-ScheduledTask | Get-ScheduledTaskInfo', @WindowsDir, @SW_HIDE, 0x2)
- $Powershell_Stdout = ""
- While 1
- $Powershell_Stdout &= StdoutRead($Powershell_PID)
- If @error Then
- ExitLoop
- EndIf
- WEnd
- If StringRegExp($Powershell_Stdout,"TaskName : ThunderSwitch",0) = 1 Then
- Return True
- Else
- Return False
- EndIf
- EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement