forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentCache.cs
More file actions
145 lines (123 loc) · 4.2 KB
/
ContentCache.cs
File metadata and controls
145 lines (123 loc) · 4.2 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 ContentCache : IContentCache
{
private const string CacheFileName = "cache.yaml";
private Dictionary<string, ContentFileCache> _cache = [];
private readonly HashSet<string> _unusedOutputs = [];
public void LoadCache(ContentBuilder builder)
{
_cache.Clear();
_unusedOutputs.Clear();
var cacheFilePath = Path.Combine(builder.Parameters.RootedIntermediateDirectory, CacheFileName);
if (!File.Exists(cacheFilePath))
{
return;
}
try
{
var text = File.ReadAllText(cacheFilePath);
_cache = ContentBuilderHelper.Deserializer.Deserialize<Dictionary<string, ContentFileCache>>(text) ?? [];
}
catch (Exception ex)
{
builder.Logger.Log(LogLevel.Error, "Failed to load the Cache!");
builder.Logger.Log(LogLevel.Error, ex.ToString());
}
foreach (var (_, fileCache) in _cache)
{
foreach (var (outputPath, _) in fileCache.Outputs)
{
_unusedOutputs.Add(outputPath);
}
}
}
public void FlushCache(ContentBuilder builder)
{
var cacheFilePath = Path.Combine(builder.Parameters.RootedIntermediateDirectory, CacheFileName);
var dirPath = Path.GetDirectoryName(cacheFilePath);
if (string.IsNullOrEmpty(dirPath))
{
return;
}
if (!File.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
var text = ContentBuilderHelper.Serializer.Serialize(_cache);
File.WriteAllText(cacheFilePath, text);
}
public IContentFileCache CreateContentFileCache(ContentBuilder builder, ContentInfo info) => new ContentFileCache
{
ContentRoot = info.ContentRoot,
CompressContent = builder.Parameters.CompressContent,
GraphicsProfile = builder.Parameters.GraphicsProfile,
ShouldBuild = info.ShouldBuild,
Importer = info.Importer,
Processor = info.Processor
};
public IContentFileCache? ReadContentFileCache(ContentBuilder builder, string relativeOutputPath)
{
if (!_cache.TryGetValue(relativeOutputPath.Sanitize(), out ContentFileCache? fileCache))
{
return null;
}
return fileCache;
}
public void WriteContentFileCache(ContentBuilder builder, string relativeOutputPath, IContentFileCache? fileCache)
{
if (fileCache is ContentFileCache builderFileCache)
{
_cache[relativeOutputPath.Sanitize()] = builderFileCache;
}
}
public void MarkUsed(IContentFileCache fileCache)
{
if (fileCache is not ContentFileCache builderFileCache)
{
return;
}
foreach (var (outputPath, _) in builderFileCache.Outputs)
{
_unusedOutputs.Remove(outputPath);
}
}
public void CleanCache(ContentBuilder builder)
{
foreach (var outputPath in _unusedOutputs)
{
_cache.Remove(outputPath);
}
foreach (var outputPath in _unusedOutputs)
{
var outputFilePath = Path.Combine(builder.Parameters.RootedOutputDirectory, outputPath);
if (!IsOutputUsed(outputPath) && File.Exists(outputFilePath))
{
builder.Logger.Log("Deleting: " + outputPath);
File.Delete(outputFilePath);
}
}
_unusedOutputs.Clear();
}
private bool IsOutputUsed(string outputPathToCheck)
{
foreach (var (mainOutput, fileCache) in _cache)
{
if (mainOutput == outputPathToCheck)
{
return true;
}
foreach (var (outputPath, _) in fileCache.Outputs)
{
if (outputPath == outputPathToCheck)
{
return true;
}
}
}
return false;
}
}