IPSwitchPatcher.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. const string BidHeader = "@nsobid-";
  10. private enum Token
  11. {
  12. Normal,
  13. String,
  14. EscapeChar,
  15. Comment
  16. }
  17. private readonly StreamReader _reader;
  18. public string BuildId { get; }
  19. public IPSwitchPatcher(StreamReader reader)
  20. {
  21. string header = reader.ReadLine();
  22. if (header == null || !header.StartsWith(BidHeader))
  23. {
  24. Logger.Error?.Print(LogClass.ModLoader, "IPSwitch: Malformed PCHTXT file. Skipping...");
  25. return;
  26. }
  27. _reader = reader;
  28. BuildId = header.Substring(BidHeader.Length).TrimEnd().TrimEnd('0');
  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 int.TryParse(str.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out value);
  117. }
  118. else
  119. {
  120. return int.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 = false;
  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. if (string.IsNullOrWhiteSpace(line))
  140. {
  141. enabled = false;
  142. continue;
  143. }
  144. line = PreprocessLine(line);
  145. lineNum += 1;
  146. if (line.Length == 0)
  147. {
  148. continue;
  149. }
  150. else if (line.StartsWith('#'))
  151. {
  152. Print(line);
  153. }
  154. else if (line.StartsWith("@stop"))
  155. {
  156. break;
  157. }
  158. else if (line.StartsWith("@enabled"))
  159. {
  160. enabled = true;
  161. }
  162. else if (line.StartsWith("@disabled"))
  163. {
  164. enabled = false;
  165. }
  166. else if (line.StartsWith("@flag"))
  167. {
  168. var tokens = line.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries);
  169. if (tokens.Length < 2)
  170. {
  171. ParseWarn();
  172. continue;
  173. }
  174. if (tokens[1] == "offset_shift")
  175. {
  176. if (tokens.Length != 3 || !ParseInt(tokens[2], out offset_shift))
  177. {
  178. ParseWarn();
  179. continue;
  180. }
  181. }
  182. else if (tokens[1] == "print_values")
  183. {
  184. printValues = true;
  185. }
  186. }
  187. else if (line.StartsWith('@'))
  188. {
  189. // Ignore
  190. }
  191. else
  192. {
  193. if (!enabled)
  194. {
  195. continue;
  196. }
  197. var tokens = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
  198. if (tokens.Length < 2)
  199. {
  200. ParseWarn();
  201. continue;
  202. }
  203. if (!int.TryParse(tokens[0], System.Globalization.NumberStyles.HexNumber, null, out int offset))
  204. {
  205. ParseWarn();
  206. continue;
  207. }
  208. offset += offset_shift;
  209. if (printValues)
  210. {
  211. Print($"print_values 0x{offset:x} <= {tokens[1]}");
  212. }
  213. if (tokens[1][0] == '"')
  214. {
  215. var patch = Encoding.ASCII.GetBytes(tokens[1].Trim('"'));
  216. patches.Add((uint)offset, patch);
  217. }
  218. else
  219. {
  220. var patch = Hex2ByteArrayBE(tokens[1]);
  221. patches.Add((uint)offset, patch);
  222. }
  223. }
  224. }
  225. return patches;
  226. }
  227. public void AddPatches(MemPatch patches)
  228. {
  229. patches.AddFrom(Parse());
  230. }
  231. }
  232. }