add EntityTypes::extractEntityTypeAndID(), remove one spaghetti

This commit is contained in:
Andrew Meadows 2018-12-08 09:58:39 -08:00
parent 1815d71158
commit 0083c77280
4 changed files with 22 additions and 18 deletions

View file

@ -93,7 +93,7 @@ void EntityItemProperties::setLastEdited(quint64 usecTime) {
bool EntityItemProperties::constructFromBuffer(const unsigned char* data, int dataLength) {
ReadBitstreamToTreeParams args;
EntityItemPointer tempEntity = EntityTypes::constructEntityItem(data, dataLength, args);
EntityItemPointer tempEntity = EntityTypes::constructEntityItem(data, dataLength);
if (!tempEntity) {
return false;
}

View file

@ -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);

View file

@ -107,8 +107,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 +118,32 @@ 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<quint32> 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::LAST) {
EntityItemID tempEntityID(id);
EntityItemProperties tempProperties;
return constructEntityItem(type, tempEntityID, tempProperties);
}
return nullptr;
}

View file

@ -112,8 +112,9 @@ public:
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);
private:
static QMap<EntityType, QString> _typeToNameMap;