forked from OpenRCT2/OpenRCT2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenrct2.deps.targets
More file actions
389 lines (354 loc) · 16.1 KB
/
openrct2.deps.targets
File metadata and controls
389 lines (354 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- ReadAssetsJson task: Parse assets.json and set properties -->
<UsingTask TaskName="ReadAssetsJson"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<JsonFilePath Required="true" ParameterType="System.String" />
<TitleSequencesUrl Required="false" ParameterType="System.String" Output="true" />
<TitleSequencesSha256 Required="false" ParameterType="System.String" Output="true" />
<ObjectsUrl Required="false" ParameterType="System.String" Output="true" />
<ObjectsSha256 Required="false" ParameterType="System.String" Output="true" />
<OpenSFXUrl Required="false" ParameterType="System.String" Output="true" />
<OpenSFXSha256 Required="false" ParameterType="System.String" Output="true" />
<OpenMusicUrl Required="false" ParameterType="System.String" Output="true" />
<OpenMusicSha256 Required="false" ParameterType="System.String" Output="true" />
<ReplaysUrl Required="false" ParameterType="System.String" Output="true" />
<ReplaysSha256 Required="false" ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Memory" />
<Reference Include="System.Text.Json" />
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text.Json"/>
<Using Namespace="Microsoft.Build.Framework"/>
<Using Namespace="Microsoft.Build.Utilities"/>
<Code Type="Method" Language="cs">
<![CDATA[
public override bool Execute()
{
try
{
if (!File.Exists(JsonFilePath))
{
Log.LogWarning("Assets JSON file not found: {0}", JsonFilePath);
return true;
}
string jsonContent = File.ReadAllText(JsonFilePath);
using (JsonDocument doc = JsonDocument.Parse(jsonContent))
{
JsonElement root = doc.RootElement;
TitleSequencesUrl = GetAssetProperty(root, "title-sequences", "url");
TitleSequencesSha256 = GetAssetProperty(root, "title-sequences", "sha256");
ObjectsUrl = GetAssetProperty(root, "objects", "url");
ObjectsSha256 = GetAssetProperty(root, "objects", "sha256");
OpenSFXUrl = GetAssetProperty(root, "opensfx", "url");
OpenSFXSha256 = GetAssetProperty(root, "opensfx", "sha256");
OpenMusicUrl = GetAssetProperty(root, "openmusic", "url");
OpenMusicSha256 = GetAssetProperty(root, "openmusic", "sha256");
ReplaysUrl = GetAssetProperty(root, "replays", "url");
ReplaysSha256 = GetAssetProperty(root, "replays", "sha256");
}
Log.LogMessage(MessageImportance.Low, "Successfully read asset definitions from {0}", JsonFilePath);
}
catch (Exception ex)
{
Log.LogErrorFromException(ex, showStackTrace: true);
return false;
}
return true;
}
private string GetAssetProperty(JsonElement assets, string assetName, string propertyName)
{
if (assets.TryGetProperty(assetName, out JsonElement asset))
{
if (asset.TryGetProperty(propertyName, out JsonElement property))
{
return property.GetString() ?? string.Empty;
}
}
Log.LogWarning("Could not find property '{0}' in asset '{1}'", propertyName, assetName);
return string.Empty;
}
]]>
</Code>
</Task>
</UsingTask>
<!-- DownloadDependency task -->
<UsingTask TaskName="DownloadDependency"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.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>
<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 loaded from assets.json -->
<PropertyGroup>
<RootDir>$(MsBuildThisFileDirectory)</RootDir>
<DependenciesCheckFile>$(RootDir).dependencies</DependenciesCheckFile>
<AssetsJsonPath>$(RootDir)assets.json</AssetsJsonPath>
<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>
</PropertyGroup>
<!-- Load asset definitions from JSON first, before any download targets run -->
<Target Name="LoadAssetsJson">
<ReadAssetsJson
JsonFilePath="$(AssetsJsonPath)"
Condition="Exists('$(AssetsJsonPath)')">
<Output TaskParameter="TitleSequencesUrl" PropertyName="TitleSequencesUrl" />
<Output TaskParameter="TitleSequencesSha256" PropertyName="TitleSequencesSha256" />
<Output TaskParameter="ObjectsUrl" PropertyName="ObjectsUrl" />
<Output TaskParameter="ObjectsSha256" PropertyName="ObjectsSha256" />
<Output TaskParameter="OpenSFXUrl" PropertyName="OpenSFXUrl" />
<Output TaskParameter="OpenSFXSha256" PropertyName="OpenSFXSha256" />
<Output TaskParameter="OpenMusicUrl" PropertyName="OpenMusicUrl" />
<Output TaskParameter="OpenMusicSha256" PropertyName="OpenMusicSha256" />
<Output TaskParameter="ReplaysUrl" PropertyName="ReplaysUrl" />
<Output TaskParameter="ReplaysSha256" PropertyName="ReplaysSha256" />
</ReadAssetsJson>
</Target>
<!-- Unified Dependency Target -->
<Target Name="DownloadAllDependencies"
BeforeTargets="PrepareForBuild"
DependsOnTargets="LoadAssetsJson;DownloadLibs;DownloadTitleSequences;DownloadObjects;DownloadOpenSFX;DownloadOpenMusic;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 OpenMusic -->
<Target Name="DownloadOpenMusic">
<DownloadDependency Name="OpenMusic"
Url="$(OpenMusicUrl)"
Sha256="$(OpenMusicSha256)"
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>