diff --git a/src/common/audio/music/i_music.cpp b/src/common/audio/music/i_music.cpp index d7460fc768..19c8309162 100644 --- a/src/common/audio/music/i_music.cpp +++ b/src/common/audio/music/i_music.cpp @@ -182,7 +182,7 @@ static void SetupGenMidi() auto genmidi = fileSystem.ReadFile(lump); if (genmidi.GetSize() < 8 + 175 * 36 || memcmp(genmidi.GetMem(), "#OPL_II#", 8)) return; - ZMusic_SetGenMidi((uint8_t*)genmidi.GetString() + 8); + ZMusic_SetGenMidi(genmidi.GetBytes() + 8); } static void SetupWgOpn() diff --git a/src/common/engine/sc_man.cpp b/src/common/engine/sc_man.cpp index 21a777756f..cc72273a18 100644 --- a/src/common/engine/sc_man.cpp +++ b/src/common/engine/sc_man.cpp @@ -200,8 +200,11 @@ void FScanner :: OpenLumpNum (int lump) { Close (); { - FileData mem = fileSystem.ReadFile(lump); - ScriptBuffer = mem.GetString(); + auto mem = fileSystem.OpenFileReader(lump); + auto buff = ScriptBuffer.LockNewBuffer(mem.GetLength()); + mem.Read(buff, mem.GetLength()); + buff[mem.GetLength()] = 0; + ScriptBuffer.UnlockBuffer(); } ScriptName = fileSystem.GetFileFullPath(lump).c_str(); LumpNum = lump; diff --git a/src/common/rendering/gl/gl_shader.cpp b/src/common/rendering/gl/gl_shader.cpp index 85ba5c1ebe..afa05b8630 100644 --- a/src/common/rendering/gl/gl_shader.cpp +++ b/src/common/rendering/gl/gl_shader.cpp @@ -375,11 +375,9 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char * int vp_lump = fileSystem.CheckNumForFullName(vert_prog_lump, 0); if (vp_lump == -1) I_Error("Unable to load '%s'", vert_prog_lump); - FileData vp_data = fileSystem.ReadFile(vp_lump); int fp_lump = fileSystem.CheckNumForFullName(frag_prog_lump, 0); if (fp_lump == -1) I_Error("Unable to load '%s'", frag_prog_lump); - FileData fp_data = fileSystem.ReadFile(fp_lump); @@ -410,8 +408,8 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char * vp_comb << "#line 1\n"; fp_comb << "#line 1\n"; - vp_comb << RemoveLayoutLocationDecl(vp_data.GetString(), "out").GetChars() << "\n"; - fp_comb << RemoveLayoutLocationDecl(fp_data.GetString(), "in").GetChars() << "\n"; + vp_comb << RemoveLayoutLocationDecl(GetStringFromLump(vp_lump), "out").GetChars() << "\n"; + fp_comb << RemoveLayoutLocationDecl(GetStringFromLump(fp_lump), "in").GetChars() << "\n"; FString placeholder = "\n"; if (proc_prog_lump != NULL) @@ -423,7 +421,7 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char * int pp_lump = fileSystem.CheckNumForFullName(proc_prog_lump, 0); // if it's a core shader, ignore overrides by user mods. if (pp_lump == -1) pp_lump = fileSystem.CheckNumForFullName(proc_prog_lump); if (pp_lump == -1) I_Error("Unable to load '%s'", proc_prog_lump); - FString pp_data = fileSystem.ReadFile(pp_lump).GetString(); + FString pp_data = GetStringFromLump(pp_lump); if (pp_data.IndexOf("ProcessMaterial") < 0 && pp_data.IndexOf("SetupMaterial") < 0) { @@ -433,15 +431,13 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char * { int pl_lump = fileSystem.CheckNumForFullName("shaders/glsl/func_defaultmat2.fp", 0); if (pl_lump == -1) I_Error("Unable to load '%s'", "shaders/glsl/func_defaultmat2.fp"); - FileData pl_data = fileSystem.ReadFile(pl_lump); - fp_comb << "\n" << pl_data.GetString(); + fp_comb << "\n" << GetStringFromLump(pl_lump); } else { int pl_lump = fileSystem.CheckNumForFullName("shaders/glsl/func_defaultmat.fp", 0); if (pl_lump == -1) I_Error("Unable to load '%s'", "shaders/glsl/func_defaultmat.fp"); - FileData pl_data = fileSystem.ReadFile(pl_lump); - fp_comb << "\n" << pl_data.GetString(); + fp_comb << "\n" << GetStringFromLump(pl_lump); if (pp_data.IndexOf("ProcessTexel") < 0) { @@ -467,8 +463,7 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char * { int pl_lump = fileSystem.CheckNumForFullName("shaders/glsl/func_defaultlight.fp", 0); if (pl_lump == -1) I_Error("Unable to load '%s'", "shaders/glsl/func_defaultlight.fp"); - FileData pl_data = fileSystem.ReadFile(pl_lump); - fp_comb << "\n" << pl_data.GetString(); + fp_comb << "\n" << GetStringFromLump(pl_lump); } // ProcessMaterial must be considered broken because it requires the user to fill in data they possibly cannot know all about. @@ -490,8 +485,7 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char * { int pp_lump = fileSystem.CheckNumForFullName(light_fragprog, 0); if (pp_lump == -1) I_Error("Unable to load '%s'", light_fragprog); - FileData pp_data = fileSystem.ReadFile(pp_lump); - fp_comb << pp_data.GetString() << "\n"; + fp_comb << GetStringFromLump(pp_lump) << "\n"; } if (gl.flags & RFL_NO_CLIP_PLANES) diff --git a/src/common/rendering/gl/gl_shaderprogram.cpp b/src/common/rendering/gl/gl_shaderprogram.cpp index 69729a8c7a..feca86aa89 100644 --- a/src/common/rendering/gl/gl_shaderprogram.cpp +++ b/src/common/rendering/gl/gl_shaderprogram.cpp @@ -28,6 +28,7 @@ #include "hw_shaderpatcher.h" #include "filesystem.h" #include "printf.h" +#include "cmdlib.h" namespace OpenGLRenderer { @@ -88,7 +89,9 @@ void FShaderProgram::Compile(ShaderType type, const char *lumpName, const char * { int lump = fileSystem.CheckNumForFullName(lumpName); if (lump == -1) I_FatalError("Unable to load '%s'", lumpName); - FString code = fileSystem.ReadFile(lump).GetString(); + auto sp = fileSystem.ReadFile(lump); + FString code = GetStringFromLump(lump); + Compile(type, lumpName, code, defines, maxGlslVersion); } diff --git a/src/common/rendering/gles/gles_shader.cpp b/src/common/rendering/gles/gles_shader.cpp index 16ff0eb65c..8f9103c610 100644 --- a/src/common/rendering/gles/gles_shader.cpp +++ b/src/common/rendering/gles/gles_shader.cpp @@ -410,8 +410,8 @@ bool FShader::Load(const char * name, const char * vert_prog_lump_, const char * vp_comb << "#line 1\n"; fp_comb << "#line 1\n"; - vp_comb << RemoveLayoutLocationDecl(vp_data.GetString(), "out").GetChars() << "\n"; - fp_comb << RemoveLayoutLocationDecl(fp_data.GetString(), "in").GetChars() << "\n"; + vp_comb << RemoveLayoutLocationDecl(GetStringFromLump(vp_lump), "out").GetChars() << "\n"; + fp_comb << RemoveLayoutLocationDecl(GetStringFromLump(fp_lump), "in").GetChars() << "\n"; FString placeholder = "\n"; if (proc_prog_lump.Len()) @@ -422,7 +422,8 @@ bool FShader::Load(const char * name, const char * vert_prog_lump_, const char * { int pp_lump = fileSystem.CheckNumForFullName(proc_prog_lump); if (pp_lump == -1) I_Error("Unable to load '%s'", proc_prog_lump.GetChars()); - FString pp_data = fileSystem.ReadFile(pp_lump).GetString(); + auto ppf = fileSystem.ReadFile(pp_lump); + FString pp_data = GetStringFromLump(pp_lump); if (pp_data.IndexOf("ProcessMaterial") < 0 && pp_data.IndexOf("SetupMaterial") < 0) { @@ -432,15 +433,13 @@ bool FShader::Load(const char * name, const char * vert_prog_lump_, const char * { int pl_lump = fileSystem.CheckNumForFullName("shaders_gles/glsl/func_defaultmat2.fp", 0); if (pl_lump == -1) I_Error("Unable to load '%s'", "shaders_gles/glsl/func_defaultmat2.fp"); - FileData pl_data = fileSystem.ReadFile(pl_lump); - fp_comb << "\n" << pl_data.GetString(); + fp_comb << "\n" << GetStringFromLump(pl_lump); } else { int pl_lump = fileSystem.CheckNumForFullName("shaders_gles/glsl/func_defaultmat.fp", 0); if (pl_lump == -1) I_Error("Unable to load '%s'", "shaders_gles/glsl/func_defaultmat.fp"); - FileData pl_data = fileSystem.ReadFile(pl_lump); - fp_comb << "\n" << pl_data.GetString(); + fp_comb << "\n" << GetStringFromLump(pl_lump); if (pp_data.IndexOf("ProcessTexel") < 0) { @@ -466,8 +465,7 @@ bool FShader::Load(const char * name, const char * vert_prog_lump_, const char * { int pl_lump = fileSystem.CheckNumForFullName("shaders_gles/glsl/func_defaultlight.fp", 0); if (pl_lump == -1) I_Error("Unable to load '%s'", "shaders_gles/glsl/func_defaultlight.fp"); - FileData pl_data = fileSystem.ReadFile(pl_lump); - fp_comb << "\n" << pl_data.GetString(); + fp_comb << "\n" << GetStringFromLump(pl_lump); } // ProcessMaterial must be considered broken because it requires the user to fill in data they possibly cannot know all about. @@ -489,8 +487,7 @@ bool FShader::Load(const char * name, const char * vert_prog_lump_, const char * { int pp_lump = fileSystem.CheckNumForFullName(light_fragprog, 0); if (pp_lump == -1) I_Error("Unable to load '%s'", light_fragprog.GetChars()); - FileData pp_data = fileSystem.ReadFile(pp_lump); - fp_comb << pp_data.GetString() << "\n"; + fp_comb << GetStringFromLump(pp_lump) << "\n"; } if (gles.flags & RFL_NO_CLIP_PLANES) diff --git a/src/common/rendering/gles/gles_shaderprogram.cpp b/src/common/rendering/gles/gles_shaderprogram.cpp index 8677257f33..289008c219 100644 --- a/src/common/rendering/gles/gles_shaderprogram.cpp +++ b/src/common/rendering/gles/gles_shaderprogram.cpp @@ -26,6 +26,7 @@ #include "hw_shaderpatcher.h" #include "filesystem.h" #include "printf.h" +#include "cmdlib.h" namespace OpenGLESRenderer { @@ -88,7 +89,8 @@ void FShaderProgram::Compile(ShaderType type, const char *lumpName, const char * { int lump = fileSystem.CheckNumForFullName(lumpName); if (lump == -1) I_FatalError("Unable to load '%s'", lumpName); - FString code = fileSystem.ReadFile(lump).GetString(); + auto sp = fileSystem.ReadFile(lump); + FString code = GetStringFromLump(lump); Compile(type, lumpName, code, defines, maxGlslVersion); } diff --git a/src/common/rendering/vulkan/shaders/vk_ppshader.cpp b/src/common/rendering/vulkan/shaders/vk_ppshader.cpp index f83b01c938..d9f6078c73 100644 --- a/src/common/rendering/vulkan/shaders/vk_ppshader.cpp +++ b/src/common/rendering/vulkan/shaders/vk_ppshader.cpp @@ -26,6 +26,7 @@ #include "zvulkan/vulkanbuilders.h" #include "vulkan/system/vk_commandbuffer.h" #include "filesystem.h" +#include "cmdlib.h" VkPPShader::VkPPShader(VulkanRenderDevice* fb, PPShader *shader) : fb(fb) { @@ -66,7 +67,8 @@ FString VkPPShader::LoadShaderCode(const FString &lumpName, const FString &defin { int lump = fileSystem.CheckNumForFullName(lumpName); if (lump == -1) I_FatalError("Unable to load '%s'", lumpName.GetChars()); - FString code = fileSystem.ReadFile(lump).GetString(); + auto sp = fileSystem.ReadFile(lump); + FString code = GetStringFromLump(lump); FString patchedCode; patchedCode.AppendFormat("#version %d\n", 450); diff --git a/src/common/rendering/vulkan/shaders/vk_shader.cpp b/src/common/rendering/vulkan/shaders/vk_shader.cpp index 2c3eb9241b..3784543b33 100644 --- a/src/common/rendering/vulkan/shaders/vk_shader.cpp +++ b/src/common/rendering/vulkan/shaders/vk_shader.cpp @@ -466,8 +466,7 @@ FString VkShaderManager::LoadPublicShaderLump(const char *lumpname) int lump = fileSystem.CheckNumForFullName(lumpname, 0); if (lump == -1) lump = fileSystem.CheckNumForFullName(lumpname); if (lump == -1) I_Error("Unable to load '%s'", lumpname); - FileData data = fileSystem.ReadFile(lump); - return data.GetString(); + return GetStringFromLump(lump); } FString VkShaderManager::LoadPrivateShaderLump(const char *lumpname) @@ -475,7 +474,7 @@ FString VkShaderManager::LoadPrivateShaderLump(const char *lumpname) int lump = fileSystem.CheckNumForFullName(lumpname, 0); if (lump == -1) I_Error("Unable to load '%s'", lumpname); FileData data = fileSystem.ReadFile(lump); - return data.GetString(); + return GetStringFromLump(lump); } VkPPShader* VkShaderManager::GetVkShader(PPShader* shader) diff --git a/src/common/scripting/interface/vmnatives.cpp b/src/common/scripting/interface/vmnatives.cpp index 636c340ae6..3e75e75d8a 100644 --- a/src/common/scripting/interface/vmnatives.cpp +++ b/src/common/scripting/interface/vmnatives.cpp @@ -841,7 +841,7 @@ DEFINE_ACTION_FUNCTION(_Wads, ReadLump) PARAM_PROLOGUE; PARAM_INT(lump); const bool isLumpValid = lump >= 0 && lump < fileSystem.GetNumEntries(); - ACTION_RETURN_STRING(isLumpValid ? fileSystem.ReadFile(lump).GetString() : ""); + ACTION_RETURN_STRING(isLumpValid ? GetStringFromLump(lump) : FString()); } //========================================================================== diff --git a/src/common/textures/animlib.cpp b/src/common/textures/animlib.cpp index 949027786a..1b31244f30 100644 --- a/src/common/textures/animlib.cpp +++ b/src/common/textures/animlib.cpp @@ -217,7 +217,7 @@ static inline void drawframe(anim_t *anim, uint16_t framenumber) } // is the file size, for consistency checking. -int32_t ANIM_LoadAnim(anim_t *anim, uint8_t *buffer, size_t length) +int32_t ANIM_LoadAnim(anim_t *anim, const uint8_t *buffer, size_t length) { if (memcmp(buffer, "LPF ", 4)) return -1; diff --git a/src/common/textures/animlib.h b/src/common/textures/animlib.h index 1fdaa354fd..26fa124f40 100644 --- a/src/common/textures/animlib.h +++ b/src/common/textures/animlib.h @@ -92,7 +92,7 @@ struct anim_t lp_descriptor * curlp; // header of large page currently in memory uint16_t * thepage; // buffer where current large page is loaded uint8_t imagebuffer[IMAGEBUFFERSIZE]; // buffer where anim frame is decoded - uint8_t * buffer; + const uint8_t * buffer; uint8_t pal[768]; int32_t currentframe; }; @@ -105,7 +105,7 @@ struct anim_t // //**************************************************************************** -int32_t ANIM_LoadAnim(anim_t *anim, uint8_t *buffer, size_t length); +int32_t ANIM_LoadAnim(anim_t *anim, const uint8_t *buffer, size_t length); //**************************************************************************** // diff --git a/src/common/textures/formats/anmtexture.cpp b/src/common/textures/formats/anmtexture.cpp index f4fb332f0d..f57b0e31e6 100644 --- a/src/common/textures/formats/anmtexture.cpp +++ b/src/common/textures/formats/anmtexture.cpp @@ -104,7 +104,7 @@ FAnmTexture::FAnmTexture (int lumpnum, int w, int h) void FAnmTexture::ReadFrame(uint8_t *pixels, uint8_t *palette) { FileData lump = fileSystem.ReadFile (SourceLump); - uint8_t *source = (uint8_t *)lump.GetMem(); + auto source = lump.GetBytes(); anim_t anim; if (ANIM_LoadAnim(&anim, source, (int)lump.GetSize()) >= 0) diff --git a/src/common/utility/cmdlib.cpp b/src/common/utility/cmdlib.cpp index 1e906e69c6..b5ac83415e 100644 --- a/src/common/utility/cmdlib.cpp +++ b/src/common/utility/cmdlib.cpp @@ -36,6 +36,7 @@ #include "cmdlib.h" #include "fs_findfile.h" +#include "filesystem.h" #include "files.h" #include "md5.h" @@ -1008,3 +1009,17 @@ void uppercopy(char* to, const char* from) to[i] = 0; } +//========================================================================== +// +// GetStringFromLump +// +// Loads a zero terminated string from a lump in the file system +//========================================================================== + +FString GetStringFromLump(int lump) +{ + FileData fd = fileSystem.ReadFile(lump); + FString ScriptBuffer(fd.GetString(), fd.GetSize()); + ScriptBuffer.Truncate(strlen(ScriptBuffer.GetChars())); // this is necessary to properly truncate the generated string to not contain 0 bytes. + return ScriptBuffer; +} diff --git a/src/common/utility/cmdlib.h b/src/common/utility/cmdlib.h index e2e4aadbd0..9d94d0d3f2 100644 --- a/src/common/utility/cmdlib.h +++ b/src/common/utility/cmdlib.h @@ -87,6 +87,7 @@ struct MD5Context; void md5Update(FileReader& file, MD5Context& md5, unsigned len); void uppercopy(char* to, const char* from); +FString GetStringFromLump(int lump); inline void fillshort(void* buff, size_t count, uint16_t clear) { diff --git a/src/common/utility/palette.cpp b/src/common/utility/palette.cpp index 7598f5d4ef..178db734b5 100644 --- a/src/common/utility/palette.cpp +++ b/src/common/utility/palette.cpp @@ -681,8 +681,8 @@ FString V_GetColorStringByName(const char* name, FScriptPosition* sc) } auto rgbNames = fileSystem.ReadFile(rgblump); - rgb = (char*)rgbNames.GetMem(); - rgbEnd = rgb + fileSystem.FileLength(rgblump); + rgb = rgbNames.GetString(); + rgbEnd = rgb + rgbNames.GetSize(); step = 0; namelen = strlen(name); diff --git a/src/gamedata/keysections.cpp b/src/gamedata/keysections.cpp index 390c8009f8..8561b6b247 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 = data.GetString() + fileSystem.FileLength (lump); - const char *conf = data.GetString(); + const char* conf = data.GetString(); + const char *eof = conf + data.GetSize(); while (conf < eof) { diff --git a/src/gamedata/xlat/parsecontext.cpp b/src/gamedata/xlat/parsecontext.cpp index 1810af55dc..0caa72f4b2 100644 --- a/src/gamedata/xlat/parsecontext.cpp +++ b/src/gamedata/xlat/parsecontext.cpp @@ -323,12 +323,12 @@ void FParseContext::ParseLump(const char *lumpname) } // Read the lump into a buffer and add a 0-terminator - auto lumpdata = fileSystem.ReadFile(lumpno); SourceLine = 0; SourceFile = lumpname; - const char *sourcep = lumpdata.GetString(); + FString source = GetStringFromLump(lumpno); + const char *sourcep = source.GetChars(); while ( (tokentype = GetToken(sourcep, &token)) ) { // It is much easier to handle include statements outside the main parser. diff --git a/src/intermission/intermission_parse.cpp b/src/intermission/intermission_parse.cpp index 5b2de2c4db..b5a51aab58 100644 --- a/src/intermission/intermission_parse.cpp +++ b/src/intermission/intermission_parse.cpp @@ -320,7 +320,7 @@ bool FIntermissionActionTextscreen::ParseKey(FScanner &sc) } } if (!done) - mText = fileSystem.ReadFile(lump).GetString(); + mText = GetStringFromLump(lump); } else { @@ -864,7 +864,7 @@ DIntermissionController* F_StartFinale (const char *music, int musicorder, int c int lump = fileSystem.CheckNumForFullName(text, true); if (lump > 0) { - textscreen->mText = fileSystem.ReadFile(lump).GetString(); + textscreen->mText = GetStringFromLump(lump); } else { diff --git a/src/maploader/glnodes.cpp b/src/maploader/glnodes.cpp index 15b4b46ba8..a72b3b7e47 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), mem.GetString())) return lump; + if (MatchHeader(fileSystem.GetFileFullName(labellump), GetStringFromLump(lump))) return lump; } } }