InlineResponses.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System.IO;
  2. using System.Text;
  3. namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
  4. {
  5. internal class InlineResponses
  6. {
  7. private const uint MaxStrLenUTF8 = 0x7D4;
  8. private const uint MaxStrLenUTF16 = 0x3EC;
  9. private static void BeginResponse(InlineKeyboardState state, InlineKeyboardResponse resCode, BinaryWriter writer)
  10. {
  11. writer.Write((uint)state);
  12. writer.Write((uint)resCode);
  13. }
  14. private static uint WriteString(string text, BinaryWriter writer, uint maxSize, Encoding encoding)
  15. {
  16. // Ensure the text fits in the buffer, but do not straight cut the bytes because
  17. // this may corrupt the encoding. Search for a cut in the source string that fits.
  18. byte[] bytes = null;
  19. for (int maxStr = text.Length; maxStr >= 0; maxStr--)
  20. {
  21. // This loop will probably will run only once.
  22. bytes = encoding.GetBytes(text.Substring(0, maxStr));
  23. if (bytes.Length <= maxSize)
  24. {
  25. break;
  26. }
  27. }
  28. writer.Write(bytes);
  29. writer.Seek((int)maxSize - bytes.Length, SeekOrigin.Current);
  30. writer.Write((uint)text.Length); // String size
  31. return (uint)text.Length; // Return the cursor position at the end of the text
  32. }
  33. private static void WriteStringWithCursor(string text, BinaryWriter writer, uint maxSize, Encoding encoding)
  34. {
  35. uint cursor = WriteString(text, writer, maxSize, encoding);
  36. writer.Write(cursor); // Cursor position
  37. }
  38. public static byte[] FinishedInitialize(InlineKeyboardState state)
  39. {
  40. uint resSize = 2 * sizeof(uint) + 0x1;
  41. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  42. using (BinaryWriter writer = new BinaryWriter(stream))
  43. {
  44. BeginResponse(state, InlineKeyboardResponse.FinishedInitialize, writer);
  45. writer.Write((byte)1); // Data (ignored by the program)
  46. return stream.ToArray();
  47. }
  48. }
  49. public static byte[] Default(InlineKeyboardState state)
  50. {
  51. uint resSize = 2 * sizeof(uint);
  52. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  53. using (BinaryWriter writer = new BinaryWriter(stream))
  54. {
  55. BeginResponse(state, InlineKeyboardResponse.Default, writer);
  56. return stream.ToArray();
  57. }
  58. }
  59. public static byte[] ChangedString(string text, InlineKeyboardState state)
  60. {
  61. uint resSize = 6 * sizeof(uint) + MaxStrLenUTF16;
  62. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  63. using (BinaryWriter writer = new BinaryWriter(stream))
  64. {
  65. BeginResponse(state, InlineKeyboardResponse.ChangedString, writer);
  66. WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
  67. writer.Write((int)0); // ?
  68. writer.Write((int)0); // ?
  69. return stream.ToArray();
  70. }
  71. }
  72. public static byte[] MovedCursor(string text, InlineKeyboardState state)
  73. {
  74. uint resSize = 4 * sizeof(uint) + MaxStrLenUTF16;
  75. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  76. using (BinaryWriter writer = new BinaryWriter(stream))
  77. {
  78. BeginResponse(state, InlineKeyboardResponse.MovedCursor, writer);
  79. WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
  80. return stream.ToArray();
  81. }
  82. }
  83. public static byte[] MovedTab(string text, InlineKeyboardState state)
  84. {
  85. // Should be the same as MovedCursor.
  86. uint resSize = 4 * sizeof(uint) + MaxStrLenUTF16;
  87. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  88. using (BinaryWriter writer = new BinaryWriter(stream))
  89. {
  90. BeginResponse(state, InlineKeyboardResponse.MovedTab, writer);
  91. WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
  92. return stream.ToArray();
  93. }
  94. }
  95. public static byte[] DecidedEnter(string text, InlineKeyboardState state)
  96. {
  97. uint resSize = 3 * sizeof(uint) + MaxStrLenUTF16;
  98. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  99. using (BinaryWriter writer = new BinaryWriter(stream))
  100. {
  101. BeginResponse(state, InlineKeyboardResponse.DecidedEnter, writer);
  102. WriteString(text, writer, MaxStrLenUTF16, Encoding.Unicode);
  103. return stream.ToArray();
  104. }
  105. }
  106. public static byte[] DecidedCancel(InlineKeyboardState state)
  107. {
  108. uint resSize = 2 * sizeof(uint);
  109. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  110. using (BinaryWriter writer = new BinaryWriter(stream))
  111. {
  112. BeginResponse(state, InlineKeyboardResponse.DecidedCancel, writer);
  113. return stream.ToArray();
  114. }
  115. }
  116. public static byte[] ChangedStringUtf8(string text, InlineKeyboardState state)
  117. {
  118. uint resSize = 6 * sizeof(uint) + MaxStrLenUTF8;
  119. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  120. using (BinaryWriter writer = new BinaryWriter(stream))
  121. {
  122. BeginResponse(state, InlineKeyboardResponse.ChangedStringUtf8, writer);
  123. WriteStringWithCursor(text, writer, MaxStrLenUTF8, Encoding.UTF8);
  124. writer.Write((int)0); // ?
  125. writer.Write((int)0); // ?
  126. return stream.ToArray();
  127. }
  128. }
  129. public static byte[] MovedCursorUtf8(string text, InlineKeyboardState state)
  130. {
  131. uint resSize = 4 * sizeof(uint) + MaxStrLenUTF8;
  132. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  133. using (BinaryWriter writer = new BinaryWriter(stream))
  134. {
  135. BeginResponse(state, InlineKeyboardResponse.MovedCursorUtf8, writer);
  136. WriteStringWithCursor(text, writer, MaxStrLenUTF8, Encoding.UTF8);
  137. return stream.ToArray();
  138. }
  139. }
  140. public static byte[] DecidedEnterUtf8(string text, InlineKeyboardState state)
  141. {
  142. uint resSize = 3 * sizeof(uint) + MaxStrLenUTF8;
  143. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  144. using (BinaryWriter writer = new BinaryWriter(stream))
  145. {
  146. BeginResponse(state, InlineKeyboardResponse.DecidedEnterUtf8, writer);
  147. WriteString(text, writer, MaxStrLenUTF8, Encoding.UTF8);
  148. return stream.ToArray();
  149. }
  150. }
  151. public static byte[] UnsetCustomizeDic(InlineKeyboardState state)
  152. {
  153. uint resSize = 2 * sizeof(uint);
  154. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  155. using (BinaryWriter writer = new BinaryWriter(stream))
  156. {
  157. BeginResponse(state, InlineKeyboardResponse.UnsetCustomizeDic, writer);
  158. return stream.ToArray();
  159. }
  160. }
  161. public static byte[] ReleasedUserWordInfo(InlineKeyboardState state)
  162. {
  163. uint resSize = 2 * sizeof(uint);
  164. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  165. using (BinaryWriter writer = new BinaryWriter(stream))
  166. {
  167. BeginResponse(state, InlineKeyboardResponse.ReleasedUserWordInfo, writer);
  168. return stream.ToArray();
  169. }
  170. }
  171. public static byte[] UnsetCustomizedDictionaries(InlineKeyboardState state)
  172. {
  173. uint resSize = 2 * sizeof(uint);
  174. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  175. using (BinaryWriter writer = new BinaryWriter(stream))
  176. {
  177. BeginResponse(state, InlineKeyboardResponse.UnsetCustomizedDictionaries, writer);
  178. return stream.ToArray();
  179. }
  180. }
  181. public static byte[] ChangedStringV2(string text, InlineKeyboardState state)
  182. {
  183. uint resSize = 6 * sizeof(uint) + MaxStrLenUTF16 + 0x1;
  184. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  185. using (BinaryWriter writer = new BinaryWriter(stream))
  186. {
  187. BeginResponse(state, InlineKeyboardResponse.ChangedStringV2, writer);
  188. WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
  189. writer.Write((int)0); // ?
  190. writer.Write((int)0); // ?
  191. writer.Write((byte)0); // Flag == 0
  192. return stream.ToArray();
  193. }
  194. }
  195. public static byte[] MovedCursorV2(string text, InlineKeyboardState state)
  196. {
  197. uint resSize = 4 * sizeof(uint) + MaxStrLenUTF16 + 0x1;
  198. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  199. using (BinaryWriter writer = new BinaryWriter(stream))
  200. {
  201. BeginResponse(state, InlineKeyboardResponse.MovedCursorV2, writer);
  202. WriteStringWithCursor(text, writer, MaxStrLenUTF16, Encoding.Unicode);
  203. writer.Write((byte)0); // Flag == 0
  204. return stream.ToArray();
  205. }
  206. }
  207. public static byte[] ChangedStringUtf8V2(string text, InlineKeyboardState state)
  208. {
  209. uint resSize = 6 * sizeof(uint) + MaxStrLenUTF8 + 0x1;
  210. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  211. using (BinaryWriter writer = new BinaryWriter(stream))
  212. {
  213. BeginResponse(state, InlineKeyboardResponse.ChangedStringUtf8V2, writer);
  214. WriteStringWithCursor(text, writer, MaxStrLenUTF8, Encoding.UTF8);
  215. writer.Write((int)0); // ?
  216. writer.Write((int)0); // ?
  217. writer.Write((byte)0); // Flag == 0
  218. return stream.ToArray();
  219. }
  220. }
  221. public static byte[] MovedCursorUtf8V2(string text, InlineKeyboardState state)
  222. {
  223. uint resSize = 4 * sizeof(uint) + MaxStrLenUTF8 + 0x1;
  224. using (MemoryStream stream = new MemoryStream(new byte[resSize]))
  225. using (BinaryWriter writer = new BinaryWriter(stream))
  226. {
  227. BeginResponse(state, InlineKeyboardResponse.MovedCursorUtf8V2, writer);
  228. WriteStringWithCursor(text, writer, MaxStrLenUTF8, Encoding.UTF8);
  229. writer.Write((byte)0); // Flag == 0
  230. return stream.ToArray();
  231. }
  232. }
  233. }
  234. }