<?xml version="1.0" encoding="utf-8"?>
<!-- Main msbuild project for OpenRCT2 -->
<!-- Inputs (/p: or environment variables)
       - OPENRCT2_BUILD_SERVER
       - GIT_TAG
       - GIT_BRANCH
       - GIT_COMMIT_SHA1
       - SIGN_PASSWORD
       - OPENRCT2_ORG_TOKEN
       - NO_NSIS: set to true to prevent the NSIS installer building
  -->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="PublishAll">
  <!-- Import custom build tasks -->
  <Import Project="openrct2.targets" />

  <PropertyGroup>
    <Configuration Condition="'$(Configuration)'==''">Debug</Configuration>
    <Platform Condition="'$(PLATFORM)'==''">x64</Platform>
    <GIT_COMMIT_SHA1_SHORT Condition="'$(GIT_COMMIT_SHA1)'!=''">$(GIT_COMMIT_SHA1.Substring(0, 7))</GIT_COMMIT_SHA1_SHORT>

    <Version>0.1.3</Version>
    <VersionExtra Condition="'$(GIT_BRANCH)'!=''">-$(GIT_BRANCH)-$(GIT_COMMIT_SHA1_SHORT)</VersionExtra>
    <VersionExtra Condition="'$(GIT_TAG)'!=''"></VersionExtra>
    <TargetLibsVersion>16</TargetLibsVersion>

    <!-- Set dynamic OpenRCT2 #defines -->
    <OPENRCT2_CL_ADDITIONALOPTIONS Condition="'$(BUILD_SERVER)'!=''">/D "OPENRCT2_BUILD_SERVER=\"$(BUILD_SERVER)\"" $(OPENRCT2_CL_ADDITIONALOPTIONS)</OPENRCT2_CL_ADDITIONALOPTIONS>
    <OPENRCT2_CL_ADDITIONALOPTIONS Condition="'$(GIT_BRANCH)'!=''">/D "OPENRCT2_BRANCH=\"$(GIT_BRANCH)\"" $(OPENRCT2_CL_ADDITIONALOPTIONS)</OPENRCT2_CL_ADDITIONALOPTIONS>
    <OPENRCT2_CL_ADDITIONALOPTIONS Condition="'$(GIT_COMMIT_SHA1)'!=''">/D "OPENRCT2_COMMIT_SHA1=\"$(GIT_COMMIT_SHA1)\"" $(OPENRCT2_CL_ADDITIONALOPTIONS)</OPENRCT2_CL_ADDITIONALOPTIONS>
    <OPENRCT2_CL_ADDITIONALOPTIONS Condition="'$(GIT_COMMIT_SHA1_SHORT)'!=''">/D "OPENRCT2_COMMIT_SHA1_SHORT=\"$(GIT_COMMIT_SHA1_SHORT)\"" $(OPENRCT2_CL_ADDITIONALOPTIONS)</OPENRCT2_CL_ADDITIONALOPTIONS>

    <RootDir>$(MsBuildThisFileDirectory)</RootDir>
    <DistDir>$(RootDir)distribution\</DistDir>
    <ArtifactsDir>$(RootDir)artifacts\</ArtifactsDir>
    <TargetDir>$(RootDir)bin\</TargetDir>

    <NsisScript>$(DistDir)windows\install.nsi</NsisScript>

    <OutputExe>$(TargetDir)openrct2.exe</OutputExe>
    <OutputCom>$(TargetDir)openrct2.com</OutputCom>
    <OutputDll>$(TargetDir)openrct2.dll</OutputDll>
    <g2Output>$(TargetDir)data\g2.dat</g2Output>

    <SignCertificate Condition="'$(SignCertificate)'==''">$(DistDir)windows\code-sign-key-openrct2.org.pfx</SignCertificate>
    <SignTimestampUrl>http://timestamp.comodoca.com/authenticode</SignTimestampUrl>

    <PublishPostfix>-$(Configuration)-$(Platform)</PublishPostfix>
    <PublishPostfix Condition="'$(GIT_COMMIT_SHA1_SHORT)'!=''">$(PublishPostfix)-$(GIT_COMMIT_SHA1_SHORT)</PublishPostfix>
    <PublishPostfix>$(PublishPostfix.ToLower())</PublishPostfix>
    <PublishZip>$(ArtifactsDir)openrct2-portable$(PublishPostfix).zip</PublishZip>
    <PublishSymbolsZip Condition="'$(GIT_COMMIT_SHA1_SHORT)'==''">$(ArtifactsDir)openrct2-symbols$(PublishPostfix).zip</PublishSymbolsZip>
    <PublishSymbolsZip Condition="'$(GIT_COMMIT_SHA1_SHORT)'!=''">$(ArtifactsDir)openrct2-symbols$(PublishPostfix).zip</PublishSymbolsZip>
    <PublishInstallerExe>$(ArtifactsDir)openrct2-installer$(PublishPostfix).exe</PublishInstallerExe>

    <UploadFilename>OpenRCT2-$(Version)$(VersionExtra)-windows-$(Platform.ToLower())</UploadFilename>

    <!-- Set openrct2.sln properties -->
    <SlnProperties>$(SlnProperties);Platform=$(Platform)</SlnProperties>
    <SlnProperties Condition="'$(BUILD_SERVER)'!=''">$(SlnProperties);OPENRCT2_CL_ADDITIONALOPTIONS=$(OPENRCT2_CL_ADDITIONALOPTIONS)</SlnProperties>
  </PropertyGroup>

  <!-- 3rd party libraries / dependencies -->
  <PropertyGroup>
    <DependenciesCheckFile>$(RootDir).dependencies</DependenciesCheckFile>
    <LibsUrl Condition="'$(Platform)'=='Win32'">https://github.com/OpenRCT2/Dependencies/releases/download/v16/openrct2-libs-v16-x86-windows-static-winssl.zip</LibsUrl>
    <LibsSha1 Condition="'$(Platform)'=='Win32'">827d86c1571bb73504b23929746b20d8094aae40</LibsSha1>
    <LibsUrl Condition="'$(Platform)'=='x64'">https://github.com/OpenRCT2/Dependencies/releases/download/v16/openrct2-libs-v16-x64-windows-static-winssl.zip</LibsUrl>
    <LibsSha1 Condition="'$(Platform)'=='x64'">5904b7e0562ace419b7c87d618e5ad00ba664b36</LibsSha1>
    <GtestVersion>1.8.0</GtestVersion>
    <GtestUrl>https://github.com/google/googletest/archive/release-1.8.0.zip</GtestUrl>
    <GtestSha1>667f873ab7a4d246062565fad32fb6d8e203ee73</GtestSha1>
    <TitleSequencesUrl>https://github.com/OpenRCT2/title-sequences/releases/download/v0.1.2/title-sequence-v0.1.2.zip</TitleSequencesUrl>
    <TitleSequencesSha1>1136ef92bfb05cd1cba9831ba6dc4a653d87a246</TitleSequencesSha1>
  </PropertyGroup>

  <ItemGroup>
    <g2Inputs Include="$(RootDir)resources\g2\*" />
    <SignItems Include="$(OutputExe)" />
    <SignItems Include="$(OutputCom)" />
    <SignItems Include="$(OutputDll)" />
  </ItemGroup>

  <ItemGroup Label="Symbol Items">
    <SymbolItems Include="$(OutputExe)" />
    <SymbolItems Include="$(OutputCom)" />
    <SymbolItems Include="$(OutputDll)" />
    <SymbolItems Include="$(TargetDir)openrct2-dll.pdb" />
    <SymbolItems Include="$(TargetDir)openrct2-win.pdb" />
  </ItemGroup>

  <ItemGroup Label="Publish Items">
    <PublishItems Include="$(OutputExe)" />
    <PublishItems Include="$(OutputCom)" />
    <PublishItems Include="$(OutputDll)" />
    <PublishItems Include="$(TargetDir)data" />
    <PublishItems Include="$(DistDir)changelog.txt" />
    <PublishItems Include="$(DistDir)readme.txt" />
    <PublishItems Include="$(RootDir)contributors.md" />
    <PublishItems Include="$(RootDir)licence.txt" />
  </ItemGroup>

  <ItemGroup Label="Upload Artifacts">
    <UploadArtifacts Include="$(PublishZip)">
      <Name>$(UploadFilename).zip</Name>
      <FlavourId Condition="'$(Platform)'=='Win32'">1</FlavourId>
      <FlavourId Condition="'$(Platform)'=='x64'">6</FlavourId>
    </UploadArtifacts>
    <UploadArtifacts Include="$(PublishInstallerExe)">
      <Name>$(UploadFilename).exe</Name>
      <FlavourId Condition="'$(Platform)'=='Win32'">2</FlavourId>
      <FlavourId Condition="'$(Platform)'=='x64'">7</FlavourId>
    </UploadArtifacts>
    <UploadArtifacts Include="$(PublishSymbolsZip)">
      <Name>$(UploadFilename)-symbols.zip</Name>
      <FlavourId Condition="'$(Platform)'=='Win32'">5</FlavourId>
      <FlavourId Condition="'$(Platform)'=='x64'">10</FlavourId>
    </UploadArtifacts>
  </ItemGroup>

  <Target Name="DownloadLibs">
    <!-- libs -->
    <DownloadDependency Name="Libs"
                        Url="$(LibsUrl)"
                        Sha1="$(LibsSha1)"
                        CheckFile="$(DependenciesCheckFile)"
                        OutputDirectory="$(RootDir)lib\$(Platform)" />

    <!-- googletest -->
    <DownloadDependency Name="googletest"
                        Url="$(GtestUrl)"
                        Sha1="$(GtestSha1)"
                        CheckFile="$(DependenciesCheckFile)"
                        OutputDirectory="$(RootDir)lib" />
    <PropertyGroup>
      <GtestSrc>$(RootDir)lib\googletest-release-$(GtestVersion)</GtestSrc>
      <GtestDst>$(RootDir)lib\googletest</GtestDst>
    </PropertyGroup>
    <Exec Command="cmd /c &quot;if exist &quot;$(GtestSrc)&quot; ( rmdir /S /Q &quot;$(GtestDst)&quot; 2> nul &amp; move &quot;$(GtestSrc)&quot; &quot;$(GtestDst)&quot; )&quot;" />
  </Target>

  <Target Name="Clean">
    <PropertyGroup>
      <SlnProperties>$(SlnProperties);Configuration=$(Configuration)</SlnProperties>
    </PropertyGroup>
    <ItemGroup>
      <CleanItems Include="$(OutputCom)" />
      <CleanItems Include="$(g2Output)" />
      <CleanItems Include="$(ArtifactsDir)openrct2-installer-$(Configuration)-$(Platform)*.exe" />
      <CleanItems Include="$(ArtifactsDir)openrct2-portable-$(Configuration)-$(Platform)*.zip" />
      <CleanItems Include="$(ArtifactsDir)openrct2-symbols-$(Configuration)-$(Platform)*.zip" />
    </ItemGroup>
    <Delete Files="@(CleanItems)" />
    <Delete Files="%(SignItems.Identity).signed" />
    <RemoveDir Directories="$(TargetDir)data" />
    <MSBuild Projects="openrct2.sln" Targets="Clean" Properties="$(SlnProperties)" />
  </Target>

  <Target Name="BeforeBuild" BeforeTargets="Build;Rebuild" DependsOnTargets="DownloadLibs">
    <PropertyGroup>
      <BuildString Condition="'$(GIT_COMMIT_SHA1_SHORT)'!=''">$(GIT_COMMIT_SHA1_SHORT)</BuildString>
      <BuildString Condition="'$(GIT_BRANCH)'!=''">$(BuildString) ($(GIT_BRANCH))</BuildString>
    </PropertyGroup>
    <ItemGroup>
      <DataItems Include="$(RootDir)data\**\*" />
    </ItemGroup>
    <Message Condition="'$(BuildString)'!=''" Text="Building $(BuildString)" Importance="high" />
    <Copy SourceFiles="@(CopyItems)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" />
    <Copy SourceFiles="@(DataItems)" DestinationFolder="$(TargetDir)data\%(RecursiveDir)" SkipUnchangedFiles="true" />
  </Target>

  <Target Name="Build">
    <PropertyGroup>
      <SlnProperties>$(SlnProperties);Configuration=$(Configuration)</SlnProperties>
    </PropertyGroup>
    <Message Text="SlnProperties: $(SlnProperties)" />
    <MSBuild Projects="openrct2.sln" Targets="Build" Properties="$(SlnProperties)" />
  </Target>
  <Target Name="Rebuild">
    <PropertyGroup>
      <SlnProperties>$(SlnProperties);Configuration=$(Configuration)</SlnProperties>
    </PropertyGroup>
    <Message Text="SlnProperties: $(SlnProperties)" />
    <MSBuild Projects="openrct2.sln" Targets="Rebuild" Properties="$(SlnProperties)" />
  </Target>

  <Target Name="Test" DependsOnTargets="Build">
    <Exec Command="$(TargetDir)tests\tests.exe &quot;--gtest_output=xml:$(ArtifactsDir)test-results.xml&quot;" />
  </Target>
  <Target Name="TestNoRCT2" DependsOnTargets="Build">
    <Exec Command="$(TargetDir)tests\tests.exe &quot;--gtest_filter=-RideRatings*:MultiLaunch*&quot; &quot;--gtest_output=xml:$(ArtifactsDir)test-results.xml&quot;" />
  </Target>

  <!-- Target to build g2.dat containing OpenRCT2 sprites -->
  <Target Name="g2" DependsOnTargets="Build" AfterTargets="Build" Inputs="@(g2Inputs)" Outputs="$(g2Output)"
          Condition="'$(TestConfig)'!='true'">
    <Message Text="Building g2.dat..." Importance="high" />
    <Exec Command="&quot;$(OutputExe)&quot; sprite build &quot;$(g2Output)&quot; &quot;$(RootDir)resources\g2\sprites.json&quot;"
          StandardOutputImportance="normal" />
  </Target>

  <!-- Target to create openrct2.exe (windows) and openrct2.com (console) -->
  <Target Name="CreateGUI" DependsOnTargets="Build" AfterTargets="Build" Inputs="$(OutputExe)" Outputs="$(OutputCom)">
    <Copy SourceFiles="$(OutputExe)" DestinationFiles="$(OutputCom)" />
    <Exec Command="editbin /subsystem:console &quot;$(OutputCom)&quot;" />
    <Exec Command="editbin /subsystem:windows &quot;$(OutputExe)&quot;" />
  </Target>

  <!-- Target to download the title sequences -->
  <Target Name="DownloadTitleSequences" AfterTargets="Build">
    <DownloadDependency Name="TitleSequences"
                        Url="$(TitleSequencesUrl)"
                        Sha1="$(TitleSequencesSha1)"
                        CheckFile="$(DependenciesCheckFile)"
                        OutputDirectory="$(TargetDir)data\title" />
  </Target>

  <!-- Target to sign OpenRCT2
       This requires the project parameter SIGN_PASSWORD to be set -->
  <Target Name="Sign" AfterTargets="Build" Inputs="@(SignItems)" Outputs="%(SignItems.Identity).signed"
          Condition="'$(TestConfig)'!='true'">
    <Warning Condition="'$(SIGN_PASSWORD)'==''" Text="SIGN_PASSWORD was not set, skipping signing of %(SignItems.Filename)%(SignItems.Extension)." />
    <Message Condition="'$(SIGN_PASSWORD)'!=''" Text="Signing %(SignItems.Filename)%(SignItems.Extension)"
             Importance="high" />
    <Exec EchoOff="true" Condition="'$(SIGN_PASSWORD)'!=''"
          Command="signtool.exe sign /f $(SignCertificate) /p $(SIGN_PASSWORD) /t $(SignTimestampUrl) %(SignItems.Identity)"
          StandardOutputImportance="low" />

    <!-- Create a dummy file so that msbuild can use it to determine if the binary is newer than the last sign time -->
    <Touch AlwaysCreate="true" Files="%(SignItems.Identity).signed" />
  </Target>

  <!-- Target to publish OpenRCT2 as a portable zip -->
  <Target Name="PublishPortable" DependsOnTargets="Build;g2" Inputs="@(PublishItems)" Outputs="$(PublishZip)"
          Condition="'$(TestConfig)'!='true'">
    <MakeDir Directories="$(ArtifactsDir)" />
    <Message Importance="high" Text="Creating $([System.IO.Path]::GetFileName($(PublishZip)))..." />
    <_7z Output="$(PublishZip)" Inputs="@(PublishItems)" />
  </Target>

  <!-- Target to publish the OpenRCT2 debug symbols -->
  <Target Name="PublishSymbols" DependsOnTargets="Build" Inputs="@(SymbolItems)" Outputs="$(PublishSymbolsZip)"
          Condition="'$(TestConfig)'!='true'">
    <MakeDir Directories="$(ArtifactsDir)" />
    <Message Importance="high" Text="Creating $([System.IO.Path]::GetFileName($(PublishSymbolsZip)))..." />
    <_7z Output="$(PublishSymbolsZip)" Inputs="@(SymbolItems)" />
  </Target>

  <!-- Target to publish the OpenRCT2 as an NSIS installer -->
  <Target Name="PublishInstaller"
          DependsOnTargets="Build;g2"
          Inputs="@(PublishItems);$(NsisScript)"
          Outputs="$(PublishInstallerExe)"
          Condition="'$(NO_NSIS)'!='true' AND '$(TestConfig)'!='true'">
    <PropertyGroup>
      <PublishInstallerExeName>$([System.IO.Path]::GetFilename($(PublishInstallerExe)))</PublishInstallerExeName>
    </PropertyGroup>

    <MakeDir Directories="$(ArtifactsDir)" />

    <!-- Create the installer -->
    <Message Text="Building Windows Installer (NSIS script)" Importance="high" />
    <Exec Command="makensis /DOUTFILE=$(PublishInstallerExe) ^
         /DAPPV_MAIN=$(Version) ^
         /DAPPV_EXTRA=$(VersionExtra) ^
         /DPLATFORM=$(Platform) ^
         $(NsisScript)"
          StandardOutputImportance="normal" />

    <!-- Sign the installer -->
    <Warning Condition="'$(SIGN_PASSWORD)'==''" Text="SIGN_PASSWORD was not set, skipping signing of $(PublishInstallerExeName)." />
    <Message Condition="'$(SIGN_PASSWORD)'!=''" Text="Signing $(PublishInstallerExeName)"
             Importance="high" />
    <Exec EchoOff="true" Condition="'$(SIGN_PASSWORD)'!=''"
          Command="signtool.exe sign /f $(SignCertificate) /p $(SIGN_PASSWORD) /t $(SignTimestampUrl) $(PublishInstallerExe)"
          StandardOutputImportance="low" />
  </Target>

  <Target Name="PublishAll"
          DependsOnTargets="PublishSymbols;PublishPortable;PublishInstaller" />

  <!-- Target to upload the artifacts to OpenRCT2.org -->
  <Target Name="UploadArtifacts" DependsOnTargets="PublishAll"
          Condition="'$(TestConfig)'!='true'">
    <PropertyGroup>
      <UploadLink>https://openrct2.org/altapi/?command=push-build</UploadLink>
      <UploadFileNameShort>%(UploadArtifacts.Filename)%(UploadArtifacts.Extension)</UploadFileNameShort>
      <UploadGitBranch>master</UploadGitBranch>
      <UploadGitBranch Condition="'$(GIT_BRANCH)'!=''">$(GIT_BRANCH)</UploadGitBranch>
    </PropertyGroup>
    <Message Text="Uploading $(UploadFileNameShort)" Importance="high" />
    <Error Condition="'$(OPENRCT2_ORG_TOKEN)'==''" Text="OPENRCT2_ORG_TOKEN not set." />
    <Error Condition="'$(GIT_COMMIT_SHA1)'==''" Text="GIT_COMMIT_SHA1 not set." />
    <Exec EchoOff="true" Command="curl.exe -s -o - ^
         --form &quot;key=$(OPENRCT2_ORG_TOKEN)&quot; ^
         --form &quot;fileName=%(UploadArtifacts.Name)&quot; ^
         --form &quot;version=$(Version)&quot; ^
         --form &quot;gitHash=$(GIT_COMMIT_SHA1)&quot; ^
         --form &quot;gitBranch=$(UploadGitBranch)&quot; ^
         --form &quot;flavourId=%(UploadArtifacts.FlavourId)&quot; ^
         --form &quot;file=@%(UploadArtifacts.Identity)&quot; ^
         &quot;$(UploadLink)&quot;"
          CustomErrorRegularExpression="&quot;error&quot;:1" />
  </Target>

</Project>
