forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentFileCache.cs
More file actions
151 lines (122 loc) · 4.93 KB
/
ContentFileCache.cs
File metadata and controls
151 lines (122 loc) · 4.93 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
// 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;
record ContentFileCache : IContentFileCache
{
public string ContentRoot { get; init; } = "";
public bool ShouldBuild { get; init; } = false;
public IContentImporter? Importer { get; init; } = null;
public IContentProcessor? Processor { get; init; } = null;
public bool CompressContent { get; init; } = false;
public GraphicsProfile GraphicsProfile { get; init; } = GraphicsProfile.HiDef;
public Dictionary<string, DateTime> Dependencies { get; init; } = [];
public Dictionary<string, DateTime> Outputs { get; init; } = [];
public void AddDependency(ContentBuilder builder, string dependencyPath)
{
string fullDependencyPath;
string relativeDependencyPath;
if (Path.IsPathRooted(dependencyPath))
{
fullDependencyPath = Path.GetFullPath(dependencyPath);
relativeDependencyPath = Path.GetRelativePath(builder.Parameters.RootedSourceDirectory, dependencyPath);
}
else
{
fullDependencyPath = Path.GetFullPath(Path.Combine(builder.Parameters.RootedSourceDirectory, dependencyPath));
relativeDependencyPath = dependencyPath;
}
if (!File.Exists(fullDependencyPath))
{
return;
}
var lastModifiedTime = File.GetLastWriteTimeUtc(fullDependencyPath);
Dependencies[relativeDependencyPath.Sanitize()] = lastModifiedTime;
}
public void RemoveDependency(ContentBuilder builder, string dependencyPath)
{
string relativeDependencyPath = Path.IsPathRooted(dependencyPath) ?
Path.GetRelativePath(builder.Parameters.RootedSourceDirectory, dependencyPath) :
dependencyPath;
Dependencies.Remove(relativeDependencyPath);
}
public void AddDependency(ContentBuilder builder, IContentFileCache fileCache)
{
if (fileCache is not ContentFileCache cache)
{
return;
}
foreach (var dependency in cache.Dependencies)
{
Dependencies[dependency.Key] = dependency.Value;
}
foreach (var output in cache.Outputs)
{
Outputs[output.Key] = output.Value;
}
}
public void AddOutputFile(ContentBuilder builder, string outputPath)
{
string fullOutputPath;
string relativeOutputPath;
if (Path.IsPathRooted(outputPath))
{
fullOutputPath = Path.GetFullPath(outputPath);
relativeOutputPath = Path.GetRelativePath(builder.Parameters.RootedOutputDirectory, outputPath);
}
else
{
fullOutputPath = Path.GetFullPath(Path.Combine(builder.Parameters.RootedOutputDirectory, outputPath));
relativeOutputPath = outputPath;
}
if (!File.Exists(fullOutputPath))
{
return;
}
var lastModifiedTime = File.GetLastWriteTimeUtc(fullOutputPath);
Outputs[relativeOutputPath.Sanitize()] = lastModifiedTime;
}
public void RemoveOutputFile(ContentBuilder builder, string outputPath)
{
var relativeOutputFile = Path.IsPathRooted(outputPath) ?
Path.GetRelativePath(builder.Parameters.RootedOutputDirectory, outputPath) :
outputPath;
Outputs.Remove(relativeOutputFile);
}
public bool IsValid(ContentBuilder builder, ContentInfo info)
{
if (info.ShouldBuild || ShouldBuild)
{
if (builder.Parameters.GraphicsProfile != GraphicsProfile ||
builder.Parameters.CompressContent != CompressContent ||
info.ContentRoot != ContentRoot ||
info.ShouldBuild != ShouldBuild ||
!ContentBuilderHelper.ArePropsEqual(Importer, info.Importer) ||
!ContentBuilderHelper.ArePropsEqual(Processor, info.Processor))
{
return false;
}
}
foreach (var (dependencyFile, cachedModifiedTime) in Dependencies)
{
var dependencyFilePath = Path.Combine(builder.Parameters.RootedSourceDirectory, dependencyFile);
var modifiedTime = File.GetLastWriteTimeUtc(dependencyFilePath);
if (modifiedTime != cachedModifiedTime)
{
return false;
}
}
foreach (var (outputPath, cachedModifiedTime) in Outputs)
{
var outputFilePath = Path.Combine(builder.Parameters.RootedOutputDirectory, outputPath);
var modifiedTime = File.GetLastWriteTimeUtc(outputFilePath);
if (modifiedTime != cachedModifiedTime)
{
return false;
}
}
return true;
}
}