name: Release (Windows Store)

on:
  workflow_call:
    inputs:
      version:
        required: true
        type: string

jobs:
  windows-store:
    name: Windows Store

    runs-on: windows-latest

    steps:
    - name: Download source
      uses: actions/download-artifact@v4
      with:
        name: internal-source

    - name: Unpack source
      shell: bash
      run: |
        tar -xf source.tar.gz --strip-components=1

    - name: Download x86 build
      uses: actions/download-artifact@v4
      with:
        name: openttd-windows-x86

    - name: Download x64 build
      uses: actions/download-artifact@v4
      with:
        name: openttd-windows-x64

    - name: Download arm64 build
      uses: actions/download-artifact@v4
      with:
        name: openttd-windows-arm64

    - name: Unpack builds
      shell: bash
      run: |
        mkdir builds
        cd builds

        function extract {
            mkdir $1

            # Extract the zip version of the release
            unzip ../openttd-*-windows-$2.zip -d $1

            # Remove the extraneous directory
            mv $1/openttd-*-windows-$2/* $1/
            rmdir $1/openttd-*-windows-$2

            # Move the openttd.exe to the '{arch}-binaries' folder
            mkdir $1-binaries
            mv $1/openttd.exe $1-binaries/
        }

        extract x86 win32
        extract x64 win64
        extract arm64 arm64

        # Use the "x86" folder as the source of the common binaries (lang files, etc) and remove the others
        mv x86 common-binaries
        rm -rf x64 arm64

    - name: Install OpenGFX
      shell: bash
      run: |
        mkdir -p builds/common-binaries/baseset
        cd builds/common-binaries/baseset

        echo "::group::Download OpenGFX"
        curl -L https://cdn.openttd.org/opengfx-releases/7.1/opengfx-7.1-all.zip -o opengfx-all.zip
        echo "::endgroup::"

        echo "::group::Unpack OpenGFX"
        unzip opengfx-all.zip
        tar xf opengfx-*.tar
        echo "::endgroup::"

        rm -f opengfx-all.zip opengfx-*.tar

    - name: Install OpenMSX
      shell: bash
      run: |
        mkdir -p builds/common-binaries/baseset
        cd builds/common-binaries/baseset

        echo "::group::Download OpenMSX"
        curl -L https://cdn.openttd.org/openmsx-releases/0.4.2/openmsx-0.4.2-all.zip -o openmsx-all.zip
        echo "::endgroup::"

        echo "::group::Unpack OpenGFX"
        unzip openmsx-all.zip
        tar xf openmsx-*.tar
        echo "::endgroup::"

        rm -f openmsx-all.zip openmsx-*.tar

    - name: Install OpenSFX
      shell: bash
      run: |
        mkdir -p builds/common-binaries/baseset/opensfx
        cd builds/common-binaries/baseset/opensfx

        echo "::group::Download OpenSFX"
        curl -L https://cdn.openttd.org/opensfx-releases/1.0.3/opensfx-1.0.3-all.zip -o opensfx-all.zip
        echo "::endgroup::"

        echo "::group::Unpack OpenSFX"
        unzip opensfx-all.zip
        tar xf opensfx-*.tar
        echo "::endgroup::"

        rm -f opensfx-all.zip opensfx-*.tar

    - name: Generate signing certificate
      shell: cmd
      run: |
        cd builds

        REM We need to provide a signed .appx to the Windows Store, so generate a certificate with password 'password'
        call ..\os\windows\winstore\generate-key.bat "CN=78024DA8-4BE4-4C77-B12E-547BBF7359D2" password cert.pfx

    - name: Generate assets
      shell: cmd
      run: |
        cd os\windows\winstore
        call generate-assets.bat

    - name: Prepare manifests
      shell: cmd
      run: |
        cd builds
        mkdir manifests

        REM Set the version environment variable
        call ..\os\windows\winstore\set-version.bat x86-binaries\openttd.exe

        call ..\os\windows\winstore\prepare-manifests.bat manifests "CN=78024DA8-4BE4-4C77-B12E-547BBF7359D2" "57420OpenTTDDevelopers.OpenTTDofficial"

    - name: Prepare binaries
      shell: bash
      run: |
        cd builds

        # Copy the Windows Store assets
        mkdir common-binaries/Assets
        cp -R ../os/windows/winstore/assets-common/* common-binaries/Assets/

        mkdir Assets
        cp -R ../os/windows/winstore/assets/* Assets/

        cp manifests/*.xml .

    - name: Build
      shell: cmd
      run: |
        REM Add the Windows SDK tools to the PATH

        echo|set /p="SET VS_INSTALLDIR=" > _vspath.bat
        vswhere -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath >> _vspath.bat
        call _vspath.bat
        call "%VS_INSTALLDIR%\Common7\Tools\VsDevCmd.bat"

        REM Set the version environment variable
        call os\windows\winstore\set-version.bat builds\x86-binaries\openttd.exe

        cd builds
        mkdir output

        REM Build and sign the package
        makeappx build /v /f PackagingLayout.xml /op output\ /bv %OTTD_VERSION% /pv %OTTD_VERSION% /ca
        SignTool sign /fd sha256 /a /f cert.pfx /p password "output\OpenTTD.appxbundle"

        REM Move resulting files to bundles folder
        mkdir bundles
        mkdir bundles\internal
        move cert.pfx bundles\internal\openttd-${{ inputs.version }}-windows-store.pfx
        move output\OpenTTD.appxbundle bundles\internal\openttd-${{ inputs.version }}-windows-store.appxbundle

    - name: Store bundles
      uses: actions/upload-artifact@v4
      with:
        name: openttd-windows-store
        path: builds/bundles
        retention-days: 5
