name: Automated Test Build

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

jobs:
  build-windows:
    strategy:
      fail-fast: false
      matrix:
        os: [windows-2019, windows-2022]
        audiolib: [miniaudio, irrklang]

    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-beta5/premake-5.0.0-beta5-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.0.22-stable/libevent-2.0.22-stable.tar.gz
        filename: libevent.tar.gz

    - name: Extract libevent
      run: |
        tar xf ${{ steps.libevent.outputs.filepath }}
        move libevent-2.0.22-stable event
        xcopy /E event\WIN32-Code event\include

    - name: Download freetype
      id: freetype
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: http://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.7.tar.gz

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

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

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

    - name: Download miniaudio
      if: matrix.audiolib == 'miniaudio'
      run: |
        git clone --depth=1 --branch 0.11.22 https://github.com/mackron/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
      id: dxsdk
      uses: actions/cache@v4
      with:
        key: dxsdk
        path: DXSDK

    - name: Download DirectX SDK
      if: 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: steps.dxsdk.outputs.cache-hit != 'true'
      run: |
        7z x ${{ steps.dxsdk-download.outputs.filepath }} -aoa

    - name: Set DirectX SDK environment variable
      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 (2019, miniaudio)
      if: matrix.os == 'windows-2019' && matrix.audiolib == 'miniaudio'
      run: |
        .\premake5.exe vs2019 --winxp-support

    - name: Use premake to generate Visual Studio solution (2022, miniaudio)
      if: matrix.os == 'windows-2022' && matrix.audiolib == 'miniaudio'
      run: |
        .\premake5.exe vs2022

    - name: Use premake to generate Visual Studio solution (2019, irrKlang)
      if: matrix.os == 'windows-2019' && matrix.audiolib == 'irrklang'
      run: |
        .\premake5.exe vs2019 --winxp-support --audio-lib=irrklang

    - name: Use premake to generate Visual Studio solution (2022, irrKlang)
      if: matrix.os == 'windows-2022' && matrix.audiolib == 'irrklang'
      run: |
        .\premake5.exe vs2022 --audio-lib=irrklang

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

    - name: Build solution
      run: |
        MSBuild.exe build\YGOPro.sln /m /p:Configuration=Release

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

  build-linux:
    strategy:
      fail-fast: false
      matrix:
        os:
#          - ubuntu-20.04
          - ubuntu-22.04
          - ubuntu-24.04
        include:
#          - os: ubuntu-20.04
#            premake_version: 5.0.0-beta2
          - os: ubuntu-22.04
            premake_version: 5.0.0-beta4
          - os: ubuntu-24.04
            premake_version: 5.0.0-beta5

    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 libevent-dev libfreetype6-dev libgl1-mesa-dev libglu1-mesa-dev libsqlite3-dev libxxf86vm-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 lua
      id: lua
      uses: mercury233/action-cache-download-file@v1.0.0
      with:
        url: https://www.lua.org/ftp/lua-5.4.7.tar.gz

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

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

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

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

    - name: Use premake to generate make files
      run: |
        ./premake5 gmake \
          --freetype-include-dir="/usr/include/freetype2" \
          --opus-include-dir="/usr/include/opus"

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

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

  build-macos:
    strategy:
      fail-fast: false
      matrix:
        os:
          - macos-13
          - macos-15
        include:
          - os: macos-13
            premake_version: 5.0.0-beta5
          - os: macos-15
            premake_version: 5.0.0-beta5

    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: |
        brew install freetype libevent libx11 sqlite opus opusfile libvorbis

    - 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 }}-macosx.tar.gz
        filename: premake5.tar.gz

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

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

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

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

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

    - name: Build irrlicht
      run: |
        cd irrlicht/source/Irrlicht/MacOSX
        xcodebuild -project MacOSX.xcodeproj
        cd ../../../..

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

    - name: Use premake to generate make files (Intel)
      if: runner.arch == 'X64'
      run: |
        ./premake5 gmake \
          --cc=clang \
          --freetype-include-dir="/usr/local/include/freetype2" \
          --opus-include-dir="/usr/local/include/opus" \
          --irrlicht-include-dir="../irrlicht/include" \
          --irrlicht-lib-dir="../irrlicht/source/Irrlicht/MacOSX/build/Release"

    - name: Use premake to generate make files (ARM64)
      if: runner.arch == 'ARM64'
      run: |
        ./premake5 gmake \
          --cc=clang \
          --event-include-dir="/opt/homebrew/include" \
          --event-lib-dir="/opt/homebrew/lib" \
          --freetype-include-dir="/opt/homebrew/include/freetype2" \
          --freetype-lib-dir="/opt/homebrew/lib" \
          --sqlite-include-dir="/opt/homebrew/opt/sqlite/include" \
          --sqlite-lib-dir="/opt/homebrew/opt/sqlite/lib" \
          --miniaudio-include-dir="/opt/homebrew/include" \
          --miniaudio-lib-dir="/opt/homebrew/lib" \
          --opus-include-dir="/opt/homebrew/include/opus" \
          --opus-lib-dir="/opt/homebrew/lib" \
          --vorbis-include-dir="/opt/homebrew/include" \
          --vorbis-lib-dir="/opt/homebrew/lib" \
          --irrlicht-include-dir="../irrlicht/include" \
          --irrlicht-lib-dir="../irrlicht/source/Irrlicht/MacOSX/build/Release"

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

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