refactor: fixing imports

This commit is contained in:
PancakeTAS 2025-09-26 17:22:19 +02:00
parent cfbc105b9d
commit 0bdc70a4c3
No known key found for this signature in database
6 changed files with 30 additions and 22 deletions

View file

@ -14,9 +14,11 @@ project(lsfg-vk-framegen
LANGUAGES C CXX) LANGUAGES C CXX)
file(GLOB SOURCES file(GLOB SOURCES
"src/trans/*.cpp"
"src/vk/core/*.cpp" "src/vk/core/*.cpp"
"src/vk/pool/*.cpp" "src/vk/pool/*.cpp"
"src/vk/registry/*.cpp" "src/vk/registry/*.cpp"
"src/vk/types/*.cpp"
"src/vk/*.cpp" "src/vk/*.cpp"
"src/*.cpp" "src/*.cpp"
"src/thirdparty/*.c" "src/thirdparty/*.c"

View file

@ -13,7 +13,7 @@ namespace Trans::DLL {
/// @param filename Path to the DLL file. /// @param filename Path to the DLL file.
/// @return A map of resource IDs to their binary data. /// @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<uint32_t, std::vector<uint8_t>> parseDLL( std::unordered_map<uint32_t, std::vector<uint8_t>> parseDLL(
const std::filesystem::path& filename); const std::filesystem::path& filename);

View file

@ -1,5 +1,6 @@
#include "exception.hpp" #include "exception.hpp"
#include <exception>
#include <stdexcept> #include <stdexcept>
#include <format> #include <format>
#include <string> #include <string>

View file

@ -1,6 +1,8 @@
#include "lsfg.hpp" #include "lsfg.hpp"
#include "trans/rsrc.hpp" #include "trans/rsrc.hpp"
#include <filesystem>
using namespace LSFG; using namespace LSFG;
Instance::Instance(const std::filesystem::path& dll) Instance::Instance(const std::filesystem::path& dll)

View file

@ -1,8 +1,8 @@
#include "trans/dll.hpp" #include "trans/dll.hpp"
#include "exception.hpp"
#include <unordered_map> #include <unordered_map>
#include <filesystem> #include <filesystem>
#include <stdexcept>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <optional> #include <optional>
@ -78,7 +78,7 @@ namespace {
const T* safe_cast(const std::vector<uint8_t>& data, size_t offset) { const T* safe_cast(const std::vector<uint8_t>& data, size_t offset) {
const size_t end = offset + sizeof(T); const size_t end = offset + sizeof(T);
if (end > data.size() || end < offset) 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<const T*>(&data.at(offset)); return reinterpret_cast<const T*>(&data.at(offset));
} }
@ -87,7 +87,7 @@ namespace {
std::span<const T> span_cast(const std::vector<uint8_t>& data, size_t offset, size_t count) { std::span<const T> span_cast(const std::vector<uint8_t>& data, size_t offset, size_t count) {
const size_t end = offset + (count * sizeof(T)); const size_t end = offset + (count * sizeof(T));
if (end > data.size() || end < offset) 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<const T>(reinterpret_cast<const T*>(&data.at(offset)), count); return std::span<const T>(reinterpret_cast<const T*>(&data.at(offset)), count);
} }
} }
@ -97,32 +97,32 @@ std::unordered_map<uint32_t, std::vector<uint8_t>> DLL::parseDLL(
const std::filesystem::path& filename) { const std::filesystem::path& filename) {
std::ifstream file(filename, std::ios::binary | std::ios::ate); std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (!file.is_open()) 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(); const auto size = file.tellg();
file.seekg(0, std::ios::beg); file.seekg(0, std::ios::beg);
std::vector<uint8_t> data(static_cast<size_t>(size)); std::vector<uint8_t> data(static_cast<size_t>(size));
if (!file.read(reinterpret_cast<char*>(data.data()), size)) if (!file.read(reinterpret_cast<char*>(data.data()), size))
throw std::runtime_error("Failed to read Lossless.dll"); throw LSFG::error("Failed to read Lossless.dll");
// parse dos header // parse dos header
size_t fileOffset = 0; size_t fileOffset = 0;
const auto* dosHdr = safe_cast<const DOSHeader>(data, 0); const auto* dosHdr = safe_cast<const DOSHeader>(data, 0);
if (dosHdr->magic != 0x5A4D) if (dosHdr->magic != 0x5A4D)
throw std::runtime_error("Invalid DOS header magic number"); throw LSFG::error("Invalid DOS header magic number");
// parse pe header // parse pe header
fileOffset += static_cast<size_t>(dosHdr->pe_offset); fileOffset += static_cast<size_t>(dosHdr->pe_offset);
const auto* peHdr = safe_cast<const PEHeader>(data, fileOffset); const auto* peHdr = safe_cast<const PEHeader>(data, fileOffset);
if (peHdr->signature != 0x00004550) if (peHdr->signature != 0x00004550)
throw std::runtime_error("Invalid PE header signature"); throw LSFG::error("Invalid PE header signature");
// parse optional pe header // parse optional pe header
fileOffset += sizeof(PEHeader); fileOffset += sizeof(PEHeader);
const auto* peOptHdr = safe_cast<const PEOptionalHeader>(data, fileOffset); const auto* peOptHdr = safe_cast<const PEOptionalHeader>(data, fileOffset);
if (peOptHdr->magic != 0x20B) 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; const auto& [rsrc_rva, rsrc_size] = peOptHdr->resource_table;
// locate section containing resources // locate section containing resources
@ -137,13 +137,13 @@ std::unordered_map<uint32_t, std::vector<uint8_t>> DLL::parseDLL(
break; break;
} }
if (!rsrc_offset) if (!rsrc_offset)
throw std::runtime_error("Failed to locate resource section"); throw LSFG::error("Failed to locate resource section");
// parse resource directory // parse resource directory
fileOffset = rsrc_offset.value(); fileOffset = rsrc_offset.value();
const auto* rsrcDir = safe_cast<const ResourceDirectory>(data, fileOffset); const auto* rsrcDir = safe_cast<const ResourceDirectory>(data, fileOffset);
if (rsrcDir->id_count < 3) if (rsrcDir->id_count < 3)
throw std::runtime_error("Incorrect resource directory"); throw LSFG::error("Incorrect resource directory");
// find resource table with data type // find resource table with data type
std::optional<size_t> rsrc_tbl_offset; std::optional<size_t> rsrc_tbl_offset;
@ -154,18 +154,18 @@ std::unordered_map<uint32_t, std::vector<uint8_t>> DLL::parseDLL(
if (rsrcDirEntry.id != 10) // RT_RCDATA if (rsrcDirEntry.id != 10) // RT_RCDATA
continue; continue;
if ((rsrcDirEntry.offset & 0x80000000) == 0) 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); rsrc_tbl_offset.emplace(rsrcDirEntry.offset & 0x7FFFFFFF);
} }
if (!rsrc_tbl_offset) 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 // parse data type resource directory
fileOffset = rsrc_offset.value() + rsrc_tbl_offset.value(); fileOffset = rsrc_offset.value() + rsrc_tbl_offset.value();
const auto* rsrcTbl = safe_cast<const ResourceDirectory>(data, fileOffset); const auto* rsrcTbl = safe_cast<const ResourceDirectory>(data, fileOffset);
if (rsrcTbl->id_count < 1) if (rsrcTbl->id_count < 1)
throw std::runtime_error("Incorrect RT_RCDATA directory"); throw LSFG::error("Incorrect RT_RCDATA directory");
// collect all resources // collect all resources
fileOffset += sizeof(ResourceDirectory); fileOffset += sizeof(ResourceDirectory);
@ -174,30 +174,30 @@ std::unordered_map<uint32_t, std::vector<uint8_t>> DLL::parseDLL(
std::unordered_map<uint32_t, std::vector<uint8_t>> resources; std::unordered_map<uint32_t, std::vector<uint8_t>> resources;
for (const auto& rsrcTblEntry : rsrcTblEntries) { for (const auto& rsrcTblEntry : rsrcTblEntries) {
if ((rsrcTblEntry.offset & 0x80000000) == 0) 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 // skip over language directory
fileOffset = rsrc_offset.value() + (rsrcTblEntry.offset & 0x7FFFFFFF); fileOffset = rsrc_offset.value() + (rsrcTblEntry.offset & 0x7FFFFFFF);
const auto* langDir = safe_cast<const ResourceDirectory>(data, fileOffset); const auto* langDir = safe_cast<const ResourceDirectory>(data, fileOffset);
if (langDir->id_count < 1) if (langDir->id_count < 1)
throw std::runtime_error("Incorrect language directory"); throw LSFG::error("Incorrect language directory");
fileOffset += sizeof(ResourceDirectory); fileOffset += sizeof(ResourceDirectory);
const auto* langDirEntry = safe_cast<const ResourceDirectoryEntry>(data, fileOffset); const auto* langDirEntry = safe_cast<const ResourceDirectoryEntry>(data, fileOffset);
if ((langDirEntry->offset & 0x80000000) != 0) 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 // parse resource data entry
fileOffset = rsrc_offset.value() + (langDirEntry->offset & 0x7FFFFFFF); fileOffset = rsrc_offset.value() + (langDirEntry->offset & 0x7FFFFFFF);
const auto* entry = safe_cast<const ResourceDataEntry>(data, fileOffset); const auto* entry = safe_cast<const ResourceDataEntry>(data, fileOffset);
if (entry->offset < rsrc_rva || entry->offset > (rsrc_rva + rsrc_size)) 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 // extract resource
std::vector<uint8_t> resource(entry->size); std::vector<uint8_t> resource(entry->size);
fileOffset = (entry->offset - rsrc_rva) + rsrc_offset.value(); fileOffset = (entry->offset - rsrc_rva) + rsrc_offset.value();
if (fileOffset + entry->size > data.size()) 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()); std::copy_n(&data.at(fileOffset), entry->size, resource.data());
resources.emplace(rsrcTblEntry.id, std::move(resource)); resources.emplace(rsrcTblEntry.id, std::move(resource));
} }

View file

@ -1,10 +1,13 @@
#include "trans/rsrc.hpp" #include "trans/rsrc.hpp"
#include "exception.hpp"
#include "trans/dll.hpp" #include "trans/dll.hpp"
#include "vk/registry/shader_registry.hpp" #include "vk/registry/shader_registry.hpp"
#include "exception.hpp"
#include <unordered_map>
#include <filesystem>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <filesystem> #include <string>
#include <vector> #include <vector>
using namespace Trans; using namespace Trans;
@ -56,7 +59,7 @@ void RSRC::loadResources(
std::unordered_map<uint32_t, std::vector<uint8_t>> rsrcs; std::unordered_map<uint32_t, std::vector<uint8_t>> rsrcs;
try { try {
rsrcs = DLL::parseDLL(filename); 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); throw LSFG::error("Unable to parse Lossless.dll file", e);
} }