Parse q3config.cfg in the home directory when first run to get setting defaults.

This commit is contained in:
Jonathan Young 2014-05-09 15:17:33 +10:00
parent 701ddd5c10
commit 6686a5579f
4 changed files with 126 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include <Qt>
#include <QProcess>
#include <QTextStream>
#ifdef Q_OS_WIN32
#define WIN32_LEAN_AND_MEAN
@ -37,6 +38,16 @@ ioLaunch::ioLaunch(QWidget *parent) :
homePath = homeEnv + "/.q3a";
#endif
#endif
QDir homeDir(homePath);
// Try to parse q3config.cfg to get default settings if this is the first time the program has run.
if (!homePath.isEmpty() && homeDir.exists() && !settings.getHaveRun())
{
parseQuake3Config();
}
settings.setHaveRun(true);
}
ioLaunch::~ioLaunch()
@ -264,3 +275,103 @@ void ioLaunch::on_sbHeight_valueChanged(int arg1)
ioHedited = false;
}
}
// Since q3config.cfg is generated it's nice and clean and shouldn't need a full parser.
static QString ParseToken(const QString &s, int &offset)
{
// Skip whitespace.
while (offset < s.length() && s[offset] == ' ')
{
offset++;
}
if (offset >= s.length())
return QString();
// Check for quoted token.
bool quoted = s[offset] == '\"';
if (quoted)
offset++;
// Parse token.
int start = offset;
while (offset < s.length() && ((quoted && s[offset] != '\"') || (!quoted && s[offset] != ' ')))
{
offset++;
}
// Get token substring.
int end = offset;
if (quoted && s[offset] == '\"')
{
offset++;
}
if (end - start <= 0)
return QString();
return s.mid(start, end - start);
}
void ioLaunch::parseQuake3Config()
{
QFile file(homePath + "/baseq3/q3config.cfg");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream stream(&file);
// These may occur in any order, so process them after parsing the entire file.
QString r_mode, r_customwidth, r_customheight;
while (!stream.atEnd())
{
const QString line(stream.readLine());
// Skip comments.
if (line.startsWith("//"))
continue;
int offset = 0;
if (ParseToken(line, offset) == "seta")
{
const QString cvar(ParseToken(line, offset));
if (cvar == "r_mode")
{
r_mode = ParseToken(line, offset);
}
else if (cvar == "r_customwidth")
{
r_customwidth = ParseToken(line, offset);
}
else if (cvar == "r_customheight")
{
r_customheight = ParseToken(line, offset);
}
else if (cvar == "r_fullscreen")
{
// Set fullscreen/windows radio buttons.
const QString value(ParseToken(line, offset));
if (value == "0")
ui->rbWin->setChecked(true);
else if (value == "1")
ui->rbFull->setChecked(true);
}
}
}
// Populate resolution spinboxes.
if (r_mode == "-1" && !r_customwidth.isEmpty() && !r_customheight.isEmpty())
{
ui->sbWidth->setValue(r_customwidth.toInt());
ui->sbHeight->setValue(r_customheight.toInt());
ioWedited = ioHedited = true;
}
}

View File

@ -41,6 +41,8 @@ private slots:
void on_sbHeight_valueChanged(int arg1);
private:
void parseQuake3Config();
Ui::ioLaunch *ui;
QString resOption;
QString screenOption;

View File

@ -28,6 +28,16 @@ Settings::Settings() :
{
}
bool Settings::getHaveRun() const
{
return settings.value("haveRun").toBool();
}
void Settings::setHaveRun(bool value)
{
settings.setValue("haveRun", value);
}
QString Settings::getQuakePath() const
{
return settings.value("quakePath").toString();

View File

@ -32,6 +32,9 @@ class Settings
public:
Settings();
bool getHaveRun() const;
void setHaveRun(bool value);
QString getQuakePath() const;
bool containsQuakePath() const;
void setQuakePath(const QString &path);