2008-09-17 13:18:22 +03:00
|
|
|
#define VCMI_DLL
|
2007-08-15 18:13:11 +03:00
|
|
|
#include "stdafx.h"
|
2007-08-17 20:42:21 +03:00
|
|
|
#include "CConsoleHandler.h"
|
|
|
|
#include "CAdvmapInterface.h"
|
2008-08-22 15:21:09 +03:00
|
|
|
#include "CCastleInterface.h"
|
2007-11-19 00:58:28 +02:00
|
|
|
#include "CPlayerInterface.h"
|
2007-08-17 20:42:21 +03:00
|
|
|
#include "CGameInfo.h"
|
|
|
|
#include "global.h"
|
2007-08-29 15:18:31 +03:00
|
|
|
#include "CGameState.h"
|
|
|
|
#include "CCallback.h"
|
2007-09-18 16:30:26 +03:00
|
|
|
#include "CPathfinder.h"
|
|
|
|
#include "mapHandler.h"
|
2007-08-17 20:42:21 +03:00
|
|
|
#include <sstream>
|
2007-10-17 01:39:11 +03:00
|
|
|
#include "SDL_Extensions.h"
|
2007-11-19 00:58:28 +02:00
|
|
|
#include "hch/CHeroHandler.h"
|
2008-02-26 01:26:50 +02:00
|
|
|
#include "hch/CLodHandler.h"
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2008-09-17 13:18:22 +03:00
|
|
|
#include "boost/function.hpp"
|
|
|
|
#include <boost/thread.hpp>
|
2008-09-18 23:24:53 +03:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
#include <windows.h>
|
|
|
|
HANDLE handleIn;
|
|
|
|
HANDLE handleOut;
|
2008-06-21 16:27:52 +03:00
|
|
|
#endif
|
2008-09-18 23:24:53 +03:00
|
|
|
WORD defColor;
|
2008-06-21 16:27:52 +03:00
|
|
|
|
2008-09-17 13:18:22 +03:00
|
|
|
void CConsoleHandler::setColor(int level)
|
|
|
|
{
|
|
|
|
WORD color;
|
|
|
|
switch(level)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
color = defColor;
|
|
|
|
break;
|
|
|
|
case 0:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;32m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;31m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;35m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 3:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;32m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 4:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[1;40;39m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
case 5:
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
color = "\x1b[0;40;39m";
|
|
|
|
#endif
|
2008-09-17 13:18:22 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
color = defColor;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
SetConsoleTextAttribute(handleOut,color);
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
std::cout << color;
|
2008-09-17 13:18:22 +03:00
|
|
|
#endif
|
|
|
|
}
|
2008-06-21 16:27:52 +03:00
|
|
|
|
2008-09-17 13:18:22 +03:00
|
|
|
int CConsoleHandler::run()
|
2007-08-15 18:13:11 +03:00
|
|
|
{
|
2008-10-26 22:58:34 +02:00
|
|
|
char buffer[5000];
|
2007-08-15 18:13:11 +03:00
|
|
|
while(true)
|
|
|
|
{
|
2008-10-26 22:58:34 +02:00
|
|
|
std::cin.getline(buffer, 5000);
|
2008-09-17 13:18:22 +03:00
|
|
|
if(cb && *cb)
|
|
|
|
(*cb)(buffer);
|
2007-08-15 18:13:11 +03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2008-09-17 13:18:22 +03:00
|
|
|
CConsoleHandler::CConsoleHandler()
|
2007-08-15 18:13:11 +03:00
|
|
|
{
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifdef _WIN32
|
2008-09-17 13:18:22 +03:00
|
|
|
handleIn = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
GetConsoleScreenBufferInfo(handleOut,&csbi);
|
|
|
|
defColor = csbi.wAttributes;
|
2008-09-18 23:24:53 +03:00
|
|
|
#else
|
|
|
|
defColor = "\x1b[0m";
|
2008-09-17 13:18:22 +03:00
|
|
|
#endif
|
|
|
|
cb = new boost::function<void(const std::string &)>;
|
2007-08-15 18:13:11 +03:00
|
|
|
}
|
2008-09-17 13:18:22 +03:00
|
|
|
CConsoleHandler::~CConsoleHandler()
|
|
|
|
{
|
|
|
|
delete cb;
|
|
|
|
}
|
2008-09-18 23:24:53 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
void CConsoleHandler::killConsole(pthread_t hThread)
|
|
|
|
#else
|
2008-09-17 13:18:22 +03:00
|
|
|
void CConsoleHandler::killConsole(void *hThread)
|
|
|
|
#endif
|
2008-09-18 23:24:53 +03:00
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog3 << "Killing console... ";
|
2008-09-18 23:24:53 +03:00
|
|
|
_kill_thread(hThread,0);
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog3 << "done!\n";
|
2008-09-18 23:24:53 +03:00
|
|
|
}
|