DigitalPhreaker

BatchGameLauncherTemplate

Dec 8th, 2018 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 13.01 KB | None | 0 0
  1. :: Batch Game Launcher Template
  2. :: /u/DigitalPhreaker
  3. :: Last Modified: 2018-12-08
  4. :: When run as Admin, this script will call PowerShell to disable Windows Defender, launch the provided program/game, lower the priorities of provided programs, set the program/game's priority to high, then pause. Once the game has closed and the user strikes another key, the script will continue, re-enabling Windows Defender, re-launching any programs set below, resetting priorities back to normal, and then finally backing up  
  5.  
  6. @echo off
  7.  
  8. ::----------------------------------------------------------------------------------------------------------------
  9. ::                              BEGIN MAIN VARIABLE DECLARATIONS
  10. ::
  11. :: Search and replace the following variables before you forget
  12. ::
  13. :: If launching the game through Steam, be sure to set the STEAMCOMMAND variable and comment out the
  14. ::  start "" %GAMEEXE%
  15. :: and
  16. ::  echo %STARTNOTIFY%
  17. :: commands directly before :NOTFOUND below.
  18. ::
  19. :: If launching the executable directly, do the opposite of above, commenting out
  20. ::  start "" %STEAMCOMMAND%
  21. :: and
  22. ::  echo %STEAMNOTIFY%
  23. ::
  24. ::  GAMPEATH: Main parent directory where the game is installed (where the main executable is stored). Do not include a final backslash .
  25. set GAMEPATH=D:\Games\Just Cause 4
  26. ::
  27. ::  GAMEEXE:  Pretty self-explanatory
  28. set GAMEEXE=JustCause4.exe
  29. ::
  30. ::  STEAMCOMMAND: Command line argument to launch a game through Steam, where XXXXXXX is the AppID. You can find this AppID by Googling "[Game Name] Steam AppID". Commented out as CPY's version of JC4 obviously doesn't go through Steam to launch, but left it in so you can see how to use this for other games.
  31. ::set STEAMCOMMAND=steam://run/XXXXXXX
  32. ::
  33. ::  BACKUPDIRECTORY: Main parent directory where you'd like your game saves/settings backups stored. Like GAMEPATH, do not include a final backslash.
  34. set BACKUPDIRECTORY=F:\BACKUPS\_GAMESAVES
  35. ::
  36. ::  GAMENAME: Whatever you want it to be set as, but DO NOT include special characters (':', '\', etc.) as this variable is used as part of the directory path for the backup location string later on, and Windows does not allow special characters in directory names.
  37. set GAMENAME=Just Cause 4
  38. ::
  39. ::  SAVELOCATION: Main parent directory where the game's save files are stored. For *most* Steam games, this will be %SteamInstallDirectory%\userdata\XXXXXXXX\AppID" (where XXXXXXXX is a random number assigned to your Steam user ID).
  40. ::  For other games (and some Steam games) this can be in your user Documents directory, or a handful of other locations. You'll want to search for where the saves are located, then set that directory here (again, without a closing backslash). For JC4, though, they are stored in:
  41. set SAVELOCATION=C:\Users\YourWindowsUsername\Documents\Square Enix\Just Cause 4\LocalData
  42. ::
  43. :: STARTNOTIFYSTEAM and STARTNOTIFY are simply used later in the script to notify you when the game has been launched. I use one for Steam games and one for non-Steam games so I remember that Steam games may take a few extra seconds to start
  44. ::
  45. ::set STARTNOTIFYSTEAM=%GAMENAME% has been started through Steam.
  46. set STARTNOTIFY=%GAMENAME% has been started, but will immediately close as you've had to launch this script with Admin rights. (%GAMENAME% cannot run with Admin rights and crashes immediately.) Use this time to manually launch it yourself. The script will still set everything as soon as it detects the game is running.
  47. ::
  48. ::
  49. ::                              END MAIN VARIABLE DECLARATIONS
  50. ::----------------------------------------------------------------------------------------------------------------
  51. goto DISABLEDEFENDER
  52.  
  53. :DEFDISABLED
  54. SETLOCAL EnableExtensions
  55. ::----------------------------------------------------------------------------------------------------------------
  56. ::                              SET PROGRAM AND PRIORITY VARIABLES
  57. ::
  58. :: Use 'wmic processs where name="programExecutable.exe" CALL setpriority "n"' to set any program matching that name
  59. :: to priority level n
  60. ::
  61. ::      wmic
  62. ::          Calls Windows Management Instrumentation Command to execute the commands following it
  63. ::      processs where name="programExecutable.exe"
  64. ::          Commands WMIC to search for any running process matching the name programExecutable.exe
  65. ::      CALL setpriority "n"
  66. ::          Calls the setpriority command and sets programExecutable.exe's prirority to n, where n =           
  67. ::              "64"    = Idle
  68. ::              "16384" = Below Normal
  69. ::              "32"    = Normal
  70. ::              "32768" = Above Normal
  71. ::              "128"   = High
  72. ::              "256"   = Real Time (NEVER RECOMMENDED)
  73. ::
  74. :: Use 'taskkill /F /T /IM programExecutable.exe' to kill any running program that matches that name, as well as any child processes started by it.
  75. ::      /F
  76. ::          tells taskkill to forcefully kill the program immediately.
  77. ::      /T
  78. ::          tells taskkill to also kill any child process launched by 'programExecutable.exe'
  79. ::      /IM
  80. ::          tells taskkill to kill any running program matching 'programExecutable.exe'
  81. ::-----------------------
  82. set IDLE=CALL setpriority "64"
  83. set BELOWNORMAL=CALL setpriority "16384"
  84. set NORMAL=CALL setpriority "32"
  85. set ABOVENORMAL=CALL setpriority "32768"
  86. set HIGH=CALL setpriority "128"
  87. ::-----------------------
  88. ::
  89. set GAMEPRIORITY=wmic process where name="%GAMEEXE%"
  90. ::
  91. ::-----------------------
  92. ::
  93. ::  Use this section to set any variable you want to enable the script to set its corresponding program's priority. Just make sure you get the executable's name EXACTLY as it appears in Task Manager/Process Explorer, or else the script will just skip over it. The variable's name can be whatever you want. The ones below are for programs that pretty much run on all Windows 10 machines regardless of setup, but you can add or remove as many as you want.
  94. ::
  95. ::  While we've already disabled Windows Defender and thus it shouldn't be interfering with the game, I still have it's main executable set to low priority anyway just in case.
  96. set WINDEFENDER=wmic process where name="MsMpEng.exe"
  97. ::
  98. set WINEXPLORER=wmic process where name="explorer.exe"
  99. set SEARCHUI=wmic process where name="SearchUI.exe"
  100. set SEARCHINDEXER=wmic process where name="SearchIndexer.exe"
  101. set SHELLHOST=wmic process where name="ShellExperienceHost.exe"
  102. set STEAM=wmic process where name="steam.exe"
  103. set STEAMWEB=wmic process where name="Steamwebhelper.exe"
  104. set XBOXAPP=wmic process where name="XboxApp.exe"
  105. set GAMEBAR=wmic process where name="GameBar.exe"
  106. ::-----------------------
  107. ::
  108. ::  For programs you'd rather just have killed when starting the game, use this section.
  109. ::  
  110. set FORCEKILL=taskkill /F /T /IM
  111. set SOFTKILL=taskkill /T /IM
  112. ::
  113. ::  This is used as part of FORCEKILL/SOFTKILL down below. Set the program executable's name here.
  114. set FIREFOX=firefox.exe
  115. ::
  116. ::  This is used to re-launch the program after the game has closed and you've told the script to continue.
  117. set FIREFOXPATH=C:\Program Files\Mozilla Firefox\firefox.exe
  118. ::
  119. ::-----------------------
  120. ::
  121. ::    These commands will set your computer's power configuration to high performance (POWERHIGH), balanced (POWERBALANCED), or power saver (POWERLOW). I've commented these out as I'm on desktop, and my power configuration is always set to high and will never be set below that. However, if you're on a laptop and would like to enable these, they can give you a bit of an extra kick while ensuring they're set back to a proper config once the script has finished.
  122. ::
  123. set POWERHIGH=powercfg -s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
  124. set POWERBALANCED=powercfg -s 381b4222-f694-41f0-9685-ff5bb260df2e
  125. set POWERLOW=powercfg -s  a1841308-3541-4fab-bc81-f71556f20b4a
  126. ::-----------------------
  127.  
  128. ::    These are the commands to launch the games and notify you when they've been launched.
  129.  
  130. ::    'start' instructs cmd.exe to start the following program/command.
  131. ::    'timeout n' pauses the script for 'n' seconds.
  132. ::    'echo' prints the following string for you to read.
  133. ::    'echo[' prints a blank line.
  134.  
  135.  
  136. start "" "%GAMEPATH%\%GAMEEXE%"
  137. ::start "" %STEAMCOMMAND%
  138. timeout 5
  139. echo %STARTNOTIFY%
  140. ::echo %STARTNOTIFYSTEAM%
  141. echo[
  142. echo -------------------------------------------------------------------------------------------------------------
  143. echo[
  144. ::----------------------------------------------------------------------------------------------------------------
  145.  
  146. ::----------------------------------------------------------------------------------------------------------------
  147. :NOTFOUND
  148. :: The script enters a for loop at this section. The loop repeats every 30 seconds until %GAMEEXE% is detected, and then the script is told to go to :FOUND, where it will continue on to set priorities. You can set the timeout below to whatever you want; I have it set to 30 specifically in this case because the CPY version of JC4 cannot run with admin rights, and will crash as soon as it launches. With the timeout set to 30, the script won't detect it before it crashes, and therefore will only detect it after it's been manually launched.
  149. ::
  150. echo %GAMENAME% is not running. Waiting 30 seconds.
  151. timeout 30
  152. FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %GAMEEXE%"') DO IF %%x == %GAMEEXE% goto FOUND
  153. goto NOTFOUND
  154. ::----------------------------------------------------------------------------------------------------------------
  155.  
  156. ::----------------------------------------------------------------------------------------------------------------
  157. :FOUND
  158. ::%DATE:~10,4% = YYYY
  159. ::%DATE:~4,2% = MM
  160. ::%DATE:~7,2% = DD
  161. ::%TIME:~0,2% = HH
  162. ::%TIME:~3,2% = MM
  163. ::%TIME:~6,2% = SS
  164. set STARTDATE=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%
  165. set STARTTIME=%TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2%
  166. ::-----------------------
  167. ::          WRITE LAUNCH DATE/TIME TO FILE
  168. echo %GAMENAME% launched on %STARTDATE% at %STARTTIME% >> "%GAMEPATH%\_LaunchAndCloseTimes.txt"
  169. ::
  170. ::-----------------------
  171. cls
  172. echo[
  173. echo -------------------------------------------------------------------------------------------------------------
  174. echo[
  175. echo %GAMENAME% is running!
  176. echo[
  177. echo Killing chosen programs and setting priorities in 5 seconds.
  178. echo[
  179. timeout 5
  180. echo[
  181. ::-----------------------
  182. ::%POWERHIGH%
  183. ::-----------------------
  184. %SOFTKILL% %FIREFOX%
  185. ::-----------------------
  186. %GAMEPRIORITY% %HIGH%
  187. %STEAM% %BELOWNORMAL%
  188. %STEAMWEB% %IDLE%
  189. %WINDEFENDER% %IDLE%
  190. %SEARCHINDEXER% %IDLE%
  191. %XBOXAPP% %BELOWNORMAL%
  192. %WINEXPLORER% %BELOWNORMAL%
  193. %SEARCHUI% %IDLE%
  194. %SHELLHOST% %IDLE%
  195. ::-----------------------
  196. ::
  197. cls
  198. echo[
  199. echo %GAMENAME% launched on %STARTDATE% at %STARTTIME%
  200. echo[
  201. echo Script will now pause until you come back to this window and press any key to make it continue. Enjoy the game!
  202. pause
  203. goto BACKUP
  204. ::----------------------------------------------------------------------------------------------------------------
  205.  
  206. :BACKUP
  207. ::%DATE:~10,4% = YYYY
  208. ::%DATE:~4,2% = MM
  209. ::%DATE:~7,2% = DD
  210. ::%TIME:~0,2% = HH
  211. ::%TIME:~3,2% = MM
  212. ::%TIME:~6,2% = SS
  213. set ENDDATE=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%
  214. set ENDTIME=%TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2%
  215. set TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2% %TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
  216. set BACKUPLOCATION=%BACKUPDIRECTORY%\%GAMENAME%\%TIMESTAMP%
  217. echo %GAMENAME% ended    on %ENDDATE% at %ENDTIME%
  218. echo %GAMENAME% ended    on %ENDDATE% at %ENDTIME% >> "%GAMEPATH%\_LaunchAndCloseTimes.txt"
  219. echo ----------------------------------------------------- >> "%GAMEPATH%\_LaunchAndCloseTimes.txt"
  220. mkdir "%BACKUPLOCATION%"
  221. copy "%SAVELOCATION%" "%BACKUPLOCATION%"
  222. copy "%GAMEPATH%\_LaunchAndCloseTimes.txt" "%BACKUPLOCATION%\_LaunchAndCloseTimes.txt"
  223. echo[
  224. echo -------------------------------------------------------------------------------------------------------------
  225. echo Game saves successfully backed up.
  226. echo[
  227. goto ENABLEDEFENDER
  228.  
  229. :DEFENABLED
  230. SETLOCAL EnableExtensions
  231. echo -------------------------------------------------------------------------------------------------------------
  232. echo[
  233. echo Restoring process priorities and relaunching programs...
  234. echo[
  235. ::-----------------------
  236. %STEAM% %NORMAL%
  237. %STEAMWEB% %NORMAL%
  238. %WINDEFENDER% %NORMAL%
  239. %SEARCHINDEXER% %NORMAL%
  240. %XBOXAPP% %NORMAL%
  241. %WINEXPLORER% %NORMAL%
  242. %SEARCHUI% %NORMAL%
  243. %SHELLHOST% %NORMAL%
  244. start "" "%FIREFOXPATH%"
  245. echo Firefox is launching...
  246. echo[
  247. ::-----------------------
  248. :: %POWERBALANCED%
  249. ::-----------------------
  250. echo -------------------------------------------------------------------------------------------------------------
  251. echo[
  252. echo All done!
  253. echo[
  254. pause
  255. exit
  256.  
  257. ::----------------------------------------------------------------------------------------------------------------
  258. ::
  259. :: Calls Windows PowerShell and executes the command "Set-MpPreference -DisableRealtimeMonitoring $true" which disables Windows Defender's real-time monitoring until ENABLEDEFENDER is called at the end of the script.
  260. :DISABLEDEFENDER
  261. echo Launching PowerShell and disabling Windows Defender...
  262. echo[
  263. PowerShell -Command "Set-MpPreference -DisableRealtimeMonitoring $true"
  264. echo[
  265. echo Windows Defender has been disabled.
  266. echo[
  267. goto DEFDISABLED
  268.  
  269. :ENABLEDEFENDER
  270. echo Launching PowerShell and enabling Windows Defender...
  271. echo[
  272. PowerShell -Command "Set-MpPreference -DisableRealtimeMonitoring $false"
  273. echo[
  274. echo Windows Defender has been enabled.
  275. echo[
  276. goto DEFENABLED
Add Comment
Please, Sign In to add comment