mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 15:17:42 +02:00
backup code is now aware of possible file extension differences
This commit is contained in:
parent
fb93d08dd8
commit
ac882f86e2
4 changed files with 74 additions and 39 deletions
|
@ -1040,8 +1040,21 @@ bool EntityTree::sendEntitiesOperation(OctreeElement* element, void* extraData)
|
||||||
|
|
||||||
class ToMapOperator : public RecurseOctreeOperator {
|
class ToMapOperator : public RecurseOctreeOperator {
|
||||||
public:
|
public:
|
||||||
ToMapOperator(QVariantMap& map) : RecurseOctreeOperator(), _map(map) {};
|
ToMapOperator(QVariantMap& map, OctreeElement *top) : RecurseOctreeOperator(), _map(map), _top(top) {
|
||||||
bool preRecursion(OctreeElement* element) {return true;}
|
// if some element "top" was given, only save information for that element and it's children.
|
||||||
|
if (_top) {
|
||||||
|
_withinTop = false;
|
||||||
|
} else {
|
||||||
|
// top was NULL, export entire tree.
|
||||||
|
_withinTop = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
bool preRecursion(OctreeElement* element) {
|
||||||
|
if (element == _top) {
|
||||||
|
_withinTop = true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
bool postRecursion(OctreeElement* element) {
|
bool postRecursion(OctreeElement* element) {
|
||||||
qDebug() << " in ToMapOperator::preRecursion";
|
qDebug() << " in ToMapOperator::preRecursion";
|
||||||
EntityTreeElement* entityTreeElement = static_cast<EntityTreeElement*>(element);
|
EntityTreeElement* entityTreeElement = static_cast<EntityTreeElement*>(element);
|
||||||
|
@ -1052,19 +1065,22 @@ public:
|
||||||
entitiesQList << entityItem->writeToMap();
|
entitiesQList << entityItem->writeToMap();
|
||||||
}
|
}
|
||||||
_map["Entities"] = entitiesQList;
|
_map["Entities"] = entitiesQList;
|
||||||
|
if (element == _top) {
|
||||||
|
_withinTop = false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
QVariantMap& _map;
|
QVariantMap& _map;
|
||||||
|
OctreeElement *_top;
|
||||||
|
bool _withinTop;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool EntityTree::writeToMap(QVariantMap& entityDescription, OctreeElement* element) {
|
bool EntityTree::writeToMap(QVariantMap& entityDescription, OctreeElement* element) {
|
||||||
|
|
||||||
// XXX how can I make the RecurseOctreeOperator start with element?
|
|
||||||
entityDescription["Entities"] = QVariantList();
|
entityDescription["Entities"] = QVariantList();
|
||||||
ToMapOperator theOperator(entityDescription);
|
ToMapOperator theOperator(entityDescription, element);
|
||||||
recurseTreeWithOperator(&theOperator);
|
recurseTreeWithOperator(&theOperator);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,9 @@
|
||||||
#include "Octree.h"
|
#include "Octree.h"
|
||||||
#include "ViewFrustum.h"
|
#include "ViewFrustum.h"
|
||||||
|
|
||||||
|
|
||||||
|
QVector<QString> persistExtensions = {"svo", "json"};
|
||||||
|
|
||||||
float boundaryDistanceForRenderLevel(unsigned int renderLevel, float voxelSizeScale) {
|
float boundaryDistanceForRenderLevel(unsigned int renderLevel, float voxelSizeScale) {
|
||||||
return voxelSizeScale / powf(2, renderLevel);
|
return voxelSizeScale / powf(2, renderLevel);
|
||||||
}
|
}
|
||||||
|
@ -1846,44 +1849,18 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* element,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QString withoutFileExtension(const QString& fileName) {
|
|
||||||
return fileName.left(fileName.lastIndexOf("."));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool Octree::readFromFile(const char* fileName) {
|
bool Octree::readFromFile(const char* fileName) {
|
||||||
bool fileOk = false;
|
bool fileOk = false;
|
||||||
|
|
||||||
// try to ease migration from svo to json or back
|
QString qFileName = findMostRecentPersist(fileName);
|
||||||
QString svoFileName = withoutFileExtension(QString(fileName)) + ".svo";
|
// QByteArray byteArray = qFileName.toUtf8();
|
||||||
QFileInfo svoFileInfo(svoFileName);
|
// const char* cFileName = byteArray.constData();
|
||||||
QString jsonFileName = withoutFileExtension(QString(fileName)) + ".json";
|
|
||||||
QFileInfo jsonFileInfo(jsonFileName);
|
|
||||||
QString qFileName;
|
|
||||||
QFileInfo fileInfo;
|
|
||||||
|
|
||||||
if (jsonFileInfo.exists() && svoFileInfo.exists()) {
|
|
||||||
if (jsonFileInfo.lastModified() >= svoFileInfo.lastModified()) {
|
|
||||||
qFileName = jsonFileName;
|
|
||||||
fileInfo = jsonFileInfo;
|
|
||||||
} else {
|
|
||||||
qFileName = svoFileName;
|
|
||||||
fileInfo = svoFileInfo;
|
|
||||||
}
|
|
||||||
} else if (jsonFileInfo.exists()) {
|
|
||||||
qFileName = jsonFileName;
|
|
||||||
fileInfo = jsonFileInfo;
|
|
||||||
} else if (svoFileInfo.exists()) {
|
|
||||||
qFileName = svoFileName;
|
|
||||||
fileInfo = svoFileInfo;
|
|
||||||
} else {
|
|
||||||
qDebug() << "failed to read Octree from nonexistant file:" << fileName;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QFile file(qFileName);
|
QFile file(qFileName);
|
||||||
fileOk = file.open(QIODevice::ReadOnly);
|
fileOk = file.open(QIODevice::ReadOnly);
|
||||||
|
|
||||||
|
QFileInfo fileInfo(qFileName);
|
||||||
|
|
||||||
if(fileOk) {
|
if(fileOk) {
|
||||||
QDataStream fileInputStream(&file);
|
QDataStream fileInputStream(&file);
|
||||||
unsigned long fileLength = fileInfo.size();
|
unsigned long fileLength = fileInfo.size();
|
||||||
|
@ -2102,7 +2079,7 @@ void Octree::writeToFile(const char* fileName, OctreeElement* element, bool pers
|
||||||
if (persistAsJson) {
|
if (persistAsJson) {
|
||||||
// make the sure file extension is correct. This isn't great, but it allows a user with
|
// make the sure file extension is correct. This isn't great, but it allows a user with
|
||||||
// an existing .svo save to migrate.
|
// an existing .svo save to migrate.
|
||||||
QString qFileName = withoutFileExtension(QString(fileName)) + ".json";
|
QString qFileName = fileNameWithoutExtension(QString(fileName), persistExtensions) + ".json";
|
||||||
QByteArray byteArray = qFileName.toUtf8();
|
QByteArray byteArray = qFileName.toUtf8();
|
||||||
const char* cFileName = byteArray.constData();
|
const char* cFileName = byteArray.constData();
|
||||||
writeToJSONFile(cFileName, element);
|
writeToJSONFile(cFileName, element);
|
||||||
|
@ -2231,3 +2208,28 @@ void Octree::cancelImport() {
|
||||||
_stopImport = true;
|
_stopImport = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions) {
|
||||||
|
foreach (const QString possibleExtension, possibleExtensions) {
|
||||||
|
if (fileName.endsWith(possibleExtension) ||
|
||||||
|
fileName.endsWith(possibleExtension.toUpper()) ||
|
||||||
|
fileName.endsWith(possibleExtension.toLower())) {
|
||||||
|
return fileName.left(fileName.count() - possibleExtension.count() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString findMostRecentPersist(const QString& originalFileName) {
|
||||||
|
QString sansExt = fileNameWithoutExtension(originalFileName, persistExtensions);
|
||||||
|
QString newestFileName = originalFileName;
|
||||||
|
QDateTime newestTime = QDateTime::fromMSecsSinceEpoch(0);
|
||||||
|
foreach (QString possibleExtension, persistExtensions) {
|
||||||
|
QString fileName = sansExt + "." + possibleExtension;
|
||||||
|
QFileInfo fileInfo(fileName);
|
||||||
|
if (fileInfo.exists() && fileInfo.lastModified() > newestTime) {
|
||||||
|
newestFileName = fileName;
|
||||||
|
newestTime = fileInfo.lastModified();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newestFileName;
|
||||||
|
}
|
||||||
|
|
|
@ -35,6 +35,9 @@ class Shape;
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QReadWriteLock>
|
#include <QReadWriteLock>
|
||||||
|
|
||||||
|
|
||||||
|
extern QVector<QString> persistExtensions;
|
||||||
|
|
||||||
/// derive from this class to use the Octree::recurseTreeWithOperator() method
|
/// derive from this class to use the Octree::recurseTreeWithOperator() method
|
||||||
class RecurseOctreeOperator {
|
class RecurseOctreeOperator {
|
||||||
public:
|
public:
|
||||||
|
@ -410,4 +413,10 @@ protected:
|
||||||
|
|
||||||
float boundaryDistanceForRenderLevel(unsigned int renderLevel, float voxelSizeScale);
|
float boundaryDistanceForRenderLevel(unsigned int renderLevel, float voxelSizeScale);
|
||||||
|
|
||||||
|
|
||||||
|
QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions);
|
||||||
|
|
||||||
|
// XXX rename this
|
||||||
|
QString findMostRecentPersist(const QString& originalFileName);
|
||||||
|
|
||||||
#endif // hifi_Octree_h
|
#endif // hifi_Octree_h
|
||||||
|
|
|
@ -44,6 +44,14 @@ OctreePersistThread::OctreePersistThread(Octree* tree, const QString& filename,
|
||||||
_persistAsJson(persistAsJson)
|
_persistAsJson(persistAsJson)
|
||||||
{
|
{
|
||||||
parseSettings(settings);
|
parseSettings(settings);
|
||||||
|
|
||||||
|
QString sansExt = fileNameWithoutExtension(_filename, persistExtensions);
|
||||||
|
if (_persistAsJson) {
|
||||||
|
_filename = sansExt + "." + "json";
|
||||||
|
} else {
|
||||||
|
_filename = sansExt + "." + "svo";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OctreePersistThread::parseSettings(const QJsonObject& settings) {
|
void OctreePersistThread::parseSettings(const QJsonObject& settings) {
|
||||||
|
@ -347,8 +355,8 @@ void OctreePersistThread::rollOldBackupVersions(const BackupRule& rule) {
|
||||||
QString backupExtensionNplusOne = rule.extensionFormat;
|
QString backupExtensionNplusOne = rule.extensionFormat;
|
||||||
backupExtensionN.replace(QString("%N"), QString::number(n));
|
backupExtensionN.replace(QString("%N"), QString::number(n));
|
||||||
backupExtensionNplusOne.replace(QString("%N"), QString::number(n+1));
|
backupExtensionNplusOne.replace(QString("%N"), QString::number(n+1));
|
||||||
|
|
||||||
QString backupFilenameN = _filename + backupExtensionN;
|
QString backupFilenameN = findMostRecentPersist(_filename) + backupExtensionN;
|
||||||
QString backupFilenameNplusOne = _filename + backupExtensionNplusOne;
|
QString backupFilenameNplusOne = _filename + backupExtensionNplusOne;
|
||||||
|
|
||||||
QFile backupFileN(backupFilenameN);
|
QFile backupFileN(backupFilenameN);
|
||||||
|
|
Loading…
Reference in a new issue