Advertisement
lyzing

ThunderSwitch

Oct 13th, 2020
3,729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 9.42 KB | None | 0 0
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_Icon=main.ico
  3. #AutoIt3Wrapper_Outfile=..\ThunderSwitch.exe
  4. #AutoIt3Wrapper_UseUpx=n
  5. #AutoIt3Wrapper_UseX64=n
  6. #AutoIt3Wrapper_Res_Fileversion=1.0.4.0
  7. #AutoIt3Wrapper_Res_LegalCopyright=Paraly
  8. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  9.  
  10. FileInstall("F:\Documents\AutoIt\ThunderSwitch\Source\ThunderSwitchSVC.xml",@TempDir & "\ThunderSwitchSVC.xml",1)
  11.  
  12. Global $GPU = False, $GPU_Cache = -1, $GPU_Name = "", $GPU_Mode = "IGPU", $Paused = False, $FirstStart = False, $JustStarted = True, $RunOn_PID = ""
  13. $CheckGPUState_Timer = TimerInit()
  14.  
  15. Opt("TrayMenuMode", 1 + 2)
  16. Global $TRAY_Status = TrayCreateItem("Current Mode = ")
  17. TrayCreateItem("")
  18. Global $TRAY_Service = TrayCreateItem("Register Service for ThunderSwitch..")
  19. Global $TRAY_Pause = TrayCreateItem("Pause")
  20. Global $TRAY_Exit = TrayCreateItem("Exit")
  21. TraySetToolTip("ThunderSwitch")
  22.  
  23. _LogWrite("=================" & @CRLF,1)
  24. _LogWrite("Startup...")
  25.  
  26. AdlibRegister("_TrayMSG", 150)
  27.  
  28.  
  29.  
  30. If FileExists(@ScriptDir & "\config.ini") = 0 Then
  31.     $f = FileOpen(@ScriptDir & "\config.ini",10)
  32.     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")
  33.     FileClose($f)
  34.     $FirstStart = True
  35. EndIf
  36. If FileExists(@ScriptDir & "\EGPU") = 0 Then
  37.     DirCreate(@ScriptDir & "\EGPU")
  38. EndIf
  39. If FileExists(@ScriptDir & "\IGPU") = 0 Then
  40.     DirCreate(@ScriptDir & "\IGPU")
  41. EndIf
  42. If $FirstStart = True Then
  43.     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")
  44. EndIf
  45. If IniRead(@ScriptDir & "\config.ini","Settings","LogDetail",-1) = -1 Then
  46.     IniWrite(@ScriptDir & "\config.ini","Settings","LogDetail","1")
  47. EndIf
  48. If IniRead(@ScriptDir & "\config.ini","Settings","RunOnIGPU",-1) = -1 Then
  49.     IniWrite(@ScriptDir & "\config.ini","Settings","RunOnIGPU","")
  50. EndIf
  51. If IniRead(@ScriptDir & "\config.ini","Settings","RunOnEGPU",-1) = -1 Then
  52.     IniWrite(@ScriptDir & "\config.ini","Settings","RunOnEGPU","")
  53. EndIf
  54.  
  55.  
  56.  
  57. While 1
  58.     If TimerDiff($CheckGPUState_Timer) > 750 Then
  59.         $GPU = _CheckGPUState()
  60.         If $GPU <> $GPU_Cache Or $JustStarted = True Then
  61.             _LogWrite("GPU Switched to " & $GPU_Name)
  62.             _ApplySettings($GPU)
  63.             $GPU_Cache = $GPU
  64.  
  65.             If $GPU = True Then
  66.                 $GPU_Mode = "EGPU"
  67.             ElseIf $GPU = False Then
  68.                 $GPU_Mode = "IGPU"
  69.             EndIf
  70.  
  71.             If IniRead(@ScriptDir & "\config.ini","Settings","RunOn" & $GPU_Mode,"") <> "" Then
  72.                 If ProcessExists($RunOn_PID) > 0 Then
  73.                     ProcessClose($RunOn_PID)
  74.                     _LogWrite("Closed RunOn Process PID " & $RunOn_PID)
  75.                 EndIf
  76.                 $RunOn_PID = Run(IniRead(@ScriptDir & "\config.ini","Settings","RunOn" & $GPU_Mode,""))
  77.                 _LogWrite("Started [" & IniRead(@ScriptDir & "\config.ini","Settings","RunOn" & $GPU_Mode,"") & "] - PID " & $RunOn_PID)
  78.             EndIf
  79.             $JustStarted = False
  80.         EndIf
  81.  
  82.         TrayItemSetText($TRAY_Status,"Current Mode = " & $GPU_Mode)
  83.         $CheckGPUState_Timer = TimerInit()
  84.     EndIf
  85.     Sleep(50)
  86. WEnd
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. Func _ApplySettings($mode)
  96.     If $mode = False Then; IGPU
  97.         _LogWrite("Applying IGPU Settings...")
  98.         $LogDetail = IniRead(@ScriptDir & "\config.ini","Settings","LogDetail",1)
  99.         $CopyError = 0
  100.         $CopySuccess = 0
  101.         For $i = 1 To 2000
  102.             $t_File = IniRead(@ScriptDir & "\config.ini","Gamelist","File_" & $i,-1)
  103.             $t_Path = IniRead(@ScriptDir & "\config.ini","Gamelist","Path_" & $i,-1)
  104.             If $t_File <> -1 And $t_Path <> -1 Then
  105.                 If FileCopy(@ScriptDir & "\IGPU\" & $t_File, $t_Path,1) <> 1 Then
  106.                     $CopyError += 1
  107.                     If $LogDetail = 2 Then
  108.                         _LogWrite("(IGPU -) ["&$t_File&"] couldn't be applied at ["&$t_Path&"]")
  109.                     EndIf
  110.                 Else
  111.                     $CopySuccess += 1
  112.                     If $LogDetail = 2 Then
  113.                         _LogWrite("(IGPU +) ["&$t_File&"] applied at ["&$t_Path&"]")
  114.                     EndIf
  115.                 EndIf
  116.             Else
  117.                 ExitLoop
  118.             EndIf
  119.         Next
  120.         _LogWrite($CopySuccess & " IGPU Settings Applied (" & $CopyError & " Errors)")
  121.     ElseIf $mode = True Then; EGPU
  122.         _LogWrite("Applying EGPU Settings...")
  123.         $LogDetail = IniRead(@ScriptDir & "\config.ini","Settings","LogDetail",1)
  124.         $CopyError = 0
  125.         $CopySuccess = 0
  126.         For $i = 1 To 2000
  127.             $t_File = IniRead(@ScriptDir & "\config.ini","Gamelist","File_" & $i,-1)
  128.             $t_Path = IniRead(@ScriptDir & "\config.ini","Gamelist","Path_" & $i,-1)
  129.             If $t_File <> -1 And $t_Path <> -1 Then
  130.                 If FileCopy(@ScriptDir & "\EGPU\" & $t_File, $t_Path,1) <> 1 Then
  131.                     $CopyError += 1
  132.                     If $LogDetail = 2 Then
  133.                         _LogWrite("(EGPU -) ["&$t_File&"] couldn't be applied at ["&$t_Path&"]")
  134.                     EndIf
  135.                 Else
  136.                     $CopySuccess += 1
  137.                     If $LogDetail = 2 Then
  138.                         _LogWrite("(EGPU +) ["&$t_File&"] applied at ["&$t_Path&"]")
  139.                     EndIf
  140.                 EndIf
  141.             Else
  142.                 ExitLoop
  143.             EndIf
  144.         Next
  145.         _LogWrite($CopySuccess & " EGPU Settings Applied (" & $CopyError & " Errors)")
  146.     EndIf
  147. EndFunc
  148.  
  149.  
  150. Func _LogWrite($tString,$mode = 0)
  151.     If FileExists(@ScriptDir & "\log.txt") = 0 Then
  152.         $tf = FileOpen(@ScriptDir & "\log.txt",10)
  153.         FileClose($tf)
  154.     EndIf
  155.     $f = FileOpen(@ScriptDir & "\log.txt",1)
  156.     If $mode = 0 Then
  157.         FileWrite($f,@MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " -- " & $tString & @CRLF)
  158.     ElseIf $mode = 1 Then
  159.         FileWrite($f,$tString)
  160.     EndIf
  161.     FileClose($f)
  162. EndFunc
  163.  
  164.  
  165. Func _CheckGPUState()
  166.     $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
  167.     $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DisplayConfiguration", "WQL", 0x10 + 0x20)
  168.     If IsObj($colItems) then
  169.        For $objItem In $colItems
  170.             $GPU_Name = $objItem.DeviceName
  171.        Next
  172.     Endif
  173.  
  174.     If StringRegExp($GPU_Name,"(?i)Intel",0) = 1 Then
  175.         Return False
  176.     Else
  177.         Return True
  178.     EndIf
  179. EndFunc
  180.  
  181.  
  182. Func _TrayMSG()
  183.     Local $msg = TrayGetMsg()
  184.     Select
  185.         Case $msg = $TRAY_Status
  186.             $t = MsgBox(4,"ThunderSwitch", "Re-apply " & $GPU_Mode & " Settings?")
  187.             If $t = 6 Then
  188.                 _ApplySettings($GPU)
  189.             EndIf
  190.         Case $msg = $TRAY_Service
  191.             if IsAdmin() Then
  192.                 $f = FileOpen(@TempDir & "\ThunderSwitchSVC.xml",0)
  193.                 $ThunderSwitchSVC_XML = FileRead($f)
  194.                 FileClose($f)
  195.                 $f = FileOpen(@TempDir & "\ThunderSwitchSVC.xml",10)
  196.                 FileWrite($f,StringRegExpReplace($ThunderSwitchSVC_XML,"REPLACEMARK",StringRegExpReplace(@ScriptFullPath,"\\","\\\\")))
  197.                 FileClose($f)
  198.  
  199.                 If _ServiceExist() = True Then
  200.                     $t = MsgBox(64 + 4,"ThunderSwitch","Service already exists, remove the service?")
  201.                     If $t = 6 Then
  202.                         RunWait('schtasks.exe /delete /tn ThunderSwitch /f',"",@SW_HIDE)
  203.                         If _ServiceExist() = False Then
  204.                             _LogWrite("ThunderSwitch Service successfully removed")
  205.                             MsgBox(64,"ThunderSwitch","ThunderSwitch Service got removed.")
  206.                         Else
  207.                             _LogWrite("ThunderSwitch Service couldn't be removed")
  208.                             MsgBox(16,"ThunderSwitch","Error, couldn't remove the ThunderSwitch Service.")
  209.                         EndIf
  210.                     EndIf
  211.                 EndIf
  212.                 If _ServiceExist() = False Then
  213.                     MsgBox(64,"ThunderSwitch","You will be asked to enter your Windows password in the next window to register ThunderSwitch as a Serivce.")
  214.                     RunWait('schtasks.exe /create /tn ThunderSwitch /xml "' & @TempDir & '\ThunderSwitchSVC.xml" /ru ' & @ComputerName & '\' & @UserName)
  215.                 EndIf
  216.                 If _ServiceExist() = True Then
  217.                     _LogWrite("Registered ThunderSwitch as Service")
  218.                     MsgBox(64,"ThunderSwitch","ThunderSwitch got registered as Service.")
  219.                 Else
  220.                     _LogWrite("Couldn't register ThunderSwitch as Service")
  221.                     MsgBox(16,"ThunderSwitch","Error at registering ThunderSwitch as Service.")
  222.                 EndIf
  223.             Else
  224.                 _LogWrite("Couldn't register ThunderSwitch as Service because of insufficient permissions")
  225.                 MsgBox(16,"ThunderSwitch","Error, registering ThunderSwitch as Service requires Admin privileges." & @CRLF & "Restart ThunderSwitch as Admin")
  226.             EndIf
  227.         Case $msg = $TRAY_Pause
  228.             $Paused = Not $Paused
  229.             If $Paused = True Then
  230.                 _LogWrite("Toggled Pause ON")
  231.                 TrayItemSetText($TRAY_Pause,"Unpause")
  232.                 While $Paused = True
  233.                     Local $msg = TrayGetMsg()
  234.                     Select
  235.                         Case $msg = $TRAY_Pause
  236.                             _LogWrite("Toggled Pause OFF")
  237.                             TrayItemSetText($TRAY_Pause,"Pause")
  238.                             $Paused = False
  239.                         Case $msg = $TRAY_Exit
  240.                             _LogWrite("Exiting...")
  241.                             _LogWrite("=================" & @CRLF,1)
  242.                             Exit
  243.                     EndSelect
  244.                 WEnd
  245.             EndIf
  246.         Case $msg = $TRAY_Exit
  247.             _LogWrite("Exiting...")
  248.             _LogWrite("=================" & @CRLF,1)
  249.             Exit
  250.     EndSelect
  251. EndFunc
  252.  
  253.  
  254. Func _ServiceExist()
  255.     $Powershell_PID = run('powershell Get-ScheduledTask | Get-ScheduledTaskInfo', @WindowsDir, @SW_HIDE, 0x2)
  256.     $Powershell_Stdout = ""
  257.      While 1
  258.         $Powershell_Stdout &= StdoutRead($Powershell_PID)
  259.         If @error Then
  260.             ExitLoop
  261.         EndIf
  262.      WEnd
  263.  
  264.     If StringRegExp($Powershell_Stdout,"TaskName           : ThunderSwitch",0) = 1 Then
  265.         Return True
  266.     Else
  267.         Return False
  268.     EndIf
  269. EndFunc
  270.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement