- rewrote the operator>> methods of FileReader as normal functions for clarity.

This commit is contained in:
Christoph Oelckers 2018-03-11 18:20:49 +01:00
parent 833dbee167
commit 0be1ed252b
12 changed files with 150 additions and 133 deletions

View file

@ -324,14 +324,17 @@ public:
buf.Resize(length);
memcpy(&buf[0], buffer, length);
}
Length = length;
FilePos = 0;
bufptr = (const char *)&buf[0];
UpdateBuffer();
}
TArray<uint8_t> &GetArray() { return buf; }
void UpdateLength() { Length = buf.Size(); }
void UpdateBuffer()
{
bufptr = (const char*)&buf[0];
FilePos = 0;
Length = buf.Size();
}
};
@ -381,7 +384,7 @@ bool FileRdr::OpenMemoryArray(std::function<bool(TArray<uint8_t>&)> getter)
if (getter(reader->GetArray()))
{
Close();
reader->UpdateLength();
reader->UpdateBuffer();
mReader = reader;
return true;
}

View file

@ -201,47 +201,63 @@ public:
return mReader->GetLength();
}
// Note: These need to go because they are fundamentally unsafe to use on a binary stream.
FileRdr &operator>> (uint8_t &v)
uint8_t ReadUInt8()
{
mReader->Read(&v, 1);
return *this;
uint8_t v;
Read(&v, 1);
return v;
}
FileRdr &operator>> (int8_t &v)
int8_t ReadInt8()
{
mReader->Read(&v, 1);
return *this;
int8_t v;
Read(&v, 1);
return v;
}
FileRdr &operator>> (uint16_t &v)
uint16_t ReadUInt16()
{
mReader->Read(&v, 2);
v = LittleShort(v);
return *this;
uint16_t v;
Read(&v, 2);
return LittleShort(v);
}
FileRdr &operator>> (int16_t &v)
int16_t ReadInt16()
{
mReader->Read(&v, 2);
v = LittleShort(v);
return *this;
uint16_t v;
Read(&v, 2);
return LittleShort(v);
}
FileRdr &operator>> (uint32_t &v)
uint32_t ReadUInt32()
{
mReader->Read(&v, 4);
v = LittleLong(v);
return *this;
uint32_t v;
Read(&v, 4);
return LittleLong(v);
}
FileRdr &operator>> (int32_t &v)
int32_t ReadInt32()
{
mReader->Read(&v, 4);
v = LittleLong(v);
return *this;
uint32_t v;
Read(&v, 4);
return LittleLong(v);
}
uint32_t ReadUInt32BE()
{
uint32_t v;
Read(&v, 4);
return BigLong(v);
}
int32_t ReadInt32BE()
{
uint32_t v;
Read(&v, 4);
return BigLong(v);
}
friend class FWadCollection;
};

View file

@ -842,9 +842,9 @@ void P_LoadVertexes (MapData * map)
// Copy and convert vertex coordinates, internal representation as fixed.
for (auto &v : level.vertexes)
{
int16_t x, y;
int16_t x = fr.ReadInt16();
int16_t y = fr.ReadInt16();
fr >> x >> y;
v.set(double(x), double(y));
}
}
@ -860,11 +860,10 @@ void P_LoadZSegs (FileRdr &data)
for (auto &seg : level.segs)
{
line_t *ldef;
uint32_t v1, v2;
uint16_t line;
uint8_t side;
data >> v1 >> v2 >> line >> side;
uint32_t v1 = data.ReadUInt32();
uint32_t v2 = data.ReadUInt32();
uint16_t line = data.ReadUInt16();
uint8_t side = data.ReadUInt8();
seg.v1 = &level.vertexes[v1];
seg.v2 = &level.vertexes[v2];
@ -899,22 +898,20 @@ void P_LoadGLZSegs (FileRdr &data, int type)
for (size_t j = 0; j < level.subsectors[i].numlines; ++j)
{
seg_t *seg;
uint32_t v1, partner;
uint32_t v1 = data.ReadUInt32();
uint32_t partner = data.ReadUInt32();
uint32_t line;
uint16_t lineword;
uint8_t side;
data >> v1 >> partner;
if (type >= 2)
{
data >> line;
line = data.ReadUInt32();
}
else
{
data >> lineword;
line = lineword == 0xFFFF ? 0xFFFFFFFF : lineword;
line = data.ReadUInt16();
if (line == 0xffff) line = 0xffffffff;
}
data >> side;
uint8_t side = data.ReadUInt8();
seg = level.subsectors[i].firstline + j;
seg->v1 = &level.vertexes[v1];
@ -964,10 +961,10 @@ void P_LoadGLZSegs (FileRdr &data, int type)
void LoadZNodes(FileRdr &data, int glnodes)
{
// Read extra vertices added during node building
uint32_t orgVerts, newVerts;
unsigned int i;
data >> orgVerts >> newVerts;
uint32_t orgVerts = data.ReadUInt32();
uint32_t newVerts = data.ReadUInt32();
if (orgVerts > level.vertexes.Size())
{ // These nodes are based on a map with more vertex data than we have.
// We can't use them.
@ -980,8 +977,8 @@ void LoadZNodes(FileRdr &data, int glnodes)
}
for (i = 0; i < newVerts; ++i)
{
int32_t x, y;
data >> x >> y;
fixed_t x = data.ReadInt32();
fixed_t y = data.ReadInt32();
level.vertexes[i + orgVerts].set(x, y);
}
if (oldvertexes != &level.vertexes[0])
@ -994,26 +991,21 @@ void LoadZNodes(FileRdr &data, int glnodes)
}
// Read the subsectors
uint32_t numSubs, currSeg;
data >> numSubs;
uint32_t currSeg;
uint32_t numSubs = data.ReadUInt32();
level.subsectors.Alloc(numSubs);
memset (&level.subsectors[0], 0, level.subsectors.Size()*sizeof(subsector_t));
for (i = currSeg = 0; i < numSubs; ++i)
{
uint32_t numsegs;
data >> numsegs;
uint32_t numsegs = data.ReadUInt32();
level.subsectors[i].firstline = (seg_t *)(size_t)currSeg; // Oh damn. I should have stored the seg count sooner.
level.subsectors[i].numlines = numsegs;
currSeg += numsegs;
}
// Read the segs
uint32_t numSegs;
data >> numSegs;
uint32_t numSegs = data.ReadUInt32();
// The number of segs stored should match the number of
// segs used by subsectors.
@ -1040,9 +1032,8 @@ void LoadZNodes(FileRdr &data, int glnodes)
}
// Read nodes
uint32_t numNodes;
uint32_t numNodes = data.ReadUInt32();
data >> numNodes;
auto &nodes = level.nodes;
nodes.Alloc(numNodes);
memset (&nodes[0], 0, sizeof(node_t)*numNodes);
@ -1051,31 +1042,28 @@ void LoadZNodes(FileRdr &data, int glnodes)
{
if (glnodes < 3)
{
int16_t x, y, dx, dy;
data >> x >> y >> dx >> dy;
nodes[i].x = x << FRACBITS;
nodes[i].y = y << FRACBITS;
nodes[i].dx = dx << FRACBITS;
nodes[i].dy = dy << FRACBITS;
nodes[i].x = data.ReadInt16() * FRACUNIT;
nodes[i].y = data.ReadInt16() * FRACUNIT;
nodes[i].dx = data.ReadInt16() * FRACUNIT;
nodes[i].dy = data.ReadInt16() * FRACUNIT;
}
else
{
data >> nodes[i].x >> nodes[i].y >> nodes[i].dx >> nodes[i].dy;
nodes[i].x = data.ReadInt32();
nodes[i].y = data.ReadInt32();
nodes[i].dx = data.ReadInt32();
nodes[i].dy = data.ReadInt32();
}
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < 4; ++k)
{
int16_t coord;
data >> coord;
nodes[i].bbox[j][k] = coord;
nodes[i].bbox[j][k] = data.ReadInt16();
}
}
for (int m = 0; m < 2; ++m)
{
uint32_t child;
data >> child;
uint32_t child = data.ReadUInt32();
if (child & 0x80000000)
{
nodes[i].children[m] = (uint8_t *)&level.subsectors[child & 0x7FFFFFFF] + 1;
@ -1399,7 +1387,8 @@ void P_LoadSubsectors (MapData * map)
{
subsectortype subd;
fr >> subd.numsegs >> subd.firstseg;
subd.numsegs = sizeof(subd.numsegs) == 2 ? fr.ReadUInt16() : fr.ReadUInt32();
subd.firstseg = sizeof(subd.firstseg) == 2 ? fr.ReadUInt16() : fr.ReadUInt32();
if (subd.numsegs == 0)
{

View file

@ -1520,8 +1520,7 @@ void SFFile::LoadSample(SFSample *sample)
// Load 16-bit sample data.
for (i = 0; i < sample->End - sample->Start; ++i)
{
int16_t samp;
fp >> samp;
int16_t samp = fp.ReadInt16();
sample->InMemoryData[i] = samp / 32768.f;
}
if (SampleDataLSBOffset != 0)
@ -1529,8 +1528,7 @@ void SFFile::LoadSample(SFSample *sample)
fp.Seek(SampleDataLSBOffset + sample->Start, FileRdr::SeekSet);
for (i = 0; i < sample->End - sample->Start; ++i)
{
uint8_t samp;
fp >> samp;
uint8_t samp = fp.ReadUInt8();
sample->InMemoryData[i] = ((((int32_t(sample->InMemoryData[i] * 32768) << 8) | samp) << 8) >> 8) / 8388608.f;
}
}

View file

@ -92,7 +92,10 @@ FTexture *IMGZTexture_TryCreate(FileRdr & file, int lumpnum)
file.Seek(0, FileRdr::SeekSet);
if (file.Read(&magic, 4) != 4) return NULL;
if (magic != MAKE_ID('I','M','G','Z')) return NULL;
file >> w >> h >> l >> t;
w = file.ReadUInt16();
h = file.ReadUInt16();
l = file.ReadInt16();
t = file.ReadInt16();
return new FIMGZTexture(lumpnum, w, h, l, t);
}

View file

@ -814,9 +814,8 @@ void FTextureManager::AddTexturesLump (const void *lumpdata, int lumpsize, int d
}
{
auto pnames = Wads.OpenLumpReader (patcheslump);
pnames >> numpatches;
auto pnames = Wads.OpenLumpReader(patcheslump);
numpatches = pnames.ReadUInt32();
// Check whether the amount of names reported is correct.
if ((signed)numpatches < 0)

View file

@ -135,7 +135,10 @@ FTexture *PatchTexture_TryCreate(FileRdr & file, int lumpnum)
if (!CheckIfPatch(file)) return NULL;
file.Seek(0, FileRdr::SeekSet);
file >> header.width >> header.height >> header.leftoffset >> header.topoffset;
header.width = file.ReadUInt16();
header.height = file.ReadUInt16();
header.leftoffset = file.ReadInt16();
header.topoffset = file.ReadInt16();
return new FPatchTexture(lumpnum, &header);
}

View file

@ -493,15 +493,15 @@ void FPCXTexture::MakeTexture()
}
else if (bitcount == 8)
{
uint8_t c;
lump.Seek(-769, FileRdr::SeekEnd);
lump >> c;
uint8_t c = lump.ReadUInt8();
//if (c !=0x0c) memcpy(PaletteMap, GrayMap, 256); // Fallback for files without palette
//else
for(int i=0;i<256;i++)
{
uint8_t r,g,b;
lump >> r >> g >> b;
uint8_t r = lump.ReadUInt8();
uint8_t g = lump.ReadUInt8();
uint8_t b = lump.ReadUInt8();
PaletteMap[i] = ColorMatcher.Pick(r,g,b);
}
lump.Seek(sizeof(header), FileRdr::SeekSet);
@ -582,9 +582,8 @@ int FPCXTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCo
}
else if (bitcount == 8)
{
uint8_t c;
lump.Seek(-769, FileRdr::SeekEnd);
lump >> c;
uint8_t c = lump.ReadUInt8();
c=0x0c; // Apparently there's many non-compliant PCXs out there...
if (c !=0x0c)
{
@ -592,8 +591,9 @@ int FPCXTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCo
}
else for(int i=0;i<256;i++)
{
uint8_t r,g,b;
lump >> r >> g >> b;
uint8_t r = lump.ReadUInt8();
uint8_t g = lump.ReadUInt8();
uint8_t b = lump.ReadUInt8();
pe[i] = PalEntry(255, r,g,b);
}
lump.Seek(sizeof(header), FileRdr::SeekSet);

View file

@ -99,8 +99,6 @@ FTexture *PNGTexture_TryCreate(FileRdr & data, int lumpnum)
uint8_t b[4];
} first4bytes;
uint32_t width, height;
uint8_t bitdepth, colortype, compression, filter, interlace;
// This is most likely a PNG, but make sure. (Note that if the
// first 4 bytes match, but later bytes don't, we assume it's
@ -118,9 +116,13 @@ FTexture *PNGTexture_TryCreate(FileRdr & data, int lumpnum)
// The PNG looks valid so far. Check the IHDR to make sure it's a
// type of PNG we support.
data.Read(&width, 4);
data.Read(&height, 4);
data >> bitdepth >> colortype >> compression >> filter >> interlace;
int width = data.ReadInt32BE();
int height = data.ReadInt32BE();
uint8_t bitdepth = data.ReadUInt8();
uint8_t colortype = data.ReadUInt8();
uint8_t compression = data.ReadUInt8();
uint8_t filter = data.ReadUInt8();
uint8_t interlace = data.ReadUInt8();
if (compression != 0 || filter != 0 || interlace > 1)
{
@ -147,8 +149,7 @@ FTexture *PNGTexture_TryCreate(FileRdr & data, int lumpnum)
}
}
return new FPNGTexture (data, lumpnum, FString(), BigLong((int)width), BigLong((int)height),
bitdepth, colortype, interlace);
return new FPNGTexture (data, lumpnum, FString(), width, height, bitdepth, colortype, interlace);
}
//==========================================================================
@ -159,8 +160,6 @@ FTexture *PNGTexture_TryCreate(FileRdr & data, int lumpnum)
FTexture *PNGTexture_CreateFromFile(PNGHandle *png, const FString &filename)
{
uint32_t width, height;
uint8_t bitdepth, colortype, compression, filter, interlace;
if (M_FindPNGChunk(png, MAKE_ID('I','H','D','R')) == 0)
{
@ -168,9 +167,14 @@ FTexture *PNGTexture_CreateFromFile(PNGHandle *png, const FString &filename)
}
// Check the IHDR to make sure it's a type of PNG we support.
png->File.Read(&width, 4);
png->File.Read(&height, 4);
png->File >> bitdepth >> colortype >> compression >> filter >> interlace;
auto &data = png->File;
int width = data.ReadInt32BE();
int height = data.ReadInt32BE();
uint8_t bitdepth = data.ReadUInt8();
uint8_t colortype = data.ReadUInt8();
uint8_t compression = data.ReadUInt8();
uint8_t filter = data.ReadUInt8();
uint8_t interlace = data.ReadUInt8();
if (compression != 0 || filter != 0 || interlace > 1)
{
@ -185,8 +189,7 @@ FTexture *PNGTexture_CreateFromFile(PNGHandle *png, const FString &filename)
return NULL;
}
return new FPNGTexture (png->File, -1, filename, BigLong((int)width), BigLong((int)height),
bitdepth, colortype, interlace);
return new FPNGTexture (png->File, -1, filename, width, height, bitdepth, colortype, interlace);
}
//==========================================================================
@ -651,7 +654,9 @@ int FPNGTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCo
case MAKE_ID('P','L','T','E'):
for(int i = 0; i < PaletteSize; i++)
{
(*lump) >> pe[i].r >> pe[i].g >> pe[i].b;
pe[i].r = lump->ReadUInt8();
pe[i].g = lump->ReadUInt8();
pe[i].b = lump->ReadUInt8();
}
break;
@ -660,7 +665,7 @@ int FPNGTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCo
{
for(uint32_t i = 0; i < len; i++)
{
(*lump) >> pe[i].a;
pe[i].a = lump->ReadUInt8();
if (pe[i].a != 0 && pe[i].a != 255)
transpal = true;
}

View file

@ -768,7 +768,7 @@ void FTextureManager::AddPatches (int lumpnum)
uint32_t numpatches, i;
char name[9];
file >> numpatches;
numpatches = file.ReadUInt32();
name[8] = '\0';
for (i = 0; i < numpatches; ++i)
@ -1186,9 +1186,8 @@ int FTextureManager::CountLumpTextures (int lumpnum)
if (lumpnum >= 0)
{
auto file = Wads.OpenLumpReader (lumpnum);
uint32_t numtex;
uint32_t numtex = file.ReadUInt32();;
file >> numtex;
return int(numtex) >= 0 ? numtex : 0;
}
return 0;

View file

@ -252,13 +252,12 @@ const uint8_t *FTGATexture::GetPixels ()
void FTGATexture::ReadCompressed(FileRdr &lump, uint8_t * buffer, int bytesperpixel)
{
uint8_t b;
uint8_t data[4];
int Size = Width * Height;
while (Size > 0)
{
lump >> b;
uint8_t b = lump.ReadUInt8();
if (b & 128)
{
b&=~128;
@ -314,7 +313,7 @@ void FTGATexture::MakeTexture ()
{
case 15:
case 16:
lump >> w;
w = lump.ReadUInt16();
r = (w & 0x001F) << 3;
g = (w & 0x03E0) >> 2;
b = (w & 0x7C00) >> 7;
@ -322,12 +321,17 @@ void FTGATexture::MakeTexture ()
break;
case 24:
lump >> b >> g >> r;
b = lump.ReadUInt8();
g = lump.ReadUInt8();
r = lump.ReadUInt8();
a=255;
break;
case 32:
lump >> b >> g >> r >> a;
b = lump.ReadUInt8();
g = lump.ReadUInt8();
r = lump.ReadUInt8();
a = lump.ReadUInt8();
if ((hdr.img_desc&15)!=8) a=255;
break;
@ -515,24 +519,29 @@ int FTGATexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCo
{
case 15:
case 16:
lump >> w;
w = lump.ReadUInt16();
r = (w & 0x001F) << 3;
g = (w & 0x03E0) >> 2;
b = (w & 0x7C00) >> 7;
a = 255;
break;
case 24:
lump >> b >> g >> r;
a=255;
b = lump.ReadUInt8();
g = lump.ReadUInt8();
r = lump.ReadUInt8();
a = 255;
break;
case 32:
lump >> b >> g >> r >> a;
if ((hdr.img_desc&15)!=8) a=255;
else if (a!=0 && a!=255) transval = true;
b = lump.ReadUInt8();
g = lump.ReadUInt8();
r = lump.ReadUInt8();
a = lump.ReadUInt8();
if ((hdr.img_desc & 15) != 8) a = 255;
else if (a != 0 && a != 255) transval = true;
break;
default: // should never happen
r=g=b=a=0;
break;

View file

@ -1899,9 +1899,7 @@ void FFontChar2::MakeTexture ()
{
if (runlen != 0)
{
uint8_t color;
lump >> color;
uint8_t color = lump.ReadUInt8();
color = MIN (color, max);
if (SourceRemap != NULL)
{
@ -1921,18 +1919,14 @@ void FFontChar2::MakeTexture ()
}
else
{
int8_t code;
lump >> code;
int8_t code = lump.ReadInt8();
if (code >= 0)
{
runlen = code + 1;
}
else if (code != -128)
{
uint8_t color;
lump >> color;
uint8_t color = lump.ReadUInt8();
setlen = (-code) + 1;
setval = MIN (color, max);
if (SourceRemap != NULL)
@ -1951,8 +1945,7 @@ void FFontChar2::MakeTexture ()
{
for (int x = Width; x != 0; --x)
{
uint8_t color;
lump >> color;
uint8_t color = lump.ReadUInt8();
if (color > max)
{
color = max;