forked from nkast/MonoGame
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
BuildEvent.cs
460 lines (370 loc) · 16.1 KB
/
BuildEvent.cs
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
namespace Microsoft.Xna.Framework.Content.Pipeline.Builder
{
public class BuildEvent
{
public const string Extension = ".kniContent";
private static readonly OpaqueDataDictionary EmptyParameters = new OpaqueDataDictionary();
public BuildEvent()
{
SourceFile = string.Empty;
DestFile = string.Empty;
Importer = string.Empty;
Processor = string.Empty;
Parameters = new OpaqueDataDictionary();
Dependencies = new List<string>();
BuildAsset = new List<string>();
BuildOutput = new List<string>();
}
/// <summary>
/// Absolute path to the source file.
/// </summary>
public string SourceFile { get; set; }
/// <summary>
/// The date/time stamp of the source file.
/// </summary>
public DateTime SourceTime { get; set; }
/// <summary>
/// Absolute path to the output file.
/// </summary>
public string DestFile { get; set; }
/// <summary>
/// The date/time stamp of the destination file.
/// </summary>
public DateTime DestTime { get; set; }
public string Importer { get; set; }
/// <summary>
/// The date/time stamp of the DLL containing the importer.
/// </summary>
public DateTime ImporterTime { get; set; }
public string Processor { get; set; }
/// <summary>
/// The date/time stamp of the DLL containing the processor.
/// </summary>
public DateTime ProcessorTime { get; set; }
public OpaqueDataDictionary Parameters { get; set; }
/// <summary>
/// Gets or sets the dependencies.
/// </summary>
/// <value>The dependencies.</value>
/// <remarks>
/// Dependencies are extra files that are required in addition to the <see cref="SourceFile"/>.
/// Dependencies are added using <see cref="ContentProcessorContext.AddDependency"/>. Changes
/// to the dependent file causes a rebuilt of the content.
/// </remarks>
public List<string> Dependencies { get; set; }
/// <summary>
/// Gets or sets the additional (nested) assets.
/// </summary>
/// <value>The additional (nested) assets.</value>
/// <remarks>
/// <para>
/// Additional assets are built by using an <see cref="ExternalReference{T}"/> and calling
/// <see cref="ContentProcessorContext.BuildAndLoadAsset{TInput,TOutput}(ExternalReference{TInput},string)"/>
/// or <see cref="ContentProcessorContext.BuildAsset{TInput,TOutput}(ExternalReference{TInput},string)"/>.
/// </para>
/// <para>
/// Examples: The mesh processor may build textures and effects in addition to the mesh.
/// </para>
/// </remarks>
public List<string> BuildAsset { get; set; }
/// <summary>
/// Gets or sets the related output files.
/// </summary>
/// <value>The related output files.</value>
/// <remarks>
/// Related output files are non-XNB files that are included in addition to the XNB files.
/// Related output files need to be copied to the output folder by a content processor and
/// registered by calling <see cref="ContentProcessorContext.AddOutputFile"/>.
/// </remarks>
public List<string> BuildOutput { get; set; }
public void SaveBinary(string filePath)
{
// Make sure the directory exists.
Directory.CreateDirectory(Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar);
using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
using (var writer = new PipelineBuildEventBinaryWriter(stream))
{
writer.Write(this);
}
}
public static BuildEvent LoadBinary(string filePath)
{
try
{
using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
using (var writer = new PipelineBuildEventBinaryReader(stream))
{
BuildEvent result = new BuildEvent();
writer.Read(result);
return result;
}
}
catch (Exception)
{
return null;
}
}
internal bool NeedsRebuild(PipelineManager manager, BuildEvent cachedEvent)
{
// If we have no previously cached build event then we cannot
// be sure that the state hasn't changed... force a rebuild.
if (cachedEvent == null)
return true;
// Verify that the last write time of the source file matches
// what we recorded when it was built. If it is different
// that means someone modified it and we need to rebuild.
DateTime sourceWriteTime = File.GetLastWriteTime(SourceFile);
if (cachedEvent.SourceTime != sourceWriteTime)
return true;
// Do the same test for the dest file.
DateTime destWriteTime = File.GetLastWriteTime(DestFile);
if (cachedEvent.DestTime != destWriteTime)
return true;
// If the source file is newer than the dest file
// then it must have been updated and needs a rebuild.
if (sourceWriteTime >= destWriteTime)
return true;
// Are any of the dependancy files newer than the dest file?
foreach (string depFile in cachedEvent.Dependencies)
{
if (File.GetLastWriteTime(depFile) >= destWriteTime)
return true;
}
// This shouldn't happen... but if the source or dest files changed
// then force a rebuild.
if (cachedEvent.SourceFile != SourceFile
|| cachedEvent.DestFile != DestFile)
return true;
// Did the importer change?
if (cachedEvent.Importer != Importer)
return true;
// Did the processor change?
if (cachedEvent.Processor != Processor)
return true;
// Did the importer assembly change?
DateTime importerAssemblyTimestamp = manager.GetImporterAssemblyTimestamp(cachedEvent.Importer);
if (importerAssemblyTimestamp > cachedEvent.ImporterTime)
return true;
// Did the processor assembly change?
DateTime processorAssemblyTimestamp = manager.GetProcessorAssemblyTimestamp(cachedEvent.Processor);
if (processorAssemblyTimestamp > cachedEvent.ProcessorTime)
return true;
// Did the parameters change?
OpaqueDataDictionary defaultValues = manager.GetProcessorDefaultValues(Processor);
if (!AreParametersEqual(cachedEvent.Parameters, Parameters, defaultValues))
return true;
return false;
}
internal static bool AreParametersEqual(OpaqueDataDictionary parameters0, OpaqueDataDictionary parameters1, OpaqueDataDictionary defaultValues)
{
Debug.Assert(defaultValues != null, "defaultValues must not be empty.");
Debug.Assert(EmptyParameters != null && EmptyParameters.Count == 0);
// Same reference or both null?
if (parameters0 == parameters1)
return true;
if (parameters0 == null)
parameters0 = EmptyParameters;
if (parameters1 == null)
parameters1 = EmptyParameters;
// Are both dictionaries empty?
if (parameters0.Count == 0 && parameters1.Count == 0)
return true;
// Compare the values with the second dictionary or
// the default values.
if (parameters0.Count < parameters1.Count)
{
OpaqueDataDictionary dummy = parameters0;
parameters0 = parameters1;
parameters1 = dummy;
}
// Compare parameters0 with parameters1 or defaultValues.
foreach (KeyValuePair<string, object> pair in parameters0)
{
object value0 = pair.Value;
object value1;
// Search for matching parameter.
if (!parameters1.TryGetValue(pair.Key, out value1) && !defaultValues.TryGetValue(pair.Key, out value1))
return false;
if (!AreEqual(value0, value1))
return false;
}
// Compare parameters which are only in parameters1 with defaultValues.
foreach (KeyValuePair<string, object> pair in parameters1)
{
if (parameters0.ContainsKey(pair.Key))
continue;
object defaultValue;
if (!defaultValues.TryGetValue(pair.Key, out defaultValue))
return false;
if (!AreEqual(pair.Value, defaultValue))
return false;
}
return true;
}
private static bool AreEqual(object value0, object value1)
{
// Are values equal or both null?
if (Equals(value0, value1))
return true;
// Is one value null?
if (value0 == null || value1 == null)
return false;
// Values are of different type: Compare string representation.
if (ConvertToString(value0) != ConvertToString(value1))
return false;
return true;
}
private static string ConvertToString(object value)
{
if (value == null)
return null;
TypeConverter typeConverter = TypeDescriptor.GetConverter(value.GetType());
return typeConverter.ConvertToInvariantString(value);
}
internal class PipelineBuildEventBinaryWriter : BinaryWriter
{
private const string Header = "KNIC"; // content db
private const short MajorVersion = 3;
private const short MinorVersion = 15;
private const short DataType = 2; // PipelineBuildEvent data
public PipelineBuildEventBinaryWriter(Stream output) : base(output)
{
}
internal void Write(BuildEvent value)
{
Write(Header.ToCharArray());
Write((Int16)MajorVersion);
Write((Int16)MinorVersion);
Write((Int32)DataType);
Write((Int32)0); // reserved
WriteStringOrNull(value.SourceFile);
WriteDateTime(value.SourceTime);
WriteStringOrNull(value.DestFile);
WriteDateTime(value.DestTime);
WriteStringOrNull(value.Importer);
WriteDateTime(value.ImporterTime);
WriteStringOrNull(value.Processor);
WriteDateTime(value.ProcessorTime);
WritePackedInt(value.Parameters.Count);
foreach (var param in value.Parameters)
{
WriteStringOrNull(param.Key);
WriteStringOrNull(ConvertToString(param.Value));
}
WritePackedInt(value.Dependencies.Count);
for (int i = 0; i < value.Dependencies.Count; i++)
WriteStringOrNull(value.Dependencies[i]);
WritePackedInt(value.BuildAsset.Count);
for (int i = 0; i < value.BuildAsset.Count; i++)
WriteStringOrNull(value.BuildAsset[i]);
WritePackedInt(value.BuildOutput.Count);
for (int i = 0; i < value.BuildOutput.Count; i++)
WriteStringOrNull(value.BuildOutput[i]);
return;
}
protected void WritePackedInt(int value)
{
// write zigzag encoded int
int zzint = ((value << 1) ^ (value >> 31));
Write7BitEncodedInt(zzint);
}
private void WriteStringOrNull(string value)
{
if (value != null)
{
Write(true);
Write(value);
}
else
Write(false);
}
private void WriteDateTime(DateTime value)
{
Write((Int64)value.ToBinary());
}
}
internal class PipelineBuildEventBinaryReader : BinaryReader
{
private const string Header = "KNIC"; // content db
private const short MajorVersion = 3;
private const short MinorVersion = 15;
private const int DataType = 2; // PipelineBuildEvent data
public PipelineBuildEventBinaryReader(Stream output) : base(output)
{
}
internal void Read(BuildEvent value)
{
if (ReadByte() != Header[0]
|| ReadByte() != Header[1]
|| ReadByte() != Header[2]
|| ReadByte() != Header[3])
throw new Exception("Invalid file.");
if (ReadInt16() != MajorVersion
|| ReadInt16() != MinorVersion)
throw new Exception("Invalid file version.");
int dataType = ReadInt32();
if (dataType != DataType)
throw new Exception("Invalid data type.");
int reserved0 = ReadInt32();
value.SourceFile = ReadStringOrNull();
value.SourceTime = ReadDateTime();
value.DestFile = ReadStringOrNull();
value.DestTime = ReadDateTime();
value.Importer = ReadStringOrNull();
value.ImporterTime = ReadDateTime();
value.Processor = ReadStringOrNull();
value.ProcessorTime = ReadDateTime();
int parametersCount = ReadPackedInt();
value.Parameters = new OpaqueDataDictionary();
for (int i = 0; i < parametersCount; i++)
{
value.Parameters.Add(
ReadStringOrNull(),
ReadStringOrNull());
}
int dependenciesCount = ReadPackedInt();
value.Dependencies = new List<string>(dependenciesCount);
for (int i = 0; i < dependenciesCount; i++)
value.Dependencies.Add(ReadStringOrNull());
int buildAssetCount = ReadPackedInt();
value.BuildAsset = new List<string>(buildAssetCount);
for (int i = 0; i < buildAssetCount; i++)
value.BuildAsset.Add(ReadStringOrNull());
int buildOutputCount = ReadPackedInt();
value.BuildOutput = new List<string>(buildOutputCount);
for (int i = 0; i < buildOutputCount; i++)
value.BuildOutput.Add(ReadStringOrNull());
return;
}
private int ReadPackedInt()
{
unchecked
{
// read zigzag encoded int
int zzint = Read7BitEncodedInt();
return ((int)((uint)zzint >> 1) ^ (-(zzint & 1)));
}
}
private string ReadStringOrNull()
{
if (ReadBoolean())
return ReadString();
else
return null;
}
private DateTime ReadDateTime()
{
return DateTime.FromBinary(ReadInt64());
}
}
}
}