qboolean G2_SaveGhoul2Models(CGhoul2Info_v &ghoul2, char **buffer, int *size) { int i, x; // is there anything to save? if (!ghoul2.IsValid()||!ghoul2.size()) { *buffer = (char *)Z_Malloc(4, TAG_GHOUL2, qfalse); int *tempBuffer = (int *)*buffer; *tempBuffer = 0; *size = 4; return qtrue; } // yeah, lets get busy *size = 0; // this one isn't a define since I couldn't work out how to figure it out at compile time int ghoul2BlockSize = (int)&ghoul2[0].mTransformedVertsArray - (int)&ghoul2[0].mModelindex; // add in count for number of ghoul2 models *size += 4; // start out working out the total size of the buffer we need to allocate for (i=0; i<ghoul2.size();i++) { *size += ghoul2BlockSize; // add in count for number of surfaces *size += 4; *size += (ghoul2[i].mSlist.size() * SURFACE_SAVE_BLOCK_SIZE); // add in count for number of bones *size += 4; *size += (ghoul2[i].mBlist.size() * BONE_SAVE_BLOCK_SIZE); // add in count for number of bolts *size += 4; *size += (ghoul2[i].mBltlist.size() * BOLT_SAVE_BLOCK_SIZE); } // ok, we should know how much space we need now *buffer = (char*)Z_Malloc(*size, TAG_GHOUL2, qfalse); // now lets start putting the data we care about into the buffer char *tempBuffer = *buffer; // save out how many ghoul2 models we have *(int *)tempBuffer = ghoul2.size(); tempBuffer +=4; for (i=0; i<ghoul2.size();i++) { // first save out the ghoul2 details themselves // OutputDebugString(va("G2_SaveGhoul2Models(): ghoul2[%d].mModelindex = %d\n",i,ghoul2[i].mModelindex)); memcpy(tempBuffer, &ghoul2[i].mModelindex, ghoul2BlockSize); tempBuffer += ghoul2BlockSize; // save out how many surfaces we have *(int*)tempBuffer = ghoul2[i].mSlist.size(); tempBuffer +=4; // now save the all the surface list info for (x=0; x<ghoul2[i].mSlist.size(); x++) { memcpy(tempBuffer, &ghoul2[i].mSlist[x], SURFACE_SAVE_BLOCK_SIZE); tempBuffer += SURFACE_SAVE_BLOCK_SIZE; } // save out how many bones we have *(int*)tempBuffer = ghoul2[i].mBlist.size(); tempBuffer +=4; // now save the all the bone list info for (x=0; x<ghoul2[i].mBlist.size(); x++) { memcpy(tempBuffer, &ghoul2[i].mBlist[x], BONE_SAVE_BLOCK_SIZE); tempBuffer += BONE_SAVE_BLOCK_SIZE; } // save out how many bolts we have *(int*)tempBuffer = ghoul2[i].mBltlist.size(); tempBuffer +=4; // lastly save the all the bolt list info for (x=0; x<ghoul2[i].mBltlist.size(); x++) { memcpy(tempBuffer, &ghoul2[i].mBltlist[x], BOLT_SAVE_BLOCK_SIZE); tempBuffer += BOLT_SAVE_BLOCK_SIZE; } } return qtrue; }