set shell := ["bash", "-euo", "pipefail", "-c"]

scons := "uvx --python 3.14 --from scons==4.10.1 scons"
engine_dir := justfile_directory() / "Engine"
engine_profile := justfile_directory() / "engine.py"
nuget_feed := justfile_directory() / ".cache/godot/nuget"
editor_bin := engine_dir / "bin/godot.linuxbsd.editor.double.x86_64.mono"
solution := justfile_directory() / "EvilEngine.slnx"
user_data := env("XDG_DATA_HOME", home_directory() / ".local/share") / "godot/app_userdata/Evil Engine"

build_memory := env("EVIL_BUILD_MEMORY", "8G")
test_memory := env("EVIL_TEST_MEMORY", "8G")
capped := "systemd-run --user --wait --pipe --collect -p MemorySwapMax=0 -p OOMPolicy=continue --same-dir -E PATH -E HOME $(compgen -e | grep -E '^(EVIL_|DOTNET_|MSBUILD|XDG_|STEAM)' | sed 's/^/-E /')"

export MSBUILDDISABLENODEREUSE := "1"
export DOTNET_CLI_USE_MSBUILD_SERVER := "0"
export CCACHE_DIR := env("XDG_CACHE_HOME", home_directory() / ".cache") / "evil-engine/ccache"
export CCACHE_MAXSIZE := "30G"

import? 'internal.just'

[private]
default:
    @just --list

# Build the fork's double-precision .NET editor, generate its C# glue and push GodotSharp and Godot.NET.Sdk to the local NuGet feed
[linux]
godot: ccache-dir compiledb
    {{scons}} -C "{{engine_dir}}" profile="{{engine_profile}}" target=editor debug_symbols=yes separate_debug_symbols=yes -j"$(nproc)"
    "{{editor_bin}}" --headless --generate-mono-glue "{{engine_dir}}/modules/mono/glue"
    mkdir -p "{{nuget_feed}}"
    uv run --no-project --python 3.14 python "{{engine_dir}}/modules/mono/build_scripts/build_assemblies.py" --godot-output-dir="{{engine_dir}}/bin" --godot-platform=linuxbsd --precision=double --push-nupkgs-local="{{nuget_feed}}"

# Write Engine/compile_commands.json for CLion with the same options as just godot, without compiling anything
[linux]
compiledb:
    {{scons}} -C "{{engine_dir}}" profile="{{engine_profile}}" target=editor debug_symbols=yes separate_debug_symbols=yes compiledb_gen_only=yes c_compiler_launcher= cpp_compiler_launcher=

# Open the fork's editor on the Godot project, or its project manager until there is one
[linux]
editor *args:
    if [ -f Godot/project.godot ]; then "{{editor_bin}}" --path Godot --editor {{args}}; else "{{editor_bin}}" --project-manager {{args}}; fi

# Build the game's C# and run it straight into its main menu without the editor, passing any Godot arguments on
[linux]
[positional-arguments]
launch *args:
    @dotnet build "Godot/Evil Engine.csproj" -v q --nologo
    "{{editor_bin}}" --path Godot "$@"

# Build every C# project in the root solution: the game, the libraries, the adapter folders in .build/adapters and the mounts inspector
build:
    {{capped}} -p MemoryMax={{build_memory}} dotnet build "{{solution}}" -v q --nologo

# Run the root solution's tests, reading a real Garry's Mod install too when EVIL_GMOD points at one, under an EVIL_TEST_MEMORY cap (8G by default)
[positional-arguments]
test *args:
    {{capped}} -p MemoryMax={{test_memory}} dotnet test --solution "{{solution}}" "$@"

# Export the Blender models, write the PNG and JPEG images the format tests read, dump the fork's humanoid profile they check theirs against, and write the music tests' library and FLAC files with ffmpeg, flac and lame
fixtures:
    blender -b --factory-startup --python Libraries/Tests/EvilEngine.Formats.Tests/Fixtures/make_fixtures.py -- Libraries/Tests/EvilEngine.Formats.Tests/Fixtures
    uv run --no-project --with pillow python -I Libraries/Tests/EvilEngine.Formats.Tests/Fixtures/make_images.py Libraries/Tests/EvilEngine.Formats.Tests/Fixtures/Images
    "{{editor_bin}}" --headless --script Libraries/Tests/EvilEngine.Formats.Tests/Fixtures/Humanoids/dump_profile.gd -- Libraries/Tests/EvilEngine.Formats.Tests/Fixtures/Humanoids/profile.json
    Libraries/Tests/EvilEngine.Music.Tests/Fixtures/make_music.sh Libraries/Tests/EvilEngine.Music.Tests/Fixtures

# Probe a location through the built adapters and print its mounts, or every pointer in user://mounts/ when none is given
[positional-arguments]
mounts *args: build
    dotnet run --no-build --project Libraries/Tools/EvilEngine.Inspector -- --adapters .build/adapters --pointers "{{user_data}}/mounts" "$@"

# Render a map through the engine's world builder to a PNG from its spawn or --at=x,y,z --look=pitch,yaw, or each line of a --shots file of 'name x y z pitch yaw fov' into an --out folder, with --exposure=<scale> to hold the tonemap scale
[linux]
[positional-arguments]
map-shot map *args:
    dotnet build "Godot/Evil Engine.csproj" -v q --nologo
    "{{editor_bin}}" --path Godot res://Client/World/MapShot.tscn -- --map="$1" "${@:2}"

# Walk the movement check's routes headless with the engine's own controller on Jolt, or only --route=<name>, failing on any route that doesn't come within its radius of its end, or with --sweep=<runs> (--seed, --seconds, --walk, --map, --watch=<run>, --trace) sprint a body in random directions from the map's spawns and count its hops and stalls
[linux]
[positional-arguments]
movement-check *args:
    dotnet build "Godot/Evil Engine.csproj" -v q --nologo
    {{capped}} -q -p MemoryMax={{test_memory}} "{{editor_bin}}" --headless --path Godot res://Client/World/MovementCheck.tscn -- "$@"

# Name the surface under a ray and a dropped box's contact on --map's (gm_construct by default) triangles of each of --surfaces (grass by default) headless through the Jolt world, failing on any the map's collider doesn't report
[linux]
[positional-arguments]
surface-check *args:
    dotnet build "Godot/Evil Engine.csproj" -v q --nologo
    {{capped}} -q -p MemoryMax={{test_memory}} "{{editor_bin}}" --headless --path Godot res://Client/World/SurfaceCheck.tscn -- "$@"

# Time a map's start the way Start runs it, with vsync on, in a fresh game each run: each stage, the world build's waits on the render thread and its first frame, under an EVIL_TEST_MEMORY cap
[linux]
map-bench map="gmod:gm_construct" runs="3":
    @dotnet build "Godot/Evil Engine.csproj" -v q --nologo
    @for run in $(seq {{runs}}); do {{capped}} -q -p MemoryMax={{test_memory}} -E DISPLAY -E WAYLAND_DISPLAY "{{editor_bin}}" --path Godot res://Client/World/MapShot.tscn -- --map={{quote(map)}} --bench=1 --size=1600x900 2>&1 | grep -E '^bench |FAILED|No map record'; done

# Show the engine build's ccache hit rate and size
ccache:
    @ccache --show-stats

[private]
ccache-dir:
    @[ -d "$CCACHE_DIR" ] || { mkdir -p "$(dirname "$CCACHE_DIR")" && { btrfs subvolume create "$CCACHE_DIR" >/dev/null 2>&1 || mkdir "$CCACHE_DIR"; }; }
