forked from blushiemagic/MagicStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cs
More file actions
72 lines (53 loc) · 1.77 KB
/
Utility.cs
File metadata and controls
72 lines (53 loc) · 1.77 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
using System;
using System.Collections;
using System.Linq;
using Terraria;
using Terraria.ModLoader.IO;
namespace MagicStorage {
public static class Utility {
public static string GetSimplifiedGenericTypeName(this Type type) {
//Handle all invalid cases here:
if (type.FullName is null)
return type.Name;
if (!type.IsGenericType)
return type.FullName;
string parent = type.GetGenericTypeDefinition().FullName!;
//Include all but the "`X" part
parent = parent[..parent.IndexOf('`')];
//Construct the child types
return $"{parent}<{string.Join(", ", type.GetGenericArguments().Select(GetSimplifiedGenericTypeName))}>";
}
public static int GetCardinality(this BitArray bitArray) {
int[] ints = new int[(bitArray.Count >> 5) + 1];
bitArray.CopyTo(ints, 0);
int count = 0;
// fix for not truncated bits in last integer that may have been set to true with SetAll()
ints[^1] &= ~(-1 << (bitArray.Count % 32));
for (int i = 0; i < ints.Length; i++) {
int c = ints[i];
// magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
unchecked {
c -= (c >> 1) & 0x55555555;
c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
count += c;
}
return count;
}
public static bool AreStrictlyEqual(Item item1, Item item2, bool checkStack = false) {
if (!ItemData.Matches(item1, item2))
return false;
if (checkStack)
return ItemIO.ToBase64(item1) == ItemIO.ToBase64(item2);
int oldStack1 = item1.stack;
item1.stack = 1;
int oldStack2 = item2.stack;
item2.stack = 1;
bool equal = ItemIO.ToBase64(item1) == ItemIO.ToBase64(item2);
item1.stack = oldStack1;
item2.stack = oldStack2;
return equal;
}
}
}