Пример #1
0
bool RsCollectionFile::load(const QString& filename, bool showError /* = true*/)
{
	QFile file(filename);

	if (!file.open(QIODevice::ReadOnly))
	{
		std::cerr << "Cannot open file " << filename.toStdString() << " !!" << std::endl;
		if (showError) {
			showErrorBox(filename, QApplication::translate("RsCollectionFile", "Cannot open file %1").arg(filename));
		}
		return false;
	}

	bool ok = _xml_doc.setContent(&file) ;
	file.close();

	if (ok) {
		_filename = filename;
	} else {
		if (showError) {
			showErrorBox(filename, QApplication::translate("RsCollectionFile", "Error parsing xml file"));
		}
	}

	return ok;
}
Пример #2
0
bool RsCollectionFile::load(const QString& filename, bool showError /* = true*/)
{
	QFile file(filename);

	if (!file.open(QIODevice::ReadOnly))
	{
		std::cerr << "Cannot open file " << filename.toStdString() << " !!" << std::endl;
		if (showError) {
			showErrorBox(filename, QApplication::translate("RsCollectionFile", "Cannot open file %1").arg(filename));
		}
		return false;
	}

	// check that the file is a valid rscollection file, and not a lol bomb or some shit like this
	
	std::cerr << "Checking this file for bomb elements and various wrong stuff" << std::endl;

	FILE *f = fopen(filename.toStdString().c_str(),"r") ;
	char c ;

	std::vector<std::string> bad_strings ;
	bad_strings.push_back(std::string("<!entity ")) ;
	static const int max_size = 12 ; // should be as large as the largest element in bad_strings
	char current[max_size] = { 0,0,0,0,0,0,0,0,0,0,0,0 } ;
	int n=0 ;
	
	while( (c = fgetc(f)) != EOF  || n >= 0)
	{
		if(c == '\t' || c == '\n' || c == '\b' || c == '\r')
			continue ;

		if(n == max_size || c==EOF)
			for(int i=0;i<n-1;++i)
				current[i] = current[i+1] ;

		if(n == max_size)
			--n ;

		if(c >= 'A' && c <= 'Z') c += 'a' - 'A' ;

		if(c != EOF)
			current[n] = c ;
		else
			current[n] = 0 ;

		//std::cerr << "n==" << n <<" Checking string " << std::string(current,n+1)  << " c = " << std::hex << (int)c << std::dec << std::endl;

		for(int i=0;i<bad_strings.size();++i)
			if(std::string(current,bad_strings[i].length()) == bad_strings[i])
			{
				showErrorBox(filename, QApplication::translate("RsCollectionFile", "This file contains the string \"%1\" and is therefore an invalid collection file. \n\nIf you believe it is correct, remove the corresponding line from the file and re-open it with Retroshare.").arg(bad_strings[i].c_str()));
				return false ;
				//std::cerr << "Bad string detected" << std::endl;
			}

		if(c == EOF)
			n-- ;
		else if(n < max_size)
			n++ ;
	}
	fclose(f) ;

//	std::cerr << "File is clean!" << std::endl;
//	return false ;

	bool ok = _xml_doc.setContent(&file) ;
	file.close();

	if (ok) {
		_filename = filename;
	} else {
		if (showError) {
			showErrorBox(filename, QApplication::translate("RsCollectionFile", "Error parsing xml file"));
		}
	}

	return ok;
}
Пример #3
0
bool Window::init(bool enableVsync, int oglMajorVersion, int oglMinorVersion)
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        SDL_ERROR_CHECK();
        return false;
    }

    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);

    m_window = SDL_CreateWindow("Hello World!",
                                       SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                       m_width, m_height,
                                       SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

    if (!m_window)
    {
        SDL_ERROR_CHECK();
        return false;
    }

#ifndef EMSCRIPTEN
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, oglMajorVersion);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, oglMinorVersion);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#endif

    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    m_context = SDL_GL_CreateContext(m_window);
    if (!m_context)
    {
        SHOW_ERROR("Failed to create OpenGL context. Make sure your GPU supports OpenGL " << oglMajorVersion << "." << oglMinorVersion);

        std::stringstream ss;
        ss << "Failed to create OpenGL context. Make sure your GPU supports OpenGL " << oglMajorVersion << "." << oglMinorVersion;
        showErrorBox("Context Creation Error", ss.str());
        return false;
    }

    setVsync(enableVsync);

	glewExperimental = GL_TRUE;
    GLenum err = glewInit();
	
    if (err != GLEW_OK)
    {
#ifdef EMSCRIPTEN
        SHOW_ERROR("Error " << err << " occured when initializing glew: \n");
#else
        const GLubyte *errString = gluErrorString(err);
        SHOW_ERROR("Error " << err << " occured when initializing glew: \n" << errString);
        std::stringstream ss;
        ss << "Error " << err << " occured when initializing glew: \n" << errString;
        showErrorBox("GLEW Initialization Error", ss.str());
#endif
        return false;
    }

    // OpenGL 3.2+ might mistakenly report an invalid enum error. Ignore it:
    glGetError();
    SDL_ERROR_CHECK();

    return true;
}
Пример #4
0
int exec(int argc, char *argv[])
{
	// Initialize Mishira libraries
	INIT_LIBBROADCAST();
	INIT_LIBDESKCAP();
	vidgfx_init();

	//-------------------------------------------------------------------------
	// Initialize our logging system and install logging hooks into our
	// libraries. Do not create any files yet as we might immediately quit if
	// another instance of the application is currently running.

	// We must create this before we get multiple threads. The `Application`
	// class is responsible for deleting this object.
	new LogFileManager();

	// Install our Qt message handler so we can intercept messages
	qInstallMessageHandler(qtMessageHandler);

	// Display application version information at the very top of the log
	appLog() << QStringLiteral(
		"%1 v%2 (Build %3)")
		.arg(APP_NAME)
		.arg(APP_VER_STR)
		.arg(getAppBuildVersion());

	//-------------------------------------------------------------------------
	// Create a named shared memory segment so we can test to see if the user
	// already has an instance of the application running.

	// Open or create shared memory segment
	AppSharedSegment *shm = new AppSharedSegment();
	if(!shm->isValid()) {
		// An unexpected error occured

		// Notify the user as they would get no feedback otherwise. We cannot
		// use translatable strings yet as we haven't initialized the
		// translation system.
		showErrorBox(
			"Failed to open or create shared memory segment. Reason:",
			shm->getErrorReason());

		// Terminate
		delete shm;
		return 1; // Error
	}

	// Test if we already have an instance open and if we don't set the process
	// ID to us.
	shm->lock();
#ifdef Q_OS_WIN
	DWORD ourProcId = GetCurrentProcessId();
	DWORD shmProcId = (DWORD)shm->getMainProcessId();
	//appLog() << "Our PID: " << (uint)ourProcId
	//	<< ", SHM PID: " << (uint)shmProcId;
	if(shmProcId != 0 && ourProcId != shmProcId) {
		// Process IDs do not match so it's not us. Test to see if the other
		// process actually exists and if so test to see if it's actually
		// another instance of ourselves
		bool exists = doesProcessExist(shmProcId);
		QString ourPath = getProcExeFilename(ourProcId, true);
		QString shmPath = getProcExeFilename(shmProcId, true);
		//appLog() << "Our path: \"" << ourPath
		//	<< "\", SHM path: \"" << shmPath << "\"";
		if(exists && ourPath == shmPath && !shmPath.isEmpty()) {
			// An instance already exists
			shm->unlock();
			appLog()
				<< "Application instance already exists, raising window and "
				<< "terminating";

			// Notify the other process to raise its main window
			shm->setRaiseWindow(true);

			// Terminate
			delete shm;
			return 0; // Success
		}

		// Our hooks might behave weirdly after a crash so log that it was
		// detected
		appLog(Log::Warning)
			<< "Our shared memory segment wasn't properly cleaned up, Mishira "
			<< "most likely crashed the last time it was run";
	}
	shm->setMainProcessId((uint32_t)ourProcId);
#else
#error Unsupported platform
#endif
	shm->unlock();

	//-------------------------------------------------------------------------
	// Create `Application` instance and continue startup sequence there

	// Force destruction of the `Application` object before returning
	int returnCode = 1;
	QString logFilename;
	{
#ifdef Q_OS_WIN
		WinApplication app(argc, argv, shm);
#else
#error Unsupported platform
#endif

		if(!app.initialize())
			return 1; // Terminate with error

		//---------------------------------------------------------------------
		// Begin main loop

		returnCode = app.execute();

		//---------------------------------------------------------------------
		// Shutdown cleanly

		logFilename = LogFileManager::getSingleton()->getFilename();
		returnCode = app.shutdown(returnCode);
	}

	// If the application exited uncleanly show a useful error dialog
	if(returnCode != 0) {
		QString msg;
		QVector<QString> msgs = Log::getCriticalMessages();
		for(int i = 0; i < msgs.count(); i++)
			msg += QStringLiteral("%1\n").arg(msgs.at(i));
		if(!logFilename.isEmpty()) {
			if(msgs.count())
				msg += QStringLiteral("\n");
			msg += QStringLiteral("More details can be found in:\n%1")
				.arg(logFilename);
		}
		showErrorBox(
			"Application quit unexpectedly with the message below:", msg);
	}

	return returnCode;
}