start.cmd 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. @rem
  2. @rem Copyright (C) 2002-2022 MlgmXyysd All Rights Reserved.
  3. @rem
  4. @if "%DEBUG%" == "" echo off
  5. pushd %~dp0
  6. set CUR_PATH=%~dp0
  7. title Grasscutter
  8. call :LOG [INFO] Welcome to Grasscutter
  9. call :LOG [INFO] To proper exit this console, use [Ctrl + C] and enter N not Y.
  10. call :LOG [INFO]
  11. call :LOG [INFO] Initializing...
  12. set CONFIG=start_config
  13. set JAVA_PATH=DO_NOT_CHECK_PATH
  14. set MITMDUMP_PATH=DO_NOT_CHECK_PATH
  15. set MONGODB_PATH=DO_NOT_CHECK_PATH
  16. set SERVER_JAR_PATH=%CUR_PATH%
  17. set DATABASE_STORAGE_PATH=%CUR_PATH%resources\Database
  18. set SERVER_JAR_NAME=grasscutter.jar
  19. set PROXY_SCRIPT_NAME=proxy
  20. if exist "%CUR_PATH%%CONFIG%.cmd" (
  21. call "%CUR_PATH%%CONFIG%.cmd" >nul 2>nul
  22. )
  23. if not "%JAVA_PATH%" == "DO_NOT_CHECK_PATH" (
  24. if "%JAVA_PATH%" == "\bin\" (
  25. call :LOG [ERROR] JAVA_HOME not found, please setup your windows environment for installed java.
  26. goto :EXIT
  27. )
  28. if not exist "%JAVA_PATH%java.exe" (
  29. call :LOG [ERROR] Java not found.
  30. goto :EXIT
  31. )
  32. ) else set JAVA_PATH=
  33. if not exist "%SERVER_PATH%%SERVER_JAR_NAME%" (
  34. call :LOG [ERROR] Server jar not found.
  35. goto :EXIT
  36. )
  37. @rem mitmproxy not found, server only
  38. if not "%MITMDUMP_PATH%" == "DO_NOT_CHECK_PATH" (
  39. if not exist "%MITMDUMP_PATH%mitmdump.exe" (
  40. call :LOG [WARN] mitmdump not found, server only mode.
  41. goto :SERVER
  42. )
  43. ) else set MITMDUMP_PATH=
  44. @rem proxy script not found, server only
  45. if not exist "%PROXY_SCRIPT_NAME%.py" (
  46. if not exist "%PROXY_SCRIPT_NAME%.pyc" (
  47. call :LOG [WARN] Missing proxy script or compiled proxy script, server only mode.
  48. goto :SERVER
  49. ) else set PROXY_SCRIPT_NAME=%PROXY_SCRIPT_NAME%.pyc
  50. ) else set PROXY_SCRIPT_NAME=%PROXY_SCRIPT_NAME%.py
  51. :PROXY
  52. @rem UAC Administrator privileges
  53. >nul 2>&1 reg query "HKU\S-1-5-19" || (
  54. call :LOG [WARN] Currently running with non Administrator privileges, raising...
  55. echo set UAC = CreateObject^("Shell.Application"^) > "%temp%\UAC.vbs"
  56. echo UAC.ShellExecute "%~f0","%1","","runas",1 >> "%temp%\UAC.vbs"
  57. "%temp%\UAC.vbs"
  58. del /f /q "%temp%\UAC.vbs" >nul 2>nul
  59. exit /b
  60. )
  61. call :LOG [INFO] Starting proxy daemon...
  62. set PROXY=true
  63. @rem Store original proxy settings
  64. for /f "tokens=2*" %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable 2^>nul') do set "ORIG_PROXY_ENABLE=%%b"
  65. for /f "tokens=2*" %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer 2^>nul') do set "ORIG_PROXY_SERVER=%%b"
  66. @rem TODO: External proxy when ORIG_PROXY_ENABLE == 0x1
  67. echo set ws = createobject("wscript.shell") > "%temp%\proxy.vbs"
  68. if not "%MITMDUMP_PATH%" == "" (
  69. echo ws.currentdirectory = "%MITMDUMP_PATH%" >> "%temp%\proxy.vbs"
  70. )
  71. echo ws.run "cmd /c mitmdump.exe -s "^&chr(34)^&"%CUR_PATH%%PROXY_SCRIPT_NAME%"^&chr(34)^&" -k --allow-hosts "^&chr(34)^&".*\.yuanshen\.com|.*\.mihoyo\.com|.*\.hoyoverse\.com"^&chr(34),0 >> "%temp%\proxy.vbs"
  72. "%temp%\proxy.vbs"
  73. del /f /q "%temp%\proxy.vbs" >nul 2>nul
  74. @rem CA certificate for HTTPS scheme
  75. call :LOG [INFO] Waiting for CA certificate generation...
  76. set CA_CERT_FILE="%USERPROFILE%\.mitmproxy\mitmproxy-ca-cert.cer"
  77. set /a TIMEOUT_COUNT=0
  78. :CERT_CA_CHECK
  79. if not exist %CA_CERT_FILE% (
  80. timeout /t 1 >nul 2>nul
  81. set /a TIMEOUT_COUNT+=1
  82. goto CERT_CA_CHECK
  83. )
  84. :EXTRA_TIMEOUT
  85. if %TIMEOUT_COUNT% LEQ 2 (
  86. timeout /t 1 >nul 2>nul
  87. set /a TIMEOUT_COUNT+=1
  88. goto EXTRA_TIMEOUT
  89. )
  90. call :LOG [INFO] Adding CA certificate to store...
  91. certutil -addstore root %CA_CERT_FILE% >nul 2>nul
  92. call :LOG [INFO] Setting up network proxy...
  93. reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f >nul 2>nul
  94. reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "127.0.0.1:8080" /f >nul 2>nul
  95. :SERVER
  96. if not "%MONGODB_PATH%" == "DO_NOT_CHECK_PATH" (
  97. if not exist "%MONGODB_PATH%mongod.exe" (
  98. call :LOG [WARN] MongoDB daemon not found, server only mode.
  99. goto :GAME
  100. )
  101. ) else set MONGODB_PATH=
  102. call :LOG [INFO] Starting MongoDB daemon...
  103. set DATABASE=true
  104. mkdir "%DATABASE_STORAGE_PATH%" >nul 2>nul
  105. echo set ws = createobject("wscript.shell") > "%temp%\db.vbs"
  106. if not "%MONGODB_PATH%" == "" (
  107. echo ws.currentdirectory = "%MONGODB_PATH%" >> "%temp%\db.vbs"
  108. )
  109. echo ws.run "cmd /c mongod.exe --dbpath "^&chr(34)^&"%DATABASE_STORAGE_PATH%"^&chr(34)^&"",0 >> "%temp%\db.vbs"
  110. "%temp%\db.vbs"
  111. del /f /q "%temp%\db.vbs" >nul 2>nul
  112. :GAME
  113. call :LOG [INFO] Starting server...
  114. "%JAVA_PATH%java.exe" -jar "%SERVER_PATH%%SERVER_JAR_NAME%"
  115. call :LOG [INFO] Server stopped
  116. :EXIT
  117. if "%DATABASE%" == "" (
  118. call :LOG [INFO] MongoDB daemon not started, no need to clean up.
  119. ) else (
  120. call :LOG [INFO] Shutting down MongoDB daemon...
  121. taskkill /t /f /im mongod.exe >nul 2>nul
  122. )
  123. if "%PROXY%" == "" (
  124. call :LOG [INFO] Proxy daemon not started, no need to clean up.
  125. ) else (
  126. call :LOG [INFO] Restoring network settings...
  127. reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d "%ORIG_PROXY_ENABLE%" /f >nul 2>nul
  128. reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "%ORIG_PROXY_SERVER%" /f >nul 2>nul
  129. call :LOG [INFO] Shutting down proxy daemon...
  130. taskkill /t /f /im mitmdump.exe >nul 2>nul
  131. call :LOG [INFO] Removing CA certificate...
  132. for /F "tokens=2" %%s in ('certutil -dump %CA_CERT_FILE% ^| findstr ^"^sha1^"') do (
  133. set SERIAL=%%s
  134. )
  135. certutil -delstore root %SERIAL% >nul 2>nul
  136. )
  137. call :LOG [INFO] See you again :)
  138. goto :EOF
  139. :LOG
  140. echo [%time:~0,8%] %*