3 Copyright (c) 2006-2009 Charlie C & Erwin Coumans http://gamekit.googlecode.com
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 #include "bBlenderFile.h"
23 // 32 && 64 bit versions
24 extern unsigned char DNAstr[];
27 extern unsigned char DNAstr64[];
31 using namespace bParse;
33 bBlenderFile::bBlenderFile(const char* fileName)
34 :bFile(fileName, "BLENDER")
36 mMain= new bMain(this, fileName, mVersion);
41 bBlenderFile::bBlenderFile(char *memoryBuffer, int len)
42 :bFile(memoryBuffer,len, "BLENDER"),
45 mMain= new bMain(this, "memoryBuf", mVersion);
49 bBlenderFile::~bBlenderFile()
55 bMain* bBlenderFile::getMain()
60 // ----------------------------------------------------- //
61 void bBlenderFile::parseData()
63 // printf ("Building datablocks\n");
64 // printf ("Chunk size = %d\n",CHUNK_HEADER_LEN);
65 // printf ("File chunk size = %d\n", ChunkUtils::getOffset(mFlags));
67 const bool swap = (mFlags&FD_ENDIAN_SWAP)!=0;
71 char *dataPtr = mFileBuffer+mDataStart;
77 //dataPtr += ChunkUtils::getNextBlock(&dataChunk, dataPtr, mFlags);
78 int seek = getNextBlock(&dataChunk, dataPtr, mFlags);
79 //dataPtr += ChunkUtils::getOffset(mFlags);
80 char *dataPtrHead = 0;
82 while (dataChunk.code != DNA1)
89 if (dataChunk.code == SDNA) break;
90 //if (dataChunk.code == DNA1) break;
92 // same as (BHEAD+DATA dependency)
93 dataPtrHead = dataPtr+ChunkUtils::getOffset(mFlags);
94 char *id = readStruct(dataPtrHead, dataChunk);
99 m_chunkPtrPtrMap.insert(dataChunk.oldPtr, dataChunk);
100 mLibPointers.insert(dataChunk.oldPtr, (bStructHandle*)id);
102 m_chunks.push_back(dataChunk);
104 bListBasePtr *listID = mMain->getListBasePtr(dataChunk.code);
106 listID->push_back((bStructHandle*)id);
109 if (dataChunk.code == GLOB)
111 m_glob = (bStructHandle*) id;
117 seek = getNextBlock(&dataChunk, dataPtr, mFlags);
124 void bBlenderFile::addDataBlock(char* dataBlock)
126 mMain->addDatablock(dataBlock);
133 // 32 && 64 bit versions
134 extern unsigned char DNAstr[];
137 //unsigned char DNAstr[]={0};
141 extern unsigned char DNAstr64[];
145 void bBlenderFile::writeDNA(FILE* fp)
149 dataChunk.code = DNA1;
150 dataChunk.dna_nr = 0;
155 dataChunk.len = DNAlen64;
156 dataChunk.oldPtr = DNAstr64;
157 fwrite(&dataChunk,sizeof(bChunkInd),1,fp);
158 fwrite(DNAstr64, DNAlen64,1,fp);
162 dataChunk.len = DNAlen;
163 dataChunk.oldPtr = DNAstr;
164 fwrite(&dataChunk,sizeof(bChunkInd),1,fp);
165 fwrite(DNAstr, DNAlen,1,fp);
169 void bBlenderFile::parse(int verboseMode)
173 parseInternal(verboseMode,(char*)DNAstr64,DNAlen64);
177 parseInternal(verboseMode,(char*)DNAstr,DNAlen);
182 int bBlenderFile::write(const char* fileName, bool fixupPointers)
184 FILE *fp = fopen(fileName, "wb");
187 char header[SIZEOFBLENDERHEADER] ;
188 memcpy(header, m_headerString, 7);
190 endian= ((char*)&endian)[0];
211 fwrite(header,SIZEOFBLENDERHEADER,1,fp);
213 writeChunks(fp, fixupPointers);
221 printf("Error: cannot open file %s for writing\n",fileName);