From 0007711eb5947d292f10325dd4af640ece79ea2d Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Thu, 1 Mar 2012 03:23:10 +0000 Subject: Use a fully stubbed out MockConsole for unit tests rather than inheriting from CommandConsole. This is so that the static MainConsole.Instance doesn't retain references to methods registered by scene and other modules to service commands. This prevents the scene from being garbage collected at the end of a test. This is not the final thing preventing GC - next up is the timer started by SimStatsReporter that holds a reference to Scene that prevents end of test gc. --- OpenSim/Framework/Console/MockConsole.cs | 59 ++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 22 deletions(-) (limited to 'OpenSim/Framework/Console') diff --git a/OpenSim/Framework/Console/MockConsole.cs b/OpenSim/Framework/Console/MockConsole.cs index a29b370..4d8751f 100644 --- a/OpenSim/Framework/Console/MockConsole.cs +++ b/OpenSim/Framework/Console/MockConsole.cs @@ -29,6 +29,7 @@ using System; using System.Threading; using System.Collections.Generic; using System.Text; +using System.Xml; namespace OpenSim.Framework.Console { @@ -37,28 +38,42 @@ namespace OpenSim.Framework.Console /// Don't use this except for Unit Testing or you're in for a world of hurt when the /// sim gets to ReadLine /// - public class MockConsole : CommandConsole + public class MockConsole : ICommandConsole { - public MockConsole(string defaultPrompt) : base(defaultPrompt) - { - } - public override void Output(string text) - { - } - public override void Output(string text, string level) - { - } + private MockCommands m_commands = new MockCommands(); - public override string ReadLine(string p, bool isCommand, bool e) - { - //Thread.CurrentThread.Join(1000); - return string.Empty; - } - public override void UnlockOutput() - { - } - public override void LockOutput() - { - } + public ICommands Commands { get { return m_commands; } } + + public void Prompt() {} + + public void RunCommand(string cmd) {} + + public string ReadLine(string p, bool isCommand, bool e) { return ""; } + + public object ConsoleScene { get { return null; } } + + public void Output(string text, string level) {} + public void Output(string text) {} + public void OutputFormat(string format, params object[] components) {} + + public string CmdPrompt(string p) { return ""; } + public string CmdPrompt(string p, string def) { return ""; } + public string CmdPrompt(string p, List excludedCharacters) { return ""; } + public string CmdPrompt(string p, string def, List excludedCharacters) { return ""; } + + public string CmdPrompt(string prompt, string defaultresponse, List options) { return ""; } + + public string PasswdPrompt(string p) { return ""; } + } + + public class MockCommands : ICommands + { + public void FromXml(XmlElement root, CommandDelegate fn) {} + public List GetHelp(string[] cmd) { return null; } + public void AddCommand(string module, bool shared, string command, string help, string longhelp, CommandDelegate fn) {} + public void AddCommand(string module, bool shared, string command, string help, string longhelp, string descriptivehelp, CommandDelegate fn) {} + public string[] FindNextOption(string[] cmd, bool term) { return null; } + public string[] Resolve(string[] cmd) { return null; } + public XmlElement GetXml(XmlDocument doc) { return null; } } -} +} \ No newline at end of file -- cgit v1.1 From 749c3fef8ad2d3af97fcd9ab9c72740675e46715 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Thu, 8 Mar 2012 01:51:37 +0000 Subject: Change "help" to display categories/module list then "help " to display commands in a category. This is to deal with the hundred lines of command splurge when one previously typed "help" Modelled somewhat on the mysql console One can still type help to get per command help at any point. Categories capitalized to avoid conflict with the all-lowercase commands (except for commander system, as of yet). Does not affect command parsing or any other aspects of the console apart from the help system. Backwards compatible with existing modules. --- OpenSim/Framework/Console/CommandConsole.cs | 114 ++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 22 deletions(-) (limited to 'OpenSim/Framework/Console') diff --git a/OpenSim/Framework/Console/CommandConsole.cs b/OpenSim/Framework/Console/CommandConsole.cs index 0d6288b..2bb7de1 100644 --- a/OpenSim/Framework/Console/CommandConsole.cs +++ b/OpenSim/Framework/Console/CommandConsole.cs @@ -29,6 +29,7 @@ using System; using System.Xml; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; @@ -40,6 +41,8 @@ namespace OpenSim.Framework.Console { public class Commands : ICommands { +// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + /// /// Encapsulates a command that can be invoked from the console /// @@ -76,6 +79,8 @@ namespace OpenSim.Framework.Console public List fn; } + public const string GeneralHelpText = "For more information, type 'help ' where is one of the following categories:"; + /// /// Commands organized by keyword in a tree /// @@ -83,6 +88,11 @@ namespace OpenSim.Framework.Console new Dictionary(); /// + /// Commands organized by module + /// + private Dictionary> m_modulesCommands = new Dictionary>(); + + /// /// Get help for the given help string /// /// Parsed parts of the help string. If empty then general help is returned. @@ -98,8 +108,8 @@ namespace OpenSim.Framework.Console // General help if (helpParts.Count == 0) { - help.AddRange(CollectHelp(tree)); - help.Sort(); + help.Add(GeneralHelpText); + help.AddRange(CollectModulesHelp(tree)); } else { @@ -118,6 +128,13 @@ namespace OpenSim.Framework.Console { string originalHelpRequest = string.Join(" ", helpParts.ToArray()); List help = new List(); + + // Check modules first to see if we just need to display a list of those commands + if (TryCollectModuleHelp(originalHelpRequest, help)) + { + help.Insert(0, GeneralHelpText); + return help; + } Dictionary dict = tree; while (helpParts.Count > 0) @@ -161,25 +178,61 @@ namespace OpenSim.Framework.Console return help; } - private List CollectHelp(Dictionary dict) + /// + /// Try to collect help for the given module if that module exists. + /// + /// + /// /param> + /// true if there was the module existed, false otherwise. + private bool TryCollectModuleHelp(string moduleName, List helpText) { - List result = new List(); - - foreach (KeyValuePair kvp in dict) + lock (m_modulesCommands) { - if (kvp.Value is Dictionary) + if (m_modulesCommands.ContainsKey(moduleName)) { - result.AddRange(CollectHelp((Dictionary)kvp.Value)); + List commands = m_modulesCommands[moduleName]; + var ourHelpText = commands.ConvertAll(c => string.Format("{0} - {1}", c.help_text, c.long_help)); + ourHelpText.Sort(); + helpText.AddRange(ourHelpText); + + return true; } else { - if (((CommandInfo)kvp.Value).long_help != String.Empty) - result.Add(((CommandInfo)kvp.Value).help_text+" - "+ - ((CommandInfo)kvp.Value).long_help); + return false; } } - return result; } + + private List CollectModulesHelp(Dictionary dict) + { + lock (m_modulesCommands) + { + List helpText = new List(m_modulesCommands.Keys); + helpText.Sort(); + return helpText; + } + } + +// private List CollectHelp(Dictionary dict) +// { +// List result = new List(); +// +// foreach (KeyValuePair kvp in dict) +// { +// if (kvp.Value is Dictionary) +// { +// result.AddRange(CollectHelp((Dictionary)kvp.Value)); +// } +// else +// { +// if (((CommandInfo)kvp.Value).long_help != String.Empty) +// result.Add(((CommandInfo)kvp.Value).help_text+" - "+ +// ((CommandInfo)kvp.Value).long_help); +// } +// } +// return result; +// } /// /// Add a command to those which can be invoked from the console. @@ -212,21 +265,19 @@ namespace OpenSim.Framework.Console Dictionary current = tree; - foreach (string s in parts) + foreach (string part in parts) { - if (current.ContainsKey(s)) + if (current.ContainsKey(part)) { - if (current[s] is Dictionary) - { - current = (Dictionary)current[s]; - } + if (current[part] is Dictionary) + current = (Dictionary)current[part]; else return; } else { - current[s] = new Dictionary(); - current = (Dictionary)current[s]; + current[part] = new Dictionary(); + current = (Dictionary)current[part]; } } @@ -250,6 +301,24 @@ namespace OpenSim.Framework.Console info.fn = new List(); info.fn.Add(fn); current[String.Empty] = info; + + // Now add command to modules dictionary + lock (m_modulesCommands) + { + List commands; + if (m_modulesCommands.ContainsKey(module)) + { + commands = m_modulesCommands[module]; + } + else + { + commands = new List(); + m_modulesCommands[module] = commands; + } + +// m_log.DebugFormat("[COMMAND CONSOLE]: Adding to category {0} command {1}", module, command); + commands.Add(info); + } } public string[] FindNextOption(string[] cmd, bool term) @@ -607,8 +676,9 @@ namespace OpenSim.Framework.Console { Commands = new Commands(); - Commands.AddCommand("console", false, "help", "help []", - "Get general command list or more detailed help on a specific command", Help); + Commands.AddCommand( + "Help", false, "help", "help []", + "Display help on a particular command or on a list of commands in a category", Help); } private void Help(string module, string[] cmd) -- cgit v1.1