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.

194 lines
6.9 KiB

  1. using Harmony;
  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(HarmonyInstance harmony)
  107. {
  108. var console = typeof(Console);
  109. var resetColor = console.GetMethod("ResetColor");
  110. var foregroundProperty = console.GetProperty("ForegroundColor");
  111. var setFg = foregroundProperty.SetMethod;
  112. var getFg = foregroundProperty.GetMethod;
  113. harmony.Patch(resetColor, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchResetColor"));
  114. harmony.Patch(setFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchSetForegroundColor"));
  115. harmony.Patch(getFg, transpiler: new HarmonyMethod(typeof(ConsoleHarmonyPatches), "PatchGetForegroundColor"));
  116. }
  117. public static ConsoleColor GetColor() => stdoutInterceptor.currentColor;
  118. public static void SetColor(ConsoleColor col) => stdoutInterceptor.currentColor = col;
  119. public static void ResetColor() => stdoutInterceptor.currentColor = defaultColor;
  120. public static IEnumerable<CodeInstruction> PatchGetForegroundColor(IEnumerable<CodeInstruction> _)
  121. {
  122. var getColorM = typeof(ConsoleHarmonyPatches).GetMethod("GetColor");
  123. return new[] {
  124. new CodeInstruction(OpCodes.Call, getColorM),
  125. new CodeInstruction(OpCodes.Ret)
  126. };
  127. }
  128. public static IEnumerable<CodeInstruction> PatchSetForegroundColor(IEnumerable<CodeInstruction> _)
  129. {
  130. var setColorM = typeof(ConsoleHarmonyPatches).GetMethod("SetColor");
  131. return new[] {
  132. new CodeInstruction(OpCodes.Ldarg_0),
  133. new CodeInstruction(OpCodes.Call, setColorM),
  134. new CodeInstruction(OpCodes.Ret)
  135. };
  136. }
  137. public static IEnumerable<CodeInstruction> PatchResetColor(IEnumerable<CodeInstruction> _)
  138. {
  139. var resetColor = typeof(ConsoleHarmonyPatches).GetMethod("ResetColor");
  140. return new[] {
  141. new CodeInstruction(OpCodes.Call, resetColor),
  142. new CodeInstruction(OpCodes.Ret)
  143. };
  144. }
  145. }
  146. private static HarmonyInstance harmony;
  147. private static bool usingInterceptor = false;
  148. public static void Intercept()
  149. {
  150. if (!usingInterceptor)
  151. {
  152. usingInterceptor = true;
  153. if (harmony == null)
  154. harmony = HarmonyInstance.Create("BSIPA Console Redirector Patcher");
  155. if (stdoutInterceptor == null)
  156. stdoutInterceptor = new StdoutInterceptor();
  157. if (stderrInterceptor == null)
  158. stderrInterceptor = new StdoutInterceptor() { isStdErr = true };
  159. RedirectConsole();
  160. ConsoleHarmonyPatches.Patch(harmony);
  161. }
  162. }
  163. public static void RedirectConsole()
  164. {
  165. if (usingInterceptor)
  166. {
  167. Console.SetOut(stdoutInterceptor);
  168. Console.SetError(stderrInterceptor);
  169. }
  170. }
  171. }
  172. }