IPSwitchPatcher.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. namespace Ryujinx.HLE.Loaders.Mods
  6. {
  7. class IPSwitchPatcher
  8. {
  9. readonly StreamReader _reader;
  10. public string BuildId { get; }
  11. const string BidHeader = "@nsobid-";
  12. public IPSwitchPatcher(StreamReader reader)
  13. {
  14. string header = reader.ReadLine();
  15. if (header == null || !header.StartsWith(BidHeader))
  16. {
  17. Logger.Error?.Print(LogClass.ModLoader, "IPSwitch: Malformed PCHTXT file. Skipping...");
  18. return;
  19. }
  20. _reader = reader;
  21. BuildId = header.Substring(BidHeader.Length).TrimEnd().TrimEnd('0');
  22. }
  23. private enum Token
  24. {
  25. Normal,
  26. String,
  27. EscapeChar,
  28. Comment
  29. }
  30. // Uncomments line and unescapes C style strings within
  31. private static string PreprocessLine(string line)
  32. {
  33. StringBuilder str = new StringBuilder();
  34. Token state = Token.Normal;
  35. for (int i = 0; i < line.Length; ++i)
  36. {
  37. char c = line[i];
  38. char la = i + 1 != line.Length ? line[i + 1] : '\0';
  39. switch (state)
  40. {
  41. case Token.Normal:
  42. state = c == '"' ? Token.String :
  43. c == '/' && la == '/' ? Token.Comment :
  44. c == '/' && la != '/' ? Token.Comment : // Ignore error and stop parsing
  45. Token.Normal;
  46. break;
  47. case Token.String:
  48. state = c switch
  49. {
  50. '"' => Token.Normal,
  51. '\\' => Token.EscapeChar,
  52. _ => Token.String
  53. };
  54. break;
  55. case Token.EscapeChar:
  56. state = Token.String;
  57. c = c switch
  58. {
  59. 'a' => '\a',
  60. 'b' => '\b',
  61. 'f' => '\f',
  62. 'n' => '\n',
  63. 'r' => '\r',
  64. 't' => '\t',
  65. 'v' => '\v',
  66. '\\' => '\\',
  67. _ => '?'
  68. };
  69. break;
  70. }
  71. if (state == Token.Comment) break;
  72. if (state < Token.EscapeChar)
  73. {
  74. str.Append(c);
  75. }
  76. }
  77. return str.ToString().Trim();
  78. }
  79. static int ParseHexByte(byte c)
  80. {
  81. if (c >= '0' && c <= '9')
  82. {
  83. return c - '0';
  84. }
  85. else if (c >= 'A' && c <= 'F')
  86. {
  87. return c - 'A' + 10;
  88. }
  89. else if (c >= 'a' && c <= 'f')
  90. {
  91. return c - 'a' + 10;
  92. }
  93. else
  94. {
  95. return 0;
  96. }
  97. }
  98. // Big Endian
  99. static byte[] Hex2ByteArrayBE(string hexstr)
  100. {
  101. if ((hexstr.Length & 1) == 1) return null;
  102. byte[] bytes = new byte[hexstr.Length >> 1];
  103. for (int i = 0; i < hexstr.Length; i += 2)
  104. {
  105. int high = ParseHexByte((byte)hexstr[i]);
  106. int low = ParseHexByte((byte)hexstr[i + 1]);
  107. bytes[i >> 1] = (byte)((high << 4) | low);
  108. }
  109. return bytes;
  110. }
  111. // Auto base discovery
  112. private static bool ParseInt(string str, out int value)
  113. {
  114. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  115. {
  116. return Int32.TryParse(str.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out value);
  117. }
  118. else
  119. {
  120. return Int32.TryParse(str, System.Globalization.NumberStyles.Integer, null, out value);
  121. }
  122. }
  123. private MemPatch Parse()
  124. {
  125. if (_reader == null)
  126. {
  127. return null;
  128. }
  129. MemPatch patches = new MemPatch();
  130. bool enabled = true;
  131. bool printValues = false;
  132. int offset_shift = 0;
  133. string line;
  134. int lineNum = 0;
  135. static void Print(string s) => Logger.Info?.Print(LogClass.ModLoader, $"IPSwitch: {s}");
  136. void ParseWarn() => Logger.Warning?.Print(LogClass.ModLoader, $"IPSwitch: Parse error at line {lineNum} for bid={BuildId}");
  137. while ((line = _reader.ReadLine()) != null)
  138. {
  139. line = PreprocessLine(line);
  140. lineNum += 1;
  141. if (line.Length == 0)
  142. {
  143. continue;
  144. }
  145. else if (line.StartsWith('#'))
  146. {
  147. Print(line);
  148. }
  149. else if (line.StartsWith("@stop"))
  150. {
  151. break;
  152. }
  153. else if (line.StartsWith("@enabled"))
  154. {
  155. enabled = true;
  156. }
  157. else if (line.StartsWith("@disabled"))
  158. {
  159. enabled = false;
  160. }
  161. else if (line.StartsWith("@flag"))
  162. {
  163. var tokens = line.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries);
  164. if (tokens.Length < 2)
  165. {
  166. ParseWarn();
  167. continue;
  168. }
  169. if (tokens[1] == "offset_shift")
  170. {
  171. if (tokens.Length != 3 || !ParseInt(tokens[2], out offset_shift))
  172. {
  173. ParseWarn();
  174. continue;
  175. }
  176. }
  177. else if (tokens[1] == "print_values")
  178. {
  179. printValues = true;
  180. }
  181. }
  182. else if (line.StartsWith('@'))
  183. {
  184. // Ignore
  185. }
  186. else
  187. {
  188. if (!enabled)
  189. {
  190. continue;
  191. }
  192. var tokens = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
  193. if (tokens.Length < 2)
  194. {
  195. ParseWarn();
  196. continue;
  197. }
  198. if (!Int32.TryParse(tokens[0], System.Globalization.NumberStyles.HexNumber, null, out int offset))
  199. {
  200. ParseWarn();
  201. continue;
  202. }
  203. offset += offset_shift;
  204. if (printValues)
  205. {
  206. Print($"print_values 0x{offset:x} <= {tokens[1]}");
  207. }
  208. if (tokens[1][0] == '"')
  209. {
  210. var patch = Encoding.ASCII.GetBytes(tokens[1].Trim('"'));
  211. patches.Add((uint)offset, patch);
  212. }
  213. else
  214. {
  215. var patch = Hex2ByteArrayBE(tokens[1]);
  216. patches.Add((uint)offset, patch);
  217. }
  218. }
  219. }
  220. return patches;
  221. }
  222. public void AddPatches(MemPatch patches)
  223. {
  224. patches.AddFrom(Parse());
  225. }
  226. }
  227. }