forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentBuilderProcessorContext.cs
More file actions
100 lines (72 loc) · 4.66 KB
/
ContentBuilderProcessorContext.cs
File metadata and controls
100 lines (72 loc) · 4.66 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
// MonoGame - Copyright (C) MonoGame Foundation, Inc
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGame.Framework.Content.Pipeline.Builder;
class ContentBuilderProcessorContext(ContentBuilder builder, string relativePath, ContentInfo contentInfo, IContentFileCache contentFileCache, string outputFilename = "") : ContentProcessorContext
{
private readonly ContentBuilder _builder = builder;
private readonly string _relativeContentPath = relativePath;
private readonly ContentInfo _contentInfo = contentInfo;
private int _contentIndex = 0;
public IContentFileCache ContentFileCache { get; } = contentFileCache;
public override string BuildConfiguration { get; } = "";
public override string IntermediateDirectory => _builder.Parameters.RootedIntermediateDirectory;
public override ContentBuildLogger Logger => _builder.Logger;
public override ContentIdentity SourceIdentity => throw new NotImplementedException();
public override string OutputDirectory => _builder.Parameters.OutputDirectory;
public override string OutputFilename { get; } = outputFilename;
public override OpaqueDataDictionary Parameters { get; } = [];
public override string ProjectDirectory => _builder.Parameters.RootedSourceDirectory;
public override TargetPlatform TargetPlatform => _builder.Parameters.Platform;
public override GraphicsProfile TargetProfile => _builder.Parameters.GraphicsProfile;
public override void AddDependency(string filename) => ContentFileCache.AddDependency(_builder, filename);
public override void AddOutputFile(string filename) => ContentFileCache.AddOutputFile(_builder, filename);
public string GetNextOutputPath()
{
_contentIndex++;
return $"{_relativeContentPath.GetDestinationPath(true, _contentInfo.GetOutputPath)[0..^4]}_{_contentIndex}.xnb";
}
[Obsolete]
public override TOutput BuildAndLoadAsset<TInput, TOutput>(ExternalReference<TInput> sourceAsset,
string processorName, OpaqueDataDictionary processorParameters, string importerName)
{
throw new NotSupportedException(
@"Converting from importerName and processorName is not supported with the ContentBuilder.
Please pass an importer and processor instance to the Convert method instead.");
}
public override TOutput BuildAndLoadAsset<TInput, TOutput>(ExternalReference<TInput> sourceAsset, IContentImporter importer, IContentProcessor processor)
{
var processedObject = _builder.BuildAndLoadContent(sourceAsset.Filename, new ContentInfo(_contentInfo.ContentRoot, true, importer, processor), GetNextOutputPath(), this);
return (TOutput)processedObject!;
}
[Obsolete]
public override ExternalReference<TOutput> BuildAsset<TInput, TOutput>(ExternalReference<TInput> sourceAsset,
string processorName, OpaqueDataDictionary processorParameters, string importerName, string assetName)
{
throw new NotSupportedException(
@"Converting from imposterName and processorName is not supported with the ContentBuilder.
Please pass an importer and processor instance to the Convert method instead.");
}
public override ExternalReference<TOutput> BuildAsset<TInput, TOutput>(ExternalReference<TInput> sourceAsset,
IContentImporter importer, IContentProcessor processor, string? assetName)
{
var outputRelativePath = string.IsNullOrWhiteSpace(assetName) ? GetNextOutputPath() : assetName;
_builder.BuildAndWriteContent(sourceAsset.Filename, new ContentInfo(_contentInfo.ContentRoot, true, importer, processor), outputRelativePath, this);
return new ExternalReference<TOutput>(Path.Combine(_builder.Parameters.RootedOutputDirectory, outputRelativePath));
}
[Obsolete]
public override TOutput Convert<TInput, TOutput>(TInput input, string processorName, OpaqueDataDictionary processorParameters)
{
throw new NotSupportedException(@"Converting from processorName is not supported with the ContentBuilder.
Please pass a processor instance to the Convert method instead.");
}
public override TOutput Convert<TInput, TOutput>(TInput input, IContentProcessor processor)
{
var processContext = new ContentBuilderProcessorContext(_builder, _relativeContentPath, _contentInfo, ContentFileCache);
using var _ = ContextScopeFactory.BeginContext(processContext);
var processedObject = processor.Process(input!, processContext);
return (TOutput)processedObject;
}
}