浏览代码

* missing files

mateuszb 14 年之前
父节点
当前提交
4ed7d19191
共有 4 个文件被更改,包括 441 次插入0 次删除
  1. 255 0
      lib/CConsoleHandler.cpp
  2. 47 0
      lib/CConsoleHandler.h
  3. 82 0
      lib/CThreadHelper.cpp
  4. 57 0
      lib/CThreadHelper.h

+ 255 - 0
lib/CConsoleHandler.cpp

@@ -0,0 +1,255 @@
+#define VCMI_DLL
+#include "../stdafx.h"
+#include "CConsoleHandler.h"
+#include <boost/function.hpp>
+#include <boost/thread.hpp>
+#include <iomanip>
+#include "CThreadHelper.h"
+
+/*
+* CConsoleHandler.cpp, part of VCMI engine
+*
+* Authors: listed in file AUTHORS in main folder
+*
+* License: GNU General Public License v2.0 or later
+* Full text of license available in license.txt file, in main folder
+*
+*/
+
+#ifndef _WIN32
+	typedef std::string TColor;
+	#define CONSOLE_GREEN "\x1b[1;32m"
+	#define CONSOLE_RED "\x1b[1;32m"
+	#define CONSOLE_MAGENTA "\x1b[1;35m"
+	#define CONSOLE_YELLOW "\x1b[1;32m"
+	#define CONSOLE_WHITE "\x1b[1;39m"
+	#define CONSOLE_GRAY "\x1b[1;30m"
+	#define CONSOLE_TEAL "\x1b[1;36m"
+#else
+	#define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
+	#include <windows.h>
+	#include <dbghelp.h>
+	#pragma comment(lib, "dbghelp.lib")
+
+	typedef WORD TColor;
+	HANDLE handleIn;
+	HANDLE handleOut;
+	#define CONSOLE_GREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
+	#define CONSOLE_RED FOREGROUND_RED | FOREGROUND_INTENSITY
+	#define CONSOLE_MAGENTA FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY
+	#define CONSOLE_YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
+	#define CONSOLE_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
+	#define CONSOLE_GRAY FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
+	#define CONSOLE_TEAL FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
+#endif
+
+TColor defColor;
+
+#ifdef _WIN32
+
+void printWinError()
+{
+	//Get error code
+	int error = GetLastError();
+	if(!error)
+	{
+		tlog0 << "No Win error information set.\n";
+		return;
+	}
+	tlog1 << "Error " << error << " encountered:\n";
+
+	//Get error description
+	char* pTemp = NULL;
+	FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
+		NULL, error,  MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)&pTemp, 1, NULL);
+	tlog1 << pTemp << std::endl;
+	LocalFree( pTemp );
+}
+
+const char* exceptionName(DWORD exc)
+{
+#define EXC_CASE(EXC)	case EXCEPTION_##EXC : return "EXCEPTION_" #EXC
+	switch (exc)
+	{
+		EXC_CASE(ACCESS_VIOLATION);
+		EXC_CASE(DATATYPE_MISALIGNMENT);
+		EXC_CASE(BREAKPOINT);
+		EXC_CASE(SINGLE_STEP);
+		EXC_CASE(ARRAY_BOUNDS_EXCEEDED);
+		EXC_CASE(FLT_DENORMAL_OPERAND);
+		EXC_CASE(FLT_DIVIDE_BY_ZERO);
+		EXC_CASE(FLT_INEXACT_RESULT);
+		EXC_CASE(FLT_INVALID_OPERATION);
+		EXC_CASE(FLT_OVERFLOW);
+		EXC_CASE(FLT_STACK_CHECK);
+		EXC_CASE(FLT_UNDERFLOW);
+		EXC_CASE(INT_DIVIDE_BY_ZERO);
+		EXC_CASE(INT_OVERFLOW);
+		EXC_CASE(PRIV_INSTRUCTION);
+		EXC_CASE(IN_PAGE_ERROR);
+		EXC_CASE(ILLEGAL_INSTRUCTION);
+		EXC_CASE(NONCONTINUABLE_EXCEPTION);
+		EXC_CASE(STACK_OVERFLOW);
+		EXC_CASE(INVALID_DISPOSITION);
+		EXC_CASE(GUARD_PAGE);
+		EXC_CASE(INVALID_HANDLE);
+	default:
+		return "UNKNOWN EXCEPTION";
+	}
+#undef EXC_CASE
+}
+
+
+
+LONG WINAPI onUnhandledException(EXCEPTION_POINTERS* exception)
+{
+	tlog1 << "Disaster happened.\n";
+
+	PEXCEPTION_RECORD einfo = exception->ExceptionRecord;
+	tlog1 << "Reason: 0x" << std::hex << einfo->ExceptionCode << " - " << exceptionName(einfo->ExceptionCode);
+	tlog1 << " at " << std::setfill('0') << std::setw(4) << exception->ContextRecord->SegCs << ":" << (void*)einfo->ExceptionAddress << std::endl;;
+
+	if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
+	{
+		tlog1 << "Attempt to " << (einfo->ExceptionInformation[0] == 1 ? "write to " : "read from ") 
+			<< "0x" <<  std::setw(8) << (void*)einfo->ExceptionInformation[1] << std::endl;;
+	}
+	const DWORD threadId = ::GetCurrentThreadId();
+	tlog1 << "Thread ID: " << threadId << " [" << std::dec << std::setw(0) << threadId << "]\n";
+
+	//exception info to be placed in the dump
+	MINIDUMP_EXCEPTION_INFORMATION meinfo = {threadId, exception, TRUE};
+
+	//create file where dump will be placed
+	char *mname = NULL;
+	char buffer[MAX_PATH + 1];
+	HMODULE hModule = NULL;	
+	GetModuleFileNameA(hModule, buffer, MAX_PATH);
+	mname = strrchr(buffer, '\\');
+	if (mname != 0)
+		mname++;
+	else
+		mname = buffer;
+
+	strcat(mname, "_crashinfo.dmp");
+	HANDLE dfile = CreateFileA(mname, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
+	tlog1 << "Crash info will be put in " << mname << std::endl;
+	MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), dfile, MiniDumpWithDataSegs, &meinfo, 0, 0);
+	MessageBoxA(0, "VCMI has crashed. We are sorry. File with information about encountered problem has been created.", "VCMI Crashhandler", MB_OK | MB_ICONERROR);
+	return EXCEPTION_EXECUTE_HANDLER;
+}
+#endif
+
+
+void CConsoleHandler::setColor(int level)
+{
+	TColor color;
+	switch(level)
+	{
+	case -1:
+		color = defColor;
+		break;
+	case 0:
+		color = CONSOLE_GREEN;
+		break;
+	case 1:
+		color = CONSOLE_RED;
+		break;
+	case 2:
+		color = CONSOLE_MAGENTA;
+		break;
+	case 3:
+		color = CONSOLE_YELLOW;
+		break;
+	case 4:
+		color = CONSOLE_WHITE;
+		break;
+	case 5:
+		color = CONSOLE_GRAY;
+		break;
+	case -2:
+		color = CONSOLE_TEAL;
+		break;
+	default:
+		color = defColor;
+		break;
+	}
+#ifdef _WIN32
+	SetConsoleTextAttribute(handleOut,color);
+#else
+	std::cout << color;
+#endif
+}
+
+int CConsoleHandler::run()
+{
+	setThreadName(-1, "CConsoleHandler::run");
+	//disabling sync to make in_avail() work (othervice always returns 0)
+	std::ios::sync_with_stdio(false);
+	std::string buffer;
+
+	while ( std::cin.good() )
+	{
+#ifndef _WIN32
+		//check if we have some unreaded symbols
+		if (std::cin.rdbuf()->in_avail())
+		{
+			if ( getline(std::cin, buffer).good() )
+				if ( cb && *cb )
+					(*cb)(buffer);
+		}
+		else
+			boost::this_thread::sleep(boost::posix_time::millisec(100));
+
+		boost::this_thread::interruption_point();
+#else
+		std::getline(std::cin, buffer);
+		if ( cb && *cb )
+			(*cb)(buffer);
+#endif
+	}
+	return -1;
+}
+CConsoleHandler::CConsoleHandler()
+{
+#ifdef _WIN32
+	handleIn = GetStdHandle(STD_INPUT_HANDLE);
+	handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
+	CONSOLE_SCREEN_BUFFER_INFO csbi;
+	GetConsoleScreenBufferInfo(handleOut,&csbi);
+	defColor = csbi.wAttributes;
+#ifndef _DEBUG
+	SetUnhandledExceptionFilter(onUnhandledException);
+#endif
+#else
+	defColor = "\x1b[0m";
+#endif
+	cb = new boost::function<void(const std::string &)>;
+	thread = NULL;
+}
+CConsoleHandler::~CConsoleHandler()
+{
+	tlog3 << "Killing console... ";
+	end();
+	delete cb;
+	tlog3 << "done!\n";
+}
+void CConsoleHandler::end()
+{
+	if (thread) 
+	{
+#ifndef _WIN32
+		thread->interrupt();
+#else
+		TerminateThread(thread->native_handle(),0);
+#endif
+		thread->join();
+		delete thread;
+		thread = NULL;
+	}
+}
+
+void CConsoleHandler::start()
+{
+	thread = new boost::thread(boost::bind(&CConsoleHandler::run,console));
+}

+ 47 - 0
lib/CConsoleHandler.h

@@ -0,0 +1,47 @@
+#ifndef __CCONSOLEHANDLER_H__
+#define __CCONSOLEHANDLER_H__
+
+/*
+ * CConsoleHandler.h, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+
+namespace boost
+{
+	template<typename signature>
+	class function;
+	class thread;
+}
+
+/// Class which wraps the native console. It can print text based on
+/// the chosen color
+class DLL_EXPORT CConsoleHandler
+{
+public:
+	boost::function<void(const std::string &)> *cb; //function to be called when message is received
+	int curLvl; //logging level
+	boost::thread *thread;
+
+	int run();
+	void setColor(int level); //sets color of text appropriate for given logging level
+
+	CConsoleHandler(); //c-tor
+	~CConsoleHandler(); //d-tor
+	void start(); //starts listening thread
+	void end(); //kills listening thread
+
+	template<typename T> void print(const T &data, int level)
+	{
+		setColor(level);
+		std::cout << data << std::flush;
+		setColor(-1);
+	}
+};
+
+
+#endif // __CCONSOLEHANDLER_H__

+ 82 - 0
lib/CThreadHelper.cpp

@@ -0,0 +1,82 @@
+#define VCMI_DLL
+
+#include "CThreadHelper.h"
+#include <boost/thread.hpp>
+#include <boost/bind.hpp>
+#ifdef _WIN32
+	#include <windows.h>
+#endif
+/*
+ * CThreadHelper.cpp, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+
+CThreadHelper::CThreadHelper(std::vector<boost::function<void()> > *Tasks, int Threads)
+{
+	currentTask = 0; amount = Tasks->size();
+	tasks = Tasks;
+	threads = Threads;
+}
+void CThreadHelper::run()
+{
+	boost::thread_group grupa;
+	for(int i=0;i<threads;i++)
+		grupa.create_thread(boost::bind(&CThreadHelper::processTasks,this));
+	grupa.join_all();
+}
+void CThreadHelper::processTasks()
+{
+	int pom;
+	while(true)
+	{
+		rtinm.lock();
+		if((pom=currentTask) >= amount)
+		{
+			rtinm.unlock();
+			break;
+		}
+		else
+		{
+			++currentTask;
+			rtinm.unlock();
+			(*tasks)[pom]();
+		}
+	}
+}
+
+void setThreadName(long threadID, const std::string &name)
+{
+#ifdef _WIN32
+	//follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
+	const DWORD MS_VC_EXCEPTION=0x406D1388;
+#pragma pack(push,8)
+	typedef struct tagTHREADNAME_INFO
+	{
+		DWORD dwType; // Must be 0x1000.
+		LPCSTR szName; // Pointer to name (in user addr space).
+		DWORD dwThreadID; // Thread ID (-1=caller thread).
+		DWORD dwFlags; // Reserved for future use, must be zero.
+	} THREADNAME_INFO;
+#pragma pack(pop)
+	THREADNAME_INFO info;
+	info.dwType = 0x1000;
+	info.szName = name.c_str();
+	info.dwThreadID = threadID;
+	info.dwFlags = 0;
+
+	__try
+	{
+		RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
+	}
+	__except(EXCEPTION_EXECUTE_HANDLER)
+	{
+	}
+#else
+	//*nix counterpart?
+#endif
+}

+ 57 - 0
lib/CThreadHelper.h

@@ -0,0 +1,57 @@
+#ifndef __CTHREADHELPER_H__
+#define __CTHREADHELPER_H__
+
+#include "../global.h"
+#include <boost/function.hpp>
+#include <boost/thread.hpp>
+
+
+/*
+ * CThreadHelper.h, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+
+typedef boost::function<void()> Task;
+
+/// Can assign CPU work to other threads/cores
+class DLL_EXPORT CThreadHelper
+{
+	boost::mutex rtinm;
+	int currentTask, amount, threads;
+	std::vector<Task> *tasks;
+
+
+	void processTasks();
+public:
+	CThreadHelper(std::vector<boost::function<void()> > *Tasks, int Threads);
+	void run();
+};
+
+template <typename T> inline void setData(T * data, boost::function<T()> func)
+{
+	*data = func();
+}
+
+void DLL_EXPORT setThreadName(long threadID, const std::string &name);
+
+#define GET_DATA(TYPE,DESTINATION,FUNCTION_TO_GET) \
+	(boost::bind(&setData<TYPE>,&DESTINATION,FUNCTION_TO_GET))
+#define GET_SURFACE(SUR_DESTINATION, SUR_NAME) \
+	(GET_DATA \
+		(SDL_Surface*,SUR_DESTINATION,\
+		boost::function<SDL_Surface*()>(boost::bind(&BitmapHandler::loadBitmap,SUR_NAME,true))))
+#define GET_DEF(DESTINATION, DEF_NAME) \
+	(GET_DATA \
+		(CDefHandler*,DESTINATION,\
+		boost::function<CDefHandler*()>(boost::bind(CDefHandler::giveDef,DEF_NAME))))
+#define GET_DEF_ESS(DESTINATION, DEF_NAME) \
+	(GET_DATA \
+		(CDefEssential*,DESTINATION,\
+		boost::function<CDefEssential*()>(boost::bind(CDefHandler::giveDefEss,DEF_NAME))))
+
+#endif // __CTHREADHELPER_H__