2019-03-21 04:50:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logic for converting a <see cref="string"/> for Generation 3.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class StringConverter3
|
|
|
|
|
{
|
2021-03-10 05:31:53 +00:00
|
|
|
|
private const byte TerminatorByte = 0xFF;
|
|
|
|
|
private const char Terminator = (char)TerminatorByte;
|
|
|
|
|
private const char TerminatorBigEndian = (char)0; // GC
|
|
|
|
|
|
2019-03-21 04:50:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a Generation 3 encoded value array to string.
|
|
|
|
|
/// </summary>
|
2020-06-17 02:46:22 +00:00
|
|
|
|
/// <param name="data">Byte array containing string data.</param>
|
2019-03-21 04:50:44 +00:00
|
|
|
|
/// <param name="offset">Offset to read from</param>
|
|
|
|
|
/// <param name="count">Length of data to read.</param>
|
|
|
|
|
/// <param name="jp">Value source is Japanese font.</param>
|
|
|
|
|
/// <returns>Decoded string.</returns>
|
2020-06-17 02:46:22 +00:00
|
|
|
|
public static string GetString3(byte[] data, int offset, int count, bool jp)
|
2019-03-21 04:50:44 +00:00
|
|
|
|
{
|
2021-01-17 01:31:05 +00:00
|
|
|
|
var s = new StringBuilder(count);
|
2019-03-21 04:50:44 +00:00
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
var val = data[offset + i];
|
2019-03-21 04:50:44 +00:00
|
|
|
|
var c = GetG3Char(val, jp); // Convert to Unicode
|
2021-03-10 05:31:53 +00:00
|
|
|
|
if (c == Terminator) // Stop if Terminator/Invalid
|
2019-03-21 04:50:44 +00:00
|
|
|
|
break;
|
|
|
|
|
s.Append(c);
|
|
|
|
|
}
|
2021-01-15 06:50:13 +00:00
|
|
|
|
StringConverter.SanitizeString(s);
|
|
|
|
|
return s.ToString();
|
2019-03-21 04:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a string to a Generation 3 encoded value array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">Decoded string.</param>
|
2021-01-17 01:31:05 +00:00
|
|
|
|
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
|
2019-03-21 04:50:44 +00:00
|
|
|
|
/// <param name="jp">String destination is Japanese font.</param>
|
2021-01-17 01:31:05 +00:00
|
|
|
|
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
|
|
|
|
|
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
|
2021-03-10 05:31:53 +00:00
|
|
|
|
/// <returns>Encoded data.</returns>
|
2019-03-21 04:50:44 +00:00
|
|
|
|
public static byte[] SetString3(string value, int maxLength, bool jp, int padTo = 0, ushort padWith = 0)
|
|
|
|
|
{
|
|
|
|
|
if (value.Length > maxLength)
|
2021-05-14 22:30:55 +00:00
|
|
|
|
value = value[..maxLength]; // Hard cap
|
2021-03-10 05:31:53 +00:00
|
|
|
|
var data = new byte[value.Length + 1]; // +1 for Terminator
|
2019-03-21 04:50:44 +00:00
|
|
|
|
for (int i = 0; i < value.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var chr = value[i];
|
|
|
|
|
var val = SetG3Char(chr, jp);
|
2021-03-10 05:31:53 +00:00
|
|
|
|
if (val == Terminator) // end
|
2019-03-21 04:50:44 +00:00
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
Array.Resize(ref data, i + 1);
|
2019-03-21 04:50:44 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-06-17 02:46:22 +00:00
|
|
|
|
data[i] = val;
|
2019-03-21 04:50:44 +00:00
|
|
|
|
}
|
2020-06-17 02:46:22 +00:00
|
|
|
|
if (data.Length > 0)
|
2021-05-14 22:30:55 +00:00
|
|
|
|
data[^1] = TerminatorByte;
|
2020-06-17 02:46:22 +00:00
|
|
|
|
if (data.Length > maxLength && padTo <= maxLength)
|
2021-03-10 05:31:53 +00:00
|
|
|
|
{
|
|
|
|
|
// Truncate
|
2020-06-17 02:46:22 +00:00
|
|
|
|
Array.Resize(ref data, maxLength);
|
2021-03-10 05:31:53 +00:00
|
|
|
|
return data;
|
|
|
|
|
}
|
2020-06-17 02:46:22 +00:00
|
|
|
|
if (data.Length < padTo)
|
2019-03-21 04:50:44 +00:00
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
var start = data.Length;
|
|
|
|
|
Array.Resize(ref data, padTo);
|
|
|
|
|
for (int i = start; i < data.Length; i++)
|
|
|
|
|
data[i] = (byte)padWith;
|
2019-03-21 04:50:44 +00:00
|
|
|
|
}
|
2020-06-17 02:46:22 +00:00
|
|
|
|
return data;
|
2019-03-21 04:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Converts Big Endian encoded data to decoded string.</summary>
|
|
|
|
|
/// <param name="data">Encoded data</param>
|
|
|
|
|
/// <param name="offset">Offset to read from</param>
|
|
|
|
|
/// <param name="count">Length of data to read.</param>
|
|
|
|
|
/// <returns>Decoded string.</returns>
|
|
|
|
|
public static string GetBEString3(byte[] data, int offset, int count)
|
|
|
|
|
{
|
2021-01-15 06:50:13 +00:00
|
|
|
|
var raw = Encoding.BigEndianUnicode.GetString(data, offset, count);
|
|
|
|
|
var sb = new StringBuilder(raw);
|
2021-03-10 05:31:53 +00:00
|
|
|
|
Util.TrimFromFirst(sb, TerminatorBigEndian);
|
2021-01-15 06:50:13 +00:00
|
|
|
|
return sb.ToString();
|
2019-03-21 04:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 02:46:22 +00:00
|
|
|
|
/// <summary>Gets the bytes for a Big Endian string.</summary>
|
2019-03-21 04:50:44 +00:00
|
|
|
|
/// <param name="value">Decoded string.</param>
|
2021-01-17 01:31:05 +00:00
|
|
|
|
/// <param name="maxLength">Maximum length of the input <see cref="value"/></param>
|
|
|
|
|
/// <param name="padTo">Pad the input <see cref="value"/> to given length</param>
|
|
|
|
|
/// <param name="padWith">Pad the input <see cref="value"/> with this character value</param>
|
2019-03-21 04:50:44 +00:00
|
|
|
|
/// <returns>Encoded data.</returns>
|
2021-03-10 05:31:53 +00:00
|
|
|
|
public static byte[] SetBEString3(string value, int maxLength, int padTo = 0, ushort padWith = TerminatorBigEndian)
|
2019-03-21 04:50:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (value.Length > maxLength)
|
2021-05-14 22:30:55 +00:00
|
|
|
|
value = value[..maxLength]; // Hard cap
|
2021-01-15 06:50:13 +00:00
|
|
|
|
var sb = new StringBuilder(value);
|
|
|
|
|
StringConverter.SanitizeString(sb);
|
2021-03-10 05:31:53 +00:00
|
|
|
|
sb.Append(TerminatorBigEndian);
|
2021-01-15 06:50:13 +00:00
|
|
|
|
var delta = padTo - value.Length;
|
|
|
|
|
if (delta > 0)
|
|
|
|
|
sb.Append((char)padWith, delta);
|
|
|
|
|
var result = sb.ToString();
|
|
|
|
|
return Encoding.BigEndianUnicode.GetBytes(result);
|
2019-03-21 04:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Decodes a character from a Generation 3 encoded value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="chr">Generation 4 decoded character.</param>
|
|
|
|
|
/// <param name="jp">Character destination is Japanese font.</param>
|
|
|
|
|
/// <returns>Generation 3 encoded value.</returns>
|
|
|
|
|
private static char GetG3Char(byte chr, bool jp)
|
|
|
|
|
{
|
|
|
|
|
var table = jp ? G3_JP : G3_EN;
|
|
|
|
|
return table[chr];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Encodes a character to a Generation 3 encoded value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="chr">Generation 4 decoded character.</param>
|
|
|
|
|
/// <param name="jp">Character destination is Japanese font.</param>
|
|
|
|
|
/// <returns>Generation 3 encoded value.</returns>
|
|
|
|
|
private static byte SetG3Char(char chr, bool jp)
|
|
|
|
|
{
|
2019-11-17 00:21:17 +00:00
|
|
|
|
if (chr == '\'') // ’
|
|
|
|
|
return 0xB4;
|
2019-03-21 04:50:44 +00:00
|
|
|
|
var table = jp ? G3_JP : G3_EN;
|
|
|
|
|
var index = Array.IndexOf(table, chr);
|
2021-03-10 05:31:53 +00:00
|
|
|
|
if (index == -1)
|
|
|
|
|
return TerminatorByte;
|
|
|
|
|
return (byte)index;
|
2019-03-21 04:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly char[] G3_EN =
|
|
|
|
|
{
|
|
|
|
|
' ', 'À', 'Á', 'Â', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'こ', 'Î', 'Ï', 'Ò', 'Ó', 'Ô', // 0
|
|
|
|
|
'Œ', 'Ù', 'Ú', 'Û', 'Ñ', 'ß', 'à', 'á', 'ね', 'Ç', 'È', 'é', 'ê', 'ë', 'ì', 'í', // 1
|
|
|
|
|
'î', 'ï', 'ò', 'ó', 'ô', 'œ', 'ù', 'ú', 'û', 'ñ', 'º', 'ª', '⒅', '&', '+', 'あ', // 2
|
|
|
|
|
'ぃ', 'ぅ', 'ぇ', 'ぉ', 'ゃ', '=', 'ょ', 'が', 'ぎ', 'ぐ', 'げ', 'ご', 'ざ', 'じ', 'ず', 'ぜ', // 3
|
|
|
|
|
'ぞ', 'だ', 'ぢ', 'づ', 'で', 'ど', 'ば', 'び', 'ぶ', 'べ', 'ぼ', 'ぱ', 'ぴ', 'ぷ', 'ぺ', 'ぽ', // 4
|
|
|
|
|
'っ', '¿', '¡', '⒆', '⒇', 'オ', 'カ', 'キ', 'ク', 'ケ', 'Í', 'コ', 'サ', 'ス', 'セ', 'ソ', // 5
|
|
|
|
|
'タ', 'チ', 'ツ', 'テ', 'ト', 'ナ', 'ニ', 'ヌ', 'â', 'ノ', 'ハ', 'ヒ', 'フ', 'ヘ', 'ホ', 'í', // 6
|
|
|
|
|
'ミ', 'ム', 'メ', 'モ', 'ヤ', 'ユ', 'ヨ', 'ラ', 'リ', 'ル', 'レ', 'ロ', 'ワ', 'ヲ', 'ン', 'ァ', // 7
|
|
|
|
|
'ィ', 'ゥ', 'ェ', 'ォ', 'ャ', 'ュ', 'ョ', 'ガ', 'ギ', 'グ', 'ゲ', 'ゴ', 'ザ', 'ジ', 'ズ', 'ゼ', // 8
|
|
|
|
|
'ゾ', 'ダ', 'ヂ', 'ヅ', 'デ', 'ド', 'バ', 'ビ', 'ブ', 'ベ', 'ボ', 'パ', 'ピ', 'プ', 'ペ', 'ポ', // 9
|
|
|
|
|
'ッ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '?', '.', '-', '・',// A
|
|
|
|
|
'⑬', '“', '”', '‘', '’', '♂', '♀', '$', ',', '⑧', '/', 'A', 'B', 'C', 'D', 'E', // B
|
|
|
|
|
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', // C
|
|
|
|
|
'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', // D
|
|
|
|
|
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', // E
|
|
|
|
|
':', 'Ä', 'Ö', 'Ü', 'ä', 'ö', 'ü', // F
|
2021-04-13 00:08:57 +00:00
|
|
|
|
|
|
|
|
|
// Make the total length 256 so that any byte access is always within the array
|
|
|
|
|
Terminator, Terminator, Terminator, Terminator, Terminator, Terminator, Terminator, Terminator, Terminator
|
2019-03-21 04:50:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static readonly char[] G3_JP =
|
|
|
|
|
{
|
|
|
|
|
' ', 'あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ', 'さ', 'し', 'す', 'せ', 'そ', // 0
|
|
|
|
|
'た', 'ち', 'つ', 'て', 'と', 'な', 'に', 'ぬ', 'ね', 'の', 'は', 'ひ', 'ふ', 'へ', 'ほ', 'ま', // 1
|
|
|
|
|
'み', 'む', 'め', 'も', 'や', 'ゆ', 'よ', 'ら', 'り', 'る', 'れ', 'ろ', 'わ', 'を', 'ん', 'ぁ', // 2
|
|
|
|
|
'ぃ', 'ぅ', 'ぇ', 'ぉ', 'ゃ', 'ゅ', 'ょ', 'が', 'ぎ', 'ぐ', 'げ', 'ご', 'ざ', 'じ', 'ず', 'ぜ', // 3
|
|
|
|
|
'ぞ', 'だ', 'ぢ', 'づ', 'で', 'ど', 'ば', 'び', 'ぶ', 'べ', 'ぼ', 'ぱ', 'ぴ', 'ぷ', 'ぺ', 'ぽ', // 4
|
|
|
|
|
'っ', 'ア', 'イ', 'ウ', 'エ', 'オ', 'カ', 'キ', 'ク', 'ケ', 'コ', 'サ', 'シ', 'ス', 'セ', 'ソ', // 5
|
|
|
|
|
'タ', 'チ', 'ツ', 'テ', 'ト', 'ナ', 'ニ', 'ヌ', 'ネ', 'ノ', 'ハ', 'ヒ', 'フ', 'ヘ', 'ホ', 'マ', // 6
|
|
|
|
|
'ミ', 'ム', 'メ', 'モ', 'ヤ', 'ユ', 'ヨ', 'ラ', 'リ', 'ル', 'レ', 'ロ', 'ワ', 'ヲ', 'ン', 'ァ', // 7
|
|
|
|
|
'ィ', 'ゥ', 'ェ', 'ォ', 'ャ', 'ュ', 'ョ', 'ガ', 'ギ', 'グ', 'ゲ', 'ゴ', 'ザ', 'ジ', 'ズ', 'ゼ', // 8
|
|
|
|
|
'ゾ', 'ダ', 'ヂ', 'ヅ', 'デ', 'ド', 'バ', 'ビ', 'ブ', 'ベ', 'ボ', 'パ', 'ピ', 'プ', 'ペ', 'ポ', // 9
|
|
|
|
|
'ッ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '?', '。', 'ー', '・', // A
|
|
|
|
|
'⋯', '『', '』', '「', '」', '♂', '♀', '$', '.', '⑧', '/', 'A', 'B', 'C', 'D', 'E', // B
|
|
|
|
|
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', // C
|
|
|
|
|
'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', // D
|
|
|
|
|
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', // E
|
|
|
|
|
':', 'Ä', 'Ö', 'Ü', 'ä', 'ö', 'ü', // F
|
2021-04-13 02:14:23 +00:00
|
|
|
|
|
|
|
|
|
// Make the total length 256 so that any byte access is always within the array
|
|
|
|
|
Terminator, Terminator, Terminator, Terminator, Terminator, Terminator, Terminator, Terminator, Terminator
|
2019-03-21 04:50:44 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2021-04-13 02:14:23 +00:00
|
|
|
|
}
|