filipkunc avatar filipkunc committed 05883a1

CrossPlatform archiving.

Comments (0)

Files changed (7)

     First = 1U,
     Colors = 2U,
     TriQuads = 3U,
+    CrossPlatform = 4U,
 
-    Latest = TriQuads
+    Latest = CrossPlatform
 };
 
 
 Item::Item(MemoryReadStream *stream)
 {
-    stream->readBytes(&position, sizeof(Vector3D));
-    stream->readBytes(&rotation, sizeof(Quaternion));
-    stream->readBytes(&scale, sizeof(Vector3D));
-    stream->readBytes(&selected, sizeof(BOOL));
+    if (stream->version() >= (uint)ModelVersion::CrossPlatform)
+    {
+        position.x = stream->read<float>();
+        position.y = stream->read<float>();
+        position.z = stream->read<float>();
+        
+        rotation.x = stream->read<float>();
+        rotation.y = stream->read<float>();
+        rotation.z = stream->read<float>();
+        rotation.w = stream->read<float>();
+        
+        scale.x = stream->read<float>();
+        scale.y = stream->read<float>();
+        scale.z = stream->read<float>();
+        
+        selected = stream->read<bool>();
+    }
+    else
+    {
+        position = stream->read<Vector3D>();
+        rotation = stream->read<Quaternion>();
+        scale = stream->read<Vector3D>();
+        selected = stream->read<bool>();        
+    }
     
     visible = true;
     
 
 void Item::encode(MemoryWriteStream *stream)
 {
-    stream->writeBytes(&position, sizeof(Vector3D));
-    stream->writeBytes(&rotation, sizeof(Quaternion));
-    stream->writeBytes(&scale, sizeof(Vector3D));
-    stream->writeBytes(&selected, sizeof(BOOL));
-	
+    stream->write<float>(position.x);
+    stream->write<float>(position.y);
+    stream->write<float>(position.z);
+
+    stream->write<float>(rotation.x);
+    stream->write<float>(rotation.y);
+    stream->write<float>(rotation.z);
+    stream->write<float>(rotation.w);
+    
+    stream->write<float>(scale.x);
+    stream->write<float>(scale.y);
+    stream->write<float>(scale.z);
+    
+    stream->write<bool>(selected);
+
     mesh->encode(stream);
 }
 

Classes/ItemCollection.mm

 
 ItemCollection::ItemCollection(MemoryReadStream *stream)
 {
-    uint itemsCount;
-    stream->readBytes(&itemsCount, sizeof(uint));
+    uint itemsCount = stream->read<uint>();
     for (uint i = 0; i < itemsCount; i++)
     {
         Item *item = new Item(stream);
 void ItemCollection::encode(MemoryWriteStream *stream)
 {
     uint itemsCount = items.size();
-    stream->writeBytes(&itemsCount, sizeof(uint));
+    stream->write<uint>(itemsCount);
 	for (uint i = 0; i < itemsCount; i++)
 	{
 		Item *item = items.at(i);

Classes/MemoryStream.h

     unsigned int version() { return _version; }
     void setVersion(unsigned int value) { _version = value; }
     void readBytes(void *buffer, unsigned int length);
+
+    template <class T>
+    T read()
+    {
+        T t;
+        readBytes(&t, sizeof(T));
+        return t;
+    }
 };
 
 class MemoryWriteStream
 #endif
     unsigned int version() { return _version; }
     void setVersion(unsigned int value) { _version = value; }
-    void writeBytes(void *buffer, unsigned int length);
+    void writeBytes(const void *buffer, unsigned int length);
+    
+    template <class T>
+    void write(const T &value)
+    {
+        writeBytes(&value, sizeof(T));
+    }
 };

Classes/MemoryStream.mm

     
 }
 
-void MemoryWriteStream::writeBytes(void *buffer, uint length)
+void MemoryWriteStream::writeBytes(const void *buffer, uint length)
 {
     [_data appendBytes:buffer length:length];
 }
     
 }
 
-void MemoryWriteStream::writeBytes(void *buffer, unsigned int length)
+void MemoryWriteStream::writeBytes(const void *buffer, unsigned int length)
 {
 	array<Byte> ^bytes = gcnew array<Byte>(length);
 	pin_ptr<Byte> bytesPointer = &bytes[0];
     setColor(generateRandomColor());
 
     Vector4D color;
-    
-    if (stream->version() >= (uint)ModelVersion::Colors)
-        stream->readBytes(&color, sizeof(Vector4D));
+
+    if (stream->version() >= (uint)ModelVersion::CrossPlatform)
+    {
+        color.x = stream->read<float>();
+        color.y = stream->read<float>();
+        color.z = stream->read<float>();
+        color.w = stream->read<float>();
+    }
+    else if (stream->version() >= (uint)ModelVersion::Colors)
+    {
+        color = stream->read<Vector4D>();
+    }
     else
+    {
         color = generateRandomColor();
+    }
     
-    uint verticesSize;
-    uint texCoordsSize;
-    uint trianglesSize;
-    
-    stream->readBytes(&verticesSize, sizeof(uint));
-    stream->readBytes(&texCoordsSize, sizeof(uint));
-    stream->readBytes(&trianglesSize, sizeof(uint));
+    uint verticesSize = stream->read<uint>();
+    uint texCoordsSize = stream->read<uint>();
+    uint trianglesSize = stream->read<uint>();
     
     vector<Vector3D> vertices;
     vector<Vector3D> texCoords;
     vector<TriQuad> triangles;
     
-    for (uint i = 0; i < verticesSize; i++)
-    {
-        Vector3D vertex;
-        stream->readBytes(&vertex, sizeof(Vector3D));
-        vertices.push_back(vertex);
-    }
-    
-    for (uint i = 0; i < texCoordsSize; i++)
-    {
-        Vector3D texCoord;
-        stream->readBytes(&texCoord, sizeof(Vector3D));
-        texCoords.push_back(texCoord);
-    }
-    
-    if (stream->version() >= (uint)ModelVersion::TriQuads)
+    if (stream->version() >= (uint)ModelVersion::CrossPlatform)
     {
+        for (uint i = 0; i < verticesSize; i++)
+        {
+            Vector3D vertex;
+            vertex.x = stream->read<float>();
+            vertex.y = stream->read<float>();
+            vertex.z = stream->read<float>();
+            vertices.push_back(vertex);
+        }
+        
+        for (uint i = 0; i < texCoordsSize; i++)
+        {
+            Vector3D texCoord;
+            texCoord.x = stream->read<float>();
+            texCoord.y = stream->read<float>();
+            texCoord.z = stream->read<float>();
+            texCoords.push_back(texCoord);
+        }
+        
         for (uint i = 0; i < trianglesSize; i++)
         {
             TriQuad triangle;
-            stream->readBytes(&triangle, sizeof(TriQuad));
+            triangle.isQuad = stream->read<bool>();
+            uint count = triangle.isQuad ? 4 : 3;
+            for (uint j = 0; j < count; j++)
+            {
+                triangle.vertexIndices[j] = stream->read<uint>();
+                triangle.texCoordIndices[j] = stream->read<uint>();
+            }
             triangles.push_back(triangle);
         }
     }
     else
     {
-        for (uint i = 0; i < trianglesSize; i++)
+        for (uint i = 0; i < verticesSize; i++)
         {
-            Triangle triangle;
-            stream->readBytes(&triangle, sizeof(Triangle));
-            TriQuad triQuad;
-            triQuad.isQuad = false;
-            for (uint j = 0; j < 3; j++)
+            Vector3D vertex = stream->read<Vector3D>();
+            vertices.push_back(vertex);
+        }
+        
+        for (uint i = 0; i < texCoordsSize; i++)
+        {
+            Vector3D texCoord = stream->read<Vector3D>();
+            texCoords.push_back(texCoord);
+        }
+        
+        if (stream->version() >= (uint)ModelVersion::TriQuads)
+        {
+            for (uint i = 0; i < trianglesSize; i++)
             {
-                triQuad.vertexIndices[j] = triangle.vertexIndices[j];
-                triQuad.texCoordIndices[j] = triangle.texCoordIndices[j];
+                TriQuad triangle = stream->read<TriQuad>();
+                triangles.push_back(triangle);
+            }
+        }
+        else
+        {
+            for (uint i = 0; i < trianglesSize; i++)
+            {
+                Triangle triangle = stream->read<Triangle>();
+                TriQuad triQuad;
+                triQuad.isQuad = false;
+                for (uint j = 0; j < 3; j++)
+                {
+                    triQuad.vertexIndices[j] = triangle.vertexIndices[j];
+                    triQuad.texCoordIndices[j] = triangle.texCoordIndices[j];
+                }
+                triangles.push_back(triQuad);
             }
-            triangles.push_back(triQuad);
         }
     }
     
 
 void Mesh2::encode(MemoryWriteStream *stream)
 {
-    if (stream->version() > (uint)ModelVersion::Colors)
-    {
-        stream->writeBytes(&_color, sizeof(Vector4D));
-    }
-    
+    stream->write<float>(_color.x);
+    stream->write<float>(_color.y);
+    stream->write<float>(_color.z);
+    stream->write<float>(_color.w);
+        
     vector<Vector3D> vertices;
     vector<Vector3D> texCoords;
     vector<TriQuad> triangles;
     
     this->toIndexRepresentation(vertices, texCoords, triangles);
     
-	uint size = vertices.size();
-    stream->writeBytes(&size, sizeof(uint));
-    size = texCoords.size();
-    stream->writeBytes(&size, sizeof(uint));
-	size = triangles.size();
-    stream->writeBytes(&size, sizeof(uint));
+    uint vertexCount = vertices.size();
+    uint texCoordCount = texCoords.size();
+    uint triangleCount = triangles.size();
     
-	if (vertices.size() > 0)
-        stream->writeBytes(&vertices.at(0), vertices.size() * sizeof(Vector3D));
+    stream->write<uint>(vertexCount);
+    stream->write<uint>(texCoordCount);
+    stream->write<uint>(triangleCount);
     
-    if (texCoords.size() > 0)
-        stream->writeBytes(&texCoords.at(0), texCoords.size() * sizeof(Vector3D));
+    for (uint i = 0; i < vertexCount; i++)
+    {
+        const Vector3D &v = vertices[i];
+        stream->write<float>(v.x);
+        stream->write<float>(v.y);
+        stream->write<float>(v.z);        
+    }
+
+    for (uint i = 0; i < texCoordCount; i++)
+    {
+        const Vector3D &v = texCoords[i];
+        stream->write<float>(v.x);
+        stream->write<float>(v.y);
+        stream->write<float>(v.z);
+    }
 	
-	if (triangles.size() > 0)
-        stream->writeBytes(&triangles.at(0), triangles.size() * sizeof(TriQuad));
+	for (uint i = 0; i < triangleCount; i++)
+    {
+        const TriQuad &t = triangles[i];
+        stream->write<bool>(t.isQuad);
+        uint count = t.isQuad ? 4 : 3;
+        for (uint j = 0; j < count; j++)
+        {
+            stream->write(t.vertexIndices[j]);
+            stream->write(t.texCoordIndices[j]);
+        }
+    }
 }
 
 void Mesh2::setColor(Vector4D color)

Classes/MyDocument+archiving.mm

 {
     MemoryReadStream *stream = new MemoryReadStream(data);
     
-    uint version = 0;
-    
-    stream->readBytes(&version, sizeof(unsigned int));
+    uint version = stream->read<uint>();
     
     if (version < (uint)ModelVersion::First || version > (uint)ModelVersion::Latest)
         return NO;
     
     uint version = (uint)ModelVersion::Latest;
     stream->setVersion(version);
-    stream->writeBytes(&version, sizeof(unsigned int));
+    stream->write<uint>(version);
     items->encode(stream);
 
     delete stream;
 	{
 		MemoryReadStream *stream = new MemoryReadStream(memoryStream);
     
-		uint version = 0;
-    
-		stream->readBytes(&version, sizeof(unsigned int));
+		uint version = stream->read<uint>();
     
 		if (version < (uint)ModelVersion::First || version > (uint)ModelVersion::Latest)
 			return;
     
 		uint version = (uint)ModelVersion::Latest;
 		stream->setVersion(version);
-		stream->writeBytes(&version, sizeof(unsigned int));
+		stream->write<uint>(version);
 		items->encode(stream);
 
 		delete stream;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.