Implement library magic checking & error handling
This commit is contained in:
parent
fbe24aa0f8
commit
5bd23aa6ed
55
src/main.cpp
55
src/main.cpp
|
@ -4,12 +4,12 @@
|
||||||
#include <base/MmapFile.hpp>
|
#include <base/MmapFile.hpp>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <structs/BoltStructs.hpp>
|
#include <structs/BoltStructs.hpp>
|
||||||
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
std::vector<u8> BoltDecompress(u8* input, usize decompressedSize) {
|
std::vector<u8> BoltDecompress(u8* input, usize decompressedSize) {
|
||||||
|
@ -72,6 +72,41 @@ std::vector<u8> BoltDecompress(u8* input, usize decompressedSize) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// error code boilerplate. This should be moved later
|
||||||
|
enum class BoltErrc { InvalidMagic = 1 };
|
||||||
|
|
||||||
|
struct BoltErrorCategory : std::error_category {
|
||||||
|
const char* name() const noexcept override { return "boltio"; }
|
||||||
|
std::string message(i32 ev) const override {
|
||||||
|
switch(static_cast<BoltErrc>(ev)) {
|
||||||
|
case BoltErrc::InvalidMagic: return "invalid bolt library file magic";
|
||||||
|
default: return "unknown error";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const BoltErrorCategory boltCategory {};
|
||||||
|
|
||||||
|
std::error_code make_error_code(BoltErrc errc) {
|
||||||
|
return { static_cast<i32>(errc), boltCategory };
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace std {
|
||||||
|
template <>
|
||||||
|
struct is_error_code_enum<BoltErrc> : true_type {};
|
||||||
|
|
||||||
|
/// Custom formatter for std::error_code. As far as I know, this is
|
||||||
|
/// Not going to be added in C++23, despite the fact fmt already has a specialization for it,
|
||||||
|
/// so I have to implement one myself. At least it's not that awful to.
|
||||||
|
template <>
|
||||||
|
struct formatter<error_code> : formatter<string_view> {
|
||||||
|
template <typename FormatContext>
|
||||||
|
inline auto format(const error_code& ec, FormatContext& ctx) const {
|
||||||
|
return std::format_to(ctx.out(), "{} [{}.{}]", ec.message(), ec.category().name(), ec.value());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
struct BoltReader {
|
struct BoltReader {
|
||||||
struct ParsedTableEntry {
|
struct ParsedTableEntry {
|
||||||
std::string_view filename;
|
std::string_view filename;
|
||||||
|
@ -98,6 +133,15 @@ struct BoltReader {
|
||||||
BoltReader() {}
|
BoltReader() {}
|
||||||
|
|
||||||
ErrorOr<void> OpenBolt(const fs::path& path) {
|
ErrorOr<void> OpenBolt(const fs::path& path) {
|
||||||
|
// Load the BOLT file
|
||||||
|
if(auto error = boltFile.Open(path); error.HasError())
|
||||||
|
return error;
|
||||||
|
|
||||||
|
lib = std::bit_cast<lightningbolt::BoltLibraryHeader*>(boltFile.GetMapping());
|
||||||
|
|
||||||
|
if(!lib->Validate())
|
||||||
|
return make_error_code(BoltErrc::InvalidMagic);
|
||||||
|
|
||||||
auto p = path;
|
auto p = path;
|
||||||
p.replace_filename("SLUS_201.14");
|
p.replace_filename("SLUS_201.14");
|
||||||
|
|
||||||
|
@ -107,11 +151,6 @@ struct BoltReader {
|
||||||
// Load table entries.
|
// Load table entries.
|
||||||
GetTableEntries();
|
GetTableEntries();
|
||||||
|
|
||||||
// Load the BOLT file
|
|
||||||
if(auto error = boltFile.Open(path); error.HasError())
|
|
||||||
return error;
|
|
||||||
|
|
||||||
lib = std::bit_cast<lightningbolt::BoltLibraryHeader*>(boltFile.GetMapping());
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +208,7 @@ struct BoltReader {
|
||||||
int main() {
|
int main() {
|
||||||
BoltReader reader;
|
BoltReader reader;
|
||||||
if(auto error = reader.OpenBolt(fs::current_path() / "ASSETS.BLT"); error.HasError()) {
|
if(auto error = reader.OpenBolt(fs::current_path() / "ASSETS.BLT"); error.HasError()) {
|
||||||
std::cout << "Error opening Bolt file: " << error.Error();
|
std::cout << std::format("Error initalizing bolt reader: {}\n", error.Error());
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.ForEachFile([](BoltReader::ParsedTableEntry& ent) {
|
reader.ForEachFile([](BoltReader::ParsedTableEntry& ent) {
|
||||||
|
|
Loading…
Reference in New Issue