Add RawEntityData subclass

This commit is contained in:
Ryan Huffman 2018-02-26 16:24:57 -08:00
parent 975bfa6809
commit 3fa538d42d
10 changed files with 70 additions and 35 deletions

View file

@ -1157,7 +1157,7 @@ void OctreeServer::domainSettingsRequestComplete() {
OctreeUtils::RawOctreeData data;
qCDebug(octree_server) << "Reading octree data from" << _persistAbsoluteFilePath;
if (OctreeUtils::readOctreeDataInfoFromFile(_persistAbsoluteFilePath, &data)) {
if (data.readOctreeDataInfoFromFile(_persistAbsoluteFilePath)) {
qCDebug(octree_server) << "Current octree data: ID(" << data.id << ") DataVersion(" << data.version << ")";
packet->writePrimitive(true);
auto id = data.id.toRfc4122();
@ -1189,7 +1189,7 @@ void OctreeServer::handleOctreeDataFileReply(QSharedPointer<ReceivedMessage> mes
OctreeUtils::RawOctreeData data;
qCDebug(octree_server) << "Reading octree data from" << _persistAbsoluteFilePath;
if (OctreeUtils::readOctreeDataInfoFromFile(_persistAbsoluteFilePath, &data)) {
if (data.readOctreeDataInfoFromFile(_persistAbsoluteFilePath)) {
if (data.id.isNull()) {
qCDebug(octree_server) << "Current octree data has a null id, updating";
data.resetIdAndVersion();

View file

@ -39,6 +39,7 @@
#include "DomainServer.h"
const std::chrono::seconds DomainContentBackupManager::DEFAULT_PERSIST_INTERVAL { 30 };
// Backup format looks like: daily_backup-TIMESTAMP.zip
static const QString DATETIME_FORMAT { "yyyy-MM-dd_HH-mm-ss" };
static const QString DATETIME_FORMAT_RE { "\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}" };

View file

@ -1745,9 +1745,9 @@ void DomainServer::processOctreeDataPersistMessage(QSharedPointer<ReceivedMessag
QFile f(filePath);
if (f.open(QIODevice::WriteOnly)) {
f.write(data);
OctreeUtils::RawOctreeData octreeData;
if (OctreeUtils::readOctreeDataInfoFromData(data, &octreeData)) {
qCDebug(domain_server) << "Wrote new entities file" << octreeData.id << octreeData.version;
OctreeUtils::RawEntityData entityData;
if (entityData.readOctreeDataInfoFromData(data)) {
qCDebug(domain_server) << "Wrote new entities file" << entityData.id << entityData.version;
} else {
qCDebug(domain_server) << "Failed to read new octree data info";
}
@ -1793,8 +1793,8 @@ void DomainServer::processOctreeDataRequestMessage(QSharedPointer<ReceivedMessag
auto entityFilePath = getEntitiesFilePath();
auto reply = NLPacketList::create(PacketType::OctreeDataFileReply, QByteArray(), true, true);
OctreeUtils::RawOctreeData data;
if (OctreeUtils::readOctreeDataInfoFromFile(entityFilePath, &data)) {
OctreeUtils::RawEntityData data;
if (data.readOctreeDataInfoFromFile(entityFilePath)) {
if (data.id == id && data.version <= version) {
qCDebug(domain_server) << "ES has sufficient octree data, not sending data";
reply->writePrimitive(false);
@ -3344,8 +3344,8 @@ void DomainServer::setupGroupCacheRefresh() {
void DomainServer::maybeHandleReplacementEntityFile() {
const auto replacementFilePath = getEntitiesReplacementFilePath();
OctreeUtils::RawOctreeData data;
if (!OctreeUtils::readOctreeDataInfoFromFile(replacementFilePath, &data)) {
OctreeUtils::RawEntityData data;
if (!data.readOctreeDataInfoFromFile(replacementFilePath)) {
qCWarning(domain_server) << "Replacement file could not be read, it either doesn't exist or is invalid.";
} else {
qCDebug(domain_server) << "Replacing existing entity date with replacement file";
@ -3381,8 +3381,8 @@ void DomainServer::handleOctreeFileReplacement(QByteArray octreeFile) {
jsonOctree = compressedOctree;
}
OctreeUtils::RawOctreeData data;
if (OctreeUtils::readOctreeDataInfoFromData(jsonOctree, &data)) {
OctreeUtils::RawEntityData data;
if (data.readOctreeDataInfoFromData(jsonOctree)) {
data.resetIdAndVersion();
gzip(data.toByteArray(), compressedOctree);

View file

@ -62,8 +62,8 @@ void EntitiesBackupHandler::recoverBackup(const QString& backupName, QuaZip& zip
zipFile.close();
OctreeUtils::RawOctreeData data;
if (!OctreeUtils::readOctreeDataInfoFromData(rawData, &data)) {
OctreeUtils::RawEntityData data;
if (!data.readOctreeDataInfoFromData(rawData)) {
qCritical() << "Unable to parse octree data during backup recovery";
return;
}

View file

@ -115,6 +115,8 @@ public:
virtual void update() override { update(true); }
std::unique_ptr<OctreeUtils::RawOctreeData> getRawOctreeData();
void update(bool simulate);
// The newer API...

View file

@ -328,7 +328,7 @@ void Socket::checkForReadyReadBackup() {
void Socket::readPendingDatagrams() {
int packetSizeWithHeader = -1;
while ((packetSizeWithHeader = _udpSocket.pendingDatagramSize()) != -1) {
while ((packetSizeWithHeader = _udpSocket.pendingDatagramSize()) > 0) {
// we're reading a packet so re-start the readyRead backup timer
_readyReadBackupTimer->start();

View file

@ -28,6 +28,7 @@
#include "OctreeElementBag.h"
#include "OctreePacketData.h"
#include "OctreeSceneStats.h"
#include "OctreeUtils.h"
class ReadBitstreamToTreeParams;
class Octree;
@ -328,7 +329,7 @@ public:
virtual void dumpTree() { }
virtual void pruneTree() { }
void setEntityVersionInfo(QUuid id, int64_t dataVersion) {
void setOctreeVersionInfo(QUuid id, int64_t dataVersion) {
_persistID = id;
_persistDataVersion = dataVersion;
}

View file

@ -176,9 +176,9 @@ bool OctreePersistThread::process() {
}
OctreeUtils::RawOctreeData data;
if (OctreeUtils::readOctreeDataInfoFromFile(_filename, &data)) {
if (data.readOctreeDataInfoFromFile(_filename)) {
qDebug() << "Setting entity version info to: " << data.id << data.version;
_tree->setEntityVersionInfo(data.id, data.version);
_tree->setOctreeVersionInfo(data.id, data.version);
}
bool persistentFileRead;

View file

@ -84,7 +84,7 @@ float getOrthographicAccuracySize(float octreeSizeScale, int boundaryLevelAdjust
// Reads octree file and parses it into a QJsonDocument. Handles both gzipped and non-gzipped files.
// Returns true if the file was successfully opened and parsed, otherwise false.
// Example failures: file does not exist, gzipped file cannot be unzipped, invalid JSON.
bool OctreeUtils::readOctreeFile(QString path, QJsonDocument* doc) {
bool readOctreeFile(QString path, QJsonDocument* doc) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Cannot open json file for reading: " << path;
@ -102,18 +102,16 @@ bool OctreeUtils::readOctreeFile(QString path, QJsonDocument* doc) {
return !doc->isNull();
}
bool readOctreeDataInfoFromJSON(QJsonObject root, OctreeUtils::RawOctreeData* octreeData) {
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromJSON(QJsonObject root) {
if (root.contains("Id") && root.contains("DataVersion")) {
octreeData->id = root["Id"].toVariant().toUuid();
octreeData->version = root["DataVersion"].toInt();
}
if (root.contains("Entities")) {
octreeData->octreeData = root["Entities"].toArray();
id = root["Id"].toVariant().toUuid();
version = root["DataVersion"].toInt();
}
readSubclassData(root);
return true;
}
bool OctreeUtils::readOctreeDataInfoFromData(QByteArray data, OctreeUtils::RawOctreeData* octreeData) {
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromData(QByteArray data) {
QByteArray jsonData;
if (gunzip(data, jsonData)) {
data = jsonData;
@ -125,30 +123,31 @@ bool OctreeUtils::readOctreeDataInfoFromData(QByteArray data, OctreeUtils::RawOc
}
auto root = doc.object();
return readOctreeDataInfoFromJSON(root, octreeData);
return readOctreeDataInfoFromJSON(root);
}
// Reads octree file and parses it into a RawOctreeData object.
// Returns false if readOctreeFile fails.
bool OctreeUtils::readOctreeDataInfoFromFile(QString path, OctreeUtils::RawOctreeData* octreeData) {
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromFile(QString path) {
QJsonDocument doc;
if (!OctreeUtils::readOctreeFile(path, &doc)) {
if (!readOctreeFile(path, &doc)) {
return false;
}
auto root = doc.object();
return readOctreeDataInfoFromJSON(root, octreeData);
return readOctreeDataInfoFromJSON(root);
}
QByteArray OctreeUtils::RawOctreeData::toByteArray() {
const auto protocolVersion = (int)versionForPacketType(PacketType::EntityData);
const auto protocolVersion = (int)versionForPacketType(dataPacketType());
QJsonObject obj {
{ "DataVersion", QJsonValue((qint64)version) },
{ "Id", QJsonValue(id.toString()) },
{ "Version", protocolVersion },
{ "Entities", octreeData }
};
writeSubclassData(obj);
QJsonDocument doc;
doc.setObject(obj);
@ -167,8 +166,24 @@ QByteArray OctreeUtils::RawOctreeData::toGzippedByteArray() {
return gzData;
}
PacketType OctreeUtils::RawOctreeData::dataPacketType() const {
Q_ASSERT(false);
qCritical() << "Attemping to read packet type for incomplete base type 'RawOctreeData'";
return (PacketType)0;
}
void OctreeUtils::RawOctreeData::resetIdAndVersion() {
id = QUuid::createUuid();
version = OctreeUtils::INITIAL_VERSION;
qDebug() << "Reset octree data to: " << id << version;
}
void OctreeUtils::RawEntityData::readSubclassData(QJsonObject root) {
if (root.contains("Entities")) {
entityData = root["Entities"].toArray();
}
}
void OctreeUtils::RawEntityData::writeSubclassData(QJsonObject root) const {
root["Entities"] = entityData;
}

View file

@ -17,6 +17,10 @@
#include <QUuid>
#include <QJsonArray>
#include <udt/PacketHeaders.h>
#include <QJsonObject>
class AABox;
class QJsonDocument;
@ -31,19 +35,31 @@ public:
QUuid id { QUuid() };
Version version { -1 };
QJsonArray octreeData;
virtual PacketType dataPacketType() const;
virtual void readSubclassData(QJsonObject root) { }
virtual void writeSubclassData(QJsonObject root) const { }
void resetIdAndVersion();
QByteArray toByteArray();
QByteArray toGzippedByteArray();
bool readOctreeDataInfoFromData(QByteArray data);
bool readOctreeDataInfoFromFile(QString path);
bool readOctreeDataInfoFromJSON(QJsonObject root);
};
bool readOctreeFile(QString path, QJsonDocument* doc);
bool readOctreeDataInfoFromData(QByteArray data, RawOctreeData* octreeData);
bool readOctreeDataInfoFromFile(QString path, RawOctreeData* octreeData);
class RawEntityData : public RawOctreeData {
PacketType dataPacketType() const override { return PacketType::EntityData; }
void readSubclassData(QJsonObject root) override;
void writeSubclassData(QJsonObject root) const override;
QJsonArray entityData;
};
}
/// renderAccuracy represents a floating point "visibility" of an object based on it's view from the camera. At a simple
/// level it returns 0.0f for things that are so small for the current settings that they could not be visible.
float calculateRenderAccuracy(const glm::vec3& position,