read multiple incbins and C arrays at once for situations where the same file could be opened many times

This commit is contained in:
garak 2023-01-17 01:17:24 -05:00 committed by t
parent 9cc55ef2f7
commit 6b47d350a0
3 changed files with 60 additions and 22 deletions

View file

@ -13,8 +13,6 @@
extern QMap<QString, QString> gFileCache;
enum TokenClass { enum TokenClass {
Number, Number,
Operator, Operator,
@ -51,9 +49,11 @@ public:
static int textFileLineCount(const QString &path); static int textFileLineCount(const QString &path);
QList<QStringList> parseAsm(const QString &filename); QList<QStringList> parseAsm(const QString &filename);
int evaluateDefine(const QString&, const QMap<QString, int>&); int evaluateDefine(const QString&, const QMap<QString, int>&);
QStringList readCArray(const QString &text, const QString &label); QStringList readCArray(const QString &filename, const QString &label);
QMap<QString, QStringList> readCArrayMulti(const QString &filename);
QMap<QString, QString> readNamedIndexCArray(const QString &text, const QString &label); QMap<QString, QString> readNamedIndexCArray(const QString &text, const QString &label);
QString readCIncbin(const QString &text, const QString &label); QString readCIncbin(const QString &text, const QString &label);
QMap<QString, QString> readCIncbinMulti(const QString &filepath);
QStringList readCIncbinArray(const QString &filename, const QString &label); QStringList readCIncbinArray(const QString &filename, const QString &label);
QMap<QString, int> readCDefines(const QString &filename, const QStringList &prefixes, QMap<QString, int> = { }); QMap<QString, int> readCDefines(const QString &filename, const QStringList &prefixes, QMap<QString, int> = { });
QStringList readCDefinesSorted(const QString&, const QStringList&, const QMap<QString, int>& = { }); QStringList readCDefinesSorted(const QString&, const QStringList&, const QMap<QString, int>& = { });

View file

@ -15,13 +15,9 @@ const QRegularExpression ParseUtil::re_poryScriptLabel("\\b(script)(\\((global|l
const QRegularExpression ParseUtil::re_globalPoryScriptLabel("\\b(script)(\\((global)\\))?\\s*\\b(?<label>[\\w_][\\w\\d_]*)"); const QRegularExpression ParseUtil::re_globalPoryScriptLabel("\\b(script)(\\((global)\\))?\\s*\\b(?<label>[\\w_][\\w\\d_]*)");
const QRegularExpression ParseUtil::re_poryRawSection("\\b(raw)\\s*`(?<raw_script>[^`]*)"); const QRegularExpression ParseUtil::re_poryRawSection("\\b(raw)\\s*`(?<raw_script>[^`]*)");
QMap<QString, QString> gFileCache;
using OrderedJson = poryjson::Json; using OrderedJson = poryjson::Json;
ParseUtil::ParseUtil() { ParseUtil::ParseUtil() { }
gFileCache.clear();
}
void ParseUtil::set_root(const QString &dir) { void ParseUtil::set_root(const QString &dir) {
this->root = dir; this->root = dir;
@ -58,10 +54,6 @@ QString ParseUtil::createErrorMessage(const QString &message, const QString &exp
} }
QString ParseUtil::readTextFile(const QString &path) { QString ParseUtil::readTextFile(const QString &path) {
if (gFileCache.contains(path)) {
return gFileCache[path];
}
QFile file(path); QFile file(path);
if (!file.open(QIODevice::ReadOnly)) { if (!file.open(QIODevice::ReadOnly)) {
logError(QString("Could not open '%1': ").arg(path) + file.errorString()); logError(QString("Could not open '%1': ").arg(path) + file.errorString());
@ -76,7 +68,6 @@ QString ParseUtil::readTextFile(const QString &path) {
text += in.readLine() + '\n'; text += in.readLine() + '\n';
} }
gFileCache[path] = text;
return text; return text;
} }
@ -287,6 +278,25 @@ QString ParseUtil::readCIncbin(const QString &filename, const QString &label) {
return path; return path;
} }
QMap<QString, QString> ParseUtil::readCIncbinMulti(const QString &filepath) {
QMap<QString, QString> incbinMap;
this->file = filepath;
this->text = readTextFile(this->root + "/" + filepath);
static const QRegularExpression regex("(?<label>[A-Za-z0-9_]+)\\s*\\[?\\s*\\]?\\s*=\\s*INCBIN_[US][0-9][0-9]?\\(\\s*\\\"(?<path>[^\\\\\"]*)\\\"\\s*\\)");
QRegularExpressionMatchIterator iter = regex.globalMatch(this->text);
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
QString label = match.captured("label");
QString labelText = match.captured("path");
incbinMap[label] = labelText;
}
return incbinMap;
}
QStringList ParseUtil::readCIncbinArray(const QString &filename, const QString &label) { QStringList ParseUtil::readCIncbinArray(const QString &filename, const QString &label) {
QStringList paths; QStringList paths;
@ -415,6 +425,35 @@ QStringList ParseUtil::readCArray(const QString &filename, const QString &label)
return list; return list;
} }
QMap<QString, QStringList> ParseUtil::readCArrayMulti(const QString &filename) {
QMap<QString, QStringList> map;
this->file = filename;
this->text = readTextFile(this->root + "/" + filename);
static const QRegularExpression regex(R"((?<label>\b[A-Za-z0-9_]+\b)\s*(\[[^\]]*\])?\s*=\s*\{(?<body>[^\}]*)\})");
QRegularExpressionMatchIterator iter = regex.globalMatch(this->text);
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
QString label = match.captured("label");
QString body = match.captured("body");
QStringList list;
QStringList split = body.split(',');
for (QString item : split) {
item = item.trimmed();
static const QRegularExpression validChars("[^A-Za-z0-9_&()\\s]");
if (!item.contains(validChars)) list.append(item);
// do not print error info here because this is called dozens of times
}
map[label] = list;
}
return map;
}
QMap<QString, QString> ParseUtil::readNamedIndexCArray(const QString &filename, const QString &label) { QMap<QString, QString> ParseUtil::readNamedIndexCArray(const QString &filename, const QString &label) {
this->text = readTextFile(this->root + "/" + filename); this->text = readTextFile(this->root + "/" + filename);
QMap<QString, QString> map; QMap<QString, QString> map;

View file

@ -1563,9 +1563,6 @@ void Project::saveTextFile(QString path, QString text) {
QFile file(path); QFile file(path);
if (file.open(QIODevice::WriteOnly)) { if (file.open(QIODevice::WriteOnly)) {
file.write(text.toUtf8()); file.write(text.toUtf8());
if (gFileCache.contains(path)) {
gFileCache[path] = text;
}
} else { } else {
logError(QString("Could not open '%1' for writing: ").arg(path) + file.errorString()); logError(QString("Could not open '%1' for writing: ").arg(path) + file.errorString());
} }
@ -1575,7 +1572,6 @@ void Project::appendTextFile(QString path, QString text) {
QFile file(path); QFile file(path);
if (file.open(QIODevice::Append)) { if (file.open(QIODevice::Append)) {
file.write(text.toUtf8()); file.write(text.toUtf8());
gFileCache[path] += text;
} else { } else {
logError(QString("Could not open '%1' for appending: ").arg(path) + file.errorString()); logError(QString("Could not open '%1' for appending: ").arg(path) + file.errorString());
} }
@ -1585,8 +1581,6 @@ void Project::deleteFile(QString path) {
QFile file(path); QFile file(path);
if (file.exists() && !file.remove()) { if (file.exists() && !file.remove()) {
logError(QString("Could not delete file '%1': ").arg(path) + file.errorString()); logError(QString("Could not delete file '%1': ").arg(path) + file.errorString());
} else {
gFileCache.remove(path);
} }
} }
@ -2406,6 +2400,10 @@ bool Project::readEventGraphics() {
QString filepath = projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx_info); QString filepath = projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx_info);
const auto gfxInfos = parser.readCStructs(filepath, "", gfxInfoMemberMap); const auto gfxInfos = parser.readCStructs(filepath, "", gfxInfoMemberMap);
QMap<QString, QStringList> picTables = parser.readCArrayMulti(projectConfig.getFilePath(ProjectFilePath::data_obj_event_pic_tables));
QMap<QString, QString> graphicIncbins = parser.readCIncbinMulti(projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx));
for (QString gfxName : gfxNames) { for (QString gfxName : gfxNames) {
EventGraphics * eventGraphics = new EventGraphics; EventGraphics * eventGraphics = new EventGraphics;
@ -2420,10 +2418,10 @@ bool Project::readEventGraphics() {
QString dimensions_label = gfxInfoAttributes.value("oam"); QString dimensions_label = gfxInfoAttributes.value("oam");
QString subsprites_label = gfxInfoAttributes.value("subspriteTables"); QString subsprites_label = gfxInfoAttributes.value("subspriteTables");
QString gfx_label = parser.readCArray(projectConfig.getFilePath(ProjectFilePath::data_obj_event_pic_tables), pic_label).value(0); QString gfx_label = picTables[pic_label].value(0);
static const QRegularExpression re_parens("[\\(\\)]"); static const QRegularExpression re_parens("[\\(\\)]");
gfx_label = gfx_label.section(re_parens, 1, 1); gfx_label = gfx_label.section(re_parens, 1, 1);
QString path = parser.readCIncbin(projectConfig.getFilePath(ProjectFilePath::data_obj_event_gfx), gfx_label); QString path = graphicIncbins[gfx_label];
if (!path.isNull()) { if (!path.isNull()) {
path = fixGraphicPath(path); path = fixGraphicPath(path);
@ -2461,8 +2459,9 @@ bool Project::readSpeciesIconPaths() {
fileWatcher.addPath(root + "/" + srcfilename); fileWatcher.addPath(root + "/" + srcfilename);
fileWatcher.addPath(root + "/" + incfilename); fileWatcher.addPath(root + "/" + incfilename);
QMap<QString, QString> monIconNames = parser.readNamedIndexCArray(srcfilename, "gMonIconTable"); QMap<QString, QString> monIconNames = parser.readNamedIndexCArray(srcfilename, "gMonIconTable");
QMap<QString, QString> iconIncbins = parser.readCIncbinMulti(incfilename);
for (QString species : monIconNames.keys()) { for (QString species : monIconNames.keys()) {
QString path = parser.readCIncbin(incfilename, monIconNames.value(species)); QString path = iconIncbins[monIconNames.value(species)];
speciesToIconPath.insert(species, root + "/" + path.replace("4bpp", "png")); speciesToIconPath.insert(species, root + "/" + path.replace("4bpp", "png"));
} }
return true; return true;