Load and save the gMapAttributes array

This commit is contained in:
Marcus Huderle 2018-02-15 18:38:19 -08:00
parent f6cb002592
commit 40a0fbee02
5 changed files with 72 additions and 4 deletions

View file

@ -9,6 +9,7 @@ Editor::Editor()
void Editor::saveProject() {
if (project) {
project->saveAllDataStructures();
project->saveAllMaps();
}
}

View file

@ -61,10 +61,12 @@ void MainWindow::openProject(QString dir) {
editor->project = new Project;
editor->project->root = dir;
setWindowTitle(editor->project->getProjectTitle() + " - pretmap");
loadDataStructures();
populateMapList();
setMap(getDefaultMap());
} else {
setWindowTitle(editor->project->getProjectTitle() + " - pretmap");
loadDataStructures();
populateMapList();
}
}
@ -278,6 +280,10 @@ void MainWindow::on_checkBox_ShowLocation_clicked(bool checked)
}
}
void MainWindow::loadDataStructures() {
Project *project = editor->project;
project->readMapAttributesTable();
}
void MainWindow::populateMapList() {
Project *project = editor->project;

View file

@ -76,6 +76,7 @@ private:
Editor *editor = NULL;
QIcon* mapIcon;
void setMap(QString);
void loadDataStructures();
void populateMapList();
QString getExistingDirectory(QString);
void openProject(QString dir);

View file

@ -22,6 +22,7 @@ Project::Project()
map_cache = new QMap<QString, Map*>;
mapConstantsToMapNames = new QMap<QString, QString>;
mapNamesToMapConstants = new QMap<QString, QString>;
mapAttributesTable = new QMap<int, QString>;
tileset_cache = new QMap<QString, Tileset*>;
}
@ -171,6 +172,53 @@ void Project::saveMapHeader(Map *map) {
saveTextFile(header_path, text);
}
void Project::readMapAttributesTable() {
int curMapIndex = 1;
QString attributesText = readTextFile(getMapAttributesTableFilepath());
QList<QStringList>* values = parse(attributesText);
bool inAttributePointers = false;
for (int i = 0; i < values->length(); i++) {
QStringList params = values->value(i);
QString macro = params.value(0);
if (macro == ".label") {
if (inAttributePointers) {
break;
}
if (params.value(1) == "gMapAttributes") {
inAttributePointers = true;
}
} else if (macro == ".4byte" && inAttributePointers) {
QString mapName = params.value(1);
if (!mapName.contains("UnknownMapAttributes")) {
// Strip off "_MapAttributes" from the label if it's a real map label.
mapName = mapName.remove(mapName.length() - 14, 14);
}
mapAttributesTable->insert(curMapIndex, mapName);
curMapIndex++;
}
}
}
void Project::saveMapAttributesTable() {
QString text = "";
text += QString("\t.align 2\n");
text += QString("gMapAttributes::\n");
for (int i = 0; i < mapAttributesTable->count(); i++) {
int mapIndex = i + 1;
QString mapName = mapAttributesTable->value(mapIndex);
if (!mapName.contains("UnknownMapAttributes")) {
text += QString("\t.4byte %1_MapAttributes\n").arg(mapName);
} else {
text += QString("\t.4byte %1\n").arg(mapName);
}
}
saveTextFile(getMapAttributesTableFilepath(), text);
}
QString Project::getMapAttributesTableFilepath() {
return QString("%1/data/maps/attributes_table.inc").arg(root);
}
void Project::readMapAttributes(Map* map) {
Asm *parser = new Asm;
@ -277,6 +325,10 @@ void Project::saveMap(Map *map) {
saveMapEvents(map);
}
void Project::saveAllDataStructures() {
saveMapAttributesTable();
}
void Project::loadTilesetAssets(Tileset* tileset) {
Asm* parser = new Asm;
QString category = (tileset->is_secondary == "TRUE") ? "secondary" : "primary";
@ -543,8 +595,6 @@ void Project::readMapGroups() {
}
void Project::addNewMapToGroup(QString mapName, int groupNum) {
int mapIndex = 0;// TODO: need to calculate the new map index.
// Write new map to project files.
// 1. Create directory data/maps/<map_name>/
// 2. Create file data/maps/<map_name>/border.bin
@ -563,10 +613,14 @@ void Project::addNewMapToGroup(QString mapName, int groupNum) {
// 12. Modify data/maps/attributes_table.inc
// 13. Modify data/maps/headers.inc
// 1. Create directory data/maps/<map_name>/
int mapIndex = mapAttributesTable->count() + 1;
mapAttributesTable->insert(mapIndex, mapName);
QString dataDir = QString("%1/data/").arg(root);
QString dataMapsDir = QString("%1maps/").arg(dataDir);
QString newMapDataDir = QString("%1%2/").arg(dataMapsDir).arg(mapName);
// 1. Create directory data/maps/<map_name>/
if (!QDir::root().mkdir(newMapDataDir)) {
qDebug() << "Error: failed to create directory for new map. " << newMapDataDir;
return;

View file

@ -19,6 +19,7 @@ public:
QStringList *mapNames = NULL;
QMap<QString, QString> *mapConstantsToMapNames;
QMap<QString, QString> *mapNamesToMapConstants;
QMap<int, QString> *mapAttributesTable;
QMap<QString, Map*> *map_cache;
Map* loadMap(QString);
@ -42,6 +43,7 @@ public:
QList<QStringList>* getLabelMacros(QList<QStringList>*, QString);
QStringList* getLabelValues(QList<QStringList>*, QString);
void readMapHeader(Map*);
void readMapAttributesTable();
void readMapAttributes(Map*);
void getTilesets(Map*);
void loadTilesetAssets(Tileset*);
@ -51,7 +53,7 @@ public:
void writeBlockdata(QString, Blockdata*);
void saveAllMaps();
void saveMap(Map*);
void saveMapHeader(Map*);
void saveAllDataStructures();
QList<QStringList>* parse(QString text);
QStringList getSongNames();
@ -77,6 +79,10 @@ public:
QStringList readCArray(QString text, QString label);
QString readCIncbin(QString text, QString label);
QMap<QString, int> readCDefines(QString text, QStringList prefixes);
private:
QString getMapAttributesTableFilepath();
void saveMapHeader(Map*);
void saveMapAttributesTable();
};
#endif // PROJECT_H