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
goto :main
:parseArgs
@REM Parse command line arguments by `call :parseArgs %*`
@REM use !ARG[%n%]! to access arguments
set "_ARG_MAX=0"
for %%A in (%*) do (
set "_ARG[!_ARG_MAX!]=%%A"
set /a _ARG_MAX+=1
:_setArg
set "_ARG_KEY=%~1"
set "_ARG_VALUE=%~2"
if "!_ARG_VALUE:~0,1!"=="\"" if "!_ARG_VALUE:~-1!"=="\"" (
set "_ARG_VALUE=!_ARG_VALUE:~1,-1!"
)
set "ARG_MAX=0"
set "_ARG_PTR=0"
set "ARG[!_ARG_KEY!]=!_ARG_VALUE!"
exit /b 0
:while
if !_ARG_PTR! geq !_ARG_MAX! goto :endwhile
set "_ARG=!_ARG[%_ARG_PTR%]!"
:parseArgs
set "ARG_MAX=0"
set "_LastKey="
for %%A in (%*) do (
set "_ARG=%%A"
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!"
if defined _LastKey call :_setArg !_LastKey! 1
set "_LastKey=!_ARG:~1!"
) else (
set "ARG[!ARG_MAX!]=!_ARG!"
set /a ARG_MAX+=1
if defined _LastKey (
call :_setArg !_LastKey! !_ARG!
set "_LastKey="
) else (
call :_setArg !ARG_MAX! !_ARG!
set /a ARG_MAX+=1
)
)
set /a _ARG_PTR+=1
goto :while
:endwhile
)
if defined _LastKey call :_setArg !_LastKey! 1
exit /b 0
:resolve_path
set "%~2=%~f1"
exit /b 0
:raise
@ -174,6 +180,9 @@ if not defined ExternalDir set "ExternalDir=%~dp0\external"
if not defined BackupDir set "BackupDir=%~dp0\backup"
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 "RESTORE_ARCHIVE=%ARG[0]%"

View File

@ -2,34 +2,41 @@
SETLOCAL EnableDelayedExpansion
goto :main
:parseArgs
@REM Parse command line arguments by `call :parseArgs %*`
@REM use !ARG[%n%]! to access arguments
set "_ARG_MAX=0"
for %%A in (%*) do (
set "_ARG[!_ARG_MAX!]=%%A"
set /a _ARG_MAX+=1
:_setArg
set "_ARG_KEY=%~1"
set "_ARG_VALUE=%~2"
if "!_ARG_VALUE:~0,1!"=="\"" if "!_ARG_VALUE:~-1!"=="\"" (
set "_ARG_VALUE=!_ARG_VALUE:~1,-1!"
)
set "ARG_MAX=0"
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
set "ARG[!_ARG_KEY!]=!_ARG_VALUE!"
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
set gErrorLevel=%~2
echo %~1
@ -80,59 +87,89 @@ exit /b 0
:ensure-mongodb
SETLOCAL EnableDelayedExpansion
set "mongodbArchive=%ExternalDir%\.tmp\mongodb.zip"
set "mongodbDir=%ExternalDir%\mongodb\bin"
set "archivePath=%ExternalDir%\.tmp\mongodb.zip"
set "targetDir=%ExternalDir%\mongodb"
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 "testFile=%targetDir%\bin\mongod.exe"
if not exist "%mongodbDir%" (
if not exist "%targetDir%" (
echo MongoDB not found...
if not exist "%mongodbArchive%" (
if not exist "%archivePath%" (
echo Downloading MongoDB...
call :download "%dlLink%" "%mongodbArchive%"
call :download "%dlLink%" "%archivePath%"
)
echo Unzipping MongoDB...
call :unzip "%mongodbArchive%" "%ExternalDir%"
call :unzip "%archivePath%" "%ExternalDir%"
if exist "%expectedDir%" (
move /Y "%expectedDir%" "%ExternalDir%\mongodb"
move /Y "%expectedDir%" "%targetDir%"
) else (
call :raise "Expected directory %expectedDir% does not exist after unzipping."
)
)
SET "PATH=%mongodbDir%;%PATH%"
where mongod >nul 2>&1
call :assert "MongoDB executable not found in PATH after setup."
if not exist "%testFile%" (
call :raise "MongoDB executable not found, expected at %testFile%."
)
echo MongoDB setup complete.
ENDLOCAL
exit /b 0
:ensure-nodejs
SETLOCAL EnableDelayedExpansion
set "nodejsArchive=%ExternalDir%\.tmp\nodejs.zip"
set "nodejsDir=%ExternalDir%\nodejs"
set "archivePath=%ExternalDir%\.tmp\nodejs.zip"
set "targetDir=%ExternalDir%\nodejs"
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"
if not exist "%nodejsDir%" (
set "testFile=%targetDir%\npm.cmd"
if not exist "%targetDir%" (
echo Node.js not found...
if not exist "%nodejsArchive%" (
if not exist "%archivePath%" (
echo Downloading Node.js...
call :download "%dlLink%" "%nodejsArchive%"
call :download "%dlLink%" "%archivePath%"
)
echo Unzipping Node.js...
call :unzip "%nodejsArchive%" "%ExternalDir%"
call :unzip "%archivePath%" "%ExternalDir%"
if exist "%expectedDir%" (
move /Y "%expectedDir%" "%ExternalDir%\nodejs"
move /Y "%expectedDir%" "%targetDir%"
) else (
call :raise "Expected directory %expectedDir% does not exist after unzipping."
)
)
SET "PATH=%nodejsDir%;%PATH%"
where npm >nul 2>&1
call :assert "Node.js executable not found in PATH after setup."
if not exist "%testFile%" (
call :raise "npm executable not found, expected at %testFile%."
)
echo Node.js setup complete.
ENDLOCAL
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
SETLOCAL EnableDelayedExpansion
@ -253,10 +290,11 @@ if exist "%noIrcFlag%" (
ENDLOCAL
exit /b 0
:main
set gErrorLevel=0
call :parseArgs %*
echo Usage: %0 [options]
echo Usage: %0 [options] [Positional Arguments]
echo Options:
echo -externalDir [path] Set the external directory (default: current script directory\external)
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 -db-options [args] Options to pass to the MongoDB server executable
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 ============================================================
if defined ARG[externalDir] (
set "ExternalDir=%ARG[externalDir]%"
) else (
set "ExternalDir=%~dp0\external"
)
if defined ARG[serverDir] (
set "ServerDir=%ARG[serverDir]%"
) else (
set "ServerDir=%~dp0\SpaceNinjaServer"
)
if defined ARG[serverRepo] (
set "ServerRepo=%ARG[serverRepo]%"
) else (
set "ServerRepo=https://openwf.io/SpaceNinjaServer.git"
)
if defined ARG[databasePath] (
set "DatabasePath=%ARG[databasePath]%"
) else (
set "DatabasePath=%ExternalDir%\mongodb-data"
)
if defined ARG[externalDir] set "ExternalDir=%ARG[externalDir]%"
if defined ARG[serverDir] set "ServerDir=%ARG[serverDir]%"
if defined ARG[serverRepo] set "ServerRepo=%ARG[serverRepo]%"
if defined ARG[databasePath] set "DatabasePath=%ARG[databasePath]%"
if not defined ExternalDir set "ExternalDir=%~dp0\external"
if not defined ServerDir set "ServerDir=%~dp0\SpaceNinjaServer"
if not defined ServerRepo set "ServerRepo=https://openwf.io/SpaceNinjaServer.git"
if not defined DatabasePath set "DatabasePath=%ExternalDir%\mongodb-data"
call :resolve_path "%ExternalDir%" ExternalDir
call :resolve_path "%ServerDir%" ServerDir
call :resolve_path "%DatabasePath%" DatabasePath
echo Using External Directory: %ExternalDir%
echo Using Server Directory: %ServerDir%
echo Using Server Repository: %ServerRepo%
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%"
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 exist "%ExternalDir%\IRC\warframe-irc-server.exe" (
set "hasIrc=1"
if %ARG_MAX%==0 (
set "LaunchDatabase=1"
set "LaunchIrc=%hasIrc%"
set "LaunchServer=1"
) 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%" (
mkdir "%DatabasePath%"
call :assert "Failed to create MongoDB data directory %DatabasePath%"
echo Launching:
if %LaunchDatabase%==1 (
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?
if %hasIrc%==1 (
echo Extra Irc options: %ARG[irc-options]%
if defined ARG[irc-options] set "IRC_OPTIONS=%ARG[irc-options]:"=%"
start /D "%ExternalDir%\IRC" "IRC Server" "%ExternalDir%\IRC\warframe-irc-server.exe" %IRC_OPTIONS%
set "WT_CMD="
if %LaunchServer%==1 set "WT_CMD=%WT_CMD% ; %WT_CMD_SERVER%"
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
ENDLOCAL