add EntityItemProperties::copyFromJSONString()

This commit is contained in:
Andrew Meadows 2018-12-07 13:49:48 -08:00
parent ec384d7dbb
commit fe2ee68b79
4 changed files with 32 additions and 25 deletions

View file

@ -50,7 +50,6 @@
#include <RecordingScriptingInterface.h>
#include <trackers/FaceTracker.h>
#include <RenderableModelEntityItem.h>
#include <VariantMapToScriptValue.h>
#include "MyHead.h"
#include "MySkeletonModel.h"
@ -1305,14 +1304,14 @@ void MyAvatar::saveData() {
void MyAvatar::saveAvatarEntityDataToSettings() {
// save new settings
uint32_t numEntities = _avatarEntitiesAsPropertiesStrings.size();
uint32_t numEntities = _avatarEntityStrings.size();
_avatarEntityCountSetting.set(numEntities);
uint32_t prevNumEntities = _avatarEntityCountSetting.get(0);
resizeAvatarEntitySettingHandles(std::max<uint32_t>(numEntities, prevNumEntities));
if (numEntities > 0) {
_avatarEntitiesLock.withReadLock([&] {
uint32_t i = 0;
for (const auto& mapEntry : _avatarEntitiesAsPropertiesStrings) {
for (const auto& mapEntry : _avatarEntityStrings) {
_avatarEntityDataSettings[i].set(mapEntry.second);
_avatarEntityIDSettings[i].set(mapEntry.first.toString());
++i;
@ -1430,7 +1429,13 @@ void MyAvatar::setEnableInverseKinematics(bool isEnabled) {
_skeletonModel->getRig().setEnableInverseKinematics(isEnabled);
}
void MyAvatar::updateAvatarEntity(const QUuid& entityID, const EntityItemProperties& properties) {
void MyAvatar::updateAvatarEntity(const QUuid& entityID, const QString& entityPropertiesString) {
/* TODO: implement this so JS can add/update (and delete?) AvatarEntitieskj:w
// convert string to properties
// NOTE: this path from EntityItemProperties JSON string to EntityItemProperties is NOT efficient
EntityItemProperties properties;
properties.copyFromJSONString(scriptEngine, entityPropertiesString);
auto entityTreeRenderer = qApp->getEntities();
EntityTreePointer entityTree = entityTreeRenderer ? entityTreeRenderer->getTree() : nullptr;
EntityItemPointer entity;
@ -1460,12 +1465,9 @@ void MyAvatar::updateAvatarEntity(const QUuid& entityID, const EntityItemPropert
return;
}
//QByteArray tempArray = QByteArray::fromRawData((const char*)packetData.getUncompressedData(), packetData.getUncompressedSize());
QByteArray tempArray((const char*)packetData.getUncompressedData(), packetData.getUncompressedSize());
for (int i = 0; i < 4; ++i) {
tempArray[i] = (uint8_t)(0xff);
}
QByteArray tempArray = QByteArray::fromRawData((const char*)packetData.getUncompressedData(), packetData.getUncompressedSize());
storeAvatarEntityDataPayload(entity->getID(), tempArray);
*/
}
void MyAvatar::updateAvatarEntities() {
@ -1558,22 +1560,14 @@ void MyAvatar::loadAvatarEntityDataFromSettings() {
entitiesToLoad.resize(numEntities);
resizeAvatarEntitySettingHandles(numEntities);
for (int i = 0; i < numEntities; i++) {
// NOTE: this path from EntityItemProperties JSON string to EntityItemProperties is NOT efficient
QString propertiesString = _avatarEntityDataSettings[i].get();
QJsonDocument propertiesDoc = QJsonDocument::fromJson(propertiesString.toUtf8());
QJsonObject propertiesObj = propertiesDoc.object();
QVariant propertiesVariant(propertiesObj);
QVariantMap propertiesMap = propertiesVariant.toMap();
QScriptValue propertiesScriptValue = variantMapToScriptValue(propertiesMap, scriptEngine);
// NOTE: we grab properties by reference and wrangle it:
// DANGER: this JSONString --> EntityItemProperties operation is expensive
EntityItemProperties& properties = entitiesToLoad[i];
EntityItemPropertiesFromScriptValueIgnoreReadOnly(propertiesScriptValue, properties);
properties.copyFromJSONString(scriptEngine, _avatarEntityDataSettings[i].get());
// the ClientOnly property can get stripped out elsewhere so we need to always set it true here
properties.setClientOnly(true);
}
_avatarEntitiesAsPropertiesStrings.clear();
_avatarEntityStrings.clear();
auto entityTreeRenderer = qApp->getEntities();
EntityTreePointer entityTree = entityTreeRenderer ? entityTreeRenderer->getTree() : nullptr;
if (entityTree) {
@ -1592,7 +1586,7 @@ void MyAvatar::loadAvatarEntityDataFromSettings() {
// only remember an AvatarEntity that successfully loads and can be packed
QByteArray tempArray = QByteArray::fromRawData((const char*)packetData.getUncompressedData(), packetData.getUncompressedSize());
storeAvatarEntityDataPayload(entityID, tempArray);
_avatarEntitiesAsPropertiesStrings[entityID] = _avatarEntityDataSettings[i].get();
_avatarEntityStrings[entityID] = _avatarEntityDataSettings[i].get();
}
packetData.reset();
}

View file

@ -1407,8 +1407,7 @@ public slots:
*/
bool getEnableMeshVisible() const override;
// TODO: make this invokable, probably also move down to AvatarData
void updateAvatarEntity(const QUuid& entityID, const EntityItemProperties& properties);
void updateAvatarEntity(const QUuid& entityID, const QString& entityPropertiesString) override;
/**jsdoc
* Set whether or not your avatar mesh is visible.
@ -1947,7 +1946,8 @@ private:
Setting::Handle<bool> _allowTeleportingSetting { "allowTeleporting", true };
std::vector<Setting::Handle<QString>> _avatarEntityIDSettings;
std::vector<Setting::Handle<QString>> _avatarEntityDataSettings;
std::map<QUuid, QString> _avatarEntitiesAsPropertiesStrings;
std::map<QUuid, QString> _avatarEntityStrings;
std::map<QUuid, QString> _avatarEntityProperties;
};
QScriptValue audioListenModeToScriptValue(QScriptEngine* engine, const AudioListenerMode& audioListenerMode);

View file

@ -28,6 +28,7 @@
#include <GLMHelpers.h>
#include <RegisteredMetaTypes.h>
#include <Extents.h>
#include <VariantMapToScriptValue.h>
#include "EntitiesLogging.h"
#include "EntityItem.h"
@ -2033,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);
@ -2262,7 +2275,6 @@ void EntityItemPropertiesFromScriptValueHonorReadOnly(const QScriptValue &object
properties.copyFromScriptValue(object, true);
}
QScriptValue EntityPropertyFlagsToScriptValue(QScriptEngine* engine, const EntityPropertyFlags& flags) {
return EntityItemProperties::entityPropertyFlagsToScriptValue(engine, flags);
}

View file

@ -109,6 +109,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);