-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceKey.cs
More file actions
85 lines (72 loc) · 2.48 KB
/
ResourceKey.cs
File metadata and controls
85 lines (72 loc) · 2.48 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
using System.Text;
using System.Text.RegularExpressions;
namespace CodeName.Modding
{
/// <summary>
/// Currently used to centralize code related to resource key parsing.
/// </summary>
public readonly struct ResourceKey
{
private readonly string internalKey;
public ResourceKey(string key)
{
internalKey = key;
}
public string GetModId()
{
var firstColonIndex = internalKey.IndexOf(':');
return internalKey.Substring(0, firstColonIndex);
}
public string GetResourcePath()
{
var firstColonIndex = internalKey.IndexOf(':');
return internalKey.Substring(firstColonIndex + 1);
}
public ResourceKey ReplaceCsharpUnsafeCharacters(char replacement = '_')
{
return new ResourceKey(Regexes.ReplaceCsharpUnsafeCharacters.Replace(internalKey, replacement.ToString()).Trim(replacement));
}
public override string ToString()
{
return internalKey;
}
public static implicit operator string(ResourceKey value)
{
return value.internalKey;
}
/// <summary>
/// Creates a key in the format: "ModId:ResourceName".
/// <para/>
/// Note: It is recommended to use PascalCase and only alphanumeric characters.
/// </summary>
public static ResourceKey Create(string modId, string resourceName)
{
return new ResourceKey($"{modId}:{resourceName}");
}
/// <summary>
/// Creates a key in the format: "ModId:Path/To/Resource".
/// <para/>
/// Note: It is recommended to use PascalCase and only alphanumeric characters.
/// Path should not have a leading or trailing slash.
/// </summary>
public static ResourceKey Create(string modId, params string[] pathSegments)
{
var builder = new StringBuilder();
builder.Append(modId);
builder.Append(":");
for (var i = 0; i < pathSegments.Length; i++)
{
if (i != 0)
{
builder.Append("/");
}
builder.Append(pathSegments[i]);
}
return new ResourceKey(builder.ToString());
}
public static class Regexes
{
public static Regex ReplaceCsharpUnsafeCharacters { get; } = new(@"[^A-Za-z0-9_]+");
}
}
}