|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Buffers.Binary; |
| 5 | +using System.IO; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace SourceGit.Commands |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Decodes human-readable names from Unreal Engine OFPA (One File Per Actor) .uasset files. |
| 12 | + /// These files have hashed names like "KCBX0GWLTFQT9RJ8M1LY8.uasset" in __ExternalActors__ folders. |
| 13 | + /// |
| 14 | + /// Algorithm: |
| 15 | + /// 1. Heuristic Header Scan - locates Name Map bypassing UE version differences |
| 16 | + /// 2. Index-based Search - finds ActorLabel/FolderLabel and StrProperty indices |
| 17 | + /// 3. Pattern Matching - finds 16-byte tag [Label_Index, 0, StrProperty_Index, 0] |
| 18 | + /// 4. Value Extraction - extracts the string value following the pattern |
| 19 | + /// |
| 20 | + /// Compatibility: UE 4.26 - 5.7+ |
| 21 | + /// Performance: ~0.1 ms/file |
| 22 | + /// </summary> |
| 23 | + public static class DecodeOFPAPath |
| 24 | + { |
| 25 | + // Unreal Engine asset magic number (little-endian: 0x9E2A83C1) |
| 26 | + private static readonly byte[] UnrealMagic = { 0xC1, 0x83, 0x2A, 0x9E }; |
| 27 | + |
| 28 | + private const int HeaderScanLimit = 1024; |
| 29 | + private const int MaxStringLength = 256; |
| 30 | + private const int PropertyTagWindow = 150; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Result of decoding an OFPA file. |
| 34 | + /// </summary> |
| 35 | + public readonly struct DecodeResult : IEquatable<DecodeResult> |
| 36 | + { |
| 37 | + public string LabelType { get; } |
| 38 | + public string LabelValue { get; } |
| 39 | + |
| 40 | + public DecodeResult(string labelType, string labelValue) |
| 41 | + { |
| 42 | + LabelType = labelType; |
| 43 | + LabelValue = labelValue; |
| 44 | + } |
| 45 | + |
| 46 | + public bool Equals(DecodeResult other) => |
| 47 | + LabelType == other.LabelType && LabelValue == other.LabelValue; |
| 48 | + |
| 49 | + public override bool Equals(object? obj) => |
| 50 | + obj is DecodeResult other && Equals(other); |
| 51 | + |
| 52 | + public override int GetHashCode() => |
| 53 | + HashCode.Combine(LabelType, LabelValue); |
| 54 | + |
| 55 | + public static bool operator ==(DecodeResult left, DecodeResult right) => |
| 56 | + left.Equals(right); |
| 57 | + |
| 58 | + public static bool operator !=(DecodeResult left, DecodeResult right) => |
| 59 | + !left.Equals(right); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Checks if the given path is an OFPA file (inside __ExternalActors__ or __ExternalObjects__ folder). |
| 64 | + /// </summary> |
| 65 | + public static bool IsOFPAFile(string path) |
| 66 | + { |
| 67 | + return path.Contains("__ExternalActors__", StringComparison.Ordinal) || |
| 68 | + path.Contains("__ExternalObjects__", StringComparison.Ordinal); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Decodes the actor/folder label from a .uasset file. |
| 73 | + /// </summary> |
| 74 | + /// <param name="filePath">Path to the .uasset file</param> |
| 75 | + /// <returns>Decoded label or null if file is invalid or not an OFPA file</returns> |
| 76 | + public static DecodeResult? Decode(string filePath) |
| 77 | + { |
| 78 | + try |
| 79 | + { |
| 80 | + var data = File.ReadAllBytes(filePath); |
| 81 | + return DecodeFromData(data); |
| 82 | + } |
| 83 | + catch |
| 84 | + { |
| 85 | + return null; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Decodes the actor/folder label from raw .uasset file data. |
| 91 | + /// </summary> |
| 92 | + /// <param name="data">Raw bytes of the .uasset file</param> |
| 93 | + /// <returns>Decoded label or null if data is invalid</returns> |
| 94 | + public static DecodeResult? DecodeFromData(byte[] data) |
| 95 | + { |
| 96 | + if (data == null || data.Length < 20) |
| 97 | + return null; |
| 98 | + |
| 99 | + // Check magic number |
| 100 | + if (data[0] != UnrealMagic[0] || data[1] != UnrealMagic[1] || |
| 101 | + data[2] != UnrealMagic[2] || data[3] != UnrealMagic[3]) |
| 102 | + return null; |
| 103 | + |
| 104 | + return ParseUAsset(data); |
| 105 | + } |
| 106 | + |
| 107 | + private static DecodeResult? ParseUAsset(byte[] data) |
| 108 | + { |
| 109 | + int size = data.Length; |
| 110 | + |
| 111 | + // Fast path: find '/' to locate FolderName string |
| 112 | + int start = 20; |
| 113 | + int slashOff = FindByte(data, (byte)'/', 20, Math.Min(size, HeaderScanLimit)); |
| 114 | + if (slashOff >= 24) |
| 115 | + { |
| 116 | + int pLen = ReadInt32(data, slashOff - 4); |
| 117 | + if (pLen > 0 && pLen < MaxStringLength) |
| 118 | + start = slashOff - 4; |
| 119 | + } |
| 120 | + |
| 121 | + // Scan for name_count and name_offset |
| 122 | + int headerLen = Math.Min(size, HeaderScanLimit); |
| 123 | + int limit = headerLen - 20; |
| 124 | + int nameCount = 0; |
| 125 | + int nameOffset = 0; |
| 126 | + |
| 127 | + for (int off = start; off < limit; off++) |
| 128 | + { |
| 129 | + int pLen = ReadInt32(data, off); |
| 130 | + if (pLen > 0 && pLen < MaxStringLength) |
| 131 | + { |
| 132 | + int strEnd = off + 4 + pLen; |
| 133 | + if (strEnd > limit) |
| 134 | + break; |
| 135 | + |
| 136 | + byte ch = data[off + 4]; |
| 137 | + // Check for '/' or "None" |
| 138 | + if (ch == 47 || (ch == 78 && MatchBytes(data, off + 4, "None"))) |
| 139 | + { |
| 140 | + int baseOff = strEnd; |
| 141 | + if (baseOff + 12 <= headerLen) |
| 142 | + { |
| 143 | + int nc = ReadInt32(data, baseOff + 4); |
| 144 | + int no = ReadInt32(data, baseOff + 8); |
| 145 | + if (nc > 0 && nc < 100000 && no > 0 && no < size) |
| 146 | + { |
| 147 | + nameCount = nc; |
| 148 | + nameOffset = no; |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (nameCount == 0) |
| 157 | + return null; |
| 158 | + |
| 159 | + // Parse Name Map - find target indices |
| 160 | + int labelIdx = -1; |
| 161 | + int strIdx = -1; |
| 162 | + string? labelType = null; |
| 163 | + |
| 164 | + int pos = nameOffset; |
| 165 | + for (int i = 0; i < nameCount && pos + 4 <= size; i++) |
| 166 | + { |
| 167 | + int sLen = ReadInt32(data, pos); |
| 168 | + pos += 4; |
| 169 | + |
| 170 | + if (sLen > 0) |
| 171 | + { |
| 172 | + int end = pos + sLen; |
| 173 | + if (end > size) |
| 174 | + break; |
| 175 | + |
| 176 | + // Check for target strings |
| 177 | + if (sLen == 11 && labelIdx < 0 && MatchBytes(data, pos, "ActorLabel")) |
| 178 | + { |
| 179 | + labelIdx = i; |
| 180 | + labelType = "ActorLabel"; |
| 181 | + } |
| 182 | + else if (sLen == 12) |
| 183 | + { |
| 184 | + if (MatchBytes(data, pos, "StrProperty")) |
| 185 | + { |
| 186 | + strIdx = i; |
| 187 | + } |
| 188 | + else if (labelIdx < 0 && MatchBytes(data, pos, "FolderLabel")) |
| 189 | + { |
| 190 | + labelIdx = i; |
| 191 | + labelType = "FolderLabel"; |
| 192 | + } |
| 193 | + } |
| 194 | + else if (sLen == 6 && labelIdx < 0 && MatchBytes(data, pos, "Label")) |
| 195 | + { |
| 196 | + labelIdx = i; |
| 197 | + labelType = "Label"; |
| 198 | + } |
| 199 | + |
| 200 | + pos = end; |
| 201 | + |
| 202 | + if (labelIdx >= 0 && strIdx >= 0) |
| 203 | + break; |
| 204 | + } |
| 205 | + else if (sLen < 0) |
| 206 | + { |
| 207 | + // UTF-16 string |
| 208 | + pos += (-sLen) * 2; |
| 209 | + } |
| 210 | + |
| 211 | + // Skip hash value if present |
| 212 | + if (pos + 4 <= size) |
| 213 | + { |
| 214 | + int nv = ReadInt32(data, pos); |
| 215 | + if (nv == 0 || nv < -512 || nv > 512) |
| 216 | + pos += 4; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + if (labelIdx < 0 || strIdx < 0 || labelType == null) |
| 221 | + return null; |
| 222 | + |
| 223 | + // Find property tag pattern: [labelIdx, 0, strIdx, 0] |
| 224 | + byte[] pattern = new byte[16]; |
| 225 | + BinaryPrimitives.WriteInt32LittleEndian(pattern.AsSpan(0), labelIdx); |
| 226 | + BinaryPrimitives.WriteInt32LittleEndian(pattern.AsSpan(4), 0); |
| 227 | + BinaryPrimitives.WriteInt32LittleEndian(pattern.AsSpan(8), strIdx); |
| 228 | + BinaryPrimitives.WriteInt32LittleEndian(pattern.AsSpan(12), 0); |
| 229 | + |
| 230 | + int tagOff = FindPattern(data, pattern); |
| 231 | + if (tagOff == -1) |
| 232 | + return null; |
| 233 | + |
| 234 | + // Extract string value |
| 235 | + int searchStart = tagOff + 16; |
| 236 | + int searchEnd = Math.Min(searchStart + PropertyTagWindow, size); |
| 237 | + |
| 238 | + for (int i = searchStart; i < searchEnd - 4; i++) |
| 239 | + { |
| 240 | + int pLen = ReadInt32(data, i); |
| 241 | + |
| 242 | + if (pLen > 0 && pLen < 128) |
| 243 | + { |
| 244 | + int strEnd = i + 4 + pLen - 1; // -1 for null terminator |
| 245 | + if (strEnd <= searchEnd) |
| 246 | + { |
| 247 | + // Check if it's printable ASCII |
| 248 | + bool valid = true; |
| 249 | + for (int j = i + 4; j < strEnd && valid; j++) |
| 250 | + { |
| 251 | + byte b = data[j]; |
| 252 | + if (b < 32 || b > 126) |
| 253 | + valid = false; |
| 254 | + } |
| 255 | + |
| 256 | + if (valid && strEnd > i + 4) |
| 257 | + { |
| 258 | + string value = Encoding.ASCII.GetString(data, i + 4, strEnd - i - 4); |
| 259 | + return new DecodeResult(labelType, value); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + else if (pLen < 0 && pLen > -128) |
| 264 | + { |
| 265 | + // UTF-16 string |
| 266 | + int strEnd = i + 4 + ((-pLen) * 2) - 2; // -2 for null terminator |
| 267 | + if (strEnd <= searchEnd && strEnd > i + 4) |
| 268 | + { |
| 269 | + try |
| 270 | + { |
| 271 | + string value = Encoding.Unicode.GetString(data, i + 4, strEnd - i - 4); |
| 272 | + return new DecodeResult(labelType, value); |
| 273 | + } |
| 274 | + catch |
| 275 | + { |
| 276 | + // Invalid UTF-16, continue searching |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + return null; |
| 283 | + } |
| 284 | + |
| 285 | + private static int ReadInt32(byte[] data, int offset) |
| 286 | + { |
| 287 | + return BinaryPrimitives.ReadInt32LittleEndian(data.AsSpan(offset)); |
| 288 | + } |
| 289 | + |
| 290 | + private static int FindByte(byte[] data, byte value, int start, int end) |
| 291 | + { |
| 292 | + for (int i = start; i < end; i++) |
| 293 | + { |
| 294 | + if (data[i] == value) |
| 295 | + return i; |
| 296 | + } |
| 297 | + return -1; |
| 298 | + } |
| 299 | + |
| 300 | + private static bool MatchBytes(byte[] data, int offset, string str) |
| 301 | + { |
| 302 | + if (offset + str.Length > data.Length) |
| 303 | + return false; |
| 304 | + |
| 305 | + for (int i = 0; i < str.Length; i++) |
| 306 | + { |
| 307 | + if (data[offset + i] != (byte)str[i]) |
| 308 | + return false; |
| 309 | + } |
| 310 | + return true; |
| 311 | + } |
| 312 | + |
| 313 | + private static int FindPattern(byte[] data, byte[] pattern) |
| 314 | + { |
| 315 | + int end = data.Length - pattern.Length; |
| 316 | + for (int i = 0; i <= end; i++) |
| 317 | + { |
| 318 | + bool match = true; |
| 319 | + for (int j = 0; j < pattern.Length && match; j++) |
| 320 | + { |
| 321 | + if (data[i + j] != pattern[j]) |
| 322 | + match = false; |
| 323 | + } |
| 324 | + if (match) |
| 325 | + return i; |
| 326 | + } |
| 327 | + return -1; |
| 328 | + } |
| 329 | + } |
| 330 | +} |
0 commit comments