diff --git a/src/common/filesystem/filesystem.cpp b/src/common/filesystem/filesystem.cpp index d768d5d287..1671438b53 100644 --- a/src/common/filesystem/filesystem.cpp +++ b/src/common/filesystem/filesystem.cpp @@ -40,10 +40,7 @@ #include #include -#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 ©) { Block = copy.Block; @@ -1537,29 +1536,15 @@ FileData &FileData::operator = (const FileData ©) 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)); - } -} //========================================================================== // diff --git a/src/common/filesystem/filesystem.h b/src/common/filesystem/filesystem.h index 9845757e3b..f286669070 100644 --- a/src/common/filesystem/filesystem.h +++ b/src/common/filesystem/filesystem.h @@ -29,19 +29,18 @@ union LumpShortName class FileData { public: - FileData (); - + FileData () = default; FileData (const FileData ©); FileData &operator= (const FileData ©); - ~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 Block; friend class FileSystem; }; diff --git a/src/common/fonts/singlelumpfont.cpp b/src/common/fonts/singlelumpfont.cpp index 94cbd18ecc..72a3b18053 100644 --- a/src/common/fonts/singlelumpfont.cpp +++ b/src/common/fonts/singlelumpfont.cpp @@ -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; diff --git a/src/common/models/model.cpp b/src/common/models/model.cpp index c93fa78767..ceb77bcffd 100644 --- a/src/common/models/model.cpp +++ b/src/common/models/model.cpp @@ -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 ) { diff --git a/src/common/models/model_ue1.h b/src/common/models/model_ue1.h index 9b529b44c7..4163dd2ba8 100644 --- a/src/common/models/model_ue1.h +++ b/src/common/models/model_ue1.h @@ -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 diff --git a/src/common/models/models_md2.cpp b/src/common/models/models_md2.cpp index 56ab6274c1..0dd402cf91 100644 --- a/src/common/models/models_md2.cpp +++ b/src/common/models/models_md2.cpp @@ -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)); diff --git a/src/common/models/models_md3.cpp b/src/common/models/models_md3.cpp index 2337a38973..ea2083a8ba 100644 --- a/src/common/models/models_md3.cpp +++ b/src/common/models/models_md3.cpp @@ -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)); diff --git a/src/common/models/models_ue1.cpp b/src/common/models/models_ue1.cpp index 5db36a672b..aa85678508 100644 --- a/src/common/models/models_ue1.cpp +++ b/src/common/models/models_ue1.cpp @@ -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; diff --git a/src/common/models/voxels.cpp b/src/common/models/voxels.cpp index d8d2b22454..6c7867ccf2 100644 --- a/src/common/models/voxels.cpp +++ b/src/common/models/voxels.cpp @@ -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 diff --git a/src/common/textures/formats/automaptexture.cpp b/src/common/textures/formats/automaptexture.cpp index c253650fda..6b660b1bac 100644 --- a/src/common/textures/formats/automaptexture.cpp +++ b/src/common/textures/formats/automaptexture.cpp @@ -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); diff --git a/src/common/textures/formats/imgztexture.cpp b/src/common/textures/formats/imgztexture.cpp index 733d20f844..fb020c9a10 100644 --- a/src/common/textures/formats/imgztexture.cpp +++ b/src/common/textures/formats/imgztexture.cpp @@ -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; diff --git a/src/common/textures/formats/qoitexture.cpp b/src/common/textures/formats/qoitexture.cpp index abe6282230..c84ac38ce3 100644 --- a/src/common/textures/formats/qoitexture.cpp +++ b/src/common/textures/formats/qoitexture.cpp @@ -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++) { diff --git a/src/common/textures/formats/rawpagetexture.cpp b/src/common/textures/formats/rawpagetexture.cpp index 337ca55209..e2b6c11466 100644 --- a/src/common/textures/formats/rawpagetexture.cpp +++ b/src/common/textures/formats/rawpagetexture.cpp @@ -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) { diff --git a/src/common/textures/formats/startuptexture.cpp b/src/common/textures/formats/startuptexture.cpp index 8a2a1e602f..718b7182a2 100644 --- a/src/common/textures/formats/startuptexture.cpp +++ b/src/common/textures/formats/startuptexture.cpp @@ -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 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); diff --git a/src/common/utility/palette.cpp b/src/common/utility/palette.cpp index 527d1cd173..4d1cd14443 100644 --- a/src/common/utility/palette.cpp +++ b/src/common/utility/palette.cpp @@ -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; diff --git a/src/common/utility/zstring.h b/src/common/utility/zstring.h index 89ae13568e..1164ddb6d4 100644 --- a/src/common/utility/zstring.h +++ b/src/common/utility/zstring.h @@ -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. diff --git a/src/gamedata/keysections.cpp b/src/gamedata/keysections.cpp index 5f06f192e6..390c8009f8 100644 --- a/src/gamedata/keysections.cpp +++ b/src/gamedata/keysections.cpp @@ -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) { diff --git a/src/gamedata/textures/anim_switches.cpp b/src/gamedata/textures/anim_switches.cpp index 78e49508df..36666bc3f8 100644 --- a/src/gamedata/textures/anim_switches.cpp +++ b/src/gamedata/textures/anim_switches.cpp @@ -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; diff --git a/src/gamedata/textures/animations.cpp b/src/gamedata/textures/animations.cpp index 0091a3ba57..51834d4e08 100644 --- a/src/gamedata/textures/animations.cpp +++ b/src/gamedata/textures/animations.cpp @@ -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; diff --git a/src/gamedata/textures/buildloader.cpp b/src/gamedata/textures/buildloader.cpp index 4bb7b2721a..515635871e 100644 --- a/src/gamedata/textures/buildloader.cpp +++ b/src/gamedata/textures/buildloader.cpp @@ -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; diff --git a/src/maploader/glnodes.cpp b/src/maploader/glnodes.cpp index ff44922795..15b4b46ba8 100644 --- a/src/maploader/glnodes.cpp +++ b/src/maploader/glnodes.cpp @@ -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; } } } diff --git a/src/r_data/r_translate.cpp b/src/r_data/r_translate.cpp index 0f130e30a6..1490faf260 100644 --- a/src/r_data/r_translate.cpp +++ b/src/r_data/r_translate.cpp @@ -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]]; diff --git a/src/r_data/sprites.cpp b/src/r_data/sprites.cpp index 97a5e2e46f..1f6b161a98 100644 --- a/src/r_data/sprites.cpp +++ b/src/r_data/sprites.cpp @@ -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) { diff --git a/src/r_data/v_palette.cpp b/src/r_data/v_palette.cpp index 333cb7579b..068edb124a 100644 --- a/src/r_data/v_palette.cpp +++ b/src/r_data/v_palette.cpp @@ -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 }