Factor out command script code.

This also allows comments in command scripts (lines starting with ;, # or //) to be used across all servers
connector_plugin
Justin Clark-Casey (justincc) 2012-11-22 05:57:20 +00:00
parent 9fcf3f1a3f
commit 3ce00e97cc
3 changed files with 50 additions and 77 deletions

View File

@ -201,6 +201,11 @@ namespace OpenSim.Framework.Servers
"General", false, "config save",
"config save <path>",
"Save current configuration to a file at the given path", HandleConfig);
m_console.Commands.AddCommand(
"General", false, "command-script",
"command-script <script>",
"Run a command script from file", HandleScript);
}
public virtual void HandleShow(string module, string[] cmd)
@ -363,7 +368,50 @@ namespace OpenSim.Framework.Servers
private void ShowLogLevel()
{
Notice(String.Format("Console log level is {0}", m_consoleAppender.Threshold));
Notice("Console log level is {0}", m_consoleAppender.Threshold);
}
protected virtual void HandleScript(string module, string[] parms)
{
if (parms.Length != 2)
{
Notice("Usage: command-script <path-to-script");
return;
}
RunCommandScript(parms[1]);
}
/// <summary>
/// Run an optional startup list of commands
/// </summary>
/// <param name="fileName"></param>
protected void RunCommandScript(string fileName)
{
if (m_console == null)
return;
if (File.Exists(fileName))
{
m_log.Info("[SERVER BASE]: Running " + fileName);
using (StreamReader readFile = File.OpenText(fileName))
{
string currentCommand;
while ((currentCommand = readFile.ReadLine()) != null)
{
currentCommand = currentCommand.Trim();
if (!(currentCommand == ""
|| currentCommand.StartsWith(";")
|| currentCommand.StartsWith("//")
|| currentCommand.StartsWith("#")))
{
m_log.Info("[SERVER BASE]: Running '" + currentCommand + "'");
m_console.RunCommand(currentCommand);
}
}
}
}
}
/// <summary>
@ -509,7 +557,7 @@ namespace OpenSim.Framework.Servers
/// </summary>
/// <param name="format"></param>
/// <param name="components"></param>
protected void Notice(string format, params string[] components)
protected void Notice(string format, params object[] components)
{
if (m_console != null)
m_console.OutputFormat(format, components);

View File

@ -466,35 +466,6 @@ namespace OpenSim
MainConsole.Instance.Output("");
}
/// <summary>
/// Run an optional startup list of commands
/// </summary>
/// <param name="fileName"></param>
private void RunCommandScript(string fileName)
{
if (File.Exists(fileName))
{
m_log.Info("[COMMANDFILE]: Running " + fileName);
using (StreamReader readFile = File.OpenText(fileName))
{
string currentCommand;
while ((currentCommand = readFile.ReadLine()) != null)
{
currentCommand = currentCommand.Trim();
if (!(currentCommand == ""
|| currentCommand.StartsWith(";")
|| currentCommand.StartsWith("//")
|| currentCommand.StartsWith("#")))
{
m_log.Info("[COMMANDFILE]: Running '" + currentCommand + "'");
m_console.RunCommand(currentCommand);
}
}
}
}
}
/// <summary>
/// Opens a file and uses it as input to the console command parser.
/// </summary>
@ -650,13 +621,6 @@ namespace OpenSim
switch (command)
{
case "command-script":
if (cmdparams.Length > 0)
{
RunCommandScript(cmdparams[0]);
}
break;
case "backup":
MainConsole.Instance.Output("Triggering save of pending object updates to persistent store");
SceneManager.BackupCurrentScene();

View File

@ -197,11 +197,6 @@ namespace OpenSim.Server.Base
"shutdown",
"Quit the application", HandleQuit);
// Register a command to read other commands from a file
MainConsole.Instance.Commands.AddCommand("General", false, "command-script",
"command-script <script>",
"Run a command script from file", HandleScript);
// Allow derived classes to perform initialization that
// needs to be done after the console has opened
//
@ -239,40 +234,6 @@ namespace OpenSim.Server.Base
}
protected virtual void HandleScript(string module, string[] parms)
{
if (parms.Length != 2)
{
return;
}
RunCommandScript(parms[1]);
}
/// <summary>
/// Run an optional startup list of commands
/// </summary>
/// <param name="fileName"></param>
private void RunCommandScript(string fileName)
{
if (File.Exists(fileName))
{
m_log.Info("[COMMANDFILE]: Running " + fileName);
using (StreamReader readFile = File.OpenText(fileName))
{
string currentCommand;
while ((currentCommand = readFile.ReadLine()) != null)
{
if (currentCommand != String.Empty)
{
m_log.Info("[COMMANDFILE]: Running '" + currentCommand + "'");
MainConsole.Instance.RunCommand(currentCommand);
}
}
}
}
}
protected virtual void ReadConfig()
{
}