From 10d581925cccab1d3cd3c9008ef9a6d1b9c70ef6 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 2 Sep 2014 10:31:26 -0700 Subject: [PATCH] DRY up the property reading code by using macro for repeated pattern --- libraries/entities/src/BoxEntityItem.cpp | 11 +- libraries/entities/src/EntityItem.cpp | 115 +------------ .../entities/src/EntityItemProperties.cpp | 155 ++---------------- libraries/entities/src/EntityItemProperties.h | 86 +++++++++- libraries/entities/src/ModelEntityItem.cpp | 79 +-------- libraries/entities/src/SphereEntityItem.cpp | 11 +- 6 files changed, 117 insertions(+), 340 deletions(-) diff --git a/libraries/entities/src/BoxEntityItem.cpp b/libraries/entities/src/BoxEntityItem.cpp index f120a29245..c683a71562 100644 --- a/libraries/entities/src/BoxEntityItem.cpp +++ b/libraries/entities/src/BoxEntityItem.cpp @@ -77,16 +77,7 @@ int BoxEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, i int bytesRead = 0; const unsigned char* dataAt = data; - // PROP_COLOR - if (propertyFlags.getHasProperty(PROP_COLOR)) { - rgbColor color; - if (overwriteLocalData) { - memcpy(_color, dataAt, sizeof(_color)); - } - dataAt += sizeof(color); - bytesRead += sizeof(color); - } - + READ_ENTITY_PROPERTY_COLOR(PROP_COLOR, _color); return bytesRead; } diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index a0928455c7..16b7e42a08 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -365,112 +365,15 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef dataAt += propertyFlags.getEncodedLength(); bytesRead += propertyFlags.getEncodedLength(); - - - // PROP_POSITION - if (propertyFlags.getHasProperty(PROP_POSITION)) { - glm::vec3 positionFromBuffer; - memcpy(&positionFromBuffer, dataAt, sizeof(positionFromBuffer)); - dataAt += sizeof(positionFromBuffer); - bytesRead += sizeof(positionFromBuffer); - if (overwriteLocalData) { - _position = positionFromBuffer; - } - } - - // PROP_RADIUS - if (propertyFlags.getHasProperty(PROP_RADIUS)) { - float radiusFromBuffer; - memcpy(&radiusFromBuffer, dataAt, sizeof(radiusFromBuffer)); - dataAt += sizeof(radiusFromBuffer); - bytesRead += sizeof(radiusFromBuffer); - if (overwriteLocalData) { - _radius = radiusFromBuffer; - } - } - - // PROP_ROTATION - if (propertyFlags.getHasProperty(PROP_ROTATION)) { - glm::quat rotation; - int bytes = unpackOrientationQuatFromBytes(dataAt, rotation); - dataAt += bytes; - bytesRead += bytes; - if (overwriteLocalData) { - _rotation = rotation; - } - } - - // PROP_MASS, - if (propertyFlags.getHasProperty(PROP_MASS)) { - float value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - bytesRead += sizeof(value); - if (overwriteLocalData) { - _mass = value; - } - } - - // PROP_VELOCITY, - if (propertyFlags.getHasProperty(PROP_VELOCITY)) { - glm::vec3 value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - bytesRead += sizeof(value); - if (overwriteLocalData) { - _velocity = value; - } - } - - // PROP_GRAVITY, - if (propertyFlags.getHasProperty(PROP_GRAVITY)) { - glm::vec3 value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - bytesRead += sizeof(value); - if (overwriteLocalData) { - _gravity = value; - } - } - - // PROP_DAMPING, - if (propertyFlags.getHasProperty(PROP_DAMPING)) { - - float value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - bytesRead += sizeof(value); - - if (overwriteLocalData) { - _damping = value; - } - } - - // PROP_LIFETIME, - if (propertyFlags.getHasProperty(PROP_LIFETIME)) { - float value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - bytesRead += sizeof(value); - if (overwriteLocalData) { - _lifetime = value; - } - } - - // PROP_SCRIPT, - if (propertyFlags.getHasProperty(PROP_SCRIPT)) { - // TODO: fix to new format... - uint16_t length; - memcpy(&length, dataAt, sizeof(length)); - dataAt += sizeof(length); - bytesRead += sizeof(length); - QString value((const char*)dataAt); - dataAt += length; - bytesRead += length; - if (overwriteLocalData) { - setScript(value); - } - } + READ_ENTITY_PROPERTY(PROP_POSITION, glm::vec3, _position); + READ_ENTITY_PROPERTY(PROP_RADIUS, float, _radius); + READ_ENTITY_PROPERTY_QUAT(PROP_ROTATION, _rotation); + READ_ENTITY_PROPERTY(PROP_MASS, float, _mass); + READ_ENTITY_PROPERTY(PROP_VELOCITY, glm::vec3, _velocity); + READ_ENTITY_PROPERTY(PROP_GRAVITY, glm::vec3, _gravity); + READ_ENTITY_PROPERTY(PROP_DAMPING, float, _damping); + READ_ENTITY_PROPERTY(PROP_LIFETIME, float, _lifetime); + READ_ENTITY_PROPERTY_STRING(PROP_SCRIPT,setScript); bytesRead += readEntitySubclassDataFromBuffer(dataAt, (bytesLeftToRead - bytesRead), args, propertyFlags, overwriteLocalData); diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 3e273b21c0..536dcea993 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -719,147 +719,22 @@ bool EntityItemProperties::decodeEntityEditPacket(const unsigned char* data, int EntityPropertyFlags propertyFlags = encodedPropertyFlags; dataAt += propertyFlags.getEncodedLength(); processedBytes += propertyFlags.getEncodedLength(); - - // PROP_POSITION - if (propertyFlags.getHasProperty(PROP_POSITION)) { - glm::vec3 position; - memcpy(&position, dataAt, sizeof(position)); - dataAt += sizeof(position); - processedBytes += sizeof(position); - properties.setPosition(position); - } - - // PROP_RADIUS - if (propertyFlags.getHasProperty(PROP_RADIUS)) { - float radius; - memcpy(&radius, dataAt, sizeof(radius)); - dataAt += sizeof(radius); - processedBytes += sizeof(radius); - properties.setRadius(radius); - } - // PROP_ROTATION - if (propertyFlags.getHasProperty(PROP_ROTATION)) { - glm::quat rotation; - int bytes = unpackOrientationQuatFromBytes(dataAt, rotation); - dataAt += bytes; - processedBytes += bytes; - properties.setRotation(rotation); - } - - // PROP_MASS, - if (propertyFlags.getHasProperty(PROP_MASS)) { - float value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - processedBytes += sizeof(value); - properties.setMass(value); - } - - // PROP_VELOCITY, - if (propertyFlags.getHasProperty(PROP_VELOCITY)) { - glm::vec3 value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - processedBytes += sizeof(value); - properties.setVelocity(value); - } - - // PROP_GRAVITY, - if (propertyFlags.getHasProperty(PROP_GRAVITY)) { - glm::vec3 value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - processedBytes += sizeof(value); - properties.setGravity(value); - } - - // PROP_DAMPING, - if (propertyFlags.getHasProperty(PROP_DAMPING)) { - float value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - processedBytes += sizeof(value); - properties.setDamping(value); - } - - // PROP_LIFETIME, - if (propertyFlags.getHasProperty(PROP_LIFETIME)) { - float value; - memcpy(&value, dataAt, sizeof(value)); - dataAt += sizeof(value); - processedBytes += sizeof(value); - properties.setLifetime(value); - } - - - // PROP_SCRIPT - // script would go here... - - - // PROP_COLOR - if (propertyFlags.getHasProperty(PROP_COLOR)) { - xColor color; - memcpy(&color, dataAt, sizeof(color)); - dataAt += sizeof(color); - processedBytes += sizeof(color); - properties.setColor(color); - } - - // PROP_MODEL_URL - if (propertyFlags.getHasProperty(PROP_MODEL_URL)) { - - // TODO: fix to new format... - uint16_t modelURLbytesToRead; - memcpy(&modelURLbytesToRead, dataAt, sizeof(modelURLbytesToRead)); - dataAt += sizeof(modelURLbytesToRead); - processedBytes += sizeof(modelURLbytesToRead); - QString modelURLString((const char*)dataAt); - dataAt += modelURLbytesToRead; - processedBytes += modelURLbytesToRead; - - properties.setModelURL(modelURLString); - } - - // PROP_ANIMATION_URL - if (propertyFlags.getHasProperty(PROP_ANIMATION_URL)) { - // animationURL - uint16_t animationURLbytesToRead; - memcpy(&animationURLbytesToRead, dataAt, sizeof(animationURLbytesToRead)); - dataAt += sizeof(animationURLbytesToRead); - processedBytes += sizeof(animationURLbytesToRead); - QString animationURLString((const char*)dataAt); - dataAt += animationURLbytesToRead; - processedBytes += animationURLbytesToRead; - properties.setAnimationURL(animationURLString); - } - - // PROP_ANIMATION_FPS - if (propertyFlags.getHasProperty(PROP_ANIMATION_FPS)) { - float animationFPS; - memcpy(&animationFPS, dataAt, sizeof(animationFPS)); - dataAt += sizeof(animationFPS); - processedBytes += sizeof(animationFPS); - properties.setAnimationFPS(animationFPS); - } - - // PROP_ANIMATION_FRAME_INDEX - if (propertyFlags.getHasProperty(PROP_ANIMATION_FRAME_INDEX)) { - float animationFrameIndex; // we keep this as a float and round to int only when we need the exact index - memcpy(&animationFrameIndex, dataAt, sizeof(animationFrameIndex)); - dataAt += sizeof(animationFrameIndex); - processedBytes += sizeof(animationFrameIndex); - properties.setAnimationFrameIndex(animationFrameIndex); - } - - // PROP_ANIMATION_PLAYING - if (propertyFlags.getHasProperty(PROP_ANIMATION_PLAYING)) { - bool animationIsPlaying; - memcpy(&animationIsPlaying, dataAt, sizeof(animationIsPlaying)); - dataAt += sizeof(animationIsPlaying); - processedBytes += sizeof(animationIsPlaying); - properties.setAnimationIsPlaying(animationIsPlaying); - } + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_POSITION, glm::vec3, setPosition); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_RADIUS, float, setRadius); + READ_ENTITY_PROPERTY_QUAT_TO_PROPERTIES(PROP_ROTATION, setRotation); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_MASS, float, setMass); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_VELOCITY, glm::vec3, setVelocity); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_GRAVITY, glm::vec3, setGravity); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_DAMPING, float, setDamping); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_LIFETIME, float, setLifetime); + //READ_ENTITY_PROPERTY_STRING(PROP_SCRIPT,setScript); // not yet supported by edit messages... + READ_ENTITY_PROPERTY_COLOR_TO_PROPERTIES(PROP_COLOR, setColor); + READ_ENTITY_PROPERTY_STRING_TO_PROPERTIES(PROP_MODEL_URL, setModelURL); + READ_ENTITY_PROPERTY_STRING_TO_PROPERTIES(PROP_ANIMATION_URL, setAnimationURL); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_ANIMATION_FPS, float, setAnimationFPS); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_ANIMATION_FRAME_INDEX, float, setAnimationFrameIndex); + READ_ENTITY_PROPERTY_TO_PROPERTIES(PROP_ANIMATION_PLAYING, bool, setAnimationIsPlaying); return valid; } diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index b6d67b7148..fc506ce10c 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -257,7 +257,6 @@ Q_DECLARE_METATYPE(EntityItemProperties); QScriptValue EntityItemPropertiesToScriptValue(QScriptEngine* engine, const EntityItemProperties& properties); void EntityItemPropertiesFromScriptValue(const QScriptValue &object, EntityItemProperties& properties); -// This macro is used in a couple of methods for appending data into an entity property stream #define APPEND_ENTITY_PROPERTY(P,O,V) \ if (requestedProperties.getHasProperty(P)) { \ LevelDetails propertyLevel = packetData->startLevel(); \ @@ -275,5 +274,90 @@ void EntityItemPropertiesFromScriptValue(const QScriptValue &object, EntityItemP propertiesDidntFit -= P; \ } +#define READ_ENTITY_PROPERTY(P,T,M) \ + if (propertyFlags.getHasProperty(P)) { \ + T fromBuffer; \ + memcpy(&fromBuffer, dataAt, sizeof(fromBuffer)); \ + dataAt += sizeof(fromBuffer); \ + bytesRead += sizeof(fromBuffer); \ + if (overwriteLocalData) { \ + M = fromBuffer; \ + } \ + } + +#define READ_ENTITY_PROPERTY_QUAT(P,M) \ + if (propertyFlags.getHasProperty(P)) { \ + glm::quat fromBuffer; \ + int bytes = unpackOrientationQuatFromBytes(dataAt, fromBuffer); \ + dataAt += bytes; \ + bytesRead += bytes; \ + if (overwriteLocalData) { \ + M = fromBuffer; \ + } \ + } + +#define READ_ENTITY_PROPERTY_STRING(P,O) \ + if (propertyFlags.getHasProperty(P)) { \ + uint16_t length; \ + memcpy(&length, dataAt, sizeof(length)); \ + dataAt += sizeof(length); \ + bytesRead += sizeof(length); \ + QString value((const char*)dataAt); \ + dataAt += length; \ + bytesRead += length; \ + if (overwriteLocalData) { \ + O(value); \ + } \ + } + +#define READ_ENTITY_PROPERTY_COLOR(P,M) \ + if (propertyFlags.getHasProperty(P)) { \ + if (overwriteLocalData) { \ + memcpy(M, dataAt, sizeof(M)); \ + } \ + dataAt += sizeof(rgbColor); \ + bytesRead += sizeof(rgbColor); \ + } + +#define READ_ENTITY_PROPERTY_TO_PROPERTIES(P,T,O) \ + if (propertyFlags.getHasProperty(P)) { \ + T fromBuffer; \ + memcpy(&fromBuffer, dataAt, sizeof(fromBuffer)); \ + dataAt += sizeof(fromBuffer); \ + processedBytes += sizeof(fromBuffer); \ + properties.O(fromBuffer); \ + } + +#define READ_ENTITY_PROPERTY_QUAT_TO_PROPERTIES(P,O) \ + if (propertyFlags.getHasProperty(P)) { \ + glm::quat fromBuffer; \ + int bytes = unpackOrientationQuatFromBytes(dataAt, fromBuffer); \ + dataAt += bytes; \ + processedBytes += bytes; \ + properties.O(fromBuffer); \ + } + +#define READ_ENTITY_PROPERTY_STRING_TO_PROPERTIES(P,O) \ + if (propertyFlags.getHasProperty(P)) { \ + uint16_t length; \ + memcpy(&length, dataAt, sizeof(length)); \ + dataAt += sizeof(length); \ + processedBytes += sizeof(length); \ + QString value((const char*)dataAt); \ + dataAt += length; \ + processedBytes += length; \ + properties.O(value); \ + } + +#define READ_ENTITY_PROPERTY_COLOR_TO_PROPERTIES(P,O) \ + if (propertyFlags.getHasProperty(P)) { \ + xColor color; \ + memcpy(&color, dataAt, sizeof(color)); \ + dataAt += sizeof(color); \ + processedBytes += sizeof(color); \ + properties.O(color); \ + } + + #endif // hifi_EntityItemProperties_h diff --git a/libraries/entities/src/ModelEntityItem.cpp b/libraries/entities/src/ModelEntityItem.cpp index 56c9c4d5b2..8cf89429ae 100644 --- a/libraries/entities/src/ModelEntityItem.cpp +++ b/libraries/entities/src/ModelEntityItem.cpp @@ -121,79 +121,12 @@ int ModelEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesRead = 0; const unsigned char* dataAt = data; - // PROP_COLOR - if (propertyFlags.getHasProperty(PROP_COLOR)) { - rgbColor color; - if (overwriteLocalData) { - memcpy(_color, dataAt, sizeof(_color)); - } - dataAt += sizeof(color); - bytesRead += sizeof(color); - } - - // PROP_MODEL_URL - if (propertyFlags.getHasProperty(PROP_MODEL_URL)) { - // TODO: fix to new format... - uint16_t modelURLLength; - memcpy(&modelURLLength, dataAt, sizeof(modelURLLength)); - dataAt += sizeof(modelURLLength); - bytesRead += sizeof(modelURLLength); - QString modelURLString((const char*)dataAt); - dataAt += modelURLLength; - bytesRead += modelURLLength; - - if (overwriteLocalData) { - setModelURL(modelURLString); - } - } - - // PROP_ANIMATION_URL - if (propertyFlags.getHasProperty(PROP_ANIMATION_URL)) { - // animationURL - uint16_t animationURLLength; - memcpy(&animationURLLength, dataAt, sizeof(animationURLLength)); - dataAt += sizeof(animationURLLength); - bytesRead += sizeof(animationURLLength); - QString animationURLString((const char*)dataAt); - dataAt += animationURLLength; - bytesRead += animationURLLength; - if (overwriteLocalData) { - setAnimationURL(animationURLString); - } - } - - // PROP_ANIMATION_FPS - if (propertyFlags.getHasProperty(PROP_ANIMATION_FPS)) { - float animationFPS; - memcpy(&animationFPS, dataAt, sizeof(animationFPS)); - dataAt += sizeof(animationFPS); - bytesRead += sizeof(animationFPS); - if (overwriteLocalData) { - _animationFPS = animationFPS; - } - } - - // PROP_ANIMATION_FRAME_INDEX - if (propertyFlags.getHasProperty(PROP_ANIMATION_FRAME_INDEX)) { - float animationFrameIndex; - memcpy(&animationFrameIndex, dataAt, sizeof(animationFrameIndex)); - dataAt += sizeof(animationFrameIndex); - bytesRead += sizeof(animationFrameIndex); - if (overwriteLocalData) { - _animationFrameIndex = animationFrameIndex; - } - } - - // PROP_ANIMATION_PLAYING - if (propertyFlags.getHasProperty(PROP_ANIMATION_PLAYING)) { - bool animationIsPlaying; - memcpy(&animationIsPlaying, dataAt, sizeof(animationIsPlaying)); - dataAt += sizeof(animationIsPlaying); - bytesRead += sizeof(animationIsPlaying); - if (overwriteLocalData) { - _animationIsPlaying = animationIsPlaying; - } - } + READ_ENTITY_PROPERTY_COLOR(PROP_COLOR, _color); + READ_ENTITY_PROPERTY_STRING(PROP_MODEL_URL, setModelURL); + READ_ENTITY_PROPERTY_STRING(PROP_ANIMATION_URL, setAnimationURL); + READ_ENTITY_PROPERTY(PROP_ANIMATION_FPS, float, _animationFPS); + READ_ENTITY_PROPERTY(PROP_ANIMATION_FRAME_INDEX, float, _animationFrameIndex); + READ_ENTITY_PROPERTY(PROP_ANIMATION_PLAYING, bool, _animationIsPlaying); return bytesRead; } diff --git a/libraries/entities/src/SphereEntityItem.cpp b/libraries/entities/src/SphereEntityItem.cpp index b85eb163d9..836cc24524 100644 --- a/libraries/entities/src/SphereEntityItem.cpp +++ b/libraries/entities/src/SphereEntityItem.cpp @@ -73,16 +73,7 @@ int SphereEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data int bytesRead = 0; const unsigned char* dataAt = data; - // PROP_COLOR - if (propertyFlags.getHasProperty(PROP_COLOR)) { - rgbColor color; - if (overwriteLocalData) { - memcpy(_color, dataAt, sizeof(_color)); - } - dataAt += sizeof(color); - bytesRead += sizeof(color); - } - + READ_ENTITY_PROPERTY_COLOR(PROP_COLOR, _color); return bytesRead; }