2015-02-02 14:25:30 -08:00
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-02-22 16:17:31 +01:00
|
|
|
*
|
|
|
|
* Authors: Orochimarufan <orochimarufan.x3@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2015-02-09 01:51:14 +01:00
|
|
|
|
|
|
|
#include "BaseProcess.h"
|
2015-02-02 02:14:14 +01:00
|
|
|
#include <QDebug>
|
2013-02-22 16:17:31 +01:00
|
|
|
#include <QDir>
|
2015-01-27 22:31:07 +01:00
|
|
|
#include <QEventLoop>
|
2013-02-22 16:17:31 +01:00
|
|
|
|
2015-01-27 22:31:07 +01:00
|
|
|
MessageLevel::Enum MessageLevel::getLevel(const QString& levelName)
|
|
|
|
{
|
|
|
|
if (levelName == "MultiMC")
|
|
|
|
return MessageLevel::MultiMC;
|
|
|
|
else if (levelName == "Debug")
|
|
|
|
return MessageLevel::Debug;
|
|
|
|
else if (levelName == "Info")
|
|
|
|
return MessageLevel::Info;
|
|
|
|
else if (levelName == "Message")
|
|
|
|
return MessageLevel::Message;
|
|
|
|
else if (levelName == "Warning")
|
|
|
|
return MessageLevel::Warning;
|
|
|
|
else if (levelName == "Error")
|
|
|
|
return MessageLevel::Error;
|
|
|
|
else if (levelName == "Fatal")
|
|
|
|
return MessageLevel::Fatal;
|
|
|
|
// Skip PrePost, it's not exposed to !![]!
|
|
|
|
else
|
|
|
|
return MessageLevel::Message;
|
|
|
|
}
|
2013-02-22 16:17:31 +01:00
|
|
|
|
2015-01-27 22:31:07 +01:00
|
|
|
BaseProcess::BaseProcess(InstancePtr instance): QProcess(), m_instance(instance)
|
|
|
|
{
|
|
|
|
}
|
2013-02-22 16:17:31 +01:00
|
|
|
|
2015-01-27 22:31:07 +01:00
|
|
|
void BaseProcess::init()
|
2013-02-22 16:17:31 +01:00
|
|
|
{
|
2013-09-23 00:23:50 +02:00
|
|
|
connect(this, SIGNAL(finished(int, QProcess::ExitStatus)),
|
|
|
|
SLOT(finish(int, QProcess::ExitStatus)));
|
|
|
|
|
2013-02-25 14:44:36 -06:00
|
|
|
// prepare the process environment
|
2014-11-01 14:11:20 +01:00
|
|
|
QProcessEnvironment rawenv = QProcessEnvironment::systemEnvironment();
|
2013-09-23 00:23:50 +02:00
|
|
|
|
2014-11-01 14:11:20 +01:00
|
|
|
QProcessEnvironment env;
|
2014-11-02 11:13:18 +01:00
|
|
|
|
2015-01-17 23:05:34 +01:00
|
|
|
QStringList ignored =
|
|
|
|
{
|
|
|
|
"JAVA_ARGS",
|
|
|
|
"CLASSPATH",
|
|
|
|
"CONFIGPATH",
|
|
|
|
"JAVA_HOME",
|
|
|
|
"JRE_HOME",
|
|
|
|
"_JAVA_OPTIONS",
|
|
|
|
"JAVA_OPTIONS",
|
|
|
|
"JAVA_TOOL_OPTIONS"
|
|
|
|
};
|
2014-11-01 14:11:20 +01:00
|
|
|
for(auto key: rawenv.keys())
|
|
|
|
{
|
|
|
|
auto value = rawenv.value(key);
|
2014-11-02 11:13:18 +01:00
|
|
|
// filter out dangerous java crap
|
|
|
|
if(ignored.contains(key))
|
|
|
|
{
|
2015-02-02 02:14:14 +01:00
|
|
|
qDebug() << "Env: ignoring" << key << value;
|
2014-11-02 11:13:18 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-01 14:11:20 +01:00
|
|
|
// filter MultiMC-related things
|
|
|
|
if(key.startsWith("QT_"))
|
|
|
|
{
|
2015-02-02 02:14:14 +01:00
|
|
|
qDebug() << "Env: ignoring" << key << value;
|
2014-11-01 14:11:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-22 16:17:31 +01:00
|
|
|
#ifdef LINUX
|
2014-11-01 14:11:20 +01:00
|
|
|
// Do not pass LD_* variables to java. They were intended for MultiMC
|
|
|
|
if(key.startsWith("LD_"))
|
|
|
|
{
|
2015-02-02 02:14:14 +01:00
|
|
|
qDebug() << "Env: ignoring" << key << value;
|
2014-11-01 14:11:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Strip IBus
|
|
|
|
// IBus is a Linux IME framework. For some reason, it breaks MC?
|
|
|
|
if (key == "XMODIFIERS" && value.contains(IBUS))
|
|
|
|
{
|
|
|
|
QString save = value;
|
|
|
|
value.replace(IBUS, "");
|
2015-02-02 02:14:14 +01:00
|
|
|
qDebug() << "Env: stripped" << IBUS << "from" << save << ":" << value;
|
2014-11-01 14:11:20 +01:00
|
|
|
}
|
2013-02-22 16:17:31 +01:00
|
|
|
#endif
|
2015-02-02 02:14:14 +01:00
|
|
|
qDebug() << "Env: " << key << value;
|
2014-11-01 14:11:20 +01:00
|
|
|
env.insert(key, value);
|
|
|
|
}
|
2014-11-09 00:19:54 +01:00
|
|
|
#ifdef LINUX
|
|
|
|
// HACK: Workaround for QTBUG-42500
|
|
|
|
env.insert("LD_LIBRARY_PATH", "");
|
|
|
|
#endif
|
2013-09-23 00:23:50 +02:00
|
|
|
|
2013-02-25 14:44:36 -06:00
|
|
|
// export some infos
|
2014-03-09 08:18:50 +01:00
|
|
|
auto variables = getVariables();
|
|
|
|
for (auto it = variables.begin(); it != variables.end(); ++it)
|
|
|
|
{
|
|
|
|
env.insert(it.key(), it.value());
|
|
|
|
}
|
2013-09-23 00:23:50 +02:00
|
|
|
|
2013-02-25 14:44:36 -06:00
|
|
|
this->setProcessEnvironment(env);
|
|
|
|
m_prepostlaunchprocess.setProcessEnvironment(env);
|
2013-09-23 00:23:50 +02:00
|
|
|
|
2013-02-25 14:44:36 -06:00
|
|
|
// std channels
|
|
|
|
connect(this, SIGNAL(readyReadStandardError()), SLOT(on_stdErr()));
|
|
|
|
connect(this, SIGNAL(readyReadStandardOutput()), SLOT(on_stdOut()));
|
2014-01-17 22:55:10 +01:00
|
|
|
|
|
|
|
// Log prepost launch command output (can be disabled.)
|
2015-05-23 16:07:47 +02:00
|
|
|
if (m_instance->settings()->get("LogPrePostOutput").toBool())
|
2014-01-17 22:55:10 +01:00
|
|
|
{
|
2014-03-09 08:18:50 +01:00
|
|
|
connect(&m_prepostlaunchprocess, &QProcess::readyReadStandardError, this,
|
2015-01-27 22:31:07 +01:00
|
|
|
&BaseProcess::on_prepost_stdErr);
|
2014-03-09 08:18:50 +01:00
|
|
|
connect(&m_prepostlaunchprocess, &QProcess::readyReadStandardOutput, this,
|
2015-01-27 22:31:07 +01:00
|
|
|
&BaseProcess::on_prepost_stdOut);
|
2014-01-17 22:55:10 +01:00
|
|
|
}
|
2014-07-23 00:12:18 +02:00
|
|
|
|
2014-06-30 02:02:57 +02:00
|
|
|
// a process has been constructed for the instance. It is running from MultiMC POV
|
|
|
|
m_instance->setRunning(true);
|
2013-02-22 16:17:31 +01:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:31:07 +01:00
|
|
|
|
|
|
|
void BaseProcess::setWorkdir(QString path)
|
2013-08-03 15:57:33 +02:00
|
|
|
{
|
|
|
|
QDir mcDir(path);
|
|
|
|
this->setWorkingDirectory(mcDir.absolutePath());
|
|
|
|
m_prepostlaunchprocess.setWorkingDirectory(mcDir.absolutePath());
|
|
|
|
}
|
|
|
|
|
2015-02-08 17:56:14 +01:00
|
|
|
void BaseProcess::printHeader()
|
|
|
|
{
|
|
|
|
emit log(m_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 22:31:07 +01:00
|
|
|
void BaseProcess::logOutput(const QStringList &lines, MessageLevel::Enum defaultLevel,
|
2014-01-17 22:55:10 +01:00
|
|
|
bool guessLevel, bool censor)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < lines.size(); ++i)
|
|
|
|
logOutput(lines[i], defaultLevel, guessLevel, censor);
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:31:07 +01:00
|
|
|
void BaseProcess::logOutput(QString line, MessageLevel::Enum defaultLevel, bool guessLevel,
|
2014-03-09 08:18:50 +01:00
|
|
|
bool censor)
|
2014-01-17 22:55:10 +01:00
|
|
|
{
|
|
|
|
MessageLevel::Enum level = defaultLevel;
|
|
|
|
|
|
|
|
// Level prefix
|
|
|
|
int endmark = line.indexOf("]!");
|
|
|
|
if (line.startsWith("!
|
|
|
|
{
|
|
|
|
if (state == QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
eventLoop.quit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto connection = connect(&m_prepostlaunchprocess, &QProcess::stateChanged, finisher);
|
|
|
|
int ret = eventLoop.exec();
|
|
|
|
disconnect(connection);
|
|
|
|
return ret == 0;
|
|
|
|
}
|
|
|
|
|
2015-01-27 22:31:07 +01:00
|
|
|
QString BaseProcess::substituteVariables(const QString &cmd) const
|
2014-03-09 08:18:50 +01:00
|
|
|
{
|
|
|
|
QString out = cmd;
|
|
|
|
auto variables = getVariables();
|
|
|
|
for (auto it = variables.begin(); it != variables.end(); ++it)
|
|
|
|
{
|
|
|
|
out.replace("$" + it.key(), it.value());
|
|
|
|
}
|
|
|
|
auto env = QProcessEnvironment::systemEnvironment();
|
|
|
|
for (auto var : env.keys())
|
|
|
|
{
|
|
|
|
out.replace("$" + var, env.value(var));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|