MessagePackObjectFormatter.cs 7.6 KB

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