instead of a json checkbox, a dropdown to chose the file type

This commit is contained in:
Seth Alves 2015-03-12 09:34:01 -07:00
parent ac882f86e2
commit 3bc85c2531
8 changed files with 44 additions and 62 deletions

View file

@ -1036,12 +1036,12 @@ void OctreeServer::readConfiguration() {
strcpy(_persistFilename, qPrintable(persistFilename)); strcpy(_persistFilename, qPrintable(persistFilename));
qDebug("persistFilename=%s", _persistFilename); qDebug("persistFilename=%s", _persistFilename);
bool persistAsJson; QString persistAsFileType;
if (!readOptionBool(QString("persistAsJson"), settingsSectionObject, persistAsJson)) { if (!readOptionString(QString("persistAsFileType"), settingsSectionObject, persistAsFileType)) {
persistAsJson = false; persistAsFileType = "svo";
} }
_persistAsJson = persistAsJson; _persistAsFileType = persistAsFileType;
qDebug() << "persistAsJson=" << _persistAsJson; qDebug() << "persistAsFileType=" << _persistAsFileType;
_persistInterval = OctreePersistThread::DEFAULT_PERSIST_INTERVAL; _persistInterval = OctreePersistThread::DEFAULT_PERSIST_INTERVAL;
readOptionInt(QString("persistInterval"), settingsSectionObject, _persistInterval); readOptionInt(QString("persistInterval"), settingsSectionObject, _persistInterval);
@ -1138,7 +1138,7 @@ void OctreeServer::run() {
// now set up PersistThread // now set up PersistThread
_persistThread = new OctreePersistThread(_tree, _persistFilename, _persistInterval, _persistThread = new OctreePersistThread(_tree, _persistFilename, _persistInterval,
_wantBackup, _settings, _debugTimestampNow, _persistAsJson); _wantBackup, _settings, _debugTimestampNow, _persistAsFileType);
if (_persistThread) { if (_persistThread) {
_persistThread->initialize(true); _persistThread->initialize(true);
} }

View file

@ -157,7 +157,7 @@ protected:
QString _statusHost; QString _statusHost;
char _persistFilename[MAX_FILENAME_LENGTH]; char _persistFilename[MAX_FILENAME_LENGTH];
bool _persistAsJson; QString _persistAsFileType;
int _packetsPerClientPerInterval; int _packetsPerClientPerInterval;
int _packetsTotalPerInterval; int _packetsTotalPerInterval;
Octree* _tree; // this IS a reaveraging tree Octree* _tree; // this IS a reaveraging tree

View file

@ -326,11 +326,21 @@
"advanced": true "advanced": true
}, },
{ {
"name": "persistAsJson", "name": "persistAsFileType",
"type": "checkbox", "label": "File format for entity server's persistent data",
"label": "Encode persist file as JSON:", "help": "This defines how the entity server will save entities to disk.",
"help": "Entity server will save entities as JSON rather than SVO", "default": "svo",
"default": false, "type": "select",
"options": [
{
"value": "svo",
"label": "Entity server persists data as SVO"
},
{
"value": "json",
"label": "Entity server persists data as JSON"
}
]
"advanced": true "advanced": true
}, },
{ {

View file

@ -1848,21 +1848,16 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* element,
return bytesAtThisLevel; return bytesAtThisLevel;
} }
bool Octree::readFromFile(const char* fileName) { bool Octree::readFromFile(const char* fileName) {
bool fileOk = false; bool fileOk = false;
QString qFileName = findMostRecentPersist(fileName); QString qFileName = findMostRecentPersist(fileName);
// QByteArray byteArray = qFileName.toUtf8();
// const char* cFileName = byteArray.constData();
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);
QFileInfo fileInfo(qFileName);
unsigned long fileLength = fileInfo.size(); unsigned long fileLength = fileInfo.size();
emit importSize(1.0f, 1.0f, 1.0f); emit importSize(1.0f, 1.0f, 1.0f);
@ -2057,38 +2052,30 @@ bool Octree::readSVOFromStream(unsigned long streamLength, QDataStream& inputStr
} }
bool Octree::readJSONFromStream(unsigned long streamLength, QDataStream& inputStream) { bool Octree::readJSONFromStream(unsigned long streamLength, QDataStream& inputStream) {
// QFile file;
// file.setFileName("/tmp/ok.json");
// file.open(QIODevice::ReadOnly | QIODevice::Text);
// QString val = file.readAll();
// file.close();
char rawData[streamLength]; char rawData[streamLength];
inputStream.readRawData(rawData, streamLength); inputStream.readRawData(rawData, streamLength);
// QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonDocument d = QJsonDocument::fromJson(rawData); QJsonDocument d = QJsonDocument::fromJson(rawData);
QVariant v = d.toVariant(); QVariant v = d.toVariant();
QVariantMap m = v.toMap(); QVariantMap m = v.toMap();
readFromMap(m); readFromMap(m);
return true; return true;
} }
void Octree::writeToFile(const char* fileName, OctreeElement* element, bool persistAsJson) { void Octree::writeToFile(const char* fileName, OctreeElement* element, QString persistAsFileType) {
if (persistAsJson) { // make the sure file extension makes sense
// make the sure file extension is correct. This isn't great, but it allows a user with QString qFileName = fileNameWithoutExtension(QString(fileName), persistExtensions) + "." + persistAsFileType;
// an existing .svo save to migrate. QByteArray byteArray = qFileName.toUtf8();
QString qFileName = fileNameWithoutExtension(QString(fileName), persistExtensions) + ".json"; const char* cFileName = byteArray.constData();
QByteArray byteArray = qFileName.toUtf8();
const char* cFileName = byteArray.constData(); if (persistAsFileType == "svo") {
writeToSVOFile(fileName, element);
} else if (persistAsFileType == "json") {
writeToJSONFile(cFileName, element); writeToJSONFile(cFileName, element);
} else { } else {
writeToSVOFile(fileName, element); qDebug() << "unable to write octree to file of type" << persistAsFileType;
} }
} }
void Octree::writeToJSONFile(const char* fileName, OctreeElement* element) { void Octree::writeToJSONFile(const char* fileName, OctreeElement* element) {
QFile persistFile(fileName); QFile persistFile(fileName);
QVariantMap entityDescription; QVariantMap entityDescription;
@ -2110,7 +2097,6 @@ void Octree::writeToJSONFile(const char* fileName, OctreeElement* element) {
} }
} }
void Octree::writeToSVOFile(const char* fileName, OctreeElement* element) { void Octree::writeToSVOFile(const char* fileName, OctreeElement* element) {
std::ofstream file(fileName, std::ios::out|std::ios::binary); std::ofstream file(fileName, std::ios::out|std::ios::binary);

View file

@ -214,7 +214,6 @@ public:
{} {}
}; };
class Octree : public QObject { class Octree : public QObject {
Q_OBJECT Q_OBJECT
public: public:
@ -328,21 +327,19 @@ public:
// Note: this assumes the fileFormat is the HIO individual voxels code files // Note: this assumes the fileFormat is the HIO individual voxels code files
void loadOctreeFile(const char* fileName, bool wantColorRandomizer); void loadOctreeFile(const char* fileName, bool wantColorRandomizer);
// these will read/write files that match the wireformat, excluding the 'V' leading // Octree exporters
void writeToFile(const char* filename, OctreeElement* element = NULL, bool persistAsJson = false); void writeToFile(const char* filename, OctreeElement* element = NULL, QString persistAsFileType = "svo");
void writeToJSONFile(const char* filename, OctreeElement* element = NULL); void writeToJSONFile(const char* filename, OctreeElement* element = NULL);
void writeToSVOFile(const char* filename, OctreeElement* element = NULL); void writeToSVOFile(const char* filename, OctreeElement* element = NULL);
virtual bool writeToMap(QVariantMap& entityDescription, OctreeElement* element) = 0; virtual bool writeToMap(QVariantMap& entityDescription, OctreeElement* element) = 0;
// Octree importers
bool readFromFile(const char* filename); bool readFromFile(const char* filename);
bool readFromURL(const QString& url); // will support file urls as well... bool readFromURL(const QString& url); // will support file urls as well...
bool readFromStream(unsigned long streamLength, QDataStream& inputStream); bool readFromStream(unsigned long streamLength, QDataStream& inputStream);
bool readSVOFromStream(unsigned long streamLength, QDataStream& inputStream); bool readSVOFromStream(unsigned long streamLength, QDataStream& inputStream);
bool readJSONFromStream(unsigned long streamLength, QDataStream& inputStream); bool readJSONFromStream(unsigned long streamLength, QDataStream& inputStream);
virtual bool readFromMap(QVariantMap& entityDescription) = 0;
unsigned long getOctreeElementsCount(); unsigned long getOctreeElementsCount();
@ -373,9 +370,6 @@ public:
virtual void dumpTree() { }; virtual void dumpTree() { };
virtual void pruneTree() { }; virtual void pruneTree() { };
virtual bool readFromMap(QVariantMap& entityDescription) = 0;
signals: signals:
void importSize(float x, float y, float z); void importSize(float x, float y, float z);
void importProgress(int progress); void importProgress(int progress);

View file

@ -49,10 +49,6 @@ public:
}; };
class OctreeElement { class OctreeElement {
protected: protected:

View file

@ -31,7 +31,7 @@ const int OctreePersistThread::DEFAULT_PERSIST_INTERVAL = 1000 * 30; // every 30
OctreePersistThread::OctreePersistThread(Octree* tree, const QString& filename, int persistInterval, OctreePersistThread::OctreePersistThread(Octree* tree, const QString& filename, int persistInterval,
bool wantBackup, const QJsonObject& settings, bool debugTimestampNow, bool wantBackup, const QJsonObject& settings, bool debugTimestampNow,
bool persistAsJson) : QString persistAsFileType) :
_tree(tree), _tree(tree),
_filename(filename), _filename(filename),
_persistInterval(persistInterval), _persistInterval(persistInterval),
@ -41,17 +41,13 @@ OctreePersistThread::OctreePersistThread(Octree* tree, const QString& filename,
_wantBackup(wantBackup), _wantBackup(wantBackup),
_debugTimestampNow(debugTimestampNow), _debugTimestampNow(debugTimestampNow),
_lastTimeDebug(0), _lastTimeDebug(0),
_persistAsJson(persistAsJson) _persistAsFileType(persistAsFileType)
{ {
parseSettings(settings); parseSettings(settings);
// in case the persist filename has an extension that doesn't match the file type
QString sansExt = fileNameWithoutExtension(_filename, persistExtensions); QString sansExt = fileNameWithoutExtension(_filename, persistExtensions);
if (_persistAsJson) { _filename = sansExt + "." + _persistAsFileType;
_filename = sansExt + "." + "json";
} else {
_filename = sansExt + "." + "svo";
}
} }
void OctreePersistThread::parseSettings(const QJsonObject& settings) { void OctreePersistThread::parseSettings(const QJsonObject& settings) {
@ -253,7 +249,7 @@ void OctreePersistThread::persist() {
if(lockFile.is_open()) { if(lockFile.is_open()) {
qDebug() << "saving Octree lock file created at:" << lockFileName; qDebug() << "saving Octree lock file created at:" << lockFileName;
_tree->writeToFile(qPrintable(_filename), NULL, _persistAsJson); _tree->writeToFile(qPrintable(_filename), NULL, _persistAsFileType);
time(&_lastPersistTime); time(&_lastPersistTime);
_tree->clearDirtyBit(); // tree is clean after saving _tree->clearDirtyBit(); // tree is clean after saving
qDebug() << "DONE saving Octree to file..."; qDebug() << "DONE saving Octree to file...";

View file

@ -35,7 +35,7 @@ public:
OctreePersistThread(Octree* tree, const QString& filename, int persistInterval = DEFAULT_PERSIST_INTERVAL, OctreePersistThread(Octree* tree, const QString& filename, int persistInterval = DEFAULT_PERSIST_INTERVAL,
bool wantBackup = false, const QJsonObject& settings = QJsonObject(), bool wantBackup = false, const QJsonObject& settings = QJsonObject(),
bool debugTimestampNow = false, bool persistAsJson=false); bool debugTimestampNow = false, QString persistAsFileType="svo");
bool isInitialLoadComplete() const { return _initialLoadComplete; } bool isInitialLoadComplete() const { return _initialLoadComplete; }
quint64 getLoadElapsedTime() const { return _loadTimeUSecs; } quint64 getLoadElapsedTime() const { return _loadTimeUSecs; }
@ -73,7 +73,7 @@ private:
bool _debugTimestampNow; bool _debugTimestampNow;
quint64 _lastTimeDebug; quint64 _lastTimeDebug;
bool _persistAsJson; QString _persistAsFileType;
}; };
#endif // hifi_OctreePersistThread_h #endif // hifi_OctreePersistThread_h