summaryrefslogtreecommitdiff
path: root/src/client/GameSave.cpp
diff options
context:
space:
mode:
authorjacob1 <jfu614@gmail.com>2013-01-13 02:06:52 (GMT)
committer jacob1 <jfu614@gmail.com>2013-01-13 02:06:52 (GMT)
commit41751da6190a6d9a893bbe4cbf91f48cb5fceda5 (patch)
treedf4f87fc970683d64b2eb5e60c25c228cfa68ac8 /src/client/GameSave.cpp
parent0b6418b78d43ea1215b9eac36a686b601872efc7 (diff)
downloadpowder-41751da6190a6d9a893bbe4cbf91f48cb5fceda5.zip
powder-41751da6190a6d9a893bbe4cbf91f48cb5fceda5.tar.gz
change some free/malloc's back to delete/new's
Diffstat (limited to 'src/client/GameSave.cpp')
-rw-r--r--src/client/GameSave.cpp162
1 files changed, 81 insertions, 81 deletions
diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp
index 8d8a95c..53736ca 100644
--- a/src/client/GameSave.cpp
+++ b/src/client/GameSave.cpp
@@ -259,7 +259,7 @@ void GameSave::setSize(int newWidth, int newHeight)
{
this->blockWidth = newWidth;
this->blockHeight = newHeight;
-
+
particlesCount = 0;
particles = new Particle[NPART];
@@ -286,7 +286,7 @@ std::vector<char> GameSave::Serialise()
int dataSize;
char * data = Serialise(dataSize);
std::vector<char> dataVect(data, data+dataSize);
- free(data);
+ delete data;
return dataVect;
}
@@ -438,54 +438,54 @@ void GameSave::readOPS(char * data, int dataLength)
int savedVersion = inputData[4];
bson b;
bson_iterator iter;
-
+
//Block sizes
blockX = 0;
blockY = 0;
blockW = inputData[6];
blockH = inputData[7];
-
+
//Full size, normalised
fullX = blockX*CELL;
fullY = blockY*CELL;
fullW = blockW*CELL;
fullH = blockH*CELL;
-
+
//From newer version
if(savedVersion > SAVE_VERSION)
throw ParseException(ParseException::WrongVersion, "Save from newer version");
-
+
//Incompatible cell size
if(inputData[5] > CELL)
throw ParseException(ParseException::InvalidDimensions, "Incorrect CELL size");
-
+
//Too large/off screen
if(blockX+blockW > XRES/CELL || blockY+blockH > YRES/CELL)
throw ParseException(ParseException::InvalidDimensions, "Save too large");
-
+
setSize(blockW, blockH);
-
+
bsonDataLen = ((unsigned)inputData[8]);
bsonDataLen |= ((unsigned)inputData[9]) << 8;
bsonDataLen |= ((unsigned)inputData[10]) << 16;
bsonDataLen |= ((unsigned)inputData[11]) << 24;
-
+
bsonData = (unsigned char*)malloc(bsonDataLen+1);
if(!bsonData)
throw ParseException(ParseException::InternalError, "Unable to allocate memory");
-
+
//Make sure bsonData is null terminated, since all string functions need null terminated strings
//(bson_iterator_key returns a pointer into bsonData, which is then used with strcmp)
bsonData[bsonDataLen] = 0;
-
+
if (BZ2_bzBuffToBuffDecompress((char*)bsonData, &bsonDataLen, (char*)(inputData+12), inputDataLen-12, 0, 0))
throw ParseException(ParseException::Corrupt, "Unable to decompress");
-
+
bson_init_data(&b, (char*)bsonData);
bson_iterator_init(&iter, &b);
-
+
std::vector<sign> tempSigns;
-
+
while(bson_iterator_next(&iter))
{
if(strcmp(bson_iterator_key(&iter), "signs")==0)
@@ -502,7 +502,7 @@ void GameSave::readOPS(char * data, int dataLength)
{
bson_iterator signiter;
bson_iterator_subiterator(&subiter, &signiter);
-
+
sign tempSign("", 0, 0, sign::Left);
while(bson_iterator_next(&signiter))
{
@@ -681,7 +681,7 @@ void GameSave::readOPS(char * data, int dataLength)
}
}
}
-
+
//Read wall and fan data
if(wallData)
{
@@ -743,7 +743,7 @@ void GameSave::readOPS(char * data, int dataLength)
}
}
}
-
+
//Read particle data
if(partsData && partsPosData)
{
@@ -778,7 +778,7 @@ void GameSave::readOPS(char * data, int dataLength)
{
goto fail;
}
-
+
//i+3 because we have 4 bytes of required fields (type (1), descriptor (2), temp (1))
if (i+3 >= partsDataLen)
goto fail;
@@ -796,19 +796,19 @@ void GameSave::readOPS(char * data, int dataLength)
if(newIndex < 0 || newIndex >= NPART)
goto fail;
-
+
//Store partsptr index+1 for this saved particle index (0 means not loaded)
partsSimIndex[partsCount++] = newIndex+1;
//Clear the particle, ready for our new properties
memset(&(particles[newIndex]), 0, sizeof(Particle));
-
+
//Required fields
particles[newIndex].type = partsData[i];
particles[newIndex].x = x;
particles[newIndex].y = y;
i+=3;
-
+
//Read temp
if(fieldDescriptor & 0x01)
{
@@ -823,7 +823,7 @@ void GameSave::readOPS(char * data, int dataLength)
tempTemp = (char)partsData[i++];
particles[newIndex].temp = tempTemp+294.15f;
}
-
+
//Read life
if(fieldDescriptor & 0x02)
{
@@ -836,7 +836,7 @@ void GameSave::readOPS(char * data, int dataLength)
particles[newIndex].life |= (((unsigned)partsData[i++]) << 8);
}
}
-
+
//Read tmp
if(fieldDescriptor & 0x08)
{
@@ -856,7 +856,7 @@ void GameSave::readOPS(char * data, int dataLength)
}
}
}
-
+
//Read ctype
if(fieldDescriptor & 0x20)
{
@@ -871,7 +871,7 @@ void GameSave::readOPS(char * data, int dataLength)
particles[newIndex].ctype |= (((unsigned)partsData[i++]) << 8);
}
}
-
+
//Read dcolour
if(fieldDescriptor & 0x40)
{
@@ -881,21 +881,21 @@ void GameSave::readOPS(char * data, int dataLength)
particles[newIndex].dcolour |= (((unsigned)partsData[i++]) << 8);
particles[newIndex].dcolour |= ((unsigned)partsData[i++]);
}
-
+
//Read vx
if(fieldDescriptor & 0x80)
{
if(i >= partsDataLen) goto fail;
particles[newIndex].vx = (partsData[i++]-127.0f)/16.0f;
}
-
+
//Read vy
if(fieldDescriptor & 0x100)
{
if(i >= partsDataLen) goto fail;
particles[newIndex].vy = (partsData[i++]-127.0f)/16.0f;
}
-
+
//Read tmp2
if(fieldDescriptor & 0x400)
{
@@ -1008,20 +1008,20 @@ void GameSave::readPSv(char * data, int dataLength)
int bx0=0, by0=0, bw, bh, w, h, y0 = 0, x0 = 0;
int nf=0, new_format = 0, ttv = 0;
int *fp = (int *)malloc(NPART*sizeof(int));
-
+
std::vector<sign> tempSigns;
char tempSignText[255];
sign tempSign("", 0, 0, sign::Left);
-
+
//Gol data used to read older saves
int goltype[NGOL];
int grule[NGOL+1][10];
-
+
int golRulesCount;
int * golRulesT = LoadGOLRules(golRulesCount);
memcpy(grule, golRulesT, sizeof(int) * (golRulesCount*10));
free(golRulesT);
-
+
int golTypesCount;
int * golTypesT = LoadGOLTypes(golTypesCount);
memcpy(goltype, golTypesT, sizeof(int) * (golTypesCount));
@@ -1031,10 +1031,10 @@ void GameSave::readPSv(char * data, int dataLength)
try
{
-
+
//New file header uses PSv, replacing fuC. This is to detect if the client uses a new save format for temperatures
//This creates a problem for old clients, that display and "corrupt" error instead of a "newer version" error
-
+
if (dataLength<16)
throw ParseException(ParseException::Corrupt, "No save data");
if (!(c[2]==0x43 && c[1]==0x75 && c[0]==0x66) && !(c[2]==0x76 && c[1]==0x53 && c[0]==0x50))
@@ -1045,7 +1045,7 @@ void GameSave::readPSv(char * data, int dataLength)
if (c[4]>SAVE_VERSION)
throw ParseException(ParseException::WrongVersion, "Save from newer version");
ver = c[4];
-
+
if (ver<34)
{
legacyEnable = 1;
@@ -1070,7 +1070,7 @@ void GameSave::readPSv(char * data, int dataLength)
}
}
}
-
+
bw = c[6];
bh = c[7];
if (bx0+bw > XRES/CELL)
@@ -1081,7 +1081,7 @@ void GameSave::readPSv(char * data, int dataLength)
bx0 = 0;
if (by0 < 0)
by0 = 0;
-
+
if (c[5]!=CELL || bx0+bw>XRES/CELL || by0+bh>YRES/CELL)
throw ParseException(ParseException::InvalidDimensions, "Save too large");
i = (unsigned)c[8];
@@ -1091,9 +1091,9 @@ void GameSave::readPSv(char * data, int dataLength)
d = (unsigned char *)malloc(i);
if (!d)
throw ParseException(ParseException::Corrupt, "Cannot allocate memory");
-
+
setSize(bw, bh);
-
+
int bzStatus = 0;
if (bzStatus = BZ2_bzBuffToBuffDecompress((char *)d, (unsigned *)&i, (char *)(c+12), dataLength-12, 0, 0))
{
@@ -1106,22 +1106,22 @@ void GameSave::readPSv(char * data, int dataLength)
#ifdef DEBUG
std::cout << "Parsing " << dataLength << " bytes of data, version " << ver << std::endl;
#endif
-
+
if (dataLength < bw*bh)
throw ParseException(ParseException::Corrupt, "Save data corrupt (missing data)");
-
+
// normalize coordinates
x0 = bx0*CELL;
y0 = by0*CELL;
w = bw *CELL;
h = bh *CELL;
-
+
if (ver<46) {
gravityMode = 0;
airMode = 0;
}
m = (int *)calloc(XRES*YRES, sizeof(int));
-
+
// load the required air state
for (y=by0; y<by0+bh; y++)
for (x=bx0; x<bx0+bw; x++)
@@ -1162,7 +1162,7 @@ void GameSave::readPSv(char * data, int dataLength)
blockMap[y][x]=WL_EHOLE;
else if (blockMap[y][x]==13)
blockMap[y][x]=WL_ALLOWGAS;
-
+
if (blockMap[y][x]==O_WL_WALLELEC)
blockMap[y][x]=WL_WALLELEC;
else if (blockMap[y][x]==O_WL_EWALL)
@@ -1196,7 +1196,7 @@ void GameSave::readPSv(char * data, int dataLength)
else if (blockMap[y][x]==O_WL_ALLOWENERGY)
blockMap[y][x]=WL_ALLOWENERGY;
}
-
+
p++;
}
for (y=by0; y<by0+bh; y++)
@@ -1215,7 +1215,7 @@ void GameSave::readPSv(char * data, int dataLength)
throw ParseException(ParseException::Corrupt, "Not enough data at line " MTOS(__LINE__) " in " MTOS(__FILE__));
fanVelY[y][x] = (d[p++]-127.0f)/64.0f;
}
-
+
// load the particle map
i = 0;
k = 0;
@@ -1250,7 +1250,7 @@ void GameSave::readPSv(char * data, int dataLength)
particlesCount = ++k;
}
}
-
+
// load particle properties
for (j=0; j<w*h; j++)
{
@@ -1479,7 +1479,7 @@ void GameSave::readPSv(char * data, int dataLength)
if (fabs(particles[i-1].vx)>0.0f || fabs(particles[i-1].vy)>0.0f)
particles[i-1].flags |= FLAG_MOVABLE;
}
-
+
if (ver<48 && (ty==OLD_PT_WIND || (ty==PT_BRAY&&particles[i-1].life==0)))
{
// Replace invisible particles with something sensible and add decoration to hide it
@@ -1559,7 +1559,7 @@ void GameSave::readPSv(char * data, int dataLength)
}
}
}
-
+
if (p >= dataLength)
goto version1;
j = d[p++];
@@ -1586,7 +1586,7 @@ void GameSave::readPSv(char * data, int dataLength)
tempSigns.push_back(tempSign);
p += x;
}
-
+
for (i = 0; i < tempSigns.size(); i++)
{
if(signs.size() == MAXSIGNS)
@@ -1614,7 +1614,7 @@ void GameSave::readPSv(char * data, int dataLength)
}
throw;
}
-
+
version1:
if (m)
{
@@ -1651,17 +1651,17 @@ char * GameSave::serialiseOPS(int & dataLength)
//Get coords in blocks
blockX = 0;//orig_x0/CELL;
blockY = 0;//orig_y0/CELL;
-
+
//Snap full coords to block size
fullX = blockX*CELL;
fullY = blockY*CELL;
-
+
//Original size + offset of original corner from snapped corner, rounded up by adding CELL-1
blockW = blockWidth;//(blockWidth-fullX+CELL-1)/CELL;
blockH = blockHeight;//(blockHeight-fullY+CELL-1)/CELL;
fullW = blockW*CELL;
fullH = blockH*CELL;
-
+
//Copy fan and wall data
wallData = (unsigned char*)malloc(blockW*blockH);
wallDataLen = blockW*blockH;
@@ -1697,7 +1697,7 @@ char * GameSave::serialiseOPS(int & dataLength)
free(wallData);
wallData = NULL;
}
-
+
//Index positions of all particles, using linked lists
//partsPosFirstMap is pmap for the first particle in each position
//partsPosLastMap is pmap for the last particle in each position
@@ -1731,7 +1731,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsPosCount[y*fullW + x]++;
}
}
-
+
//Store number of particles in each position
partsPosData = (unsigned char*)malloc(fullW*fullH*3);
partsPosDataLen = 0;
@@ -1745,7 +1745,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsPosData[partsPosDataLen++] = (posCount&0x000000FF);
}
}
-
+
//Copy parts data
/* Field descriptor format:
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
@@ -1762,27 +1762,27 @@ char * GameSave::serialiseOPS(int & dataLength)
{
//Find the first particle in this position
i = partsPosFirstMap[y*fullW + x];
-
+
//Loop while there is a pmap entry
while (i)
{
unsigned short fieldDesc = 0;
int fieldDescLoc = 0, tempTemp, vTemp;
-
+
//Turn pmap entry into a particles index
i = i>>8;
-
+
//Store saved particle index+1 for this partsptr index (0 means not saved)
partsSaveIndex[i] = (partsCount++) + 1;
//Type (required)
partsData[partsDataLen++] = particles[i].type;
elementCount[particles[i].type]++;
-
+
//Location of the field descriptor
fieldDescLoc = partsDataLen++;
partsDataLen++;
-
+
//Extra Temperature (2nd byte optional, 1st required), 1 to 2 bytes
//Store temperature as an offset of 21C(294.15K) or go into a 16byte int and store the whole thing
if(fabs(particles[i].temp-294.15f)<127)
@@ -1797,7 +1797,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = tempTemp;
partsData[partsDataLen++] = tempTemp >> 8;
}
-
+
//Life (optional), 1 to 2 bytes
if(particles[i].life)
{
@@ -1809,7 +1809,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = particles[i].life >> 8;
}
}
-
+
//Tmp (optional), 1 to 2 bytes
if(particles[i].tmp)
{
@@ -1827,7 +1827,7 @@ char * GameSave::serialiseOPS(int & dataLength)
}
}
}
-
+
//Ctype (optional), 1 or 4 bytes
if(particles[i].ctype)
{
@@ -1841,7 +1841,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = (particles[i].ctype&0x0000FF00)>>8;
}
}
-
+
//Dcolour (optional), 4 bytes
if(particles[i].dcolour && (particles[i].dcolour & 0xFF000000))
{
@@ -1851,7 +1851,7 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = (particles[i].dcolour&0x0000FF00)>>8;
partsData[partsDataLen++] = (particles[i].dcolour&0x000000FF);
}
-
+
//VX (optional), 1 byte
if(fabs(particles[i].vx) > 0.001f)
{
@@ -1861,7 +1861,7 @@ char * GameSave::serialiseOPS(int & dataLength)
if (vTemp>255) vTemp=255;
partsData[partsDataLen++] = vTemp;
}
-
+
//VY (optional), 1 byte
if(fabs(particles[i].vy) > 0.001f)
{
@@ -1871,7 +1871,7 @@ char * GameSave::serialiseOPS(int & dataLength)
if (vTemp>255) vTemp=255;
partsData[partsDataLen++] = vTemp;
}
-
+
//Tmp2 (optional), 1 or 2 bytes
if(particles[i].tmp2)
{
@@ -1883,11 +1883,11 @@ char * GameSave::serialiseOPS(int & dataLength)
partsData[partsDataLen++] = particles[i].tmp2 >> 8;
}
}
-
+
//Write the field descriptor;
partsData[fieldDescLoc] = fieldDesc;
partsData[fieldDescLoc+1] = fieldDesc>>8;
-
+
//Get the pmap entry for the next particle in the same position
i = partsPosLink[i];
}
@@ -1940,7 +1940,7 @@ char * GameSave::serialiseOPS(int & dataLength)
free(partsData);
partsData = NULL;
}
-
+
bson_init(&b);
bson_append_bool(&b, "waterEEnabled", waterEEnabled);
bson_append_bool(&b, "legacyEnable", legacyEnable);
@@ -1948,7 +1948,7 @@ char * GameSave::serialiseOPS(int & dataLength)
bson_append_bool(&b, "paused", paused);
bson_append_int(&b, "gravityMode", gravityMode);
bson_append_int(&b, "airMode", airMode);
-
+
//bson_append_int(&b, "leftSelectedElement", sl);
//bson_append_int(&b, "rightSelectedElement", sr);
//bson_append_int(&b, "activeMenu", active_menu);
@@ -2000,12 +2000,12 @@ char * GameSave::serialiseOPS(int & dataLength)
#ifdef DEBUG
bson_print(&b);
#endif
-
+
finalData = (unsigned char *)bson_data(&b);
finalDataLen = bson_size(&b);
outputDataLen = finalDataLen*2+12;
- outputData = (unsigned char *)malloc(outputDataLen);
-
+ outputData = new unsigned char[outputDataLen];
+
outputData[0] = 'O';
outputData[1] = 'P';
outputData[2] = 'S';
@@ -2018,7 +2018,7 @@ char * GameSave::serialiseOPS(int & dataLength)
outputData[9] = finalDataLen >> 8;
outputData[10] = finalDataLen >> 16;
outputData[11] = finalDataLen >> 24;
-
+
if (BZ2_bzBuffToBuffCompress((char*)(outputData+12), &outputDataLen, (char*)finalData, bson_size(&b), 9, 0, 0) != BZ_OK)
{
puts("Save Error\n");
@@ -2027,12 +2027,12 @@ char * GameSave::serialiseOPS(int & dataLength)
outputData = NULL;
goto fin;
}
-
+
#ifdef DEBUG
printf("compressed data: %d\n", outputDataLen);
#endif
dataLength = outputDataLen + 12;
-
+
fin:
bson_destroy(&b);
if(partsData)
@@ -2047,7 +2047,7 @@ fin:
free(partsSaveIndex);
if (soapLinkData)
free(soapLinkData);
-
+
return (char*)outputData;
}