lyzing

ThunderSwitch

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