name: Build Game

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
    - '**'              # every branch
    - '!no-build-**'    # unless marked as no-build

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release
  CPM_SOURCE_CACHE: ${{ github.workspace }}/cpm-cache

permissions:
  contents: write

jobs:
  build-windows:
    name: Windows Build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup caches
        uses: ./.github/actions/setup-cache
        with:
          host: win
          target: win
      
      - name: Download xwin
        uses: robinraju/release-downloader@v1.10
        with:
          repository: Jake-Shadle/xwin
          latest: true
          fileName: 'xwin-*-x86_64-unknown-linux-musl.tar.gz'
          tarBall: false
          zipBall: false
          out-file-path: "epic-xwin"
        
      - name: Prepare xwin
        run: |
          tar -xzf epic-xwin/xwin-*-x86_64-unknown-linux-musl.tar.gz -C epic-xwin
          mv epic-xwin/xwin-*-x86_64-unknown-linux-musl/xwin ./xwin
          ./xwin --arch x86_64 --sdk-version 10.0.22621 --accept-license list
      
      # this caches '.xwin-cache/**/*' but that's actually only gonna
      # contain the manifest cuz we only ran `xwin list` so far
      - name: Setup xwin Cache
        id: xwin-cache
        uses: actions/cache@v4
        with:
          path: .xwin-cache
          key: xwin-win-v1-${{ hashFiles('.xwin-cache/**/*') }}

      - name: Download Windows headers
        run: ./xwin --arch x86_64 --sdk-version 10.0.22621 --accept-license splat --include-debug-libs
        if: steps.xwin-cache.outputs.cache-hit != 'true'


      - name: Download clang-msvc-sdk toolchain
        uses: actions/checkout@v4
        with:
          repository: 'Nemirtingas/clang-msvc-sdk'
          path: toolchain
          submodules: recursive

      - name: Install Clang
        run: |
          sudo apt-get update
          sudo apt-get install -y wget lsb-release software-properties-common gnupg

          wget https://apt.llvm.org/llvm.sh
          chmod +x llvm.sh
          sudo ./llvm.sh 21 all

      - name: Add LLVM 21 to path
        run: echo "/usr/lib/llvm-21/bin" >> $GITHUB_PATH

      - name: Verify LLVM
        run: |
          which clang-cl
          which lld-link
          clang-cl --version
          lld-link --version

      - name: Create Build Environment
        # Some projects don't allow in-source building, so create a separate build directory
        # We'll use this as our working directory for all subsequent commands
        run: cmake -E make_directory ${{github.workspace}}/build
      
      - name: Configure CMake
        working-directory: ${{github.workspace}}/build
        run: cmake ${{github.workspace}} -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
        env:
          SPLAT_DIR: ${{ github.workspace }}/.xwin-cache/splat
          TOOLCHAIN: ${{ github.workspace }}/toolchain/clang-cl-msvc.cmake
          HOST_ARCH: x86_64

      - name: Build
        working-directory: ${{github.workspace}}/build
        run: cmake --build . --config $BUILD_TYPE --target MinecraftPE --parallel
      
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: mcpe-windows
          path: |
            ${{github.workspace}}/build/${{ env.BUILD_TYPE }}/MinecraftPE.exe
            ${{github.workspace}}/build/${{ env.BUILD_TYPE }}/glfw3.dll
            ${{github.workspace}}/build/${{ env.BUILD_TYPE }}/libpng16.dll
            ${{github.workspace}}/build/${{ env.BUILD_TYPE }}/OpenAL32.dll
            ${{github.workspace}}/build/${{ env.BUILD_TYPE }}/z.dll

  build-linux:
    name: Linux Build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup caches
        uses: ./.github/actions/setup-cache
        with:
          host: linux
          target: linux

      - name: Create Build Environment
        # Some projects don't allow in-source building, so create a separate build directory
        # We'll use this as our working directory for all subsequent commands
        run: cmake -E make_directory ${{github.workspace}}/build

      - name: Setup Environment
        run: |
          sudo apt-get update -qq
          sudo apt-get install gcc-multilib
          sudo apt-get install -y --no-install-recommends build-essential libgl-dev libwayland-dev xorg-dev libxkbcommon-dev
      
      - name: Configure CMake
        # Use a bash shell so we can use the same syntax for environment variable
        # access regardless of the host operating system
        shell: bash
        working-directory: ${{github.workspace}}/build
        run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE

      - name: Build
        working-directory: ${{github.workspace}}/build
        shell: bash
        run: |
          cmake --build . --config $BUILD_TYPE --target MinecraftPE --parallel
          cmake --build . --config $BUILD_TYPE --target MinecraftPE-server --parallel
      
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: mcpe-linux
          path: |
            ${{github.workspace}}/build/MinecraftPE
            ${{github.workspace}}/build/MinecraftPE-server

  build-android: # pray to god
    name: Build Android APK (${{ matrix.abi }})
    runs-on: ubuntu-latest

    strategy:
      # keep going with the other ABI if one fails so you at least get something useful
      fail-fast: false
      matrix:
        abi: [ arm64-v8a, armeabi-v7a ]

    env:
      ANDROID_SDK_ROOT: ${{ github.workspace }}/android-sdk
      ANDROID_NDK_PATH: ${{ github.workspace }}/android-ndk-r14b
      ANDROID_PLATFORM_API: 36
      ANDROID_BUILD_TOOLS_VERSION: 36.0.0
      ADB: /bin/true
      # build.sh reads MATRIX_ABI to decide which ABI to build when no --abi flag is passed
      MATRIX_ABI: ${{ matrix.abi }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Cache Android command-line tools
        id: cache-android-tools
        uses: actions/cache@v3
        with:
          path: ${{ env.ANDROID_SDK_ROOT }}
          key: android-cmdline-tools-v36

      - name: Setup Android command line tools
        if: steps.cache-android-tools.outputs.cache-hit != 'true'
        run: |
          if [ ! -d "$ANDROID_SDK_ROOT/cmdline-tools/latest" ]; then
            mkdir -p "$ANDROID_SDK_ROOT/cmdline-tools"
            curl -o cmdline-tools.zip -L "https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip"
            unzip -q cmdline-tools.zip -d "$ANDROID_SDK_ROOT/cmdline-tools"
            mv "$ANDROID_SDK_ROOT/cmdline-tools/cmdline-tools" "$ANDROID_SDK_ROOT/cmdline-tools/latest"
          fi
          yes | "$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" --sdk_root="$ANDROID_SDK_ROOT" "platform-tools" "platforms;android-${ANDROID_PLATFORM_API}" "build-tools;${ANDROID_BUILD_TOOLS_VERSION}"


      - name: Cache Android NDK r14b
        id: cache-android-ndk
        uses: actions/cache@v3
        with:
          path: ${{ github.workspace }}/android-ndk-r14b
          key: android-ndk-r14b

      - name: Install Android NDK r14b
        if: steps.cache-android-ndk.outputs.cache-hit != 'true'
        run: |
          if [ ! -d "$ANDROID_NDK_PATH" ]; then
            curl -L -o ndk.zip "https://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip"
            unzip -q ndk.zip -d "$GITHUB_WORKSPACE"
          fi

      - name: Install system prerequisites
        run: |
          sudo apt-get update -qq
          sudo apt-get install -y --no-install-recommends wget unzip curl git python3 libncurses6 libtinfo6
          if ! ldconfig -p | grep -q "libncurses.so.5"; then
            sudo ln -sf /lib/x86_64-linux-gnu/libncurses.so.6 /usr/lib/x86_64-linux-gnu/libncurses.so.5 || true
          fi
          if ! ldconfig -p | grep -q "libtinfo.so.5"; then
            sudo ln -sf /lib/x86_64-linux-gnu/libtinfo.so.6 /usr/lib/x86_64-linux-gnu/libtinfo.so.5 || true
          fi

      - name: Setup Java 25
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 25

      - name: Validate environment
        run: |
          echo "ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT"
          echo "ANDROID_NDK_PATH=$ANDROID_NDK_PATH"
          echo "JAVA_HOME=$JAVA_HOME"
          echo "MATRIX_ABI=$MATRIX_ABI"
          $ANDROID_SDK_ROOT/platform-tools/adb version || true
          java -version
          javac -version

      - name: Run Android build script
        run: |
          chmod +x ./build.sh
          ./build.sh

      - name: Upload APK
        uses: actions/upload-artifact@v4
        with:
          # artifact name is ABI-specific so both matrix legs can upload without clobbering each other
          name: minecraftpe-apk-${{ matrix.abi }}
          path: ${{ github.workspace }}/build-apk/minecraftpe-*-debug.apk

  publish:
    name: Publish
    runs-on: ubuntu-latest
    needs: [ build-windows, build-linux, build-android ]
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Declare Version Variables
        id: ref
        run: |
          echo "version=$(cat VERSION | xargs)" >> $GITHUB_OUTPUT
          echo "hash=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_OUTPUT

      - name: Download Artifacts
        uses: actions/download-artifact@v4

      - name: Zip Windows Artifacts
        uses: vimtor/action-zip@v1.2
        with:
          files: data mcpe-windows/MinecraftPE.exe mcpe-windows/glfw3.dll mcpe-windows/libpng16.dll mcpe-windows/OpenAL32.dll mcpe-windows/z.dll
          dest: minecraftpe-${{ steps.ref.outputs.hash }}-windows.zip

      - name: Zip Linux Artifacts
        uses: vimtor/action-zip@v1.2
        with:
          files: data mcpe-linux/MinecraftPE
          dest: minecraftpe-${{ steps.ref.outputs.hash }}-linux.zip

      - name: Zip Linux Server Artifacts
        uses: vimtor/action-zip@v1.2
        with:
          files: mcpe-linux/MinecraftPE-server
          dest: minecraftpe-server-${{ steps.ref.outputs.hash }}.zip

      - name: Zip Android arm64-v8a Artifact
        uses: vimtor/action-zip@v1.2
        with:
          files: minecraftpe-apk-arm64-v8a/minecraftpe-v8a-debug.apk
          dest: minecraftpe-${{ steps.ref.outputs.hash }}-android-arm64-v8a.zip

      - name: Zip Android armeabi-v7a Artifact
        uses: vimtor/action-zip@v1.2
        with:
          files: minecraftpe-apk-armeabi-v7a/minecraftpe-v7a-debug.apk
          dest: minecraftpe-${{ steps.ref.outputs.hash }}-android-armeabi-v7a.zip

      - name: Update Development Release
        uses: andelf/nightly-release@main
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: dev
          name: 'Development Release'
          body: |
            MinecraftPE development release for commit ${{ github.sha }}.
          files: |
            ./minecraftpe-${{ steps.ref.outputs.hash }}-windows.zip
            ./minecraftpe-${{ steps.ref.outputs.hash }}-linux.zip
            ./minecraftpe-server-${{ steps.ref.outputs.hash }}.zip
            ./minecraftpe-${{ steps.ref.outputs.hash }}-android-arm64-v8a.zip
            ./minecraftpe-${{ steps.ref.outputs.hash }}-android-armeabi-v7a.zip