SafeConsole.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // --------------------------------------------------
  2. // UnityInjector - SafeConsole.cs
  3. // Copyright (c) Usagirei 2015 - 2015
  4. // --------------------------------------------------
  5. using System;
  6. using System.Reflection;
  7. namespace UnityInjector.ConsoleUtil
  8. {
  9. /// <summary>
  10. /// Console class with safe handlers for Unity 4.x, which does not have a proper Console implementation
  11. /// </summary>
  12. internal static class SafeConsole
  13. {
  14. public static bool BackgroundColorExists { get; private set; }
  15. private static GetColorDelegate _getBackgroundColor;
  16. private static SetColorDelegate _setBackgroundColor;
  17. public static ConsoleColor BackgroundColor
  18. {
  19. get => _getBackgroundColor();
  20. set => _setBackgroundColor(value);
  21. }
  22. public static bool ForegroundColorExists { get; private set; }
  23. private static GetColorDelegate _getForegroundColor;
  24. private static SetColorDelegate _setForegroundColor;
  25. public static ConsoleColor ForegroundColor
  26. {
  27. get => _getForegroundColor();
  28. set => _setForegroundColor(value);
  29. }
  30. public static bool TitleExists { get; private set; }
  31. private static GetStringDelegate _getTitle;
  32. private static SetStringDelegate _setTitle;
  33. public static string Title
  34. {
  35. get => _getTitle();
  36. set => _setTitle(value);
  37. }
  38. static SafeConsole()
  39. {
  40. var tConsole = typeof(Console);
  41. InitColors(tConsole);
  42. }
  43. private static void InitColors(Type tConsole)
  44. {
  45. const BindingFlags BINDING_FLAGS = BindingFlags.Public | BindingFlags.Static;
  46. var gfc = tConsole.GetMethod("get_ForegroundColor", BINDING_FLAGS);
  47. var sfc = tConsole.GetMethod("set_ForegroundColor", BINDING_FLAGS);
  48. var gbc = tConsole.GetMethod("get_BackgroundColor", BINDING_FLAGS);
  49. var sbc = tConsole.GetMethod("set_BackgroundColor", BINDING_FLAGS);
  50. var gtt = tConsole.GetMethod("get_Title", BINDING_FLAGS);
  51. var stt = tConsole.GetMethod("set_Title", BINDING_FLAGS);
  52. _setForegroundColor = sfc != null
  53. ? (SetColorDelegate)Delegate.CreateDelegate(typeof(SetColorDelegate), sfc)
  54. : (value => { });
  55. _setBackgroundColor = sbc != null
  56. ? (SetColorDelegate)Delegate.CreateDelegate(typeof(SetColorDelegate), sbc)
  57. : (value => { });
  58. _getForegroundColor = gfc != null
  59. ? (GetColorDelegate)Delegate.CreateDelegate(typeof(GetColorDelegate), gfc)
  60. : (() => ConsoleColor.Gray);
  61. _getBackgroundColor = gbc != null
  62. ? (GetColorDelegate)Delegate.CreateDelegate(typeof(GetColorDelegate), gbc)
  63. : (() => ConsoleColor.Black);
  64. _getTitle = gtt != null
  65. ? (GetStringDelegate)Delegate.CreateDelegate(typeof(GetStringDelegate), gtt)
  66. : (() => string.Empty);
  67. _setTitle = stt != null
  68. ? (SetStringDelegate)Delegate.CreateDelegate(typeof(SetStringDelegate), stt)
  69. : (value => { });
  70. BackgroundColorExists = _setBackgroundColor != null && _getBackgroundColor != null;
  71. ForegroundColorExists = _setForegroundColor != null && _getForegroundColor != null;
  72. TitleExists = _setTitle != null && _getTitle != null;
  73. }
  74. private delegate ConsoleColor GetColorDelegate();
  75. private delegate void SetColorDelegate(ConsoleColor value);
  76. private delegate string GetStringDelegate();
  77. private delegate void SetStringDelegate(string value);
  78. }
  79. }