ConsoleBase.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.IO;
  3. namespace OpenSim.Framework.Console
  4. {
  5. public class ConsoleBase
  6. {
  7. StreamWriter Log;
  8. public conscmd_callback cmdparser;
  9. public string componentname;
  10. // STUPID HACK ALERT!!!! STUPID HACK ALERT!!!!!
  11. // constype - the type of console to use (see enum ConsoleType)
  12. // sparam - depending on the console type:
  13. // TCP - the IP to bind to (127.0.0.1 if blank)
  14. // Local - param ignored
  15. // and for the iparam:
  16. // TCP - the port to bind to
  17. // Local - param ignored
  18. // LogFile - duh
  19. // componentname - which component of the OGS system? (user, asset etc)
  20. // cmdparser - a reference to a conscmd_callback object
  21. public ConsoleBase(string LogFile, string componentname, conscmd_callback cmdparser)
  22. {
  23. this.componentname = componentname;
  24. this.cmdparser = cmdparser;
  25. System.Console.WriteLine("ServerConsole.cs - creating new local console");
  26. System.Console.WriteLine("Logs will be saved to current directory in " + LogFile);
  27. Log = File.AppendText(LogFile);
  28. Log.WriteLine("========================================================================");
  29. Log.WriteLine(componentname + " Started at " + DateTime.Now.ToString());
  30. }
  31. public void Close()
  32. {
  33. Log.WriteLine("Shutdown at " + DateTime.Now.ToString());
  34. Log.Close();
  35. }
  36. public void Write(string format, params object[] args)
  37. {
  38. Log.Write(format, args);
  39. System.Console.Write(format, args);
  40. return;
  41. }
  42. public void WriteLine(string format, params object[] args)
  43. {
  44. Log.WriteLine(format, args);
  45. System.Console.WriteLine(format, args);
  46. return;
  47. }
  48. public string ReadLine()
  49. {
  50. string TempStr = System.Console.ReadLine();
  51. Log.WriteLine(TempStr);
  52. return TempStr;
  53. }
  54. public int Read()
  55. {
  56. int TempInt = System.Console.Read();
  57. Log.Write((char)TempInt);
  58. return TempInt;
  59. }
  60. // Displays a prompt and waits for the user to enter a string, then returns that string
  61. // Done with no echo and suitable for passwords
  62. public string PasswdPrompt(string prompt)
  63. {
  64. // FIXME: Needs to be better abstracted
  65. Log.WriteLine(prompt);
  66. this.Write(prompt);
  67. ConsoleColor oldfg = System.Console.ForegroundColor;
  68. System.Console.ForegroundColor = System.Console.BackgroundColor;
  69. string temp = System.Console.ReadLine();
  70. System.Console.ForegroundColor = oldfg;
  71. return temp;
  72. }
  73. // Displays a command prompt and waits for the user to enter a string, then returns that string
  74. public string CmdPrompt(string prompt)
  75. {
  76. this.Write(prompt);
  77. return this.ReadLine();
  78. }
  79. // Displays a command prompt and returns a default value if the user simply presses enter
  80. public string CmdPrompt(string prompt, string defaultresponse)
  81. {
  82. string temp = CmdPrompt(prompt);
  83. if (temp == "")
  84. {
  85. return defaultresponse;
  86. }
  87. else
  88. {
  89. return temp;
  90. }
  91. }
  92. // Displays a command prompt and returns a default value, user may only enter 1 of 2 options
  93. public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB)
  94. {
  95. bool itisdone = false;
  96. string temp = CmdPrompt(prompt, defaultresponse);
  97. while (itisdone == false)
  98. {
  99. if ((temp == OptionA) || (temp == OptionB))
  100. {
  101. itisdone = true;
  102. }
  103. else
  104. {
  105. this.WriteLine("Valid options are " + OptionA + " or " + OptionB);
  106. temp = CmdPrompt(prompt, defaultresponse);
  107. }
  108. }
  109. return temp;
  110. }
  111. // Runs a command with a number of parameters
  112. public Object RunCmd(string Cmd, string[] cmdparams)
  113. {
  114. cmdparser.RunCmd(Cmd, cmdparams);
  115. return null;
  116. }
  117. // Shows data about something
  118. public void ShowCommands(string ShowWhat)
  119. {
  120. cmdparser.Show(ShowWhat);
  121. }
  122. public void MainConsolePrompt()
  123. {
  124. string[] tempstrarray;
  125. string tempstr = this.CmdPrompt(this.componentname + "# ");
  126. tempstrarray = tempstr.Split(' ');
  127. string cmd = tempstrarray[0];
  128. Array.Reverse(tempstrarray);
  129. Array.Resize<string>(ref tempstrarray, tempstrarray.Length - 1);
  130. Array.Reverse(tempstrarray);
  131. string[] cmdparams = (string[])tempstrarray;
  132. RunCmd(cmd, cmdparams);
  133. }
  134. }
  135. }