-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathteResourceGUID.cs
More file actions
195 lines (157 loc) · 6.38 KB
/
Copy pathteResourceGUID.cs
File metadata and controls
195 lines (157 loc) · 6.38 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
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
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace TankLib {
/// <summary>Tank GUID</summary>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct teResourceGUID {
/// <summary>GUID Value</summary>
public ulong GUID;
[Flags]
public enum AttributeEnum : ulong {
Index = 0x00000000FFFFFFFF,
Locale = 0x0000001F00000000,
Reserved = 0x0000006000000000,
Region = 0x00000F8000000000,
Platform = 0x0000F00000000000,
Type = 0x0FFF000000000000,
Engine = 0xF000000000000000,
Key = 0x0000FFFFFFFFFFFF
};
public teResourceGUID(ulong guid) {
GUID = guid;
}
public static ulong Attribute(ulong key, AttributeEnum flags) {
return key & (ulong)flags;
}
public static ulong LongKey(ulong key) {
return Attribute(key, AttributeEnum.Key);
}
/// <summary>Unique ID</summary>
public static uint Index(ulong key) {
return (uint)Attribute(key, AttributeEnum.Index);
}
/// <summary>The locale that this GUID was authored for</summary>
public static byte Locale(ulong key) {
return (byte)(Attribute(key, AttributeEnum.Locale) >> 32);
}
/// <summary>The reserved component</summary>
public static byte Reserved(ulong key) {
return (byte)(Attribute(key, AttributeEnum.Reserved) >> 37);
}
/// <summary>Region that this GUID was authored for</summary>
public static byte Region(ulong key) {
return (byte)(Attribute(key, AttributeEnum.Region) >> 39);
}
/// <summary>Platform that this GUID was authored for</summary>
public static byte Platform(ulong key) {
return (byte)(Attribute(key, AttributeEnum.Platform) >> 44);
}
/// <summary>Type of this GUID</summary>
public static ushort Type(ulong key) {
ulong num = Attribute(key, AttributeEnum.Type) >> 48;
num = FlipTypeBits(num);
return (ushort)(num + 1);
}
/// <summary>Type of this GUID, but manged</summary>
public static ushort MangledType(ulong key) {
return (ushort)(Attribute(key, AttributeEnum.Type) >> 48);
}
/// <summary>Reserved engine component of this GUID</summary>
public static byte Engine(ulong key) {
return (byte)(Attribute(key, AttributeEnum.Engine) >> 60);
}
public void SetAttribute(ulong value, AttributeEnum flags) {
var attr = (ulong) flags;
GUID &= ~attr;
GUID |= value & attr;
}
public void SetLongKey(ulong value) {
SetAttribute(value, AttributeEnum.Key);
}
public void SetIndex(uint value) {
SetAttribute(value, AttributeEnum.Index);
}
public void SetLocale(byte value) {
SetAttribute((ulong) value << 32, AttributeEnum.Locale);
}
public void SetReserved(byte value) {
SetAttribute((ulong) value << 37, AttributeEnum.Reserved);
}
public void SetRegion(byte value) {
SetAttribute((ulong) value << 39, AttributeEnum.Region);
}
public void SetPlatform(byte value) {
SetAttribute((ulong) value << 44, AttributeEnum.Platform);
}
public void SetType(ushort value) {
SetMangledType((ushort) FlipTypeBits((ulong) (value - 1)));
}
public void SetMangledType(ushort value) {
SetAttribute((ulong) value << 48, AttributeEnum.Type);
}
public void SetEngine(byte value) {
SetAttribute((ulong) value << 60, AttributeEnum.Engine);
}
public teResourceGUID WithIndex(uint value) {
SetAttribute(value, AttributeEnum.Index);
return this;
}
public teResourceGUID WithType(ushort value) {
SetMangledType((ushort) FlipTypeBits((ulong) (value - 1)));
return this;
}
private static ulong FlipTypeBits(ulong num) {
num = ((num >> 1) & 0x55555555) | ((num & 0x55555555) << 1);
num = ((num >> 2) & 0x33333333) | ((num & 0x33333333) << 2);
num = ((num >> 4) & 0x0F0F0F0F) | ((num & 0x0F0F0F0F) << 4);
num = ((num >> 8) & 0x00FF00FF) | ((num & 0x00FF00FF) << 8);
num = (num >> 16) | (num << 16);
num >>= 20;
return num;
}
/// <summary>Dump info about a guid</summary>
public static void DumpAttributes(TextWriter @out, ulong key) {
@out.WriteLine($"Index: {Index(key):X4}");
@out.WriteLine($"Locale: {Locale(key):X1}");
@out.WriteLine($"Region: {Region(key):X1}");
@out.WriteLine($"Reserved: {Reserved(key):X1}");
@out.WriteLine($"Platform: {Platform(key):X1}");
@out.WriteLine($"Type: {Type(key):X3}");
@out.WriteLine($"Engine: {Engine(key):X1}");
@out.WriteLine();
}
public static implicit operator ulong(teResourceGUID guid) {
return guid.GUID;
}
public static explicit operator teResourceGUID(ulong guid) {
return new teResourceGUID(guid);
}
/// <summary>String representation of this GUID</summary>
public override string ToString() {
return AsString(GUID);
}
/// <summary>String representation a GUID</summary>
public static string AsString(ulong guid) {
return $"{LongKey(guid):X12}.{Type(guid):X3}";
}
/// <summary>String representation a GUID</summary>
public static string AsIndexString(ulong guid) {
return $"{LongKey(guid):X12}";
}
/// <summary>String representation a GUID</summary>
public static string AsHexString(ulong guid) {
return $"0x{guid:X16}";
}
public override int GetHashCode() {
return GUID.GetHashCode();
}
public bool Equals(teResourceGUID other) {
return GUID == other.GUID;
}
/// <summary>Shorted string representation of a GUID (ABB.075)</summary>
public string ToStringShort() {
return $"{LongKey(GUID):X}.{Type(GUID):X3}";
}
}
}