FF8TextEncodingCodepage.cs 19 KB

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