1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-10 01:22:25 +01:00
Files
OpenRCT2/openrct2.deps.targets
2025-09-28 09:52:35 +02:00

297 lines
12 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- DownloadDependency task -->
<UsingTask TaskName="DownloadDependency"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Name Required="true" ParameterType="System.String" />
<Url Required="true" ParameterType="System.String" />
<Sha256 Required="true" ParameterType="System.String" />
<CheckFile Required="false" ParameterType="System.String" />
<OutputDirectory Required="true" ParameterType="System.String" />
</ParameterGroup>
<Task>
<Reference Include="System.IO.Compression, Version=4.0.0.0" />
<Reference Include="System.IO.Compression.FileSystem" />
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.IO.Compression"/>
<Using Namespace="System.Net"/>
<Using Namespace="System.Text"/>
<Using Namespace="Microsoft.Build.Framework"/>
<Using Namespace="Microsoft.Build.Utilities"/>
<Code Type="Method" Language="cs">
<![CDATA[
public override bool Execute()
{
if (!String.IsNullOrEmpty(CheckFile))
{
string checkSha256 = GetSha256FromCheckFile(CheckFile, Name);
if (String.Equals(checkSha256, Sha256, StringComparison.OrdinalIgnoreCase) && Directory.Exists(OutputDirectory))
{
Log.LogMessage(MessageImportance.Normal, String.Format("{0} up to date", Name));
return true;
}
}
string tempFile = Path.GetTempFileName();
try
{
// Download the file
Log.LogMessage(MessageImportance.High, String.Format("Downloading '{0}'.", Url));
var client = new WebClient();
client.DownloadFile(Url, tempFile);
// Check the file matches
string actualSha256;
if (!CheckFileSha256(tempFile, Sha256, out actualSha256))
{
Log.LogError("Download file did not match expected SHA256\n expected: {0}\n actual: {1}", Sha256, actualSha256);
return false;
}
// Extract contents
Log.LogMessage(MessageImportance.High, String.Format("Extracting to '{0}'.", OutputDirectory));
if (!Directory.Exists(OutputDirectory))
{
Directory.CreateDirectory(OutputDirectory);
}
ExtractZip(tempFile, OutputDirectory, overwrite: true);
SetSha256InCheckFile(CheckFile, Name, Sha256);
}
catch (Exception ex)
{
Log.LogErrorFromException(ex, showStackTrace: false);
}
finally
{
try
{
File.Delete(tempFile);
}
catch
{
}
}
return true;
}
private string GetSha256FromCheckFile(string checkFile, string name)
{
string result = null;
try
{
if (File.Exists(checkFile))
{
string[] lines = File.ReadAllLines(checkFile);
string sha256;
GetCheckFileLineIndexSha256(lines, name, out sha256);
return sha256;
}
}
catch (Exception ex)
{
Log.LogWarningFromException(ex, showStackTrace: false);
}
return result;
}
private void SetSha256InCheckFile(string checkFile, string name, string sha256)
{
try
{
string newLine = String.Format("{0} = {1}", name, sha256.ToLower());
string[] lines = new string[0];
int lineIndex = -1;
if (File.Exists(checkFile))
{
lines = File.ReadAllLines(checkFile);
string oldsha256;
lineIndex = GetCheckFileLineIndexSha256(lines, name, out oldsha256);
}
if (lineIndex == -1)
{
if (lines.Length == 0 || lines[lines.Length - 1].Trim().Length > 0)
{
Array.Resize(ref lines, lines.Length + 1);
}
lineIndex = lines.Length - 1;
// End with new line
Array.Resize(ref lines, lines.Length + 1);
}
lines[lineIndex] = newLine;
File.WriteAllLines(checkFile, lines);
}
catch (Exception ex)
{
Log.LogWarningFromException(ex, showStackTrace: false);
}
}
private int GetCheckFileLineIndexSha256(string[] lines, string name, out string sha256)
{
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
string[] lineParts = line.Split('=');
if (lineParts.Length == 2)
{
string lineTag = lineParts[0].Trim();
string lineSha256 = lineParts[1].Trim();
if (lineTag == name)
{
sha256 = lineSha256;
return i;
}
}
}
sha256 = null;
return -1;
}
private bool CheckFileSha256(string file, string expectedSha256, out string actualSha256)
{
using (var fs = new FileStream(file, FileMode.Open))
{
var hasher = System.Security.Cryptography.SHA256.Create();
byte[] hash = hasher.ComputeHash(fs);
actualSha256 = BytesToHexString(hash);
if (String.Equals(actualSha256, expectedSha256, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private string BytesToHexString(byte[] data)
{
var sb = new StringBuilder();
foreach (byte b in data)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
private static void ExtractZip(string zipPath, string destinationDirectory, bool overwrite)
{
var archive = ZipFile.OpenRead(zipPath);
if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectory);
return;
}
foreach (ZipArchiveEntry file in archive.Entries)
{
string fileName = Path.Combine(destinationDirectory, file.FullName);
string directory = Path.GetDirectoryName(fileName);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (file.Name != String.Empty)
{
file.ExtractToFile(fileName, true);
}
}
}
]]>
</Code>
</Task>
</UsingTask>
<!-- 3rd party libraries / dependencies -->
<PropertyGroup>
<RootDir>$(MsBuildThisFileDirectory)</RootDir>
<DependenciesCheckFile>$(RootDir).dependencies</DependenciesCheckFile>
<LibsUrl Condition="'$(Platform)'=='ARM64'">https://github.com/OpenRCT2/Dependencies/releases/download/v41/openrct2-libs-v41-arm64-windows-static.zip</LibsUrl>
<LibsSha256 Condition="'$(Platform)'=='ARM64'">5d171f23eee584bdc29b1186dc7a86d935a6ea58efa42b5674c196c5b581923b</LibsSha256>
<LibsUrl Condition="'$(Platform)'=='x64'">https://github.com/OpenRCT2/Dependencies/releases/download/v41/openrct2-libs-v41-x64-windows-static.zip</LibsUrl>
<LibsSha256 Condition="'$(Platform)'=='x64'">8e78f1c3c09c50cb0f509eab6845a93dd69771ff95cdf82d747568093d138f73</LibsSha256>
<LibsUrl Condition="'$(Platform)'=='Win32'">https://github.com/OpenRCT2/Dependencies/releases/download/v41/openrct2-libs-v41-x86-windows-static.zip</LibsUrl>
<LibsSha256 Condition="'$(Platform)'=='Win32'">490263b873dd02c13a043a04d26bb9837d2d378dacd8b2c8d29660fef44db3db</LibsSha256>
<TitleSequencesUrl>https://github.com/OpenRCT2/title-sequences/releases/download/v0.4.26/title-sequences.zip</TitleSequencesUrl>
<TitleSequencesSha256>dabb9787b1576342fca4dd9f64b3f8cfa04a7e6ce9c2bb9610f47b762905c858</TitleSequencesSha256>
<ObjectsUrl>https://github.com/OpenRCT2/objects/releases/download/v1.7.3/objects.zip</ObjectsUrl>
<ObjectsSha256>c81029264578706ed1db88665e12a70a583e71dc4d3eb4db262535d2f0589eab</ObjectsSha256>
<OpenSFXUrl>https://github.com/OpenRCT2/OpenSoundEffects/releases/download/v1.0.6/opensound.zip</OpenSFXUrl>
<OpenSFXSha256>06b90f3e19c216752df441d551b26a9e3e1ba7755bdd2102504b73bf993608be</OpenSFXSha256>
<OpenMSXUrl>https://github.com/OpenRCT2/OpenMusic/releases/download/v1.6.1/openmusic.zip</OpenMSXUrl>
<OpenMSXSha256>994b350d3b180ee1cb9619fe27f7ebae3a1a5232840c4bd47a89f33fa89de1a1</OpenMSXSha256>
<ReplaysUrl>https://github.com/OpenRCT2/replays/releases/download/v0.0.90/replays.zip</ReplaysUrl>
<ReplaysSha256>f8474a927e155056e5729b6fa9f05af2a85ae7e1435f5fa89ba496242f9f255e</ReplaysSha256>
</PropertyGroup>
<!-- Unified Dependency Target -->
<Target Name="DownloadAllDependencies"
BeforeTargets="PrepareForBuild"
DependsOnTargets="DownloadLibs;DownloadTitleSequences;DownloadObjects;DownloadOpenSFX;DownloadOpenMSX;DownloadReplays">
<!-- Add completion marker -->
<Touch Files="$(DependenciesCheckFile)" AlwaysCreate="true" />
</Target>
<!-- Target Implementations -->
<Target Name="DownloadLibs">
<!-- libs -->
<DownloadDependency Name="Libs"
Url="$(LibsUrl)"
Sha256="$(LibsSha256)"
CheckFile="$(DependenciesCheckFile)"
OutputDirectory="$(RootDir)lib\$(Platform)" />
</Target>
<!-- Target to download the title sequences -->
<Target Name="DownloadTitleSequences">
<DownloadDependency Name="TitleSequences"
Url="$(TitleSequencesUrl)"
Sha256="$(TitleSequencesSha256)"
CheckFile="$(DependenciesCheckFile)"
OutputDirectory="$(TargetDir)data\sequence" />
</Target>
<!-- Target to download the objects -->
<Target Name="DownloadObjects">
<DownloadDependency Name="Objects"
Url="$(ObjectsUrl)"
Sha256="$(ObjectsSha256)"
CheckFile="$(DependenciesCheckFile)"
OutputDirectory="$(TargetDir)data\object" />
</Target>
<!-- Target to download OpenSFX -->
<Target Name="DownloadOpenSFX">
<DownloadDependency Name="OpenSFX"
Url="$(OpenSFXUrl)"
Sha256="$(OpenSFXSha256)"
CheckFile="$(DependenciesCheckFile)"
OutputDirectory="$(TargetDir)data" />
</Target>
<!-- Target to download OpenMSX -->
<Target Name="DownloadOpenMSX">
<DownloadDependency Name="OpenMSX"
Url="$(OpenMSXUrl)"
Sha256="$(OpenMSXSha256)"
CheckFile="$(DependenciesCheckFile)"
OutputDirectory="$(TargetDir)data" />
</Target>
<!-- Target to download replays -->
<Target Name="DownloadReplays">
<DownloadDependency Name="Replays"
Url="$(ReplaysUrl)"
Sha256="$(ReplaysSha256)"
CheckFile="$(DependenciesCheckFile)"
OutputDirectory="$(TargetDir)testdata\replays" />
</Target>
</Project>