-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathResourceManager.cs
267 lines (231 loc) · 8.44 KB
/
ResourceManager.cs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// <copyright file="ResourceManager.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>
namespace FinalEngine.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using FinalEngine.Resources.Exceptions;
/// <summary>
/// Provides a standard implementation of an <see cref="IResourceManager"/>.
/// </summary>
///
/// <seealso cref="IResourceManager" />
public sealed class ResourceManager : IResourceManager
{
private static ResourceManager? instance;
private readonly Dictionary<string, ResourceData> pathToResourceDataMap;
private readonly Dictionary<Type, IResourceLoader> typeToLoaderMap;
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="ResourceManager"/> class.
/// </summary>
public ResourceManager()
{
this.typeToLoaderMap = [];
this.pathToResourceDataMap = [];
}
/// <summary>
/// Finalizes an instance of the <see cref="ResourceManager"/> class.
/// </summary>
~ResourceManager()
{
this.Dispose(false);
}
/// <summary>
/// Gets the instance.
/// </summary>
///
/// <value>
/// The instance.
/// </value>
public static ResourceManager Instance
{
get
{
return instance ??= new ResourceManager();
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Loads a resource at the specified <paramref name="filePath"/>.
/// </summary>
/// <typeparam name="T">
/// The type of resource to load.
/// </typeparam>
///
/// <param name="filePath">
/// The file path of the resource to load.
/// </param>
///
/// <remarks>
/// The typical implementation of <see cref="IResourceManager"/> should attempt to cache resources; this way if <see cref="LoadResource{T}(string)"/> is called for the same file a reference the already loaded resource can be fetched.
/// </remarks>
///
/// <returns>
/// The loaded resource, of type <typeparamref name="T"/>.
/// </returns>
///
/// <exception cref="ResourceLoaderNotRegisteredException">
/// The specified <typeparamref name="T"/> parameter does not have an associated registered loader. You should call <see cref="RegisterLoader{T}(ResourceLoaderBase{T})"/> on the resource type you wish to load.
/// </exception>
///
/// <exception cref="ObjectDisposedException">
/// The <see cref="ResourceManager"/> has been disposed.
/// </exception>
///
/// <exception cref="ArgumentException">
/// The specified <paramref name="filePath"/> parameter cannot be null, empty or consist of only whitespace characters.
/// </exception>
public T LoadResource<T>(string filePath)
where T : IResource
{
ObjectDisposedException.ThrowIf(this.isDisposed, this);
ArgumentException.ThrowIfNullOrWhiteSpace(filePath, nameof(filePath));
if (!this.typeToLoaderMap.TryGetValue(typeof(T), out var loader))
{
throw new ResourceLoaderNotRegisteredException($"The specified {nameof(T)} parameter does not have an associated registered loader.");
}
if (!this.pathToResourceDataMap.TryGetValue(filePath, out var resourceData))
{
resourceData = new ResourceData(filePath, loader.LoadResource(filePath));
this.pathToResourceDataMap.Add(filePath, resourceData);
}
resourceData.IncrementReferenceCount();
return (T)resourceData.Reference;
}
/// <summary>
/// Registers the specified <paramref name="loader"/> to this <see cref="IResourceManager"/>.
/// </summary>
///
/// <typeparam name="T">
/// The type of resource that can be loaded.
/// </typeparam>
///
/// <param name="loader">
/// The resource loader to be used when attempting to resolve a resource of type <typeparamref name="T"/>.
/// </param>
///
/// <remarks>
/// The typical implementation of an <see cref="IResourceManager"/> should likely throw a <see cref="ResourceLoaderNotRegisteredException"/> if a loader of the specified <typeparamref name="T"/> type has already been registered.
/// </remarks>
///
/// <exception cref="ObjectDisposedException">
/// The <see cref="ResourceManager"/> has been disposed.
/// </exception>
///
/// <exception cref="ArgumentException">
/// The specified <paramref name="loader"/> parameter cannot be null.
/// </exception>
public void RegisterLoader<T>(ResourceLoaderBase<T> loader)
where T : IResource
{
this.RegisterLoader(typeof(T), loader);
}
public void RegisterLoader(Type type, IResourceLoader loader)
{
ObjectDisposedException.ThrowIf(this.isDisposed, this);
ArgumentNullException.ThrowIfNull(type, nameof(type));
ArgumentNullException.ThrowIfNull(loader, nameof(loader));
if (this.typeToLoaderMap.ContainsKey(type))
{
throw new ArgumentException($"The specified {nameof(type)} parameter has already been registered to a resource loader.", nameof(type));
}
this.typeToLoaderMap.Add(type, loader);
}
/// <summary>
/// Unloads the specified <paramref name="resource"/> from this <see cref="IResourceManager"/> (calling it's dispose method if <see cref="IDisposable"/> is implemented and there are no references).
/// </summary>
///
/// <param name="resource">
/// The resource to unload.
/// </param>
///
/// <exception cref="ObjectDisposedException">
/// The <see cref="ResourceManager"/> has been disposed.
/// </exception>
///
/// <exception cref="ArgumentException">
/// The specified <paramref name="resource"/> parameter cannot be null.
/// </exception>
public void UnloadResource(IResource resource)
{
ObjectDisposedException.ThrowIf(this.isDisposed, this);
ArgumentNullException.ThrowIfNull(resource, nameof(resource));
for (int i = this.pathToResourceDataMap.Count - 1; i >= 0; i--)
{
var kvp = this.pathToResourceDataMap.ElementAt(i);
var resourceData = kvp.Value;
if (ReferenceEquals(resourceData.Reference, resource))
{
resourceData.DecrementReferenceCount();
if (resourceData.ReferenceCount == 0)
{
if (resourceData.Reference is IDisposable disposable)
{
disposable.Dispose();
}
this.pathToResourceDataMap.Remove(resourceData.FilePath);
}
}
}
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
///
/// <param name="disposing">
/// <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.
/// </param>
private void Dispose(bool disposing)
{
if (this.isDisposed)
{
return;
}
if (disposing)
{
if (this.pathToResourceDataMap != null)
{
for (int i = this.pathToResourceDataMap.Count - 1; i >= 0; i--)
{
var kvp = this.pathToResourceDataMap.ElementAt(i);
var resourceData = kvp.Value;
if (resourceData.Reference is IDisposable disposable)
{
disposable.Dispose();
}
this.pathToResourceDataMap.Remove(resourceData.FilePath);
}
}
this.typeToLoaderMap.Clear();
}
this.isDisposed = true;
}
private sealed class ResourceData
{
public ResourceData(string filePath, IResource reference)
{
this.FilePath = filePath;
this.Reference = reference;
}
public string FilePath { get; }
public IResource Reference { get; }
public int ReferenceCount { get; private set; }
public void DecrementReferenceCount()
{
this.ReferenceCount--;
}
public void IncrementReferenceCount()
{
this.ReferenceCount++;
}
}
}