Skip to content

ReStreet: FIFA Street (2012) recompiled for Windows PC, with online play, a launcher with friends, chat and rooms. Bring your own game.

14.3 KB
Raw Blame
param(
    [Parameter(Mandatory = $true)]
    [string]$GameData,

    [Parameter(Mandatory = $true)]
    [string]$Output,

    [string]$Workspace,

    [switch]$SourceBuild,

    [ValidateSet('D3D12', 'Vulkan')]
    [string]$GraphicsApi = 'D3D12',

    [ValidateRange(1, 64)]
    [int]$Jobs = 2
)

$ErrorActionPreference = "Stop"
[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)
$OutputEncoding = [Console]::OutputEncoding

function Invoke-BuildTool {
    param([string]$Tool, [string[]]$Arguments)
    $ErrorActionPreference = 'Continue'
    $nativeLog = $logPath + '.native'
    $writer = [IO.StreamWriter]::new($nativeLog, $true, [Text.UTF8Encoding]::new($false))
    try {
        $writer.WriteLine("$Tool " + ($Arguments -join ' '))
        & $Tool @Arguments 2>&1 | ForEach-Object {
            $line = if ($_ -is [Management.Automation.ErrorRecord]) { $_.Exception.Message } else { [string]$_ }
            $writer.WriteLine($line)
            if ($line -match '^\[\d+|^\s*(start|phase|done)\s|FAILED:|fatal error:|error:|^Failed:|Codegen summary:|^Done in') {
                Write-Host $line
            }
        }
        $exitCode = $LASTEXITCODE
    }
    finally { $writer.Dispose() }
    if ($exitCode -ne 0) { throw "$Tool failed with code $exitCode. Full log: $nativeLog" }
}

$installerRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
. (Join-Path $installerRoot "BuildIntegrity.ps1")

$template = Join-Path $installerRoot "recomp-template"

$rexGlueSource = if ($env:REXSDK_DIR) { [IO.Path]::GetFullPath($env:REXSDK_DIR) } else {
    Join-Path (Split-Path $installerRoot -Parent) "ReXGlue"
}

$bundledSdk = Join-Path $installerRoot 'sdk'
$bundledTools = Join-Path $installerRoot 'compiler'
$useBundledSdk = !$SourceBuild -and (Test-Path -LiteralPath (Join-Path $bundledSdk 'bin\rexglue.exe'))
$llvmBin = if (Test-Path -LiteralPath $bundledTools) { Join-Path $bundledTools 'bin' } else { "C:\Program Files\LLVM\bin" }

$clang = Join-Path $llvmBin "clang.exe"
$clangxx = Join-Path $llvmBin "clang++.exe"

if (!(Test-Path $clang)) {
    throw "clang.exe not found at $clang"
}

if (!(Test-Path $clangxx)) {
    throw "clang++.exe not found at $clangxx"
}

$env:PATH = "$llvmBin;$env:PATH"
if ($useBundledSdk) { $env:PATH = "$(Join-Path $bundledSdk 'bin');$env:PATH" }

$shortTempRoot = if ($env:FIFA_BUILD_ROOT) { $env:FIFA_BUILD_ROOT } else { "C:\FSTRB" }

if (!(Test-Path $shortTempRoot)) {
    New-Item -ItemType Directory -Path $shortTempRoot -Force | Out-Null
}

$resume = ![string]::IsNullOrWhiteSpace($Workspace)
$workRoot = if ($resume) { [IO.Path]::GetFullPath($Workspace) } else {
    Join-Path $shortTempRoot ([guid]::NewGuid().ToString("N").Substring(0, 8))
}

$workProject = Join-Path $workRoot "FifaStreetRex"
if ($resume -and !(Test-Path -LiteralPath (Join-Path $workProject 'CMakeLists.txt'))) {
    throw "Invalid build workspace: $workRoot"
}
$GameData = [IO.Path]::GetFullPath($GameData)
$Output = [IO.Path]::GetFullPath($Output)

Write-Host ""
Write-Host "======================================"
Write-Host " FIFA Street PC - Recomp Build"
Write-Host "======================================"
Write-Host ""

if (!(Test-Path $GameData)) {
    throw "GameData does not exist: $GameData"
}

$defaultXex = Join-Path $GameData "default.xex"
$fifaDll = Join-Path $GameData "fifadllzf.xex.dll"
$footballCompDll = Join-Path $GameData "dlc\dlc_FootballCompEng\dlc\FootballCompEng\FootballCompEngzf.xex.dll"

if (!(Test-Path $defaultXex)) {
    throw "default.xex was not found."
}

if (!(Test-Path $fifaDll)) {
    throw "fifadllzf.xex.dll was not found."
}

if (!(Test-Path $footballCompDll)) {
    throw "FootballCompEngzf.xex.dll was not found. This module is required for World Tour."
}

if (!(Test-Path $template)) {
    throw "recomp-template was not found: $template"
}

if (!$useBundledSdk -and !(Test-Path $rexGlueSource)) {
    throw "ReXGlue source was not found: $rexGlueSource"
}

Write-Host "[1/6] Preparing temporary workspace..."

New-Item -ItemType Directory -Path $workProject -Force | Out-Null

Get-ChildItem -LiteralPath $template | Where-Object { $_.Name -ne 'fifastreet_manifest.toml' } |
    Copy-Item -Destination $workProject -Recurse -Force

$logPath = Join-Path $workRoot 'build.log'
Write-Host "Workspace: $workRoot"
Write-Host "Log: $logPath"
Write-Host "Full diagnostics: $logPath.native"
Start-Transcript -Path $logPath -Append | Out-Null
trap {
    Write-Host $_.ToString()
    Stop-Transcript -ErrorAction SilentlyContinue | Out-Null
    throw $_
}

Write-Host "[2/6] Preparing manifest..."

$manifestPath = Join-Path $workProject "fifastreet_manifest.toml"

$gameDataUnix = $GameData.Replace("\", "/").Replace('"', '\"')

$manifest = @"
[project]
name = "fifastreet"
sdk_version = "0.10.0"
game_root = "$gameDataUnix"

[entrypoint]
file_path = "$gameDataUnix/default.xex"
out_directory_path = "generated/default"
includes = []

[[modules]]
guest_path = "game:\\fifadllzf.xex.dll"
file_path = "$gameDataUnix/fifadllzf.xex.dll"
out_directory_path = "generated/fifadllzf_xex"
includes = ["config/fifadllzf_overrides.toml"]

[[modules]]
guest_path = "game:\\FootballCompEngzf.xex.dll"
file_path = "$gameDataUnix/dlc/dlc_FootballCompEng/dlc/FootballCompEng/FootballCompEngzf.xex.dll"
out_directory_path = "generated/FootballCompEngzf_xex"
includes = ["config/footballcompeng_overrides.toml"]
"@

$manifest = $manifest.Replace("`r`n", "`n")
$manifestChanged = !$resume -or !(Test-Path -LiteralPath $manifestPath) -or [IO.File]::ReadAllText($manifestPath).Replace("`r`n", "`n").Trim() -ne $manifest.Trim()
if ($manifestChanged) {
    [System.IO.File]::WriteAllText($manifestPath, $manifest, [System.Text.UTF8Encoding]::new($false))
}

Write-Host ""
Write-Host "Manifest:"
Get-Content $manifestPath
Write-Host ""

Write-Host "[3/6] Cleaning previous generated code..."

$generatedDefault = Join-Path $workProject "generated\default"
$generatedDll = Join-Path $workProject "generated\fifadllzf_xex"
$generatedFootballComp = Join-Path $workProject "generated\FootballCompEngzf_xex"

if (!$resume -and (Test-Path $generatedDefault)) {
    if (![IO.Path]::GetFullPath($generatedDefault).StartsWith($workProject + '\', [StringComparison]::OrdinalIgnoreCase)) { throw 'Unsafe generated path' }
    Remove-Item $generatedDefault -Recurse -Force
}

if (!$resume -and (Test-Path $generatedDll)) {
    if (![IO.Path]::GetFullPath($generatedDll).StartsWith($workProject + '\', [StringComparison]::OrdinalIgnoreCase)) { throw 'Unsafe generated path' }
    Remove-Item $generatedDll -Recurse -Force
}

if (!$resume -and (Test-Path $generatedFootballComp)) {
    if (![IO.Path]::GetFullPath($generatedFootballComp).StartsWith($workProject + '\', [StringComparison]::OrdinalIgnoreCase)) { throw 'Unsafe generated path' }
    Remove-Item $generatedFootballComp -Recurse -Force
}

Write-Host "[4/6] Configuring build..."

Push-Location $workProject

try {

    # Generate sources before configuration so CMake discovers both binaries.
    $codegen = if ($useBundledSdk) { Join-Path $bundledSdk 'bin\rexglue.exe' } else { Join-Path $rexGlueSource 'out\win-amd64\Release\rexglue.exe' }
    if (!$useBundledSdk -and !(Test-Path -LiteralPath $codegen)) { $codegen = Join-Path $rexGlueSource 'out\win-amd64\rexglue.exe' }
    if (!(Test-Path -LiteralPath $codegen)) { throw "ReXGlue code generator not found: $codegen" }
    $fingerprintPath = Join-Path $workProject 'codegen-inputs.sha256'
    $fingerprintInputs = @($manifestPath, $codegen)
    $fingerprintInputs += @(Get-ChildItem -LiteralPath $GameData -Filter '*.xex*' -File -Recurse | ForEach-Object { $_.FullName })
    $fingerprintInputs += @(Get-ChildItem -LiteralPath (Join-Path $workProject 'config') -File -Recurse | ForEach-Object { $_.FullName })
    $fingerprint = Get-CodegenFingerprint $fingerprintInputs
    $inputsChanged = !(Test-Path -LiteralPath $fingerprintPath) -or [IO.File]::ReadAllText($fingerprintPath).Trim() -ne $fingerprint
    if ($manifestChanged -or $inputsChanged -or !(Test-Path -LiteralPath (Join-Path $generatedDefault 'sources.cmake')) -or
        !(Test-Path -LiteralPath (Join-Path $generatedDll 'sources.cmake')) -or
        !(Test-Path -LiteralPath (Join-Path $generatedFootballComp 'sources.cmake'))) {
        if (Test-Path -LiteralPath $fingerprintPath) { Remove-Item -LiteralPath $fingerprintPath -Force }
        Invoke-BuildTool $codegen @('--log-file', (Join-Path $workRoot 'codegen.log'), 'codegen', $manifestPath)
        [IO.File]::WriteAllText($fingerprintPath, $fingerprint, [Text.UTF8Encoding]::new($false))
    }

    $sdkArguments = if ($useBundledSdk) { @('-DREXSDK_DIR=', "-DCMAKE_PREFIX_PATH=$bundledSdk") } else {
        @("-DREXSDK_DIR=$rexGlueSource", "-DREXGLUE_USE_D3D12=$($GraphicsApi -eq 'D3D12')", "-DREXGLUE_USE_VULKAN=$($GraphicsApi -eq 'Vulkan')",
          "-DREXGLUE_OUTPUT_DIRECTORY=$workProject/out/build/win-amd64-release/runtime")
    }
    Invoke-BuildTool 'cmake' (@('--preset', 'win-amd64-release') + $sdkArguments + @(
        "-DCMAKE_C_COMPILER=$clang",
        "-DCMAKE_CXX_COMPILER=$clangxx", '-DCMAKE_C_FLAGS=-mssse3', '-DCMAKE_CXX_FLAGS=-mssse3')
    )

    Write-Host ""
    Write-Host "[5/6] Compiling FIFA Street..."
    Write-Host ""

    Invoke-BuildTool 'cmake' @('--build', '--preset', 'win-amd64-release', '--parallel', "$Jobs")

}
finally {
    Pop-Location
}

$buildFolder = Join-Path $workProject "out\build\win-amd64-release"

$exe = Join-Path $buildFolder "fifastreet.exe"

$dll = Join-Path $buildFolder "fifastreet_fifadllzf_xex.dll"
$footballCompOut = Join-Path $buildFolder "fifastreet_FootballCompEngzf_xex.dll"

if (!(Test-Path $exe)) {
    throw "The build finished but fifastreet.exe was not found."
}

if (!(Test-Path $dll)) {
    throw "The build finished but fifastreet_fifadllzf_xex.dll was not found."
}

if (!(Test-Path $footballCompOut)) {
    throw "The build finished but fifastreet_FootballCompEngzf_xex.dll was not found."
}

Write-Host ""
Write-Host "[6/6] Installing game binaries..."

New-Item `
    -ItemType Directory `
    -Path $Output `
    -Force | Out-Null

Copy-Item $exe $Output -Force

# Copy the recompiled game modules.
Get-ChildItem -LiteralPath $buildFolder -Filter '*.dll' -File |
    Where-Object { $_.Name -notin @('rexruntime.dll', 'rexgpu-xenos.dll') } |
    Copy-Item -Destination $Output -Force

# Source builds use the paired runtime/GPU DLLs compiled by this invocation.
if (!$useBundledSdk) {
    . (Join-Path $installerRoot 'InstallSourceBackend.ps1')
    Install-SourceBackend -RuntimeDirectory (Join-Path $buildFolder 'runtime') -Output $Output -GraphicsApi $GraphicsApi
    Stop-Transcript | Out-Null
    Write-Host "Source build complete: $Output ($GraphicsApi)"
    return
}

# Install both validated graphics backends.
$bundledBackends = Join-Path $bundledSdk 'backends'
$outputBackends = Join-Path $Output 'Backends'

New-Item -ItemType Directory -Path $outputBackends -Force | Out-Null

$expectedBackends = @{
    'D3D12' = @{
        RuntimeHash = '790B6B1B13765249160E53DCE6F28AF0F03B66D5D1AEDB1054949C50B13F04E8'
        GpuHash = '461A88F1D27C605106453B0F3C92BD72BD4BC166DCDC0BBF01E825BCEFB8418D'
    }
    'Vulkan' = @{
        RuntimeHash = '115722CC5905D07FC6A6212B47692FFE6405DD1BA9F87CDA97F8DA2C25E4767A'
        GpuHash = '71FA61D82FF6134F1F407D682ACEBAC01F2B3B2DCEFCC34151A0C082B746BDD2'
    }
}

foreach ($backendName in @('D3D12', 'Vulkan')) {
    # New bundles carry the exact backend manifest verified during packaging.
    # Retain the legacy constants above for historical v0.3 bundles.
    $backendManifestPath = Join-Path $installerRoot 'backend-manifest.json'
    if (Test-Path -LiteralPath $backendManifestPath) {
        $backendManifest = Get-Content -LiteralPath $backendManifestPath -Raw | ConvertFrom-Json
        $entry = $backendManifest.backends.$backendName
        if (!$entry -or $entry.runtimeHash -notmatch '^[a-fA-F0-9]{64}$' -or $entry.gpuHash -notmatch '^[a-fA-F0-9]{64}$') {
            throw "Invalid backend integrity manifest: $backendName"
        }
        $expectedBackends[$backendName] = @{ RuntimeHash = $entry.runtimeHash; GpuHash = $entry.gpuHash }
    }
    $sourceBackend = Join-Path $bundledBackends $backendName
    $destinationBackend = Join-Path $outputBackends $backendName

    $sourceRuntime = Join-Path $sourceBackend 'rexruntime.dll'
    $sourceGpu = Join-Path $sourceBackend 'rexgpu-xenos.dll'

    if (!(Test-Path -LiteralPath $sourceRuntime)) {
        throw "$backendName rexruntime.dll was not found: $sourceRuntime"
    }

    if (!(Test-Path -LiteralPath $sourceGpu)) {
        throw "$backendName rexgpu-xenos.dll was not found: $sourceGpu"
    }

    $runtimeHash = Get-InputSha256 $sourceRuntime
    $gpuHash = Get-InputSha256 $sourceGpu

    if ($runtimeHash -ne $expectedBackends[$backendName].RuntimeHash) {
        throw "$backendName rexruntime.dll is not the validated runtime."
    }

    if ($gpuHash -ne $expectedBackends[$backendName].GpuHash) {
        throw "$backendName rexgpu-xenos.dll is not the validated GPU backend."
    }

    New-Item -ItemType Directory -Path $destinationBackend -Force | Out-Null

    Copy-Item -LiteralPath $sourceRuntime `
        -Destination (Join-Path $destinationBackend 'rexruntime.dll') `
        -Force

    Copy-Item -LiteralPath $sourceGpu `
        -Destination (Join-Path $destinationBackend 'rexgpu-xenos.dll') `
        -Force
}

# D3D12 is the launcher's default graphics API, so install it as the
# initially active backend beside fifastreet.exe.
Copy-Item `
    -LiteralPath (Join-Path $outputBackends 'D3D12\rexruntime.dll') `
    -Destination (Join-Path $Output 'rexruntime.dll') `
    -Force

Copy-Item `
    -LiteralPath (Join-Path $outputBackends 'D3D12\rexgpu-xenos.dll') `
    -Destination (Join-Path $Output 'rexgpu-xenos.dll') `
    -Force
Write-Host ""
Write-Host "======================================"
Write-Host " BUILD COMPLETE"
Write-Host "======================================"
Write-Host ""

Write-Host "Created:"
Write-Host "  $(Join-Path $Output 'fifastreet.exe')"
Write-Host "  $(Join-Path $Output 'fifastreet_fifadllzf_xex.dll')"
Write-Host "  $(Join-Path $Output 'fifastreet_FootballCompEngzf_xex.dll')"

Write-Host ""
Write-Host "Temporary workspace:"
Write-Host $workRoot
Stop-Transcript | Out-Null