public void AddEntry(Entry entry) { if (!EntriesByUnicode.ContainsKey(entry.UnicodeValue)) { // There are duplicate unicode values in the table // TODO: more robust handling (maybe consider the Unknown as well) EntriesByUnicode.Add(entry.UnicodeValue, entry); } EntriesByCodeString.Add(entry.CodeString, entry); }
/// <summary> /// Replaces Unicode symbols from message.bin with human readable text tokens /// </summary> /// <param name="text">Encoded text</param> public string UnicodeDecode(string text) { var sb = new StringBuilder(); var textBytes = Encoding.Unicode.GetBytes(text); for (int i = 0; i < textBytes.Length; i += 2) { ushort shortCharacter = BitConverter.ToUInt16(textBytes, i); if (EntriesByUnicode.TryGetValue(shortCharacter, out var entry)) { // A direct match for the character was found sb.Append($"[{entry.CodeString}]"); } else if (EntriesByUnicode.TryGetValue((ushort)(shortCharacter & 0xFF00), out entry)) { // A match for the low bytes was found, which means that it encodes some other value. // The amount of words to decode is found in the Length. uint encodedValue = shortCharacter & 0xFFu; if (entry.Length > 0) { encodedValue = 0; for (int j = 0; j < entry.Length; j++) { i += (j + 1) * 2; uint partialValue = BitConverter.ToUInt16(textBytes, i); encodedValue |= partialValue << (j * 2); } encodedValue--; } string encodedValueString; if (ConstantReplacementTable.TryGetValue(entry.CodeString, out var enumType)) { encodedValueString = Enum.GetName(enumType, encodedValue) ?? encodedValue.ToString(); } else { encodedValueString = encodedValue.ToString(); } sb.Append($"[{entry.CodeString}{encodedValueString}]"); } else { sb.Append(Encoding.Unicode.GetString(textBytes, i, 2)); } } return(sb.ToString()); }