Planeshift
|
00001 /* 00002 * serverconsole.h - author: Matze Braun <[email protected]> 00003 * 00004 * Copyright (C) 2001 Atomic Blue ([email protected], http://www.atomicblue.org) 00005 * 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation (version 2 of the License) 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 * 00018 */ 00019 #ifndef __SERVERCONSOLE_H__ 00020 #define __SERVERCONSOLE_H__ 00021 00022 #include <csutil/threading/thread.h> 00023 #include "util/consoleout.h" 00024 #include "util/gameevent.h" 00025 00026 #ifdef USE_ANSI_ESCAPE 00027 #define COL_NORMAL "\033[m\017" 00028 #define COL_RED "\033[31m" 00029 #define COL_BLUE "\033[34m" 00030 #define COL_YELLOW "\033[33m" 00031 #define COL_CYAN "\033[36m" 00032 #define COL_GREEN "\033[32m" 00033 #else 00034 #define COL_NORMAL "" 00035 #define COL_RED "" 00036 #define COL_BLUE "" 00037 #define COL_CYAN "" 00038 #define COL_GREEN "" 00039 #define COL_YELLOW "" 00040 #endif 00041 00042 struct COMMAND; 00043 00044 const COMMAND *find_command(const char *name); 00045 int execute_line(const char *line,csString *buffer); 00046 00047 struct iObjectRegistry; 00048 00057 class iCommandCatcher 00058 { 00059 public: 00060 virtual void CatchCommand(const char *cmd) = 0; 00061 virtual ~iCommandCatcher() {} 00062 }; 00063 00067 class ServerConsole : public ConsoleOut, public CS::Threading::Runnable 00068 { 00069 public: 00070 ServerConsole(iObjectRegistry *oreg, const char *appname, const char *prompt); 00071 ~ServerConsole(); 00072 00081 static void ExecuteScript(const char* script); 00082 00087 void Run(); 00088 00095 void MainLoop(); 00096 00097 void SetCommandCatcher(iCommandCatcher *cmdcatch) 00098 { 00099 cmdcatcher = cmdcatch; 00100 } 00101 00103 static const char *prompt; 00104 00105 protected: 00107 const char *appname; 00108 00110 csRef<CS::Threading::Thread> thread; 00111 00113 bool stop; 00114 00116 iCommandCatcher *cmdcatcher; 00117 00118 iObjectRegistry *objreg; 00119 }; 00120 00121 // Allows the console commands to be thread-safe by inserting them into the main event queue 00122 class psServerConsoleCommand : public psGameEvent 00123 { 00124 csString command; 00125 00126 public: 00127 // 0 offset for highest priority in the game event queue 00128 psServerConsoleCommand(const char* command) : psGameEvent(0, 0, "psServerStatusRunEvent"), command(command) {}; 00129 void Trigger() 00130 { 00131 execute_line(command,NULL); 00132 CPrintf (CON_CMDOUTPUT, COL_BLUE "%s: " COL_NORMAL, ServerConsole::prompt); 00133 }; 00134 virtual csString ToString() const 00135 { 00136 return command; 00137 } 00138 }; 00139 00142 #endif 00143