name: Automated Test Build

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build-windows:
    strategy:
      fail-fast: false
      matrix:
        name:
          - windows
#          - windows-irrklang
#          - windows-no-dxsdk
          - windows-x64
#          - windows-2025
        include:
          - name: windows
            os: windows-2022
            vs: vs2022
            audiolib: miniaudio
#          - name: windows-irrklang
#            os: windows-2022
#            vs: vs2022
#            audiolib: irrklang
#          - name: windows-no-dxsdk
#            os: windows-2022
#            vs: vs2022
#            audiolib: miniaudio
#            nodxsdk: true
          - name: windows-x64
            os: windows-2022
            vs: vs2022
            audiolib: miniaudio
            x64: true
#          - name: windows-2025
#            os: windows-2025
#            vs: vs2025 # to be enabled after the release of Visual Studio 2025
#            audiolib: miniaudio

    runs-on: ${{ matrix.os }}

    steps:
    - name: Checkout repository with submodules
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: true

    - name: Update submodules
      run: |
        cd ocgcore
        git checkout master
        git pull origin master
        cd ..
#        cd script
#        git checkout master
#        git pull origin master
#        cd ..

    - name: Download premake
      id: premake
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/premake/premake-core/releases/download/v5.0.0-beta7/premake-5.0.0-beta7-windows.zip
        filename: premake5.zip

    - name: Extract premake
      run: |
        7z x ${{ steps.premake.outputs.filepath }}

    - name: Download libevent
      id: libevent
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
        filename: libevent.tar.gz

    - name: Extract libevent
      run: |
        tar xf ${{ steps.libevent.outputs.filepath }}
        move libevent-2.1.12-stable event

    - name: Download freetype
      id: freetype
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://downloads.sourceforge.net/freetype/freetype-2.13.3.tar.gz

    - name: Extract freetype
      run: |
        tar xf ${{ steps.freetype.outputs.filepath }}
        move freetype-2.13.3 freetype

    - name: Download lua
      id: lua
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://www.lua.org/ftp/lua-5.4.8.tar.gz

    - name: Extract lua
      run: |
        tar xf ${{ steps.lua.outputs.filepath }}
        move lua-5.4.8 lua

    - name: Download sqlite
      id: sqlite
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://www.sqlite.org/2025/sqlite-amalgamation-3500100.zip

    - name: Extract sqlite
      run: |
        7z x ${{ steps.sqlite.outputs.filepath }}
        move sqlite-amalgamation-3500100 sqlite3

    - name: Download miniaudio
      if: matrix.audiolib == 'miniaudio'
      run: |
        git clone --depth=1 https://github.com/mercury233/miniaudio
        cd miniaudio
        xcopy /Y extras\miniaudio_split\miniaudio.* .
        cd ..

    - name: Download ogg
      if: matrix.audiolib == 'miniaudio'
      id: ogg
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/ogg/releases/download/v1.3.5/libogg-1.3.5.tar.gz

    - name: Extract ogg
      if: matrix.audiolib == 'miniaudio'
      run: |
        tar xf ${{ steps.ogg.outputs.filepath }}
        move libogg-1.3.5 miniaudio/external/ogg

    - name: Download opus
      if: matrix.audiolib == 'miniaudio'
      id: opus
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/opus/releases/download/v1.5.2/opus-1.5.2.tar.gz

    - name: Extract opus
      if: matrix.audiolib == 'miniaudio'
      run: |
        tar xf ${{ steps.opus.outputs.filepath }}
        move opus-1.5.2 miniaudio/external/opus

    - name: Download opusfile
      if: matrix.audiolib == 'miniaudio'
      id: opusfile
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/opusfile/releases/download/v0.12/opusfile-0.12.tar.gz

    - name: Extract opusfile
      if: matrix.audiolib == 'miniaudio'
      run: |
        tar xf ${{ steps.opusfile.outputs.filepath }}
        move opusfile-0.12 miniaudio/external/opusfile

    - name: Download vorbis
      if: matrix.audiolib == 'miniaudio'
      id: vorbis
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/vorbis/releases/download/v1.3.7/libvorbis-1.3.7.tar.gz

    - name: Extract vorbis
      if: matrix.audiolib == 'miniaudio'
      run: |
        tar xf ${{ steps.vorbis.outputs.filepath }}
        move libvorbis-1.3.7 miniaudio/external/vorbis

    - name: Download irrKlang
      if: matrix.audiolib == 'irrklang'
      id: irrKlang
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://www.ambiera.at/downloads/irrKlang-32bit-1.6.0.zip

    - name: Extract irrKlang
      if: matrix.audiolib == 'irrklang'
      run: |
        7z x ${{ steps.irrKlang.outputs.filepath }}
        move irrKlang-1.6.0 irrKlang

    - name: Download irrlicht
      run: |
        git clone --depth=1 https://github.com/mercury233/irrlicht

    - name: Check DirectX SDK
      if: matrix.nodxsdk != true
      id: dxsdk
      uses: actions/cache@v4
      with:
        key: dxsdk
        path: DXSDK

    - name: Download DirectX SDK
      if: matrix.nodxsdk != true && steps.dxsdk.outputs.cache-hit != 'true'
      id: dxsdk-download
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://download.microsoft.com/download/a/e/7/ae743f1f-632b-4809-87a9-aa1bb3458e31/DXSDK_Jun10.exe

    - name: Install DirectX SDK
      if: matrix.nodxsdk != true && steps.dxsdk.outputs.cache-hit != 'true'
      run: |
        7z x ${{ steps.dxsdk-download.outputs.filepath }} -aoa

    - name: Set DirectX SDK environment variable
      if: matrix.nodxsdk != true
      run: |
        $dxsdkPath = Resolve-Path 'DXSDK'
        "DXSDK_DIR=$($dxsdkPath.ProviderPath)\" | Out-File -FilePath $env:GITHUB_ENV -Append

    - name: Copy premake files
      run: |
        xcopy /E premake\* .
        xcopy /E resource\* .

    - name: Use premake to generate Visual Studio solution
      run: |
        .\premake5.exe ${{ matrix.vs }} --audio-lib=${{ matrix.audiolib }}

    - name: Add msbuild to PATH
      uses: microsoft/setup-msbuild@v2

    - name: Build solution
      run: |
        MSBuild.exe build\YGOPro.sln /m /p:Configuration=Release /p:Platform=${{ matrix.x64 && 'x64' || 'Win32' }}

    - name: Upload build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: YGOPro-${{ matrix.name }}
        path: |
          bin/release/x86/YGOPro.exe
          bin/release/x64/YGOPro.exe

  build-linux:
    strategy:
      fail-fast: false
      matrix:
        name:
          - ubuntu-22
          - ubuntu-24
          - ubuntu-static-link
        include:
          - name: ubuntu-22
            os: ubuntu-22.04
            premake-version: 5.0.0-beta4
          - name: ubuntu-24
            os: ubuntu-24.04
            premake-version: 5.0.0-beta7
          - name: ubuntu-static-link
            os: ubuntu-22.04
            premake-version: 5.0.0-beta4
            static-link: true

    runs-on: ${{ matrix.os }}

    steps:
    - name: Checkout repository with submodules
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: true

    - name: Update submodules
      run: |
        cd ocgcore
        git checkout master
        git pull origin master
        cd ..
#        cd script
#        git checkout master
#        git pull origin master
#        cd ..

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libgl1-mesa-dev libglu1-mesa-dev libxxf86vm-dev

    - name: Install dependencies (dynamic link)
      if: matrix.static-link != true
      run: |
        sudo apt-get install -y libevent-dev libfreetype6-dev libsqlite3-dev libopusfile-dev libvorbis-dev

    - name: Download premake
      id: premake
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/premake/premake-core/releases/download/v${{ matrix.premake-version }}/premake-${{ matrix.premake-version }}-linux.tar.gz
        filename: premake5.tar.gz

    - name: Extract premake
      run: |
        tar xf ${{ steps.premake.outputs.filepath }}
        chmod +x ./premake5

    - name: Download libevent
      if: matrix.static-link == true
      id: libevent
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
        filename: libevent.tar.gz

    - name: Extract libevent
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.libevent.outputs.filepath }}
        mv libevent-2.1.12-stable event

    - name: Configure libevent
      if: matrix.static-link == true
      run: |
        cd event
        ./configure --disable-openssl --enable-static=yes --enable-shared=no
        sed -f make-event-config.sed < config.h > ./include/event2/event-config.h
        cd ..

    - name: Download freetype
      if: matrix.static-link == true
      id: freetype
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://downloads.sourceforge.net/freetype/freetype-2.13.3.tar.gz

    - name: Extract freetype
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.freetype.outputs.filepath }}
        mv freetype-2.13.3 freetype

    - name: Download lua
      id: lua
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://www.lua.org/ftp/lua-5.4.8.tar.gz

    - name: Extract lua
      run: |
        tar xf ${{ steps.lua.outputs.filepath }}
        mv lua-5.4.8 lua

    - name: Download sqlite
      if: matrix.static-link == true
      id: sqlite
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://www.sqlite.org/2025/sqlite-amalgamation-3500100.zip

    - name: Extract sqlite
      if: matrix.static-link == true
      run: |
        7z x ${{ steps.sqlite.outputs.filepath }}
        mv sqlite-amalgamation-3500100 sqlite3

    - name: Download miniaudio
      run: |
        git clone --depth=1 https://github.com/mercury233/miniaudio
        cd miniaudio
        cp extras/miniaudio_split/miniaudio.* .
        cd ..

    - name: Download ogg
      if: matrix.static-link == true
      id: ogg
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/ogg/releases/download/v1.3.5/libogg-1.3.5.tar.gz

    - name: Extract ogg
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.ogg.outputs.filepath }}
        mv libogg-1.3.5 miniaudio/external/ogg

    - name: Configure ogg
      if: matrix.static-link == true
      run: |
        cd miniaudio/external/ogg
        ./configure
        cd ../../..

    - name: Download opus
      if: matrix.static-link == true
      id: opus
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/opus/releases/download/v1.5.2/opus-1.5.2.tar.gz

    - name: Extract opus
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.opus.outputs.filepath }}
        mv opus-1.5.2 miniaudio/external/opus

    - name: Download opusfile
      if: matrix.static-link == true
      id: opusfile
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/opusfile/releases/download/v0.12/opusfile-0.12.tar.gz

    - name: Extract opusfile
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.opusfile.outputs.filepath }}
        mv opusfile-0.12 miniaudio/external/opusfile

    - name: Download vorbis
      if: matrix.static-link == true
      id: vorbis
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/vorbis/releases/download/v1.3.7/libvorbis-1.3.7.tar.gz

    - name: Extract vorbis
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.vorbis.outputs.filepath }}
        mv libvorbis-1.3.7 miniaudio/external/vorbis

    - name: Download irrlicht
      run: |
        git clone --depth=1 https://github.com/mercury233/irrlicht

    - name: Copy premake files
      run: |
        cp -pr premake/* .
        cp -pr resource/* .

    - name: Use premake to generate make files (apt packages)
      if: matrix.static-link != true
      run: |
        ./premake5 gmake

    - name: Use premake to generate make files (static link)
      if: matrix.static-link == true
      run: |
        ./premake5 gmake \
          --build-event \
          --build-freetype \
          --build-sqlite \
          --build-opus-vorbis

    - name: Make
      run: |
        cd build
        make -j 4 config=release
        cd ..

    - name: Upload build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: YGOPro-${{ matrix.name }}
        path: |
          bin/release/YGOPro

  build-macos:
    strategy:
      fail-fast: false
      matrix:
        name:
          - macos-13-intel
#          - macos-13-arm-cross-compile-static-link
          - macos-13-universal-static-link
          - macos-15-arm
#          - macos-15-intel-cross-compile-static-link
#          - macos-15-universal-static-link
        include:
          - name: macos-13-intel
            os: macos-13
#          - name: macos-13-arm-cross-compile-static-link
#            os: macos-13
#            cross-build-arm: true
#            static-link: true
          - name: macos-13-universal-static-link
            os: macos-13
            cross-build-intel: true
            cross-build-arm: true
            static-link: true
          - name: macos-15-arm
            os: macos-15
#          - name: macos-15-intel-cross-compile-static-link
#            os: macos-15
#            cross-build-intel: true
#            static-link: true
#          - name: macos-15-universal-static-link
#            os: macos-15
#            cross-build-intel: true
#            cross-build-arm: true
#            static-link: true

    runs-on: ${{ matrix.os }}

    steps:
    - name: Checkout repository with submodules
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        submodules: true

    - name: Update submodules
      run: |
        cd ocgcore
        git checkout master
        git pull origin master
        cd ..
#        cd script
#        git checkout master
#        git pull origin master
#        cd ..

    - name: Install dependencies
      if: matrix.static-link != true
      run: |
        brew install opus opusfile libvorbis
#        brew install sqlite libx11 freetype libevent

    - name: Download premake
      id: premake
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/premake/premake-core/releases/download/v5.0.0-beta7/premake-5.0.0-beta7-macosx.tar.gz
        filename: premake5.tar.gz

    - name: Extract premake
      run: |
        tar xf ${{ steps.premake.outputs.filepath }}
        chmod +x ./premake5

    - name: Download libevent
      if: matrix.static-link == true
      id: libevent
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
        filename: libevent.tar.gz

    - name: Extract libevent
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.libevent.outputs.filepath }}
        mv libevent-2.1.12-stable event

    - name: Configure libevent
      if: matrix.static-link == true
      run: |
        cd event
        ./configure --disable-openssl --enable-static=yes --enable-shared=no
        sed -f make-event-config.sed < config.h > ./include/event2/event-config.h
        cd ..

    - name: Download freetype
      if: matrix.static-link == true
      id: freetype
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://downloads.sourceforge.net/freetype/freetype-2.13.3.tar.gz

    - name: Extract freetype
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.freetype.outputs.filepath }}
        mv freetype-2.13.3 freetype

    - name: Download lua
      id: lua
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://www.lua.org/ftp/lua-5.4.8.tar.gz

    - name: Extract lua
      run: |
        tar xf ${{ steps.lua.outputs.filepath }}
        mv lua-5.4.8 lua

    - name: Download sqlite
      if: matrix.static-link == true
      id: sqlite
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://www.sqlite.org/2025/sqlite-amalgamation-3500100.zip

    - name: Extract sqlite
      if: matrix.static-link == true
      run: |
        7z x ${{ steps.sqlite.outputs.filepath }}
        mv sqlite-amalgamation-3500100 sqlite3

    - name: Download miniaudio
      run: |
        git clone --depth=1 https://github.com/mercury233/miniaudio
        cd miniaudio
        cp extras/miniaudio_split/miniaudio.* .
        cd ..

    - name: Download ogg
      if: matrix.static-link == true
      id: ogg
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/ogg/releases/download/v1.3.5/libogg-1.3.5.tar.gz

    - name: Extract ogg
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.ogg.outputs.filepath }}
        mv libogg-1.3.5 miniaudio/external/ogg

    - name: Download opus
      if: matrix.static-link == true
      id: opus
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/opus/releases/download/v1.5.2/opus-1.5.2.tar.gz

    - name: Extract opus
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.opus.outputs.filepath }}
        mv opus-1.5.2 miniaudio/external/opus

    - name: Download opusfile
      if: matrix.static-link == true
      id: opusfile
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/opusfile/releases/download/v0.12/opusfile-0.12.tar.gz

    - name: Extract opusfile
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.opusfile.outputs.filepath }}
        mv opusfile-0.12 miniaudio/external/opusfile

    - name: Download vorbis
      if: matrix.static-link == true
      id: vorbis
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://github.com/xiph/vorbis/releases/download/v1.3.7/libvorbis-1.3.7.tar.gz

    - name: Extract vorbis
      if: matrix.static-link == true
      run: |
        tar xf ${{ steps.vorbis.outputs.filepath }}
        mv libvorbis-1.3.7 miniaudio/external/vorbis

    - name: Download irrlicht
      run: |
        git clone --depth=1 https://github.com/mercury233/irrlicht

    - name: Copy premake files
      run: |
        cp -pr premake/* .
        cp -pr resource/* .

    - name: Use premake to generate make files (Homebrew packages)
      if: matrix.static-link != true
      run: |
        DYLD_LIBRARY_PATH=$(brew --prefix)/lib ./premake5 gmake

    - name: Use premake to generate make files (static link)
      if: matrix.static-link == true
      run: |
        ./premake5 gmake ${{ matrix.cross-build-intel == true && '--mac-intel' || '' }} ${{ matrix.cross-build-arm == true && '--mac-arm' || '' }} \
          --build-event \
          --build-freetype \
          --build-sqlite \
          --build-opus-vorbis

    - name: Make
      run: |
        cd build
        make -j 3 config=release
        cd ..

    - name: Upload build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: YGOPro-${{ matrix.name }}
        path: |
          bin/release/YGOPro
