-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIngredientCode.cs
More file actions
79 lines (65 loc) · 1.84 KB
/
IngredientCode.cs
File metadata and controls
79 lines (65 loc) · 1.84 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
using System.Text;
using Vintagestory.API.Common;
using Vintagestory.API.Util;
namespace QuickCraft;
#pragma warning disable CS0618
internal readonly struct IngredientCode
{
private readonly string[]? include;
private readonly string[]? exclude;
private readonly string? key;
public readonly AssetLocation Code;
public readonly bool Wild;
public string Key => key ?? MakeKey();
public IngredientCode(CraftingRecipeIngredient ingredient)
{
include = null;
exclude = null;
key = null;
Code = ingredient.Code!;
Wild = ingredient.IsWildCard;
if (Wild)
{
include = ingredient.AllowedVariants;
exclude = ingredient.SkipVariants;
}
}
public bool Matches(AssetLocation item)
{
if (!Wild)
{
return Code == item;
}
return WildcardUtil.Match(Code, item, include) && (exclude == null || !WildcardUtil.MatchesVariants(Code, item, exclude));
}
public override bool Equals(object? obj)
{
return obj is IngredientCode other && Key.Equals(other.Key, StringComparison.Ordinal);
}
public override int GetHashCode()
{
return Key.GetHashCode();
}
private string MakeKey()
{
StringBuilder builder = new();
builder.Append(Code);
AddArray(builder, include, '[');
AddArray(builder, exclude, ']');
return builder.ToString();
}
private static void AddArray(StringBuilder builder, string[]? values, char prefix)
{
if (values == null || values.Length == 0)
{
return;
}
builder.Append(prefix);
builder.Append(values[0]);
for (int i = 1; i < values.Length; i++)
{
builder.Append(',');
builder.Append(values[i]);
}
}
}