XTermFix.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection.Emit;
  5. using HarmonyLib;
  6. using MonoMod.RuntimeDetour;
  7. using MonoMod.RuntimeDetour.Platforms;
  8. using MonoMod.Utils;
  9. namespace BepInEx.Preloader.RuntimeFixes
  10. {
  11. internal static class XTermFix
  12. {
  13. public static void Apply()
  14. {
  15. if (PlatformHelper.Is(Platform.Windows))
  16. return;
  17. if (typeof(Console).Assembly.GetType("System.ConsoleDriver") == null)
  18. {
  19. // Mono version is too old, use our own TTY implementation instead
  20. return;
  21. }
  22. if (AccessTools.Method("System.TermInfoReader:DetermineVersion") != null)
  23. {
  24. // Fix has been applied officially
  25. return;
  26. }
  27. // Apparently on older Unity versions (4.x), using Process.Start can run Console..cctor
  28. // And since MonoMod's PlatformHelper (used by DetourHelper.Native) runs Process.Start to determine ARM/x86 platform,
  29. // this causes a crash owing to TermInfoReader running before it can be patched and fixed
  30. // Because Doorstop does not support ARM at the moment, we can get away with just forcing x86 detour platform.
  31. // TODO: Figure out a way to detect ARM on Unix without running Process.Start
  32. DetourHelper.Native = new DetourNativeX86Platform();
  33. var harmony = new HarmonyLib.Harmony("com.bepinex.xtermfix");
  34. harmony.Patch(AccessTools.Method("System.TermInfoReader:ReadHeader"),
  35. prefix: new HarmonyMethod(typeof(XTermFix), nameof(ReadHeaderPrefix)));
  36. harmony.Patch(AccessTools.Method("System.TermInfoReader:Get", new []{ AccessTools.TypeByName("System.TermInfoNumbers") }),
  37. transpiler: new HarmonyMethod(typeof(XTermFix), nameof(GetTermInfoNumbersTranspiler)));
  38. harmony.Patch(AccessTools.Method("System.TermInfoReader:Get", new []{ AccessTools.TypeByName("System.TermInfoStrings") }),
  39. transpiler: new HarmonyMethod(typeof(XTermFix), nameof(GetTermInfoStringsTranspiler)));
  40. harmony.Patch(AccessTools.Method("System.TermInfoReader:GetStringBytes", new []{ AccessTools.TypeByName("System.TermInfoStrings") }),
  41. transpiler: new HarmonyMethod(typeof(XTermFix), nameof(GetTermInfoStringsTranspiler)));
  42. DetourHelper.Native = null;
  43. }
  44. public static int intOffset;
  45. public static int GetInt32(byte[] buffer, int offset)
  46. {
  47. int b1 = buffer[offset];
  48. int b2 = buffer[offset + 1];
  49. int b3 = buffer[offset + 2];
  50. int b4 = buffer[offset + 3];
  51. return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
  52. }
  53. public static short GetInt16(byte[] buffer, int offset)
  54. {
  55. int b1 = buffer[offset];
  56. int b2 = buffer[offset + 1];
  57. return (short)(b1 | (b2 << 8));
  58. }
  59. public static int GetInteger(byte[] buffer, int offset)
  60. {
  61. return intOffset == 2
  62. ? GetInt16(buffer, offset)
  63. : GetInt32(buffer, offset);
  64. }
  65. public static void DetermineVersion(short magic)
  66. {
  67. if (magic == 0x11a)
  68. intOffset = 2;
  69. else if (magic == 0x21e)
  70. intOffset = 4;
  71. else
  72. throw new Exception($"Unknown xterm header format: {magic}");
  73. }
  74. public static bool ReadHeaderPrefix(byte[] buffer, ref int position, ref short ___boolSize, ref short ___numSize, ref short ___strOffsets)
  75. {
  76. short magic = GetInt16(buffer, position);
  77. position += 2;
  78. DetermineVersion(magic);
  79. // nameSize = GetInt16(buffer, position);
  80. position += 2;
  81. ___boolSize = GetInt16(buffer, position);
  82. position += 2;
  83. ___numSize = GetInt16(buffer, position);
  84. position += 2;
  85. ___strOffsets = GetInt16(buffer, position);
  86. position += 2;
  87. // strSize = GetInt16(buffer, position);
  88. position += 2;
  89. return false;
  90. }
  91. public static IEnumerable<CodeInstruction> GetTermInfoNumbersTranspiler(IEnumerable<CodeInstruction> instructions)
  92. {
  93. // This implementation does not seem to have changed so I will be using indexes like the lazy fuck I am
  94. var list = instructions.ToList();
  95. list[31] = new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(XTermFix), nameof(intOffset)));
  96. list[36] = new CodeInstruction(OpCodes.Nop);
  97. list[39] = new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(XTermFix), nameof(GetInteger)));
  98. return list;
  99. }
  100. public static IEnumerable<CodeInstruction> GetTermInfoStringsTranspiler(IEnumerable<CodeInstruction> instructions)
  101. {
  102. // This implementation does not seem to have changed so I will be using indexes like the lazy fuck I am
  103. var list = instructions.ToList();
  104. list[32] = new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(XTermFix), nameof(intOffset)));
  105. return list;
  106. }
  107. }
  108. }