diff --git a/mainwindow.cpp b/mainwindow.cpp index a053ec90..3ee30ad2 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -285,6 +285,8 @@ void MainWindow::loadDataStructures() { project->readMapAttributesTable(); project->readAllMapAttributes(); project->readItemNames(); + project->readFlagNames(); + project->readVarNames(); } void MainWindow::populateMapList() { @@ -570,8 +572,8 @@ void MainWindow::updateSelectedObjects() { field_labels["sight_radius"] = "Sight Radius"; field_labels["destination_warp"] = "Destination Warp"; field_labels["destination_map_name"] = "Destination Map"; - field_labels["coord_unknown1"] = "Unknown 1"; - field_labels["coord_unknown2"] = "Unknown 2"; + field_labels["script_var"] = "Var"; + field_labels["script_var_value"] = "Var Value"; field_labels["type"] = "Type"; field_labels["item"] = "Item"; field_labels["item_unknown5"] = "Unknown 5"; @@ -614,8 +616,8 @@ void MainWindow::updateSelectedObjects() { } else if (event_type == "trap") { fields << "script_label"; - fields << "coord_unknown1"; - fields << "coord_unknown2"; + fields << "script_var"; + fields << "script_var_value"; } else if (event_type == "trap_weather") { fields << "weather"; @@ -650,6 +652,16 @@ void MainWindow::updateSelectedObjects() { combo->addItem(value); } combo->addItems(*editor->project->itemNames); + } else if (key == "flag") { + if (!editor->project->flagNames->contains(value)) { + combo->addItem(value); + } + combo->addItems(*editor->project->flagNames); + } else if (key == "script_var") { + if (!editor->project->varNames->contains(value)) { + combo->addItem(value); + } + combo->addItems(*editor->project->varNames); } else { combo->addItem(value); } diff --git a/project.cpp b/project.cpp index d036795c..6768e1fd 100755 --- a/project.cpp +++ b/project.cpp @@ -19,6 +19,8 @@ Project::Project() map_groups = new QMap; mapNames = new QStringList; itemNames = new QStringList; + flagNames = new QStringList; + varNames = new QStringList; map_cache = new QMap; mapConstantsToMapNames = new QMap; mapNamesToMapConstants = new QMap; @@ -1064,25 +1066,39 @@ QStringList Project::getBattleScenes() { } void Project::readItemNames() { - QString text = readTextFile(root + "/include/constants/items.h"); + QString filepath = root + "/include/constants/items.h"; + QStringList prefixes = (QStringList() << "ITEM_"); + readCDefinesSorted(filepath, prefixes, itemNames); +} + +void Project::readFlagNames() { + QString filepath = root + "/include/constants/flags.h"; + QStringList prefixes = (QStringList() << "FLAG_"); + readCDefinesSorted(filepath, prefixes, flagNames, "SYSTEM_FLAGS", 0x800); +} + +void Project::readVarNames() { + QString filepath = root + "/include/constants/vars.h"; + QStringList prefixes = (QStringList() << "VAR_"); + readCDefinesSorted(filepath, prefixes, varNames); +} + +void Project::readCDefinesSorted(QString filepath, QStringList prefixes, QStringList* definesToSet) { + return readCDefinesSorted(filepath, prefixes, definesToSet, "", 0); +} + +void Project::readCDefinesSorted(QString filepath, QStringList prefixes, QStringList* definesToSet, QString hardcodedDefine, int hardcodedDefineValue) { + QString text = readTextFile(filepath); if (!text.isNull()) { - QStringList itemDefinePrefixes; - itemDefinePrefixes << "ITEM_"; - QMap itemDefines = readCDefines(text, itemDefinePrefixes); + QMap defines = readCDefines(text, prefixes, hardcodedDefine, hardcodedDefineValue); - // The item names should to be sorted by their underlying value, not alphabetically. + // The defines should to be sorted by their underlying value, not alphabetically. // Reverse the map and read out the resulting keys in order. - QMultiMap itemDefinesInverse; - for (QString itemName : itemDefines.keys()) { - itemDefinesInverse.insert(itemDefines[itemName], itemName); - } - - for (int itemValue : itemDefinesInverse.keys()) { - QList names = itemDefinesInverse.values(itemValue); - for (QString name : names) { - itemNames->append(name); - } + QMultiMap definesInverse; + for (QString defineName : defines.keys()) { + definesInverse.insert(defines[defineName], defineName); } + *definesToSet = definesInverse.values(); } } @@ -1252,8 +1268,8 @@ void Project::saveMapEvents(Map *map) { text += QString(", %1").arg(coords->get("y")); text += QString(", %1").arg(coords->get("elevation")); text += QString(", 0"); - text += QString(", %1").arg(coords->get("coord_unknown1")); - text += QString(", %1").arg(coords->get("coord_unknown2")); + text += QString(", %1").arg(coords->get("script_var")); + text += QString(", %1").arg(coords->get("script_var_value")); text += QString(", 0"); text += QString(", %1").arg(coords->get("script_label")); text += "\n"; @@ -1413,8 +1429,8 @@ void Project::readMapEvents(Map *map) { coord->put("x", command.value(i++)); coord->put("y", command.value(i++)); coord->put("elevation", command.value(i++)); - coord->put("coord_unknown1", command.value(i++)); - coord->put("coord_unknown2", command.value(i++)); + coord->put("script_var", command.value(i++)); + coord->put("script_var_value", command.value(i++)); coord->put("script_label", command.value(i++)); //coord_unknown3 //coord_unknown4 @@ -1539,23 +1555,37 @@ QString Project::readCIncbin(QString text, QString label) { } QMap Project::readCDefines(QString text, QStringList prefixes) { + return readCDefines(text, prefixes, "", 0); +} + +QMap Project::readCDefines(QString text, QStringList prefixes, QString hardcodedDefine, int hardcodedDefineValue) { QMap defines; - QString combinedPrefixes = "[" + prefixes.join('|') + "]"; - QRegularExpression re(QString("#define\\s+(?%1\\w+)\\s(?\\w+)").arg(combinedPrefixes)); + QString combinedPrefixes = "(" + prefixes.join('|') + ")"; + QString regex; + if (hardcodedDefine.isEmpty()) { + regex = QString("#define\\s+(?(%1)\\w+)\\s+(?\\w+)").arg(combinedPrefixes); + } else { + regex = QString("#define\\s+(?(%1)\\w+)\\s+\\(*(?\\\s*%2\\s+\\+\\s+)*(?\\w+\)\\)*").arg(combinedPrefixes, hardcodedDefine); + } + + QRegularExpression re(regex); QRegularExpressionMatchIterator iter = re.globalMatch(text); while (iter.hasNext()) { QRegularExpressionMatch match = iter.next(); QString name = match.captured("defineName"); + QString hardcodedDefineName = match.captured("hardcodedDefineName"); QString value = match.captured("defineValue"); bool valid; int parsedValue = value.startsWith("0x") ? value.toInt(&valid, 16) : value.toInt(&valid, 10); if (valid) { - if (!defines.contains(name)) { - defines.insert(name, parsedValue); - } else { - qDebug() << QString("Define '%1' is defined multiple times'").arg(name); + int actualValue = parsedValue; + if (!hardcodedDefine.isEmpty() && !hardcodedDefineName.isEmpty()) { + actualValue += hardcodedDefineValue; } + defines.insert(name, actualValue); + } else if (defines.contains(value)) { + defines.insert(name, defines.value(value)); } else { qDebug() << QString("Failed to parse define '%1' value '%2' as base 10 or hexadecimal value").arg(name, value); } diff --git a/project.h b/project.h index 2a67f0b6..d595821b 100755 --- a/project.h +++ b/project.h @@ -24,6 +24,8 @@ public: QMap> mapAttributes; QMap> mapAttributesMaster; QStringList *itemNames = NULL; + QStringList *flagNames = NULL; + QStringList *varNames = NULL; QMap *map_cache; Map* loadMap(QString); @@ -74,6 +76,8 @@ public: QStringList getMapTypes(); QStringList getBattleScenes(); void readItemNames(); + void readFlagNames(); + void readVarNames(); void loadObjectPixmaps(QList objects); QMap getMapObjGfxConstants(); @@ -90,12 +94,15 @@ public: QStringList readCArray(QString text, QString label); QString readCIncbin(QString text, QString label); QMap readCDefines(QString text, QStringList prefixes); + QMap readCDefines(QString text, QStringList prefixes, QString hardcodedDefine, int hardcodedDefineValue); private: QString getMapAttributesTableFilepath(); QString getMapAssetsFilepath(); void saveMapHeader(Map*); void saveMapAttributesTable(); void updateMapAttributes(Map* map); + void readCDefinesSorted(QString, QStringList, QStringList*); + void readCDefinesSorted(QString, QStringList, QStringList*, QString, int); void setNewMapHeader(Map* map, int mapIndex); void setNewMapAttributes(Map* map);