-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathResourceStore.cs
214 lines (177 loc) · 6.18 KB
/
ResourceStore.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
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace osu.Framework.IO.Stores
{
public class ResourceStore<T> : IResourceStore<T>
where T : class
{
private readonly Dictionary<string, Action> actionList = new Dictionary<string, Action>();
private readonly List<IResourceStore<T>> stores = new List<IResourceStore<T>>();
private readonly List<string> searchExtensions = new List<string>();
/// <summary>
/// Initializes a resource store with no stores.
/// </summary>
public ResourceStore()
{
}
/// <summary>
/// Initializes a resource store with a single store.
/// </summary>
/// <param name="store">The store.</param>
public ResourceStore(IResourceStore<T> store = null)
{
if (store != null)
AddStore(store);
}
/// <summary>
/// Initializes a resource store with a collection of stores.
/// </summary>
/// <param name="stores">The collection of stores.</param>
public ResourceStore(IResourceStore<T>[] stores)
{
foreach (var resourceStore in stores)
AddStore(resourceStore);
}
/// <summary>
/// Notifies a bound delegate that the resource has changed.
/// </summary>
/// <param name="name">The resource that has changed.</param>
protected virtual void NotifyChanged(string name)
{
if (!actionList.TryGetValue(name, out Action action))
return;
action?.Invoke();
}
/// <summary>
/// Adds a nested resource store to this store.
/// </summary>
/// <param name="store">The store to add.</param>
public virtual void AddStore(IResourceStore<T> store)
{
lock (stores)
stores.Add(store);
}
/// <summary>
/// Removes a store from this store.
/// </summary>
/// <param name="store">The store to remove.</param>
public virtual void RemoveStore(IResourceStore<T> store)
{
lock (stores)
stores.Remove(store);
}
public virtual async Task<T> GetAsync(string name, CancellationToken cancellationToken = default)
{
if (name == null)
return null;
var filenames = GetFilenames(name);
foreach (IResourceStore<T> store in getStores())
{
foreach (string f in filenames)
{
T result = await store.GetAsync(f, cancellationToken).ConfigureAwait(false);
if (result != null)
return result;
}
}
return default;
}
/// <summary>
/// Retrieves an object from the store.
/// </summary>
/// <param name="name">The name of the object.</param>
/// <returns>The object.</returns>
public virtual T Get(string name)
{
if (name == null)
return null;
var filenames = GetFilenames(name);
foreach (IResourceStore<T> store in getStores())
{
foreach (string f in filenames)
{
T result = store.Get(f);
if (result != null)
return result;
}
}
return default;
}
public Stream GetStream(string name)
{
if (name == null)
return null;
var filenames = GetFilenames(name);
foreach (IResourceStore<T> store in getStores())
{
foreach (string f in filenames)
{
var result = store.GetStream(f);
if (result != null)
return result;
}
}
return null;
}
protected virtual IEnumerable<string> GetFilenames(string name)
{
yield return name;
//add file extension if it's missing.
foreach (string ext in searchExtensions)
yield return $@"{name}.{ext}";
}
/// <summary>
/// Binds a reload function to an object held by the store.
/// </summary>
/// <param name="name">The name of the object.</param>
/// <param name="onReload">The reload function to bind.</param>
public void BindReload(string name, Action onReload)
{
if (onReload == null)
return;
// Check if there's already a reload action bound
if (!actionList.TryAdd(name, onReload))
throw new InvalidOperationException($"A reload delegate is already bound to the resource '{name}'.");
}
/// <summary>
/// Add a file extension to automatically append to any lookups on this store.
/// </summary>
public void AddExtension(string extension)
{
extension = extension.Trim('.');
if (!searchExtensions.Contains(extension))
searchExtensions.Add(extension);
}
public virtual IEnumerable<string> GetAvailableResources()
{
lock (stores) return stores.SelectMany(s => s.GetAvailableResources()).ExcludeSystemFileNames();
}
private IResourceStore<T>[] getStores()
{
lock (stores) return stores.ToArray();
}
#region IDisposable Support
private bool isDisposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
isDisposed = true;
lock (stores) stores.ForEach(s => s.Dispose());
}
}
#endregion
}
}