start.cmd 5.53 KB
Newer Older
Jaida Wu's avatar
Jaida Wu committed
1
2
3
4
5
6
@rem
@rem Copyright (C) 2002-2022 MlgmXyysd All Rights Reserved.
@rem

@if "%DEBUG%" == "" echo off
pushd %~dp0
Jaida Wu's avatar
Jaida Wu committed
7
set CUR_PATH=%~dp0
Jaida Wu's avatar
Jaida Wu committed
8
title Grasscutter
Jaida Wu's avatar
Jaida Wu committed
9
10
11
call :LOG [INFO] Welcome to Grasscutter
call :LOG [INFO] To proper exit this console, use [Ctrl + C] and enter N not Y.
call :LOG [INFO]
Jaida Wu's avatar
Jaida Wu committed
12
13
call :LOG [INFO] Initializing...

Jaida Wu's avatar
Jaida Wu committed
14
15
set CONFIG=start_config

Jaida Wu's avatar
Jaida Wu committed
16
17
set JAVA_PATH=DO_NOT_CHECK_PATH
set MITMDUMP_PATH=DO_NOT_CHECK_PATH
Jaida Wu's avatar
Jaida Wu committed
18
set MONGODB_PATH=DO_NOT_CHECK_PATH
Jaida Wu's avatar
Jaida Wu committed
19

Jaida Wu's avatar
Jaida Wu committed
20
set SERVER_JAR_PATH=%CUR_PATH%
Jaida Wu's avatar
Jaida Wu committed
21
set DATABASE_STORAGE_PATH=%CUR_PATH%resources\Database
Jaida Wu's avatar
Jaida Wu committed
22
23
24
25

set SERVER_JAR_NAME=grasscutter.jar
set PROXY_SCRIPT_NAME=proxy

Jaida Wu's avatar
Jaida Wu committed
26
27
28
29
if exist "%CUR_PATH%%CONFIG%.cmd" (
	call "%CUR_PATH%%CONFIG%.cmd" >nul 2>nul
)

Jaida Wu's avatar
Jaida Wu committed
30
if not "%JAVA_PATH%" == "DO_NOT_CHECK_PATH" (
Apexo01's avatar
Apexo01 committed
31
32
33
34
	if "%JAVA_PATH%" == "\bin\" (
		call :LOG [ERROR] JAVA_HOME not found, please setup your windows enviroment for installed java.
		goto :EXIT
	)
Jaida Wu's avatar
Jaida Wu committed
35
36
37
38
39
	if not exist "%JAVA_PATH%java.exe" (
		call :LOG [ERROR] Java not found.
		goto :EXIT
	)
) else set JAVA_PATH=
40
if not exist "%SERVER_PATH%%SERVER_JAR_NAME%" (
Jaida Wu's avatar
Jaida Wu committed
41
42
	call :LOG [ERROR] Server jar not found.
	goto :EXIT
Jaida Wu's avatar
Jaida Wu committed
43
)
Jaida Wu's avatar
Jaida Wu committed
44
45
46
47
48
49
50
51

@rem mitmproxy not found, server only
if not "%MITMDUMP_PATH%" == "DO_NOT_CHECK_PATH" (
	if not exist "%MITMDUMP_PATH%mitmdump.exe" (
		call :LOG [WARN] mitmdump not found, server only mode.
		goto :SERVER
	)
) else set MITMDUMP_PATH=
Jaida Wu's avatar
Jaida Wu committed
52
@rem proxy script not found, server only
Jaida Wu's avatar
Jaida Wu committed
53
54
if not exist "%PROXY_SCRIPT_NAME%.py" (
	if not exist "%PROXY_SCRIPT_NAME%.pyc" (
Jaida Wu's avatar
Jaida Wu committed
55
56
		call :LOG [WARN] Missing proxy script or compiled proxy script, server only mode.
		goto :SERVER
Jaida Wu's avatar
Jaida Wu committed
57
58
	) else set PROXY_SCRIPT_NAME=%PROXY_SCRIPT_NAME%.pyc
) else set PROXY_SCRIPT_NAME=%PROXY_SCRIPT_NAME%.py
Jaida Wu's avatar
Jaida Wu committed
59
60
61
62
63
64

:PROXY
@rem UAC Administrator privileges
>nul 2>&1 reg query "HKU\S-1-5-19" || (
	call :LOG [WARN] Currently running with non Administrator privileges, raising...
	echo set UAC = CreateObject^("Shell.Application"^) > "%temp%\UAC.vbs"
Jaida Wu's avatar
Jaida Wu committed
65
	echo UAC.ShellExecute "%~f0","%1","","runas",1 >> "%temp%\UAC.vbs"
Jaida Wu's avatar
Jaida Wu committed
66
67
68
69
70
	"%temp%\UAC.vbs"
	del /f /q "%temp%\UAC.vbs" >nul 2>nul
	exit /b
)

Jaida Wu's avatar
Jaida Wu committed
71
72
call :LOG [INFO] Starting proxy daemon...

Jaida Wu's avatar
Jaida Wu committed
73
set PROXY=true
Jaida Wu's avatar
Jaida Wu committed
74

Jaida Wu's avatar
Jaida Wu committed
75
76
77
78
79
@rem Store original proxy settings
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"
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"

@rem TODO: External proxy when ORIG_PROXY_ENABLE == 0x1
Jaida Wu's avatar
Jaida Wu committed
80
echo set ws = createobject("wscript.shell") > "%temp%\proxy.vbs"
81

82
if not "%MITMDUMP_PATH%" == "" (
Jaida Wu's avatar
Jaida Wu committed
83
	echo ws.currentdirectory = "%MITMDUMP_PATH%" >> "%temp%\proxy.vbs"
84
)
85
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"
Jaida Wu's avatar
Jaida Wu committed
86
87
"%temp%\proxy.vbs"
del /f /q "%temp%\proxy.vbs" >nul 2>nul
Jaida Wu's avatar
Jaida Wu committed
88

Jaida Wu's avatar
Jaida Wu committed
89
@rem CA certificate for HTTPS scheme
Jaida Wu's avatar
Jaida Wu committed
90
91
92
call :LOG [INFO] Waiting for CA certificate generation...
set CA_CERT_FILE="%USERPROFILE%\.mitmproxy\mitmproxy-ca-cert.cer"

Jaida Wu's avatar
Jaida Wu committed
93
set /a TIMEOUT_COUNT=0
Jaida Wu's avatar
Jaida Wu committed
94
95
96

:CERT_CA_CHECK
if not exist %CA_CERT_FILE% (
Jaida Wu's avatar
Jaida Wu committed
97
98
	timeout /t 1 >nul 2>nul
	set /a TIMEOUT_COUNT+=1
Jaida Wu's avatar
Jaida Wu committed
99
100
101
102
	goto CERT_CA_CHECK
)
:EXTRA_TIMEOUT
if %TIMEOUT_COUNT% LEQ 2 (
Jaida Wu's avatar
Jaida Wu committed
103
104
	timeout /t 1 >nul 2>nul
	set /a TIMEOUT_COUNT+=1
Jaida Wu's avatar
Jaida Wu committed
105
106
107
108
109
110
111
112
113
114
	goto EXTRA_TIMEOUT
)
call :LOG [INFO] Adding CA certificate to store...
certutil -addstore root %CA_CERT_FILE% >nul 2>nul

call :LOG [INFO] Setting up network proxy...
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f >nul 2>nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "127.0.0.1:8080" /f >nul 2>nul

:SERVER
Jaida Wu's avatar
Jaida Wu committed
115
116
117
118
119
120
121
122
123
124
125
126
if not "%MONGODB_PATH%" == "DO_NOT_CHECK_PATH" (
	if not exist "%MONGODB_PATH%mongod.exe" (
		call :LOG [WARN] MongoDB daemon not found, server only mode.
		goto :GAME
	)
) else set MONGODB_PATH=
call :LOG [INFO] Starting MongoDB daemon...
set DATABASE=true

mkdir "%DATABASE_STORAGE_PATH%" >nul 2>nul

echo set ws = createobject("wscript.shell") > "%temp%\db.vbs"
127
if not "%MONGODB_PATH%" == "" (
Jaida Wu's avatar
Jaida Wu committed
128
echo ws.currentdirectory = "%MONGODB_PATH%" >> "%temp%\db.vbs"
129
)
Jaida Wu's avatar
Jaida Wu committed
130
131
132
133
134
echo ws.run "cmd /c mongod.exe --dbpath "^&chr(34)^&"%DATABASE_STORAGE_PATH%"^&chr(34)^&"",0 >> "%temp%\db.vbs"
"%temp%\db.vbs"
del /f /q "%temp%\db.vbs" >nul 2>nul

:GAME
Jaida Wu's avatar
Jaida Wu committed
135
call :LOG [INFO] Starting server...
136
"%JAVA_PATH%java.exe" -jar "%SERVER_PATH%%SERVER_JAR_NAME%"
Jaida Wu's avatar
Jaida Wu committed
137
138
139
call :LOG [INFO] Server stopped

:EXIT
Jaida Wu's avatar
Jaida Wu committed
140
141
142
143
144
145
if "%DATABASE%" == "" (
	call :LOG [INFO] MongoDB daemon not started, no need to clean up.
) else (
	call :LOG [INFO] Shutting down MongoDB daemon...
	taskkill /t /f /im mongod.exe >nul 2>nul
)
Jaida Wu's avatar
Jaida Wu committed
146
if "%PROXY%" == "" (
Jaida Wu's avatar
Jaida Wu committed
147
	call :LOG [INFO] Proxy daemon not started, no need to clean up.
Jaida Wu's avatar
Jaida Wu committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
) else (
	call :LOG [INFO] Restoring network settings...

	reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d "%ORIG_PROXY_ENABLE%" /f >nul 2>nul
	reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /d "%ORIG_PROXY_SERVER%" /f >nul 2>nul

	call :LOG [INFO] Shutting down proxy daemon...
	taskkill /t /f /im mitmdump.exe >nul 2>nul

	call :LOG [INFO] Removing CA certificate...
	for /F "tokens=2" %%s in ('certutil -dump %CA_CERT_FILE% ^| findstr ^"^sha1^"') do (
		set SERIAL=%%s
	)

	certutil -delstore root %SERIAL% >nul 2>nul
)

call :LOG [INFO] See you again :)
goto :EOF

:LOG
WangYneos's avatar
WangYneos committed
169
echo [%time:~0,8%] %*