forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildContent.cs
409 lines (349 loc) · 16.4 KB
/
BuildContent.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
// 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.IO;
using System.Linq;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Framework.Content.Pipeline.Builder;
namespace MGCB
{
class BuildContent
{
[CommandLineParameter(
Name = "launchdebugger",
Description = "Wait for debugger to attach before building content.")]
public bool LaunchDebugger = false;
[CommandLineParameter(
Name = "quiet",
Description = "Only output content build errors.")]
public bool Quiet = false;
[CommandLineParameter(
Name = "@",
ValueName = "responseFile",
Description = "Read a text response file with additional command line options and switches.")]
// This property only exists for documentation.
// The actual handling of '/@' is done in the preprocess step.
public List<string> ResponseFiles
{
get { throw new InvalidOperationException(); }
set { throw new InvalidOperationException(); }
}
[CommandLineParameter(
Name = "workingDir",
ValueName = "directoryPath",
Description = "The working directory where all source content is located.")]
public void SetWorkingDir(string path)
{
Directory.SetCurrentDirectory(path);
}
[CommandLineParameter(
Name = "outputDir",
ValueName = "directoryPath",
Description = "The directory where all content is written.")]
public string OutputDir = string.Empty;
[CommandLineParameter(
Name = "intermediateDir",
ValueName = "directoryPath",
Description = "The directory where all intermediate files are written.")]
public string IntermediateDir = string.Empty;
[CommandLineParameter(
Name = "rebuild",
Description = "Forces a full rebuild of all content.")]
public bool Rebuild = false;
[CommandLineParameter(
Name = "clean",
Description = "Delete all previously built content and intermediate files.")]
public bool Clean = false;
[CommandLineParameter(
Name = "incremental",
Description = "Skip cleaning files not included in the current build.")]
public bool Incremental = false;
[CommandLineParameter(
Name = "reference",
ValueName = "assemblyNameOrFile",
Description = "Adds an assembly reference for resolving content importers, processors, and writers.")]
public readonly List<string> References = new List<string>();
[CommandLineParameter(
Name = "platform",
ValueName = "targetPlatform",
Description = "Set the target platform for this build. Defaults to Windows desktop DirectX.")]
public TargetPlatform Platform = TargetPlatform.Windows;
[CommandLineParameter(
Name = "profile",
ValueName = "graphicsProfile",
Description = "Set the target graphics profile for this build. Defaults to HiDef.")]
public GraphicsProfile Profile = GraphicsProfile.HiDef;
[CommandLineParameter(
Name = "config",
ValueName = "string",
Description = "The optional build config string from the build system.")]
public string Config = string.Empty;
[CommandLineParameter(
Name = "importer",
ValueName = "className",
Description = "Defines the class name of the content importer for reading source content.")]
public string Importer = null;
[CommandLineParameter(
Name = "processor",
ValueName = "className",
Description = "Defines the class name of the content processor for processing imported content.")]
public void SetProcessor(string processor)
{
_processor = processor;
// If you are changing the processor then reset all
// the processor parameters.
_processorParams.Clear();
}
private string _processor = null;
private readonly OpaqueDataDictionary _processorParams = new OpaqueDataDictionary();
[CommandLineParameter(
Name = "processorParam",
ValueName = "name=value",
Description = "Defines a parameter name and value to set on a content processor.")]
public void AddProcessorParam(string nameAndValue)
{
var keyAndValue = nameAndValue.Split('=');
if (keyAndValue.Length != 2)
{
// Do we error out or something?
return;
}
_processorParams.Remove(keyAndValue[0]);
_processorParams.Add(keyAndValue[0], keyAndValue[1]);
}
[CommandLineParameter(
Name = "build",
ValueName = "sourceFile",
Description = "Build the content source file using the previously set switches and options.")]
public void OnBuild(string sourceFile)
{
// Make sure the source file is absolute.
if (!Path.IsPathRooted(sourceFile))
sourceFile = Path.Combine(Directory.GetCurrentDirectory(), sourceFile);
sourceFile = PathHelper.Normalize(sourceFile);
// Remove duplicates... keep this new one.
var previous = _content.FindIndex(e => string.Equals(e.SourceFile, sourceFile, StringComparison.InvariantCultureIgnoreCase));
if (previous != -1)
_content.RemoveAt(previous);
// Create the item for processing later.
var item = new ContentItem
{
SourceFile = sourceFile,
Importer = Importer,
Processor = _processor,
ProcessorParams = new OpaqueDataDictionary()
};
_content.Add(item);
// Copy the current processor parameters blind as we
// will validate and remove invalid parameters during
// the build process later.
foreach (var pair in _processorParams)
item.ProcessorParams.Add(pair.Key, pair.Value);
}
[CommandLineParameter(
Name = "copy",
ValueName = "sourceFile",
Description = "Copy the content source file verbatim to the output directory.")]
public void OnCopy(string sourceFile)
{
if (!Path.IsPathRooted(sourceFile))
sourceFile = Path.Combine(Directory.GetCurrentDirectory(), sourceFile);
sourceFile = PathHelper.Normalize(sourceFile);
// Remove duplicates... keep this new one.
var previous = _copyItems.FindIndex(e => string.Equals(e, sourceFile, StringComparison.InvariantCultureIgnoreCase));
if (previous != -1)
_copyItems.RemoveAt(previous);
_copyItems.Add(sourceFile);
}
[CommandLineParameter(
Name = "compress",
Description = "Compress the XNB files for smaller file sizes.")]
public bool CompressContent = false;
public class ContentItem
{
public string SourceFile;
public string Importer;
public string Processor;
public OpaqueDataDictionary ProcessorParams;
}
private readonly List<ContentItem> _content = new List<ContentItem>();
private readonly List<string> _copyItems = new List<string>();
private PipelineManager _manager;
public bool HasWork
{
get { return _content.Count > 0 || _copyItems.Count > 0 || Clean; }
}
string ReplaceSymbols(string parameter)
{
if (string.IsNullOrWhiteSpace(parameter))
return parameter;
return parameter
.Replace("$(Platform)", Platform.ToString())
.Replace("$(Configuration)", Config)
.Replace("$(Config)", Config)
.Replace("$(Profile)", this.Profile.ToString());
}
public void Build(out int successCount, out int errorCount)
{
var projectDirectory = PathHelper.Normalize(Directory.GetCurrentDirectory());
var outputPath = ReplaceSymbols (OutputDir);
if (!Path.IsPathRooted(outputPath))
outputPath = PathHelper.Normalize(Path.GetFullPath(Path.Combine(projectDirectory, outputPath)));
var intermediatePath = ReplaceSymbols (IntermediateDir);
if (!Path.IsPathRooted(intermediatePath))
intermediatePath = PathHelper.Normalize(Path.GetFullPath(Path.Combine(projectDirectory, intermediatePath)));
_manager = new PipelineManager(projectDirectory, outputPath, intermediatePath);
_manager.Logger = new ConsoleLogger();
_manager.CompressContent = CompressContent;
// If the intent is to debug build, break at the original location
// of any exception, eg, within the actual importer/processor.
if (LaunchDebugger)
_manager.RethrowExceptions = false;
// Feed all the assembly references to the pipeline manager
// so it can resolve importers, processors, writers, and types.
foreach (var r in References)
{
var assembly = r;
if (!Path.IsPathRooted(assembly))
assembly = Path.GetFullPath(Path.Combine(projectDirectory, assembly));
_manager.AddAssembly(assembly);
}
// Load the previously serialized list of built content.
var contentFile = Path.Combine(intermediatePath, PipelineBuildEvent.Extension);
var previousContent = SourceFileCollection.Read(contentFile);
// If the target changed in any way then we need to force
// a fuull rebuild even under incremental builds.
var targetChanged = previousContent.Config != Config ||
previousContent.Platform != Platform ||
previousContent.Profile != Profile;
// First clean previously built content.
foreach (var sourceFile in previousContent.SourceFiles)
{
var inContent = _content.Any(e => string.Equals(e.SourceFile, sourceFile, StringComparison.InvariantCultureIgnoreCase));
var cleanOldContent = !inContent && !Incremental;
var cleanRebuiltContent = inContent && (Rebuild || Clean);
if (cleanRebuiltContent || cleanOldContent || targetChanged)
_manager.CleanContent(sourceFile);
}
var newContent = new SourceFileCollection
{
Profile = _manager.Profile = Profile,
Platform = _manager.Platform = Platform,
Config = _manager.Config = Config
};
errorCount = 0;
successCount = 0;
// Before building the content, register all files to be built. (Necessary to
// correctly resolve external references.)
foreach (var c in _content)
{
try
{
_manager.RegisterContent(c.SourceFile, null, c.Importer, c.Processor, c.ProcessorParams);
}
catch
{
// Ignore exception. Exception will be handled below.
}
}
foreach (var c in _content)
{
try
{
_manager.BuildContent(c.SourceFile,
null,
c.Importer,
c.Processor,
c.ProcessorParams);
newContent.SourceFiles.Add(c.SourceFile);
++successCount;
}
catch (InvalidContentException ex)
{
var message = string.Empty;
if (ex.ContentIdentity != null && !string.IsNullOrEmpty(ex.ContentIdentity.SourceFilename))
{
message = ex.ContentIdentity.SourceFilename;
if (!string.IsNullOrEmpty(ex.ContentIdentity.FragmentIdentifier))
message += "(" + ex.ContentIdentity.FragmentIdentifier + ")";
message += ": ";
}
message += ex.Message;
Console.WriteLine(message);
++errorCount;
}
catch (PipelineException ex)
{
Console.Error.WriteLine("{0}: error: {1}", c.SourceFile, ex.Message);
if (ex.InnerException != null)
Console.Error.WriteLine(ex.InnerException.ToString());
++errorCount;
}
catch (Exception ex)
{
Console.Error.WriteLine("{0}: error: {1}", c.SourceFile, ex.Message);
if (ex.InnerException != null)
Console.Error.WriteLine(ex.InnerException.ToString());
++errorCount;
}
}
// If this is an incremental build we merge the list
// of previous content with the new list.
if (Incremental && !targetChanged)
newContent.Merge(previousContent);
// Delete the old file and write the new content
// list if we have any to serialize.
FileHelper.DeleteIfExists(contentFile);
if (newContent.SourceFiles.Count > 0)
newContent.Write(contentFile);
// Process copy items (files that bypass the content pipeline)
foreach (var c in _copyItems)
{
try
{
// Figure out an asset name relative to the project directory,
// retaining the file extension.
// Note that replacing a sub-path like this requires consistent
// directory separator characters.
var relativeName = c.Replace(projectDirectory, string.Empty)
.TrimStart(Path.DirectorySeparatorChar)
.TrimStart(Path.AltDirectorySeparatorChar);
var dest = Path.Combine(outputPath, relativeName);
// Only copy if the source file is newer than the destination.
// We may want to provide an option for overriding this, but for
// nearly all cases this is the desired behavior.
if (File.Exists(dest))
{
var srcTime = File.GetLastWriteTimeUtc(c);
var dstTime = File.GetLastWriteTimeUtc(dest);
if (srcTime <= dstTime)
{
Console.WriteLine("Skipping {0}", c);
continue;
}
}
// Create the destination directory if it doesn't already exist.
var destPath = Path.GetDirectoryName(dest);
if (!Directory.Exists(destPath))
Directory.CreateDirectory(destPath);
File.Copy(c, dest, true);
// Destination file should not be read-only even if original was.
var fileAttr = File.GetAttributes(dest);
fileAttr = fileAttr & (~FileAttributes.ReadOnly);
File.SetAttributes(dest, fileAttr);
++successCount;
}
catch (Exception ex)
{
Console.Error.WriteLine("{0}: error: {1}", c, ex.Message);
if (ex.InnerException != null)
Console.Error.WriteLine(ex.InnerException.ToString());
++errorCount;
}
}
}
}
}