diff --git a/.gitattributes b/.gitattributes index 02ce0cb..8b12f4e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -17,6 +17,7 @@ *.ps1 text eol=crlf *.cmd text eol=crlf bd2w text eol=lf +go/build/tasks.txt text eol=lf *.dll binary *.exe binary diff --git a/bd2w b/bd2w index 2c60a34..27633cc 100755 --- a/bd2w +++ b/bd2w @@ -2,12 +2,31 @@ set -eu root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P) +validate_task() { + for argument do + case "$argument" in + --plan) continue ;; + help|-h|--help|-[Hh][Ee][Ll][Pp]) cat "$root/go/build/help.txt"; exit 0 ;; + esac + while IFS= read -r task || [ -n "$task" ]; do + [ "$argument" != "$task" ] || return 0 + done < "$root/go/build/tasks.txt" + printf 'bd2w: unknown task "%s"; use --help for available commands\n' "$argument" >&2 + return 1 + done +} +if [ "$#" -eq 0 ]; then + cat "$root/go/build/help.txt" + exit 0 +fi +validate_task "$@" + tool_dir="$root/.build/tools" tool="$tool_dir/bd2w" tool_is_current() { [ -f "$tool" ] || return 1 - newer_sources=$(find "$root/go/build" "$root/go/internal/common" "$root/go/internal/client" "$root/go/go.mod" "$root/go/go.sum" -type f -newer "$tool" -print) || { + newer_sources=$(find "$root/go/build" "$root/go/internal" "$root/go/go.mod" "$root/go/go.sum" -type f -newer "$tool" -print) || { printf 'bd2w: cannot check build tool source timestamps.\n' >&2 exit 1 } diff --git a/bd2w.cmd b/bd2w.cmd index 9ba62a6..b1fba52 100644 --- a/bd2w.cmd +++ b/bd2w.cmd @@ -1,44 +1,67 @@ @echo off setlocal DisableDelayedExpansion set "BD2W_ROOT=%~dp0" +call :validate_task %* +if errorlevel 2 goto show_help +if errorlevel 1 exit /b 1 set "BD2W_TOOL_DIR=%BD2W_ROOT%.build\tools" -set "BD2W_TOOL=%BD2W_TOOL_DIR%\bd2w.exe" -set "BD2W_RUN_TOOL=%BD2W_TOOL%" if not exist "%BD2W_TOOL_DIR%" ( mkdir "%BD2W_TOOL_DIR%" if errorlevel 1 exit /b 1 ) +set "BD2W_TEMP=%BD2W_TOOL_DIR%\.bd2w-%RANDOM%-%RANDOM%.exe" +set "BD2W_INPUTS=%BD2W_TEMP%.inputs" +call :check_inputs +if errorlevel 2 exit /b 1 +if not errorlevel 1 goto run_tool set "BD2W_LOCK=%BD2W_TOOL_DIR%\.bootstrap.lock" set "BD2W_ATTEMPTS=0" :lock mkdir "%BD2W_LOCK%" 2>nul if errorlevel 1 goto wait -set "BD2W_TEMP=%BD2W_TOOL_DIR%\.bd2w-%RANDOM%-%RANDOM%.exe" +call :check_inputs +if errorlevel 2 goto failed +if not errorlevel 1 goto unlock call :compile_tool if errorlevel 1 goto failed -if not exist "%BD2W_TOOL%" goto replace_tool -fc /b "%BD2W_TEMP%" "%BD2W_TOOL%" >nul 2>nul -if errorlevel 1 goto replace_tool -del /q "%BD2W_TEMP%" +move /y "%BD2W_TEMP%" "%BD2W_TOOL%" >nul if errorlevel 1 goto failed -goto unlock -:replace_tool -move /y "%BD2W_TEMP%" "%BD2W_TOOL%" >nul 2>nul -if errorlevel 1 set "BD2W_RUN_TOOL=%BD2W_TEMP%" :unlock rmdir "%BD2W_LOCK%" if errorlevel 1 exit /b 1 -"%BD2W_RUN_TOOL%" %* +:run_tool +"%BD2W_TOOL%" %* set "BD2W_EXIT_CODE=%errorlevel%" -if exist "%BD2W_TEMP%" ( - del /q "%BD2W_TEMP%" - if errorlevel 1 exit /b 1 -) +if exist "%BD2W_TEMP%" del /q "%BD2W_TEMP%" +if exist "%BD2W_INPUTS%" del /q "%BD2W_INPUTS%" exit /b %BD2W_EXIT_CODE% +:show_help +type "%BD2W_ROOT%go\build\help.txt" +exit /b %errorlevel% + +:validate_task +if "%~1"=="--plan" ( + shift + goto validate_task +) +if "%~1"=="" exit /b 2 +if "%~1"=="help" exit /b 2 +if "%~1"=="-h" exit /b 2 +if "%~1"=="--help" exit /b 2 +if /i "%~1"=="-Help" exit /b 2 +if not exist "%BD2W_ROOT%go\build\tasks.txt" ( + echo bd2w: Task catalog not found. 1>&2 + exit /b 1 +) +for /f "usebackq" %%T in ("%BD2W_ROOT%go\build\tasks.txt") do if "%~1"=="%%T" exit /b 0 +echo bd2w: unknown task "%~1"; use --help for available commands 1>&2 +exit /b 1 + :wait set /a BD2W_ATTEMPTS+=1 >nul if %BD2W_ATTEMPTS% geq 120 ( + if exist "%BD2W_INPUTS%" del /q "%BD2W_INPUTS%" echo bd2w: Bootstrap lock still held at "%BD2W_LOCK%"; check the first launcher. 1>&2 exit /b 1 ) @@ -46,11 +69,18 @@ ping -n 2 127.0.0.1 >nul goto lock :failed -if defined BD2W_TEMP if exist "%BD2W_TEMP%" del /q "%BD2W_TEMP%" +if exist "%BD2W_TEMP%" del /q "%BD2W_TEMP%" +if exist "%BD2W_INPUTS%" del /q "%BD2W_INPUTS%" rmdir "%BD2W_LOCK%" echo bd2w: Build tool compilation failed. 1>&2 exit /b 1 +:check_inputs +powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%BD2W_ROOT%go\build\bootstrap.ps1" -Root "%BD2W_ROOT%." -Candidate "%BD2W_INPUTS%" +set "BD2W_CHECK_EXIT=%errorlevel%" +if exist "%BD2W_INPUTS%" set /p BD2W_TOOL=<"%BD2W_INPUTS%" +exit /b %BD2W_CHECK_EXIT% + :compile_tool setlocal pushd "%BD2W_ROOT%go" diff --git a/go/build/bootstrap.ps1 b/go/build/bootstrap.ps1 new file mode 100644 index 0000000..ec865bf --- /dev/null +++ b/go/build/bootstrap.ps1 @@ -0,0 +1,34 @@ +param( + [Parameter(Mandatory)][string]$Root, + [Parameter(Mandatory)][string]$Candidate +) + +$ErrorActionPreference = 'Stop' +try { + $inputs = [Collections.Generic.List[string]]::new() + foreach ($directory in @('go/build', 'go/internal')) { + foreach ($file in Get-ChildItem -LiteralPath (Join-Path $Root $directory) -Recurse -File) { + $inputs.Add("$($file.FullName)|$($file.Length)|$($file.LastWriteTimeUtc.Ticks)") + } + } + foreach ($name in @('bd2w.cmd', 'go/go.mod', 'go/go.sum')) { + $file = Get-Item -LiteralPath (Join-Path $Root $name) + $inputs.Add("$($file.FullName)|$($file.Length)|$($file.LastWriteTimeUtc.Ticks)") + } + $compiler = Get-Item -LiteralPath (Get-Command go -CommandType Application).Source + $inputs.Add("$($compiler.FullName)|$($compiler.Length)|$($compiler.LastWriteTimeUtc.Ticks)") + $inputs.Sort([StringComparer]::Ordinal) + $sha = [Security.Cryptography.SHA256]::Create() + try { + $signature = [BitConverter]::ToString($sha.ComputeHash([Text.Encoding]::UTF8.GetBytes([string]::Join("`n", $inputs)))).Replace("-", "").ToLowerInvariant() + } finally { + $sha.Dispose() + } + $tool = Join-Path $Root ('.build/tools/bd2w-' + $signature + '.exe') + [IO.File]::WriteAllText($Candidate, $tool, [Text.Encoding]::Default) + if (Test-Path -LiteralPath $tool -PathType Leaf) { exit 0 } + exit 1 +} catch { + [Console]::Error.WriteLine("bd2w: cannot check build tool inputs: $($_.Exception.Message)") + exit 2 +} diff --git a/go/build/build.go b/go/build/build.go index a6cb885..06fc67f 100644 --- a/go/build/build.go +++ b/go/build/build.go @@ -2,17 +2,22 @@ package main import ( "context" + _ "embed" "errors" "fmt" "os" "os/exec" "os/signal" + "slices" "strings" "bd2server/build/sourcetools" "bd2server/internal/common/taskgraph" ) +//go:embed tasks.txt +var taskNames string + func main() { if err := run(os.Args[1:]); err != nil { if errors.Is(err, context.Canceled) { @@ -27,30 +32,11 @@ func main() { } } -func usage() { - fmt.Printf(`Usage: - bd2w build [-GameDir directory] [-SkipTests] [-SchedulesOnly] - bd2w release changelog --component server|client [--bump patch] | publish [--no-announcement] [--component all|schedules] - bd2w runClient [client options] - bd2w runClient2 [--instance player2] [--game-dir directory] - bd2w plugins [--game-dir directory] - bd2w runServer [server options] - bd2w check-csharp [%s] - bd2w sdk pack [--game-dir directory] [--package-version version] [--output-directory directory] - bd2w sdk verify [--game-dir directory] [--package-version version] [--package-directory directory] - bd2w sdk update-names --game-mapping file [--game-dir directory] [--version-config file] - bd2w version-source --config file --output file --plugin name - bd2w deobfuscate --source directory --mapping file --output directory - bd2w diff --a directory --b directory --out report.txt [diff options] - bd2w download-client [--package-dir directory] [--manifest file] [--download | --base] - bd2w download-resources --output directory --bundle-version version --game-data-version version - Add --plan to show task dependencies without running them. +//go:embed help.txt +var helpText string -Windows: .\bd2w build -Linux/macOS: ./bd2w build -Build automatically selects the native platform and architecture. -The wrappers cache the build tool and refresh it when its sources change. -`, strings.Join(csharpProjects(), " ")) +func usage() { + fmt.Print(helpText) } func run(args []string) error { @@ -71,15 +57,15 @@ func run(args []string) error { usage() return nil } + if !slices.Contains(strings.Fields(taskNames), args[0]) { + return fmt.Errorf("unknown task %q; use --help for available commands", args[0]) + } switch args[0] { case "deobfuscate": return t.execute("deobfuscate", taskgraph.Task{Name: "deobfuscate", Run: func(context.Context) error { return sourcetools.RunDeobfuscate(args[1:], os.Stdout) }}) case "diff": return t.execute("diff", taskgraph.Task{Name: "diff", Run: func(context.Context) error { return sourcetools.RunDiff(args[1:], os.Stdout) }}) } - if args[0] != "build" && args[0] != "runClient" && args[0] != "runClient2" && args[0] != "runServer" && args[0] != "sdk" && args[0] != "version-source" && args[0] != "plugins" && args[0] != "check-csharp" && args[0] != "download-client" && args[0] != "download-resources" && args[0] != "release" { - return fmt.Errorf("unknown task %q; use --help for available commands", args[0]) - } if len(args) == 2 && isHelp(args[1]) && args[0] != "release" { usage() return nil diff --git a/go/build/help.txt b/go/build/help.txt new file mode 100644 index 0000000..3be1f61 --- /dev/null +++ b/go/build/help.txt @@ -0,0 +1,22 @@ +Usage: + bd2w build [-GameDir directory] [-SkipTests] [-SchedulesOnly] + bd2w release changelog --component server|client [--bump patch] | publish [--no-announcement] [--component all|schedules] + bd2w runClient [client options] + bd2w runClient2 [--instance player2] [--game-dir directory] + bd2w plugins [--game-dir directory] + bd2w runServer [server options] + bd2w check-csharp [project ...] + bd2w sdk pack [--game-dir directory] [--package-version version] [--output-directory directory] + bd2w sdk verify [--game-dir directory] [--package-version version] [--package-directory directory] + bd2w sdk update-names --game-mapping file [--game-dir directory] [--version-config file] + bd2w version-source --config file --output file --plugin name + bd2w deobfuscate --source directory --mapping file --output directory + bd2w diff --a directory --b directory --out report.txt [diff options] + bd2w download-client [--package-dir directory] [--manifest file] [--download | --base] + bd2w download-resources --output directory --bundle-version version --game-data-version version + Add --plan to show task dependencies without running them. + +Windows: .\bd2w build +Linux/macOS: ./bd2w build +Build automatically selects the native platform and architecture. +The wrappers cache the build tool and refresh it when its sources change. diff --git a/go/build/tasks.txt b/go/build/tasks.txt new file mode 100644 index 0000000..51eca8c --- /dev/null +++ b/go/build/tasks.txt @@ -0,0 +1,13 @@ +build +release +runClient +runClient2 +plugins +runServer +check-csharp +sdk +version-source +deobfuscate +diff +download-client +download-resources