Beispiel #1
0
ChessPlayer* EngineBuilder::create(QObject* receiver,
				   const char* method,
				   QObject* parent) const
{
	QString path(QDir::currentPath());
	EngineProcess* process = new EngineProcess();

	QString workDir = m_config.workingDirectory();
	QString cmd = m_config.command();

	if (workDir.isEmpty())
	{
		process->setWorkingDirectory(QDir::tempPath());

		QFileInfo cmdInfo(cmd);
		if (cmdInfo.isFile())
			cmd = cmdInfo.absoluteFilePath();
	}
	else
	{
		// Make sure the path to the executable is resolved
		// in the engine's working directory
		if (!QDir::setCurrent(workDir))
		{
			qWarning() << "Invalid working directory:" << workDir;
			delete process;
			return 0;
		}
		process->setWorkingDirectory(QDir::currentPath());
	}

	if (!m_config.arguments().isEmpty())
		process->start(cmd, m_config.arguments());
	else
		process->start(cmd);
	bool ok = process->waitForStarted();

	if (!workDir.isEmpty())
		QDir::setCurrent(path);
	if (!ok)
	{
		qWarning() << "Cannot start engine" << m_config.command();
		delete process;
		return 0;
	}

	ChessEngine* engine = EngineFactory::create(m_config.protocol());
	Q_ASSERT(engine != 0);
	engine->setParent(parent);
	if (receiver != 0 && method != 0)
		QObject::connect(engine, SIGNAL(debugMessage(QString)),
				 receiver, method);
	engine->setDevice(process);
	engine->applyConfiguration(m_config);

	engine->start();
	return engine;
}
Beispiel #2
0
ChessPlayer* EngineBuilder::create(QObject* receiver,
				   const char* method,
				   QObject* parent,
				   QString* error) const
{
	QString workDir = m_config.workingDirectory();
	QString cmd = m_config.command().trimmed();
	QString stderrFile = m_config.stderrFile();

	if (cmd.isEmpty())
	{
		setError(error, tr("Empty engine command"));
		return nullptr;
	}

	if (!EngineFactory::protocols().contains(m_config.protocol()))
	{
		setError(error, tr("Unknown chess protocol: %1")
			 .arg(m_config.protocol()));
		return nullptr;
	}

	EngineProcess* process = new EngineProcess();

	if (workDir.isEmpty())
	{
		process->setWorkingDirectory(QDir::tempPath());

		QFileInfo cmdInfo(cmd);
		if (cmdInfo.isFile())
			cmd = cmdInfo.absoluteFilePath();
	}
	else
		process->setWorkingDirectory(workDir);

	if (!stderrFile.isEmpty())
		process->setStandardErrorFile(stderrFile, QIODevice::Append);

	if (!m_config.arguments().isEmpty())
		process->start(cmd, m_config.arguments());
	else
		process->start(cmd);

	bool ok = process->waitForStarted();
	if (!ok)
	{
		setError(error, tr("Cannot execute command: %1")
			 .arg(m_config.command()));
		delete process;
		return nullptr;
	}

	ChessEngine* engine = EngineFactory::create(m_config.protocol());
	Q_ASSERT(engine != nullptr);

	engine->setParent(parent);
	if (receiver != nullptr && method != nullptr)
		QObject::connect(engine, SIGNAL(debugMessage(QString)),
				 receiver, method);
	engine->setDevice(process);
	engine->applyConfiguration(m_config);

	engine->start();
	return engine;
}