Debugger.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.Xna.Framework.Input;
  7. namespace DwarfCorp
  8. {
  9. /// <summary>
  10. /// Provides static access to debugger switches toggleable on god menu
  11. /// </summary>
  12. public static class Debugger
  13. {
  14. public static Object ConsoleCommandContext = null;
  15. public static void ClearConsoleCommandContext() { ConsoleCommandContext = null; }
  16. public static void SetConsoleCommandContext(Object Context) { ConsoleCommandContext = Context; }
  17. public static class Switches
  18. {
  19. public static bool DrawBoundingBoxes = false;
  20. public static bool DrawPaths = false;
  21. public static bool DrawRailNetwork = false;
  22. public static bool DrawPipeNetwork = false;
  23. public static bool DrawToolDebugInfo = false;
  24. public static bool HideTerrain = false;
  25. public static bool ABTestSwitch = false;
  26. public static bool DrawComposites = false;
  27. public static bool DrawSelectionBuffer = false;
  28. public static bool HideSliceTop = false;
  29. public static bool DebugElevators = false;
  30. public static bool DrawUpdateBox = false;
  31. public static bool DisableWaterUpdate = false;
  32. public static bool DrawInvisible = false;
  33. public static bool ShowEntityInfo = false;
  34. public static bool DrawTiledInstanceAtlas = false;
  35. public static bool UseNewVoxelGeoGen = false;
  36. }
  37. public class Switch
  38. {
  39. public String Name;
  40. public Action<bool> Set;
  41. public bool State;
  42. }
  43. public static IEnumerable<Switch> EnumerateSwitches()
  44. {
  45. foreach (var member in typeof(Switches).GetFields(global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.Static))
  46. {
  47. global::System.Diagnostics.Debug.Assert(member.FieldType == typeof(bool));
  48. yield return new Switch
  49. {
  50. Name = member.Name,
  51. Set = (value) => member.SetValue(null, value),
  52. State = (member.GetValue(null) as bool?).Value
  53. };
  54. }
  55. }
  56. internal static string GetNicelyFormattedName(string Name)
  57. {
  58. var r = "";
  59. foreach (var c in Name)
  60. {
  61. if (c >= 'A' && c <= 'Z' && !String.IsNullOrEmpty(r))
  62. r += " ";
  63. r += c;
  64. }
  65. return r;
  66. }
  67. public static void RaiseDebugWarning(String Message)
  68. {
  69. #if DEBUG
  70. throw new InvalidProgramException(Message);
  71. #endif
  72. }
  73. private static Dictionary<String, Func<String, String>> CommandHandlers = null;
  74. public static String HandleConsoleCommand(String Command)
  75. {
  76. if (CommandHandlers == null)
  77. {
  78. CommandHandlers = new Dictionary<string, Func<string, string>>();
  79. foreach (var hook in AssetManager.EnumerateModHooks(typeof(ConsoleCommandHandlerAttribute), typeof(String), new Type[] { typeof(String) }))
  80. {
  81. var lambdaCopy = hook;
  82. var attribute = hook.GetCustomAttributes(false).FirstOrDefault(a => a is ConsoleCommandHandlerAttribute) as ConsoleCommandHandlerAttribute;
  83. if (attribute == null) continue;
  84. CommandHandlers[attribute.Name.ToUpperInvariant()] = (s) => lambdaCopy.Invoke(null, new Object[] { s }) as String;
  85. }
  86. }
  87. var commandWord = "";
  88. var commandArgs = "";
  89. var space = Command.IndexOf(' ');
  90. if (space == -1)
  91. commandWord = Command;
  92. else
  93. {
  94. commandWord = Command.Substring(0, space);
  95. commandArgs = Command.Substring(space + 1);
  96. }
  97. if (CommandHandlers.ContainsKey(commandWord.ToUpperInvariant()))
  98. return CommandHandlers[commandWord.ToUpperInvariant()](commandArgs);
  99. else
  100. return "Unknown command.";
  101. }
  102. [ConsoleCommandHandler("HELP")]
  103. public static string ListSettings(String Name)
  104. {
  105. var builder = new StringBuilder();
  106. foreach (var hook in AssetManager.EnumerateModHooks(typeof(ConsoleCommandHandlerAttribute), typeof(String), new Type[] { typeof(String) }))
  107. {
  108. var lambdaCopy = hook;
  109. var attribute = hook.GetCustomAttributes(false).FirstOrDefault(a => a is ConsoleCommandHandlerAttribute) as ConsoleCommandHandlerAttribute;
  110. if (attribute == null) continue;
  111. builder.AppendLine(attribute.Name.ToUpperInvariant());
  112. }
  113. return builder.ToString();
  114. }
  115. }
  116. }