- FileData cleanup and uncoupling from FString.

Work is still needed to allow this to use the lump cache directly because some decisions in the past made this data padded by a zero byte.
This commit is contained in:
Christoph Oelckers 2023-08-19 22:59:11 +02:00
parent ca1d7fd539
commit a0d679b84b
24 changed files with 69 additions and 90 deletions

View file

@ -40,10 +40,7 @@
#include <ctype.h>
#include <string.h>
#include "m_argv.h"
#include "cmdlib.h"
#include "filesystem.h"
#include "m_crc32.h"
#if __has_include("md5.h")
@ -1320,7 +1317,13 @@ void FileSystem::ReadFile (int lump, void *dest)
FileData FileSystem::ReadFile (int lump)
{
return FileData(FString(*this, ELumpNum(lump)));
if ((unsigned)lump >= (unsigned)FileInfo.size())
{
throw FileSystemException("ReadFile: %u >= NumEntries", lump);
}
auto lumpp = FileInfo[lump].lump;
return FileData(lumpp);
}
//==========================================================================
@ -1522,10 +1525,6 @@ bool FileSystem::CreatePathlessCopy(const char *name, int id, int /*flags*/)
// FileData -----------------------------------------------------------------
FileData::FileData ()
{
}
FileData::FileData (const FileData &copy)
{
Block = copy.Block;
@ -1537,29 +1536,15 @@ FileData &FileData::operator = (const FileData &copy)
return *this;
}
FileData::FileData (const FString &source)
: Block (source)
FileData::FileData (FResourceLump* lump)
{
auto size = lump->LumpSize;
Block.resize(1 + size);
memcpy(Block.data(), lump->Lock(), size);
Block[size] = 0;
lump->Unlock();
}
FileData::~FileData ()
{
}
FString::FString (FileSystem& fileSystem, ELumpNum lumpnum)
{
auto lumpr = fileSystem.OpenFileReader ((int)lumpnum);
auto size = lumpr.GetLength ();
AllocBuffer (1 + size);
auto numread = lumpr.Read (&Chars[0], size);
Chars[size] = '\0';
if (numread != size)
{
throw FileSystemException("ConstructStringFromLump: Only read %ld of %ld bytes on lump %i (%s)\n",
numread, size, lumpnum, fileSystem.GetFileFullName((int)lumpnum));
}
}
//==========================================================================
//

View file

@ -29,19 +29,18 @@ union LumpShortName
class FileData
{
public:
FileData ();
FileData () = default;
FileData (const FileData &copy);
FileData &operator= (const FileData &copy);
~FileData ();
const void *GetMem () { return Block.Len() == 0 ? NULL : (void *)Block.GetChars(); }
size_t GetSize () { return Block.Len(); }
const char* GetString () const { return Block.GetChars(); }
const void *GetMem () { return Block.size() <= 1 ? NULL : (void *)Block.data(); }
size_t GetSize () { return Block.size(); }
const char* GetString () const { return (const char*)Block.data(); }
const uint8_t* GetBytes() const { return Block.data(); }
private:
FileData (const FString &source);
FileData (FResourceLump* lump);
FString Block;
std::vector<uint8_t> Block;
friend class FileSystem;
};

View file

@ -125,7 +125,7 @@ FSingleLumpFont::FSingleLumpFont (const char *name, int lump) : FFont(lump)
FontName = name;
FileData data1 = fileSystem.ReadFile (lump);
const uint8_t *data = (const uint8_t *)data1.GetMem();
auto data = data1.GetBytes();
if (data[0] == 0xE1 && data[1] == 0xE6 && data[2] == 0xD5 && data[3] == 0x1A)
{
@ -475,7 +475,7 @@ void FSingleLumpFont::LoadBMF(int lump, const uint8_t *data)
void FSingleLumpFont::CheckFON1Chars()
{
FileData memLump = fileSystem.ReadFile(Lump);
const uint8_t* data = (const uint8_t*)memLump.GetMem();
auto data = memLump.GetBytes();
const uint8_t* data_p;
data_p = data + 8;

View file

@ -173,7 +173,7 @@ unsigned FindModel(const char * path, const char * modelfile, bool silent)
int len = fileSystem.FileLength(lump);
FileData lumpd = fileSystem.ReadFile(lump);
char * buffer = (char*)lumpd.GetMem();
const char * buffer = lumpd.GetString();
if ( (size_t)fullname.LastIndexOf("_d.3d") == fullname.Len()-5 )
{

View file

@ -70,15 +70,15 @@ private:
{
uint16_t numframes, framesize;
};
d3dhead * dhead;
d3dpoly * dpolys;
a3dhead * ahead;
uint32_t * averts;
const d3dhead * dhead;
const d3dpoly * dpolys;
const a3dhead * ahead;
const uint32_t * averts;
struct dxvert
{
int16_t x, y, z, pad;
};
dxvert * dxverts;
const dxvert * dxverts;
// converted data structures
struct UE1Vertex

View file

@ -178,7 +178,7 @@ void FDMDModel::LoadGeometry()
{
static int axis[3] = { VX, VY, VZ };
FileData lumpdata = fileSystem.ReadFile(mLumpNum);
const char *buffer = (const char *)lumpdata.GetMem();
auto buffer = lumpdata.GetString();
texCoords = new FTexCoord[info.numTexCoords];
memcpy(texCoords, buffer + info.offsetTexCoords, info.numTexCoords * sizeof(FTexCoord));
@ -502,7 +502,7 @@ void FMD2Model::LoadGeometry()
static int axis[3] = { VX, VY, VZ };
uint8_t *md2_frames;
FileData lumpdata = fileSystem.ReadFile(mLumpNum);
const char *buffer = (const char *)lumpdata.GetMem();
auto buffer = lumpdata.GetString();
texCoords = new FTexCoord[info.numTexCoords];
memcpy(texCoords, (uint8_t*)buffer + info.offsetTexCoords, info.numTexCoords * sizeof(FTexCoord));

View file

@ -190,7 +190,7 @@ bool FMD3Model::Load(const char * path, int lumpnum, const char * buffer, int le
void FMD3Model::LoadGeometry()
{
FileData lumpdata = fileSystem.ReadFile(mLumpNum);
const char *buffer = (const char *)lumpdata.GetMem();
auto buffer = lumpdata.GetString();
md3_header_t * hdr = (md3_header_t *)buffer;
md3_surface_t * surf = (md3_surface_t*)(buffer + LittleLong(hdr->Ofs_Surfaces));

View file

@ -69,26 +69,25 @@ bool FUE1Model::Load( const char *filename, int lumpnum, const char *buffer, int
void FUE1Model::LoadGeometry()
{
FileData lump, lump2;
const char *buffer, *buffer2;
lump = fileSystem.ReadFile(mDataLump);
buffer = (char*)lump.GetMem();
lump2 = fileSystem.ReadFile(mAnivLump);
buffer2 = (char*)lump2.GetMem();
FileData lump = fileSystem.ReadFile(mDataLump);
buffer = lump.GetString();
FileData lump2 = fileSystem.ReadFile(mAnivLump);
buffer2 = lump2.GetString();
// map structures
dhead = (d3dhead*)(buffer);
dpolys = (d3dpoly*)(buffer+sizeof(d3dhead));
ahead = (a3dhead*)(buffer2);
dhead = (const d3dhead*)(buffer);
dpolys = (const d3dpoly*)(buffer+sizeof(d3dhead));
ahead = (const a3dhead*)(buffer2);
// detect deus ex format
if ( (ahead->framesize/dhead->numverts) == 8 )
{
averts = NULL;
dxverts = (dxvert*)(buffer2+sizeof(a3dhead));
averts = nullptr;
dxverts = (const dxvert*)(buffer2+sizeof(a3dhead));
}
else
{
averts = (uint32_t*)(buffer2+sizeof(a3dhead));
dxverts = NULL;
averts = (const uint32_t*)(buffer2+sizeof(a3dhead));
dxverts = nullptr;
}
// set counters
numVerts = dhead->numverts;

View file

@ -162,7 +162,7 @@ FVoxel *R_LoadKVX(int lumpnum)
int i, j, n;
FileData lump = fileSystem.ReadFile(lumpnum); // FileData adds an extra 0 byte to the end.
uint8_t *rawvoxel = (uint8_t *)lump.GetMem();
auto rawvoxel = lump.GetBytes();
int voxelsize = (int)(lump.GetSize()-1);
// Oh, KVX, why couldn't you have a proper header? We'll just go through

View file

@ -93,7 +93,7 @@ PalettedPixels FAutomapTexture::CreatePalettedPixels(int conversion)
{
int x, y;
FileData data = fileSystem.ReadFile (SourceLump);
const uint8_t *indata = (const uint8_t *)data.GetMem();
auto indata = data.GetBytes();
PalettedPixels Pixels(Width * Height);

View file

@ -121,7 +121,7 @@ FIMGZTexture::FIMGZTexture (int lumpnum, uint16_t w, uint16_t h, int16_t l, int1
PalettedPixels FIMGZTexture::CreatePalettedPixels(int conversion)
{
FileData lump = fileSystem.ReadFile (SourceLump);
const ImageHeader *imgz = (const ImageHeader *)lump.GetMem();
auto imgz = (const ImageHeader *)lump.GetMem();
const uint8_t *data = (const uint8_t *)&imgz[1];
uint8_t *dest_p;

View file

@ -148,7 +148,7 @@ int FQOITexture::CopyPixels(FBitmap *bmp, int conversion)
size_t p = 14, run = 0;
size_t chunks_len = lump.GetSize() - 8;
auto bytes = (const uint8_t*)lump.GetMem();
auto bytes = lump.GetBytes();
for (int h = 0; h < Height; h++)
{

View file

@ -173,7 +173,7 @@ FRawPageTexture::FRawPageTexture (int lumpnum)
PalettedPixels FRawPageTexture::CreatePalettedPixels(int conversion)
{
FileData lump = fileSystem.ReadFile (SourceLump);
const uint8_t *source = (const uint8_t *)lump.GetMem();
auto source = lump.GetBytes();
const uint8_t *source_p = source;
uint8_t *dest_p;
@ -206,8 +206,8 @@ int FRawPageTexture::CopyPixels(FBitmap *bmp, int conversion)
{
FileData lump = fileSystem.ReadFile(SourceLump);
FileData plump = fileSystem.ReadFile(mPaletteLump);
const uint8_t *source = (const uint8_t *)lump.GetMem();
const uint8_t *psource = (const uint8_t *)plump.GetMem();
auto source = lump.GetBytes();
auto psource = plump.GetBytes();
PalEntry paldata[256];
for (auto & pe : paldata)
{

View file

@ -165,7 +165,7 @@ FStartupTexture::FStartupTexture (int lumpnum)
bUseGamePalette = false;
FileData lump = fileSystem.ReadFile (SourceLump);
const uint8_t *source = (const uint8_t *)lump.GetMem();
auto source = lump.GetBytes();
// Initialize the bitmap palette.
// the palette is static so that the notches can share it.
@ -234,7 +234,7 @@ void PlanarToChunky(T* dest, const uint8_t* src, const T* remap, int width, int
PalettedPixels FStartupTexture::CreatePalettedPixels(int conversion)
{
FileData lump = fileSystem.ReadFile (SourceLump);
const uint8_t *source = (const uint8_t *)lump.GetMem();
auto source = lump.GetBytes();
const uint8_t *remap = ImageHelpers::GetRemap(conversion == luminance);
@ -254,7 +254,7 @@ PalettedPixels FStartupTexture::CreatePalettedPixels(int conversion)
int FStartupTexture::CopyPixels(FBitmap *bmp, int conversion)
{
FileData lump = fileSystem.ReadFile (SourceLump);
const uint8_t *source = (const uint8_t *)lump.GetMem();
auto source = lump.GetBytes();
PlanarToChunky((uint32_t*)bmp->GetPixels(), source + 48, startuppalette32, Width, Height);
return 0;
}
@ -282,7 +282,7 @@ FNotchTexture::FNotchTexture (int lumpnum, int width, int height)
PalettedPixels FNotchTexture::CreatePalettedPixels(int conversion)
{
FileData lump = fileSystem.ReadFile (SourceLump);
const uint8_t *source = (const uint8_t *)lump.GetMem();
auto source = lump.GetBytes();
const uint8_t *remap = ImageHelpers::GetRemap(conversion == luminance);
TArray<uint8_t> Work(Width*Height, true);
@ -305,7 +305,7 @@ PalettedPixels FNotchTexture::CreatePalettedPixels(int conversion)
int FNotchTexture::CopyPixels(FBitmap *bmp, int conversion)
{
FileData lump = fileSystem.ReadFile (SourceLump);
const uint8_t *source = (const uint8_t *)lump.GetMem();
auto source = lump.GetBytes();
auto Work = (uint32_t*)bmp->GetPixels();
for(int i = 0; i < Width * Height / 2; i++)
@ -339,7 +339,7 @@ FStrifeStartupTexture::FStrifeStartupTexture (int lumpnum, int w, int h)
PalettedPixels FStrifeStartupTexture::CreatePalettedPixels(int conversion)
{
FileData lump = fileSystem.ReadFile (SourceLump);
const uint8_t *source = (const uint8_t *)lump.GetMem();
auto source = lump.GetBytes();
PalettedPixels Pixels(Width*Height);
const uint8_t *remap = ImageHelpers::GetRemap(conversion == luminance);
ImageHelpers::FlipNonSquareBlockRemap(Pixels.Data(), source, Width, Height, Width, remap);

View file

@ -663,9 +663,8 @@ int V_GetColorFromString(const char* cstr, FScriptPosition* sc)
FString V_GetColorStringByName(const char* name, FScriptPosition* sc)
{
FileData rgbNames;
char* rgbEnd;
char* rgb, * endp;
const char* rgbEnd;
const char* rgb, * endp;
int rgblump;
int c[3], step;
size_t namelen;
@ -680,7 +679,7 @@ FString V_GetColorStringByName(const char* name, FScriptPosition* sc)
return FString();
}
rgbNames = fileSystem.ReadFile(rgblump);
auto rgbNames = fileSystem.ReadFile(rgblump);
rgb = (char*)rgbNames.GetMem();
rgbEnd = rgb + fileSystem.FileLength(rgblump);
step = 0;
@ -705,7 +704,7 @@ FString V_GetColorStringByName(const char* name, FScriptPosition* sc)
}
else if (step < 3)
{ // collect RGB values
c[step++] = strtoul(rgb, &endp, 10);
c[step++] = strtoul(rgb, (char**)&endp, 10);
if (endp == rgb)
{
break;
@ -930,7 +929,7 @@ int ReadPalette(int lumpnum, uint8_t* buffer)
return 0;
}
FileData lump = fileSystem.ReadFile(lumpnum);
uint8_t* lumpmem = (uint8_t*)lump.GetMem();
auto lumpmem = lump.GetBytes();
memset(buffer, 0, 768);
FileReader fr;

View file

@ -147,9 +147,6 @@ public:
FString (const char *head, const char *tail);
FString (char head, const FString &tail);
// Other constructors
FString (FileSystem&, ELumpNum); // Create from a lump
~FString ();
// Discard string's contents, create a new buffer, and lock it.

View file

@ -166,8 +166,8 @@ void D_LoadWadSettings ()
while ((lump = fileSystem.FindLump ("KEYCONF", &lastlump)) != -1)
{
FileData data = fileSystem.ReadFile (lump);
const char *eof = (char *)data.GetMem() + fileSystem.FileLength (lump);
const char *conf = (char *)data.GetMem();
const char *eof = data.GetString() + fileSystem.FileLength (lump);
const char *conf = data.GetString();
while (conf < eof)
{

View file

@ -67,7 +67,7 @@ void FTextureAnimator::InitSwitchList ()
if (lump != -1)
{
FileData lumpdata = fileSystem.ReadFile (lump);
const char *alphSwitchList = (const char *)lumpdata.GetMem();
auto alphSwitchList = lumpdata.GetString();
const char *list_p;
FSwitchDef *def1, *def2;

View file

@ -205,7 +205,7 @@ void FTextureAnimator::InitAnimated (void)
{
FileData animatedlump = fileSystem.ReadFile (lumpnum);
int animatedlen = fileSystem.FileLength(lumpnum);
const uint8_t *animdefs = (const uint8_t *)animatedlump.GetMem();
auto animdefs = animatedlump.GetBytes();
const uint8_t *anim_p;
FTextureID pic1, pic2;
int animtype;

View file

@ -85,7 +85,7 @@ static int BuildPaletteTranslation(int lump)
}
FileData data = fileSystem.ReadFile(lump);
const uint8_t *ipal = (const uint8_t *)data.GetMem();
auto ipal = data.GetBytes();
FRemapTable opal;
bool blood = false;

View file

@ -754,7 +754,7 @@ static int FindGLNodesInWAD(int labellump)
if (fileSystem.GetFileContainer(lump)==wadfile)
{
FileData mem = fileSystem.ReadFile(lump);
if (MatchHeader(fileSystem.GetFileFullName(labellump), (const char *)mem.GetMem())) return lump;
if (MatchHeader(fileSystem.GetFileFullName(labellump), mem.GetString())) return lump;
}
}
}

View file

@ -431,7 +431,7 @@ static void R_CreatePlayerTranslation (float h, float s, float v, const FPlayerC
else
{
FileData translump = fileSystem.ReadFile(colorset->Lump);
const uint8_t *trans = (const uint8_t *)translump.GetMem();
auto trans = translump.GetBytes();
for (i = start; i <= end; ++i)
{
table->Remap[i] = GPalette.Remap[trans[i]];

View file

@ -947,7 +947,7 @@ CCMD (skins)
static void R_CreateSkinTranslation (const char *palname)
{
FileData lump = fileSystem.ReadFile (palname);
const uint8_t *otherPal = (uint8_t *)lump.GetMem();
auto otherPal = lump.GetBytes();
for (int i = 0; i < 256; ++i)
{

View file

@ -63,7 +63,7 @@ void InitPalette ()
if (lump != -1)
{
FileData cmap = fileSystem.ReadFile(lump);
const unsigned char* cmapdata = (const unsigned char*)cmap.GetMem();
auto cmapdata = cmap.GetBytes();
GPalette.GenerateGlobalBrightmapFromColormap(cmapdata, 32);
MakeGoodRemap((uint32_t*)GPalette.BaseColors, GPalette.Remap, cmapdata + 7936); // last entry in colormap
}