| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Input;
- namespace DwarfCorp
- {
- /// <summary>
- /// Provides static access to debugger switches toggleable on god menu
- /// </summary>
- public static class Debugger
- {
- public static Object ConsoleCommandContext = null;
- public static void ClearConsoleCommandContext() { ConsoleCommandContext = null; }
- public static void SetConsoleCommandContext(Object Context) { ConsoleCommandContext = Context; }
- public static class Switches
- {
- public static bool DrawBoundingBoxes = false;
- public static bool DrawPaths = false;
- public static bool DrawRailNetwork = false;
- public static bool DrawPipeNetwork = false;
- public static bool DrawToolDebugInfo = false;
- public static bool HideTerrain = false;
- public static bool ABTestSwitch = false;
- public static bool DrawComposites = false;
- public static bool DrawSelectionBuffer = false;
- public static bool HideSliceTop = false;
- public static bool DebugElevators = false;
- public static bool DrawUpdateBox = false;
- public static bool DisableWaterUpdate = false;
- public static bool DrawInvisible = false;
- public static bool ShowEntityInfo = false;
- public static bool DrawTiledInstanceAtlas = false;
- public static bool UseNewVoxelGeoGen = false;
- }
- public class Switch
- {
- public String Name;
- public Action<bool> Set;
- public bool State;
- }
- public static IEnumerable<Switch> EnumerateSwitches()
- {
- foreach (var member in typeof(Switches).GetFields(global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.Static))
- {
- global::System.Diagnostics.Debug.Assert(member.FieldType == typeof(bool));
- yield return new Switch
- {
- Name = member.Name,
- Set = (value) => member.SetValue(null, value),
- State = (member.GetValue(null) as bool?).Value
- };
- }
- }
- internal static string GetNicelyFormattedName(string Name)
- {
- var r = "";
- foreach (var c in Name)
- {
- if (c >= 'A' && c <= 'Z' && !String.IsNullOrEmpty(r))
- r += " ";
- r += c;
- }
- return r;
- }
- public static void RaiseDebugWarning(String Message)
- {
- #if DEBUG
- throw new InvalidProgramException(Message);
- #endif
- }
- private static Dictionary<String, Func<String, String>> CommandHandlers = null;
- public static String HandleConsoleCommand(String Command)
- {
- if (CommandHandlers == null)
- {
- CommandHandlers = new Dictionary<string, Func<string, string>>();
- foreach (var hook in AssetManager.EnumerateModHooks(typeof(ConsoleCommandHandlerAttribute), typeof(String), new Type[] { typeof(String) }))
- {
- var lambdaCopy = hook;
- var attribute = hook.GetCustomAttributes(false).FirstOrDefault(a => a is ConsoleCommandHandlerAttribute) as ConsoleCommandHandlerAttribute;
- if (attribute == null) continue;
- CommandHandlers[attribute.Name.ToUpperInvariant()] = (s) => lambdaCopy.Invoke(null, new Object[] { s }) as String;
- }
- }
- var commandWord = "";
- var commandArgs = "";
- var space = Command.IndexOf(' ');
- if (space == -1)
- commandWord = Command;
- else
- {
- commandWord = Command.Substring(0, space);
- commandArgs = Command.Substring(space + 1);
- }
- if (CommandHandlers.ContainsKey(commandWord.ToUpperInvariant()))
- return CommandHandlers[commandWord.ToUpperInvariant()](commandArgs);
- else
- return "Unknown command.";
- }
- [ConsoleCommandHandler("HELP")]
- public static string ListSettings(String Name)
- {
- var builder = new StringBuilder();
- foreach (var hook in AssetManager.EnumerateModHooks(typeof(ConsoleCommandHandlerAttribute), typeof(String), new Type[] { typeof(String) }))
- {
- var lambdaCopy = hook;
- var attribute = hook.GetCustomAttributes(false).FirstOrDefault(a => a is ConsoleCommandHandlerAttribute) as ConsoleCommandHandlerAttribute;
- if (attribute == null) continue;
- builder.AppendLine(attribute.Name.ToUpperInvariant());
- }
- return builder.ToString();
- }
- }
- }
|