Centralize file-changed logic in the callback. Ignore watched files for 5 seconds, when saving them. On Windows, there is a fuzzy delay between the time a file is written and when the signal fires in Qt, and we can't rely on blocking signals or temporarily removing the watched filepaths. It also fires three or four duplicate times for a single file.
This commit is contained in:
parent
b849ace6e2
commit
b9522c24f5
3 changed files with 29 additions and 13 deletions
|
@ -64,6 +64,7 @@ public:
|
||||||
QMap<QString, QString> facingDirections;
|
QMap<QString, QString> facingDirections;
|
||||||
ParseUtil parser;
|
ParseUtil parser;
|
||||||
QFileSystemWatcher fileWatcher;
|
QFileSystemWatcher fileWatcher;
|
||||||
|
QMap<QString, qint64> modifiedFileTimestamps;
|
||||||
|
|
||||||
void set_root(QString);
|
void set_root(QString);
|
||||||
|
|
||||||
|
@ -194,6 +195,8 @@ private:
|
||||||
void setNewMapEvents(Map *map);
|
void setNewMapEvents(Map *map);
|
||||||
void setNewMapConnections(Map *map);
|
void setNewMapConnections(Map *map);
|
||||||
|
|
||||||
|
void ignoreWatchedFileTemporarily(QString filepath);
|
||||||
|
|
||||||
static int num_tiles_primary;
|
static int num_tiles_primary;
|
||||||
static int num_tiles_total;
|
static int num_tiles_total;
|
||||||
static int num_metatiles_primary;
|
static int num_metatiles_primary;
|
||||||
|
|
|
@ -314,6 +314,7 @@ bool MainWindow::openProject(QString dir) {
|
||||||
&& setMap(getDefaultMap(), true);
|
&& setMap(getDefaultMap(), true);
|
||||||
} else {
|
} else {
|
||||||
QString open_map = editor->map->name;
|
QString open_map = editor->map->name;
|
||||||
|
editor->project->fileWatcher.removePaths(editor->project->fileWatcher.files());
|
||||||
editor->project->clearMapCache();
|
editor->project->clearMapCache();
|
||||||
editor->project->clearTilesetCache();
|
editor->project->clearTilesetCache();
|
||||||
success = loadDataStructures() && populateMapList() && setMap(open_map, true);
|
success = loadDataStructures() && populateMapList() && setMap(open_map, true);
|
||||||
|
@ -1260,7 +1261,6 @@ void MainWindow::on_actionUse_Encounter_Json_triggered(bool checked)
|
||||||
|
|
||||||
void MainWindow::on_actionMonitor_Project_Files_triggered(bool checked)
|
void MainWindow::on_actionMonitor_Project_Files_triggered(bool checked)
|
||||||
{
|
{
|
||||||
editor->project->fileWatcher.blockSignals(!checked);
|
|
||||||
porymapConfig.setMonitorFiles(checked);
|
porymapConfig.setMonitorFiles(checked);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,14 @@ Project::~Project()
|
||||||
void Project::initSignals() {
|
void Project::initSignals() {
|
||||||
// detect changes to specific filepaths being monitored
|
// detect changes to specific filepaths being monitored
|
||||||
QObject::connect(&fileWatcher, &QFileSystemWatcher::fileChanged, [this](QString changed){
|
QObject::connect(&fileWatcher, &QFileSystemWatcher::fileChanged, [this](QString changed){
|
||||||
|
if (!porymapConfig.getMonitorFiles()) return;
|
||||||
|
if (modifiedFileTimestamps.contains(changed)) {
|
||||||
|
if (QDateTime::currentMSecsSinceEpoch() < modifiedFileTimestamps[changed]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
modifiedFileTimestamps.remove(changed);
|
||||||
|
}
|
||||||
|
|
||||||
static bool showing = false;
|
static bool showing = false;
|
||||||
if (showing) return;
|
if (showing) return;
|
||||||
|
|
||||||
|
@ -108,7 +116,6 @@ void Project::initSignals() {
|
||||||
} else if (choice == QMessageBox::No) {
|
} else if (choice == QMessageBox::No) {
|
||||||
if (showAgainCheck.isChecked()) {
|
if (showAgainCheck.isChecked()) {
|
||||||
porymapConfig.setMonitorFiles(false);
|
porymapConfig.setMonitorFiles(false);
|
||||||
this->fileWatcher.blockSignals(true);
|
|
||||||
emit uncheckMonitorFilesAction();
|
emit uncheckMonitorFilesAction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -666,7 +673,6 @@ bool Project::readMapLayouts() {
|
||||||
|
|
||||||
void Project::saveMapLayouts() {
|
void Project::saveMapLayouts() {
|
||||||
QString layoutsFilepath = QString("%1/data/layouts/layouts.json").arg(root);
|
QString layoutsFilepath = QString("%1/data/layouts/layouts.json").arg(root);
|
||||||
fileWatcher.removePath(layoutsFilepath);
|
|
||||||
QFile layoutsFile(layoutsFilepath);
|
QFile layoutsFile(layoutsFilepath);
|
||||||
if (!layoutsFile.open(QIODevice::WriteOnly)) {
|
if (!layoutsFile.open(QIODevice::WriteOnly)) {
|
||||||
logError(QString("Error: Could not open %1 for writing").arg(layoutsFilepath));
|
logError(QString("Error: Could not open %1 for writing").arg(layoutsFilepath));
|
||||||
|
@ -696,12 +702,18 @@ void Project::saveMapLayouts() {
|
||||||
layoutsArr.push_back(layoutObj);
|
layoutsArr.push_back(layoutObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ignoreWatchedFileTemporarily(layoutsFilepath);
|
||||||
|
|
||||||
layoutsObj["layouts"] = layoutsArr;
|
layoutsObj["layouts"] = layoutsArr;
|
||||||
OrderedJson layoutJson(layoutsObj);
|
OrderedJson layoutJson(layoutsObj);
|
||||||
OrderedJsonDoc jsonDoc(&layoutJson);
|
OrderedJsonDoc jsonDoc(&layoutJson);
|
||||||
jsonDoc.dump(&layoutsFile);
|
jsonDoc.dump(&layoutsFile);
|
||||||
layoutsFile.close();
|
layoutsFile.close();
|
||||||
fileWatcher.addPath(layoutsFilepath);
|
}
|
||||||
|
|
||||||
|
void Project::ignoreWatchedFileTemporarily(QString filepath) {
|
||||||
|
// Ignore any file-change events for this filepath for the next 5 seconds.
|
||||||
|
modifiedFileTimestamps.insert(filepath, QDateTime::currentMSecsSinceEpoch() + 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::setNewMapLayout(Map* map) {
|
void Project::setNewMapLayout(Map* map) {
|
||||||
|
@ -726,7 +738,6 @@ void Project::setNewMapLayout(Map* map) {
|
||||||
|
|
||||||
void Project::saveMapGroups() {
|
void Project::saveMapGroups() {
|
||||||
QString mapGroupsFilepath = QString("%1/data/maps/map_groups.json").arg(root);
|
QString mapGroupsFilepath = QString("%1/data/maps/map_groups.json").arg(root);
|
||||||
fileWatcher.removePath(mapGroupsFilepath);
|
|
||||||
QFile mapGroupsFile(mapGroupsFilepath);
|
QFile mapGroupsFile(mapGroupsFilepath);
|
||||||
if (!mapGroupsFile.open(QIODevice::WriteOnly)) {
|
if (!mapGroupsFile.open(QIODevice::WriteOnly)) {
|
||||||
logError(QString("Error: Could not open %1 for writing").arg(mapGroupsFilepath));
|
logError(QString("Error: Could not open %1 for writing").arg(mapGroupsFilepath));
|
||||||
|
@ -752,18 +763,18 @@ void Project::saveMapGroups() {
|
||||||
groupNum++;
|
groupNum++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ignoreWatchedFileTemporarily(mapGroupsFilepath);
|
||||||
|
|
||||||
OrderedJson mapGroupJson(mapGroupsObj);
|
OrderedJson mapGroupJson(mapGroupsObj);
|
||||||
OrderedJsonDoc jsonDoc(&mapGroupJson);
|
OrderedJsonDoc jsonDoc(&mapGroupJson);
|
||||||
jsonDoc.dump(&mapGroupsFile);
|
jsonDoc.dump(&mapGroupsFile);
|
||||||
mapGroupsFile.close();
|
mapGroupsFile.close();
|
||||||
fileWatcher.addPath(mapGroupsFilepath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveWildMonData() {
|
void Project::saveWildMonData() {
|
||||||
if (!projectConfig.getEncounterJsonActive()) return;
|
if (!projectConfig.getEncounterJsonActive()) return;
|
||||||
|
|
||||||
QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root);
|
QString wildEncountersJsonFilepath = QString("%1/src/data/wild_encounters.json").arg(root);
|
||||||
fileWatcher.removePath(wildEncountersJsonFilepath);
|
|
||||||
QFile wildEncountersFile(wildEncountersJsonFilepath);
|
QFile wildEncountersFile(wildEncountersJsonFilepath);
|
||||||
if (!wildEncountersFile.open(QIODevice::WriteOnly)) {
|
if (!wildEncountersFile.open(QIODevice::WriteOnly)) {
|
||||||
logError(QString("Error: Could not open %1 for writing").arg(wildEncountersJsonFilepath));
|
logError(QString("Error: Could not open %1 for writing").arg(wildEncountersJsonFilepath));
|
||||||
|
@ -842,11 +853,12 @@ void Project::saveWildMonData() {
|
||||||
}
|
}
|
||||||
|
|
||||||
wildEncountersObject["wild_encounter_groups"] = wildEncounterGroups;
|
wildEncountersObject["wild_encounter_groups"] = wildEncounterGroups;
|
||||||
|
|
||||||
|
ignoreWatchedFileTemporarily(wildEncountersJsonFilepath);
|
||||||
OrderedJson encounterJson(wildEncountersObject);
|
OrderedJson encounterJson(wildEncountersObject);
|
||||||
OrderedJsonDoc jsonDoc(&encounterJson);
|
OrderedJsonDoc jsonDoc(&encounterJson);
|
||||||
jsonDoc.dump(&wildEncountersFile);
|
jsonDoc.dump(&wildEncountersFile);
|
||||||
wildEncountersFile.close();
|
wildEncountersFile.close();
|
||||||
fileWatcher.addPath(wildEncountersJsonFilepath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveMapConstantsHeader() {
|
void Project::saveMapConstantsHeader() {
|
||||||
|
@ -879,7 +891,10 @@ void Project::saveMapConstantsHeader() {
|
||||||
|
|
||||||
text += QString("#define MAP_GROUPS_COUNT %1\n\n").arg(groupNum);
|
text += QString("#define MAP_GROUPS_COUNT %1\n\n").arg(groupNum);
|
||||||
text += QString("#endif // GUARD_CONSTANTS_MAP_GROUPS_H\n");
|
text += QString("#endif // GUARD_CONSTANTS_MAP_GROUPS_H\n");
|
||||||
saveTextFile(root + "/include/constants/map_groups.h", text);
|
|
||||||
|
QString mapGroupFilepath = root + "/include/constants/map_groups.h";
|
||||||
|
ignoreWatchedFileTemporarily(mapGroupFilepath);
|
||||||
|
saveTextFile(mapGroupFilepath, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
// saves heal location coords in root + /src/data/heal_locations.h
|
// saves heal location coords in root + /src/data/heal_locations.h
|
||||||
|
@ -981,14 +996,12 @@ void Project::saveHealLocationStruct(Map *map) {
|
||||||
constants_text += QString("\n#endif // GUARD_CONSTANTS_HEAL_LOCATIONS_H\n");
|
constants_text += QString("\n#endif // GUARD_CONSTANTS_HEAL_LOCATIONS_H\n");
|
||||||
|
|
||||||
QString healLocationFilepath = root + "/src/data/heal_locations.h";
|
QString healLocationFilepath = root + "/src/data/heal_locations.h";
|
||||||
fileWatcher.removePath(healLocationFilepath);
|
ignoreWatchedFileTemporarily(healLocationFilepath);
|
||||||
saveTextFile(healLocationFilepath, data_text);
|
saveTextFile(healLocationFilepath, data_text);
|
||||||
fileWatcher.addPath(healLocationFilepath);
|
|
||||||
|
|
||||||
QString healLocationConstantsFilepath = root + "/include/constants/heal_locations.h";
|
QString healLocationConstantsFilepath = root + "/include/constants/heal_locations.h";
|
||||||
fileWatcher.removePath(healLocationConstantsFilepath);
|
ignoreWatchedFileTemporarily(healLocationConstantsFilepath);
|
||||||
saveTextFile(healLocationConstantsFilepath, constants_text);
|
saveTextFile(healLocationConstantsFilepath, constants_text);
|
||||||
fileWatcher.addPath(healLocationConstantsFilepath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Project::saveTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
void Project::saveTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) {
|
||||||
|
|
Loading…
Reference in a new issue