Optimize Code

This commit is contained in:
nyaoouo 2025-06-09 21:29:26 +08:00
parent 4eb56e781b
commit 35969ecf47
2 changed files with 204 additions and 124 deletions

View File

@ -2,32 +2,38 @@
SETLOCAL EnableDelayedExpansion SETLOCAL EnableDelayedExpansion
goto :main goto :main
:parseArgs :_setArg
@REM Parse command line arguments by `call :parseArgs %*` set "_ARG_KEY=%~1"
@REM use !ARG[%n%]! to access arguments set "_ARG_VALUE=%~2"
set "_ARG_MAX=0" if "!_ARG_VALUE:~0,1!"=="\"" if "!_ARG_VALUE:~-1!"=="\"" (
for %%A in (%*) do ( set "_ARG_VALUE=!_ARG_VALUE:~1,-1!"
set "_ARG[!_ARG_MAX!]=%%A"
set /a _ARG_MAX+=1
) )
set "ARG_MAX=0" set "ARG[!_ARG_KEY!]=!_ARG_VALUE!"
set "_ARG_PTR=0" exit /b 0
:while :parseArgs
if !_ARG_PTR! geq !_ARG_MAX! goto :endwhile set "ARG_MAX=0"
set "_ARG=!_ARG[%_ARG_PTR%]!" set "_LastKey="
for %%A in (%*) do (
set "_ARG=%%A"
if "!_ARG:~0,1!"=="-" ( if "!_ARG:~0,1!"=="-" (
set "_ARG_KEY=!_ARG:~1!" if defined _LastKey call :_setArg !_LastKey! 1
set /a _ARG_PTR+=1 set "_LastKey=!_ARG:~1!"
call set "_ARG_VALUE=%%_ARG[!_ARG_PTR!]%%"
set "ARG[!_ARG_KEY!]=!_ARG_VALUE!"
) else ( ) else (
set "ARG[!ARG_MAX!]=!_ARG!" if defined _LastKey (
set /a ARG_MAX+=1 call :_setArg !_LastKey! !_ARG!
set "_LastKey="
) else (
call :_setArg !ARG_MAX! !_ARG!
set /a ARG_MAX+=1
)
) )
set /a _ARG_PTR+=1 )
goto :while if defined _LastKey call :_setArg !_LastKey! 1
:endwhile exit /b 0
:resolve_path
set "%~2=%~f1"
exit /b 0 exit /b 0
:raise :raise
@ -174,6 +180,9 @@ if not defined ExternalDir set "ExternalDir=%~dp0\external"
if not defined BackupDir set "BackupDir=%~dp0\backup" if not defined BackupDir set "BackupDir=%~dp0\backup"
if not defined MongodbUri set "MongodbUri=mongodb://127.0.0.1:27017/openWF" if not defined MongodbUri set "MongodbUri=mongodb://127.0.0.1:27017/openWF"
call :resolve_path "%ExternalDir%" "ExternalDir"
call :resolve_path "%BackupDir%" "BackupDir"
set "LOG_FILE=%BackupDir%\.backup_log.txt" set "LOG_FILE=%BackupDir%\.backup_log.txt"
set "RESTORE_ARCHIVE=%ARG[0]%" set "RESTORE_ARCHIVE=%ARG[0]%"

View File

@ -2,34 +2,41 @@
SETLOCAL EnableDelayedExpansion SETLOCAL EnableDelayedExpansion
goto :main goto :main
:parseArgs :_setArg
@REM Parse command line arguments by `call :parseArgs %*` set "_ARG_KEY=%~1"
@REM use !ARG[%n%]! to access arguments set "_ARG_VALUE=%~2"
set "_ARG_MAX=0" if "!_ARG_VALUE:~0,1!"=="\"" if "!_ARG_VALUE:~-1!"=="\"" (
for %%A in (%*) do ( set "_ARG_VALUE=!_ARG_VALUE:~1,-1!"
set "_ARG[!_ARG_MAX!]=%%A"
set /a _ARG_MAX+=1
) )
set "ARG_MAX=0" set "ARG[!_ARG_KEY!]=!_ARG_VALUE!"
set "_ARG_PTR=0"
:while
if !_ARG_PTR! geq !_ARG_MAX! goto :endwhile
set "_ARG=!_ARG[%_ARG_PTR%]!"
if "!_ARG:~0,1!"=="-" (
set "_ARG_KEY=!_ARG:~1!"
set /a _ARG_PTR+=1
call set "_ARG_VALUE=%%_ARG[!_ARG_PTR!]%%"
set "ARG[!_ARG_KEY!]=!_ARG_VALUE!"
) else (
set "ARG[!ARG_MAX!]=!_ARG!"
set /a ARG_MAX+=1
)
set /a _ARG_PTR+=1
goto :while
:endwhile
exit /b 0 exit /b 0
:parseArgs
set "ARG_MAX=0"
set "_LastKey="
for %%A in (%*) do (
set "_ARG=%%A"
if "!_ARG:~0,1!"=="-" (
if defined _LastKey call :_setArg !_LastKey! 1
set "_LastKey=!_ARG:~1!"
) else (
if defined _LastKey (
call :_setArg !_LastKey! !_ARG!
set "_LastKey="
) else (
call :_setArg !ARG_MAX! !_ARG!
set /a ARG_MAX+=1
)
)
)
if defined _LastKey call :_setArg !_LastKey! 1
exit /b 0
:resolve_path
set "%~2=%~f1"
exit /b 0
:raise :raise
set gErrorLevel=%~2 set gErrorLevel=%~2
echo %~1 echo %~1
@ -80,59 +87,89 @@ exit /b 0
:ensure-mongodb :ensure-mongodb
SETLOCAL EnableDelayedExpansion SETLOCAL EnableDelayedExpansion
set "mongodbArchive=%ExternalDir%\.tmp\mongodb.zip" set "archivePath=%ExternalDir%\.tmp\mongodb.zip"
set "mongodbDir=%ExternalDir%\mongodb\bin" set "targetDir=%ExternalDir%\mongodb"
set "expectedDir=%ExternalDir%\mongodb-win32-x86_64-windows-8.0.10" set "expectedDir=%ExternalDir%\mongodb-win32-x86_64-windows-8.0.10"
set "dlLink=https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-8.0.10.zip" set "dlLink=https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-8.0.10.zip"
set "testFile=%targetDir%\bin\mongod.exe"
if not exist "%mongodbDir%" ( if not exist "%targetDir%" (
echo MongoDB not found... echo MongoDB not found...
if not exist "%mongodbArchive%" ( if not exist "%archivePath%" (
echo Downloading MongoDB... echo Downloading MongoDB...
call :download "%dlLink%" "%mongodbArchive%" call :download "%dlLink%" "%archivePath%"
) )
echo Unzipping MongoDB... echo Unzipping MongoDB...
call :unzip "%mongodbArchive%" "%ExternalDir%" call :unzip "%archivePath%" "%ExternalDir%"
if exist "%expectedDir%" ( if exist "%expectedDir%" (
move /Y "%expectedDir%" "%ExternalDir%\mongodb" move /Y "%expectedDir%" "%targetDir%"
) else ( ) else (
call :raise "Expected directory %expectedDir% does not exist after unzipping." call :raise "Expected directory %expectedDir% does not exist after unzipping."
) )
) )
SET "PATH=%mongodbDir%;%PATH%" if not exist "%testFile%" (
where mongod >nul 2>&1 call :raise "MongoDB executable not found, expected at %testFile%."
call :assert "MongoDB executable not found in PATH after setup." )
echo MongoDB setup complete. echo MongoDB setup complete.
ENDLOCAL ENDLOCAL
exit /b 0 exit /b 0
:ensure-nodejs :ensure-nodejs
SETLOCAL EnableDelayedExpansion SETLOCAL EnableDelayedExpansion
set "nodejsArchive=%ExternalDir%\.tmp\nodejs.zip" set "archivePath=%ExternalDir%\.tmp\nodejs.zip"
set "nodejsDir=%ExternalDir%\nodejs" set "targetDir=%ExternalDir%\nodejs"
set "expectedDir=%ExternalDir%\node-v22.16.0-win-x64" set "expectedDir=%ExternalDir%\node-v22.16.0-win-x64"
set "dlLink=https://nodejs.org/dist/v22.16.0/node-v22.16.0-win-x64.zip" set "dlLink=https://nodejs.org/dist/v22.16.0/node-v22.16.0-win-x64.zip"
if not exist "%nodejsDir%" ( set "testFile=%targetDir%\npm.cmd"
if not exist "%targetDir%" (
echo Node.js not found... echo Node.js not found...
if not exist "%nodejsArchive%" ( if not exist "%archivePath%" (
echo Downloading Node.js... echo Downloading Node.js...
call :download "%dlLink%" "%nodejsArchive%" call :download "%dlLink%" "%archivePath%"
) )
echo Unzipping Node.js... echo Unzipping Node.js...
call :unzip "%nodejsArchive%" "%ExternalDir%" call :unzip "%archivePath%" "%ExternalDir%"
if exist "%expectedDir%" ( if exist "%expectedDir%" (
move /Y "%expectedDir%" "%ExternalDir%\nodejs" move /Y "%expectedDir%" "%targetDir%"
) else ( ) else (
call :raise "Expected directory %expectedDir% does not exist after unzipping." call :raise "Expected directory %expectedDir% does not exist after unzipping."
) )
) )
SET "PATH=%nodejsDir%;%PATH%" if not exist "%testFile%" (
where npm >nul 2>&1 call :raise "npm executable not found, expected at %testFile%."
call :assert "Node.js executable not found in PATH after setup." )
echo Node.js setup complete. echo Node.js setup complete.
ENDLOCAL ENDLOCAL
exit /b 0 exit /b 0
:ensure-windows-terminal
SETLOCAL EnableDelayedExpansion
set "archivePath=%ExternalDir%\.tmp\WindowsTerminal.zip"
set "targetDir=%ExternalDir%\WindowsTerminal"
set "expectedDir=%ExternalDir%\terminal-1.22.11141.0"
set "dlLink=https://github.com/microsoft/terminal/releases/download/v1.22.11141.0/Microsoft.WindowsTerminal_1.22.11141.0_x64.zip"
set "testFile=%targetDir%\wt.exe"
if not exist "%targetDir%" (
echo Windows Terminal not found...
if not exist "%archivePath%" (
echo Downloading Windows Terminal...
call :download "%dlLink%" "%archivePath%"
)
echo Unzipping Windows Terminal...
call :unzip "%archivePath%" "%ExternalDir%"
if exist "%expectedDir%" (
move /Y "%expectedDir%" "%targetDir%"
) else (
call :raise "Expected directory %expectedDir% does not exist after unzipping."
)
)
if not exist "%testFile%" (
call :raise "Windows Terminal executable not found, expected at %testFile%."
)
ENDLOCAL
exit /b 0
:ensure-git :ensure-git
SETLOCAL EnableDelayedExpansion SETLOCAL EnableDelayedExpansion
@ -253,10 +290,11 @@ if exist "%noIrcFlag%" (
ENDLOCAL ENDLOCAL
exit /b 0 exit /b 0
:main :main
set gErrorLevel=0 set gErrorLevel=0
call :parseArgs %* call :parseArgs %*
echo Usage: %0 [options] echo Usage: %0 [options] [Positional Arguments]
echo Options: echo Options:
echo -externalDir [path] Set the external directory (default: current script directory\external) echo -externalDir [path] Set the external directory (default: current script directory\external)
echo -serverDir [path] Set the server directory (default: current script directory\SpaceNinjaServer) echo -serverDir [path] Set the server directory (default: current script directory\SpaceNinjaServer)
@ -266,79 +304,112 @@ echo -ignorePull Ignore pulling updates for the server repository
echo -irc-options [args] Options to pass to the IRC server executable echo -irc-options [args] Options to pass to the IRC server executable
echo -db-options [args] Options to pass to the MongoDB server executable echo -db-options [args] Options to pass to the MongoDB server executable
echo -server-options [args] Options to pass to the main server echo -server-options [args] Options to pass to the main server
echo Positional Arguments:
echo database launch the database server
echo irc launch the IRC server
echo server launch the main server
echo note: if no positional arguments are provided, all servers will be launched
echo ============================================================ echo ============================================================
if defined ARG[externalDir] ( if defined ARG[externalDir] set "ExternalDir=%ARG[externalDir]%"
set "ExternalDir=%ARG[externalDir]%" if defined ARG[serverDir] set "ServerDir=%ARG[serverDir]%"
) else ( if defined ARG[serverRepo] set "ServerRepo=%ARG[serverRepo]%"
set "ExternalDir=%~dp0\external" if defined ARG[databasePath] set "DatabasePath=%ARG[databasePath]%"
)
if defined ARG[serverDir] ( if not defined ExternalDir set "ExternalDir=%~dp0\external"
set "ServerDir=%ARG[serverDir]%" if not defined ServerDir set "ServerDir=%~dp0\SpaceNinjaServer"
) else ( if not defined ServerRepo set "ServerRepo=https://openwf.io/SpaceNinjaServer.git"
set "ServerDir=%~dp0\SpaceNinjaServer" if not defined DatabasePath set "DatabasePath=%ExternalDir%\mongodb-data"
)
if defined ARG[serverRepo] ( call :resolve_path "%ExternalDir%" ExternalDir
set "ServerRepo=%ARG[serverRepo]%" call :resolve_path "%ServerDir%" ServerDir
) else ( call :resolve_path "%DatabasePath%" DatabasePath
set "ServerRepo=https://openwf.io/SpaceNinjaServer.git"
)
if defined ARG[databasePath] (
set "DatabasePath=%ARG[databasePath]%"
) else (
set "DatabasePath=%ExternalDir%\mongodb-data"
)
echo Using External Directory: %ExternalDir% echo Using External Directory: %ExternalDir%
echo Using Server Directory: %ServerDir% echo Using Server Directory: %ServerDir%
echo Using Server Repository: %ServerRepo% echo Using Server Repository: %ServerRepo%
echo Using MongoDB Data Directory: %DatabasePath% echo Using MongoDB Data Directory: %DatabasePath%
if not exist "%ExternalDir%" (
mkdir "%ExternalDir%"
)
call :ensure-mongodb
call :ensure-nodejs
call :ensure-git
call :ensure-server
call :ensure-extras
call :ensure-irc
pushd "%ServerDir%" if %ARG_MAX%==0 (
call npm install --omit=dev set "LaunchDatabase=1"
call :assert "Failed to install Node.js dependencies." set "LaunchIrc=%hasIrc%"
call npm run build set "LaunchServer=1"
call :assert "Failed to build the server."
popd
if exist "%ExternalDir%\IRC\warframe-irc-server.exe" (
set "hasIrc=1"
) else ( ) else (
set "hasIrc=0" set "LaunchDatabase=0"
set "LaunchIrc=0"
set "LaunchServer=0"
for /L %%i in (0,1,%ARG_MAX%) do (
if "!ARG[%%i]!"=="database" set "LaunchDatabase=1"
if "!ARG[%%i]!"=="irc" set "LaunchIrc=1"
if "!ARG[%%i]!"=="server" set "LaunchServer=1"
)
) )
if not exist "%DatabasePath%" ( echo Launching:
mkdir "%DatabasePath%" if %LaunchDatabase%==1 (
call :assert "Failed to create MongoDB data directory %DatabasePath%" echo - Database
) else (
echo - Database [skipped]
) )
if %LaunchIrc%==1 (
echo - IRC
) else (
echo - IRC [skipped]
)
if %LaunchServer%==1 (
echo - Server
) else (
echo - Server [skipped]
)
if not exist "%ExternalDir%" mkdir "%ExternalDir%"
if %LaunchDatabase%==1 call :ensure-mongodb
if %LaunchServer%==1 call :ensure-nodejs
call :ensure-windows-terminal
if %LaunchServer%==1 call :ensure-git
if %LaunchServer%==1 call :ensure-server
if %LaunchServer%==1 call :ensure-extras
if %LaunchIrc%==1 call :ensure-irc
if %LaunchServer%==1 (
pushd "%ServerDir%"
call npm install --omit=dev
call :assert "Failed to install Node.js dependencies."
call npm run build
call :assert "Failed to build the server."
popd
)
if not exist "%ExternalDir%\IRC\warframe-irc-server.exe" (
set LaunchIrc=0
)
set WT_CMD_SERVER=nt --title "Main Server" -d "%ServerDir%" "%ExternalDir%\nodejs\npm.cmd" run start %ARG[server-options]%
set WT_CMD_DATABASE=nt --title "Database" -d "%ExternalDir%" "%ExternalDir%\mongodb\bin\mongod" --dbpath "%DatabasePath%" %ARG[db-options]%
set WT_CMD_IRC=nt --title "IRC" -d "%ExternalDir%\IRC" "%ExternalDir%\IRC\warframe-irc-server.exe" %ARG[irc-options]%
@REM Maybe use docker-compose instead? @REM Maybe use docker-compose instead?
if %hasIrc%==1 (
echo Extra Irc options: %ARG[irc-options]% set "WT_CMD="
if defined ARG[irc-options] set "IRC_OPTIONS=%ARG[irc-options]:"=%" if %LaunchServer%==1 set "WT_CMD=%WT_CMD% ; %WT_CMD_SERVER%"
start /D "%ExternalDir%\IRC" "IRC Server" "%ExternalDir%\IRC\warframe-irc-server.exe" %IRC_OPTIONS% if %LaunchDatabase%==1 (
if not exist "%DatabasePath%" (
mkdir "%DatabasePath%"
call :assert "Failed to create MongoDB data directory %DatabasePath%"
)
set "WT_CMD= ; %WT_CMD_DATABASE%"
)
if %LaunchIrc%==1 set "WT_CMD=%WT_CMD% ; %WT_CMD_IRC%"
if "!WT_CMD:~0,2!"==" ;" set "WT_CMD=!WT_CMD:~2!"
if "!WT_CMD!"=="" (
echo No servers to launch.
) else (
start "OpenWf Server" %ExternalDir%\WindowsTerminal\wt.exe %WT_CMD%
call :assert "Failed to start the server in Windows Terminal."
exit /b 0
) )
echo Starting MongoDB with options: %ARG[db-options]%
if defined ARG[db-options] set "DB_OPTIONS=%ARG[db-options]:"=%"
start /D "%ExternalDir%" "Database" "%ExternalDir%\mongodb\bin\mongod" --dbpath "%DatabasePath%" %DB_OPTIONS%
echo Starting Main Server with options: %ARG[server-options]%
if defined ARG[server-options] set "SERVER_OPTIONS=%ARG[server-options]:"=%"
@REM start /D "%ServerDir%" "Main Server" "%ExternalDir%\nodejs\npm" run start %SERVER_OPTIONS%
@REM exit /b 0
pushd "%ServerDir%"
call npm run start %SERVER_OPTIONS%
popd
exit /b %errorlevel%
:end :end
ENDLOCAL ENDLOCAL