CheezusCrust

Server Script 2

May 15th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 14.17 KB | None | 0 0
  1. ::Want to understand how this code works? www.ss64.com/nt has a great explanation for most of the commands in here
  2.  
  3. ::Keep the window clean and easy to read
  4. @echo off
  5.  
  6. ::Set the title of the window
  7. title Garry's Mod Server Manager v0.2
  8.  
  9. ::Forces the if statements to be processed line-by-line instead of all at once (fixes some wonky variable issues)
  10. setlocal enabledelayedexpansion
  11.  
  12. :mainMenu
  13.  
  14. ::Clears the screen
  15. cls
  16.  
  17. color B
  18. echo ::================MAIN  MENU=====================::
  19. echo Welcome to Cheezus' Server Manager
  20. echo If this is your first time using this, follow all of the instructions carefully
  21. echo ::=============================================::
  22. echo:
  23. echo What would you like to do?
  24. echo:
  25. echo Type 1 to install a new server/download SteamCMD
  26. echo Type 2 to update an existing server
  27. echo Type 3 to generate a new SRCDS Guardian server launcher
  28. echo:
  29.  
  30. ::Simple prompt which asks the user what they want to do and goes to a different section of code accordingly
  31. :invalidAnswer1
  32. set /P c=Answer:
  33. if /I "!c!" EQU "1" goto newServer
  34. if /I "!c!" EQU "2" goto updateServer
  35. if /I "!c!" EQU "3" goto generateGuardian
  36. goto invalidAnswer1
  37.  
  38. :newServer
  39. cls
  40.  
  41. color C
  42. echo ::=====::
  43. echo IMPORTANT
  44. echo ::=====::
  45. echo:
  46. echo When this script runs it will generate a cfg.txt file which will store some
  47. echo information regarding your server. It is recomended that you keep that
  48. echo file in the same directory as this script, if you plan on running this in the
  49. echo future to update your server.
  50. echo:
  51. pause
  52.  
  53. cls
  54.  
  55. color B
  56. echo First, set the install location. Example - typing C:\My GMod Server\ will
  57. echo create a folder named 'My GMod Server' in the root path of drive C:
  58. echo YOU MUST INCLUDE A BACKSLASH AT THE END LIKE IN THE EXAMPLE
  59. echo:
  60.  
  61. ::Prompt the user for their preferred install directory, and save it to a file called cfg.txt in the same directory as this script
  62. SET /p downloadDir=Install Directory: %"="%
  63. echo !downloadDir! >cfg.txt
  64.  
  65. cls
  66.  
  67. ::If the download directory does not exist, create it
  68. if not exist !downloadDir! (
  69.     echo:
  70.     echo It appears that directory doesn't exist, creating it...
  71.     mkdir !downloadDir!
  72. )
  73.  
  74. ::Set the script to run from the server's download directory to make things easier
  75. cd !downloadDir!
  76.  
  77. ::Use Windows PowerShell command to download steamcmd.zip from Valve's servers
  78. echo:
  79. echo steamcmd.exe not found, downloading...
  80. powershell -Command "(New-Object Net.WebClient).DownloadFile('https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip', 'steamcmd.zip')"
  81.  
  82. ::Because we can't run vbscript commands directly in Batch, first we write each line of code to a temporary file called zip.vbs
  83. ::This will be used to extract the zip file
  84. echo Extracting...
  85.  
  86.     echo ZipFile="steamcmd.zip" > zip.vbs
  87.     echo ExtractTo="./" >> zip.vbs
  88.     echo Set fso = CreateObject("Scripting.FileSystemObject") >> zip.vbs
  89.     echo sourceFile = fso.GetAbsolutePathName(ZipFile) >> zip.vbs
  90.     echo destFolder = fso.GetAbsolutePathName(ExtractTo) >> zip.vbs
  91.     echo Set objShell = CreateObject("Shell.Application") >> zip.vbs
  92.     echo Set FilesInZip=objShell.NameSpace(sourceFile).Items() >> zip.vbs
  93.     echo objShell.NameSpace(destFolder).copyHere FilesInZip, 16 >> zip.vbs
  94.     echo Set fso = Nothing >> zip.vbs
  95.     echo Set objShell = Nothing >> zip.vbs
  96.     echo Set FilesInZip = Nothing >> zip.vbs
  97.  
  98. ::Run the script we just wrote
  99. CScript zip.vbs
  100.  
  101. ::Delete the temporary script file and the zip to keep things clean
  102. echo Finished extracting, cleaning up...
  103. del steamcmd.zip
  104. del zip.vbs
  105.  
  106. ::Run steamcmd.exe and download/update Garry's Mod Dedicated Server, then quit when it's finished
  107. steamcmd.exe +login anonymous +app_update 4020 validate +quit
  108.  
  109. echo Garry's Mod Dedicated Server successfully downloaded and validated.
  110.  
  111. ::Set the script to run from the directory it's currently in, as it was when it was first started
  112. cd !%~dp0!
  113.  
  114. ::Prompt the user for some general server settings, then write them line-by-line to the cfg file
  115. echo:
  116. echo Let's set up some things like...
  117. echo:
  118. echo The name of your server?
  119. SET /p serverName=Server Name: %"="%
  120. echo !serverName! >>cfg.txt
  121. echo:
  122. echo The default map?
  123. SET /p defaultMap=Default Map: %"="%
  124. echo !defaultMap! >>cfg.txt
  125. echo:
  126. echo Max amount of players?
  127. SET /p maxPlayers=Max Players: %"="%
  128. echo !maxPlayers! >>cfg.txt
  129. echo:
  130. echo Remote console password?
  131. SET /p rconPassword=RCON Password: %"="%
  132. echo !rconPassword! >>cfg.txt
  133. echo:
  134. echo Workshop collection ID? If you aren't sure, leave this blank
  135. SET /p collectionID=Workshop Collection ID: %"="%
  136. echo !collectionID! >>cfg.txt
  137. echo:
  138. echo Workshop auth key? If you don't have a collection for your server,
  139. echo leave this blank
  140. SET /p authKey=Workshop Authentication Key: %"="%
  141. echo !authKey! >>cfg.txt
  142. echo:
  143.  
  144. ::If the server's cfg folder doesn't exist for some reason, create it
  145. if not exist !downloadDir!\steamapps\common\GarrysModDS\garrysmod\cfg\ (
  146.     mkdir !downloadDir!\steamapps\common\GarrysModDS\garrysmod\cfg\
  147. )
  148.  
  149. ::Write the server name to the server's server.cfg file, to set the name that appears in Garry's Mod
  150. echo hostname "!servername!" >!downloadDir!\steamapps\common\GarrysModDS\garrysmod\cfg\server.cfg
  151.  
  152. cls
  153.  
  154. goto writeGuardian
  155.  
  156. ::If you clicked "update server" in the main menu, you're sent to this part of the code
  157. :updateServer
  158.  
  159. cls
  160.  
  161. ::If cfg.txt is detected, get the settings from there. Otherwise, prompt user to input their info again
  162. if exist cfg.txt (
  163.     SET /p downloadDir=<cfg.txt
  164.     echo Directory automatically detected as !downloadDir!
  165. ) else (
  166.     echo cfg.txt not found, please enter the directory the server installed to
  167.     echo YOU MUST INCLUDE A BACKSLASH AT THE END ^( \ ^)
  168.     echo:
  169.     SET /p downloadDir=Install Directory: %"="%
  170. )
  171.  
  172. ::If steamcmd.exe exists, continue updating. Otherwise, you probably fucked up.
  173. if exist !downloadDir!\steamcmd.exe (
  174.     goto steamCmdExists
  175. ) else (
  176.     echo:
  177.     echo steamcmd.exe not found. Are you the directory is correct?
  178.     echo Press any key to return to the main menu
  179.     echo:
  180.     pause
  181.     goto mainMenu
  182. )
  183. :steamCmdExists
  184.  
  185. cls
  186.  
  187. ::Update the server with SteamCMD
  188. echo Updating server...
  189. echo:
  190. steamcmd.exe +login anonymous +app_update 4020 validate +quit
  191. echo:
  192. echo Server successfully updated and validated
  193. echo Press any key to return to the main menu
  194. echo:
  195. pause
  196. goto mainMenu
  197.  
  198. ::If the user wants to generate a new SRCDS Guardian, they get sent to this part of the code
  199. :generateGuardian
  200.  
  201. cls
  202.  
  203. ::If cfg.txt exists, get settings from there. Otherwise, prompt user for new settings.
  204. if exist cfg.txt (
  205.     echo Automatically detected the following server settings:
  206.     echo:
  207.     type cfg.txt
  208.     echo:
  209.     echo Are these settings correct or would you like to change them?
  210.     echo Y for correct, N to change
  211.     :invalidAnswer2
  212.     set /P c=Answer:
  213.     if /I "!c!" EQU "Y" goto settingsCorrect
  214.     if /I "!c!" EQU "N" goto settingsNotCorrect
  215.     goto invalidAnswer2
  216. ) else (
  217.     goto cfgNotFound
  218. )
  219.  
  220. :cfgNotFound
  221. echo cfg.txt not detected. Press any key to re-enter your settings
  222. pause
  223. :settingsNotCorrect
  224.  
  225. cls
  226.  
  227. ::Prompt user for settings and stuff
  228. del cfg.txt
  229. echo Please enter the following settings:
  230. echo:
  231. echo Server install directory? END MUST HAVE A BACKSLASH ^( \ ^)
  232. SET /p downloadDir=Install Directory: %"="%
  233. echo !downloadDir! >cfg.txt
  234. echo:
  235. echo The name of your server?
  236. SET /p serverName=Server Name: %"="%
  237. echo !serverName! >>cfg.txt
  238. echo:
  239. echo The default map?
  240. SET /p defaultMap=Default Map: %"="%
  241. echo !defaultMap! >>cfg.txt
  242. echo:
  243. echo Max amount of players?
  244. SET /p maxPlayers=Max Players: %"="%
  245. echo !maxPlayers! >>cfg.txt
  246. echo:
  247. echo Remote console password?
  248. SET /p rconPassword=RCON Password: %"="%
  249. echo !rconPassword! >>cfg.txt
  250. echo:
  251. echo Workshop collection ID? If you aren't sure, leave this blank
  252. SET /p collectionID=Workshop Collection ID: %"="%
  253. echo !collectionID! >>cfg.txt
  254. echo:
  255. echo Workshop auth key? If you don't have a collection for your server,
  256. echo leave this blank
  257. SET /p authKey=Workshop Authentication Key: %"="%
  258. echo !authKey! >>cfg.txt
  259.  
  260. ::If the settings are correct, continue with generating the startup script
  261. :settingsCorrect
  262. :writeGuardian
  263. cls
  264. echo Press any key to generate the server launcher/watch-dog script
  265. echo which will start your server, and restart it if it ever crashes
  266. pause
  267.  
  268. echo Creating instance of SRCDS Guardian...
  269. echo:
  270. if not exist "Launch GMod Server - SRCDS Guardian 3.bat" (
  271.     echo ::File generated by Cheezus' Server Installer:: > "Launch Server - SRCDS Guardian 3.bat"
  272.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  273.     echo ::  SRCDS Guardian 3.0   :: >> "Launch Server - SRCDS Guardian 3.bat"
  274.     echo ::         Mooga         :: >> "Launch Server - SRCDS Guardian 3.bat"
  275.     echo ::       SRCDS.com       :: >> "Launch Server - SRCDS Guardian 3.bat"
  276.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  277.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  278.     echo ::=========================================================:: >> "Launch Server - SRCDS Guardian 3.bat"
  279.     echo :: Thanks To Black-Sky ^& Drocona for making SRCDS Guardian :: >> "Launch Server - SRCDS Guardian 3.bat"
  280.     echo :: This script is open source.  Feel free to edit at will. :: >> "Launch Server - SRCDS Guardian 3.bat"
  281.     echo ::                                                         :: >> "Launch Server - SRCDS Guardian 3.bat"
  282.     echo :: This script was writen for the use of the srcds.com     :: >> "Launch Server - SRCDS Guardian 3.bat"
  283.     echo :: online community.  If you are interested in running a   :: >> "Launch Server - SRCDS Guardian 3.bat"
  284.     echo :: Source Dedicated Server or need help, drop by our       :: >> "Launch Server - SRCDS Guardian 3.bat"
  285.     echo :: forums at... http://forums.srcds.com                    :: >> "Launch Server - SRCDS Guardian 3.bat"
  286.     echo ::=========================================================:: >> "Launch Server - SRCDS Guardian 3.bat"
  287.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  288.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  289.     echo ::  SET YOUR VARIABLES!  : class="re0">: >> "Launch Server - SRCDS Guardian 3.bat"
  290.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  291.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  292.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  293.     echo ::  Window and Log name  :: >> "Launch Server - SRCDS Guardian 3.bat"
  294.     echo ::  Replace "My Server"  :: >> "Launch Server - SRCDS Guardian 3.bat"
  295.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  296.     echo set servername=!serverName! >> "Launch Server - SRCDS Guardian 3.bat"
  297.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  298.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  299.     echo ::   Your start command  :: >> "Launch Server - SRCDS Guardian 3.bat"
  300.     echo ::    Replace after =    :: >> "Launch Server - SRCDS Guardian 3.bat"
  301.     echo ::=======================::
  302.     echo set runcmd=!downloadDir!steamapps\common\GarrysModDS\srcds.exe -console -game garrysmod +map !defaultMap! +maxplayers !maxPlayers! +rcon_password !rconPassword! +host_workshop_collection !collectionID! -authkey !authKey! >> "Launch Server - SRCDS Guardian 3.bat"
  303.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  304.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  305.     echo ::   End of variables    :: >> "Launch Server - SRCDS Guardian 3.bat"
  306.     echo ::=======================:: >> "Launch Server - SRCDS Guardian 3.bat"
  307.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  308.     echo :: This will keep the window clean and easy to read >> "Launch Server - SRCDS Guardian 3.bat"
  309.     echo @echo off >> "Launch Server - SRCDS Guardian 3.bat"
  310.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  311.     echo :: Sets the title of the window >> "Launch Server - SRCDS Guardian 3.bat"
  312.     echo title SRCDS Guardian 3.0   %%servername%% >> "Launch Server - SRCDS Guardian 3.bat"
  313.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  314.     echo :: Clears the window incase there is anything there >> "Launch Server - SRCDS Guardian 3.bat"
  315.     echo cls >> "Launch Server - SRCDS Guardian 3.bat"
  316.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  317.     echo :: Prints to the window what we are doing >> "Launch Server - SRCDS Guardian 3.bat"
  318.     echo echo SRCDS Guardian 3.0 has been started! >> "Launch Server - SRCDS Guardian 3.bat"
  319.     echo echo. >> "Launch Server - SRCDS Guardian 3.bat"
  320.     echo echo ************************************************************************* >> "Launch Server - SRCDS Guardian 3.bat"
  321.     echo echo To close the server, close this window and type exit in the server window >> "Launch Server - SRCDS Guardian 3.bat"
  322.     echo echo ************************************************************************* >> "Launch Server - SRCDS Guardian 3.bat"
  323.     echo echo. >> "Launch Server - SRCDS Guardian 3.bat"
  324.     echo echo. >> "Launch Server - SRCDS Guardian 3.bat"
  325.     echo echo %%servername%% is now starting... >> "Launch Server - SRCDS Guardian 3.bat"
  326.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  327.     echo :: This is a return point in case the server crashes or is closed >> "Launch Server - SRCDS Guardian 3.bat"
  328.     echo :restart >> "Launch Server - SRCDS Guardian 3.bat"
  329.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  330.     echo echo. >> "Launch Server - SRCDS Guardian 3.bat"
  331.     echo echo ^(%%date%%^)^(%%time%%^) %%servername%% is now ONLINE >> "Launch Server - SRCDS Guardian 3.bat"
  332.     echo echo Watching %%servername%% for crashes... >> "Launch Server - SRCDS Guardian 3.bat"
  333.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  334.     echo ::Start the actual server >> "Launch Server - SRCDS Guardian 3.bat"
  335.     echo start /wait %%runcmd%% >> "Launch Server - SRCDS Guardian 3.bat"
  336.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  337.     echo echo. >> "Launch Server - SRCDS Guardian 3.bat"
  338.     echo echo ^(%%date%%^)^(%%time%%^) Crash or Close detected! >> "Launch Server - SRCDS Guardian 3.bat"
  339.     echo echo %%servername%% is now restarting... >> "Launch Server - SRCDS Guardian 3.bat"
  340.     echo: >> "Launch Server - SRCDS Guardian 3.bat"
  341.     echo ::Server crashed or closed, so we point it to the return point to start the server again >> "Launch Server - SRCDS Guardian 3.bat"
  342.     echo goto restart >> "Launch Server - SRCDS Guardian 3.bat"
  343. )
  344. echo:
  345. echo Finished^^! Press any key to return to the main menu
  346. echo:
  347. pause
  348. goto mainMenu
Add Comment
Please, Sign In to add comment