lyzing

ThunderSwitch

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