You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.4 KiB

  1. using HarmonyLib;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection.Emit;
  6. using System.Text;
  7. namespace IPA.Logging
  8. {
  9. internal class StdoutInterceptor : TextWriter
  10. {
  11. public override Encoding Encoding => Encoding.Default;
  12. private bool isStdErr;
  13. public override void Write(char value)
  14. {
  15. Write(value.ToString());
  16. }
  17. private string lineBuffer = "";
  18. private object bufferLock = new object();
  19. public override void Write(string value)
  20. {
  21. lock (bufferLock)
  22. { // avoid threading issues
  23. lineBuffer += value;
  24. var parts = lineBuffer.Split(new[] { Environment.NewLine, "\n", "\r" }, StringSplitOptions.None);
  25. for (int i = 0; i < parts.Length; i++)
  26. {
  27. if (i + 1 == parts.Length) // last element
  28. lineBuffer = parts[i];
  29. else
  30. {
  31. var str = parts[i];
  32. if (string.IsNullOrEmpty(str)) continue;
  33. if (!isStdErr && WinConsole.IsInitialized)
  34. str = ConsoleColorToForegroundSet(currentColor) + str;
  35. if (isStdErr)
  36. Logger.stdout.Error(str);
  37. else
  38. Logger.stdout.Info(str);
  39. }
  40. }
  41. }
  42. }
  43. private const ConsoleColor defaultColor = ConsoleColor.Gray;
  44. private ConsoleColor currentColor = defaultColor;
  45. private static string ConsoleColorToForegroundSet(ConsoleColor col)
  46. {
  47. if (!WinConsole.UseVTEscapes) return "";
  48. string code = "0"; // reset
  49. switch (col)
  50. {
  51. case ConsoleColor.Black:
  52. code = "30";
  53. break;
  54. case ConsoleColor.DarkBlue:
  55. code = "34";
  56. break;
  57. case ConsoleColor.DarkGreen:
  58. code = "32";
  59. break;
  60. case ConsoleColor.DarkCyan:
  61. code = "36";
  62. break;
  63. case ConsoleColor.DarkRed:
  64. code = "31";
  65. break;
  66. case ConsoleColor.DarkMagenta:
  67. code = "35";
  68. break;
  69. case ConsoleColor.DarkYellow:
  70. code = "33";
  71. break;
  72. case ConsoleColor.Gray:
  73. code = "37";
  74. break;
  75. case ConsoleColor.DarkGray:
  76. code = "90"; // literally bright black
  77. break;
  78. case ConsoleColor.Blue:
  79. code = "94";
  80. break;
  81. case ConsoleColor.Green:
  82. code = "92";
  83. break;
  84. case ConsoleColor.Cyan:
  85. code = "96";
  86. break;
  87. case ConsoleColor.Red:
  88. code = "91";
  89. break;
  90. case ConsoleColor.Magenta:
  91. code = "95";
  92. break;
  93. case ConsoleColor.Yellow:
  94. code = "93";
  95. break;
  96. case ConsoleColor.White:
  97. code = "97";
  98. break;
  99. }
  100. return "\x1b[" + code + "m";
  101. }
  102. private static StdoutInterceptor stdoutInterceptor;
  103. private static StdoutInterceptor stderrInterceptor;
  104. private static class ConsoleHarmonyPatches
  105. {
  106. public static void Patch(Harmony harmony)
  107. {
  108. var console = typeof(Console);
  109. var resetColor = console.GetMethod("ResetColor");
  110. var foregroundProperty = console.GetProperty("ForegroundColor");
  111. var setFg = foregroundProperty?.GetSetMethod();
  112. var getFg = foregroundProperty?.GetGetMethod();
  113. if (resetColor != null)
  114. harmony.Patch(resetColor, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchResetColor)));
  115. if (foregroundProperty != null)
  116. {
  117. harmony.Patch(setFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchSetForegroundColor)));
  118. harmony.Patch(getFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), nameof(PatchGetForegroundColor)));
  119. }
  120. }
  121. public static ConsoleColor GetColor() => stdoutInterceptor.currentColor;
  122. public static void SetColor(ConsoleColor col) => stdoutInterceptor.currentColor = col;
  123. public static void ResetColor() => stdoutInterceptor.currentColor = defaultColor;
  124. public static IEnumerable<CodeInstruction> PatchGetForegroundColor(IEnumerable<CodeInstruction> _)
  125. {
  126. var getColorM = typeof(ConsoleHarmonyPatches).GetMethod("GetColor");
  127. return new[] {
  128. new CodeInstruction(OpCodes.Tailcall),
  129. new CodeInstruction(OpCodes.Call, getColorM),
  130. new CodeInstruction(OpCodes.Ret)
  131. };
  132. }
  133. public static IEnumerable<CodeInstruction> PatchSetForegroundColor(IEnumerable<CodeInstruction> _)
  134. {
  135. var setColorM = typeof(ConsoleHarmonyPatches).GetMethod("SetColor");
  136. return new[] {
  137. new CodeInstruction(OpCodes.Ldarg_0),
  138. new CodeInstruction(OpCodes.Tailcall),
  139. new CodeInstruction(OpCodes.Call, setColorM),
  140. new CodeInstruction(OpCodes.Ret)
  141. };
  142. }
  143. public static IEnumerable<CodeInstruction> PatchResetColor(IEnumerable<CodeInstruction> _)
  144. {
  145. var resetColor = typeof(ConsoleHarmonyPatches).GetMethod("ResetColor");
  146. return new[] {
  147. new CodeInstruction(OpCodes.Tailcall),
  148. new CodeInstruction(OpCodes.Call, resetColor),
  149. new CodeInstruction(OpCodes.Ret)
  150. };
  151. }
  152. }
  153. private static Harmony harmony;
  154. private static bool usingInterceptor = false;
  155. public static void Intercept()
  156. {
  157. if (!usingInterceptor)
  158. {
  159. usingInterceptor = true;
  160. if (harmony == null)
  161. harmony = new Harmony("BSIPA Console Redirector Patcher");
  162. if (stdoutInterceptor == null)
  163. stdoutInterceptor = new StdoutInterceptor();
  164. if (stderrInterceptor == null)
  165. stderrInterceptor = new StdoutInterceptor() { isStdErr = true };
  166. RedirectConsole();
  167. ConsoleHarmonyPatches.Patch(harmony);
  168. }
  169. }
  170. public static void RedirectConsole()
  171. {
  172. if (usingInterceptor)
  173. {
  174. Console.SetOut(stdoutInterceptor);
  175. Console.SetError(stderrInterceptor);
  176. }
  177. }
  178. }
  179. }