2
0

FF8TextEncodingCodepage.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. namespace OpenVIII.Encoding
  5. {
  6. public sealed class FF8TextEncodingCodepage
  7. {
  8. private readonly Char[] _chars;
  9. private readonly Dictionary<Char, Byte> _bytes;
  10. /// <summary>
  11. /// Characters not in the codepage but have alternative characters that can be used.
  12. /// </summary>
  13. private readonly Dictionary<Char, Char> _alts;
  14. public FF8TextEncodingCodepage(Char[] chars, Dictionary<Char, Byte> bytes)
  15. {
  16. _chars = chars ?? throw new ArgumentNullException(nameof(chars));
  17. _bytes = bytes ?? throw new ArgumentNullException(nameof(bytes));
  18. _alts = GetAlts();
  19. }
  20. public Char this[Byte b]
  21. {
  22. get
  23. {
  24. Char c = _chars[b];
  25. if (c == '\0')
  26. return '�';
  27. //throw new ArgumentOutOfRangeException("b", b, $"Cannot find maping for the encoded character: {b}.");
  28. return c;
  29. }
  30. }
  31. public Byte this[Char c]
  32. {
  33. get {
  34. if (_bytes.ContainsKey(c))
  35. return _bytes[c];
  36. else if(_alts.ContainsKey(c))
  37. return _bytes[_alts[c]];
  38. else
  39. throw new KeyNotFoundException(String.Format(@"Character 0x{0:x4} '{1}' is unsupported. please add to alts or codepage", (ushort)c,c));
  40. }
  41. }
  42. public Char? TryGetChar(Byte b)
  43. {
  44. Char c = _chars[b];
  45. if (c == '\0')
  46. return null;
  47. return c;
  48. }
  49. public Byte? TryGetByte(Char c)
  50. {
  51. if (_bytes.TryGetValue(c, out byte b))
  52. return b;
  53. else if (_alts.ContainsKey(c) && _bytes.TryGetValue(_alts[c], out b))
  54. return b;
  55. return null;
  56. }
  57. public void GetParameters(out Char[] chars, out HashSet<Char>[] bytes)
  58. {
  59. chars = (Char[])_chars.Clone();
  60. bytes = new HashSet<Char>[256];
  61. for (Int32 i = 0; i < 256; i++)
  62. bytes[i] = new HashSet<Char>();
  63. foreach (KeyValuePair<Char, Byte> pair in _bytes)
  64. bytes[pair.Value].Add(pair.Key);
  65. }
  66. public static FF8TextEncodingCodepage Create()
  67. {
  68. var chars = CreateDefaultEncoding();
  69. Dictionary<Char, Byte> bytes = new Dictionary<Char, Byte>(chars.Length);
  70. for (Int32 i = chars.Length - 1; i >= 0; i--)
  71. {
  72. Char ch = chars[i];
  73. switch (ch)
  74. {
  75. case '¥':
  76. case '☻':
  77. case 'ⱷ':
  78. chars[i] = '\0';
  79. continue;
  80. case '\0':
  81. continue;
  82. }
  83. bytes[chars[i]] = (Byte)i;
  84. }
  85. return new FF8TextEncodingCodepage(chars, bytes);
  86. }
  87. private static Dictionary<Char, Char> GetAlts()
  88. {
  89. switch (CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
  90. {
  91. case "jp":
  92. return new Dictionary<char, char>
  93. {
  94. { '\'','・' },
  95. { '{','「' },
  96. { '}','」' },
  97. };
  98. case "ru":
  99. return new Dictionary<char, char>
  100. {
  101. { '\'','‘' },
  102. { '{','「' },
  103. { '}','」' },
  104. };
  105. default:
  106. return new Dictionary<char, char>
  107. {
  108. { '\'','‘' },
  109. { '{','「' },
  110. { '}','」' },
  111. };
  112. }
  113. }
  114. private static Char[] CreateDefaultEncoding()
  115. {
  116. switch (CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
  117. {
  118. case "jp":
  119. return CreateJapaneseCodepage();
  120. case "ru":
  121. return CreateRussianCodepage();
  122. default:
  123. return CreateEuropeanCodepage();
  124. }
  125. }
  126. private static Char[] CreateEuropeanCodepage()
  127. {
  128. Char[] chars = new Char[256]
  129. {
  130. '\0', '\0', '\n', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  131. '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '®', '®', '®', '®', '\0', '\0', '\0',
  132. ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '%', '/', ':', '!', '?',
  133. '…', '+', '-', '=', '*', '&', '「', '」', '(', ')', '·', '.', ',', '~', '“', '”',
  134. '‘', '#', '$', '"', '_', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  135. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
  136. 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
  137. 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'À', 'Á', 'Â', 'Ä', 'Ç', 'È', 'É',
  138. 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ñ', 'Ò', 'Ó', 'Ô', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Œ',
  139. 'ß', 'à', 'á', 'â', 'ä', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò',
  140. 'ó', 'ô', 'ö', 'ù', 'ú', 'û', 'ü', 'œ',
  141. 'Ⅷ', '[', ']', '■', '◎', '♦', '〖', '〗',
  142. '□', '★', '『', '』', '▽', ';', '▼', '‾', '⨯', '☆', '¥', '↓', '°', '¡', '¿', '─',
  143. '«', '»', '±', '♬', '¥', '↑', '¥', '¥', '¥', '™', '<', '>', '¥', '¥', '¥', '¥',
  144. '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '\0', '\0',
  145. '¥', '¥', '¥', '☻', '☻', '☻', '☻', '☻', '☻', '☻', '☻', '\0', '\0', '☻', '☻', '☻',
  146. '☻', '☻', '☻', '☻', '☻', '☻', '☻', '☻', '☻', '\0', '☻', '\0', '☻', 'ⱷ', 'ⱷ', 'ⱷ'
  147. };
  148. return chars;
  149. }
  150. private static Char[] CreateJapaneseCodepage()
  151. {
  152. Char[] chars = new Char[256]
  153. {
  154. '\0', '\0', '\n', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  155. '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '®', '®', '®', '®', '\0', '\0', '\0',
  156. 'バ','ば','ビ','び','ブ','ぶ','ベ','べ','ボ','ぼ','ガ','が','ギ','ぎ','グ','ぐ',
  157. 'ゲ','げ','ゴ','ご','ザ','ざ','ジ','じ','ズ','ず','ゼ','ぜ','ゾ','ぞ','ダ','だ',
  158. 'ヂ','ぢ','ヅ','づ','デ','で','ド','ど','ヴ','パ','ぱ','ピ','ぴ','プ','ぷ','ペ',
  159. 'ぺ','ポ','ぽ','0','1','2','3','4','5','6','7','8','9','、','。',' ',
  160. 'ハ','は','ヒ','ひ','フ','ふ','ヘ','へ','ホ','ほ','カ','か','キ','き','ク','く',
  161. 'ケ','け','コ','こ','サ','さ','シ','し','ス','す','セ','せ','ソ','そ','タ','た',
  162. 'チ','ち','ツ','つ','テ','て','ト','と','ウ','う','ア','あ','イ','い','エ','え',
  163. 'オ','お','ナ','な','ニ','に','ヌ','ぬ','ネ','ね','ノ','の','マ','ま','ミ','み',
  164. 'ム','む','メ','め','モ','も','ラ','ら','リ','り','ル','る','レ','れ','ロ','ろ',
  165. 'ヤ','や','ユ','ゆ','ヨ','よ','ワ','わ','ン','ん','ヲ','を','ッ','っ','ャ','ゃ',
  166. 'ュ','ゅ','ョ','ょ','ァ','ぁ','ィ','ぃ','ゥ','ぅ','ェ','ぇ','ォ','ぉ','A','B','C',
  167. 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
  168. 'S','T','U','V','W','X','Y','Z','!','?','…','+','-','=','*','/',
  169. '%','&','「','」','(',')','収','容','所','駅','・','.',',',':','~','ー'
  170. };
  171. return chars;
  172. }
  173. private static Char[] CreateRussianCodepage()
  174. {
  175. Char[] chars = new Char[256]
  176. {
  177. '\0', '\0', '\n', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  178. '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '®', '®', '®', '®', '\0', '\0', '\0',
  179. ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '%', '/', ':', '!', '?',
  180. '…', '+', '-', '=', '*', '&', '「', '」', '(', ')', '∙', '.', ',', '~', '”', '“',
  181. '‘', '#', '$', '’', '_', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  182. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
  183. 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
  184. 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'Б', 'Г', 'Д', 'Ё', 'Ж', 'З', 'И',
  185. 'Й', 'Л', 'П', 'У', 'Ф', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'б',
  186. 'в', 'г', 'д', 'ж', 'з', 'й', 'к', 'л', 'м', 'н', 'п', 'т', 'ф', 'ц', 'ч', 'ш',
  187. 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я', 'ё',
  188. 'Ⅷ', '[', ']', '■', '◎', '♦', '〖', '〗',
  189. '□', '★', '『', '』', '▽', ';', '▼', '‾', '⨯', '☆', '¥', '↓', '°', '¡', '¿', '─',
  190. '«', '»', '±', '♬', '¥', '↑', '¥', '¥', '¥', '™', '<', '>', '¥', '¥', '¥', '¥',
  191. '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '¥', '\0', '\0',
  192. '¥', '¥', '¥', '☻', '☻', '☻', '☻', '☻', '☻', '☻', '☻', '\0', '\0', '☻', '☻', '☻',
  193. '☻', '☻', '☻', '☻', '☻', '☻', '☻', '☻', '☻', '\0', '☻', '\0', '☻', 'ⱷ', 'ⱷ', 'ⱷ'
  194. };
  195. return chars;
  196. }
  197. }
  198. }