name: re3 premake amd64 build

on:
  workflow_dispatch:
  pull_request:
  push:
    branches: [ main, master ]
  release:
    types: [ published ]

env:
  GLFW_VER: "3.3.2"
  GLFW_BASE: "glfw-3.3.2.bin.WIN64"
  GLFW_FILE: "glfw-3.3.2.bin.WIN64.zip"
  GLFW_URL: "https://github.com/glfw/glfw/releases/download/3.3.2/glfw-3.3.2.bin.WIN64.zip"

jobs:
  build:
    runs-on: windows-2022
    strategy:
      matrix:
        platform: [win-amd64-librw_d3d9-oal, win-amd64-librw_gl3_glfw-oal]
        buildtype: [Debug, Release]
      fail-fast: false

    steps:
    - name: Add MSBuild to PATH
      uses: microsoft/setup-msbuild@v1.0.2

    - uses: actions/checkout@v4
      with:
        submodules: recursive

    - name: Download GLFW
      if: contains(matrix.platform, 'librw_gl3_glfw')
      uses: carlosperate/download-file-action@v1.0.3
      with:
        file-url: ${{ env.GLFW_URL }}
        file-name: ${{ env.GLFW_FILE }}

    - name: Extract GLFW
      if: contains(matrix.platform, 'librw_gl3_glfw')
      shell: pwsh
      run: |
        Expand-Archive -Path "${{ env.GLFW_FILE }}" -DestinationPath "." -Force

    - name: Configure build
      shell: cmd
      run: |
        premake5 vs2019 --with-librw --no-full-paths --glfwdir64="${{ env.GLFW_BASE }}"

    - name: Build
      shell: cmd
      run: |
        msbuild -m -p:Configuration=${{ matrix.buildtype }} -p:Platform=${{ matrix.platform }} build/re3.sln

    - name: Stage gamefiles (vanilla resources) + binaries
      shell: pwsh
      run: |
        New-Item -ItemType Directory -Path "dist" -Force
        Copy-Item -Path "gamefiles/vanilla/*" -Destination "dist/" -Recurse -Force

    - name: Copy binaries
      shell: pwsh
      run: |
        Move-Item -Path "bin/${{ matrix.platform }}/${{ matrix.buildtype }}/re3.exe" -Destination "dist/" -Force
        Move-Item -Path "bin/${{ matrix.platform }}/${{ matrix.buildtype }}/re3.pdb" -Destination "dist/" -Force

    - name: Copy dependencies
      shell: pwsh
      run: |
        Move-Item -Path "vendor/mpg123/dist/Win64/libmpg123-0.dll" -Destination "dist/" -Force
        Move-Item -Path "vendor/openal-soft/dist/Win64/OpenAL32.dll" -Destination "dist/" -Force

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: re3_${{ matrix.buildtype }}_${{ matrix.platform }}
        path: dist/*
