MessagePackObjectFormatter.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using MsgPack;
  2. using System;
  3. using System.Text;
  4. namespace Ryujinx.Common.Utilities
  5. {
  6. public static class MessagePackObjectFormatter
  7. {
  8. public static string ToString(this MessagePackObject obj, bool pretty)
  9. {
  10. if (pretty)
  11. {
  12. return Format(obj);
  13. }
  14. else
  15. {
  16. return obj.ToString();
  17. }
  18. }
  19. public static string Format(MessagePackObject obj)
  20. {
  21. var builder = new IndentedStringBuilder();
  22. FormatMsgPackObj(obj, builder);
  23. return builder.ToString();
  24. }
  25. private static void FormatMsgPackObj(MessagePackObject obj, IndentedStringBuilder builder)
  26. {
  27. if (obj.IsMap || obj.IsDictionary)
  28. {
  29. FormatMsgPackMap(obj, builder);
  30. }
  31. else if (obj.IsArray || obj.IsList)
  32. {
  33. FormatMsgPackArray(obj, builder);
  34. }
  35. else if (obj.IsNil)
  36. {
  37. builder.Append("null");
  38. }
  39. else
  40. {
  41. var literal = obj.ToObject();
  42. if (literal is String)
  43. {
  44. builder.AppendQuotedString(obj.AsStringUtf16());
  45. }
  46. else if (literal is byte[] byteArray)
  47. {
  48. FormatByteArray(byteArray, builder);
  49. }
  50. else if (literal is MessagePackExtendedTypeObject extObject)
  51. {
  52. builder.Append('{');
  53. // Indent
  54. builder.IncreaseIndent()
  55. .AppendLine();
  56. // Print TypeCode field
  57. builder.AppendQuotedString("TypeCode")
  58. .Append(": ")
  59. .Append(extObject.TypeCode)
  60. .AppendLine(",");
  61. // Print Value field
  62. builder.AppendQuotedString("Value")
  63. .Append(": ");
  64. FormatByteArrayAsString(extObject.GetBody(), builder, true);
  65. // Unindent
  66. builder.DecreaseIndent()
  67. .AppendLine();
  68. builder.Append('}');
  69. }
  70. else
  71. {
  72. builder.Append(literal);
  73. }
  74. }
  75. }
  76. private static void FormatByteArray(byte[] arr, IndentedStringBuilder builder)
  77. {
  78. builder.Append("[ ");
  79. foreach (var b in arr)
  80. {
  81. builder.Append("0x");
  82. builder.Append(ToHexChar(b >> 4));
  83. builder.Append(ToHexChar(b & 0xF));
  84. builder.Append(", ");
  85. }
  86. // Remove trailing comma
  87. builder.Remove(builder.Length - 2, 2);
  88. builder.Append(" ]");
  89. }
  90. private static void FormatByteArrayAsString(byte[] arr, IndentedStringBuilder builder, bool withPrefix)
  91. {
  92. builder.Append('"');
  93. if (withPrefix)
  94. {
  95. builder.Append("0x");
  96. }
  97. foreach (var b in arr)
  98. {
  99. builder.Append(ToHexChar(b >> 4));
  100. builder.Append(ToHexChar(b & 0xF));
  101. }
  102. builder.Append('"');
  103. }
  104. private static void FormatMsgPackMap(MessagePackObject obj, IndentedStringBuilder builder)
  105. {
  106. var map = obj.AsDictionary();
  107. builder.Append('{');
  108. // Indent
  109. builder.IncreaseIndent()
  110. .AppendLine();
  111. foreach (var item in map)
  112. {
  113. FormatMsgPackObj(item.Key, builder);
  114. builder.Append(": ");
  115. FormatMsgPackObj(item.Value, builder);
  116. builder.AppendLine(",");
  117. }
  118. // Remove the trailing new line and comma
  119. builder.TrimLastLine()
  120. .Remove(builder.Length - 1, 1);
  121. // Unindent
  122. builder.DecreaseIndent()
  123. .AppendLine();
  124. builder.Append('}');
  125. }
  126. private static void FormatMsgPackArray(MessagePackObject obj, IndentedStringBuilder builder)
  127. {
  128. var arr = obj.AsList();
  129. builder.Append("[ ");
  130. foreach (var item in arr)
  131. {
  132. FormatMsgPackObj(item, builder);
  133. builder.Append(", ");
  134. }
  135. // Remove trailing comma
  136. builder.Remove(builder.Length - 2, 2);
  137. builder.Append(" ]");
  138. }
  139. private static char ToHexChar(int b)
  140. {
  141. if (b < 10)
  142. {
  143. return unchecked((char)('0' + b));
  144. }
  145. else
  146. {
  147. return unchecked((char)('A' + (b - 10)));
  148. }
  149. }
  150. internal class IndentedStringBuilder
  151. {
  152. const string DefaultIndent = " ";
  153. private int _indentCount = 0;
  154. private int _newLineIndex = 0;
  155. private StringBuilder _builder;
  156. public string IndentString { get; set; } = DefaultIndent;
  157. public IndentedStringBuilder(StringBuilder builder)
  158. {
  159. _builder = builder;
  160. }
  161. public IndentedStringBuilder()
  162. : this(new StringBuilder())
  163. { }
  164. public IndentedStringBuilder(string str)
  165. : this(new StringBuilder(str))
  166. { }
  167. public IndentedStringBuilder(int length)
  168. : this(new StringBuilder(length))
  169. { }
  170. public int Length { get => _builder.Length; }
  171. public IndentedStringBuilder IncreaseIndent()
  172. {
  173. _indentCount++;
  174. return this;
  175. }
  176. public IndentedStringBuilder DecreaseIndent()
  177. {
  178. _indentCount--;
  179. return this;
  180. }
  181. public IndentedStringBuilder Append(char value)
  182. {
  183. _builder.Append(value);
  184. return this;
  185. }
  186. public IndentedStringBuilder Append(string value)
  187. {
  188. _builder.Append(value);
  189. return this;
  190. }
  191. public IndentedStringBuilder Append(object value)
  192. {
  193. this.Append(value.ToString());
  194. return this;
  195. }
  196. public IndentedStringBuilder AppendQuotedString(string value)
  197. {
  198. _builder.Append('"');
  199. _builder.Append(value);
  200. _builder.Append('"');
  201. return this;
  202. }
  203. public IndentedStringBuilder AppendLine()
  204. {
  205. _newLineIndex = _builder.Length;
  206. _builder.AppendLine();
  207. for (int i = 0; i < _indentCount; i++)
  208. _builder.Append(IndentString);
  209. return this;
  210. }
  211. public IndentedStringBuilder AppendLine(string value)
  212. {
  213. _builder.Append(value);
  214. this.AppendLine();
  215. return this;
  216. }
  217. public IndentedStringBuilder TrimLastLine()
  218. {
  219. _builder.Remove(_newLineIndex, _builder.Length - _newLineIndex);
  220. return this;
  221. }
  222. public IndentedStringBuilder Remove(int startIndex, int length)
  223. {
  224. _builder.Remove(startIndex, length);
  225. return this;
  226. }
  227. public override string ToString()
  228. {
  229. return _builder.ToString();
  230. }
  231. }
  232. }
  233. }