diff --git a/framegen/CMakeLists.txt b/framegen/CMakeLists.txt index b200c1b..f01c318 100644 --- a/framegen/CMakeLists.txt +++ b/framegen/CMakeLists.txt @@ -14,9 +14,11 @@ project(lsfg-vk-framegen LANGUAGES C CXX) file(GLOB SOURCES + "src/trans/*.cpp" "src/vk/core/*.cpp" "src/vk/pool/*.cpp" "src/vk/registry/*.cpp" + "src/vk/types/*.cpp" "src/vk/*.cpp" "src/*.cpp" "src/thirdparty/*.c" diff --git a/framegen/include/trans/dll.hpp b/framegen/include/trans/dll.hpp index 072b8e4..f99728e 100644 --- a/framegen/include/trans/dll.hpp +++ b/framegen/include/trans/dll.hpp @@ -13,7 +13,7 @@ namespace Trans::DLL { /// @param filename Path to the DLL file. /// @return A map of resource IDs to their binary data. /// - /// @throws std::runtime_error on various failure points. + /// @throws LSFG::error on various failure points. /// std::unordered_map> parseDLL( const std::filesystem::path& filename); diff --git a/framegen/src/exception.cpp b/framegen/src/exception.cpp index 9cfd801..06cbe1f 100644 --- a/framegen/src/exception.cpp +++ b/framegen/src/exception.cpp @@ -1,5 +1,6 @@ #include "exception.hpp" +#include #include #include #include diff --git a/framegen/src/lsfg.cpp b/framegen/src/lsfg.cpp index 9f61dc2..c6508f3 100644 --- a/framegen/src/lsfg.cpp +++ b/framegen/src/lsfg.cpp @@ -1,6 +1,8 @@ #include "lsfg.hpp" #include "trans/rsrc.hpp" +#include + using namespace LSFG; Instance::Instance(const std::filesystem::path& dll) diff --git a/framegen/src/trans/dll.cpp b/framegen/src/trans/dll.cpp index df05002..2b4dcfd 100644 --- a/framegen/src/trans/dll.cpp +++ b/framegen/src/trans/dll.cpp @@ -1,8 +1,8 @@ #include "trans/dll.hpp" +#include "exception.hpp" #include #include -#include #include #include #include @@ -78,7 +78,7 @@ namespace { const T* safe_cast(const std::vector& data, size_t offset) { const size_t end = offset + sizeof(T); if (end > data.size() || end < offset) - throw std::runtime_error("Buffer overflow during safe cast"); + throw LSFG::error("Buffer overflow during safe cast"); return reinterpret_cast(&data.at(offset)); } @@ -87,7 +87,7 @@ namespace { std::span span_cast(const std::vector& data, size_t offset, size_t count) { const size_t end = offset + (count * sizeof(T)); if (end > data.size() || end < offset) - throw std::runtime_error("Buffer overflow during safe cast"); + throw LSFG::error("Buffer overflow during safe cast"); return std::span(reinterpret_cast(&data.at(offset)), count); } } @@ -97,32 +97,32 @@ std::unordered_map> DLL::parseDLL( const std::filesystem::path& filename) { std::ifstream file(filename, std::ios::binary | std::ios::ate); if (!file.is_open()) - throw std::runtime_error("Failed to open Lossless.dll"); + throw LSFG::error("Failed to open Lossless.dll"); const auto size = file.tellg(); file.seekg(0, std::ios::beg); std::vector data(static_cast(size)); if (!file.read(reinterpret_cast(data.data()), size)) - throw std::runtime_error("Failed to read Lossless.dll"); + throw LSFG::error("Failed to read Lossless.dll"); // parse dos header size_t fileOffset = 0; const auto* dosHdr = safe_cast(data, 0); if (dosHdr->magic != 0x5A4D) - throw std::runtime_error("Invalid DOS header magic number"); + throw LSFG::error("Invalid DOS header magic number"); // parse pe header fileOffset += static_cast(dosHdr->pe_offset); const auto* peHdr = safe_cast(data, fileOffset); if (peHdr->signature != 0x00004550) - throw std::runtime_error("Invalid PE header signature"); + throw LSFG::error("Invalid PE header signature"); // parse optional pe header fileOffset += sizeof(PEHeader); const auto* peOptHdr = safe_cast(data, fileOffset); if (peOptHdr->magic != 0x20B) - throw std::runtime_error("Unsupported PE format (not PE32+)"); + throw LSFG::error("Unsupported PE format (not PE32+)"); const auto& [rsrc_rva, rsrc_size] = peOptHdr->resource_table; // locate section containing resources @@ -137,13 +137,13 @@ std::unordered_map> DLL::parseDLL( break; } if (!rsrc_offset) - throw std::runtime_error("Failed to locate resource section"); + throw LSFG::error("Failed to locate resource section"); // parse resource directory fileOffset = rsrc_offset.value(); const auto* rsrcDir = safe_cast(data, fileOffset); if (rsrcDir->id_count < 3) - throw std::runtime_error("Incorrect resource directory"); + throw LSFG::error("Incorrect resource directory"); // find resource table with data type std::optional rsrc_tbl_offset; @@ -154,18 +154,18 @@ std::unordered_map> DLL::parseDLL( if (rsrcDirEntry.id != 10) // RT_RCDATA continue; if ((rsrcDirEntry.offset & 0x80000000) == 0) - throw std::runtime_error("Expected resource directory, but found data entry"); + throw LSFG::error("Expected resource directory, but found data entry"); rsrc_tbl_offset.emplace(rsrcDirEntry.offset & 0x7FFFFFFF); } if (!rsrc_tbl_offset) - throw std::runtime_error("Failed to locate RT_RCDATA directory"); + throw LSFG::error("Failed to locate RT_RCDATA directory"); // parse data type resource directory fileOffset = rsrc_offset.value() + rsrc_tbl_offset.value(); const auto* rsrcTbl = safe_cast(data, fileOffset); if (rsrcTbl->id_count < 1) - throw std::runtime_error("Incorrect RT_RCDATA directory"); + throw LSFG::error("Incorrect RT_RCDATA directory"); // collect all resources fileOffset += sizeof(ResourceDirectory); @@ -174,30 +174,30 @@ std::unordered_map> DLL::parseDLL( std::unordered_map> resources; for (const auto& rsrcTblEntry : rsrcTblEntries) { if ((rsrcTblEntry.offset & 0x80000000) == 0) - throw std::runtime_error("Expected resource directory, but found data entry"); + throw LSFG::error("Expected resource directory, but found data entry"); // skip over language directory fileOffset = rsrc_offset.value() + (rsrcTblEntry.offset & 0x7FFFFFFF); const auto* langDir = safe_cast(data, fileOffset); if (langDir->id_count < 1) - throw std::runtime_error("Incorrect language directory"); + throw LSFG::error("Incorrect language directory"); fileOffset += sizeof(ResourceDirectory); const auto* langDirEntry = safe_cast(data, fileOffset); if ((langDirEntry->offset & 0x80000000) != 0) - throw std::runtime_error("Expected resource data entry, but found directory"); + throw LSFG::error("Expected resource data entry, but found directory"); // parse resource data entry fileOffset = rsrc_offset.value() + (langDirEntry->offset & 0x7FFFFFFF); const auto* entry = safe_cast(data, fileOffset); if (entry->offset < rsrc_rva || entry->offset > (rsrc_rva + rsrc_size)) - throw std::runtime_error("Resource data entry points outside resource section"); + throw LSFG::error("Resource data entry points outside resource section"); // extract resource std::vector resource(entry->size); fileOffset = (entry->offset - rsrc_rva) + rsrc_offset.value(); if (fileOffset + entry->size > data.size()) - throw std::runtime_error("Resource data entry points outside file"); + throw LSFG::error("Resource data entry points outside file"); std::copy_n(&data.at(fileOffset), entry->size, resource.data()); resources.emplace(rsrcTblEntry.id, std::move(resource)); } diff --git a/framegen/src/trans/rsrc.cpp b/framegen/src/trans/rsrc.cpp index 9cd0ef0..e2260eb 100644 --- a/framegen/src/trans/rsrc.cpp +++ b/framegen/src/trans/rsrc.cpp @@ -1,10 +1,13 @@ #include "trans/rsrc.hpp" -#include "exception.hpp" #include "trans/dll.hpp" #include "vk/registry/shader_registry.hpp" +#include "exception.hpp" + +#include +#include #include #include -#include +#include #include using namespace Trans; @@ -56,7 +59,7 @@ void RSRC::loadResources( std::unordered_map> rsrcs; try { rsrcs = DLL::parseDLL(filename); - } catch (const std::runtime_error& e) { + } catch (const LSFG::error& e) { throw LSFG::error("Unable to parse Lossless.dll file", e); }