2013-11-24 05:36:16 +00:00
|
|
|
#include "JavaChecker.h"
|
2015-10-05 00:47:27 +01:00
|
|
|
#include <FileSystem.h>
|
|
|
|
#include <Commandline.h>
|
2013-11-24 05:36:16 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QProcess>
|
2013-12-11 03:54:39 +00:00
|
|
|
#include <QMap>
|
2013-12-12 23:02:58 +00:00
|
|
|
#include <QTemporaryFile>
|
2015-02-02 00:09:28 +00:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDebug>
|
2013-11-24 05:36:16 +00:00
|
|
|
|
|
|
|
JavaChecker::JavaChecker(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-11 03:54:39 +00:00
|
|
|
void JavaChecker::performCheck()
|
2013-11-24 05:36:16 +00:00
|
|
|
{
|
2015-10-05 00:47:27 +01:00
|
|
|
QString checkerJar = FS::PathCombine(QCoreApplication::applicationDirPath(), "jars", "JavaCheck.jar");
|
2013-11-24 05:36:16 +00:00
|
|
|
|
2015-05-04 00:20:48 +01:00
|
|
|
QStringList args;
|
2013-11-24 05:36:16 +00:00
|
|
|
|
|
|
|
process.reset(new QProcess());
|
2015-05-04 00:20:48 +01:00
|
|
|
if(m_args.size())
|
|
|
|
{
|
2015-10-05 00:47:27 +01:00
|
|
|
auto extraArgs = Commandline::splitArgs(m_args);
|
2015-05-04 00:20:48 +01:00
|
|
|
args.append(extraArgs);
|
|
|
|
}
|
|
|
|
if(m_minMem != 0)
|
|
|
|
{
|
|
|
|
args << QString("-Xms%1m").arg(m_minMem);
|
|
|
|
}
|
|
|
|
if(m_maxMem != 0)
|
|
|
|
{
|
|
|
|
args << QString("-Xmx%1m").arg(m_maxMem);
|
|
|
|
}
|
|
|
|
if(m_permGen != 64)
|
|
|
|
{
|
|
|
|
args << QString("-XX:PermSize=%1m").arg(m_permGen);
|
|
|
|
}
|
|
|
|
|
|
|
|
args.append({"-jar", checkerJar});
|
2013-11-24 05:36:16 +00:00
|
|
|
process->setArguments(args);
|
2015-05-04 00:20:48 +01:00
|
|
|
process->setProgram(m_path);
|
2013-11-24 05:36:16 +00:00
|
|
|
process->setProcessChannelMode(QProcess::SeparateChannels);
|
2015-05-04 00:20:48 +01:00
|
|
|
qDebug() << "Running java checker: " + m_path + args.join(" ");;
|
|
|
|
|
|
|
|
connect(process.get(), SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
|
|
|
|
connect(process.get(), SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)));
|
|
|
|
connect(process.get(), SIGNAL(readyReadStandardOutput()), this, SLOT(stdoutReady()));
|
|
|
|
connect(process.get(), SIGNAL(readyReadStandardError()), this, SLOT(stderrReady()));
|
2013-11-24 05:36:16 +00:00
|
|
|
connect(&killTimer, SIGNAL(timeout()), SLOT(timeout()));
|
|
|
|
killTimer.setSingleShot(true);
|
2014-08-10 14:09:47 +01:00
|
|
|
killTimer.start(15000);
|
2013-11-24 05:36:16 +00:00
|
|
|
process->start();
|
|
|
|
}
|
|
|
|
|
2015-05-04 00:20:48 +01:00
|
|
|
void JavaChecker::stdoutReady()
|
|
|
|
{
|
|
|
|
QByteArray data = process->readAllStandardOutput();
|
|
|
|
QString added = QString::fromLocal8Bit(data);
|
|
|
|
added.remove('\r');
|
|
|
|
m_stdout += added;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavaChecker::stderrReady()
|
|
|
|
{
|
|
|
|
QByteArray data = process->readAllStandardError();
|
|
|
|
QString added = QString::fromLocal8Bit(data);
|
|
|
|
added.remove('\r');
|
|
|
|
m_stderr += added;
|
|
|
|
}
|
|
|
|
|
2013-11-24 05:36:16 +00:00
|
|
|
void JavaChecker::finished(int exitcode, QProcess::ExitStatus status)
|
|
|
|
{
|
|
|
|
killTimer.stop();
|
|
|
|
QProcessPtr _process;
|
|
|
|
_process.swap(process);
|
|
|
|
|
2013-12-11 03:54:39 +00:00
|
|
|
JavaCheckResult result;
|
|
|
|
{
|
2015-05-04 00:20:48 +01:00
|
|
|
result.path = m_path;
|
|
|
|
result.id = m_id;
|
2013-12-11 03:54:39 +00:00
|
|
|
}
|
2015-05-04 00:28:16 +01:00
|
|
|
result.errorLog = m_stderr;
|
2015-05-04 00:20:48 +01:00
|
|
|
qDebug() << "STDOUT" << m_stdout;
|
|
|
|
qWarning() << "STDERR" << m_stderr;
|
2015-02-02 00:09:28 +00:00
|
|
|
qDebug() << "Java checker finished with status " << status << " exit code " << exitcode;
|
2013-12-11 03:54:39 +00:00
|
|
|
|
2013-11-24 05:36:16 +00:00
|
|
|
if (status == QProcess::CrashExit || exitcode == 1)
|
|
|
|
{
|
2015-02-02 00:09:28 +00:00
|
|
|
qDebug() << "Java checker failed!";
|
2013-12-11 03:54:39 +00:00
|
|
|
emit checkFinished(result);
|
2013-11-24 05:36:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 03:54:39 +00:00
|
|
|
bool success = true;
|
2014-01-09 00:20:24 +00:00
|
|
|
|
2013-12-11 03:54:39 +00:00
|
|
|
QMap<QString, QString> results;
|
2015-05-04 00:20:48 +01:00
|
|
|
QStringList lines = m_stdout.split("\n", QString::SkipEmptyParts);
|
2013-12-11 03:54:39 +00:00
|
|
|
for(QString line : lines)
|
|
|
|
{
|
|
|
|
line = line.trimmed();
|
|
|
|
|
|
|
|
auto parts = line.split('=', QString::SkipEmptyParts);
|
|
|
|
if(parts.size() != 2 || parts[0].isEmpty() || parts[1].isEmpty())
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
results.insert(parts[0], parts[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!results.contains("os.arch") || !results.contains("java.version") || !success)
|
2013-11-24 05:36:16 +00:00
|
|
|
{
|
2015-02-02 00:09:28 +00:00
|
|
|
qDebug() << "Java checker failed - couldn't extract required information.";
|
2013-12-11 03:54:39 +00:00
|
|
|
emit checkFinished(result);
|
2013-11-24 05:36:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 03:54:39 +00:00
|
|
|
auto os_arch = results["os.arch"];
|
|
|
|
auto java_version = results["java.version"];
|
2013-11-24 05:36:16 +00:00
|
|
|
bool is_64 = os_arch == "x86_64" || os_arch == "amd64";
|
|
|
|
|
2013-12-11 03:54:39 +00:00
|
|
|
|
|
|
|
result.valid = true;
|
|
|
|
result.is_64bit = is_64;
|
|
|
|
result.mojangPlatform = is_64 ? "64" : "32";
|
|
|
|
result.realPlatform = os_arch;
|
|
|
|
result.javaVersion = java_version;
|
2015-02-02 00:09:28 +00:00
|
|
|
qDebug() << "Java checker succeeded.";
|
2013-11-24 05:36:16 +00:00
|
|
|
emit checkFinished(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavaChecker::error(QProcess::ProcessError err)
|
|
|
|
{
|
|
|
|
if(err == QProcess::FailedToStart)
|
|
|
|
{
|
|
|
|
killTimer.stop();
|
2015-02-02 00:09:28 +00:00
|
|
|
qDebug() << "Java checker has failed to start.";
|
2013-12-11 03:54:39 +00:00
|
|
|
JavaCheckResult result;
|
|
|
|
{
|
2015-05-04 00:20:48 +01:00
|
|
|
result.path = m_path;
|
|
|
|
result.id = m_id;
|
2013-12-11 03:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit checkFinished(result);
|
2013-11-24 05:36:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavaChecker::timeout()
|
|
|
|
{
|
|
|
|
// NO MERCY. NO ABUSE.
|
|
|
|
if(process)
|
|
|
|
{
|
2015-02-02 00:09:28 +00:00
|
|
|
qDebug() << "Java checker has been killed by timeout.";
|
2013-11-24 05:36:16 +00:00
|
|
|
process->kill();
|
|
|
|
}
|
|
|
|
}
|