LinuxConsoleDriver.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.IO;
  3. using BepInEx.Logging;
  4. using HarmonyLib;
  5. using UnityInjector.ConsoleUtil;
  6. namespace BepInEx.Unix
  7. {
  8. internal class LinuxConsoleDriver : IConsoleDriver
  9. {
  10. public static bool UseMonoTtyDriver { get; }
  11. static LinuxConsoleDriver()
  12. {
  13. UseMonoTtyDriver = false;
  14. var consoleDriverType = typeof(Console).Assembly.GetType("System.ConsoleDriver");
  15. if (consoleDriverType != null)
  16. {
  17. UseMonoTtyDriver = typeof(Console).Assembly.GetType("System.ParameterizedStrings") != null;
  18. }
  19. }
  20. public TextWriter StandardOut { get; private set; }
  21. public TextWriter ConsoleOut { get; private set; }
  22. public bool ConsoleActive { get; private set; }
  23. public bool ConsoleIsExternal => false;
  24. public bool StdoutRedirected { get; private set; }
  25. public TtyInfo TtyInfo { get; private set; }
  26. public void Initialize(bool alreadyActive)
  27. {
  28. // Console is always considered active on Unix
  29. ConsoleActive = true;
  30. StdoutRedirected = UnixStreamHelper.isatty(1) != 1;
  31. var duplicateStream = UnixStreamHelper.CreateDuplicateStream(1);
  32. if (UseMonoTtyDriver && !StdoutRedirected)
  33. {
  34. // Mono implementation handles xterm for us
  35. var writer = ConsoleWriter.CreateConsoleStreamWriter(duplicateStream, Console.Out.Encoding, true);
  36. StandardOut = TextWriter.Synchronized(writer);
  37. var driver = AccessTools.Field(AccessTools.TypeByName("System.ConsoleDriver"), "driver").GetValue(null);
  38. AccessTools.Field(AccessTools.TypeByName("System.TermInfoDriver"), "stdout").SetValue(driver, writer);
  39. }
  40. else
  41. {
  42. // Handle TTY ourselves
  43. var writer = new StreamWriter(duplicateStream, Console.Out.Encoding);
  44. writer.AutoFlush = true;
  45. StandardOut = TextWriter.Synchronized(writer);
  46. TtyInfo = TtyHandler.GetTtyInfo();
  47. }
  48. ConsoleOut = StandardOut;
  49. }
  50. public void CreateConsole(uint codepage)
  51. {
  52. Logger.LogWarning("An external console currently cannot be spawned on a Unix platform.");
  53. }
  54. public void DetachConsole()
  55. {
  56. throw new PlatformNotSupportedException("Cannot detach console on a Unix platform");
  57. }
  58. public void SetConsoleColor(ConsoleColor color)
  59. {
  60. if (StdoutRedirected)
  61. return;
  62. if (UseMonoTtyDriver)
  63. {
  64. // Use mono's inbuilt terminfo driver to set the foreground color for us
  65. SafeConsole.ForegroundColor = color;
  66. }
  67. else
  68. {
  69. ConsoleOut.Write(TtyInfo.GetAnsiCode(color));
  70. }
  71. }
  72. public void SetConsoleTitle(string title)
  73. {
  74. if (StdoutRedirected)
  75. return;
  76. if (UseMonoTtyDriver && SafeConsole.TitleExists)
  77. {
  78. SafeConsole.Title = title;
  79. }
  80. else
  81. {
  82. ConsoleOut.Write($"\u001B]2;{title.Replace("\\", "\\\\")}\u0007");
  83. }
  84. }
  85. }
  86. }