forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentCollectionRoot.cs
More file actions
145 lines (119 loc) · 5.43 KB
/
ContentCollectionRoot.cs
File metadata and controls
145 lines (119 loc) · 5.43 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
// 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;
namespace MonoGame.Framework.Content.Pipeline.Builder;
class ContentCollectionRoot(string contentRoot)
{
private readonly string _contentRoot = contentRoot;
private readonly Dictionary<string, ContentInfo?> _imputFiles = [];
private readonly List<(ContentRule rule, ContentInfo? info)> _rules = [];
public void IncludeCopy(string inputPath, string? outputPath)
=> _imputFiles[inputPath.Sanitize()] = new(_contentRoot, false, null, null, string.IsNullOrWhiteSpace(outputPath) ? (s => s) : (_ => outputPath));
public void Include(string inputPath, IContentImporter? contentImporter, IContentProcessor? contentProcessor)
=> _imputFiles[inputPath.Sanitize()] = new(_contentRoot, true, contentImporter, contentProcessor);
public void Include(string inputPath, string outputPath, IContentImporter? contentImporter, IContentProcessor? contentProcessor )
=> _imputFiles[inputPath.Sanitize()] = new(_contentRoot, true, contentImporter, contentProcessor, _ => outputPath);
public void Exclude(string excludePath) => _imputFiles[excludePath.Sanitize()] = null;
public void IncludeCopy<T>(string includePattern, Func<string, string>? outputPath)
where T : ContentRule, new()
{
var rule = new T { Pattern = includePattern };
RemoveFilesByRule(rule, false, outputPath ?? (s => s));
_rules.Add((rule, new(_contentRoot, false, null, null, outputPath)));
}
public void Include<T>(string includePattern, IContentImporter? contentImporter, IContentProcessor? contentProcessor)
where T : ContentRule, new()
{
var rule = new T { Pattern = includePattern };
RemoveFilesByRule(rule, true, s => s);
_rules.Add((rule, new(_contentRoot, true, contentImporter, contentProcessor)));
}
public void Include<T>(string includePattern, Func<string, string> outputPath, IContentImporter? contentImporter, IContentProcessor? contentProcessor)
where T : ContentRule, new()
{
var rule = new T { Pattern = includePattern };
RemoveFilesByRule(rule, true, outputPath);
_rules.Add((rule, new(_contentRoot, true, contentImporter, contentProcessor, outputPath)));
}
public void Exclude<T>(string excludePattern)
where T : ContentRule, new()
{
var rule = new T { Pattern = excludePattern };
RemoveFilesByRule(rule, true, null);
_rules.Add((rule, null));
}
public bool GetContentInfo(string filePath, out List<ContentInfo> contentInfos)
{
HashSet<string> usedOutputs = [];
contentInfos = [];
bool inputFiles = _imputFiles.TryGetValue(filePath.Sanitize(), out ContentInfo? inputFilesInfo);
bool? shouldBuild = null;
if (inputFilesInfo != null)
{
usedOutputs.Add(filePath.GetDestinationPath(inputFilesInfo.ShouldBuild, inputFilesInfo.GetOutputPath));
shouldBuild = inputFilesInfo.ShouldBuild;
}
else if (inputFiles)
{
usedOutputs.Add(filePath.GetDestinationPath(true));
usedOutputs.Add(filePath.GetDestinationPath(false));
}
for (int i = _rules.Count - 1; i >= 0; i--)
{
(ContentRule rule, ContentInfo? info) rule = _rules[i];
if (rule.rule.IsMatch(filePath))
{
if (rule.info == null)
{
// Everything before this got discarded, we can exit here.
break;
}
var outputPath = filePath.GetDestinationPath(rule.info.ShouldBuild, rule.info.GetOutputPath);
if (usedOutputs.Contains(outputPath))
{
// Output already got used up later in list, nothing for us to do here.
continue;
}
if (shouldBuild != null && shouldBuild != rule.info.ShouldBuild)
{
continue;
}
usedOutputs.Add(outputPath);
contentInfos.Add(rule.info); // TODO: check for build action
shouldBuild = rule.info?.ShouldBuild;
}
}
if (inputFilesInfo != null)
{
contentInfos.Add(inputFilesInfo);
}
return contentInfos.Count > 0;
}
private void RemoveFilesByRule(ContentRule rule, bool build, Func<string, string>? outputPathRes)
{
List<string> elementsToRemove = [];
foreach (var pair in _imputFiles)
{
if (!rule.IsMatch(pair.Key))
{
continue;
}
// we want to check the output path, but only when the build action does not change
if (outputPathRes != null && pair.Value?.ShouldBuild == build)
{
var ruleOutput = pair.Key.GetDestinationPath(build, outputPathRes);
var inputFilesOutput = pair.Key.GetDestinationPath(build, pair.Value.GetOutputPath);
if (ruleOutput != inputFilesOutput)
{
continue;
}
}
elementsToRemove.Add(pair.Key);
}
foreach (var element in elementsToRemove)
{
_imputFiles.Remove(element);
}
}
}