-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathAtlasReference.cs
More file actions
68 lines (53 loc) · 2.44 KB
/
Copy pathAtlasReference.cs
File metadata and controls
68 lines (53 loc) · 2.44 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
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Intersect.Client.Framework.GenericClasses;
namespace Intersect.Client.Framework.Graphics;
public record AtlasReference(
string Name,
Rectangle Bounds,
bool IsRotated,
Rectangle SourceBounds,
IGameTexture Texture
)
{
public string Name { get; } = Name.Replace('\\', '/');
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<AtlasReference, string>>
_atlasReferencesByFolderName = new();
private static readonly ConcurrentDictionary<string, AtlasReference> _atlasReferences = [];
public static void Add(AtlasReference atlasReference)
{
var assetName = atlasReference.Name.Replace('\\', '/');
_atlasReferences[assetName] = atlasReference;
_atlasReferences[assetName.ToLowerInvariant()] = atlasReference;
var assetNameParts = assetName.Split('/');
if (assetNameParts.Length < 3)
{
throw new InvalidOperationException(
$"Expected 'resources/<folderName>/<assetFileName>' but got '{assetName}'"
);
}
var folderName = assetNameParts[1];
if (string.IsNullOrWhiteSpace(folderName))
{
throw new InvalidOperationException($"Invalid (empty/whitespace) segment in '{assetName}'");
}
var normalizedFolderName = folderName.ToLowerInvariant();
var referencesForFolder = _atlasReferencesByFolderName.GetOrAdd(normalizedFolderName, _ => []);
_ = referencesForFolder.AddOrUpdate(atlasReference, assetName, AssetNameFrom);
}
private static string AssetNameFrom(AtlasReference atlasReference, string assetName) => assetName;
public static AtlasReference[] GetAllFor(string folderName)
{
ArgumentException.ThrowIfNullOrWhiteSpace(folderName);
var normalizedFolderName = folderName.ToLowerInvariant();
return _atlasReferencesByFolderName.TryGetValue(normalizedFolderName, out var referencesForFolder)
? referencesForFolder.Keys.ToArray()
: [];
}
public static bool TryGet(string assetName, [NotNullWhen(true)] out AtlasReference? atlasReference)
{
var normalizedAssetName = assetName.Replace('\\', '/');
return _atlasReferences.TryGetValue(normalizedAssetName, out atlasReference) ||
_atlasReferences.TryGetValue(normalizedAssetName.ToLowerInvariant(), out atlasReference);
}
}