- use TArray for most buffers in p_glnodes.cpp.

This commit is contained in:
Christoph Oelckers 2018-11-10 14:15:39 +01:00 committed by drfrag666
parent 91663e1202
commit f744b9a0f8

View file

@ -218,16 +218,14 @@ static bool format5;
static bool LoadGLVertexes(FileReader &lump)
{
uint8_t *gldata;
int i;
firstglvertex = level.vertexes.Size();
auto gllen=lump.GetLength();
gldata = new uint8_t[gllen];
lump.Seek(0, FileReader::SeekSet);
lump.Read(gldata, gllen);
auto glbuf = lump.Read();
auto gllen=lump.GetLength();
auto gldata = glbuf.Data();
if (*(int *)gldata == gNd5)
{
@ -241,7 +239,6 @@ static bool LoadGLVertexes(FileReader &lump)
Printf("GL nodes v%d found. This format is not supported by " GAMENAME "\n",
(*(int *)gldata == gNd4)? 4:1);
delete [] gldata;
return false;
}
else format5=false;
@ -264,7 +261,6 @@ static bool LoadGLVertexes(FileReader &lump)
level.vertexes[i].set(LittleLong(mgl->x)/65536., LittleLong(mgl->y)/65536.);
mgl++;
}
delete[] gldata;
return true;
}
@ -296,27 +292,21 @@ static inline int checkGLVertex3(int num)
static bool LoadGLSegs(FileReader &lump)
{
char *data;
int i;
line_t *ldef=NULL;
int numsegs = (int)lump.GetLength();
data= new char[numsegs];
lump.Seek(0, FileReader::SeekSet);
lump.Read(data, numsegs);
auto data = lump.Read();
int numsegs = (int)lump.GetLength();
auto &segs = level.segs;
#ifdef _MSC_VER
__try
#endif
{
if (!format5 && memcmp(data, "gNd3", 4))
if (!format5 && memcmp(data.Data(), "gNd3", 4))
{
numsegs/=sizeof(glseg_t);
level.segs.Alloc(numsegs);
memset(&segs[0],0,sizeof(seg_t)*numsegs);
glseg_t * ml = (glseg_t*)data;
glseg_t * ml = (glseg_t*)data.Data();
for(i = 0; i < numsegs; i++)
{
// check for gl-vertices
@ -368,7 +358,7 @@ static bool LoadGLSegs(FileReader &lump)
level.segs.Alloc(numsegs);
memset(&segs[0],0,sizeof(seg_t)*numsegs);
glseg3_t * ml = (glseg3_t*)(data+ (format5? 0:4));
glseg3_t * ml = (glseg3_t*)(data.Data() + (format5? 0:4));
for(i = 0; i < numsegs; i++)
{ // check for gl-vertices
segs[i].v1 = &level.vertexes[checkGLVertex3(LittleLong(ml->v1))];
@ -414,22 +404,8 @@ static bool LoadGLSegs(FileReader &lump)
ml++;
}
}
delete [] data;
return true;
}
#ifdef _MSC_VER
__except(1)
{
// Invalid data has the bad habit of requiring extensive checks here
// so let's just catch anything invalid and output a message.
// (at least under MSVC. GCC can't do SEH even for Windows... :( )
Printf("Invalid GL segs. The BSP will have to be rebuilt.\n");
delete [] data;
level.segs.Clear();
return false;
}
#endif
}
//==========================================================================
@ -440,23 +416,20 @@ static bool LoadGLSegs(FileReader &lump)
static bool LoadGLSubsectors(FileReader &lump)
{
char * datab;
int i;
int numsubsectors = (int)lump.GetLength();
datab = new char[numsubsectors];
lump.Seek(0, FileReader::SeekSet);
lump.Read(datab, numsubsectors);
auto datab = lump.Read();
if (numsubsectors == 0)
{
delete [] datab;
return false;
}
if (!format5 && memcmp(datab, "gNd3", 4))
if (!format5 && memcmp(datab.Data(), "gNd3", 4))
{
mapsubsector_t * data = (mapsubsector_t*) datab;
mapsubsector_t * data = (mapsubsector_t*) datab.Data();
numsubsectors /= sizeof(mapsubsector_t);
level.subsectors.Alloc(numsubsectors);
auto &subsectors = level.subsectors;
@ -469,14 +442,13 @@ static bool LoadGLSubsectors(FileReader &lump)
if (subsectors[i].numlines == 0)
{
delete [] datab;
return false;
}
}
}
else
{
gl3_mapsubsector_t * data = (gl3_mapsubsector_t*) (datab+(format5? 0:4));
gl3_mapsubsector_t * data = (gl3_mapsubsector_t*) (datab.Data()+(format5? 0:4));
numsubsectors /= sizeof(gl3_mapsubsector_t);
level.subsectors.Alloc(numsubsectors);
auto &subsectors = level.subsectors;
@ -489,7 +461,6 @@ static bool LoadGLSubsectors(FileReader &lump)
if (subsectors[i].numlines == 0)
{
delete [] datab;
return false;
}
}
@ -507,12 +478,10 @@ static bool LoadGLSubsectors(FileReader &lump)
// The subsector must be closed. If it isn't we can't use these nodes and have to do a rebuild.
if (lastseg->v2 != firstseg->v1)
{
delete [] datab;
return false;
}
}
delete [] datab;
return true;
}
@ -530,7 +499,7 @@ static bool LoadNodes (FileReader &lump)
int j;
int k;
node_t* no;
uint16_t* used;
TArray<uint16_t> used;
if (!format5)
{
@ -542,11 +511,11 @@ static bool LoadNodes (FileReader &lump)
level.nodes.Alloc(numnodes);
lump.Seek(0, FileReader::SeekSet);
basemn = mn = new mapnode_t[numnodes];
lump.Read(mn, lump.GetLength());
auto buf = lump.Read();
basemn = mn = (mapnode_t*)buf.Data();
used = (uint16_t *)alloca (sizeof(uint16_t)*numnodes);
memset (used, 0, sizeof(uint16_t)*numnodes);
used.Resize(numnodes);
memset (used.Data(), 0, sizeof(uint16_t)*numnodes);
no = &level.nodes[0];
@ -564,19 +533,16 @@ static bool LoadNodes (FileReader &lump)
child &= ~NF_SUBSECTOR;
if (child >= level.subsectors.Size())
{
delete [] basemn;
return false;
}
no->children[j] = (uint8_t *)&level.subsectors[child] + 1;
}
else if (child >= numnodes)
{
delete [] basemn;
return false;
}
else if (used[child])
{
delete [] basemn;
return false;
}
else
@ -590,7 +556,6 @@ static bool LoadNodes (FileReader &lump)
}
}
}
delete [] basemn;
}
else
{
@ -602,11 +567,11 @@ static bool LoadNodes (FileReader &lump)
level.nodes.Alloc(numnodes);
lump.Seek(0, FileReader::SeekSet);
basemn = mn = new gl5_mapnode_t[numnodes];
lump.Read(mn, lump.GetLength());
auto buf = lump.Read();
basemn = mn = (gl5_mapnode_t*)buf.Data();
used = (uint16_t *)alloca (sizeof(uint16_t)*numnodes);
memset (used, 0, sizeof(uint16_t)*numnodes);
used.Resize(numnodes);
memset(used.Data(), 0, sizeof(uint16_t)*numnodes);
no = &level.nodes[0];
@ -624,19 +589,16 @@ static bool LoadNodes (FileReader &lump)
child &= ~GL5_NF_SUBSECTOR;
if ((unsigned)child >= level.subsectors.Size())
{
delete [] basemn;
return false;
}
no->children[j] = (uint8_t *)&level.subsectors[child] + 1;
}
else if ((unsigned)child >= numnodes)
{
delete [] basemn;
return false;
}
else if (used[child])
{
delete [] basemn;
return false;
}
else
@ -650,7 +612,6 @@ static bool LoadNodes (FileReader &lump)
}
}
}
delete [] basemn;
}
return true;
}
@ -1105,31 +1066,30 @@ static void CreateCachedNodes(MapData *map)
}
uLongf outlen = ZNodes.Size();
uint8_t *compressed;
TArray<Bytef> compressed;
int offset = level.lines.Size() * 8 + 12 + 16;
int r;
do
{
compressed = new Bytef[outlen + offset];
r = compress (compressed + offset, &outlen, &ZNodes[0], ZNodes.Size());
compressed.Resize(outlen + offset);
r = compress (compressed.Data() + offset, &outlen, &ZNodes[0], ZNodes.Size());
if (r == Z_BUF_ERROR)
{
delete[] compressed;
outlen += 1024;
}
}
while (r == Z_BUF_ERROR);
memcpy(compressed, "CACH", 4);
memcpy(compressed.Data(), "CACH", 4);
uint32_t len = LittleLong(level.lines.Size());
memcpy(compressed+4, &len, 4);
map->GetChecksum(compressed+8);
memcpy(&compressed[4], &len, 4);
map->GetChecksum(&compressed[8]);
for (unsigned i = 0; i < level.lines.Size(); i++)
{
uint32_t ndx[2] = { LittleLong(uint32_t(level.lines[i].v1->Index())), LittleLong(uint32_t(level.lines[i].v2->Index())) };
memcpy(compressed + 8 + 16 + 8 * i, ndx, 8);
memcpy(&compressed[8 + 16 + 8 * i], ndx, 8);
}
memcpy(compressed + offset - 4, "ZGL3", 4);
memcpy(&compressed[offset - 4], "ZGL3", 4);
FString path = CreateCacheName(map, true);
FileWriter *fw = FileWriter::Open(path);
@ -1137,7 +1097,7 @@ static void CreateCachedNodes(MapData *map)
if (fw != nullptr)
{
const size_t length = outlen + offset;
if (fw->Write(compressed, length) != length)
if (fw->Write(compressed.Data(), length) != length)
{
Printf("Error saving nodes to file %s\n", path.GetChars());
}
@ -1147,8 +1107,6 @@ static void CreateCachedNodes(MapData *map)
{
Printf("Cannot open nodes file %s for writing\n", path.GetChars());
}
delete [] compressed;
}
@ -1158,29 +1116,29 @@ static bool CheckCachedNodes(MapData *map)
uint8_t md5[16];
uint8_t md5map[16];
uint32_t numlin;
uint32_t *verts = NULL;
TArray<uint32_t> verts;
FString path = CreateCacheName(map, false);
FileReader fr;
if (!fr.OpenFile(path)) return false;
if (fr.Read(magic, 4) != 4) goto errorout;
if (memcmp(magic, "CACH", 4)) goto errorout;
if (fr.Read(magic, 4) != 4) return false;
if (memcmp(magic, "CACH", 4)) return false;
if (fr.Read(&numlin, 4) != 4) goto errorout;
if (fr.Read(&numlin, 4) != 4) return false;
numlin = LittleLong(numlin);
if (numlin != level.lines.Size()) goto errorout;
if (numlin != level.lines.Size()) return false;
if (fr.Read(md5, 16) != 16) goto errorout;
if (fr.Read(md5, 16) != 16) return false;
map->GetChecksum(md5map);
if (memcmp(md5, md5map, 16)) goto errorout;
if (memcmp(md5, md5map, 16)) return false;
verts = new uint32_t[numlin * 8];
if (fr.Read(verts, 8 * numlin) != 8 * numlin) goto errorout;
verts.Resize(numlin * 2);
if (fr.Read(verts.Data(), 8 * numlin) != 8 * numlin) return false;
if (fr.Read(magic, 4) != 4) goto errorout;
if (memcmp(magic, "ZGL2", 4) && memcmp(magic, "ZGL3", 4)) goto errorout;
if (fr.Read(magic, 4) != 4) return false;
if (memcmp(magic, "ZGL2", 4) && memcmp(magic, "ZGL3", 4)) return false;
try
@ -1194,7 +1152,7 @@ static bool CheckCachedNodes(MapData *map)
level.subsectors.Clear();
level.segs.Clear();
level.nodes.Clear();
goto errorout;
return false;
}
for(auto &line : level.lines)
@ -1203,16 +1161,7 @@ static bool CheckCachedNodes(MapData *map)
line.v1 = &level.vertexes[LittleLong(verts[i*2])];
line.v2 = &level.vertexes[LittleLong(verts[i*2+1])];
}
delete [] verts;
return true;
errorout:
if (verts != NULL)
{
delete[] verts;
}
return false;
}
UNSAFE_CCMD(clearnodecache)
@ -1316,32 +1265,6 @@ void P_SetRenderSector()
TArray<subsector_t *> undetermined;
subsector_t * ss;
#if 0 // doesn't work as expected :(
// hide all sectors on textured automap that only have hidden lines.
bool *hidesec = new bool[numsectors];
for(i = 0; i < numsectors; i++)
{
hidesec[i] = true;
}
for(i = 0; i < numlines; i++)
{
if (!(lines[i].flags & ML_DONTDRAW))
{
hidesec[lines[i].frontsector - sectors] = false;
if (lines[i].backsector != NULL)
{
hidesec[lines[i].backsector - sectors] = false;
}
}
}
for(i = 0; i < numsectors; i++)
{
if (hidesec[i]) sectors[i].MoreFlags |= SECMF_HIDDEN;
}
delete [] hidesec;
#endif
// Check for incorrect partner seg info so that the following code does not crash.
for (auto &seg : level.segs)