Fixed: #1051 Create a console-based working patcher

This commit is contained in:
kervala 2010-08-05 15:20:06 +02:00
parent 2da52ebc36
commit 6b84d99679

View file

@ -3,6 +3,10 @@
#include "client_cfg.h" #include "client_cfg.h"
#include <locale.h> #include <locale.h>
#ifdef NL_OS_WINDOWS
#include <windows.h>
#endif
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
#endif #endif
@ -28,6 +32,12 @@ uint32 LoginShardId = 0xFFFFFFFF;
extern void tmpFlagRemovedPatchCategories(NLMISC::CConfigFile &cf); extern void tmpFlagRemovedPatchCategories(NLMISC::CConfigFile &cf);
bool useUtf8 = false; bool useUtf8 = false;
bool useEsc = false;
#ifdef NL_OS_WINDOWS
HANDLE hStdout = NULL;
sint attributes = 0;
#endif
std::string convert(const ucstring &str) std::string convert(const ucstring &str)
{ {
@ -39,14 +49,54 @@ std::string convert(const ucstring &str)
void printError(const std::string &str) void printError(const std::string &str)
{ {
// display error in red // display error in red if possible
printf("\033[22;31mError: %s\033[0m\n", str.c_str()); if (useEsc)
{
printf("\033[1;31mError: %s\033[0m\n", str.c_str());
}
else
{
#ifdef NL_OS_WINDOWS
if (hStdout != INVALID_HANDLE_VALUE && hStdout)
SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY);
#endif
printf("Error: %s\n", str.c_str());
#ifdef NL_OS_WINDOWS
if (hStdout != INVALID_HANDLE_VALUE && hStdout)
SetConsoleTextAttribute(hStdout, attributes);
#endif
}
} }
void printProgress(const std::string &str) void printCheck(const std::string &str)
{ {
// display progress in green // display check
printf("\033[F\033[22;31mError: %d\033[0m\033[[1J\n", str.c_str()); printf("%s\n", str.c_str());
}
void printDownload(const std::string &str)
{
// display download in purple
if (useEsc)
{
printf("\033[1;35m%s\033[0m\n", str.c_str());
}
else
{
#ifdef NL_OS_WINDOWS
if (hStdout != INVALID_HANDLE_VALUE && hStdout)
SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
#endif
printf("%s\n", str.c_str());
#ifdef NL_OS_WINDOWS
if (hStdout != INVALID_HANDLE_VALUE && hStdout)
SetConsoleTextAttribute(hStdout, attributes);
#endif
}
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -71,14 +121,28 @@ int main(int argc, char *argv[])
CPath::setCurrentPath(currentPath); CPath::setCurrentPath(currentPath);
} }
std::string lang; // check if console supports utf-8
std::string lang = toLower(std::string(setlocale(LC_CTYPE, "")));
char *locale = setlocale(LC_CTYPE, NULL);
if (locale)
lang = toLower(std::string(locale));
useUtf8 = (lang.find("utf8") != string::npos || lang.find("utf-8") != string::npos); useUtf8 = (lang.find("utf8") != string::npos || lang.find("utf-8") != string::npos);
// check if console supports colors
std::string term = toLower(std::string(getenv("TERM") ? getenv("TERM"):""));
useEsc = (term.find("xterm") != string::npos || term.find("linux") != string::npos);
#ifdef NL_OS_WINDOWS
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdout == INVALID_HANDLE_VALUE)
{
printError("Console error");
return 1;
}
CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo;
if (GetConsoleScreenBufferInfo(hStdout, &consoleScreenBufferInfo))
attributes = consoleScreenBufferInfo.wAttributes;
#endif
// create or load client.cfg // create or load client.cfg
ClientCfg.init("client.cfg"); ClientCfg.init("client.cfg");
@ -180,7 +244,7 @@ int main(int argc, char *argv[])
{ {
for(uint i = 0; i < log.size(); ++i) for(uint i = 0; i < log.size(); ++i)
{ {
printf("%s\n", convert(log[i]).c_str()); printCheck(convert(log[i]));
} }
} }
} }
@ -220,11 +284,11 @@ int main(int argc, char *argv[])
if (pPM->getThreadState(state, log)) if (pPM->getThreadState(state, log))
{ {
printProgress(convert(state)); printDownload(convert(state));
for(uint i = 0; i < log.size(); ++i) for(uint i = 0; i < log.size(); ++i)
{ {
printf("%s\n", convert(log[i]).c_str()); printCheck(convert(log[i]));
} }
} }
} }