mirror of
https://github.com/overte-org/overte.git
synced 2025-08-04 04:03:35 +02:00
move blob conversion recipes to EntityItemProperties
This commit is contained in:
parent
855497e604
commit
63ed0a3a98
4 changed files with 42 additions and 41 deletions
|
@ -1460,38 +1460,6 @@ void MyAvatar::clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFrom
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool blobToProperties(QScriptEngine& scriptEngine, const QByteArray& blob, EntityItemProperties& properties) {
|
|
||||||
// begin recipe for converting unfortunately-formatted-binary-blob to EntityItemProperties
|
|
||||||
QJsonDocument jsonProperties = QJsonDocument::fromBinaryData(blob);
|
|
||||||
if (!jsonProperties.isObject()) {
|
|
||||||
qCDebug(interfaceapp) << "bad avatarEntityData json" << QString(blob.toHex());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
QVariant variant = jsonProperties.toVariant();
|
|
||||||
QVariantMap variantMap = variant.toMap();
|
|
||||||
QScriptValue scriptValue = variantMapToScriptValue(variantMap, scriptEngine);
|
|
||||||
EntityItemPropertiesFromScriptValueHonorReadOnly(scriptValue, properties);
|
|
||||||
// end recipe
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void propertiesToBlob(QScriptEngine& scriptEngine, const QUuid& myAvatarID, const EntityItemProperties& properties, QByteArray& blob) {
|
|
||||||
// begin recipe for extracting unfortunately-formatted-binary-blob from EntityItem
|
|
||||||
QScriptValue scriptValue = EntityItemNonDefaultPropertiesToScriptValue(&scriptEngine, properties);
|
|
||||||
QVariant variantProperties = scriptValue.toVariant();
|
|
||||||
QJsonDocument jsonProperties = QJsonDocument::fromVariant(variantProperties);
|
|
||||||
// the ID of the parent/avatar changes from session to session. use a special UUID to indicate the avatar
|
|
||||||
QJsonObject jsonObject = jsonProperties.object();
|
|
||||||
if (jsonObject.contains("parentID")) {
|
|
||||||
if (QUuid(jsonObject["parentID"].toString()) == myAvatarID) {
|
|
||||||
jsonObject["parentID"] = AVATAR_SELF_ID.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
jsonProperties = QJsonDocument(jsonObject);
|
|
||||||
blob = jsonProperties.toBinaryData();
|
|
||||||
// end recipe
|
|
||||||
}
|
|
||||||
|
|
||||||
void MyAvatar::sanitizeAvatarEntityProperties(EntityItemProperties& properties) const {
|
void MyAvatar::sanitizeAvatarEntityProperties(EntityItemProperties& properties) const {
|
||||||
properties.setEntityHostType(entity::HostType::AVATAR);
|
properties.setEntityHostType(entity::HostType::AVATAR);
|
||||||
properties.setOwningAvatarID(getID());
|
properties.setOwningAvatarID(getID());
|
||||||
|
@ -1605,7 +1573,7 @@ void MyAvatar::updateAvatarEntities() {
|
||||||
blobFailed = true; // blob doesn't exist
|
blobFailed = true; // blob doesn't exist
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!blobToProperties(*_myScriptEngine, itr.value(), properties)) {
|
if (!EntityItemProperties::blobToProperties(*_myScriptEngine, itr.value(), properties)) {
|
||||||
blobFailed = true; // blob is corrupt
|
blobFailed = true; // blob is corrupt
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1634,7 +1602,7 @@ void MyAvatar::updateAvatarEntities() {
|
||||||
skip = true;
|
skip = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!blobToProperties(*_myScriptEngine, itr.value(), properties)) {
|
if (!EntityItemProperties::blobToProperties(*_myScriptEngine, itr.value(), properties)) {
|
||||||
skip = true;
|
skip = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1740,7 +1708,7 @@ bool MyAvatar::updateStaleAvatarEntityBlobs() const {
|
||||||
if (found) {
|
if (found) {
|
||||||
++numFound;
|
++numFound;
|
||||||
QByteArray blob;
|
QByteArray blob;
|
||||||
propertiesToBlob(*_myScriptEngine, getID(), properties, blob);
|
EntityItemProperties::propertiesToBlob(*_myScriptEngine, getID(), properties, blob);
|
||||||
_avatarEntitiesLock.withWriteLock([&] {
|
_avatarEntitiesLock.withWriteLock([&] {
|
||||||
_cachedAvatarEntityBlobs[id] = blob;
|
_cachedAvatarEntityBlobs[id] = blob;
|
||||||
});
|
});
|
||||||
|
@ -1767,7 +1735,7 @@ AvatarEntityMap MyAvatar::getAvatarEntityData() const {
|
||||||
|
|
||||||
void MyAvatar::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) {
|
void MyAvatar::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) {
|
||||||
// Note: this is an invokable Script call
|
// Note: this is an invokable Script call
|
||||||
// The argument is expected to be a map of QByteArrays that represent EntityItemProperties objects from JavaScript,
|
// avatarEntityData is expected to be a map of QByteArrays that represent EntityItemProperties objects from JavaScript,
|
||||||
// aka: unfortunately-formatted-binary-blobs because we store them in non-human-readable format in Settings.
|
// aka: unfortunately-formatted-binary-blobs because we store them in non-human-readable format in Settings.
|
||||||
//
|
//
|
||||||
if (avatarEntityData.size() > MAX_NUM_AVATAR_ENTITIES) {
|
if (avatarEntityData.size() > MAX_NUM_AVATAR_ENTITIES) {
|
||||||
|
@ -1788,13 +1756,10 @@ void MyAvatar::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) {
|
||||||
_avatarEntitiesLock.withWriteLock([&] {
|
_avatarEntitiesLock.withWriteLock([&] {
|
||||||
// find new and updated IDs
|
// find new and updated IDs
|
||||||
AvatarEntityMap::const_iterator constItr = avatarEntityData.begin();
|
AvatarEntityMap::const_iterator constItr = avatarEntityData.begin();
|
||||||
std::vector<QUuid> blobsToCache;
|
|
||||||
blobsToCache.reserve(avatarEntityData.size());
|
|
||||||
while (constItr != avatarEntityData.end()) {
|
while (constItr != avatarEntityData.end()) {
|
||||||
QUuid id = constItr.key();
|
QUuid id = constItr.key();
|
||||||
if (_cachedAvatarEntityBlobs.find(id) == _cachedAvatarEntityBlobs.end()) {
|
if (_cachedAvatarEntityBlobs.find(id) == _cachedAvatarEntityBlobs.end()) {
|
||||||
_entitiesToAdd.push_back(id);
|
_entitiesToAdd.push_back(id);
|
||||||
blobsToCache.push_back(id);
|
|
||||||
} else {
|
} else {
|
||||||
_entitiesToUpdate.push_back(id);
|
_entitiesToUpdate.push_back(id);
|
||||||
}
|
}
|
||||||
|
@ -1812,7 +1777,7 @@ void MyAvatar::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) {
|
||||||
++itr;
|
++itr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// now that we've 'deleted' unknown ids, copy over the new ones
|
// copy new data
|
||||||
constItr = avatarEntityData.begin();
|
constItr = avatarEntityData.begin();
|
||||||
while (constItr != avatarEntityData.end()) {
|
while (constItr != avatarEntityData.end()) {
|
||||||
_cachedAvatarEntityBlobs.insert(constItr.key(), constItr.value());
|
_cachedAvatarEntityBlobs.insert(constItr.key(), constItr.value());
|
||||||
|
|
|
@ -2794,7 +2794,8 @@ AvatarEntityMap AvatarData::getAvatarEntityData() const {
|
||||||
|
|
||||||
void AvatarData::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) {
|
void AvatarData::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) {
|
||||||
// overridden where needed
|
// overridden where needed
|
||||||
// NOTE: the argument is expected to be a map of unfortunately-formatted-binary-blobs
|
// avatarEntityData is expected to be a map of QByteArrays
|
||||||
|
// each QByteArray represents an EntityItemProperties object from JavaScript
|
||||||
}
|
}
|
||||||
|
|
||||||
void AvatarData::insertDetachedEntityID(const QUuid entityID) {
|
void AvatarData::insertDetachedEntityID(const QUuid entityID) {
|
||||||
|
|
|
@ -4612,6 +4612,38 @@ void EntityItemProperties::convertToCloneProperties(const EntityItemID& entityID
|
||||||
setCloneAvatarEntity(ENTITY_ITEM_DEFAULT_CLONE_AVATAR_ENTITY);
|
setCloneAvatarEntity(ENTITY_ITEM_DEFAULT_CLONE_AVATAR_ENTITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool EntityItemProperties::blobToProperties(QScriptEngine& scriptEngine, const QByteArray& blob, EntityItemProperties& properties) {
|
||||||
|
// begin recipe for converting unfortunately-formatted-binary-blob to EntityItemProperties
|
||||||
|
QJsonDocument jsonProperties = QJsonDocument::fromBinaryData(blob);
|
||||||
|
if (!jsonProperties.isObject()) {
|
||||||
|
qCDebug(entities) << "bad avatarEntityData json" << QString(blob.toHex());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
QVariant variant = jsonProperties.toVariant();
|
||||||
|
QVariantMap variantMap = variant.toMap();
|
||||||
|
QScriptValue scriptValue = variantMapToScriptValue(variantMap, scriptEngine);
|
||||||
|
EntityItemPropertiesFromScriptValueHonorReadOnly(scriptValue, properties);
|
||||||
|
// end recipe
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntityItemProperties::propertiesToBlob(QScriptEngine& scriptEngine, const QUuid& myAvatarID, const EntityItemProperties& properties, QByteArray& blob) {
|
||||||
|
// begin recipe for extracting unfortunately-formatted-binary-blob from EntityItem
|
||||||
|
QScriptValue scriptValue = EntityItemNonDefaultPropertiesToScriptValue(&scriptEngine, properties);
|
||||||
|
QVariant variantProperties = scriptValue.toVariant();
|
||||||
|
QJsonDocument jsonProperties = QJsonDocument::fromVariant(variantProperties);
|
||||||
|
// the ID of the parent/avatar changes from session to session. use a special UUID to indicate the avatar
|
||||||
|
QJsonObject jsonObject = jsonProperties.object();
|
||||||
|
if (jsonObject.contains("parentID")) {
|
||||||
|
if (QUuid(jsonObject["parentID"].toString()) == myAvatarID) {
|
||||||
|
jsonObject["parentID"] = AVATAR_SELF_ID.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jsonProperties = QJsonDocument(jsonObject);
|
||||||
|
blob = jsonProperties.toBinaryData();
|
||||||
|
// end recipe
|
||||||
|
}
|
||||||
|
|
||||||
QDebug& operator<<(QDebug& dbg, const EntityPropertyFlags& f) {
|
QDebug& operator<<(QDebug& dbg, const EntityPropertyFlags& f) {
|
||||||
QString result = "[ ";
|
QString result = "[ ";
|
||||||
|
|
||||||
|
|
|
@ -98,6 +98,9 @@ class EntityItemProperties {
|
||||||
friend class ZoneEntityItem;
|
friend class ZoneEntityItem;
|
||||||
friend class MaterialEntityItem;
|
friend class MaterialEntityItem;
|
||||||
public:
|
public:
|
||||||
|
static bool blobToProperties(QScriptEngine& scriptEngine, const QByteArray& blob, EntityItemProperties& properties);
|
||||||
|
static void propertiesToBlob(QScriptEngine& scriptEngine, const QUuid& myAvatarID, const EntityItemProperties& properties, QByteArray& blob);
|
||||||
|
|
||||||
EntityItemProperties(EntityPropertyFlags desiredProperties = EntityPropertyFlags());
|
EntityItemProperties(EntityPropertyFlags desiredProperties = EntityPropertyFlags());
|
||||||
virtual ~EntityItemProperties() = default;
|
virtual ~EntityItemProperties() = default;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue