Commit 5f015ec4 authored by Stefan Brüns's avatar Stefan Brüns

[ExeThumbnailer] Simplify and cleanup wrestool invocation

Instead of parsing the type from "wrestool --list" output, supply the
type during the list and extraction invocation.

Simplify the regex, only capture the name and remove name from the
optional group.

As only GROUP_ICONs are listed, the first match is the correct one.
parent cba20605
......@@ -15,81 +15,51 @@
#include "icoutils.h"
#include <QList>
#include <QPair>
#include <QRegularExpression>
#include <QString>
#include <QStringList>
#include <QFile>
#include <QProcess>
#include <QSet>
#define abs(n) ( ( n < 0 ) ? -n : n )
typedef QPair < QString, int > IconInExe;
bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, QIODevice *outputDevice)
{
QProcess wrestool;
wrestool.start("wrestool", QStringList() << "-l" << inputFileName);
// list all resources with type RT_GROUP_ICON=14
wrestool.start(QStringLiteral("wrestool"), {QStringLiteral("-t14"), QStringLiteral("-l"), inputFileName});
wrestool.waitForFinished();
if ( wrestool.exitCode() != 0 )
if (wrestool.exitCode() != 0) {
return false;
QList<IconInExe> icons;
}
const QString output = QString::fromUtf8(wrestool.readAll());
// 16 bit binaries don't have "--language"
const QRegularExpression regExp(QStringLiteral("--type=(\\d+) (?:--name=(.*) --language=(.*)|--name=(.*)) \\[.*\\]"));
auto matches = regExp.globalMatch(output);
const QRegularExpression regExp(QStringLiteral("--type=\\d+ --name=(\\S+) (?:--language=.* )?\\[.*\\]"));
while (matches.hasNext()) {
const auto match = matches.next();
// https://docs.microsoft.com/en-us/windows/win32/menurc/about-icons#icon-display
// "Select the RT_GROUP_ICON resource. If more than one such resource exists,
// the system uses the first resource listed in the resource scrip."
auto match = regExp.match(output);
if (!match.hasMatch()) {
return false;
}
const int type = match.capturedRef(1).toInt();
if (type != 14) {
continue;
}
QString name = match.captured(1);
if (name.at(0) == '\'') {
name = name.mid(1, name.size()-2);
}
QString name = match.captured(2);
if (name.isEmpty()) {
name = match.captured(4);
}
wrestool.start(QStringLiteral("wrestool"), {QStringLiteral("-x"), QStringLiteral("-t14"), QStringLiteral("-n"), name, inputFileName});
wrestool.waitForFinished();
icons << qMakePair(name, type);
if (wrestool.exitCode() != 0) {
return false;
}
if ( icons.isEmpty() )
const QByteArray iconData = wrestool.readAllStandardOutput();
if (outputDevice->write(iconData) != iconData.size()) {
return false;
for (const IconInExe &icon : qAsConst(icons)) {
QString name = icon.first;
int type = icon.second;
if ( name.at(0) == '\'' )
name = name.mid(1, name.size()-2);
wrestool.start("wrestool", QStringList() << "-x" << "-t" << QString::number(type) << "-n" << name << inputFileName);
wrestool.waitForFinished();
if (wrestool.exitCode() != 0) {
return false;
}
const QByteArray iconData = wrestool.readAllStandardOutput();
if (outputDevice->write(iconData) != iconData.size()) {
return false;
}
return true;
}
return false;
return true;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment