forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentCollection.cs
More file actions
183 lines (164 loc) · 9.46 KB
/
ContentCollection.cs
File metadata and controls
183 lines (164 loc) · 9.46 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
// 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;
/// <summary>
/// <para>Collection of rules on how the content should be handled.</para>
/// <para>
/// The way this system works is by adding each call you make with include/exclude to a list of rules,
/// then during the <see cref="GetContentInfo"/> it will check if the filepath is valid for each one and return the last match it finds.
/// </para>
/// </summary>
public class ContentCollection : IContentCollection
{
private const string DEFAULT_CONTENT_ROOT = "Content";
private string _contentRoot = DEFAULT_CONTENT_ROOT;
private readonly Dictionary<string, ContentCollectionRoot> _content = new()
{
[DEFAULT_CONTENT_ROOT] = new ContentCollectionRoot(DEFAULT_CONTENT_ROOT)
};
private ContentCollectionRoot CurrentContent
{
get
{
if (_content.TryGetValue(_contentRoot, out ContentCollectionRoot? val))
{
return val;
}
var collection = new ContentCollectionRoot(_contentRoot);
_content[_contentRoot] = collection;
return collection;
}
}
/// <summary>
/// Sets the content root path that will be used when generating the output paths during include methods.
/// </summary>
/// <param name="contentRoot">Relative path that will get prefixed to the output paths.</param>
public void SetContentRoot(string contentRoot) => _contentRoot = contentRoot;
/// <summary>
/// Marks the file at inputPath to be copied to the output directory.
/// </summary>
/// <param name="inputPath">Relative path to the content file.</param>
/// <param name="outputPath">Relative path for the copy, if its not specified, the inputPath will be used.</param>
public void IncludeCopy(string inputPath, string? outputPath) => CurrentContent.IncludeCopy(inputPath, outputPath);
/// <summary>
/// Marks the file at inputPath to be built with specified settings.
/// </summary>
/// <param name="inputPath">Relative path to the content file.</param>
/// <param name="contentImporter">
/// An <see cref="IContentImporter"/> to be used for the building, if its not specified,
/// the system will use reflection to try to figure out an apropriate importer.
/// </param>
/// <param name="contentProcessor">
/// An <see cref="IContentProcessor"/> to be used for the building, if its not specified,
/// the system will use reflection to try to figure out an apropriate processor.
/// </param>
public void Include(string inputPath, IContentImporter? contentImporter = null, IContentProcessor? contentProcessor = null)
=> CurrentContent.Include(inputPath, contentImporter, contentProcessor);
/// <summary>
/// Marks the file at inputPath to be built with specified settings.
/// </summary>
/// <param name="inputPath">Relative path to the content file.</param>
/// <param name="outputPath">Relative path for the output content, if its not specified, the inputPath will be used with .xnb extension.</param>
/// <param name="contentImporter">
/// An <see cref="IContentImporter"/> to be used for the building, if its not specified,
/// the system will use reflection to try to figure out an apropriate importer.
/// </param>
/// <param name="contentProcessor">
/// An <see cref="IContentProcessor"/> to be used for the building, if its not specified,
/// the system will use reflection to try to figure out an apropriate processor.
/// </param>
public void Include(string inputPath, string outputPath, IContentImporter? contentImporter = null, IContentProcessor? contentProcessor = null)
=> CurrentContent.Include(inputPath, outputPath, contentImporter, contentProcessor);
/// <summary>
/// Marks the file at excludePath to be excluded from the content handling.
/// </summary>
/// <param name="excludePath">Relative path to the content file.</param>
public void Exclude(string excludePath) => CurrentContent.Exclude(excludePath);
/// <summary>
/// Marks the files that match the includePattern to be copied to the output directory.
/// </summary>
/// <typeparam name="T">
/// A <see cref="ContentRule"/> to be used for matching the includePattern. Some built in options are:
/// <para><see cref="WildcardRule"/> - specifies that input pattern should be a wildcard.</para>
/// <para><see cref="RegexRule"/> - specifies that input pattern should be a regex.</para>
/// </typeparam>
/// <param name="includePattern">A pattern to use for building of content files.</param>
/// <param name="outputPath">Relative path for the output content, if its not specified, the relative filePath will be used with .xnb extension.</param>
public void IncludeCopy<T>(string includePattern, Func<string, string>? outputPath = null) where T : ContentRule, new()
=> CurrentContent.IncludeCopy<T>(includePattern, outputPath);
/// <summary>
/// Marks the files that match the includePattern to be built with the specified settings.
/// </summary>
/// <typeparam name="T">
/// A <see cref="ContentRule"/> to be used for matching the includePattern. Some built in options are:
/// <para><see cref="WildcardRule"/> - specifies that input pattern should be a wildcard.</para>
/// <para><see cref="RegexRule"/> - specifies that input pattern should be a regex.</para>
/// </typeparam>
/// <param name="includePattern">A pattern to use for building of content files.</param>
/// <param name="contentImporter">
/// An <see cref="IContentImporter"/> to be used for the building, if its not specified,
/// the system will use reflection to try to figure out an apropriate importer.
/// </param>
/// <param name="contentProcessor">
/// An <see cref="IContentProcessor"/> to be used for the building, if its not specified,
/// the system will use reflection to try to figure out an apropriate processor.
/// </param>
public void Include<T>(string includePattern, IContentImporter? contentImporter = null, IContentProcessor? contentProcessor = null)
where T : ContentRule, new()
=> CurrentContent.Include<T>(includePattern, contentImporter, contentProcessor);
/// <summary>
/// Marks the files that match the includePattern to be built with the specified settings.
/// </summary>
/// <typeparam name="T">
/// A <see cref="ContentRule"/> to be used for matching the includePattern. Some built in options are:
/// <para><see cref="WildcardRule"/> - specifies that input pattern should be a wildcard.</para>
/// <para><see cref="RegexRule"/> - specifies that input pattern should be a regex.</para>
/// </typeparam>
/// <param name="includePattern">A pattern to use for building of content files.</param>
/// <param name="outputPath">A function that gives a relative output filepath for the specified relative content filepath.</param>
/// <param name="contentImporter">
/// An <see cref="IContentImporter"/> to be used for the building, if its not specified,
/// the system will use reflection to try to figure out an apropriate importer.
/// </param>
/// <param name="contentProcessor">
/// An <see cref="IContentProcessor"/> to be used for the building, if its not specified,
/// the system will use reflection to try to figure out an apropriate processor.
/// </param>
public void Include<T>(string includePattern, Func<string, string> outputPath, IContentImporter? contentImporter = null,
IContentProcessor? contentProcessor = null) where T : ContentRule, new()
=> CurrentContent.Include<T>(includePattern, outputPath, contentImporter, contentProcessor);
/// <summary>
/// Marks the files that match the excludePattern to be excluded from the content handling.
/// </summary>
/// <typeparam name="T">
/// A <see cref="ContentRule"/> to be used for matching the includePattern. Some built in options are:
/// <para><see cref="WildcardRule"/> - specifies that input pattern should be a wildcard.</para>
/// <para><see cref="RegexRule"/> - specifies that input pattern should be a regex.</para>
/// </typeparam>
/// <param name="excludePattern">A pattern to use for exclusion of content files.</param>
public void Exclude<T>(string excludePattern) where T : ContentRule, new() => CurrentContent.Exclude<T>(excludePattern);
/// <summary>
/// Gets information about how the ocntent should be handled for the passed relative filepath.
/// </summary>
/// <param name="filePath">Relative path to the content file.</param>
/// <param name="contentInfos">A list of <see cref="ContentInfo"/> describing the desired content handling.</param>
/// <returns><c>true</c> if the content should be handled, <c>false</c> otherwise.</returns>
public bool GetContentInfo(string filePath, out List<ContentInfo> contentInfos)
{
contentInfos = [];
foreach (var pair in _content)
{
if (!pair.Value.GetContentInfo(filePath, out List<ContentInfo> currentContent))
{
continue;
}
foreach (var info in currentContent)
{
contentInfos.Add(info);
}
}
return contentInfos.Count > 0;
}
}