diff --git a/assignment-client/src/avatars/AvatarMixerClientData.cpp b/assignment-client/src/avatars/AvatarMixerClientData.cpp index 0caa7a0d9e..987cb7aa2f 100644 --- a/assignment-client/src/avatars/AvatarMixerClientData.cpp +++ b/assignment-client/src/avatars/AvatarMixerClientData.cpp @@ -157,7 +157,7 @@ void AvatarMixerClientData::processSetTraitsMessage(ReceivedMessage& message, if (traitSize == AvatarTraits::DELETED_TRAIT_SIZE) { _avatar->processDeletedTraitInstance(traitType, instanceID); // Mixer doesn't need deleted IDs. - _avatar->getAndClearRecentlyDetachedIDs(); + _avatar->getAndClearRecentlyRemovedIDs(); // to track a deleted instance but keep version information // the avatar mixer uses the negative value of the sent version diff --git a/assignment-client/src/avatars/ScriptableAvatar.cpp b/assignment-client/src/avatars/ScriptableAvatar.cpp index a2e13a03be..c61e41fbbe 100644 --- a/assignment-client/src/avatars/ScriptableAvatar.cpp +++ b/assignment-client/src/avatars/ScriptableAvatar.cpp @@ -21,6 +21,8 @@ #include #include #include +#include +#include ScriptableAvatar::ScriptableAvatar() { @@ -249,3 +251,157 @@ void ScriptableAvatar::setHasProceduralEyeFaceMovement(bool hasProceduralEyeFace void ScriptableAvatar::setHasAudioEnabledFaceMovement(bool hasAudioEnabledFaceMovement) { _headData->setHasAudioEnabledFaceMovement(hasAudioEnabledFaceMovement); } + +AvatarEntityMap ScriptableAvatar::getAvatarEntityData() const { + // DANGER: Now that we store the AvatarEntityData in packed format this call is potentially Very Expensive! + // Avoid calling this method if possible. + AvatarEntityMap data; + QUuid sessionID = getID(); + _avatarEntitiesLock.withReadLock([&] { + for (const auto& itr : _entities) { + QUuid id = itr.first; + EntityItemPointer entity = itr.second; + EntityItemProperties properties = entity->getProperties(); + QByteArray blob; + EntityItemProperties::propertiesToBlob(_scriptEngine, sessionID, properties, blob); + data[id] = blob; + } + }); + return data; +} + +void ScriptableAvatar::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) { + // Note: this is an invokable Script call + // avatarEntityData is expected to be a map of QByteArrays that represent EntityItemProperties objects from JavaScript + // + if (avatarEntityData.size() > MAX_NUM_AVATAR_ENTITIES) { + // the data is suspect + qCDebug(avatars) << "discard suspect avatarEntityData with size =" << avatarEntityData.size(); + return; + } + + // convert binary data to EntityItemProperties + // NOTE: this operation is NOT efficient + std::map newProperties; + AvatarEntityMap::const_iterator dataItr = avatarEntityData.begin(); + while (dataItr != avatarEntityData.end()) { + EntityItemProperties properties; + const QByteArray& blob = dataItr.value(); + if (!blob.isNull() && EntityItemProperties::blobToProperties(_scriptEngine, blob, properties)) { + newProperties[dataItr.key()] = properties; + } + ++dataItr; + } + + // delete existing entities not found in avatarEntityData + std::vector idsToClear; + _avatarEntitiesLock.withWriteLock([&] { + std::map::iterator entityItr = _entities.begin(); + while (entityItr != _entities.end()) { + QUuid id = entityItr->first; + std::map::const_iterator propertiesItr = newProperties.find(id); + if (propertiesItr == newProperties.end()) { + idsToClear.push_back(id); + entityItr = _entities.erase(entityItr); + } else { + ++entityItr; + } + } + }); + + // add or update entities + _avatarEntitiesLock.withWriteLock([&] { + std::map::const_iterator propertiesItr = newProperties.begin(); + while (propertiesItr != newProperties.end()) { + QUuid id = propertiesItr->first; + const EntityItemProperties& properties = propertiesItr->second; + std::map::iterator entityItr = _entities.find(id); + EntityItemPointer entity; + if (entityItr != _entities.end()) { + entity = entityItr->second; + entity->setProperties(properties); + } else { + entity = EntityTypes::constructEntityItem(id, properties); + } + if (entity) { + // build outgoing payload + OctreePacketData packetData(false, AvatarTraits::MAXIMUM_TRAIT_SIZE); + EncodeBitstreamParams params; + EntityTreeElementExtraEncodeDataPointer extra { nullptr }; + OctreeElement::AppendState appendState = entity->appendEntityData(&packetData, params, extra); + + if (appendState == OctreeElement::COMPLETED) { + _entities[id] = entity; + QByteArray tempArray((const char*)packetData.getUncompressedData(), packetData.getUncompressedSize()); + storeAvatarEntityDataPayload(id, tempArray); + } else { + // payload doesn't fit + entityItr = _entities.find(id); + if (entityItr != _entities.end()) { + _entities.erase(entityItr); + idsToClear.push_back(id); + } + + } + } + ++propertiesItr; + } + }); + + // clear deleted traits + for (const auto& id : idsToClear) { + clearAvatarEntity(id); + } +} + +void ScriptableAvatar::updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData) { + if (entityData.isNull()) { + // interpret this as a DELETE + std::map::iterator itr = _entities.find(entityID); + if (itr != _entities.end()) { + _entities.erase(itr); + clearAvatarEntity(entityID); + } + return; + } + + EntityItemPointer entity; + EntityItemProperties properties; + if (!EntityItemProperties::blobToProperties(_scriptEngine, entityData, properties)) { + // entityData is corrupt + return; + } + + std::map::iterator itr = _entities.find(entityID); + if (itr == _entities.end()) { + // this is an ADD + entity = EntityTypes::constructEntityItem(entityID, properties); + if (entity) { + OctreePacketData packetData(false, AvatarTraits::MAXIMUM_TRAIT_SIZE); + EncodeBitstreamParams params; + EntityTreeElementExtraEncodeDataPointer extra { nullptr }; + OctreeElement::AppendState appendState = entity->appendEntityData(&packetData, params, extra); + + if (appendState == OctreeElement::COMPLETED) { + _entities[entityID] = entity; + QByteArray tempArray((const char*)packetData.getUncompressedData(), packetData.getUncompressedSize()); + storeAvatarEntityDataPayload(entityID, tempArray); + } + } + } else { + // this is an UPDATE + entity = itr->second; + bool somethingChanged = entity->setProperties(properties); + if (somethingChanged) { + OctreePacketData packetData(false, AvatarTraits::MAXIMUM_TRAIT_SIZE); + EncodeBitstreamParams params; + EntityTreeElementExtraEncodeDataPointer extra { nullptr }; + OctreeElement::AppendState appendState = entity->appendEntityData(&packetData, params, extra); + + if (appendState == OctreeElement::COMPLETED) { + QByteArray tempArray((const char*)packetData.getUncompressedData(), packetData.getUncompressedSize()); + storeAvatarEntityDataPayload(entityID, tempArray); + } + } + } +} diff --git a/assignment-client/src/avatars/ScriptableAvatar.h b/assignment-client/src/avatars/ScriptableAvatar.h index 66b0b5ae3f..df949f8bff 100644 --- a/assignment-client/src/avatars/ScriptableAvatar.h +++ b/assignment-client/src/avatars/ScriptableAvatar.h @@ -16,6 +16,7 @@ #include #include #include +#include /**jsdoc * The Avatar API is used to manipulate scriptable avatars on the domain. This API is a subset of the @@ -185,6 +186,26 @@ public: void setHasAudioEnabledFaceMovement(bool hasAudioEnabledFaceMovement); bool getHasAudioEnabledFaceMovement() const override { return _headData->getHasAudioEnabledFaceMovement(); } + /**jsdoc + * Potentially Very Expensive. Do not use. + * @function Avatar.getAvatarEntityData + * @returns {object} + */ + Q_INVOKABLE AvatarEntityMap getAvatarEntityData() const override; + + /**jsdoc + * @function MyAvatar.setAvatarEntityData + * @param {object} avatarEntityData + */ + Q_INVOKABLE void setAvatarEntityData(const AvatarEntityMap& avatarEntityData) override; + + /**jsdoc + * @function MyAvatar.updateAvatarEntity + * @param {Uuid} entityID + * @param {string} entityData + */ + Q_INVOKABLE void updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData) override; + public slots: void update(float deltatime); @@ -202,6 +223,8 @@ private: QHash _fstJointIndices; ///< 1-based, since zero is returned for missing keys QStringList _fstJointNames; ///< in order of depth-first traversal QUrl _skeletonFBXURL; + mutable QScriptEngine _scriptEngine; + std::map _entities; /// Loads the joint indices, names from the FST file (if any) void updateJointMappings(); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 134c375b56..5fc3bad8e5 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2459,6 +2459,9 @@ void Application::updateHeartbeat() const { } void Application::onAboutToQuit() { + // quickly save AvatarEntityData before the EntityTree is dismantled + getMyAvatar()->saveAvatarEntityDataToSettings(); + emit beforeAboutToQuit(); if (getLoginDialogPoppedUp() && _firstRun.get()) { @@ -6753,8 +6756,10 @@ void Application::updateWindowTitle() const { } void Application::clearDomainOctreeDetails() { + // before we delete all entities get MyAvatar's AvatarEntityData ready + getMyAvatar()->prepareAvatarEntityDataForReload(); - // if we're about to quit, we really don't need to do any of these things... + // if we're about to quit, we really don't need to do the rest of these things... if (_aboutToQuit) { return; } @@ -6782,8 +6787,6 @@ void Application::clearDomainOctreeDetails() { ShaderCache::instance().clearUnusedResources(); DependencyManager::get()->clearUnusedResources(); DependencyManager::get()->clearUnusedResources(); - - getMyAvatar()->setAvatarEntityDataChanged(true); } void Application::domainURLChanged(QUrl domainURL) { diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index e40fc7f9dd..91ea8f0291 100755 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -50,6 +50,7 @@ #include #include #include +#include #include "MyHead.h" #include "MySkeletonModel.h" @@ -281,6 +282,8 @@ MyAvatar::MyAvatar(QThread* thread) : MyAvatar::~MyAvatar() { _lookAtTargetAvatar.reset(); + delete _myScriptEngine; + _myScriptEngine = nullptr; } void MyAvatar::setDominantHand(const QString& hand) { @@ -671,7 +674,7 @@ void MyAvatar::update(float deltaTime) { _clientTraitsHandler->sendChangedTraitsToMixer(); - simulate(deltaTime); + simulate(deltaTime, true); currentEnergy += energyChargeRate; currentEnergy -= getAccelerationEnergy(); @@ -743,7 +746,7 @@ void MyAvatar::updateChildCauterization(SpatiallyNestablePointer object, bool ca } } -void MyAvatar::simulate(float deltaTime) { +void MyAvatar::simulate(float deltaTime, bool inView) { PerformanceTimer perfTimer("simulate"); animateScaleChanges(deltaTime); @@ -887,7 +890,7 @@ void MyAvatar::simulate(float deltaTime) { _characterController.setCollisionlessAllowed(collisionlessAllowed); } - updateAvatarEntities(); + handleChangedAvatarEntityData(); updateFadingStatus(); } @@ -1251,7 +1254,7 @@ void MyAvatar::saveAvatarUrl() { } } -void MyAvatar::resizeAvatarEntitySettingHandles(unsigned int avatarEntityIndex) { +void MyAvatar::resizeAvatarEntitySettingHandles(uint32_t maxIndex) { // The original Settings interface saved avatar-entity array data like this: // Avatar/avatarEntityData/size: 5 // Avatar/avatarEntityData/1/id: ... @@ -1261,14 +1264,15 @@ void MyAvatar::resizeAvatarEntitySettingHandles(unsigned int avatarEntityIndex) // Avatar/avatarEntityData/5/properties: ... // // Create Setting::Handles to mimic this. - - while (_avatarEntityIDSettings.size() <= avatarEntityIndex) { + uint32_t settingsIndex = (uint32_t)_avatarEntityIDSettings.size() + 1; + while (settingsIndex <= maxIndex) { Setting::Handle idHandle(QStringList() << AVATAR_SETTINGS_GROUP_NAME << "avatarEntityData" - << QString::number(avatarEntityIndex + 1) << "id", QUuid()); + << QString::number(settingsIndex) << "id", QUuid()); _avatarEntityIDSettings.push_back(idHandle); Setting::Handle dataHandle(QStringList() << AVATAR_SETTINGS_GROUP_NAME << "avatarEntityData" - << QString::number(avatarEntityIndex + 1) << "properties", QByteArray()); + << QString::number(settingsIndex) << "properties", QByteArray()); _avatarEntityDataSettings.push_back(dataHandle); + settingsIndex++; } } @@ -1298,26 +1302,54 @@ void MyAvatar::saveData() { _flyingHMDSetting.set(getFlyingHMDPref()); auto hmdInterface = DependencyManager::get(); - _avatarEntitiesLock.withReadLock([&] { - QList avatarEntityIDs = _avatarEntityData.keys(); - unsigned int avatarEntityCount = avatarEntityIDs.size(); - unsigned int previousAvatarEntityCount = _avatarEntityCountSetting.get(0); - resizeAvatarEntitySettingHandles(std::max(avatarEntityCount, previousAvatarEntityCount)); - _avatarEntityCountSetting.set(avatarEntityCount); + saveAvatarEntityDataToSettings(); +} - unsigned int avatarEntityIndex = 0; - for (auto entityID : avatarEntityIDs) { - _avatarEntityIDSettings[avatarEntityIndex].set(entityID); - _avatarEntityDataSettings[avatarEntityIndex].set(_avatarEntityData.value(entityID)); - avatarEntityIndex++; - } +void MyAvatar::saveAvatarEntityDataToSettings() { + if (!_needToSaveAvatarEntitySettings) { + return; + } + bool success = updateStaleAvatarEntityBlobs(); + if (!success) { + return; + } + _needToSaveAvatarEntitySettings = false; - // clean up any left-over (due to the list shrinking) slots - for (; avatarEntityIndex < previousAvatarEntityCount; avatarEntityIndex++) { - _avatarEntityIDSettings[avatarEntityIndex].remove(); - _avatarEntityDataSettings[avatarEntityIndex].remove(); + uint32_t numEntities = (uint32_t)_cachedAvatarEntityBlobs.size(); + uint32_t prevNumEntities = _avatarEntityCountSetting.get(0); + resizeAvatarEntitySettingHandles(std::max(numEntities, prevNumEntities)); + + // save new Settings + if (numEntities > 0) { + // save all unfortunately-formatted-binary-blobs to Settings + _avatarEntitiesLock.withWriteLock([&] { + uint32_t i = 0; + AvatarEntityMap::const_iterator itr = _cachedAvatarEntityBlobs.begin(); + while (itr != _cachedAvatarEntityBlobs.end()) { + _avatarEntityIDSettings[i].set(itr.key()); + _avatarEntityDataSettings[i].set(itr.value()); + ++itr; + ++i; + } + numEntities = i; + }); + } + _avatarEntityCountSetting.set(numEntities); + + // remove old Settings if any + if (numEntities < prevNumEntities) { + uint32_t numEntitiesToRemove = prevNumEntities - numEntities; + for (uint32_t i = 0; i < numEntitiesToRemove; ++i) { + if (_avatarEntityIDSettings.size() > numEntities) { + _avatarEntityIDSettings.back().remove(); + _avatarEntityIDSettings.pop_back(); + } + if (_avatarEntityDataSettings.size() > numEntities) { + _avatarEntityDataSettings.back().remove(); + _avatarEntityDataSettings.pop_back(); + } } - }); + } } float loadSetting(Settings& settings, const QString& name, float defaultValue) { @@ -1414,7 +1446,410 @@ void MyAvatar::setEnableInverseKinematics(bool isEnabled) { _skeletonModel->getRig().setEnableInverseKinematics(isEnabled); } +void MyAvatar::storeAvatarEntityDataPayload(const QUuid& entityID, const QByteArray& payload) { + AvatarData::storeAvatarEntityDataPayload(entityID, payload); + _avatarEntitiesLock.withWriteLock([&] { + _cachedAvatarEntityBlobsToAddOrUpdate.push_back(entityID); + }); +} + +void MyAvatar::clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFromTree) { + AvatarData::clearAvatarEntity(entityID, requiresRemovalFromTree); + _avatarEntitiesLock.withWriteLock([&] { + _cachedAvatarEntityBlobsToDelete.push_back(entityID); + }); +} + +void MyAvatar::sanitizeAvatarEntityProperties(EntityItemProperties& properties) const { + properties.setEntityHostType(entity::HostType::AVATAR); + properties.setOwningAvatarID(getID()); + + // there's no entity-server to tell us we're the simulation owner, so always set the + // simulationOwner to the owningAvatarID and a high priority. + properties.setSimulationOwner(getID(), AVATAR_ENTITY_SIMULATION_PRIORITY); + + if (properties.getParentID() == AVATAR_SELF_ID) { + properties.setParentID(getID()); + } + + // When grabbing avatar entities, they are parented to the joint moving them, then when un-grabbed + // they go back to the default parent (null uuid). When un-gripped, others saw the entity disappear. + // The thinking here is the local position was noticed as changing, but not the parentID (since it is now + // back to the default), and the entity flew off somewhere. Marking all changed definitely fixes this, + // and seems safe (per Seth). + properties.markAllChanged(); +} + +void MyAvatar::handleChangedAvatarEntityData() { + // NOTE: this is a per-frame update + if (getID().isNull() || + getID() == AVATAR_SELF_ID || + DependencyManager::get()->getSessionUUID() == QUuid()) { + // wait until MyAvatar and this Node gets an ID before doing this. Otherwise, various things go wrong: + // things get their parent fixed up from AVATAR_SELF_ID to a null uuid which means "no parent". + return; + } + if (_reloadAvatarEntityDataFromSettings) { + loadAvatarEntityDataFromSettings(); + } + + auto treeRenderer = DependencyManager::get(); + EntityTreePointer entityTree = treeRenderer ? treeRenderer->getTree() : nullptr; + if (!entityTree) { + return; + } + + // We collect changes to AvatarEntities and then handle them all in one spot per frame: handleChangedAvatarEntityData(). + // Basically this is a "transaction pattern" with an extra complication: these changes can come from two + // "directions" and the "authoritative source" of each direction is different, so we maintain two distinct sets + // of transaction lists: + // + // The _entitiesToDelete/Add/Update lists are for changes whose "authoritative sources" are already + // correctly stored in _cachedAvatarEntityBlobs. These come from loadAvatarEntityDataFromSettings() and + // setAvatarEntityData(). These changes need to be extracted from _cachedAvatarEntityBlobs and applied to + // real EntityItems. + // + // The _cachedAvatarEntityBlobsToDelete/Add/Update lists are for changes whose "authoritative sources" are + // already reflected in real EntityItems. These changes need to be propagated to _cachedAvatarEntityBlobs + // and eventually to Settings. + // + // The DELETEs also need to be propagated to the traits, which will eventually propagate to + // AvatarData::_packedAvatarEntityData via deeper logic. + + // move the lists to minimize lock time + std::vector cachedBlobsToDelete; + std::vector cachedBlobsToUpdate; + std::vector entitiesToDelete; + std::vector entitiesToAdd; + std::vector entitiesToUpdate; + _avatarEntitiesLock.withWriteLock([&] { + cachedBlobsToDelete = std::move(_cachedAvatarEntityBlobsToDelete); + cachedBlobsToUpdate = std::move(_cachedAvatarEntityBlobsToAddOrUpdate); + entitiesToDelete = std::move(_entitiesToDelete); + entitiesToAdd = std::move(_entitiesToAdd); + entitiesToUpdate = std::move(_entitiesToUpdate); + }); + + auto removeAllInstancesHelper = [] (const QUuid& id, std::vector& v) { + uint32_t i = 0; + while (i < v.size()) { + if (id == v[i]) { + v[i] = v.back(); + v.pop_back(); + } else { + ++i; + } + } + }; + + // remove delete-add and delete-update overlap + for (const auto& id : entitiesToDelete) { + removeAllInstancesHelper(id, cachedBlobsToUpdate); + removeAllInstancesHelper(id, entitiesToAdd); + removeAllInstancesHelper(id, entitiesToUpdate); + } + for (const auto& id : cachedBlobsToDelete) { + removeAllInstancesHelper(id, entitiesToUpdate); + removeAllInstancesHelper(id, cachedBlobsToUpdate); + } + for (const auto& id : entitiesToAdd) { + removeAllInstancesHelper(id, entitiesToUpdate); + } + + // DELETE real entities + for (const auto& id : entitiesToDelete) { + entityTree->withWriteLock([&] { + entityTree->deleteEntity(id); + }); + } + + // ADD real entities + EntityEditPacketSender* packetSender = qApp->getEntityEditPacketSender(); + for (const auto& id : entitiesToAdd) { + bool blobFailed = false; + EntityItemProperties properties; + _avatarEntitiesLock.withReadLock([&] { + AvatarEntityMap::iterator itr = _cachedAvatarEntityBlobs.find(id); + if (itr == _cachedAvatarEntityBlobs.end()) { + blobFailed = true; // blob doesn't exist + return; + } + if (!EntityItemProperties::blobToProperties(*_myScriptEngine, itr.value(), properties)) { + blobFailed = true; // blob is corrupt + } + }); + if (blobFailed) { + // remove from _cachedAvatarEntityBlobUpdatesToSkip just in case: + // avoids a resource leak when blob updates to be skipped are never actually skipped + // when the blob fails to result in a real EntityItem + _avatarEntitiesLock.withWriteLock([&] { + removeAllInstancesHelper(id, _cachedAvatarEntityBlobUpdatesToSkip); + }); + continue; + } + sanitizeAvatarEntityProperties(properties); + entityTree->withWriteLock([&] { + EntityItemPointer entity = entityTree->addEntity(id, properties); + if (entity) { + packetSender->queueEditEntityMessage(PacketType::EntityAdd, entityTree, id, properties); + } + }); + } + + // CHANGE real entities + for (const auto& id : entitiesToUpdate) { + EntityItemProperties properties; + bool skip = false; + _avatarEntitiesLock.withReadLock([&] { + AvatarEntityMap::iterator itr = _cachedAvatarEntityBlobs.find(id); + if (itr == _cachedAvatarEntityBlobs.end()) { + skip = true; + return; + } + if (!EntityItemProperties::blobToProperties(*_myScriptEngine, itr.value(), properties)) { + skip = true; + } + }); + sanitizeAvatarEntityProperties(properties); + entityTree->withWriteLock([&] { + entityTree->updateEntity(id, properties); + }); + } + + // DELETE cached blobs + _avatarEntitiesLock.withWriteLock([&] { + for (const auto& id : cachedBlobsToDelete) { + AvatarEntityMap::iterator itr = _cachedAvatarEntityBlobs.find(id); + // remove blob and remember to remove from settings + if (itr != _cachedAvatarEntityBlobs.end()) { + _cachedAvatarEntityBlobs.erase(itr); + _needToSaveAvatarEntitySettings = true; + } + // also remove from list of stale blobs to avoid failed entity lookup later + std::set::iterator blobItr = _staleCachedAvatarEntityBlobs.find(id); + if (blobItr != _staleCachedAvatarEntityBlobs.end()) { + _staleCachedAvatarEntityBlobs.erase(blobItr); + } + // also remove from _cachedAvatarEntityBlobUpdatesToSkip just in case: + // avoids a resource leak when things are deleted before they could be skipped + removeAllInstancesHelper(id, _cachedAvatarEntityBlobUpdatesToSkip); + } + }); + + // ADD/UPDATE cached blobs + for (const auto& id : cachedBlobsToUpdate) { + // computing the blobs is expensive and we want to avoid it when possible + // so we add these ids to _staleCachedAvatarEntityBlobs for later + // and only build the blobs when absolutely necessary + bool skip = false; + uint32_t i = 0; + _avatarEntitiesLock.withWriteLock([&] { + while (i < _cachedAvatarEntityBlobUpdatesToSkip.size()) { + if (id == _cachedAvatarEntityBlobUpdatesToSkip[i]) { + _cachedAvatarEntityBlobUpdatesToSkip[i] = _cachedAvatarEntityBlobUpdatesToSkip.back(); + _cachedAvatarEntityBlobUpdatesToSkip.pop_back(); + skip = true; + break; // assume no duplicates + } else { + ++i; + } + } + }); + if (!skip) { + _staleCachedAvatarEntityBlobs.insert(id); + _needToSaveAvatarEntitySettings = true; + } + } + + // DELETE traits + // (no need to worry about the ADDs and UPDATEs: each will be handled when the interface + // tries to send a real update packet (via AvatarData::storeAvatarEntityDataPayload())) + if (_clientTraitsHandler) { + // we have a client traits handler + // flag removed entities as deleted so that changes are sent next frame + _avatarEntitiesLock.withWriteLock([&] { + for (const auto& id : entitiesToDelete) { + if (_packedAvatarEntityData.find(id) != _packedAvatarEntityData.end()) { + _clientTraitsHandler->markInstancedTraitDeleted(AvatarTraits::AvatarEntity, id); + } + } + for (const auto& id : cachedBlobsToDelete) { + if (_packedAvatarEntityData.find(id) != _packedAvatarEntityData.end()) { + _clientTraitsHandler->markInstancedTraitDeleted(AvatarTraits::AvatarEntity, id); + } + } + }); + } +} + +bool MyAvatar::updateStaleAvatarEntityBlobs() const { + // call this right before you actually need to use the blobs + // + // Note: this method is const (and modifies mutable data members) + // so we can call it at the Last Minute inside other const methods + // + auto treeRenderer = DependencyManager::get(); + EntityTreePointer entityTree = treeRenderer ? treeRenderer->getTree() : nullptr; + if (!entityTree) { + return false; + } + + std::set staleBlobs = std::move(_staleCachedAvatarEntityBlobs); + int32_t numFound = 0; + for (const auto& id : staleBlobs) { + bool found = false; + EntityItemProperties properties; + entityTree->withReadLock([&] { + EntityItemPointer entity = entityTree->findEntityByID(id); + if (entity) { + properties = entity->getProperties(); + found = true; + } + }); + if (found) { + ++numFound; + QByteArray blob; + EntityItemProperties::propertiesToBlob(*_myScriptEngine, getID(), properties, blob); + _avatarEntitiesLock.withWriteLock([&] { + _cachedAvatarEntityBlobs[id] = blob; + }); + } + } + return true; +} + +void MyAvatar::prepareAvatarEntityDataForReload() { + saveAvatarEntityDataToSettings(); + + _avatarEntitiesLock.withWriteLock([&] { + _packedAvatarEntityData.clear(); + _entitiesToDelete.clear(); + _entitiesToAdd.clear(); + _entitiesToUpdate.clear(); + _cachedAvatarEntityBlobs.clear(); + _cachedAvatarEntityBlobsToDelete.clear(); + _cachedAvatarEntityBlobsToAddOrUpdate.clear(); + _cachedAvatarEntityBlobUpdatesToSkip.clear(); + }); + + _reloadAvatarEntityDataFromSettings = true; +} + +AvatarEntityMap MyAvatar::getAvatarEntityData() const { + // NOTE: the return value is expected to be a map of unfortunately-formatted-binary-blobs + updateStaleAvatarEntityBlobs(); + AvatarEntityMap result; + _avatarEntitiesLock.withReadLock([&] { + result = _cachedAvatarEntityBlobs; + }); + return result; +} + +void MyAvatar::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) { + // Note: this is an invokable Script call + // 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. + // + if (avatarEntityData.size() > MAX_NUM_AVATAR_ENTITIES) { + // the data is suspect + qCDebug(interfaceapp) << "discard suspect AvatarEntityData with size =" << avatarEntityData.size(); + return; + } + + // this overwrites ALL AvatarEntityData so we clear pending operations + _avatarEntitiesLock.withWriteLock([&] { + _packedAvatarEntityData.clear(); + _entitiesToDelete.clear(); + _entitiesToAdd.clear(); + _entitiesToUpdate.clear(); + }); + _needToSaveAvatarEntitySettings = true; + + _avatarEntitiesLock.withWriteLock([&] { + // find new and updated IDs + AvatarEntityMap::const_iterator constItr = avatarEntityData.begin(); + while (constItr != avatarEntityData.end()) { + QUuid id = constItr.key(); + if (_cachedAvatarEntityBlobs.find(id) == _cachedAvatarEntityBlobs.end()) { + _entitiesToAdd.push_back(id); + } else { + _entitiesToUpdate.push_back(id); + } + ++constItr; + } + // find and erase deleted IDs from _cachedAvatarEntityBlobs + std::vector deletedIDs; + AvatarEntityMap::iterator itr = _cachedAvatarEntityBlobs.begin(); + while (itr != _cachedAvatarEntityBlobs.end()) { + QUuid id = itr.key(); + if (std::find(_entitiesToUpdate.begin(), _entitiesToUpdate.end(), id) == _entitiesToUpdate.end()) { + deletedIDs.push_back(id); + itr = _cachedAvatarEntityBlobs.erase(itr); + } else { + ++itr; + } + } + // copy new data + constItr = avatarEntityData.begin(); + while (constItr != avatarEntityData.end()) { + _cachedAvatarEntityBlobs.insert(constItr.key(), constItr.value()); + ++constItr; + } + // erase deleted IDs from _packedAvatarEntityData + for (const auto& id : deletedIDs) { + itr = _packedAvatarEntityData.find(id); + if (itr != _packedAvatarEntityData.end()) { + _packedAvatarEntityData.erase(itr); + } else { + ++itr; + } + _entitiesToDelete.push_back(id); + } + }); +} + +void MyAvatar::updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData) { + // NOTE: this is an invokable Script call + // TODO: we should handle the case where entityData is corrupt or invalid + // BEFORE we store into _cachedAvatarEntityBlobs + _needToSaveAvatarEntitySettings = true; + _avatarEntitiesLock.withWriteLock([&] { + AvatarEntityMap::iterator itr = _cachedAvatarEntityBlobs.find(entityID); + if (itr != _cachedAvatarEntityBlobs.end()) { + _entitiesToUpdate.push_back(entityID); + itr.value() = entityData; + } else { + _entitiesToAdd.push_back(entityID); + _cachedAvatarEntityBlobs.insert(entityID, entityData); + } + }); +} + +void MyAvatar::avatarEntityDataToJson(QJsonObject& root) const { + updateStaleAvatarEntityBlobs(); + _avatarEntitiesLock.withReadLock([&] { + if (!_cachedAvatarEntityBlobs.empty()) { + QJsonArray avatarEntityJson; + int entityCount = 0; + AvatarEntityMap::const_iterator itr = _cachedAvatarEntityBlobs.begin(); + while (itr != _cachedAvatarEntityBlobs.end()) { + QVariantMap entityData; + QUuid id = _avatarEntityForRecording.size() == _cachedAvatarEntityBlobs.size() ? _avatarEntityForRecording.values()[entityCount++] : itr.key(); + entityData.insert("id", id); + entityData.insert("properties", itr.value().toBase64()); + avatarEntityJson.push_back(QVariant(entityData).toJsonObject()); + ++itr; + } + const QString JSON_AVATAR_ENTITIES = QStringLiteral("attachedEntities"); + root[JSON_AVATAR_ENTITIES] = avatarEntityJson; + } + }); +} + void MyAvatar::loadData() { + if (!_myScriptEngine) { + _myScriptEngine = new QScriptEngine(); + } getHead()->setBasePitch(_headPitchSetting.get()); _yawSpeed = _yawSpeedSetting.get(_yawSpeed); @@ -1426,14 +1861,7 @@ void MyAvatar::loadData() { useFullAvatarURL(_fullAvatarURLFromPreferences, _fullAvatarModelName); - int avatarEntityCount = _avatarEntityCountSetting.get(0); - for (int i = 0; i < avatarEntityCount; i++) { - resizeAvatarEntitySettingHandles(i); - // QUuid entityID = QUuid::createUuid(); // generate a new ID - QUuid entityID = _avatarEntityIDSettings[i].get(QUuid()); - QByteArray properties = _avatarEntityDataSettings[i].get(); - updateAvatarEntity(entityID, properties); - } + loadAvatarEntityDataFromSettings(); // Flying preferences must be loaded before calling setFlyingEnabled() Setting::Handle firstRunVal { Settings::firstRun, true }; @@ -1455,6 +1883,38 @@ void MyAvatar::loadData() { setEnableDebugDrawPosition(Menu::getInstance()->isOptionChecked(MenuOption::AnimDebugDrawPosition)); } +void MyAvatar::loadAvatarEntityDataFromSettings() { + // this overwrites ALL AvatarEntityData so we clear pending operations + _avatarEntitiesLock.withWriteLock([&] { + _packedAvatarEntityData.clear(); + _entitiesToDelete.clear(); + _entitiesToAdd.clear(); + _entitiesToUpdate.clear(); + }); + _reloadAvatarEntityDataFromSettings = false; + _needToSaveAvatarEntitySettings = false; + + int numEntities = _avatarEntityCountSetting.get(0); + if (numEntities == 0) { + return; + } + resizeAvatarEntitySettingHandles(numEntities); + + _avatarEntitiesLock.withWriteLock([&] { + _entitiesToAdd.reserve(numEntities); + // TODO: build map between old and new IDs so we can restitch parent-child relationships + for (int i = 0; i < numEntities; i++) { + QUuid id = QUuid::createUuid(); // generate a new ID + _cachedAvatarEntityBlobs[id] = _avatarEntityDataSettings[i].get(); + _entitiesToAdd.push_back(id); + // this blob is the "authoritative source" for this AvatarEntity and we want to avoid overwriting it + // (the outgoing update packet will flag it for save-back into the blob) + // which is why we remember its id: to skip its save-back later + _cachedAvatarEntityBlobUpdatesToSkip.push_back(id); + } + }); +} + void MyAvatar::saveAttachmentData(const AttachmentData& attachment) const { Settings settings; settings.beginGroup("savedAttachmentData"); @@ -1906,8 +2366,11 @@ void MyAvatar::clearAvatarEntities() { auto treeRenderer = DependencyManager::get(); EntityTreePointer entityTree = treeRenderer ? treeRenderer->getTree() : nullptr; - AvatarEntityMap avatarEntities = getAvatarEntityData(); - for (auto entityID : avatarEntities.keys()) { + QList avatarEntityIDs; + _avatarEntitiesLock.withReadLock([&] { + avatarEntityIDs = _packedAvatarEntityData.keys(); + }); + for (const auto& entityID : avatarEntityIDs) { entityTree->withWriteLock([&entityID, &entityTree] { // remove this entity first from the entity tree entityTree->deleteEntity(entityID, true, true); @@ -1922,10 +2385,12 @@ void MyAvatar::clearAvatarEntities() { void MyAvatar::removeWearableAvatarEntities() { auto treeRenderer = DependencyManager::get(); EntityTreePointer entityTree = treeRenderer ? treeRenderer->getTree() : nullptr; - if (entityTree) { - AvatarEntityMap avatarEntities = getAvatarEntityData(); - for (auto entityID : avatarEntities.keys()) { + QList avatarEntityIDs; + _avatarEntitiesLock.withReadLock([&] { + avatarEntityIDs = _packedAvatarEntityData.keys(); + }); + for (const auto& entityID : avatarEntityIDs) { auto entity = entityTree->findEntityByID(entityID); if (entity && isWearableEntity(entity)) { entityTree->withWriteLock([&entityID, &entityTree] { @@ -1942,13 +2407,16 @@ void MyAvatar::removeWearableAvatarEntities() { } QVariantList MyAvatar::getAvatarEntitiesVariant() { + // NOTE: this method is NOT efficient QVariantList avatarEntitiesData; - QScriptEngine scriptEngine; auto treeRenderer = DependencyManager::get(); EntityTreePointer entityTree = treeRenderer ? treeRenderer->getTree() : nullptr; if (entityTree) { - AvatarEntityMap avatarEntities = getAvatarEntityData(); - for (auto entityID : avatarEntities.keys()) { + QList avatarEntityIDs; + _avatarEntitiesLock.withReadLock([&] { + avatarEntityIDs = _packedAvatarEntityData.keys(); + }); + for (const auto& entityID : avatarEntityIDs) { auto entity = entityTree->findEntityByID(entityID); if (!entity) { continue; @@ -1959,7 +2427,7 @@ QVariantList MyAvatar::getAvatarEntitiesVariant() { desiredProperties += PROP_LOCAL_POSITION; desiredProperties += PROP_LOCAL_ROTATION; EntityItemProperties entityProperties = entity->getProperties(desiredProperties); - QScriptValue scriptProperties = EntityItemPropertiesToScriptValue(&scriptEngine, entityProperties); + QScriptValue scriptProperties = EntityItemPropertiesToScriptValue(_myScriptEngine, entityProperties); avatarEntityData["properties"] = scriptProperties.toVariant(); avatarEntitiesData.append(QVariant(avatarEntityData)); } @@ -2348,17 +2816,17 @@ void MyAvatar::setAttachmentData(const QVector& attachmentData) } QVector MyAvatar::getAttachmentData() const { - QVector avatarData; - auto avatarEntities = getAvatarEntityData(); - AvatarEntityMap::const_iterator dataItr = avatarEntities.begin(); - while (dataItr != avatarEntities.end()) { - QUuid entityID = dataItr.key(); + QVector attachmentData; + QList avatarEntityIDs; + _avatarEntitiesLock.withReadLock([&] { + avatarEntityIDs = _packedAvatarEntityData.keys(); + }); + for (const auto& entityID : avatarEntityIDs) { auto properties = DependencyManager::get()->getEntityProperties(entityID); AttachmentData data = entityPropertiesToAttachmentData(properties); - avatarData.append(data); - dataItr++; + attachmentData.append(data); } - return avatarData; + return attachmentData; } QVariantList MyAvatar::getAttachmentsVariant() const { @@ -2387,16 +2855,16 @@ void MyAvatar::setAttachmentsVariant(const QVariantList& variant) { } bool MyAvatar::findAvatarEntity(const QString& modelURL, const QString& jointName, QUuid& entityID) { - auto avatarEntities = getAvatarEntityData(); - AvatarEntityMap::const_iterator dataItr = avatarEntities.begin(); - while (dataItr != avatarEntities.end()) { - entityID = dataItr.key(); + QList avatarEntityIDs; + _avatarEntitiesLock.withReadLock([&] { + avatarEntityIDs = _packedAvatarEntityData.keys(); + }); + for (const auto& entityID : avatarEntityIDs) { auto props = DependencyManager::get()->getEntityProperties(entityID); if (props.getModelURL() == modelURL && (jointName.isEmpty() || props.getParentJointIndex() == getJointIndex(jointName))) { return true; } - dataItr++; } return false; } diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 17b71153ea..67a449b274 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -575,9 +575,11 @@ public: float getHMDRollControlRate() const { return _hmdRollControlRate; } // get/set avatar data - void resizeAvatarEntitySettingHandles(unsigned int avatarEntityIndex); + void resizeAvatarEntitySettingHandles(uint32_t maxIndex); void saveData(); + void saveAvatarEntityDataToSettings(); void loadData(); + void loadAvatarEntityDataFromSettings(); void saveAttachmentData(const AttachmentData& attachment) const; AttachmentData loadAttachmentData(const QUrl& modelURL, const QString& jointName = QString()) const; @@ -1184,6 +1186,7 @@ public: virtual void setAttachmentsVariant(const QVariantList& variant) override; glm::vec3 getNextPosition() { return _goToPending ? _goToPosition : getWorldPosition(); } + void prepareAvatarEntityDataForReload(); /**jsdoc * Create a new grab. @@ -1204,6 +1207,11 @@ public: */ Q_INVOKABLE void releaseGrab(const QUuid& grabID); + AvatarEntityMap getAvatarEntityData() const override; + void setAvatarEntityData(const AvatarEntityMap& avatarEntityData) override; + void updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData) override; + void avatarEntityDataToJson(QJsonObject& root) const override; + public slots: /**jsdoc @@ -1402,6 +1410,10 @@ public slots: */ bool getEnableMeshVisible() const override; + void storeAvatarEntityDataPayload(const QUuid& entityID, const QByteArray& payload) override; + void clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFromTree = true) override; + void sanitizeAvatarEntityProperties(EntityItemProperties& properties) const; + /**jsdoc * Set whether or not your avatar mesh is visible. * @function MyAvatar.setEnableMeshVisible @@ -1601,23 +1613,24 @@ signals: */ void disableHandTouchForIDChanged(const QUuid& entityID, bool disable); - private slots: void leaveDomain(); void updateCollisionCapsuleCache(); protected: + void handleChangedAvatarEntityData(); virtual void beParentOfChild(SpatiallyNestablePointer newChild) const override; virtual void forgetChild(SpatiallyNestablePointer newChild) const override; virtual void recalculateChildCauterization() const override; private: + bool updateStaleAvatarEntityBlobs() const; bool requiresSafeLanding(const glm::vec3& positionIn, glm::vec3& positionOut); virtual QByteArray toByteArrayStateful(AvatarDataDetail dataDetail, bool dropFaceTracking) override; - void simulate(float deltaTime); + void simulate(float deltaTime, bool inView) override; void updateFromTrackers(float deltaTime); void saveAvatarUrl(); virtual void render(RenderArgs* renderArgs) override; @@ -1920,6 +1933,7 @@ private: bool _haveReceivedHeightLimitsFromDomain { false }; int _disableHandTouchCount { 0 }; bool _skeletonModelLoaded { false }; + bool _reloadAvatarEntityDataFromSettings { true }; Setting::Handle _dominantHandSetting; Setting::Handle _headPitchSetting; @@ -1938,6 +1952,38 @@ private: Setting::Handle _allowTeleportingSetting { "allowTeleporting", true }; std::vector> _avatarEntityIDSettings; std::vector> _avatarEntityDataSettings; + + // AvatarEntities stuff: + // We cache the "map of unfortunately-formatted-binary-blobs" because they are expensive to compute + // Do not confuse these with AvatarData::_packedAvatarEntityData which are in wire-format. + mutable AvatarEntityMap _cachedAvatarEntityBlobs; + + // We collect changes to AvatarEntities and then handle them all in one spot per frame: updateAvatarEntities(). + // Basically this is a "transaction pattern" with an extra complication: these changes can come from two + // "directions" and the "authoritative source" of each direction is different, so maintain two distinct sets of + // transaction lists; + // + // The _entitiesToDelete/Add/Update lists are for changes whose "authoritative sources" are already + // correctly stored in _cachedAvatarEntityBlobs. These come from loadAvatarEntityDataFromSettings() and + // setAvatarEntityData(). These changes need to be extracted from _cachedAvatarEntityBlobs and applied to + // real EntityItems. + std::vector _entitiesToDelete; + std::vector _entitiesToAdd; + std::vector _entitiesToUpdate; + // + // The _cachedAvatarEntityBlobsToDelete/Add/Update lists are for changes whose "authoritative sources" are + // already reflected in real EntityItems. These changes need to be propagated to _cachedAvatarEntityBlobs + // and eventually to settings. + std::vector _cachedAvatarEntityBlobsToDelete; + std::vector _cachedAvatarEntityBlobsToAddOrUpdate; + std::vector _cachedAvatarEntityBlobUpdatesToSkip; + // + // Also these lists for tracking delayed changes to blobs and Settings + mutable std::set _staleCachedAvatarEntityBlobs; + // + // keep a ScriptEngine around so we don't have to instantiate on the fly (these are very slow to create/delete) + QScriptEngine* _myScriptEngine { nullptr }; + bool _needToSaveAvatarEntitySettings { false }; }; QScriptValue audioListenModeToScriptValue(QScriptEngine* engine, const AudioListenerMode& audioListenerMode); diff --git a/interface/src/avatar/OtherAvatar.cpp b/interface/src/avatar/OtherAvatar.cpp index a71d2478ad..0dfc349e18 100644 --- a/interface/src/avatar/OtherAvatar.cpp +++ b/interface/src/avatar/OtherAvatar.cpp @@ -7,10 +7,18 @@ // #include "OtherAvatar.h" -#include "Application.h" +#include +#include + +#include + +#include "Application.h" #include "AvatarMotionState.h" +const float DISPLAYNAME_FADE_TIME = 0.5f; +const float DISPLAYNAME_FADE_FACTOR = pow(0.01f, 1.0f / DISPLAYNAME_FADE_TIME); + static glm::u8vec3 getLoadingOrbColor(Avatar::LoadingStatus loadingStatus) { const glm::u8vec3 NO_MODEL_COLOR(0xe3, 0xe3, 0xe3); @@ -142,4 +150,293 @@ void OtherAvatar::updateCollisionGroup(bool myAvatarCollide) { _motionState->addDirtyFlags(Simulation::DIRTY_COLLISION_GROUP); } } -} \ No newline at end of file +} + +void OtherAvatar::simulate(float deltaTime, bool inView) { + PROFILE_RANGE(simulation, "simulate"); + + _globalPosition = _transit.isActive() ? _transit.getCurrentPosition() : _serverPosition; + if (!hasParent()) { + setLocalPosition(_globalPosition); + } + + _simulationRate.increment(); + if (inView) { + _simulationInViewRate.increment(); + } + + PerformanceTimer perfTimer("simulate"); + { + PROFILE_RANGE(simulation, "updateJoints"); + if (inView) { + Head* head = getHead(); + if (_hasNewJointData || _transit.isActive()) { + _skeletonModel->getRig().copyJointsFromJointData(_jointData); + glm::mat4 rootTransform = glm::scale(_skeletonModel->getScale()) * glm::translate(_skeletonModel->getOffset()); + _skeletonModel->getRig().computeExternalPoses(rootTransform); + _jointDataSimulationRate.increment(); + + _skeletonModel->simulate(deltaTime, true); + + locationChanged(); // joints changed, so if there are any children, update them. + _hasNewJointData = false; + + glm::vec3 headPosition = getWorldPosition(); + if (!_skeletonModel->getHeadPosition(headPosition)) { + headPosition = getWorldPosition(); + } + head->setPosition(headPosition); + } + head->setScale(getModelScale()); + head->simulate(deltaTime); + relayJointDataToChildren(); + } else { + // a non-full update is still required so that the position, rotation, scale and bounds of the skeletonModel are updated. + _skeletonModel->simulate(deltaTime, false); + } + _skeletonModelSimulationRate.increment(); + } + + // update animation for display name fade in/out + if ( _displayNameTargetAlpha != _displayNameAlpha) { + // the alpha function is + // Fade out => alpha(t) = factor ^ t => alpha(t+dt) = alpha(t) * factor^(dt) + // Fade in => alpha(t) = 1 - factor^t => alpha(t+dt) = 1-(1-alpha(t))*coef^(dt) + // factor^(dt) = coef + float coef = pow(DISPLAYNAME_FADE_FACTOR, deltaTime); + if (_displayNameTargetAlpha < _displayNameAlpha) { + // Fading out + _displayNameAlpha *= coef; + } else { + // Fading in + _displayNameAlpha = 1.0f - (1.0f - _displayNameAlpha) * coef; + } + _displayNameAlpha = glm::abs(_displayNameAlpha - _displayNameTargetAlpha) < 0.01f ? _displayNameTargetAlpha : _displayNameAlpha; + } + + { + PROFILE_RANGE(simulation, "misc"); + measureMotionDerivatives(deltaTime); + simulateAttachments(deltaTime); + updatePalms(); + } + { + PROFILE_RANGE(simulation, "entities"); + handleChangedAvatarEntityData(); + updateAttachedAvatarEntities(); + } + + { + PROFILE_RANGE(simulation, "grabs"); + updateGrabs(); + } + + updateFadingStatus(); +} + +void OtherAvatar::handleChangedAvatarEntityData() { + PerformanceTimer perfTimer("attachments"); + + // AVATAR ENTITY UPDATE FLOW + // - if queueEditEntityMessage() sees "AvatarEntity" HostType it calls _myAvatar->storeAvatarEntityDataPayload() + // - storeAvatarEntityDataPayload() saves the payload and flags the trait instance for the entity as updated, + // - ClientTraitsHandler::sendChangedTraitsToMixea() sends the entity bytes to the mixer which relays them to other interfaces + // - AvatarHashMap::processBulkAvatarTraits() on other interfaces calls avatar->processTraitInstance() + // - AvatarData::processTraitInstance() calls storeAvatarEntityDataPayload(), which sets _avatarEntityDataChanged = true + // - (My)Avatar::simulate() calls handleChangedAvatarEntityData() every frame which checks _avatarEntityDataChanged + // and here we are... + + // AVATAR ENTITY DELETE FLOW + // - EntityScriptingInterface::deleteEntity() calls _myAvatar->clearAvatarEntity() for deleted avatar entities + // - clearAvatarEntity() removes the avatar entity and flags the trait instance for the entity as deleted + // - ClientTraitsHandler::sendChangedTraitsToMixer() sends a deletion to the mixer which relays to other interfaces + // - AvatarHashMap::processBulkAvatarTraits() on other interfaces calls avatar->processDeletedTraitInstace() + // - AvatarData::processDeletedTraitInstance() calls clearAvatarEntity() + // - AvatarData::clearAvatarEntity() sets _avatarEntityDataChanged = true and adds the ID to the detached list + // - (My)Avatar::simulate() calls handleChangedAvatarEntityData() every frame which checks _avatarEntityDataChanged + // and here we are... + + if (!_avatarEntityDataChanged) { + return; + } + + auto treeRenderer = DependencyManager::get(); + EntityTreePointer entityTree = treeRenderer ? treeRenderer->getTree() : nullptr; + if (!entityTree) { + return; + } + + PackedAvatarEntityMap packedAvatarEntityData; + _avatarEntitiesLock.withReadLock([&] { + packedAvatarEntityData = _packedAvatarEntityData; + }); + entityTree->withWriteLock([&] { + AvatarEntityMap::const_iterator dataItr = packedAvatarEntityData.begin(); + while (dataItr != packedAvatarEntityData.end()) { + // compute hash of data. TODO? cache this? + QByteArray data = dataItr.value(); + uint32_t newHash = qHash(data); + + // check to see if we recognize this hash and whether it was already successfully processed + QUuid entityID = dataItr.key(); + MapOfAvatarEntityDataHashes::iterator stateItr = _avatarEntityDataHashes.find(entityID); + if (stateItr != _avatarEntityDataHashes.end()) { + if (stateItr.value().success) { + if (newHash == stateItr.value().hash) { + // data hasn't changed --> nothing to do + ++dataItr; + continue; + } + } else { + // NOTE: if the data was unsuccessful in producing an entity in the past + // we will try again just in case something changed (unlikely). + // Unfortunately constantly trying to build the entity for this data costs + // CPU cycles that we'd rather not spend. + // TODO? put a maximum number of tries on this? + } + } else { + // sanity check data + QUuid id; + EntityTypes::EntityType type; + EntityTypes::extractEntityTypeAndID((unsigned char*)(data.data()), data.size(), type, id); + if (id != entityID || !EntityTypes::typeIsValid(type)) { + // skip for corrupt + ++dataItr; + continue; + } + // remember this hash for the future + stateItr = _avatarEntityDataHashes.insert(entityID, AvatarEntityDataHash(newHash)); + } + ++dataItr; + + EntityItemProperties properties; + int32_t bytesLeftToRead = data.size(); + unsigned char* dataAt = (unsigned char*)(data.data()); + if (!properties.constructFromBuffer(dataAt, bytesLeftToRead)) { + // properties are corrupt + continue; + } + + properties.setEntityHostType(entity::HostType::AVATAR); + properties.setOwningAvatarID(getID()); + + // there's no entity-server to tell us we're the simulation owner, so always set the + // simulationOwner to the owningAvatarID and a high priority. + properties.setSimulationOwner(getID(), AVATAR_ENTITY_SIMULATION_PRIORITY); + + if (properties.getParentID() == AVATAR_SELF_ID) { + properties.setParentID(getID()); + } + + // NOTE: if this avatar entity is not attached to us, strip its entity script completely... + auto attachedScript = properties.getScript(); + if (!isMyAvatar() && !attachedScript.isEmpty()) { + QString noScript; + properties.setScript(noScript); + } + + auto specifiedHref = properties.getHref(); + if (!isMyAvatar() && !specifiedHref.isEmpty()) { + qCDebug(avatars) << "removing entity href from avatar attached entity:" << entityID << "old href:" << specifiedHref; + QString noHref; + properties.setHref(noHref); + } + + // When grabbing avatar entities, they are parented to the joint moving them, then when un-grabbed + // they go back to the default parent (null uuid). When un-gripped, others saw the entity disappear. + // The thinking here is the local position was noticed as changing, but not the parentID (since it is now + // back to the default), and the entity flew off somewhere. Marking all changed definitely fixes this, + // and seems safe (per Seth). + properties.markAllChanged(); + + // try to build the entity + EntityItemPointer entity = entityTree->findEntityByEntityItemID(EntityItemID(entityID)); + bool success = true; + if (entity) { + QUuid oldParentID = entity->getParentID(); + if (entityTree->updateEntity(entityID, properties)) { + entity->updateLastEditedFromRemote(); + } else { + success = false; + } + if (oldParentID != entity->getParentID()) { + if (entity->getParentID() == getID()) { + onAddAttachedAvatarEntity(entityID); + } else if (oldParentID == getID()) { + onRemoveAttachedAvatarEntity(entityID); + } + } + } else { + entity = entityTree->addEntity(entityID, properties); + if (!entity) { + success = false; + } else if (entity->getParentID() == getID()) { + onAddAttachedAvatarEntity(entityID); + } + } + stateItr.value().success = success; + } + + AvatarEntityIDs recentlyRemovedAvatarEntities = getAndClearRecentlyRemovedIDs(); + if (!recentlyRemovedAvatarEntities.empty()) { + // only lock this thread when absolutely necessary + AvatarEntityMap packedAvatarEntityData; + _avatarEntitiesLock.withReadLock([&] { + packedAvatarEntityData = _packedAvatarEntityData; + }); + foreach (auto entityID, recentlyRemovedAvatarEntities) { + if (!packedAvatarEntityData.contains(entityID)) { + entityTree->deleteEntity(entityID, true, true); + } + } + + // TODO: move this outside of tree lock + // remove stale data hashes + foreach (auto entityID, recentlyRemovedAvatarEntities) { + MapOfAvatarEntityDataHashes::iterator stateItr = _avatarEntityDataHashes.find(entityID); + if (stateItr != _avatarEntityDataHashes.end()) { + _avatarEntityDataHashes.erase(stateItr); + } + onRemoveAttachedAvatarEntity(entityID); + } + } + if (packedAvatarEntityData.size() != _avatarEntityForRecording.size()) { + createRecordingIDs(); + } + }); + + setAvatarEntityDataChanged(false); +} + +void OtherAvatar::onAddAttachedAvatarEntity(const QUuid& id) { + for (uint32_t i = 0; i < _attachedAvatarEntities.size(); ++i) { + if (_attachedAvatarEntities[i] == id) { + return; + } + } + _attachedAvatarEntities.push_back(id); +} + +void OtherAvatar::onRemoveAttachedAvatarEntity(const QUuid& id) { + for (uint32_t i = 0; i < _attachedAvatarEntities.size(); ++i) { + if (_attachedAvatarEntities[i] == id) { + if (i != _attachedAvatarEntities.size() - 1) { + _attachedAvatarEntities[i] = _attachedAvatarEntities.back(); + } + _attachedAvatarEntities.pop_back(); + break; + } + } +} + +void OtherAvatar::updateAttachedAvatarEntities() { + if (!_attachedAvatarEntities.empty()) { + auto treeRenderer = DependencyManager::get(); + if (!treeRenderer) { + return; + } + for (const QUuid& id : _attachedAvatarEntities) { + treeRenderer->onEntityChanged(id); + } + } +} diff --git a/interface/src/avatar/OtherAvatar.h b/interface/src/avatar/OtherAvatar.h index 48402fe55c..a1dc5724a9 100644 --- a/interface/src/avatar/OtherAvatar.h +++ b/interface/src/avatar/OtherAvatar.h @@ -10,6 +10,7 @@ #define hifi_OtherAvatar_h #include +#include #include #include @@ -47,9 +48,17 @@ public: void updateCollisionGroup(bool myAvatarCollide); + void simulate(float deltaTime, bool inView) override; + friend AvatarManager; protected: + void handleChangedAvatarEntityData(); + void updateAttachedAvatarEntities(); + void onAddAttachedAvatarEntity(const QUuid& id); + void onRemoveAttachedAvatarEntity(const QUuid& id); + + std::vector _attachedAvatarEntities; std::shared_ptr _otherAvatarOrbMeshPlaceholder { nullptr }; OverlayID _otherAvatarOrbMeshPlaceholderID { UNKNOWN_OVERLAY_ID }; AvatarMotionState* _motionState { nullptr }; diff --git a/libraries/avatars-renderer/src/avatars-renderer/Avatar.cpp b/libraries/avatars-renderer/src/avatars-renderer/Avatar.cpp index dbf5899386..a6185d7e79 100644 --- a/libraries/avatars-renderer/src/avatars-renderer/Avatar.cpp +++ b/libraries/avatars-renderer/src/avatars-renderer/Avatar.cpp @@ -308,171 +308,16 @@ void Avatar::setAvatarEntityDataChanged(bool value) { _avatarEntityDataHashes.clear(); } -void Avatar::updateAvatarEntities() { - PerformanceTimer perfTimer("attachments"); - - // AVATAR ENTITY UPDATE FLOW - // - if queueEditEntityMessage sees avatarEntity flag it does _myAvatar->updateAvatarEntity() - // - updateAvatarEntity saves the bytes and flags the trait instance for the entity as updated - // - ClientTraitsHandler::sendChangedTraitsToMixer sends the entity bytes to the mixer which relays them to other interfaces - // - AvatarHashMap::processBulkAvatarTraits on other interfaces calls avatar->processTraitInstace - // - AvatarData::processTraitInstance calls updateAvatarEntity, which sets _avatarEntityDataChanged = true - // - (My)Avatar::simulate notices _avatarEntityDataChanged and here we are... - - // AVATAR ENTITY DELETE FLOW - // - EntityScriptingInterface::deleteEntity calls _myAvatar->clearAvatarEntity() for deleted avatar entities - // - clearAvatarEntity removes the avatar entity and flags the trait instance for the entity as deleted - // - ClientTraitsHandler::sendChangedTraitsToMixer sends a deletion to the mixer which relays to other interfaces - // - AvatarHashMap::processBulkAvatarTraits on other interfaces calls avatar->processDeletedTraitInstace - // - AvatarData::processDeletedTraitInstance calls clearAvatarEntity - // - AvatarData::clearAvatarEntity sets _avatarEntityDataChanged = true and adds the ID to the detached list - // - Avatar::simulate notices _avatarEntityDataChanged and here we are... - - if (!_avatarEntityDataChanged) { - return; - } - - if (getID().isNull() || - getID() == AVATAR_SELF_ID || - DependencyManager::get()->getSessionUUID() == QUuid()) { - // wait until MyAvatar and this Node gets an ID before doing this. Otherwise, various things go wrong -- - // things get their parent fixed up from AVATAR_SELF_ID to a null uuid which means "no parent". - return; - } - - auto treeRenderer = DependencyManager::get(); - EntityTreePointer entityTree = treeRenderer ? treeRenderer->getTree() : nullptr; - if (!entityTree) { - return; - } - - QScriptEngine scriptEngine; - entityTree->withWriteLock([&] { - AvatarEntityMap avatarEntities = getAvatarEntityData(); - AvatarEntityMap::const_iterator dataItr = avatarEntities.begin(); - while (dataItr != avatarEntities.end()) { - // compute hash of data. TODO? cache this? - QByteArray data = dataItr.value(); - uint32_t newHash = qHash(data); - - // check to see if we recognize this hash and whether it was already successfully processed - QUuid entityID = dataItr.key(); - MapOfAvatarEntityDataHashes::iterator stateItr = _avatarEntityDataHashes.find(entityID); - if (stateItr != _avatarEntityDataHashes.end()) { - if (stateItr.value().success) { - if (newHash == stateItr.value().hash) { - // data hasn't changed --> nothing to do - ++dataItr; - continue; - } - } else { - // NOTE: if the data was unsuccessful in producing an entity in the past - // we will try again just in case something changed (unlikely). - // Unfortunately constantly trying to build the entity for this data costs - // CPU cycles that we'd rather not spend. - // TODO? put a maximum number of tries on this? - } - } else { - // remember this hash for the future - stateItr = _avatarEntityDataHashes.insert(entityID, AvatarEntityDataHash(newHash)); - } - ++dataItr; - - // see EntityEditPacketSender::queueEditEntityMessage for the other end of this. unpack properties - // and either add or update the entity. - QJsonDocument jsonProperties = QJsonDocument::fromBinaryData(data); - if (!jsonProperties.isObject()) { - qCDebug(avatars_renderer) << "got bad avatarEntity json" << QString(data.toHex()); - continue; - } - - QVariant variantProperties = jsonProperties.toVariant(); - QVariantMap asMap = variantProperties.toMap(); - QScriptValue scriptProperties = variantMapToScriptValue(asMap, scriptEngine); - EntityItemProperties properties; - EntityItemPropertiesFromScriptValueIgnoreReadOnly(scriptProperties, properties); - properties.setEntityHostType(entity::HostType::AVATAR); - properties.setOwningAvatarID(getID()); - - if (properties.getParentID() == AVATAR_SELF_ID) { - properties.setParentID(getID()); - } - - // NOTE: if this avatar entity is not attached to us, strip its entity script completely... - auto attachedScript = properties.getScript(); - if (!isMyAvatar() && !attachedScript.isEmpty()) { - QString noScript; - properties.setScript(noScript); - } - - auto specifiedHref = properties.getHref(); - if (!isMyAvatar() && !specifiedHref.isEmpty()) { - qCDebug(avatars_renderer) << "removing entity href from avatar attached entity:" << entityID << "old href:" << specifiedHref; - QString noHref; - properties.setHref(noHref); - } - - // When grabbing avatar entities, they are parented to the joint moving them, then when un-grabbed - // they go back to the default parent (null uuid). When un-gripped, others saw the entity disappear. - // The thinking here is the local position was noticed as changing, but not the parentID (since it is now - // back to the default), and the entity flew off somewhere. Marking all changed definitely fixes this, - // and seems safe (per Seth). - properties.markAllChanged(); - - // try to build the entity - EntityItemPointer entity = entityTree->findEntityByEntityItemID(EntityItemID(entityID)); - bool success = true; - if (entity) { - if (entityTree->updateEntity(entityID, properties)) { - entity->updateLastEditedFromRemote(); - } else { - success = false; - } - } else { - entity = entityTree->addEntity(entityID, properties); - if (!entity) { - success = false; - } - } - stateItr.value().success = success; - } - - AvatarEntityIDs recentlyDetachedAvatarEntities = getAndClearRecentlyDetachedIDs(); - if (!recentlyDetachedAvatarEntities.empty()) { - // only lock this thread when absolutely necessary - AvatarEntityMap avatarEntityData; - _avatarEntitiesLock.withReadLock([&] { - avatarEntityData = _avatarEntityData; - }); - foreach (auto entityID, recentlyDetachedAvatarEntities) { - if (!avatarEntityData.contains(entityID)) { - entityTree->deleteEntity(entityID, true, true); - } - } - - // remove stale data hashes - foreach (auto entityID, recentlyDetachedAvatarEntities) { - MapOfAvatarEntityDataHashes::iterator stateItr = _avatarEntityDataHashes.find(entityID); - if (stateItr != _avatarEntityDataHashes.end()) { - _avatarEntityDataHashes.erase(stateItr); - } - } - } - if (avatarEntities.size() != _avatarEntityForRecording.size()) { - createRecordingIDs(); - } - }); - - setAvatarEntityDataChanged(false); -} - void Avatar::removeAvatarEntitiesFromTree() { auto treeRenderer = DependencyManager::get(); EntityTreePointer entityTree = treeRenderer ? treeRenderer->getTree() : nullptr; if (entityTree) { + QList avatarEntityIDs; + _avatarEntitiesLock.withReadLock([&] { + avatarEntityIDs = _packedAvatarEntityData.keys(); + }); entityTree->withWriteLock([&] { - AvatarEntityMap avatarEntities = getAvatarEntityData(); - for (auto entityID : avatarEntities.keys()) { + for (const auto& entityID : avatarEntityIDs) { entityTree->deleteEntity(entityID, true, true); } }); @@ -647,87 +492,6 @@ void Avatar::relayJointDataToChildren() { _reconstructSoftEntitiesJointMap = false; } -void Avatar::simulate(float deltaTime, bool inView) { - PROFILE_RANGE(simulation, "simulate"); - - _globalPosition = _transit.isActive() ? _transit.getCurrentPosition() : _serverPosition; - if (!hasParent()) { - setLocalPosition(_globalPosition); - } - - _simulationRate.increment(); - if (inView) { - _simulationInViewRate.increment(); - } - - PerformanceTimer perfTimer("simulate"); - { - PROFILE_RANGE(simulation, "updateJoints"); - if (inView) { - Head* head = getHead(); - if (_hasNewJointData || _transit.isActive()) { - _skeletonModel->getRig().copyJointsFromJointData(_jointData); - glm::mat4 rootTransform = glm::scale(_skeletonModel->getScale()) * glm::translate(_skeletonModel->getOffset()); - _skeletonModel->getRig().computeExternalPoses(rootTransform); - _jointDataSimulationRate.increment(); - - _skeletonModel->simulate(deltaTime, true); - - locationChanged(); // joints changed, so if there are any children, update them. - _hasNewJointData = false; - - glm::vec3 headPosition = getWorldPosition(); - if (!_skeletonModel->getHeadPosition(headPosition)) { - headPosition = getWorldPosition(); - } - head->setPosition(headPosition); - } - head->setScale(getModelScale()); - head->simulate(deltaTime); - relayJointDataToChildren(); - } else { - // a non-full update is still required so that the position, rotation, scale and bounds of the skeletonModel are updated. - _skeletonModel->simulate(deltaTime, false); - } - _skeletonModelSimulationRate.increment(); - } - - // update animation for display name fade in/out - if ( _displayNameTargetAlpha != _displayNameAlpha) { - // the alpha function is - // Fade out => alpha(t) = factor ^ t => alpha(t+dt) = alpha(t) * factor^(dt) - // Fade in => alpha(t) = 1 - factor^t => alpha(t+dt) = 1-(1-alpha(t))*coef^(dt) - // factor^(dt) = coef - float coef = pow(DISPLAYNAME_FADE_FACTOR, deltaTime); - if (_displayNameTargetAlpha < _displayNameAlpha) { - // Fading out - _displayNameAlpha *= coef; - } else { - // Fading in - _displayNameAlpha = 1 - (1 - _displayNameAlpha) * coef; - } - _displayNameAlpha = abs(_displayNameAlpha - _displayNameTargetAlpha) < 0.01f ? _displayNameTargetAlpha : _displayNameAlpha; - } - - { - PROFILE_RANGE(simulation, "misc"); - measureMotionDerivatives(deltaTime); - simulateAttachments(deltaTime); - updatePalms(); - } - { - PROFILE_RANGE(simulation, "entities"); - updateAvatarEntities(); - } - - { - PROFILE_RANGE(simulation, "grabs"); - updateGrabs(); - } - - updateFadingStatus(); -} - float Avatar::getSimulationRate(const QString& rateName) const { if (rateName == "") { return _simulationRate.rate(); @@ -1042,7 +806,6 @@ void Avatar::render(RenderArgs* renderArgs) { } } - void Avatar::setEnableMeshVisible(bool isEnabled) { if (_isMeshVisible != isEnabled) { _isMeshVisible = isEnabled; diff --git a/libraries/avatars-renderer/src/avatars-renderer/Avatar.h b/libraries/avatars-renderer/src/avatars-renderer/Avatar.h index fddd52a6dd..4ff3e9cc13 100644 --- a/libraries/avatars-renderer/src/avatars-renderer/Avatar.h +++ b/libraries/avatars-renderer/src/avatars-renderer/Avatar.h @@ -139,9 +139,8 @@ public: typedef render::Payload Payload; void init(); - void updateAvatarEntities(); void removeAvatarEntitiesFromTree(); - void simulate(float deltaTime, bool inView); + virtual void simulate(float deltaTime, bool inView) = 0; virtual void simulateAttachments(float deltaTime); virtual void render(RenderArgs* renderArgs); @@ -240,8 +239,6 @@ public: static void renderJointConnectingCone(gpu::Batch& batch, glm::vec3 position1, glm::vec3 position2, float radius1, float radius2, const glm::vec4& color); - virtual void applyCollision(const glm::vec3& contactPoint, const glm::vec3& penetration) { } - /**jsdoc * Set the offset applied to the current avatar. The offset adjusts the position that the avatar is rendered. For example, * with an offset of { x: 0, y: 0.1, z: 0 }, your avatar will appear to be raised off the ground slightly. diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 2f1c8d3d82..21e0a6aba2 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -1908,10 +1908,9 @@ qint64 AvatarData::packAvatarEntityTraitInstance(AvatarTraits::TraitType traitTy // grab a read lock on the avatar entities and check for entity data for the given ID QByteArray entityBinaryData; - _avatarEntitiesLock.withReadLock([this, &entityBinaryData, &traitInstanceID] { - if (_avatarEntityData.contains(traitInstanceID)) { - entityBinaryData = _avatarEntityData[traitInstanceID]; + if (_packedAvatarEntityData.contains(traitInstanceID)) { + entityBinaryData = _packedAvatarEntityData[traitInstanceID]; } }); @@ -1998,7 +1997,7 @@ qint64 AvatarData::packTraitInstance(AvatarTraits::TraitType traitType, AvatarTr void AvatarData::prepareResetTraitInstances() { if (_clientTraitsHandler) { _avatarEntitiesLock.withReadLock([this]{ - foreach (auto entityID, _avatarEntityData.keys()) { + foreach (auto entityID, _packedAvatarEntityData.keys()) { _clientTraitsHandler->markInstancedTraitUpdated(AvatarTraits::AvatarEntity, entityID); } foreach (auto grabID, _avatarGrabData.keys()) { @@ -2019,7 +2018,7 @@ void AvatarData::processTrait(AvatarTraits::TraitType traitType, QByteArray trai void AvatarData::processTraitInstance(AvatarTraits::TraitType traitType, AvatarTraits::TraitInstanceID instanceID, QByteArray traitBinaryData) { if (traitType == AvatarTraits::AvatarEntity) { - updateAvatarEntity(instanceID, traitBinaryData); + storeAvatarEntityDataPayload(instanceID, traitBinaryData); } else if (traitType == AvatarTraits::Grab) { updateAvatarGrabData(instanceID, traitBinaryData); } @@ -2367,7 +2366,7 @@ void AvatarData::setRecordingBasis(std::shared_ptr recordingBasis) { void AvatarData::createRecordingIDs() { _avatarEntitiesLock.withReadLock([&] { _avatarEntityForRecording.clear(); - for (int i = 0; i < _avatarEntityData.size(); i++) { + for (int i = 0; i < _packedAvatarEntityData.size(); i++) { _avatarEntityForRecording.insert(QUuid::createUuid()); } }); @@ -2422,6 +2421,10 @@ JointData jointDataFromJsonValue(int version, const QJsonValue& json) { return result; } +void AvatarData::avatarEntityDataToJson(QJsonObject& root) const { + // overridden where needed +} + QJsonObject AvatarData::toJson() const { QJsonObject root; @@ -2433,20 +2436,8 @@ QJsonObject AvatarData::toJson() const { if (!getDisplayName().isEmpty()) { root[JSON_AVATAR_DISPLAY_NAME] = getDisplayName(); } - _avatarEntitiesLock.withReadLock([&] { - if (!_avatarEntityData.empty()) { - QJsonArray avatarEntityJson; - int entityCount = 0; - for (auto entityID : _avatarEntityData.keys()) { - QVariantMap entityData; - QUuid newId = _avatarEntityForRecording.size() == _avatarEntityData.size() ? _avatarEntityForRecording.values()[entityCount++] : entityID; - entityData.insert("id", newId); - entityData.insert("properties", _avatarEntityData.value(entityID).toBase64()); - avatarEntityJson.push_back(QVariant(entityData).toJsonObject()); - } - root[JSON_AVATAR_ENTITIES] = avatarEntityJson; - } - }); + + avatarEntityDataToJson(root); auto recordingBasis = getRecordingBasis(); bool success; @@ -2568,9 +2559,9 @@ void AvatarData::fromJson(const QJsonObject& json, bool useFrameSkeleton) { for (auto attachmentJson : attachmentsJson) { if (attachmentJson.isObject()) { QVariantMap entityData = attachmentJson.toObject().toVariantMap(); - QUuid entityID = entityData.value("id").toUuid(); - QByteArray properties = QByteArray::fromBase64(entityData.value("properties").toByteArray()); - updateAvatarEntity(entityID, properties); + QUuid id = entityData.value("id").toUuid(); + QByteArray data = QByteArray::fromBase64(entityData.value("properties").toByteArray()); + updateAvatarEntity(id, data); } } } @@ -2752,17 +2743,15 @@ void AvatarData::setAttachmentsVariant(const QVariantList& variant) { setAttachmentData(newAttachments); } -const int MAX_NUM_AVATAR_ENTITIES = 42; - -void AvatarData::updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData) { +void AvatarData::storeAvatarEntityDataPayload(const QUuid& entityID, const QByteArray& data) { _avatarEntitiesLock.withWriteLock([&] { - AvatarEntityMap::iterator itr = _avatarEntityData.find(entityID); - if (itr == _avatarEntityData.end()) { - if (_avatarEntityData.size() < MAX_NUM_AVATAR_ENTITIES) { - _avatarEntityData.insert(entityID, entityData); + PackedAvatarEntityMap::iterator itr = _packedAvatarEntityData.find(entityID); + if (itr == _packedAvatarEntityData.end()) { + if (_packedAvatarEntityData.size() < MAX_NUM_AVATAR_ENTITIES) { + _packedAvatarEntityData.insert(entityID, data); } } else { - itr.value() = entityData; + itr.value() = data; } }); @@ -2775,15 +2764,20 @@ void AvatarData::updateAvatarEntity(const QUuid& entityID, const QByteArray& ent } } +void AvatarData::updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData) { + // overridden where needed + // expects 'entityData' to be a JavaScript EntityItemProperties Object in QByteArray form +} + void AvatarData::clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFromTree) { bool removedEntity = false; _avatarEntitiesLock.withWriteLock([this, &removedEntity, &entityID] { - removedEntity = _avatarEntityData.remove(entityID); + removedEntity = _packedAvatarEntityData.remove(entityID); }); - insertDetachedEntityID(entityID); + insertRemovedEntityID(entityID); if (removedEntity && _clientTraitsHandler) { // we have a client traits handler, so we need to mark this removed instance trait as deleted @@ -2793,75 +2787,29 @@ void AvatarData::clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFr } AvatarEntityMap AvatarData::getAvatarEntityData() const { - AvatarEntityMap result; - _avatarEntitiesLock.withReadLock([&] { - result = _avatarEntityData; - }); - return result; -} - -void AvatarData::insertDetachedEntityID(const QUuid entityID) { - _avatarEntitiesLock.withWriteLock([&] { - _avatarEntityDetached.insert(entityID); - }); - - _avatarEntityDataChanged = true; + // overridden where needed + // NOTE: the return value is expected to be a map of unfortunately-formatted-binary-blobs + return AvatarEntityMap(); } void AvatarData::setAvatarEntityData(const AvatarEntityMap& avatarEntityData) { - if (avatarEntityData.size() > MAX_NUM_AVATAR_ENTITIES) { - // the data is suspect - qCDebug(avatars) << "discard suspect AvatarEntityData with size =" << avatarEntityData.size(); - return; - } - - std::vector deletedEntityIDs; - QList updatedEntityIDs; - - _avatarEntitiesLock.withWriteLock([&] { - if (_avatarEntityData != avatarEntityData) { - - // keep track of entities that were attached to this avatar but no longer are - AvatarEntityIDs previousAvatarEntityIDs = QSet::fromList(_avatarEntityData.keys()); - - _avatarEntityData = avatarEntityData; - setAvatarEntityDataChanged(true); - - deletedEntityIDs.reserve(previousAvatarEntityIDs.size()); - - foreach (auto entityID, previousAvatarEntityIDs) { - if (!_avatarEntityData.contains(entityID)) { - _avatarEntityDetached.insert(entityID); - deletedEntityIDs.push_back(entityID); - } - } - - updatedEntityIDs = _avatarEntityData.keys(); - } - }); - - if (_clientTraitsHandler) { - // we have a client traits handler - - // flag removed entities as deleted so that changes are sent next frame - for (auto& deletedEntityID : deletedEntityIDs) { - _clientTraitsHandler->markInstancedTraitDeleted(AvatarTraits::AvatarEntity, deletedEntityID); - } - - // flag any updated or created entities so that we send changes for them next frame - for (auto& entityID : updatedEntityIDs) { - _clientTraitsHandler->markInstancedTraitUpdated(AvatarTraits::AvatarEntity, entityID); - } - } - - + // overridden where needed + // avatarEntityData is expected to be a map of QByteArrays + // each QByteArray represents an EntityItemProperties object from JavaScript } -AvatarEntityIDs AvatarData::getAndClearRecentlyDetachedIDs() { +void AvatarData::insertRemovedEntityID(const QUuid entityID) { + _avatarEntitiesLock.withWriteLock([&] { + _avatarEntityRemoved.insert(entityID); + }); + _avatarEntityDataChanged = true; +} + +AvatarEntityIDs AvatarData::getAndClearRecentlyRemovedIDs() { AvatarEntityIDs result; _avatarEntitiesLock.withWriteLock([&] { - result = _avatarEntityDetached; - _avatarEntityDetached.clear(); + result = _avatarEntityRemoved; + _avatarEntityRemoved.clear(); }); return result; } diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index b42c387f61..ec5ea4a5d0 100644 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -63,6 +63,7 @@ using AvatarWeakPointer = std::weak_ptr; using AvatarHash = QHash; using AvatarEntityMap = QMap; +using PackedAvatarEntityMap = QMap; // similar to AvatarEntityMap, but different internal format using AvatarEntityIDs = QSet; using AvatarGrabDataMap = QMap; @@ -71,6 +72,8 @@ using AvatarGrabMap = QMap; using AvatarDataSequenceNumber = uint16_t; +const int MAX_NUM_AVATAR_ENTITIES = 42; + // avatar motion behaviors const quint32 AVATAR_MOTION_ACTION_MOTOR_ENABLED = 1U << 0; const quint32 AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED = 1U << 1; @@ -952,19 +955,20 @@ public: // FIXME: Can this name be improved? Can it be deprecated? Q_INVOKABLE virtual void setAttachmentsVariant(const QVariantList& variant); + virtual void storeAvatarEntityDataPayload(const QUuid& entityID, const QByteArray& payload); /**jsdoc * @function MyAvatar.updateAvatarEntity * @param {Uuid} entityID * @param {string} entityData */ - Q_INVOKABLE void updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData); + Q_INVOKABLE virtual void updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData); /**jsdoc * @function MyAvatar.clearAvatarEntity * @param {Uuid} entityID */ - Q_INVOKABLE void clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFromTree = true); + Q_INVOKABLE virtual void clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFromTree = true); /**jsdoc @@ -1125,6 +1129,7 @@ public: TransformPointer getRecordingBasis() const; void setRecordingBasis(TransformPointer recordingBasis = TransformPointer()); void createRecordingIDs(); + virtual void avatarEntityDataToJson(QJsonObject& root) const; QJsonObject toJson() const; void fromJson(const QJsonObject& json, bool useFrameSkeleton = true); @@ -1136,17 +1141,16 @@ public: * @function MyAvatar.getAvatarEntityData * @returns {object} */ - Q_INVOKABLE AvatarEntityMap getAvatarEntityData() const; + Q_INVOKABLE virtual AvatarEntityMap getAvatarEntityData() const; /**jsdoc * @function MyAvatar.setAvatarEntityData * @param {object} avatarEntityData */ - Q_INVOKABLE void setAvatarEntityData(const AvatarEntityMap& avatarEntityData); + Q_INVOKABLE virtual void setAvatarEntityData(const AvatarEntityMap& avatarEntityData); virtual void setAvatarEntityDataChanged(bool value) { _avatarEntityDataChanged = value; } - void insertDetachedEntityID(const QUuid entityID); - AvatarEntityIDs getAndClearRecentlyDetachedIDs(); + AvatarEntityIDs getAndClearRecentlyRemovedIDs(); /**jsdoc * @function MyAvatar.getSensorToWorldMatrix @@ -1333,6 +1337,7 @@ public slots: void resetLastSent() { _lastToByteArray = 0; } protected: + void insertRemovedEntityID(const QUuid entityID); void lazyInitHeadData() const; float getDistanceBasedMinRotationDOT(glm::vec3 viewerPosition) const; @@ -1461,9 +1466,9 @@ protected: AABox _defaultBubbleBox; mutable ReadWriteLockable _avatarEntitiesLock; - AvatarEntityIDs _avatarEntityDetached; // recently detached from this avatar + AvatarEntityIDs _avatarEntityRemoved; // recently removed AvatarEntity ids AvatarEntityIDs _avatarEntityForRecording; // create new entities id for avatar recording - AvatarEntityMap _avatarEntityData; + PackedAvatarEntityMap _packedAvatarEntityData; bool _avatarEntityDataChanged { false }; mutable ReadWriteLockable _avatarGrabsLock; diff --git a/libraries/entities/src/EntityEditPacketSender.cpp b/libraries/entities/src/EntityEditPacketSender.cpp index c414a7a4ac..8b051ef37e 100644 --- a/libraries/entities/src/EntityEditPacketSender.cpp +++ b/libraries/entities/src/EntityEditPacketSender.cpp @@ -39,13 +39,12 @@ void EntityEditPacketSender::adjustEditPacketForClockSkew(PacketType type, QByte } } -void EntityEditPacketSender::queueEditAvatarEntityMessage(PacketType type, - EntityTreePointer entityTree, +void EntityEditPacketSender::queueEditAvatarEntityMessage(EntityTreePointer entityTree, EntityItemID entityItemID, const EntityItemProperties& properties) { assert(_myAvatar); if (!entityTree) { - qCDebug(entities) << "EntityEditPacketSender::queueEditEntityMessage null entityTree."; + qCDebug(entities) << "EntityEditPacketSender::queueEditAvatarEntityMessage null entityTree."; return; } EntityItemPointer entity = entityTree->findEntityByEntityItemID(entityItemID); @@ -53,33 +52,27 @@ void EntityEditPacketSender::queueEditAvatarEntityMessage(PacketType type, qCDebug(entities) << "EntityEditPacketSender::queueEditAvatarEntityMessage can't find entity: " << entityItemID; return; } + entity->setLastBroadcast(usecTimestampNow()); - // the properties that get serialized into the avatar identity packet should be the entire set + // serialize ALL properties in an "AvatarEntity" packet // rather than just the ones being edited. EntityItemProperties entityProperties = entity->getProperties(); entityProperties.merge(properties); - std::lock_guard lock(_mutex); - QScriptValue scriptProperties = EntityItemNonDefaultPropertiesToScriptValue(&_scriptEngine, entityProperties); - QVariant variantProperties = scriptProperties.toVariant(); - QJsonDocument jsonProperties = QJsonDocument::fromVariant(variantProperties); + OctreePacketData packetData(false, AvatarTraits::MAXIMUM_TRAIT_SIZE); + EncodeBitstreamParams params; + EntityTreeElementExtraEncodeDataPointer extra { nullptr }; + OctreeElement::AppendState appendState = entity->appendEntityData(&packetData, params, extra); - // 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()) == _myAvatar->getID()) { - jsonObject["parentID"] = AVATAR_SELF_ID.toString(); - } + if (appendState != OctreeElement::COMPLETED) { + // this entity's payload is too big + return; } - jsonProperties = QJsonDocument(jsonObject); - QByteArray binaryProperties = jsonProperties.toBinaryData(); - _myAvatar->updateAvatarEntity(entityItemID, binaryProperties); - - entity->setLastBroadcast(usecTimestampNow()); + QByteArray tempArray((const char*)packetData.getUncompressedData(), packetData.getUncompressedSize()); + _myAvatar->storeAvatarEntityDataPayload(entityItemID, tempArray); } - void EntityEditPacketSender::queueEditEntityMessage(PacketType type, EntityTreePointer entityTree, EntityItemID entityItemID, @@ -89,7 +82,7 @@ void EntityEditPacketSender::queueEditEntityMessage(PacketType type, qCWarning(entities) << "Suppressing entity edit message: cannot send avatar entity edit with no myAvatar"; } else if (properties.getOwningAvatarID() == _myAvatar->getID()) { // this is an avatar-based entity --> update our avatar-data rather than sending to the entity-server - queueEditAvatarEntityMessage(type, entityTree, entityItemID, properties); + queueEditAvatarEntityMessage(entityTree, entityItemID, properties); } else { qCWarning(entities) << "Suppressing entity edit message: cannot send avatar entity edit for another avatar"; } diff --git a/libraries/entities/src/EntityEditPacketSender.h b/libraries/entities/src/EntityEditPacketSender.h index 9bf9095f7f..a4ec2c45f9 100644 --- a/libraries/entities/src/EntityEditPacketSender.h +++ b/libraries/entities/src/EntityEditPacketSender.h @@ -50,7 +50,7 @@ public slots: void processEntityEditNackPacket(QSharedPointer message, SharedNodePointer sendingNode); private: - void queueEditAvatarEntityMessage(PacketType type, EntityTreePointer entityTree, + void queueEditAvatarEntityMessage(EntityTreePointer entityTree, EntityItemID entityItemID, const EntityItemProperties& properties); private: diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 26bb450b2e..3b1e9a3e2b 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "EntitiesLogging.h" #include "EntityItem.h" @@ -90,6 +91,16 @@ void EntityItemProperties::setLastEdited(quint64 usecTime) { _lastEdited = usecTime > _created ? usecTime : _created; } +bool EntityItemProperties::constructFromBuffer(const unsigned char* data, int dataLength) { + ReadBitstreamToTreeParams args; + EntityItemPointer tempEntity = EntityTypes::constructEntityItem(data, dataLength); + if (!tempEntity) { + return false; + } + tempEntity->readEntityDataFromBuffer(data, dataLength, args); + (*this) = tempEntity->getProperties(); + return true; +} QHash stringToShapeTypeLookup; @@ -2023,6 +2034,18 @@ void EntityItemProperties::copyFromScriptValue(const QScriptValue& object, bool _lastEdited = usecTimestampNow(); } +void EntityItemProperties::copyFromJSONString(QScriptEngine& scriptEngine, const QString& jsonString) { + // DANGER: this method is expensive + QJsonDocument propertiesDoc = QJsonDocument::fromJson(jsonString.toUtf8()); + QJsonObject propertiesObj = propertiesDoc.object(); + QVariant propertiesVariant(propertiesObj); + QVariantMap propertiesMap = propertiesVariant.toMap(); + QScriptValue propertiesScriptValue = variantMapToScriptValue(propertiesMap, scriptEngine); + bool honorReadOnly = true; + copyFromScriptValue(propertiesScriptValue, honorReadOnly); +} + + void EntityItemProperties::merge(const EntityItemProperties& other) { // Core COPY_PROPERTY_IF_CHANGED(simulationOwner); @@ -2252,7 +2275,6 @@ void EntityItemPropertiesFromScriptValueHonorReadOnly(const QScriptValue &object properties.copyFromScriptValue(object, true); } - QScriptValue EntityPropertyFlagsToScriptValue(QScriptEngine* engine, const EntityPropertyFlags& flags) { return EntityItemProperties::entityPropertyFlagsToScriptValue(engine, flags); } @@ -4590,6 +4612,40 @@ void EntityItemProperties::convertToCloneProperties(const EntityItemID& entityID setCloneAvatarEntity(ENTITY_ITEM_DEFAULT_CLONE_AVATAR_ENTITY); } +bool EntityItemProperties::blobToProperties(QScriptEngine& scriptEngine, const QByteArray& blob, EntityItemProperties& properties) { + // DANGER: this method is NOT efficient. + // 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) { + // DANGER: this method is NOT efficient. + // 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) { QString result = "[ "; diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index 17ec83be78..bb4d8c5878 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -98,6 +98,9 @@ class EntityItemProperties { friend class ZoneEntityItem; friend class MaterialEntityItem; 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()); virtual ~EntityItemProperties() = default; @@ -109,6 +112,7 @@ public: virtual QScriptValue copyToScriptValue(QScriptEngine* engine, bool skipDefaults, bool allowUnknownCreateTime = false, bool strictSemantics = false, EntityPsuedoPropertyFlags psueudoPropertyFlags = EntityPsuedoPropertyFlags()) const; virtual void copyFromScriptValue(const QScriptValue& object, bool honorReadOnly); + void copyFromJSONString(QScriptEngine& scriptEngine, const QString& jsonString); static QScriptValue entityPropertyFlagsToScriptValue(QScriptEngine* engine, const EntityPropertyFlags& flags); static void entityPropertyFlagsFromScriptValue(const QScriptValue& object, EntityPropertyFlags& flags); @@ -135,6 +139,8 @@ public: EntityPropertyFlags getDesiredProperties() { return _desiredProperties; } void setDesiredProperties(EntityPropertyFlags properties) { _desiredProperties = properties; } + bool constructFromBuffer(const unsigned char* data, int dataLength); + // Note: DEFINE_PROPERTY(PROP_FOO, Foo, foo, type, value) creates the following methods and variables: // type getFoo() const; // void setFoo(type); diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index 0957b226e9..fb1a11d43f 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -174,7 +174,7 @@ int EntityTree::readEntityDataFromBuffer(const unsigned char* data, int bytesLef addToNeedsParentFixupList(entity); } } else { - entity = EntityTypes::constructEntityItem(dataAt, bytesLeftToRead, args); + entity = EntityTypes::constructEntityItem(dataAt, bytesLeftToRead); if (entity) { bytesForThisEntity = entity->readEntityDataFromBuffer(dataAt, bytesLeftToRead, args); @@ -490,7 +490,6 @@ bool EntityTree::updateEntity(EntityItemPointer entity, const EntityItemProperti } EntityItemPointer EntityTree::addEntity(const EntityItemID& entityID, const EntityItemProperties& properties, bool isClone) { - EntityItemPointer result = NULL; EntityItemProperties props = properties; auto nodeList = DependencyManager::get(); @@ -517,12 +516,12 @@ EntityItemPointer EntityTree::addEntity(const EntityItemID& entityID, const Enti if (containingElement) { qCWarning(entities) << "EntityTree::addEntity() on existing entity item with entityID=" << entityID << "containingElement=" << containingElement.get(); - return result; + return nullptr; } // construct the instance of the entity EntityTypes::EntityType type = props.getType(); - result = EntityTypes::constructEntityItem(type, entityID, props); + EntityItemPointer result = EntityTypes::constructEntityItem(type, entityID, props); if (result) { if (recordCreationTime) { @@ -531,10 +530,6 @@ EntityItemPointer EntityTree::addEntity(const EntityItemID& entityID, const Enti // Recurse the tree and store the entity in the correct tree element AddEntityOperator theOperator(getThisPointer(), result); recurseTreeWithOperator(&theOperator); - if (!result->getParentID().isNull()) { - addToNeedsParentFixupList(result); - } - postAddEntity(result); } return result; @@ -2969,27 +2964,30 @@ void EntityTree::updateEntityQueryAACubeWorker(SpatiallyNestablePointer object, MovingEntitiesOperator& moveOperator, bool force, bool tellServer) { // if the queryBox has changed, tell the entity-server EntityItemPointer entity = std::dynamic_pointer_cast(object); - if (entity && (entity->updateQueryAACube() || force)) { - bool success; - AACube newCube = entity->getQueryAACube(success); - if (success) { - moveOperator.addEntityToMoveList(entity, newCube); - } - // send an edit packet to update the entity-server about the queryAABox. We do this for domain-hosted - // entities as well as for avatar-entities; the packet-sender will route the update accordingly - if (tellServer && packetSender && (entity->isDomainEntity() || entity->isAvatarEntity())) { - quint64 now = usecTimestampNow(); - EntityItemProperties properties = entity->getProperties(); - properties.setQueryAACubeDirty(); - properties.setLocationDirty(); - properties.setLastEdited(now); + if (entity) { + bool tellServerThis = tellServer && (entity->getEntityHostType() != entity::HostType::AVATAR); + if ((entity->updateQueryAACube() || force)) { + bool success; + AACube newCube = entity->getQueryAACube(success); + if (success) { + moveOperator.addEntityToMoveList(entity, newCube); + } + // send an edit packet to update the entity-server about the queryAABox. We do this for domain-hosted + // entities as well as for avatar-entities; the packet-sender will route the update accordingly + if (tellServerThis && packetSender && (entity->isDomainEntity() || entity->isAvatarEntity())) { + quint64 now = usecTimestampNow(); + EntityItemProperties properties = entity->getProperties(); + properties.setQueryAACubeDirty(); + properties.setLocationDirty(); + properties.setLastEdited(now); - packetSender->queueEditEntityMessage(PacketType::EntityEdit, getThisPointer(), entity->getID(), properties); - entity->setLastBroadcast(now); // for debug/physics status icons - } + packetSender->queueEditEntityMessage(PacketType::EntityEdit, getThisPointer(), entity->getID(), properties); + entity->setLastBroadcast(now); // for debug/physics status icons + } - entity->markDirtyFlags(Simulation::DIRTY_POSITION); - entityChanged(entity); + entity->markDirtyFlags(Simulation::DIRTY_POSITION); + entityChanged(entity); + } } object->forEachDescendant([&](SpatiallyNestablePointer descendant) { diff --git a/libraries/entities/src/EntityTypes.cpp b/libraries/entities/src/EntityTypes.cpp index e511af83b0..ad078190dd 100644 --- a/libraries/entities/src/EntityTypes.cpp +++ b/libraries/entities/src/EntityTypes.cpp @@ -58,6 +58,10 @@ REGISTER_ENTITY_TYPE(Light) REGISTER_ENTITY_TYPE(Zone) REGISTER_ENTITY_TYPE(Material) +bool EntityTypes::typeIsValid(EntityType type) { + return type > EntityType::Unknown && type <= EntityType::NUM_TYPES; +} + const QString& EntityTypes::getEntityTypeName(EntityType entityType) { QMap::iterator matchedTypeName = _typeToNameMap.find(entityType); if (matchedTypeName != _typeToNameMap.end()) { @@ -107,8 +111,7 @@ EntityItemPointer EntityTypes::constructEntityItem(EntityType entityType, const return newEntityItem; } -EntityItemPointer EntityTypes::constructEntityItem(const unsigned char* data, int bytesToRead, - ReadBitstreamToTreeParams& args) { +void EntityTypes::extractEntityTypeAndID(const unsigned char* data, int dataLength, EntityTypes::EntityType& typeOut, QUuid& idOut) { // Header bytes // object ID [16 bytes] @@ -119,28 +122,36 @@ EntityItemPointer EntityTypes::constructEntityItem(const unsigned char* data, in // ~27-35 bytes... const int MINIMUM_HEADER_BYTES = 27; - int bytesRead = 0; - if (bytesToRead >= MINIMUM_HEADER_BYTES) { - int originalLength = bytesToRead; - QByteArray originalDataBuffer((const char*)data, originalLength); + if (dataLength >= MINIMUM_HEADER_BYTES) { + int bytesRead = 0; + QByteArray originalDataBuffer = QByteArray::fromRawData((const char*)data, dataLength); // id QByteArray encodedID = originalDataBuffer.mid(bytesRead, NUM_BYTES_RFC4122_UUID); // maximum possible size - QUuid actualID = QUuid::fromRfc4122(encodedID); + idOut = QUuid::fromRfc4122(encodedID); bytesRead += encodedID.size(); // type QByteArray encodedType = originalDataBuffer.mid(bytesRead); // maximum possible size ByteCountCoded typeCoder = encodedType; encodedType = typeCoder; // determine true length - bytesRead += encodedType.size(); quint32 type = typeCoder; - EntityTypes::EntityType entityType = (EntityTypes::EntityType)type; - - EntityItemID tempEntityID(actualID); - EntityItemProperties tempProperties; - return constructEntityItem(entityType, tempEntityID, tempProperties); + typeOut = (EntityTypes::EntityType)type; } - - return NULL; +} + +EntityItemPointer EntityTypes::constructEntityItem(const unsigned char* data, int bytesToRead) { + QUuid id; + EntityTypes::EntityType type = EntityTypes::Unknown; + extractEntityTypeAndID(data, bytesToRead, type, id); + if (type > EntityTypes::Unknown && type <= EntityTypes::NUM_TYPES) { + EntityItemID tempEntityID(id); + EntityItemProperties tempProperties; + return constructEntityItem(type, tempEntityID, tempProperties); + } + return nullptr; +} + +EntityItemPointer EntityTypes::constructEntityItem(const QUuid& id, const EntityItemProperties& properties) { + return constructEntityItem(properties.getType(), id, properties); } diff --git a/libraries/entities/src/EntityTypes.h b/libraries/entities/src/EntityTypes.h index 29a695718e..2e8914c8a7 100644 --- a/libraries/entities/src/EntityTypes.h +++ b/libraries/entities/src/EntityTypes.h @@ -109,11 +109,14 @@ public: NUM_TYPES } EntityType; + static bool typeIsValid(EntityType type); static const QString& getEntityTypeName(EntityType entityType); static EntityTypes::EntityType getEntityTypeFromName(const QString& name); static bool registerEntityType(EntityType entityType, const char* name, EntityTypeFactory factoryMethod); + static void extractEntityTypeAndID(const unsigned char* data, int dataLength, EntityTypes::EntityType& typeOut, QUuid& idOut); static EntityItemPointer constructEntityItem(EntityType entityType, const EntityItemID& entityID, const EntityItemProperties& properties); - static EntityItemPointer constructEntityItem(const unsigned char* data, int bytesToRead, ReadBitstreamToTreeParams& args); + static EntityItemPointer constructEntityItem(const unsigned char* data, int bytesToRead); + static EntityItemPointer constructEntityItem(const QUuid& id, const EntityItemProperties& properties); private: static QMap _typeToNameMap; diff --git a/libraries/networking/src/udt/PacketHeaders.cpp b/libraries/networking/src/udt/PacketHeaders.cpp index aba3822883..642914cd56 100644 --- a/libraries/networking/src/udt/PacketHeaders.cpp +++ b/libraries/networking/src/udt/PacketHeaders.cpp @@ -41,7 +41,7 @@ PacketVersion versionForPacketType(PacketType packetType) { return static_cast(AvatarMixerPacketVersion::CollisionFlag); case PacketType::BulkAvatarData: case PacketType::KillAvatar: - return static_cast(AvatarMixerPacketVersion::GrabTraits); + return static_cast(AvatarMixerPacketVersion::FasterAvatarEntities); case PacketType::MessagesData: return static_cast(MessageDataVersion::TextOrBinaryData); // ICE packets diff --git a/libraries/networking/src/udt/PacketHeaders.h b/libraries/networking/src/udt/PacketHeaders.h index 6f019f39b6..f53a287d71 100644 --- a/libraries/networking/src/udt/PacketHeaders.h +++ b/libraries/networking/src/udt/PacketHeaders.h @@ -311,7 +311,8 @@ enum class AvatarMixerPacketVersion : PacketVersion { JointTransScaled, GrabTraits, CollisionFlag, - AvatarTraitsAck + AvatarTraitsAck, + FasterAvatarEntities }; enum class DomainConnectRequestVersion : PacketVersion { diff --git a/libraries/octree/src/OctreePacketData.cpp b/libraries/octree/src/OctreePacketData.cpp index 6c0bba5ec6..fd57f2fa3a 100644 --- a/libraries/octree/src/OctreePacketData.cpp +++ b/libraries/octree/src/OctreePacketData.cpp @@ -38,7 +38,11 @@ void OctreePacketData::changeSettings(bool enableCompression, unsigned int targe _enableCompression = enableCompression; _targetSize = targetSize; _uncompressedByteArray.resize(_targetSize); - _compressedByteArray.resize(_targetSize); + if (_enableCompression) { + _compressedByteArray.resize(_targetSize); + } else { + _compressedByteArray.resize(0); + } _uncompressed = (unsigned char*)_uncompressedByteArray.data(); _compressed = (unsigned char*)_compressedByteArray.data(); @@ -586,13 +590,10 @@ bool OctreePacketData::appendRawData(QByteArray data) { AtomicUIntStat OctreePacketData::_compressContentTime { 0 }; AtomicUIntStat OctreePacketData::_compressContentCalls { 0 }; -bool OctreePacketData::compressContent() { +bool OctreePacketData::compressContent() { PerformanceWarning warn(false, "OctreePacketData::compressContent()", false, &_compressContentTime, &_compressContentCalls); - - // without compression, we always pass... - if (!_enableCompression) { - return true; - } + assert(_dirty); + assert(_enableCompression); _bytesInUseLastCheck = _bytesInUse; @@ -605,13 +606,13 @@ bool OctreePacketData::compressContent() { QByteArray compressedData = qCompress(uncompressedData, uncompressedSize, MAX_COMPRESSION); - if (compressedData.size() < (int)MAX_OCTREE_PACKET_DATA_SIZE) { + if (compressedData.size() < _compressedByteArray.size()) { _compressedBytes = compressedData.size(); memcpy(_compressed, compressedData.constData(), _compressedBytes); _dirty = false; success = true; } else { - qCWarning(octree) << "OctreePacketData::compressContent -- compressedData.size >= MAX_OCTREE_PACKET_DATA_SIZE"; + qCWarning(octree) << "OctreePacketData::compressContent -- compressedData.size >= " << _compressedByteArray.size(); assert(false); } return success; @@ -644,8 +645,7 @@ void OctreePacketData::loadFinalizedContent(const unsigned char* data, int lengt memcpy(_uncompressed, uncompressedData.constData(), _bytesInUse); } else { memcpy(_uncompressed, data, length); - memcpy(_compressed, data, length); - _bytesInUse = _compressedBytes = length; + _bytesInUse = length; } } else { if (_debug) {