XTermFix.cs 4.4 KB

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