Thread: [wpdev-commits] wolfpack console.cpp,NONE,1.1 console.h,NONE,1.1
Brought to you by:
rip,
thiagocorrea
|
From: <dar...@us...> - 2003-09-09 03:42:40
|
Update of /cvsroot/wpdev/wolfpack In directory sc8-pr-cvs1:/tmp/cvs-serv32514 Added Files: console.cpp console.h Log Message: Trying out new console code. --- NEW FILE: console.cpp --- //================================================================================== // // Wolfpack Emu (WP) // UO Server Emulation Program // // Copyright 1997, 98 by Marcus Rating (Cironian) // Copyright 2001-2003 by holders identified in authors.txt // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Palace - Suite 330, Boston, MA 02111-1307, USA. // // * In addition to that license, if you are running this program or modified // * versions of it on a public system you HAVE TO make the complete source of // * the version used by you available or provide people with a location to // * download it. // // // // Wolfpack Homepage: http://wpdev.sf.net/ //================================================================================== // Wolfpack Includes #include "console.h" #include "pythonscript.h" #include "log.h" #include "globals.h" #include "world.h" #include "network.h" #include "srvparams.h" #include "player.h" #include "accounts.h" #include "prototypes.h" #include "inlines.h" // Library Includes #include <qstring.h> #include <qglobal.h> #include <qthread.h> #include <iostream> #if defined(Q_OS_WIN32) # include <windows.h> # include <conio.h> #endif #if defined(Q_OS_UNIX) #include <sys/types.h> #include <sys/time.h> #include <unistd.h> #else #endif using namespace std; //======================================================================================== // Constructor cConsole::cConsole() { bEnabled = true; // do nothing at the moment setStreams( &cin, &cout, &cerr, &cout ); } //======================================================================================== // Destuctor cConsole::~cConsole() { // Clean up any terminal settings } void cConsole::enabled(bool bState) { bEnabled = bState; } void cConsole::setStreams(istream *in, ostream *out, ostream *error, ostream *log) { inputstrm = in; outputstrm = out; errorstrm = error; logstrm = log; } //======================================================================================== // Send a message to the console void cConsole::send(const QString &sMessage) { if( outputstrm != NULL ) { (*outputstrm) << sMessage.latin1(); flush( *outputstrm ); } if( sMessage.contains( "\n" ) ) { #if defined(Q_OS_UNIX) && 0 sMessage.replace("\e[0m", ""); sMessage.replace("\e[1;32m", ""); sMessage.replace("\e[1;31m", ""); sMessage.replace("\e[1;33m", ""); sMessage.replace("\e[1;37m", ""); #endif incompleteLine_.append( sMessage ); // Split by \n QStringList lines = QStringList::split( "\n", incompleteLine_, true ); // Insert all except the last element for( int i = 0; i < lines.count()-1; ++i ) linebuffer_.push_back( lines[i] ); incompleteLine_ = lines[ lines.count() - 1 ]; } else { incompleteLine_.append( sMessage ); } } //======================================================================================== void cConsole::log( UINT8 logLevel, const QString &message ) { // Legacy Code QString msg = message; if( msg.endsWith( "\n" ) ) msg = msg.left( msg.length() - 1 ); Log::instance()->print( (eLogLevel)logLevel, msg + "\n" ); } //======================================================================================== // Send a message to the console void cConsole::error(const QString& sMessage) { if (errorstrm != NULL) (*errorstrm) << sMessage.latin1(); } //========================================================================================= // Get input from the console UI08 cConsole::getkey(void) { UI08 key = 0; // if (cin.peek()) // key = cin.get(); return key; } //========================================================================================= // Prepare a "progess" line void cConsole::PrepareProgress( const QString &sMessage ) { UI08 PrintedChars = sMessage.length() + 1; // one spacer send( sMessage.right(59) + " " ); ChangeColor( WPC_WHITE ); // Fill up the remaining chars with "....." for( UI08 i = 0; i < 60 - PrintedChars; i++ ) send( "." ); ChangeColor( WPC_NORMAL ); send( " [____]" ); } //========================================================================================= // Print Progress Done void cConsole::ProgressDone( void ) { send( "\b\b\b\b\b" ); // Go 5 Characters back ChangeColor( WPC_GREEN ); send( "done" ); ChangeColor( WPC_NORMAL ); send( "]\n" ); } //========================================================================================= // Print "Fail" void cConsole::ProgressFail( void ) { send( "\b\b\b\b\b" ); // Go 5 Characters back ChangeColor( WPC_RED ); send( "fail" ); ChangeColor( WPC_NORMAL ); send( "]\n" ); } //========================================================================================= // Print "Skip" (maps etc.) void cConsole::ProgressSkip( void ) { send( "\b\b\b\b\b" ); // Go 5 Characters back ChangeColor( WPC_YELLOW ); send( "skip" ); ChangeColor( WPC_NORMAL ); send( "]\n" ); } //========================================================================================= // Change the console Color void cConsole::ChangeColor( WPC_ColorKeys Color ) { #if defined(Q_OS_UNIX) QString cb = "\e[0m"; switch( Color ) { case WPC_GREEN: cb = "\e[1;32m"; break; case WPC_RED: cb = "\e[1;31m"; break; case WPC_YELLOW:cb = "\e[1;33m"; break; case WPC_NORMAL:cb = "\e[0m"; break; case WPC_WHITE: cb = "\e[1;37m"; break; default: cb = "\e[0m"; } send( cb ); #elif defined(Q_OS_WIN32) HANDLE ConsoleHandle = GetStdHandle( STD_OUTPUT_HANDLE ); UI16 ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; switch( Color ) { case WPC_GREEN: ColorKey = FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; case WPC_RED: ColorKey = FOREGROUND_RED | FOREGROUND_INTENSITY; break; case WPC_YELLOW: ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; break; case WPC_NORMAL: ColorKey = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; break; case WPC_WHITE: ColorKey = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; break; default: break; } SetConsoleTextAttribute( ConsoleHandle, ColorKey ); #endif } void cConsole::setConsoleTitle( const QString& data ) { #if defined(Q_OS_WIN32) SetConsoleTitle( data.latin1() ); #endif } bool cConsole::handleCommand( const QString &command, bool silentFail ) { cUOSocket *mSock; int i; char c = command.latin1()[0]; c = toupper(c); if( c == 'S' ) { secure = !secure; if( !secure ) Console::instance()->send("WOLFPACK: Secure mode disabled. Press ? for a commands list.\n"); else Console::instance()->send("WOLFPACK: Secure mode re-enabled.\n"); return true; } // Allow Help in Secure Mode if( secure && c != '?' ) { Console::instance()->send( "WOLFPACK: Secure mode prevents keyboard commands! Press 'S' to disable.\n" ); return false; } switch( c ) { case 'Q': Console::instance()->send("WOLFPACK: Immediate Shutdown initialized!\n"); keeprun=0; break; case '#': World::instance()->save(); SrvParams->flush(); break; case 'W': Console::instance()->send( "Current Users in the World:\n" ); mSock = cNetwork::instance()->first(); i = 0; for( mSock = cNetwork::instance()->first(); mSock; mSock = cNetwork::instance()->next() ) { if( mSock->player() ) Console::instance()->send( QString("%1) %2 [%3]\n").arg(++i).arg(mSock->player()->name()).arg(QString::number( mSock->player()->serial(), 16) ) ); } Console::instance()->send( tr("Total Users Online: %1\n").arg(cNetwork::instance()->count()) ); break; case 'A': //reload the accounts file Accounts::instance()->reload(); break; case 'R': reloadScripts(); break; case '?': Console::instance()->send("Console commands:\n"); Console::instance()->send(" Q: Shutdown the server.\n"); Console::instance()->send(" # - Save world\n" ); Console::instance()->send(" W - Display logged in characters\n" ); Console::instance()->send(" A - Reload accounts\n" ); Console::instance()->send(" R - Reload scripts\n" ); Console::instance()->send(" S - Toggle Secure mode " ); if( secure ) Console::instance()->send( "[enabled]\n" ); else Console::instance()->send( "[disabled]\n" ); Console::instance()->send( " ? - Commands list (this)\n" ); Console::instance()->send( "End of commands list.\n" ); break; default: break; } return true; } class cConsoleThread : public QThread { protected: virtual void run() { try { while( serverState < SHUTDOWN ) { char c = getch(); if( c > 0 && serverState == RUNNING ) { Console::instance()->send( QChar( c ) ); } else { Sleep( 100 ); } } } catch( ... ) { } } }; cConsoleThread *thread = 0; void cConsole::start() { thread = new cConsoleThread; thread->start(); } void cConsole::poll() { // Normally we would check if there is a command in the command queue and execute it } void cConsole::stop() { thread->wait(); delete thread; } --- NEW FILE: console.h --- //================================================================================== // // Wolfpack Emu (WP) // UO Server Emulation Program // // Copyright 1997, 98 by Marcus Rating (Cironian) // Copyright 2001-2003 by holders identified in authors.txt // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Palace - Suite 330, Boston, MA 02111-1307, USA. // // * In addition to that license, if you are running this program or modified // * versions of it on a public system you HAVE TO make the complete source of // * the version used by you available or provide people with a location to // * download it. // // // // Wolfpack Homepage: http://wpdev.sf.net/ //================================================================================== #if !defined(__WPCONSOLE_H__) #define __WPCONSOLE_H__ // Platform specifics #include "platform.h" // System Includes #include <iosfwd> #include <cstdarg> #include <cstdio> #include <qstringlist.h> #include <qstring.h> // Third Party includes // Forward class declaration class cPythonScript; // Wolfpack Includes #include "singleton.h" // Class definitions enum WPC_ColorKeys { WPC_NORMAL = 0, WPC_RED, WPC_GREEN, WPC_YELLOW, WPC_WHITE, }; class cConsole { private: QStringList linebuffer_; QString incompleteLine_; public: cConsole(); virtual ~cConsole(); void enabled(bool); void setStreams( std::istream *in, std::ostream *out, std::ostream *error, std::ostream *log ); // Send a message to the console void send(const QString &sMessage); // Log a message void log( UINT8 logLevel, const QString &message ); // Flag an error void error(const QString&); // Get input from the console UI08 getkey(void); // Prepare a // xxxxx -----------------------[ ] // line void PrepareProgress( const QString &sMessage ); void ProgressDone( void ); void ProgressFail( void ); void ProgressSkip( void ); void ChangeColor( WPC_ColorKeys Color ); void setConsoleTitle( const QString& data ); QStringList linebuffer() const { return linebuffer_; } bool handleCommand( const QString &command, bool silentFail = false ); void start(); void poll(); void stop(); private: std::istream *inputstrm; std::ostream *outputstrm; std::ostream *errorstrm; std::ostream *logstrm; bool bEnabled; }; typedef SingletonHolder< cConsole > Console; #endif |