ConsoleBase.cs 5.5 KB

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