From 161d4480e6da7e1cea193d608798ceb22d230bd0 Mon Sep 17 00:00:00 2001 From: Eidolon Date: Sat, 9 Mar 2024 13:51:49 -0600 Subject: [PATCH] Force rhi to only load shaders from shaders.pk3 Loading custom shaders is dangerous and error-prone and I don't want to implicitly allow this to occur without having good barriers in place. --- src/d_main.cpp | 5 ++-- src/sdl/rhi_gl2_platform.cpp | 46 +++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/d_main.cpp b/src/d_main.cpp index 3eafa8c67..d9ec49489 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -1600,7 +1600,7 @@ void D_SRB2Main(void) #ifndef DEVELOP // Check MD5s of autoloaded files // Note: Do not add any files that ignore MD5! - W_VerifyFileMD5(mainwads, ASSET_HASH_BIOS_PK3); // bios.pk3 + mainwads++; W_VerifyFileMD5(mainwads, ASSET_HASH_BIOS_PK3); // bios.pk3 #ifdef USE_PATCH_FILE mainwads++; W_VerifyFileMD5(mainwads, ASSET_HASH_PATCH_PK3); // patch.pk3 #endif @@ -1615,6 +1615,7 @@ void D_SRB2Main(void) mainwads++; W_VerifyFileMD5(mainwads, ASSET_HASH_STAFFGHOSTS_PK3); // staffghosts.pk3 mainwads++; W_VerifyFileMD5(mainwads, ASSET_HASH_SHADERS_PK3); // shaders.pk3 #else + mainwads++; // bios.pk3 #ifdef USE_PATCH_FILE mainwads++; // scripts.pk3 #endif @@ -1629,9 +1630,9 @@ void D_SRB2Main(void) mainwads++; // unlocks.pk3 #endif mainwads++; // staffghosts.pk3 + mainwads++; // shaders.pk3 #endif //ifndef DEVELOP - mainwads++; // shaders.pk3 // Load credits_def lump F_LoadCreditsDefinitions(); diff --git a/src/sdl/rhi_gl2_platform.cpp b/src/sdl/rhi_gl2_platform.cpp index 47e6203ff..58abd41a0 100644 --- a/src/sdl/rhi_gl2_platform.cpp +++ b/src/sdl/rhi_gl2_platform.cpp @@ -16,6 +16,7 @@ #include #include "../cxxutil.hpp" +#include "../doomstat.h" // mainwads #include "../w_wad.h" #include "../z_zone.h" @@ -65,11 +66,34 @@ static std::array glsllist_lump_names(rhi::PipelineProgram progr static std::vector get_sources_from_glsllist_lump(const char* lumpname) { - lumpnum_t glsllist_lump_num = W_GetNumForLongName(lumpname); - void* glsllist_lump = W_CacheLumpNum(glsllist_lump_num, PU_CACHE); - size_t glsllist_lump_length = W_LumpLength(glsllist_lump_num); + std::string shaderspk3 = "shaders.pk3"; + INT32 shaderwadnum = -1; + for (INT32 wadnum = 0; wadnum < mainwads; wadnum++) + { + std::string wadname = std::string(wadfiles[wadnum]->filename); + if (wadname.find(shaderspk3) != std::string::npos) + { + shaderwadnum = wadnum; + break; + } + } - std::istringstream glsllist(std::string(static_cast(glsllist_lump), glsllist_lump_length)); + if (shaderwadnum < 0) + { + throw std::runtime_error("Unable to identify the shaders.pk3 wadnum"); + } + + UINT16 glsllist_lump_num = W_CheckNumForLongNamePwad(lumpname, shaderwadnum, 0); + if (glsllist_lump_num == INT16_MAX) + { + throw std::runtime_error(fmt::format("Unable to find glsllist lump {}", lumpname)); + } + + std::string glsllist_lump_data; + glsllist_lump_data.resize(W_LumpLengthPwad(shaderwadnum, glsllist_lump_num)); + W_ReadLumpPwad(shaderwadnum, glsllist_lump_num, glsllist_lump_data.data()); + + std::istringstream glsllist(glsllist_lump_data); std::vector sources; for (std::string line; std::getline(glsllist, line); ) { @@ -88,11 +112,17 @@ static std::vector get_sources_from_glsllist_lump(const char* lumpn line.pop_back(); } - lumpnum_t source_lump_num = W_GetNumForLongName(line.c_str()); - void* source_lump = W_CacheLumpNum(source_lump_num, PU_CACHE); - size_t source_lump_length = W_LumpLength(source_lump_num); + UINT16 source_lump_num = W_CheckNumForLongNamePwad(line.c_str(), shaderwadnum, 0); + if (source_lump_num == INT16_MAX) + { + throw std::runtime_error(fmt::format("Unable to find glsl source lump lump {}", lumpname)); + } - sources.emplace_back(static_cast(source_lump), source_lump_length); + std::string source_lump; + source_lump.resize(W_LumpLengthPwad(shaderwadnum, source_lump_num)); + W_ReadLumpPwad(shaderwadnum, source_lump_num, source_lump.data()); + + sources.emplace_back(source_lump); } return sources;